diff options
| author | Brian Anderson <banderson@mozilla.com> | 2013-04-15 13:40:36 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2013-04-15 15:18:49 -0700 |
| commit | 1f8ebb6a8ebc5ee490158d3f4264f8aefd509045 (patch) | |
| tree | ec18069f89b923ca3f8cb751ef3d25182f407a71 /src/libcore/rt | |
| parent | 4f1dd1fd212f51f0a2f4fe20f9ebc7e0dc9081d8 (diff) | |
| download | rust-1f8ebb6a8ebc5ee490158d3f4264f8aefd509045.tar.gz rust-1f8ebb6a8ebc5ee490158d3f4264f8aefd509045.zip | |
core::rt: Move thread-local scheduler to its own module
Diffstat (limited to 'src/libcore/rt')
| -rw-r--r-- | src/libcore/rt/mod.rs | 1 | ||||
| -rw-r--r-- | src/libcore/rt/sched/local.rs | 106 | ||||
| -rw-r--r-- | src/libcore/rt/sched/mod.rs (renamed from src/libcore/rt/sched.rs) | 101 |
3 files changed, 109 insertions, 99 deletions
diff --git a/src/libcore/rt/mod.rs b/src/libcore/rt/mod.rs index 0f2a6cd7ef9..b1227af5f4c 100644 --- a/src/libcore/rt/mod.rs +++ b/src/libcore/rt/mod.rs @@ -32,6 +32,7 @@ macro_rules! rtdebug ( ($( $arg:expr),+) => ( $(let _ = $arg)*; ) ) +#[path = "sched/mod.rs"] mod sched; mod rtio; pub mod uvll; diff --git a/src/libcore/rt/sched/local.rs b/src/libcore/rt/sched/local.rs new file mode 100644 index 00000000000..1b1a0033017 --- /dev/null +++ b/src/libcore/rt/sched/local.rs @@ -0,0 +1,106 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Access to the thread-local Scheduler + +use ptr::mut_null; +use libc::c_void; +use cast::transmute; + +use super::Scheduler; +use tls = super::super::thread_local_storage; +#[cfg(test)] use super::super::uvio::UvEventLoop; + +/// Give the Scheduler to thread-local storage +pub fn put(sched: ~Scheduler) { + unsafe { + let key = tls_key(); + let void_sched: *mut c_void = transmute::<~Scheduler, *mut c_void>(sched); + tls::set(key, void_sched); + } +} + +/// Take ownership of the Scheduler from thread-local storage +pub fn take() -> ~Scheduler { + unsafe { + let key = tls_key(); + let void_sched: *mut c_void = tls::get(key); + assert!(void_sched.is_not_null()); + let sched = transmute::<*mut c_void, ~Scheduler>(void_sched); + tls::set(key, mut_null()); + return sched; + } +} + +/// Give the Scheduler to thread-local storage for the duration of the block +pub fn install(sched: ~Scheduler, f: &fn()) -> ~Scheduler { + put(sched); + f(); + return take(); +} + +/// Borrow a mutable reference to the thread-local Scheduler +/// # Safety Note +/// Because this leaves the Scheduler in thread-local storage it is possible +/// For the Scheduler pointer to be aliased +pub fn borrow(f: &fn(&mut Scheduler)) { + unsafe { + let key = tls_key(); + let mut void_sched: *mut c_void = tls::get(key); + assert!(void_sched.is_not_null()); + { + let void_sched_ptr = &mut void_sched; + let sched: &mut ~Scheduler = { + transmute::<&mut *mut c_void, &mut ~Scheduler>(void_sched_ptr) + }; + let sched: &mut Scheduler = &mut **sched; + f(sched); + } + } +} + +fn tls_key() -> tls::Key { + unsafe { + let key: *mut c_void = rust_get_sched_tls_key(); + let key: &mut tls::Key = transmute(key); + return *key; + } +} + +extern { + fn rust_get_sched_tls_key() -> *mut c_void; +} + +#[test] +fn thread_local_scheduler_smoke_test() { + let scheduler = ~UvEventLoop::new_scheduler(); + put(scheduler); + let _scheduler = take(); +} + +#[test] +fn thread_local_scheduler_two_instances() { + let scheduler = ~UvEventLoop::new_scheduler(); + put(scheduler); + let _scheduler = take(); + let scheduler = ~UvEventLoop::new_scheduler(); + put(scheduler); + let _scheduler = take(); +} + +#[test] +fn install_borrow_smoke_test() { + let scheduler = ~UvEventLoop::new_scheduler(); + let _scheduler = do install(scheduler) { + do borrow |_sched| { + } + }; +} + diff --git a/src/libcore/rt/sched.rs b/src/libcore/rt/sched/mod.rs index 6ed21200876..bc25023d05f 100644 --- a/src/libcore/rt/sched.rs +++ b/src/libcore/rt/sched/mod.rs @@ -21,6 +21,8 @@ use super::context::Context; #[cfg(test)] use unstable::run_in_bare_thread; #[cfg(test)] use int; +mod local; + /// The Scheduler is responsible for coordinating execution of Tasks /// on a single thread. When the scheduler is running it is owned by /// thread local storage and the running task is owned by the @@ -350,105 +352,6 @@ pub impl Task { } } -mod local { - - //! Access to the thread-local Scheduler - - use ptr::mut_null; - use libc::c_void; - use cast::transmute; - - use super::Scheduler; - use tls = super::super::thread_local_storage; - #[cfg(test)] use super::super::uvio::UvEventLoop; - - /// Give the Scheduler to thread-local storage - pub fn put(sched: ~Scheduler) { - unsafe { - let key = tls_key(); - let void_sched: *mut c_void = transmute::<~Scheduler, *mut c_void>(sched); - tls::set(key, void_sched); - } - } - - /// Take ownership of the Scheduler from thread-local storage - pub fn take() -> ~Scheduler { - unsafe { - let key = tls_key(); - let void_sched: *mut c_void = tls::get(key); - assert!(void_sched.is_not_null()); - let sched = transmute::<*mut c_void, ~Scheduler>(void_sched); - tls::set(key, mut_null()); - return sched; - } - } - - /// Give the Scheduler to thread-local storage for the duration of the block - pub fn install(sched: ~Scheduler, f: &fn()) -> ~Scheduler { - put(sched); - f(); - return take(); - } - - /// Borrow a mutable reference to the thread-local Scheduler - /// # Safety Note - /// Because this leaves the Scheduler in thread-local storage it is possible - /// For the Scheduler pointer to be aliased - pub fn borrow(f: &fn(&mut Scheduler)) { - unsafe { - let key = tls_key(); - let mut void_sched: *mut c_void = tls::get(key); - assert!(void_sched.is_not_null()); - { - let void_sched_ptr = &mut void_sched; - let sched: &mut ~Scheduler = { - transmute::<&mut *mut c_void, &mut ~Scheduler>(void_sched_ptr) - }; - let sched: &mut Scheduler = &mut **sched; - f(sched); - } - } - } - - fn tls_key() -> tls::Key { - unsafe { - let key: *mut c_void = rust_get_sched_tls_key(); - let key: &mut tls::Key = transmute(key); - return *key; - } - } - - extern { - fn rust_get_sched_tls_key() -> *mut c_void; - } - - #[test] - fn thread_local_scheduler_smoke_test() { - let scheduler = ~UvEventLoop::new_scheduler(); - put(scheduler); - let _scheduler = take(); - } - - #[test] - fn thread_local_scheduler_two_instances() { - let scheduler = ~UvEventLoop::new_scheduler(); - put(scheduler); - let _scheduler = take(); - let scheduler = ~UvEventLoop::new_scheduler(); - put(scheduler); - let _scheduler = take(); - } - - #[test] - fn install_borrow_smoke_test() { - let scheduler = ~UvEventLoop::new_scheduler(); - let _scheduler = do install(scheduler) { - do borrow |_sched| { - } - }; - } -} - #[test] fn test_simple_scheduling() { do run_in_bare_thread { |
