Skip to content

Commit 3b8a354

Browse files
committed
core::rt: A little bit of cleanup to thread-local scheduler
1 parent 1f8ebb6 commit 3b8a354

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

src/libcore/rt/sched/local.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,11 @@ pub fn take() -> ~Scheduler {
3939
}
4040
}
4141

42-
/// Give the Scheduler to thread-local storage for the duration of the block
43-
pub fn install(sched: ~Scheduler, f: &fn()) -> ~Scheduler {
44-
put(sched);
45-
f();
46-
return take();
47-
}
48-
4942
/// Borrow a mutable reference to the thread-local Scheduler
5043
/// # Safety Note
5144
/// Because this leaves the Scheduler in thread-local storage it is possible
5245
/// For the Scheduler pointer to be aliased
53-
pub fn borrow(f: &fn(&mut Scheduler)) {
46+
pub unsafe fn borrow(f: &fn(&mut Scheduler)) {
5447
unsafe {
5548
let key = tls_key();
5649
let mut void_sched: *mut c_void = tls::get(key);
@@ -96,11 +89,13 @@ fn thread_local_scheduler_two_instances() {
9689
}
9790

9891
#[test]
99-
fn install_borrow_smoke_test() {
92+
fn borrow_smoke_test() {
10093
let scheduler = ~UvEventLoop::new_scheduler();
101-
let _scheduler = do install(scheduler) {
94+
put(scheduler);
95+
unsafe {
10296
do borrow |_sched| {
10397
}
104-
};
98+
}
99+
let _scheduler = take();
105100
}
106101

src/libcore/rt/sched/mod.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ pub impl Scheduler {
9393
assert!(!self.in_task_context());
9494

9595
// Give ownership of the scheduler (self) to the thread
96-
do self.install |scheduler| {
96+
local::put(self);
97+
98+
do Scheduler::local |scheduler| {
9799
fn run_scheduler_once() {
98100
do Scheduler::local |scheduler| {
99101
if scheduler.resume_task_from_queue() {
@@ -106,16 +108,12 @@ pub impl Scheduler {
106108
scheduler.event_loop.callback(run_scheduler_once);
107109
scheduler.event_loop.run();
108110
}
109-
}
110111

111-
fn install(~self, f: &fn(&mut Scheduler)) -> ~Scheduler {
112-
do local::install(self) {
113-
local::borrow(f)
114-
}
112+
return local::take();
115113
}
116114

117115
fn local(f: &fn(&mut Scheduler)) {
118-
local::borrow(f)
116+
unsafe { local::borrow(f) }
119117
}
120118

121119
// * Scheduler-context operations
@@ -206,7 +204,7 @@ pub impl Scheduler {
206204
}
207205

208206
// We could be executing in a different thread now
209-
do local::borrow |sched| {
207+
do Scheduler::local |sched| {
210208
sched.run_cleanup_job();
211209
}
212210
}
@@ -230,7 +228,7 @@ pub impl Scheduler {
230228
}
231229

232230
// We could be executing in a different thread now
233-
do local::borrow |sched| {
231+
do Scheduler::local |sched| {
234232
sched.run_cleanup_job();
235233
}
236234
}

0 commit comments

Comments
 (0)