core::rt: Make local_sched a wrapper around Local

This commit is contained in:
Brian Anderson 2013-05-19 15:25:35 -07:00
parent 71aa6b6631
commit 18fab45aab
2 changed files with 20 additions and 8 deletions

View File

@ -8,10 +8,21 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use rt::sched::Scheduler;
use rt::local_ptr;
pub trait Local {
fn put_local(value: ~Self);
fn take_local() -> ~Self;
fn exists() -> bool;
fn borrow(f: &fn(&mut Self));
fn unsafe_borrow() -> *mut Self;
fn exists_local() -> bool;
fn borrow_local(f: &fn(&mut Self));
unsafe fn unsafe_borrow_local() -> *mut Self;
}
impl Local for Scheduler {
fn put_local(value: ~Scheduler) { unsafe { local_ptr::put(value) }}
fn take_local() -> ~Scheduler { unsafe { local_ptr::take() } }
fn exists_local() -> bool { local_ptr::exists() }
fn borrow_local(f: &fn(&mut Scheduler)) { unsafe { local_ptr::borrow(f) } }
unsafe fn unsafe_borrow_local() -> *mut Scheduler { local_ptr::unsafe_borrow() }
}

View File

@ -21,21 +21,22 @@ use rt::rtio::{EventLoop, IoFactoryObject};
use unstable::finally::Finally;
use rt::local_ptr;
use tls = rt::thread_local_storage;
use rt::local::Local;
#[cfg(test)] use rt::uv::uvio::UvEventLoop;
/// Give the Scheduler to thread-local storage
pub fn put(sched: ~Scheduler) { unsafe { local_ptr::put(sched) } }
pub fn put(sched: ~Scheduler) { Local::put_local(sched) }
/// Take ownership of the Scheduler from thread-local storage
pub fn take() -> ~Scheduler { unsafe { local_ptr::take() } }
pub fn take() -> ~Scheduler { Local::take_local() }
/// Check whether there is a thread-local Scheduler attached to the running thread
pub fn exists() -> bool { local_ptr::exists() }
pub fn exists() -> bool { Local::exists_local::<Scheduler>() }
/// Borrow the thread-local scheduler from thread-local storage.
/// While the scheduler is borrowed it is not available in TLS.
pub fn borrow(f: &fn(&mut Scheduler)) { unsafe { local_ptr::borrow(f) } }
pub fn borrow(f: &fn(&mut Scheduler)) { Local::borrow_local(f) }
/// Borrow a mutable reference to the thread-local Scheduler
///
@ -43,7 +44,7 @@ pub fn borrow(f: &fn(&mut Scheduler)) { unsafe { local_ptr::borrow(f) } }
///
/// Because this leaves the Scheduler in thread-local storage it is possible
/// For the Scheduler pointer to be aliased
pub unsafe fn unsafe_borrow() -> *mut Scheduler { local_ptr::unsafe_borrow() }
pub unsafe fn unsafe_borrow() -> *mut Scheduler { Local::unsafe_borrow_local() }
pub unsafe fn unsafe_borrow_io() -> *mut IoFactoryObject {
let sched = unsafe_borrow();