Skip to content

Commit

Permalink
core: Initialize global state lazily in the Scheduler ctor
Browse files Browse the repository at this point in the history
I don't want any global one-time initalization functions because
that will make embedding harder.
  • Loading branch information
brson committed Mar 19, 2013
1 parent 0447034 commit 5af5766
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
10 changes: 0 additions & 10 deletions src/libcore/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,10 @@ mod context;
mod thread;
pub mod env;

pub fn initialize() {
unsafe { rust_initialize_global_state(); }
extern {
fn rust_initialize_global_state();
}
}

pub fn start(main: *u8, _argc: int, _argv: *c_char, _crate_map: *u8) -> int {
use self::sched::{Scheduler, Task};
use self::uvio::UvEventLoop;

// XXX: Would rather do this lazily in Scheduler
initialize();

let loop_ = ~UvEventLoop::new();
let mut sched = ~Scheduler::new(loop_);
let main_task = ~do Task::new(&mut sched.stack_pool) {
Expand Down
7 changes: 7 additions & 0 deletions src/libcore/rt/sched.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ enum CleanupJob {
pub impl Scheduler {

static fn new(event_loop: ~EventLoopObject) -> Scheduler {

// Lazily initialize the global state, currently the scheduler TLS key
unsafe { rust_initialize_global_state(); }
extern {
fn rust_initialize_global_state();
}

Scheduler {
event_loop: event_loop,
task_queue: WorkQueue::new(),
Expand Down
17 changes: 13 additions & 4 deletions src/rt/rust_builtin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,6 @@ rust_call_nullary_fn(nullary_fn f) {
f();
}


#ifndef _WIN32
pthread_key_t sched_key;
#else
Expand All @@ -901,16 +900,26 @@ rust_get_sched_tls_key() {
return &sched_key;
}

// Initialize the global state required by the new scheduler
extern "C" CDECL void
rust_initialize_global_state() {

static lock_and_signal init_lock;
static bool initialized = false;

scoped_lock with(init_lock);

if (!initialized) {

#ifndef _WIN32
assert(!pthread_key_create(&sched_key, NULL));
assert(!pthread_key_create(&sched_key, NULL));
#else
sched_key = TlsAlloc();
assert(sched_key != TLS_OUT_OF_INDEXES);
sched_key = TlsAlloc();
assert(sched_key != TLS_OUT_OF_INDEXES);
#endif

initialized = true;
}
}

//
Expand Down

3 comments on commit 5af5766

@bors
Copy link
Contributor

@bors bors commented on 5af5766 Mar 19, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at brson@5af5766

@bors
Copy link
Contributor

@bors bors commented on 5af5766 Mar 19, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging brson/rust/rt = 5af5766 into auto

@bors
Copy link
Contributor

@bors bors commented on 5af5766 Mar 19, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging brson/rust/rt = 5af5766 into auto failed

Please sign in to comment.