Skip to content

Commit 761f5fb

Browse files
committed
std::rt: Optimize TLS use in change_task_context
1 parent 5402786 commit 761f5fb

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/libstd/rt/local.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub trait Local {
2121
fn take() -> ~Self;
2222
fn exists() -> bool;
2323
fn borrow<T>(f: &fn(&mut Self) -> T) -> T;
24+
unsafe fn unsafe_take() -> ~Self;
2425
unsafe fn unsafe_borrow() -> *mut Self;
2526
unsafe fn try_unsafe_borrow() -> Option<*mut Self>;
2627
}
@@ -46,6 +47,8 @@ impl Local for Task {
4647
}
4748
}
4849
#[inline]
50+
unsafe fn unsafe_take() -> ~Task { local_ptr::unsafe_take() }
51+
#[inline]
4952
unsafe fn unsafe_borrow() -> *mut Task { local_ptr::unsafe_borrow() }
5053
#[inline]
5154
unsafe fn try_unsafe_borrow() -> Option<*mut Task> {
@@ -89,6 +92,7 @@ impl Local for Scheduler {
8992
}
9093
}
9194
}
95+
unsafe fn unsafe_take() -> ~Scheduler { rtabort!("unimpl") }
9296
unsafe fn unsafe_borrow() -> *mut Scheduler {
9397
match (*Local::unsafe_borrow::<Task>()).sched {
9498
Some(~ref mut sched) => {
@@ -122,6 +126,7 @@ impl Local for IoFactoryObject {
122126
fn take() -> ~IoFactoryObject { rtabort!("unimpl") }
123127
fn exists() -> bool { rtabort!("unimpl") }
124128
fn borrow<T>(_f: &fn(&mut IoFactoryObject) -> T) -> T { rtabort!("unimpl") }
129+
unsafe fn unsafe_take() -> ~IoFactoryObject { rtabort!("unimpl") }
125130
unsafe fn unsafe_borrow() -> *mut IoFactoryObject {
126131
let sched = Local::unsafe_borrow::<Scheduler>();
127132
let io: *mut IoFactoryObject = (*sched).event_loop.io().unwrap();

src/libstd/rt/local_ptr.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,23 @@ pub unsafe fn take<T>() -> ~T {
6464
return ptr;
6565
}
6666

67+
/// Take ownership of a pointer from thread-local storage.
68+
///
69+
/// # Safety note
70+
///
71+
/// Does not validate the pointer type.
72+
/// Leaves the old pointer in TLS for speed.
73+
#[inline]
74+
pub unsafe fn unsafe_take<T>() -> ~T {
75+
let key = tls_key();
76+
let void_ptr: *mut c_void = tls::get(key);
77+
if void_ptr.is_null() {
78+
rtabort!("thread-local pointer is null. bogus!");
79+
}
80+
let ptr: ~T = cast::transmute(void_ptr);
81+
return ptr;
82+
}
83+
6784
/// Check whether there is a thread-local pointer installed.
6885
pub fn exists() -> bool {
6986
unsafe {

src/libstd/rt/sched.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,9 @@ impl Scheduler {
505505
let mut this = self;
506506

507507
// The current task is grabbed from TLS, not taken as an input.
508-
let current_task: ~Task = Local::take::<Task>();
508+
// Doing an unsafe_take to avoid writing back a null pointer -
509+
// We're going to call `put` later to do that.
510+
let current_task: ~Task = unsafe { Local::unsafe_take::<Task>() };
509511

510512
// Check that the task is not in an atomically() section (e.g.,
511513
// holding a pthread mutex, which could deadlock the scheduler).

0 commit comments

Comments
 (0)