Skip to content

Commit 2376522

Browse files
committed
rt: Protect cond and cond_name with the state_lock
1 parent 0432030 commit 2376522

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

src/rt/rust_task.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,6 @@ rust_task::rust_task(rust_task_thread *thread, rust_task_list *state,
7474
cache(NULL),
7575
kernel(thread->kernel),
7676
name(name),
77-
cond(NULL),
78-
cond_name("none"),
7977
list_index(-1),
8078
next_port_id(0),
8179
rendezvous_ptr(0),
@@ -87,6 +85,8 @@ rust_task::rust_task(rust_task_thread *thread, rust_task_list *state,
8785
cc_counter(0),
8886
total_stack_sz(0),
8987
state(state),
88+
cond(NULL),
89+
cond_name("none"),
9090
killed(false),
9191
reentered_rust_stack(false),
9292
c_stack(NULL),
@@ -242,7 +242,7 @@ rust_task::start(spawn_fn spawnee_fn,
242242

243243
void rust_task::start()
244244
{
245-
transition(&thread->newborn_tasks, &thread->running_tasks);
245+
transition(&thread->newborn_tasks, &thread->running_tasks, NULL, "none");
246246
}
247247

248248
bool
@@ -369,7 +369,8 @@ rust_task::blocked()
369369
bool
370370
rust_task::blocked_on(rust_cond *on)
371371
{
372-
return blocked() && cond == on;
372+
scoped_lock with(state_lock);
373+
return cond == on;
373374
}
374375

375376
bool
@@ -398,7 +399,8 @@ rust_task::free(void *p)
398399
}
399400

400401
void
401-
rust_task::transition(rust_task_list *src, rust_task_list *dst) {
402+
rust_task::transition(rust_task_list *src, rust_task_list *dst,
403+
rust_cond *cond, const char* cond_name) {
402404
bool unlock = false;
403405
if(!thread->lock.lock_held_by_current_thread()) {
404406
unlock = true;
@@ -413,6 +415,8 @@ rust_task::transition(rust_task_list *src, rust_task_list *dst) {
413415
{
414416
scoped_lock with(state_lock);
415417
state = dst;
418+
this->cond = cond;
419+
this->cond_name = cond_name;
416420
}
417421
thread->lock.signal();
418422
if(unlock)
@@ -426,9 +430,7 @@ rust_task::block(rust_cond *on, const char* name) {
426430
A(thread, cond == NULL, "Cannot block an already blocked task.");
427431
A(thread, on != NULL, "Cannot block on a NULL object.");
428432

429-
transition(&thread->running_tasks, &thread->blocked_tasks);
430-
cond = on;
431-
cond_name = name;
433+
transition(&thread->running_tasks, &thread->blocked_tasks, on, name);
432434
}
433435

434436
void
@@ -438,14 +440,12 @@ rust_task::wakeup(rust_cond *from) {
438440
(uintptr_t) cond, (uintptr_t) from);
439441
A(thread, cond == from, "Cannot wake up blocked task on wrong condition.");
440442

441-
cond = NULL;
442-
cond_name = "none";
443-
transition(&thread->blocked_tasks, &thread->running_tasks);
443+
transition(&thread->blocked_tasks, &thread->running_tasks, NULL, "none");
444444
}
445445

446446
void
447447
rust_task::die() {
448-
transition(&thread->running_tasks, &thread->dead_tasks);
448+
transition(&thread->running_tasks, &thread->dead_tasks, NULL, "none");
449449
}
450450

451451
void

src/rt/rust_task.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ rust_task : public kernel_owned<rust_task>, rust_cond
6868
// Fields known only to the runtime.
6969
rust_kernel *kernel;
7070
const char *const name;
71-
rust_cond *cond;
72-
const char *cond_name;
7371
int32_t list_index;
7472

7573
rust_port_id next_port_id;
@@ -106,8 +104,11 @@ rust_task : public kernel_owned<rust_task>, rust_cond
106104

107105
private:
108106

107+
// Protects state, cond, cond_name
109108
lock_and_signal state_lock;
110109
rust_task_list *state;
110+
rust_cond *cond;
111+
const char *cond_name;
111112

112113
// Protects the killed flag
113114
lock_and_signal kill_lock;
@@ -162,7 +163,8 @@ rust_task : public kernel_owned<rust_task>, rust_cond
162163
void *realloc(void *data, size_t sz);
163164
void free(void *p);
164165

165-
void transition(rust_task_list *src, rust_task_list *dst);
166+
void transition(rust_task_list *src, rust_task_list *dst,
167+
rust_cond *cond, const char* cond_name);
166168

167169
void block(rust_cond *on, const char* name);
168170
void wakeup(rust_cond *from);
@@ -222,6 +224,8 @@ rust_task : public kernel_owned<rust_task>, rust_cond
222224
rust_port_selector *get_port_selector() { return &port_selector; }
223225

224226
rust_task_list *get_state() { return state; }
227+
rust_cond *get_cond() { return cond; }
228+
const char *get_cond_name() { return cond_name; }
225229
};
226230

227231
// This stuff is on the stack-switching fast path

src/rt/rust_task_thread.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,8 @@ rust_task_thread::log_state() {
209209
log(NULL, log_debug, "\t task: %s @0x%" PRIxPTR ", blocked on: 0x%"
210210
PRIxPTR " '%s'",
211211
blocked_tasks[i]->name, blocked_tasks[i],
212-
blocked_tasks[i]->cond, blocked_tasks[i]->cond_name);
212+
blocked_tasks[i]->get_cond(),
213+
blocked_tasks[i]->get_cond_name());
213214
}
214215
}
215216

0 commit comments

Comments
 (0)