@@ -95,29 +95,33 @@ pub impl Scheduler {
95
95
// Give ownership of the scheduler (self) to the thread
96
96
local:: put ( self ) ;
97
97
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) ;
106
104
}
107
-
108
- scheduler. event_loop . callback ( run_scheduler_once) ;
109
- scheduler. event_loop . run ( ) ;
110
105
}
111
106
107
+ scheduler. event_loop . callback ( run_scheduler_once) ;
108
+ scheduler. event_loop . run ( ) ;
109
+
112
110
return local:: take ( ) ;
113
111
}
114
112
115
113
/// Get a mutable pointer to the thread-local scheduler.
116
114
/// # Safety Note
117
115
/// This allows other mutable aliases to the scheduler, both in the current
118
116
/// 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) ;
121
125
}
122
126
123
127
// * Scheduler-context operations
@@ -208,9 +212,8 @@ pub impl Scheduler {
208
212
}
209
213
210
214
// 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 ( ) ;
214
217
}
215
218
216
219
/// Switch directly to another task, without going through the scheduler.
@@ -232,9 +235,8 @@ pub impl Scheduler {
232
235
}
233
236
234
237
// 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 ( ) ;
238
240
}
239
241
240
242
// * Other stuff
@@ -331,15 +333,13 @@ pub impl Task {
331
333
// This is the first code to execute after the initial
332
334
// context switch to the task. The previous context may
333
335
// 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 ( ) ;
337
338
338
339
start ( ) ;
339
340
340
- do Scheduler :: unsafe_local |sched| {
341
- sched. terminate_current_task ( ) ;
342
- }
341
+ let sched = Scheduler :: unsafe_local_borrow ( ) ;
342
+ sched. terminate_current_task ( ) ;
343
343
} ;
344
344
return wrapper;
345
345
}
@@ -398,13 +398,12 @@ fn test_swap_tasks() {
398
398
let mut sched = ~UvEventLoop :: new_scheduler ( ) ;
399
399
let task1 = ~do Task :: new ( & mut sched. stack_pool ) {
400
400
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) ;
408
407
unsafe { * count_ptr = * count_ptr + 1 ; }
409
408
} ;
410
409
sched. task_queue . push_back ( task1) ;
@@ -431,7 +430,7 @@ fn test_run_a_lot_of_tasks_queued() {
431
430
assert ! ( count == MAX ) ;
432
431
433
432
fn run_task ( count_ptr : * mut int ) {
434
- do Scheduler :: unsafe_local |sched| {
433
+ do Scheduler :: local_borrow |sched| {
435
434
let task = ~do Task :: new ( & mut sched. stack_pool ) {
436
435
unsafe {
437
436
* count_ptr = * count_ptr + 1 ;
@@ -464,18 +463,17 @@ fn test_run_a_lot_of_tasks_direct() {
464
463
assert ! ( count == MAX ) ;
465
464
466
465
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) ;
474
472
}
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 ) ;
479
477
} ;
480
478
}
481
479
}
@@ -485,12 +483,11 @@ fn test_block_task() {
485
483
do run_in_bare_thread {
486
484
let mut sched = ~UvEventLoop : : new_scheduler ( ) ;
487
485
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) ;
494
491
}
495
492
} ;
496
493
sched. task_queue . push_back ( task) ;
0 commit comments