Skip to content

Commit

Permalink
Add global rust_get_current_task
Browse files Browse the repository at this point in the history
Previously two methods existed: rust_sched_loop::get_task and rust_task::get_task_from_tcb. Merge both of them into one, trying the faster one (tcb) first, and if that fails, the slower one from the tls.
  • Loading branch information
jamorton authored and brson committed Apr 2, 2012
1 parent d0268cb commit 33a949e
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 101 deletions.
68 changes: 34 additions & 34 deletions src/rt/rust_builtin.cpp
Expand Up @@ -22,7 +22,7 @@ extern char **environ;

extern "C" CDECL rust_str*
last_os_error() {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();

LOG(task, task, "last_os_error()");

Expand Down Expand Up @@ -65,7 +65,7 @@ last_os_error() {

extern "C" CDECL rust_str *
rust_getcwd() {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
LOG(task, task, "rust_getcwd()");

char cbuf[BUF_BYTES];
Expand All @@ -85,7 +85,7 @@ rust_getcwd() {
#if defined(__WIN32__)
extern "C" CDECL rust_vec *
rust_env_pairs() {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
size_t envc = 0;
LPTCH ch = GetEnvironmentStringsA();
LPTCH c;
Expand All @@ -111,7 +111,7 @@ rust_env_pairs() {
#else
extern "C" CDECL rust_vec *
rust_env_pairs() {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
#ifdef __APPLE__
char **environ = *_NSGetEnviron();
#endif
Expand All @@ -133,21 +133,21 @@ refcount(intptr_t *v) {

extern "C" CDECL void
unsupervise() {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
task->unsupervise();
}

extern "C" CDECL void
vec_reserve_shared(type_desc* ty, rust_vec** vp,
size_t n_elts) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
reserve_vec_exact(task, vp, n_elts * ty->size);
}

extern "C" CDECL void
str_reserve_shared(rust_vec** sp,
size_t n_elts) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
reserve_vec_exact(task, sp, n_elts + 1);
}

Expand All @@ -157,7 +157,7 @@ str_reserve_shared(rust_vec** sp,
*/
extern "C" CDECL rust_vec*
vec_from_buf_shared(type_desc *ty, void *ptr, size_t count) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
size_t fill = ty->size * count;
rust_vec* v = (rust_vec*)task->kernel->malloc(fill + sizeof(rust_vec),
"vec_from_buf");
Expand All @@ -168,7 +168,7 @@ vec_from_buf_shared(type_desc *ty, void *ptr, size_t count) {

extern "C" CDECL void
rust_str_push(rust_vec** sp, uint8_t byte) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
size_t fill = (*sp)->fill;
reserve_vec(task, sp, fill + 1);
(*sp)->data[fill-1] = byte;
Expand All @@ -178,7 +178,7 @@ rust_str_push(rust_vec** sp, uint8_t byte) {

extern "C" CDECL void *
rand_new() {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
rust_sched_loop *thread = task->sched_loop;
randctx *rctx = (randctx *) task->malloc(sizeof(randctx), "randctx");
if (!rctx) {
Expand All @@ -196,7 +196,7 @@ rand_next(randctx *rctx) {

extern "C" CDECL void
rand_free(randctx *rctx) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
task->free(rctx);
}

Expand Down Expand Up @@ -242,22 +242,22 @@ debug_abi_2(floats f) {
static void
debug_tydesc_helper(type_desc *t)
{
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
LOG(task, stdlib, " size %" PRIdPTR ", align %" PRIdPTR
", first_param 0x%" PRIxPTR,
t->size, t->align, t->first_param);
}

extern "C" CDECL void
debug_tydesc(type_desc *t) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
LOG(task, stdlib, "debug_tydesc");
debug_tydesc_helper(t);
}

extern "C" CDECL void
debug_opaque(type_desc *t, uint8_t *front) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
LOG(task, stdlib, "debug_opaque");
debug_tydesc_helper(t);
// FIXME may want to actually account for alignment. `front` may not
Expand All @@ -277,7 +277,7 @@ struct rust_box {

extern "C" CDECL void
debug_box(type_desc *t, rust_box *box) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
LOG(task, stdlib, "debug_box(0x%" PRIxPTR ")", box);
debug_tydesc_helper(t);
LOG(task, stdlib, " refcount %" PRIdPTR,
Expand All @@ -294,7 +294,7 @@ struct rust_tag {

extern "C" CDECL void
debug_tag(type_desc *t, rust_tag *tag) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();

LOG(task, stdlib, "debug_tag");
debug_tydesc_helper(t);
Expand All @@ -312,7 +312,7 @@ struct rust_fn {

extern "C" CDECL void
debug_fn(type_desc *t, rust_fn *fn) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
LOG(task, stdlib, "debug_fn");
debug_tydesc_helper(t);
LOG(task, stdlib, " thunk at 0x%" PRIxPTR, fn->thunk);
Expand All @@ -326,7 +326,7 @@ extern "C" CDECL void *
debug_ptrcast(type_desc *from_ty,
type_desc *to_ty,
void *ptr) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
LOG(task, stdlib, "debug_ptrcast from");
debug_tydesc_helper(from_ty);
LOG(task, stdlib, "to");
Expand All @@ -336,13 +336,13 @@ debug_ptrcast(type_desc *from_ty,

extern "C" CDECL void *
debug_get_stk_seg() {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
return task->stk;
}

extern "C" CDECL rust_vec*
rust_list_files(rust_str *path) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
array_list<rust_str*> strings;
#if defined(__WIN32__)
WIN32_FIND_DATA FindFileData;
Expand Down Expand Up @@ -443,20 +443,20 @@ precise_time_ns(uint64_t *ns) {

extern "C" CDECL rust_sched_id
rust_get_sched_id() {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
return task->sched->get_id();
}

extern "C" CDECL rust_sched_id
rust_new_sched(uintptr_t threads) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
assert(threads > 0 && "Can't create a scheduler with no threads, silly!");
return task->kernel->create_scheduler(threads);
}

extern "C" CDECL rust_task_id
get_task_id() {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
return task->id;
}

Expand All @@ -467,13 +467,13 @@ new_task_common(rust_scheduler *sched, rust_task *parent) {

extern "C" CDECL rust_task*
new_task() {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
return new_task_common(task->sched, task);
}

extern "C" CDECL rust_task*
rust_new_task_in_sched(rust_sched_id id) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
rust_scheduler *sched = task->kernel->get_scheduler_by_id(id);
// FIXME: What if we didn't get the scheduler?
return new_task_common(sched, task);
Expand All @@ -486,7 +486,7 @@ rust_task_config_notify(rust_task *target, rust_port_id *port) {

extern "C" rust_task *
rust_get_task() {
return rust_sched_loop::get_task();
return rust_get_current_task();
}

extern "C" CDECL void
Expand All @@ -496,13 +496,13 @@ start_task(rust_task *target, fn_env_pair *f) {

extern "C" CDECL int
sched_threads() {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
return task->sched->number_of_threads();
}

extern "C" CDECL rust_port*
new_port(size_t unit_sz) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
LOG(task, comm, "new_port(task=0x%" PRIxPTR " (%s), unit_sz=%d)",
(uintptr_t) task, task->name, unit_sz);
// port starts with refcount == 1
Expand All @@ -511,7 +511,7 @@ new_port(size_t unit_sz) {

extern "C" CDECL void
rust_port_begin_detach(rust_port *port, uintptr_t *yield) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
LOG(task, comm, "rust_port_detach(0x%" PRIxPTR ")", (uintptr_t) port);
port->begin_detach(yield);
}
Expand All @@ -523,7 +523,7 @@ rust_port_end_detach(rust_port *port) {

extern "C" CDECL void
del_port(rust_port *port) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
LOG(task, comm, "del_port(0x%" PRIxPTR ")", (uintptr_t) port);
delete port;
}
Expand All @@ -541,7 +541,7 @@ get_port_id(rust_port *port) {
extern "C" CDECL uintptr_t
rust_port_id_send(type_desc *t, rust_port_id target_port_id, void *sptr) {
bool sent = false;
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();

LOG(task, comm, "rust_port_id*_send port: 0x%" PRIxPTR,
(uintptr_t) target_port_id);
Expand Down Expand Up @@ -572,14 +572,14 @@ port_recv(uintptr_t *dptr, rust_port *port, uintptr_t *yield) {
extern "C" CDECL void
rust_port_select(rust_port **dptr, rust_port **ports,
size_t n_ports, uintptr_t *yield) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
rust_port_selector *selector = task->get_port_selector();
selector->select(task, dptr, ports, n_ports, yield);
}

extern "C" CDECL void
rust_set_exit_status(intptr_t code) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
task->kernel->set_exit_status((int)code);
}

Expand All @@ -594,7 +594,7 @@ extern void log_console_off(rust_env *env);

extern "C" CDECL void
rust_log_console_off() {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
log_console_off(task->kernel->env);
}

Expand Down
43 changes: 7 additions & 36 deletions src/rt/rust_sched_loop.h
Expand Up @@ -38,13 +38,6 @@ struct rust_sched_loop

const int id;

#ifndef __WIN32__
static pthread_key_t task_key;
#else
static DWORD task_key;
#endif

static bool tls_initialized;
context c_context;

bool should_exit;
Expand All @@ -69,6 +62,13 @@ struct rust_sched_loop
public:
rust_kernel *kernel;
rust_scheduler *sched;
static bool tls_initialized;

#ifndef __WIN32__
static pthread_key_t task_key;
#else
static DWORD task_key;
#endif

// NB: this is used to filter *runtime-originating* debug
// logging, on a per-scheduler basis. It's not likely what
Expand Down Expand Up @@ -116,8 +116,6 @@ struct rust_sched_loop
void init_tls();
void place_task_in_tls(rust_task *task);

static rust_task *get_task();

// Called by each task when they are ready to be destroyed
void release_task(rust_task *task);

Expand All @@ -134,33 +132,6 @@ rust_sched_loop::get_log() {
return _log;
}

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

#ifndef __WIN32__

inline rust_task *
rust_sched_loop::get_task() {
if (!tls_initialized)
return NULL;
rust_task *task = reinterpret_cast<rust_task *>
(pthread_getspecific(task_key));
assert(task && "Couldn't get the task from TLS!");
return task;
}

#else

inline rust_task *
rust_sched_loop::get_task() {
if (!tls_initialized)
return NULL;
rust_task *task = reinterpret_cast<rust_task *>(TlsGetValue(task_key));
assert(task && "Couldn't get the task from TLS!");
return task;
}

#endif

// NB: Runs on the Rust stack
inline stk_seg *
rust_sched_loop::borrow_c_stack() {
Expand Down
6 changes: 3 additions & 3 deletions src/rt/rust_shape.cpp
Expand Up @@ -552,7 +552,7 @@ extern "C" void
shape_cmp_type(int8_t *result, const type_desc *tydesc,
const type_desc **subtydescs, uint8_t *data_0,
uint8_t *data_1, uint8_t cmp_type) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();
shape::arena arena;

// FIXME: This may well be broken when comparing two closures or objects
Expand All @@ -573,7 +573,7 @@ shape_cmp_type(int8_t *result, const type_desc *tydesc,

extern "C" rust_str *
shape_log_str(const type_desc *tydesc, uint8_t *data) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();

shape::arena arena;
shape::type_param *params =
Expand All @@ -591,7 +591,7 @@ shape_log_str(const type_desc *tydesc, uint8_t *data) {

extern "C" void
shape_log_type(const type_desc *tydesc, uint8_t *data, uint32_t level) {
rust_task *task = rust_sched_loop::get_task();
rust_task *task = rust_get_current_task();

shape::arena arena;
shape::type_param *params =
Expand Down

0 comments on commit 33a949e

Please sign in to comment.