From 237652299ec5fa3529ac7bd1bfddc52b7526bb82 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 2 Mar 2012 23:40:27 -0800 Subject: [PATCH] rt: Protect cond and cond_name with the state_lock --- src/rt/rust_task.cpp | 24 ++++++++++++------------ src/rt/rust_task.h | 10 +++++++--- src/rt/rust_task_thread.cpp | 3 ++- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/rt/rust_task.cpp b/src/rt/rust_task.cpp index 7b5b528782b..3c375f0b4c3 100644 --- a/src/rt/rust_task.cpp +++ b/src/rt/rust_task.cpp @@ -74,8 +74,6 @@ rust_task::rust_task(rust_task_thread *thread, rust_task_list *state, cache(NULL), kernel(thread->kernel), name(name), - cond(NULL), - cond_name("none"), list_index(-1), next_port_id(0), rendezvous_ptr(0), @@ -87,6 +85,8 @@ rust_task::rust_task(rust_task_thread *thread, rust_task_list *state, cc_counter(0), total_stack_sz(0), state(state), + cond(NULL), + cond_name("none"), killed(false), reentered_rust_stack(false), c_stack(NULL), @@ -242,7 +242,7 @@ rust_task::start(spawn_fn spawnee_fn, void rust_task::start() { - transition(&thread->newborn_tasks, &thread->running_tasks); + transition(&thread->newborn_tasks, &thread->running_tasks, NULL, "none"); } bool @@ -369,7 +369,8 @@ rust_task::blocked() bool rust_task::blocked_on(rust_cond *on) { - return blocked() && cond == on; + scoped_lock with(state_lock); + return cond == on; } bool @@ -398,7 +399,8 @@ rust_task::free(void *p) } void -rust_task::transition(rust_task_list *src, rust_task_list *dst) { +rust_task::transition(rust_task_list *src, rust_task_list *dst, + rust_cond *cond, const char* cond_name) { bool unlock = false; if(!thread->lock.lock_held_by_current_thread()) { unlock = true; @@ -413,6 +415,8 @@ rust_task::transition(rust_task_list *src, rust_task_list *dst) { { scoped_lock with(state_lock); state = dst; + this->cond = cond; + this->cond_name = cond_name; } thread->lock.signal(); if(unlock) @@ -426,9 +430,7 @@ rust_task::block(rust_cond *on, const char* name) { A(thread, cond == NULL, "Cannot block an already blocked task."); A(thread, on != NULL, "Cannot block on a NULL object."); - transition(&thread->running_tasks, &thread->blocked_tasks); - cond = on; - cond_name = name; + transition(&thread->running_tasks, &thread->blocked_tasks, on, name); } void @@ -438,14 +440,12 @@ rust_task::wakeup(rust_cond *from) { (uintptr_t) cond, (uintptr_t) from); A(thread, cond == from, "Cannot wake up blocked task on wrong condition."); - cond = NULL; - cond_name = "none"; - transition(&thread->blocked_tasks, &thread->running_tasks); + transition(&thread->blocked_tasks, &thread->running_tasks, NULL, "none"); } void rust_task::die() { - transition(&thread->running_tasks, &thread->dead_tasks); + transition(&thread->running_tasks, &thread->dead_tasks, NULL, "none"); } void diff --git a/src/rt/rust_task.h b/src/rt/rust_task.h index cc4a2a0ffe3..a940b6299ac 100644 --- a/src/rt/rust_task.h +++ b/src/rt/rust_task.h @@ -68,8 +68,6 @@ rust_task : public kernel_owned, rust_cond // Fields known only to the runtime. rust_kernel *kernel; const char *const name; - rust_cond *cond; - const char *cond_name; int32_t list_index; rust_port_id next_port_id; @@ -106,8 +104,11 @@ rust_task : public kernel_owned, rust_cond private: + // Protects state, cond, cond_name lock_and_signal state_lock; rust_task_list *state; + rust_cond *cond; + const char *cond_name; // Protects the killed flag lock_and_signal kill_lock; @@ -162,7 +163,8 @@ public: void *realloc(void *data, size_t sz); void free(void *p); - void transition(rust_task_list *src, rust_task_list *dst); + void transition(rust_task_list *src, rust_task_list *dst, + rust_cond *cond, const char* cond_name); void block(rust_cond *on, const char* name); void wakeup(rust_cond *from); @@ -222,6 +224,8 @@ public: rust_port_selector *get_port_selector() { return &port_selector; } rust_task_list *get_state() { return state; } + rust_cond *get_cond() { return cond; } + const char *get_cond_name() { return cond_name; } }; // This stuff is on the stack-switching fast path diff --git a/src/rt/rust_task_thread.cpp b/src/rt/rust_task_thread.cpp index cb05a986476..472b58f1671 100644 --- a/src/rt/rust_task_thread.cpp +++ b/src/rt/rust_task_thread.cpp @@ -209,7 +209,8 @@ rust_task_thread::log_state() { log(NULL, log_debug, "\t task: %s @0x%" PRIxPTR ", blocked on: 0x%" PRIxPTR " '%s'", blocked_tasks[i]->name, blocked_tasks[i], - blocked_tasks[i]->cond, blocked_tasks[i]->cond_name); + blocked_tasks[i]->get_cond(), + blocked_tasks[i]->get_cond_name()); } }