Skip to content

Commit 28dff0a

Browse files
committed
---
yaml --- r: 139835 b: refs/heads/try2 c: ebefe07 h: refs/heads/master i: 139833: 64f1c29 139831: 7ed3bc0 v: v3
1 parent 10357d2 commit 28dff0a

File tree

4 files changed

+229
-248
lines changed

4 files changed

+229
-248
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 473b4d19ad51529afb8f1391cf471a20053c781c
8+
refs/heads/try2: ebefe07792caf17c03c6f90fb1979d4e6c935001
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libcore/rt/sched/local.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn take() -> ~Scheduler {
4343
/// # Safety Note
4444
/// Because this leaves the Scheduler in thread-local storage it is possible
4545
/// For the Scheduler pointer to be aliased
46-
pub unsafe fn borrow(f: &fn(&mut Scheduler)) {
46+
pub unsafe fn borrow() -> &mut Scheduler {
4747
unsafe {
4848
let key = tls_key();
4949
let mut void_sched: *mut c_void = tls::get(key);
@@ -54,7 +54,7 @@ pub unsafe fn borrow(f: &fn(&mut Scheduler)) {
5454
transmute::<&mut *mut c_void, &mut ~Scheduler>(void_sched_ptr)
5555
};
5656
let sched: &mut Scheduler = &mut **sched;
57-
f(sched);
57+
return sched;
5858
}
5959
}
6060
}
@@ -93,8 +93,7 @@ fn borrow_smoke_test() {
9393
let scheduler = ~UvEventLoop::new_scheduler();
9494
put(scheduler);
9595
unsafe {
96-
do borrow |_sched| {
97-
}
96+
let _scheduler = borrow();
9897
}
9998
let _scheduler = take();
10099
}

branches/try2/src/libcore/rt/sched/mod.rs

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -95,29 +95,33 @@ pub impl Scheduler {
9595
// Give ownership of the scheduler (self) to the thread
9696
local::put(self);
9797

98-
do Scheduler::unsafe_local |scheduler| {
99-
fn run_scheduler_once() {
100-
do Scheduler::unsafe_local |scheduler| {
101-
if scheduler.resume_task_from_queue() {
102-
// Ok, a task ran. Nice! We'll do it again later
103-
scheduler.event_loop.callback(run_scheduler_once);
104-
}
105-
}
98+
let scheduler = Scheduler::unsafe_local_borrow();
99+
fn run_scheduler_once() {
100+
let scheduler = Scheduler::unsafe_local_borrow();
101+
if scheduler.resume_task_from_queue() {
102+
// Ok, a task ran. Nice! We'll do it again later
103+
scheduler.event_loop.callback(run_scheduler_once);
106104
}
107-
108-
scheduler.event_loop.callback(run_scheduler_once);
109-
scheduler.event_loop.run();
110105
}
111106

107+
scheduler.event_loop.callback(run_scheduler_once);
108+
scheduler.event_loop.run();
109+
112110
return local::take();
113111
}
114112

115113
/// Get a mutable pointer to the thread-local scheduler.
116114
/// # Safety Note
117115
/// This allows other mutable aliases to the scheduler, both in the current
118116
/// execution context and other execution contexts.
119-
fn unsafe_local(f: &fn(&mut Scheduler)) {
120-
unsafe { local::borrow(f) }
117+
fn unsafe_local_borrow() -> &mut Scheduler {
118+
unsafe { local::borrow() }
119+
}
120+
121+
fn local_borrow(f: &fn(&mut Scheduler)) {
122+
let mut sched = local::take();
123+
f(sched);
124+
local::put(sched);
121125
}
122126

123127
// * Scheduler-context operations
@@ -208,9 +212,8 @@ pub impl Scheduler {
208212
}
209213

210214
// We could be executing in a different thread now
211-
do Scheduler::unsafe_local |sched| {
212-
sched.run_cleanup_job();
213-
}
215+
let sched = Scheduler::unsafe_local_borrow();
216+
sched.run_cleanup_job();
214217
}
215218

216219
/// Switch directly to another task, without going through the scheduler.
@@ -232,9 +235,8 @@ pub impl Scheduler {
232235
}
233236

234237
// We could be executing in a different thread now
235-
do Scheduler::unsafe_local |sched| {
236-
sched.run_cleanup_job();
237-
}
238+
let sched = Scheduler::unsafe_local_borrow();
239+
sched.run_cleanup_job();
238240
}
239241

240242
// * Other stuff
@@ -331,15 +333,13 @@ pub impl Task {
331333
// This is the first code to execute after the initial
332334
// context switch to the task. The previous context may
333335
// have asked us to do some cleanup.
334-
do Scheduler::unsafe_local |sched| {
335-
sched.run_cleanup_job();
336-
}
336+
let sched = Scheduler::unsafe_local_borrow();
337+
sched.run_cleanup_job();
337338

338339
start();
339340

340-
do Scheduler::unsafe_local |sched| {
341-
sched.terminate_current_task();
342-
}
341+
let sched = Scheduler::unsafe_local_borrow();
342+
sched.terminate_current_task();
343343
};
344344
return wrapper;
345345
}
@@ -398,13 +398,12 @@ fn test_swap_tasks() {
398398
let mut sched = ~UvEventLoop::new_scheduler();
399399
let task1 = ~do Task::new(&mut sched.stack_pool) {
400400
unsafe { *count_ptr = *count_ptr + 1; }
401-
do Scheduler::unsafe_local |sched| {
402-
let task2 = ~do Task::new(&mut sched.stack_pool) {
403-
unsafe { *count_ptr = *count_ptr + 1; }
404-
};
405-
// Context switch directly to the new task
406-
sched.resume_task_from_running_task_direct(task2);
407-
}
401+
let sched = Scheduler::unsafe_local_borrow();
402+
let task2 = ~do Task::new(&mut sched.stack_pool) {
403+
unsafe { *count_ptr = *count_ptr + 1; }
404+
};
405+
// Context switch directly to the new task
406+
sched.resume_task_from_running_task_direct(task2);
408407
unsafe { *count_ptr = *count_ptr + 1; }
409408
};
410409
sched.task_queue.push_back(task1);
@@ -431,7 +430,7 @@ fn test_run_a_lot_of_tasks_queued() {
431430
assert!(count == MAX);
432431

433432
fn run_task(count_ptr: *mut int) {
434-
do Scheduler::unsafe_local |sched| {
433+
do Scheduler::local_borrow |sched| {
435434
let task = ~do Task::new(&mut sched.stack_pool) {
436435
unsafe {
437436
*count_ptr = *count_ptr + 1;
@@ -464,18 +463,17 @@ fn test_run_a_lot_of_tasks_direct() {
464463
assert!(count == MAX);
465464

466465
fn run_task(count_ptr: *mut int) {
467-
do Scheduler::unsafe_local |sched| {
468-
let task = ~do Task::new(&mut sched.stack_pool) {
469-
unsafe {
470-
*count_ptr = *count_ptr + 1;
471-
if *count_ptr != MAX {
472-
run_task(count_ptr);
473-
}
466+
let sched = Scheduler::unsafe_local_borrow();
467+
let task = ~do Task::new(&mut sched.stack_pool) {
468+
unsafe {
469+
*count_ptr = *count_ptr + 1;
470+
if *count_ptr != MAX {
471+
run_task(count_ptr);
474472
}
475-
};
476-
// Context switch directly to the new task
477-
sched.resume_task_from_running_task_direct(task);
478-
}
473+
}
474+
};
475+
// Context switch directly to the new task
476+
sched.resume_task_from_running_task_direct(task);
479477
};
480478
}
481479
}
@@ -485,12 +483,11 @@ fn test_block_task() {
485483
do run_in_bare_thread {
486484
let mut sched = ~UvEventLoop::new_scheduler();
487485
let task = ~do Task::new(&mut sched.stack_pool) {
488-
do Scheduler::unsafe_local |sched| {
489-
assert!(sched.in_task_context());
490-
do sched.deschedule_running_task_and_then() |sched, task| {
491-
assert!(!sched.in_task_context());
492-
sched.task_queue.push_back(task);
493-
}
486+
let sched = Scheduler::unsafe_local_borrow();
487+
assert!(sched.in_task_context());
488+
do sched.deschedule_running_task_and_then() |sched, task| {
489+
assert!(!sched.in_task_context());
490+
sched.task_queue.push_back(task);
494491
}
495492
};
496493
sched.task_queue.push_back(task);

0 commit comments

Comments
 (0)