Skip to content

Commit cc6bb9e

Browse files
committed
---
yaml --- r: 139837 b: refs/heads/try2 c: ed74ac1 h: refs/heads/master i: 139835: 28dff0a v: v3
1 parent 3fcd74d commit cc6bb9e

File tree

3 files changed

+48
-48
lines changed

3 files changed

+48
-48
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: 68583a25a0b31bc113cf1f4ec479339cbf876e4d
8+
refs/heads/try2: ed74ac169e3e79ff4ce59329a06223e23f2b99b6
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/mod.rs

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use cast::transmute;
1414

1515
use super::work_queue::WorkQueue;
1616
use super::stack::{StackPool, StackSegment};
17-
use super::rtio::{EventLoop, EventLoopObject};
17+
use super::rtio::{EventLoop, EventLoopObject, IoFactoryObject};
1818
use super::context::Context;
1919

2020
#[cfg(test)] use super::uvio::UvEventLoop;
@@ -96,12 +96,12 @@ pub impl Scheduler {
9696
// Give ownership of the scheduler (self) to the thread
9797
local::put(self);
9898

99-
let scheduler = Scheduler::unsafe_local_borrow();
99+
let scheduler = unsafe { local::borrow() };
100100
fn run_scheduler_once() {
101-
let scheduler = Scheduler::local_take();
101+
let scheduler = Scheduler::take_local();
102102
if scheduler.resume_task_from_queue() {
103103
// Ok, a task ran. Nice! We'll do it again later
104-
do Scheduler::local_borrow |scheduler| {
104+
do Scheduler::borrow_local |scheduler| {
105105
scheduler.event_loop.callback(run_scheduler_once);
106106
}
107107
}
@@ -113,21 +113,27 @@ pub impl Scheduler {
113113
return local::take();
114114
}
115115

116-
/// Get a mutable pointer to the thread-local scheduler.
116+
/// Get a mutable pointer to the thread-local I/O
117117
/// # Safety Note
118118
/// This allows other mutable aliases to the scheduler, both in the current
119119
/// execution context and other execution contexts.
120-
fn unsafe_local_borrow() -> &mut Scheduler {
121-
unsafe { local::borrow() }
120+
unsafe fn borrow_local_io() -> &mut IoFactoryObject {
121+
unsafe {
122+
let io = local::borrow().event_loop.io().unwrap();
123+
transmute::<&mut IoFactoryObject, &mut IoFactoryObject>(io)
124+
}
122125
}
123126

124-
fn local_borrow(f: &fn(&mut Scheduler)) {
127+
/// Borrow the thread-local scheduler from thread-local storage.
128+
/// While the scheduler is borrowed it is not available in TLS.
129+
fn borrow_local(f: &fn(&mut Scheduler)) {
125130
let mut sched = local::take();
126131
f(sched);
127132
local::put(sched);
128133
}
129134

130-
fn local_take() -> ~Scheduler {
135+
/// Take ownership of the scheduler from thread local storage
136+
fn take_local() -> ~Scheduler {
131137
local::take()
132138
}
133139

@@ -163,14 +169,14 @@ pub impl Scheduler {
163169
local::put(self);
164170

165171
// Take pointers to both the task and scheduler's saved registers.
166-
let sched = Scheduler::unsafe_local_borrow();
172+
let sched = unsafe { local::borrow() };
167173
let (sched_context, _, next_task_context) = sched.get_contexts();
168174
let next_task_context = next_task_context.unwrap();
169175
// Context switch to the task, restoring it's registers
170176
// and saving the scheduler's
171177
Context::swap(sched_context, next_task_context);
172178

173-
let sched = Scheduler::unsafe_local_borrow();
179+
let sched = unsafe { local::borrow() };
174180
// The running task should have passed ownership elsewhere
175181
assert!(sched.current_task.is_none());
176182

@@ -194,7 +200,7 @@ pub impl Scheduler {
194200

195201
local::put(self);
196202

197-
let sched = Scheduler::unsafe_local_borrow();
203+
let sched = unsafe { local::borrow() };
198204
let (sched_context, last_task_context, _) = sched.get_contexts();
199205
let last_task_context = last_task_context.unwrap();
200206
Context::swap(last_task_context, sched_context);
@@ -225,13 +231,13 @@ pub impl Scheduler {
225231

226232
local::put(self);
227233

228-
let sched = Scheduler::unsafe_local_borrow();
234+
let sched = unsafe { local::borrow() };
229235
let (sched_context, last_task_context, _) = sched.get_contexts();
230236
let last_task_context = last_task_context.unwrap();
231237
Context::swap(last_task_context, sched_context);
232238

233239
// We could be executing in a different thread now
234-
let sched = Scheduler::unsafe_local_borrow();
240+
let sched = unsafe { local::borrow() };
235241
sched.run_cleanup_job();
236242
}
237243

@@ -250,14 +256,14 @@ pub impl Scheduler {
250256

251257
local::put(self);
252258

253-
let sched = Scheduler::unsafe_local_borrow();
259+
let sched = unsafe { local::borrow() };
254260
let (_, last_task_context, next_task_context) = sched.get_contexts();
255261
let last_task_context = last_task_context.unwrap();
256262
let next_task_context = next_task_context.unwrap();
257263
Context::swap(last_task_context, next_task_context);
258264

259265
// We could be executing in a different thread now
260-
let sched = Scheduler::unsafe_local_borrow();
266+
let sched = unsafe { local::borrow() };
261267
sched.run_cleanup_job();
262268
}
263269

@@ -355,12 +361,12 @@ pub impl Task {
355361
// This is the first code to execute after the initial
356362
// context switch to the task. The previous context may
357363
// have asked us to do some cleanup.
358-
let sched = Scheduler::unsafe_local_borrow();
364+
let sched = unsafe { local::borrow() };
359365
sched.run_cleanup_job();
360366

361367
start();
362368

363-
let sched = Scheduler::local_take();
369+
let sched = Scheduler::take_local();
364370
sched.terminate_current_task();
365371
};
366372
return wrapper;
@@ -420,7 +426,7 @@ fn test_swap_tasks() {
420426
let mut sched = ~UvEventLoop::new_scheduler();
421427
let task1 = ~do Task::new(&mut sched.stack_pool) {
422428
unsafe { *count_ptr = *count_ptr + 1; }
423-
let mut sched = Scheduler::local_take();
429+
let mut sched = Scheduler::take_local();
424430
let task2 = ~do Task::new(&mut sched.stack_pool) {
425431
unsafe { *count_ptr = *count_ptr + 1; }
426432
};
@@ -452,7 +458,7 @@ fn test_run_a_lot_of_tasks_queued() {
452458
assert!(count == MAX);
453459

454460
fn run_task(count_ptr: *mut int) {
455-
do Scheduler::local_borrow |sched| {
461+
do Scheduler::borrow_local |sched| {
456462
let task = ~do Task::new(&mut sched.stack_pool) {
457463
unsafe {
458464
*count_ptr = *count_ptr + 1;
@@ -485,7 +491,7 @@ fn test_run_a_lot_of_tasks_direct() {
485491
assert!(count == MAX);
486492

487493
fn run_task(count_ptr: *mut int) {
488-
let mut sched = Scheduler::local_take();
494+
let mut sched = Scheduler::take_local();
489495
let task = ~do Task::new(&mut sched.stack_pool) {
490496
unsafe {
491497
*count_ptr = *count_ptr + 1;
@@ -505,11 +511,11 @@ fn test_block_task() {
505511
do run_in_bare_thread {
506512
let mut sched = ~UvEventLoop::new_scheduler();
507513
let task = ~do Task::new(&mut sched.stack_pool) {
508-
let sched = Scheduler::local_take();
514+
let sched = Scheduler::take_local();
509515
assert!(sched.in_task_context());
510516
do sched.deschedule_running_task_and_then() |task| {
511517
let task = Cell(task);
512-
do Scheduler::local_borrow |sched| {
518+
do Scheduler::borrow_local |sched| {
513519
assert!(!sched.in_task_context());
514520
sched.task_queue.push_back(task.take());
515521
}

branches/try2/src/libcore/rt/uvio.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ impl IoFactory for UvIoFactory {
104104
let result_cell = empty_cell();
105105
let result_cell_ptr: *Cell<Option<~StreamObject>> = &result_cell;
106106

107-
let scheduler = Scheduler::local_take();
107+
let scheduler = Scheduler::take_local();
108108
assert!(scheduler.in_task_context());
109109

110110
// Block this task and take ownership, switch to scheduler context
111111
do scheduler.deschedule_running_task_and_then |task| {
112112

113113
rtdebug!("connect: entered scheduler context");
114-
do Scheduler::local_borrow |scheduler| {
114+
do Scheduler::borrow_local |scheduler| {
115115
assert!(!scheduler.in_task_context());
116116
}
117117
let mut tcp_watcher = TcpWatcher::new(self.uv_loop());
@@ -133,7 +133,7 @@ impl IoFactory for UvIoFactory {
133133
unsafe { (*result_cell_ptr).put_back(maybe_stream); }
134134

135135
// Context switch
136-
let scheduler = Scheduler::local_take();
136+
let scheduler = Scheduler::take_local();
137137
scheduler.resume_task_immediately(task_cell.take());
138138
}
139139
}
@@ -178,7 +178,7 @@ impl TcpListener for UvTcpListener {
178178

179179
let server_tcp_watcher = self.watcher();
180180

181-
let scheduler = Scheduler::local_take();
181+
let scheduler = Scheduler::take_local();
182182
assert!(scheduler.in_task_context());
183183

184184
do scheduler.deschedule_running_task_and_then |task| {
@@ -201,7 +201,7 @@ impl TcpListener for UvTcpListener {
201201

202202
rtdebug!("resuming task from listen");
203203
// Context switch
204-
let scheduler = Scheduler::local_take();
204+
let scheduler = Scheduler::take_local();
205205
scheduler.resume_task_immediately(task_cell.take());
206206
}
207207
}
@@ -241,13 +241,13 @@ impl Stream for UvStream {
241241
let result_cell = empty_cell();
242242
let result_cell_ptr: *Cell<Result<uint, ()>> = &result_cell;
243243

244-
let scheduler = Scheduler::local_take();
244+
let scheduler = Scheduler::take_local();
245245
assert!(scheduler.in_task_context());
246246
let watcher = self.watcher();
247247
let buf_ptr: *&mut [u8] = &buf;
248248
do scheduler.deschedule_running_task_and_then |task| {
249249
rtdebug!("read: entered scheduler context");
250-
do Scheduler::local_borrow |scheduler| {
250+
do Scheduler::borrow_local |scheduler| {
251251
assert!(!scheduler.in_task_context());
252252
}
253253
let mut watcher = watcher;
@@ -275,7 +275,7 @@ impl Stream for UvStream {
275275

276276
unsafe { (*result_cell_ptr).put_back(result); }
277277

278-
let scheduler = Scheduler::local_take();
278+
let scheduler = Scheduler::take_local();
279279
scheduler.resume_task_immediately(task_cell.take());
280280
}
281281
}
@@ -287,7 +287,7 @@ impl Stream for UvStream {
287287
fn write(&mut self, buf: &[u8]) -> Result<(), ()> {
288288
let result_cell = empty_cell();
289289
let result_cell_ptr: *Cell<Result<(), ()>> = &result_cell;
290-
let scheduler = Scheduler::local_take();
290+
let scheduler = Scheduler::take_local();
291291
assert!(scheduler.in_task_context());
292292
let watcher = self.watcher();
293293
let buf_ptr: *&[u8] = &buf;
@@ -306,7 +306,7 @@ impl Stream for UvStream {
306306

307307
unsafe { (*result_cell_ptr).put_back(result); }
308308

309-
let scheduler = Scheduler::local_take();
309+
let scheduler = Scheduler::take_local();
310310
scheduler.resume_task_immediately(task_cell.take());
311311
}
312312
}
@@ -322,8 +322,7 @@ fn test_simple_io_no_connect() {
322322
do run_in_bare_thread {
323323
let mut sched = ~UvEventLoop::new_scheduler();
324324
let task = ~do Task::new(&mut sched.stack_pool) {
325-
let sched = Scheduler::unsafe_local_borrow();
326-
let io = sched.event_loop.io().unwrap();
325+
let io = unsafe { Scheduler::borrow_local_io() };
327326
let addr = Ipv4(127, 0, 0, 1, 2926);
328327
let maybe_chan = io.connect(addr);
329328
assert!(maybe_chan.is_none());
@@ -341,16 +340,14 @@ fn test_simple_tcp_server_and_client() {
341340
let addr = Ipv4(127, 0, 0, 1, 2929);
342341

343342
let client_task = ~do Task::new(&mut sched.stack_pool) {
344-
let sched = Scheduler::unsafe_local_borrow();
345-
let io = sched.event_loop.io().unwrap();
343+
let io = unsafe { Scheduler::borrow_local_io() };
346344
let mut stream = io.connect(addr).unwrap();
347345
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
348346
stream.close();
349347
};
350348

351349
let server_task = ~do Task::new(&mut sched.stack_pool) {
352-
let sched = Scheduler::unsafe_local_borrow();
353-
let io = sched.event_loop.io().unwrap();
350+
let io = unsafe { Scheduler::borrow_local_io() };
354351
let mut listener = io.bind(addr).unwrap();
355352
let mut stream = listener.listen().unwrap();
356353
let mut buf = [0, .. 2048];
@@ -378,8 +375,7 @@ fn test_read_and_block() {
378375
let addr = Ipv4(127, 0, 0, 1, 2930);
379376

380377
let client_task = ~do Task::new(&mut sched.stack_pool) {
381-
let sched = Scheduler::unsafe_local_borrow();
382-
let io = sched.event_loop.io().unwrap();
378+
let io = unsafe { Scheduler::borrow_local_io() };
383379
let mut stream = io.connect(addr).unwrap();
384380
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
385381
stream.write([0, 1, 2, 3, 4, 5, 6, 7]);
@@ -389,8 +385,7 @@ fn test_read_and_block() {
389385
};
390386

391387
let server_task = ~do Task::new(&mut sched.stack_pool) {
392-
let sched = Scheduler::unsafe_local_borrow();
393-
let io = sched.event_loop.io().unwrap();
388+
let io = unsafe { Scheduler::borrow_local_io() };
394389
let mut listener = io.bind(addr).unwrap();
395390
let mut stream = listener.listen().unwrap();
396391
let mut buf = [0, .. 2048];
@@ -408,13 +403,13 @@ fn test_read_and_block() {
408403
}
409404
reads += 1;
410405

411-
let scheduler = Scheduler::local_take();
406+
let scheduler = Scheduler::take_local();
412407
// Yield to the other task in hopes that it
413408
// will trigger a read callback while we are
414409
// not ready for it
415410
do scheduler.deschedule_running_task_and_then |task| {
416411
let task = Cell(task);
417-
do Scheduler::local_borrow |scheduler| {
412+
do Scheduler::borrow_local |scheduler| {
418413
scheduler.task_queue.push_back(task.take());
419414
}
420415
}
@@ -441,8 +436,7 @@ fn test_read_read_read() {
441436
let addr = Ipv4(127, 0, 0, 1, 2931);
442437

443438
let client_task = ~do Task::new(&mut sched.stack_pool) {
444-
let sched = Scheduler::unsafe_local_borrow();
445-
let io = sched.event_loop.io().unwrap();
439+
let io = unsafe { Scheduler::borrow_local_io() };
446440
let mut stream = io.connect(addr).unwrap();
447441
let mut buf = [0, .. 2048];
448442
let mut total_bytes_read = 0;

0 commit comments

Comments
 (0)