Skip to content

Commit 33a949e

Browse files
jamortonbrson
authored andcommitted
Add global rust_get_current_task
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.
1 parent d0268cb commit 33a949e

File tree

6 files changed

+88
-101
lines changed

6 files changed

+88
-101
lines changed

Diff for: src/rt/rust_builtin.cpp

+34-34
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern char **environ;
2222

2323
extern "C" CDECL rust_str*
2424
last_os_error() {
25-
rust_task *task = rust_sched_loop::get_task();
25+
rust_task *task = rust_get_current_task();
2626

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

@@ -65,7 +65,7 @@ last_os_error() {
6565

6666
extern "C" CDECL rust_str *
6767
rust_getcwd() {
68-
rust_task *task = rust_sched_loop::get_task();
68+
rust_task *task = rust_get_current_task();
6969
LOG(task, task, "rust_getcwd()");
7070

7171
char cbuf[BUF_BYTES];
@@ -85,7 +85,7 @@ rust_getcwd() {
8585
#if defined(__WIN32__)
8686
extern "C" CDECL rust_vec *
8787
rust_env_pairs() {
88-
rust_task *task = rust_sched_loop::get_task();
88+
rust_task *task = rust_get_current_task();
8989
size_t envc = 0;
9090
LPTCH ch = GetEnvironmentStringsA();
9191
LPTCH c;
@@ -111,7 +111,7 @@ rust_env_pairs() {
111111
#else
112112
extern "C" CDECL rust_vec *
113113
rust_env_pairs() {
114-
rust_task *task = rust_sched_loop::get_task();
114+
rust_task *task = rust_get_current_task();
115115
#ifdef __APPLE__
116116
char **environ = *_NSGetEnviron();
117117
#endif
@@ -133,21 +133,21 @@ refcount(intptr_t *v) {
133133

134134
extern "C" CDECL void
135135
unsupervise() {
136-
rust_task *task = rust_sched_loop::get_task();
136+
rust_task *task = rust_get_current_task();
137137
task->unsupervise();
138138
}
139139

140140
extern "C" CDECL void
141141
vec_reserve_shared(type_desc* ty, rust_vec** vp,
142142
size_t n_elts) {
143-
rust_task *task = rust_sched_loop::get_task();
143+
rust_task *task = rust_get_current_task();
144144
reserve_vec_exact(task, vp, n_elts * ty->size);
145145
}
146146

147147
extern "C" CDECL void
148148
str_reserve_shared(rust_vec** sp,
149149
size_t n_elts) {
150-
rust_task *task = rust_sched_loop::get_task();
150+
rust_task *task = rust_get_current_task();
151151
reserve_vec_exact(task, sp, n_elts + 1);
152152
}
153153

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

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

179179
extern "C" CDECL void *
180180
rand_new() {
181-
rust_task *task = rust_sched_loop::get_task();
181+
rust_task *task = rust_get_current_task();
182182
rust_sched_loop *thread = task->sched_loop;
183183
randctx *rctx = (randctx *) task->malloc(sizeof(randctx), "randctx");
184184
if (!rctx) {
@@ -196,7 +196,7 @@ rand_next(randctx *rctx) {
196196

197197
extern "C" CDECL void
198198
rand_free(randctx *rctx) {
199-
rust_task *task = rust_sched_loop::get_task();
199+
rust_task *task = rust_get_current_task();
200200
task->free(rctx);
201201
}
202202

@@ -242,22 +242,22 @@ debug_abi_2(floats f) {
242242
static void
243243
debug_tydesc_helper(type_desc *t)
244244
{
245-
rust_task *task = rust_sched_loop::get_task();
245+
rust_task *task = rust_get_current_task();
246246
LOG(task, stdlib, " size %" PRIdPTR ", align %" PRIdPTR
247247
", first_param 0x%" PRIxPTR,
248248
t->size, t->align, t->first_param);
249249
}
250250

251251
extern "C" CDECL void
252252
debug_tydesc(type_desc *t) {
253-
rust_task *task = rust_sched_loop::get_task();
253+
rust_task *task = rust_get_current_task();
254254
LOG(task, stdlib, "debug_tydesc");
255255
debug_tydesc_helper(t);
256256
}
257257

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

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

295295
extern "C" CDECL void
296296
debug_tag(type_desc *t, rust_tag *tag) {
297-
rust_task *task = rust_sched_loop::get_task();
297+
rust_task *task = rust_get_current_task();
298298

299299
LOG(task, stdlib, "debug_tag");
300300
debug_tydesc_helper(t);
@@ -312,7 +312,7 @@ struct rust_fn {
312312

313313
extern "C" CDECL void
314314
debug_fn(type_desc *t, rust_fn *fn) {
315-
rust_task *task = rust_sched_loop::get_task();
315+
rust_task *task = rust_get_current_task();
316316
LOG(task, stdlib, "debug_fn");
317317
debug_tydesc_helper(t);
318318
LOG(task, stdlib, " thunk at 0x%" PRIxPTR, fn->thunk);
@@ -326,7 +326,7 @@ extern "C" CDECL void *
326326
debug_ptrcast(type_desc *from_ty,
327327
type_desc *to_ty,
328328
void *ptr) {
329-
rust_task *task = rust_sched_loop::get_task();
329+
rust_task *task = rust_get_current_task();
330330
LOG(task, stdlib, "debug_ptrcast from");
331331
debug_tydesc_helper(from_ty);
332332
LOG(task, stdlib, "to");
@@ -336,13 +336,13 @@ debug_ptrcast(type_desc *from_ty,
336336

337337
extern "C" CDECL void *
338338
debug_get_stk_seg() {
339-
rust_task *task = rust_sched_loop::get_task();
339+
rust_task *task = rust_get_current_task();
340340
return task->stk;
341341
}
342342

343343
extern "C" CDECL rust_vec*
344344
rust_list_files(rust_str *path) {
345-
rust_task *task = rust_sched_loop::get_task();
345+
rust_task *task = rust_get_current_task();
346346
array_list<rust_str*> strings;
347347
#if defined(__WIN32__)
348348
WIN32_FIND_DATA FindFileData;
@@ -443,20 +443,20 @@ precise_time_ns(uint64_t *ns) {
443443

444444
extern "C" CDECL rust_sched_id
445445
rust_get_sched_id() {
446-
rust_task *task = rust_sched_loop::get_task();
446+
rust_task *task = rust_get_current_task();
447447
return task->sched->get_id();
448448
}
449449

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

457457
extern "C" CDECL rust_task_id
458458
get_task_id() {
459-
rust_task *task = rust_sched_loop::get_task();
459+
rust_task *task = rust_get_current_task();
460460
return task->id;
461461
}
462462

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

468468
extern "C" CDECL rust_task*
469469
new_task() {
470-
rust_task *task = rust_sched_loop::get_task();
470+
rust_task *task = rust_get_current_task();
471471
return new_task_common(task->sched, task);
472472
}
473473

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

487487
extern "C" rust_task *
488488
rust_get_task() {
489-
return rust_sched_loop::get_task();
489+
return rust_get_current_task();
490490
}
491491

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

497497
extern "C" CDECL int
498498
sched_threads() {
499-
rust_task *task = rust_sched_loop::get_task();
499+
rust_task *task = rust_get_current_task();
500500
return task->sched->number_of_threads();
501501
}
502502

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

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

524524
extern "C" CDECL void
525525
del_port(rust_port *port) {
526-
rust_task *task = rust_sched_loop::get_task();
526+
rust_task *task = rust_get_current_task();
527527
LOG(task, comm, "del_port(0x%" PRIxPTR ")", (uintptr_t) port);
528528
delete port;
529529
}
@@ -541,7 +541,7 @@ get_port_id(rust_port *port) {
541541
extern "C" CDECL uintptr_t
542542
rust_port_id_send(type_desc *t, rust_port_id target_port_id, void *sptr) {
543543
bool sent = false;
544-
rust_task *task = rust_sched_loop::get_task();
544+
rust_task *task = rust_get_current_task();
545545

546546
LOG(task, comm, "rust_port_id*_send port: 0x%" PRIxPTR,
547547
(uintptr_t) target_port_id);
@@ -572,14 +572,14 @@ port_recv(uintptr_t *dptr, rust_port *port, uintptr_t *yield) {
572572
extern "C" CDECL void
573573
rust_port_select(rust_port **dptr, rust_port **ports,
574574
size_t n_ports, uintptr_t *yield) {
575-
rust_task *task = rust_sched_loop::get_task();
575+
rust_task *task = rust_get_current_task();
576576
rust_port_selector *selector = task->get_port_selector();
577577
selector->select(task, dptr, ports, n_ports, yield);
578578
}
579579

580580
extern "C" CDECL void
581581
rust_set_exit_status(intptr_t code) {
582-
rust_task *task = rust_sched_loop::get_task();
582+
rust_task *task = rust_get_current_task();
583583
task->kernel->set_exit_status((int)code);
584584
}
585585

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

595595
extern "C" CDECL void
596596
rust_log_console_off() {
597-
rust_task *task = rust_sched_loop::get_task();
597+
rust_task *task = rust_get_current_task();
598598
log_console_off(task->kernel->env);
599599
}
600600

Diff for: src/rt/rust_sched_loop.h

+7-36
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ struct rust_sched_loop
3838

3939
const int id;
4040

41-
#ifndef __WIN32__
42-
static pthread_key_t task_key;
43-
#else
44-
static DWORD task_key;
45-
#endif
46-
47-
static bool tls_initialized;
4841
context c_context;
4942

5043
bool should_exit;
@@ -69,6 +62,13 @@ struct rust_sched_loop
6962
public:
7063
rust_kernel *kernel;
7164
rust_scheduler *sched;
65+
static bool tls_initialized;
66+
67+
#ifndef __WIN32__
68+
static pthread_key_t task_key;
69+
#else
70+
static DWORD task_key;
71+
#endif
7272

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

119-
static rust_task *get_task();
120-
121119
// Called by each task when they are ready to be destroyed
122120
void release_task(rust_task *task);
123121

@@ -134,33 +132,6 @@ rust_sched_loop::get_log() {
134132
return _log;
135133
}
136134

137-
// This stuff is on the stack-switching fast path
138-
139-
#ifndef __WIN32__
140-
141-
inline rust_task *
142-
rust_sched_loop::get_task() {
143-
if (!tls_initialized)
144-
return NULL;
145-
rust_task *task = reinterpret_cast<rust_task *>
146-
(pthread_getspecific(task_key));
147-
assert(task && "Couldn't get the task from TLS!");
148-
return task;
149-
}
150-
151-
#else
152-
153-
inline rust_task *
154-
rust_sched_loop::get_task() {
155-
if (!tls_initialized)
156-
return NULL;
157-
rust_task *task = reinterpret_cast<rust_task *>(TlsGetValue(task_key));
158-
assert(task && "Couldn't get the task from TLS!");
159-
return task;
160-
}
161-
162-
#endif
163-
164135
// NB: Runs on the Rust stack
165136
inline stk_seg *
166137
rust_sched_loop::borrow_c_stack() {

Diff for: src/rt/rust_shape.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ extern "C" void
552552
shape_cmp_type(int8_t *result, const type_desc *tydesc,
553553
const type_desc **subtydescs, uint8_t *data_0,
554554
uint8_t *data_1, uint8_t cmp_type) {
555-
rust_task *task = rust_sched_loop::get_task();
555+
rust_task *task = rust_get_current_task();
556556
shape::arena arena;
557557

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

574574
extern "C" rust_str *
575575
shape_log_str(const type_desc *tydesc, uint8_t *data) {
576-
rust_task *task = rust_sched_loop::get_task();
576+
rust_task *task = rust_get_current_task();
577577

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

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

596596
shape::arena arena;
597597
shape::type_param *params =

0 commit comments

Comments
 (0)