Skip to content

Commit

Permalink
Cleanup, refactoring, and some runtime tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbebenita committed Sep 10, 2010
1 parent f985fde commit a493350
Show file tree
Hide file tree
Showing 22 changed files with 279 additions and 129 deletions.
4 changes: 4 additions & 0 deletions src/Makefile
Expand Up @@ -257,6 +257,7 @@ RUNTIME_CS := rt/sync/timer.cpp \
rt/rust_comm.cpp \
rt/rust_dom.cpp \
rt/rust_task.cpp \
rt/rust_task_list.cpp \
rt/rust_proxy.cpp \
rt/rust_chan.cpp \
rt/rust_port.cpp \
Expand All @@ -270,6 +271,7 @@ RUNTIME_CS := rt/sync/timer.cpp \
rt/rust_kernel.cpp \
rt/memory_region.cpp \
rt/test/rust_test_harness.cpp \
rt/test/rust_test_runtime.cpp \
rt/test/rust_test_util.cpp

RUNTIME_HDR := rt/globals.h \
Expand All @@ -281,6 +283,7 @@ RUNTIME_HDR := rt/globals.h \
rt/rust_port.h \
rt/rust_dom.h \
rt/rust_task.h \
rt/rust_task_list.h \
rt/rust_proxy.h \
rt/rust_log.h \
rt/rust_message.h \
Expand All @@ -297,6 +300,7 @@ RUNTIME_HDR := rt/globals.h \
rt/memory_region.h \
rt/memory.h \
rt/test/rust_test_harness.h \
rt/test/rust_test_runtime.h \
rt/test/rust_test_util.h

RUNTIME_INCS := -Irt/isaac -Irt/uthash
Expand Down
93 changes: 28 additions & 65 deletions src/rt/rust_dom.cpp
Expand Up @@ -2,8 +2,6 @@
#include <stdarg.h>
#include "rust_internal.h"

template class ptr_vec<rust_task>;

