Skip to content

Commit 5c67905

Browse files
author
Eric Holk
committed
Reducing the chances for race conditions in join.
1 parent 55c9842 commit 5c67905

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

src/rt/rust_internal.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ static size_t const BUF_BYTES = 2048;
115115
private: \
116116
intptr_t ref_count; \
117117
public: \
118-
void ref() { sync::increment(ref_count); } \
118+
void ref() { \
119+
intptr_t old = sync::increment(ref_count); \
120+
assert(old > 0); \
121+
} \
119122
void deref() { if(0 == sync::decrement(ref_count)) { delete this; } }
120123

121124
template <typename T> struct rc_base {

src/rt/rust_kernel.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,17 @@ rust_kernel::get_task_by_id(rust_task_id id) {
160160
rust_task *task = NULL;
161161
// get leaves task unchanged if not found.
162162
task_table.get(id, &task);
163-
if(task) task->ref();
163+
if(task) {
164+
if(task->get_ref_count() == 0) {
165+
// this means the destructor is running, since the destructor
166+
// grabs the kernel lock to unregister the task. Pretend this
167+
// doesn't actually exist.
168+
return NULL;
169+
}
170+
else {
171+
task->ref();
172+
}
173+
}
164174
return task;
165175
}
166176

src/rt/rust_task.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ rust_task : public kernel_owned<rust_task>, rust_cond
169169
rust_port_id register_port(rust_port *port);
170170
void release_port(rust_port_id id);
171171
rust_port *get_port_by_id(rust_port_id id);
172+
173+
// Use this function sparingly. Depending on the ref count is generally
174+
// not at all safe.
175+
intptr_t get_ref_count() const { return ref_count; }
172176
};
173177

174178
//

src/test/bench/task-perf-spawnalot.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ fn main(args: vec[str]) {
2727
task::_spawn(bind f(n));
2828
i += 1u;
2929
}
30-
}
30+
}

0 commit comments

Comments
 (0)