Skip to content

Commit b2a075e

Browse files
committed
rt: Protect rust_task::state with a lock
1 parent d7298a7 commit b2a075e

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/rt/rust_task.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +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-
state(state),
7877
cond(NULL),
7978
cond_name("none"),
8079
list_index(-1),
@@ -87,6 +86,7 @@ rust_task::rust_task(rust_task_thread *thread, rust_task_list *state,
8786
dynastack(this),
8887
cc_counter(0),
8988
total_stack_sz(0),
89+
state(state),
9090
killed(false),
9191
reentered_rust_stack(false),
9292
c_stack(NULL),
@@ -355,12 +355,14 @@ rust_task::get_frame_glue_fns(uintptr_t fp) {
355355
bool
356356
rust_task::running()
357357
{
358+
scoped_lock with(state_lock);
358359
return state == &thread->running_tasks;
359360
}
360361

361362
bool
362363
rust_task::blocked()
363364
{
365+
scoped_lock with(state_lock);
364366
return state == &thread->blocked_tasks;
365367
}
366368

@@ -373,6 +375,7 @@ rust_task::blocked_on(rust_cond *on)
373375
bool
374376
rust_task::dead()
375377
{
378+
scoped_lock with(state_lock);
376379
return state == &thread->dead_tasks;
377380
}
378381

@@ -407,7 +410,10 @@ rust_task::transition(rust_task_list *src, rust_task_list *dst) {
407410
I(thread, state == src);
408411
src->remove(this);
409412
dst->append(this);
410-
state = dst;
413+
{
414+
scoped_lock with(state_lock);
415+
state = dst;
416+
}
411417
thread->lock.signal();
412418
if(unlock)
413419
thread->lock.unlock();

src/rt/rust_task.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +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_task_list *state;
7271
rust_cond *cond;
7372
const char *cond_name;
7473
int32_t list_index;
@@ -107,6 +106,9 @@ rust_task : public kernel_owned<rust_task>, rust_cond
107106

108107
private:
109108

109+
lock_and_signal state_lock;
110+
rust_task_list *state;
111+
110112
// Protects the killed flag
111113
lock_and_signal kill_lock;
112114
// Indicates that the task was killed and needs to unwind
@@ -218,6 +220,8 @@ rust_task : public kernel_owned<rust_task>, rust_cond
218220
bool have_c_stack() { return c_stack != NULL; }
219221

220222
rust_port_selector *get_port_selector() { return &port_selector; }
223+
224+
rust_task_list *get_state() { return state; }
221225
};
222226

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

src/rt/rust_task_thread.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ rust_task_thread::start_main_loop() {
259259
", state: %s",
260260
scheduled_task->name,
261261
(uintptr_t)scheduled_task,
262-
scheduled_task->state->name);
262+
scheduled_task->get_state()->name);
263263

264264
place_task_in_tls(scheduled_task);
265265

@@ -273,7 +273,7 @@ rust_task_thread::start_main_loop() {
273273
" in state '%s', worker id=%d" PRIxPTR,
274274
scheduled_task->name,
275275
(uintptr_t)scheduled_task,
276-
scheduled_task->state->name,
276+
scheduled_task->get_state()->name,
277277
id);
278278

279279
reap_dead_tasks();

0 commit comments

Comments
 (0)