rust_dom::rust_dom(rust_kernel *kernel,
rust_message_queue *message_queue, rust_srv *srv,
rust_crate const *root_crate, const char *name) :
Expand All @@ -14,9 +12,10 @@ rust_dom::rust_dom(rust_kernel *kernel,
local_region(&srv->local_region),
synchronized_region(&srv->synchronized_region),
name(name),
running_tasks(this),
blocked_tasks(this),
dead_tasks(this),
newborn_tasks(this, "newborn"),
running_tasks(this, "running"),
blocked_tasks(this, "blocked"),
dead_tasks(this, "dead"),
caches(this),
root_task(NULL),
curr_task(NULL),
Expand All @@ -31,28 +30,16 @@ rust_dom::rust_dom(rust_kernel *kernel,
pthread_attr_setstacksize(&attr, 1024 * 1024);
pthread_attr_setdetachstate(&attr, true);
#endif
root_task = new (this) rust_task(this, NULL, name);
}

static void
del_all_tasks(rust_dom *dom, ptr_vec<rust_task> *v) {
I(dom, v);
while (v->length()) {
dom->log(rust_log::TASK, "deleting task 0x%" PRIdPTR,
v->length() - 1);
delete v->pop();
}
root_task = create_task(NULL, name);
}

rust_dom::~rust_dom() {
log(rust_log::MEM | rust_log::DOM,
"~rust_dom %s @0x%" PRIxPTR, name, (uintptr_t)this);
log(rust_log::TASK, "deleting all running tasks");
del_all_tasks(this, &running_tasks);
log(rust_log::TASK, "deleting all blocked tasks");
del_all_tasks(this, &blocked_tasks);
log(rust_log::TASK, "deleting all dead tasks");
del_all_tasks(this, &dead_tasks);
newborn_tasks.delete_all();
running_tasks.delete_all();
blocked_tasks.delete_all();
dead_tasks.delete_all();
#ifndef __WIN32__
pthread_attr_destroy(&attr);
#endif
Expand Down Expand Up @@ -198,42 +185,10 @@ rust_dom::win32_require(LPCTSTR fn, BOOL ok) {
#endif

size_t
rust_dom::n_live_tasks()
{
rust_dom::number_of_live_tasks() {
return running_tasks.length() + blocked_tasks.length();
}

void
rust_dom::add_task_to_state_vec(ptr_vec<rust_task> *v, rust_task *task)
{
log(rust_log::MEM|rust_log::TASK,
"adding task %s @0x%" PRIxPTR " in state '%s' to vec 0x%" PRIxPTR,
task->name, (uintptr_t)task, state_vec_name(v), (uintptr_t)v);
v->push(task);
}


void
rust_dom::remove_task_from_state_vec(ptr_vec<rust_task> *v, rust_task *task)
{
log(rust_log::MEM|rust_log::TASK,
"removing task %s @0x%" PRIxPTR " in state '%s' from vec 0x%" PRIxPTR,
task->name, (uintptr_t)task, state_vec_name(v), (uintptr_t)v);
I(this, (*v)[task->idx] == task);
v->swap_delete(task);
}

const char *
rust_dom::state_vec_name(ptr_vec<rust_task> *v)
{
if (v == &running_tasks)
return "running";
if (v == &blocked_tasks)
return "blocked";
I(this, v == &dead_tasks);
return "dead";
}

/**
* Delete any dead tasks.
*/
Expand All @@ -243,7 +198,7 @@ rust_dom::reap_dead_tasks() {
rust_task *task = dead_tasks[i];
if (task->ref_count == 0) {
I(this, task->tasks_waiting_to_join.is_empty());
dead_tasks.swap_delete(task);
dead_tasks.remove(task);
log(rust_log::TASK,
"deleting unreferenced dead task %s @0x%" PRIxPTR,
task->name, task);
Expand Down Expand Up @@ -288,7 +243,6 @@ rust_dom::schedule_task() {
return (rust_task *)running_tasks[i];
}
}
// log(rust_log::DOM|rust_log::TASK, "no schedulable tasks");
return NULL;
}

Expand Down Expand Up @@ -334,16 +288,16 @@ rust_dom::log_state() {
* drop to zero.
*/
int
rust_dom::start_main_loop()
{
rust_dom::start_main_loop() {
// Make sure someone is watching, to pull us out of infinite loops.
rust_timer timer(this);

log(rust_log::DOM, "running main-loop on domain %s @0x%" PRIxPTR,
name, this);
logptr("exit-task glue", root_crate->get_exit_task_glue());
log(rust_log::DOM, "started domain loop");
log(rust_log::DOM | rust_log::MEM,
"activate glue: " PTR ", exit glue: " PTR,
root_crate->get_activate_glue(), root_crate->get_exit_task_glue());

while (n_live_tasks() > 0) {
while (number_of_live_tasks() > 0) {
A(this, kernel->is_deadlocked() == false, "deadlock");

drain_incoming_message_queue(true);
Expand Down Expand Up @@ -377,7 +331,7 @@ rust_dom::start_main_loop()
(uintptr_t)scheduled_task,
scheduled_task->rust_sp,
scheduled_task->ref_count,
scheduled_task->state_str());
scheduled_task->state->name);

interrupt_flag = 0;

Expand All @@ -388,7 +342,7 @@ rust_dom::start_main_loop()
" in state '%s', sp=0x%" PRIxPTR,
scheduled_task->name,
(uintptr_t)scheduled_task,
state_vec_name(scheduled_task->state),
scheduled_task->state->name,
scheduled_task->rust_sp);

I(this, scheduled_task->rust_sp >=
Expand Down Expand Up @@ -443,6 +397,15 @@ rust_dom::get_cache(rust_crate const *crate) {
return cache;
}

rust_task *
rust_dom::create_task(rust_task *spawner, const char *name) {
rust_task *task =
new (this) rust_task (this, &newborn_tasks, spawner, name);
log(rust_log::TASK, "created task: " PTR ", spawner: %s, name: %s",
task, spawner ? spawner->name : "null", name);
newborn_tasks.append(task);
return task;
}

//
// Local Variables:
Expand Down
17 changes: 10 additions & 7 deletions src/rt/rust_dom.h
Expand Up @@ -18,10 +18,14 @@ struct rust_dom : public kernel_owned<rust_dom>, rc_base<rust_dom>
memory_region local_region;
memory_region synchronized_region;
const char *const name;
ptr_vec<rust_task> running_tasks;
ptr_vec<rust_task> blocked_tasks;
ptr_vec<rust_task> dead_tasks;

rust_task_list newborn_tasks;
rust_task_list running_tasks;
rust_task_list blocked_tasks;
rust_task_list dead_tasks;

ptr_vec<rust_crate_cache> caches;

randctx rctx;
rust_task *root_task;
rust_task *curr_task;
Expand Down Expand Up @@ -71,10 +75,7 @@ struct rust_dom : public kernel_owned<rust_dom>, rc_base<rust_dom>
#endif

rust_crate_cache *get_cache(rust_crate const *crate);
size_t n_live_tasks();
void add_task_to_state_vec(ptr_vec<rust_task> *v, rust_task *task);
void remove_task_from_state_vec(ptr_vec<rust_task> *v, rust_task *task);
const char *state_vec_name(ptr_vec<rust_task> *v);
size_t number_of_live_tasks();

void reap_dead_tasks();
rust_task *schedule_task();
Expand All @@ -83,6 +84,8 @@ struct rust_dom : public kernel_owned<rust_dom>, rc_base<rust_dom>

void log_state();
static void log_all_state();

rust_task *create_task(rust_task *spawner, const char *name);
};

//
Expand Down
7 changes: 4 additions & 3 deletions src/rt/rust_internal.h
Expand Up @@ -124,6 +124,8 @@ template <typename T> struct region_owned {
}
};

#include "rust_task_list.h"

// A cond(ition) is something we can block on. This can be a channel
// (writing), a port (reading) or a task (waiting).

Expand Down Expand Up @@ -203,9 +205,7 @@ crate_rel(rust_crate const *crate, T const *t) {

typedef void CDECL (*activate_glue_ty)(rust_task *);

class
rust_crate
{
class rust_crate {
// The following fields are emitted by the compiler for the static
// rust_crate object inside each compiled crate.

Expand Down Expand Up @@ -563,6 +563,7 @@ struct gc_alloc {

#include "test/rust_test_harness.h"
#include "test/rust_test_util.h"
#include "test/rust_test_runtime.h"

//
// Local Variables:
Expand Down
12 changes: 7 additions & 5 deletions src/rt/rust_kernel.cpp
Expand Up @@ -4,9 +4,7 @@ rust_kernel::rust_kernel(rust_srv *srv) :
_region(&srv->local_region),
_log(srv, NULL),
_srv(srv),
_interrupt_kernel_loop(FALSE),
domains(&srv->local_region),
message_queues(&srv->local_region) {
_interrupt_kernel_loop(FALSE) {
// Nop.
}

Expand All @@ -22,6 +20,9 @@ rust_kernel::create_domain(const rust_crate *crate, const char *name) {
message_queue->associate(handle);
domains.append(dom);
message_queues.append(message_queue);
log(rust_log::KERN | rust_log::TASK,
"created domain: " PTR ", name: %s, index: %d, domains %d",
dom, name, dom->list_index, domains.length());
_kernel_lock.signal_all();
_kernel_lock.unlock();
return handle;
Expand All @@ -30,8 +31,9 @@ rust_kernel::create_domain(const rust_crate *crate, const char *name) {
void
rust_kernel::destroy_domain(rust_dom *dom) {
_kernel_lock.lock();
log(rust_log::KERN, "deleting domain: " PTR ", index: %d, domains %d",
dom, dom->list_index, domains.length());
log(rust_log::KERN,
"deleting domain: " PTR ", name: %s, index: %d, domains %d",
dom, dom->name, dom->list_index, domains.length());
domains.remove(dom);
dom->message_queue->disassociate();
rust_srv *srv = dom->srv;
Expand Down

0 comments on commit a493350

Please sign in to comment.