Skip to content

Commit 5af5766

Browse files
committed
core: Initialize global state lazily in the Scheduler ctor
I don't want any global one-time initalization functions because that will make embedding harder.
1 parent 0447034 commit 5af5766

File tree

3 files changed

+20
-14
lines changed

3 files changed

+20
-14
lines changed

src/libcore/rt/mod.rs

-10
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,10 @@ mod context;
4646
mod thread;
4747
pub mod env;
4848

49-
pub fn initialize() {
50-
unsafe { rust_initialize_global_state(); }
51-
extern {
52-
fn rust_initialize_global_state();
53-
}
54-
}
55-
5649
pub fn start(main: *u8, _argc: int, _argv: *c_char, _crate_map: *u8) -> int {
5750
use self::sched::{Scheduler, Task};
5851
use self::uvio::UvEventLoop;
5952

60-
// XXX: Would rather do this lazily in Scheduler
61-
initialize();
62-
6353
let loop_ = ~UvEventLoop::new();
6454
let mut sched = ~Scheduler::new(loop_);
6555
let main_task = ~do Task::new(&mut sched.stack_pool) {

src/libcore/rt/sched.rs

+7
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ enum CleanupJob {
7171
pub impl Scheduler {
7272

7373
static fn new(event_loop: ~EventLoopObject) -> Scheduler {
74+
75+
// Lazily initialize the global state, currently the scheduler TLS key
76+
unsafe { rust_initialize_global_state(); }
77+
extern {
78+
fn rust_initialize_global_state();
79+
}
80+
7481
Scheduler {
7582
event_loop: event_loop,
7683
task_queue: WorkQueue::new(),

src/rt/rust_builtin.cpp

+13-4
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,6 @@ rust_call_nullary_fn(nullary_fn f) {
889889
f();
890890
}
891891

892-
893892
#ifndef _WIN32
894893
pthread_key_t sched_key;
895894
#else
@@ -901,16 +900,26 @@ rust_get_sched_tls_key() {
901900
return &sched_key;
902901
}
903902

903+
// Initialize the global state required by the new scheduler
904904
extern "C" CDECL void
905905
rust_initialize_global_state() {
906906

907+
static lock_and_signal init_lock;
908+
static bool initialized = false;
909+
910+
scoped_lock with(init_lock);
911+
912+
if (!initialized) {
913+
907914
#ifndef _WIN32
908-
assert(!pthread_key_create(&sched_key, NULL));
915+
assert(!pthread_key_create(&sched_key, NULL));
909916
#else
910-
sched_key = TlsAlloc();
911-
assert(sched_key != TLS_OUT_OF_INDEXES);
917+
sched_key = TlsAlloc();
918+
assert(sched_key != TLS_OUT_OF_INDEXES);
912919
#endif
913920

921+
initialized = true;
922+
}
914923
}
915924

916925
//

0 commit comments

Comments
 (0)