Skip to content

Commit 678761b

Browse files
committed
---
yaml --- r: 94188 b: refs/heads/try c: 7cac9fe h: refs/heads/master v: v3
1 parent 10797f4 commit 678761b

File tree

15 files changed

+211
-179
lines changed

15 files changed

+211
-179
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 0da105a8b7b6b1e0568e8ff20f6ff4b13cc7ecc2
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: a6d3e57dca68fde4effdda3e4ae2887aa535fcd6
5-
refs/heads/try: 786dea207d5b891d37e596e96dd2f84c4cb59f49
5+
refs/heads/try: 7cac9fe76349120ea2373f3ce47a561271b5e8b6
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/librustuv/lib.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,20 @@ pub struct ForbidSwitch {
162162

163163
impl ForbidSwitch {
164164
fn new(s: &'static str) -> ForbidSwitch {
165+
let mut sched = Local::borrow(None::<Scheduler>);
165166
ForbidSwitch {
166-
msg: s, sched: Local::borrow(|s: &mut Scheduler| s.sched_id())
167+
msg: s,
168+
sched: sched.get().sched_id(),
167169
}
168170
}
169171
}
170172

171173
impl Drop for ForbidSwitch {
172174
fn drop(&mut self) {
173-
assert!(self.sched == Local::borrow(|s: &mut Scheduler| s.sched_id()),
174-
"didnt want a scheduler switch: {}", self.msg);
175+
let mut sched = Local::borrow(None::<Scheduler>);
176+
assert!(self.sched == sched.get().sched_id(),
177+
"didnt want a scheduler switch: {}",
178+
self.msg);
175179
}
176180
}
177181

@@ -389,15 +393,16 @@ pub fn slice_to_uv_buf(v: &[u8]) -> Buf {
389393
#[cfg(test)]
390394
fn local_loop() -> &'static mut Loop {
391395
unsafe {
392-
cast::transmute(Local::borrow(|sched: &mut Scheduler| {
396+
cast::transmute({
397+
let mut sched = Local::borrow(None::<Scheduler>);
393398
let mut io = None;
394-
sched.event_loop.io(|i| {
399+
sched.get().event_loop.io(|i| {
395400
let (_vtable, uvio): (uint, &'static mut uvio::UvIoFactory) =
396401
cast::transmute(i);
397402
io = Some(uvio);
398403
});
399404
io.unwrap()
400-
}).uv_loop())
405+
}.uv_loop())
401406
}
402407
}
403408

branches/try/src/librustuv/macros.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ macro_rules! uvdebug (
2929

3030
// get a handle for the current scheduler
3131
macro_rules! get_handle_to_current_scheduler(
32-
() => (Local::borrow(|sched: &mut Scheduler| sched.make_handle()))
32+
() => ({
33+
let mut sched = Local::borrow(None::<Scheduler>);
34+
sched.get().make_handle()
35+
})
3336
)
3437

3538
pub fn dumb_println(args: &fmt::Arguments) {

branches/try/src/librustuv/net.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,11 +1080,10 @@ mod test {
10801080
};
10811081
10821082
unsafe fn local_io() -> &'static mut IoFactory {
1083-
Local::borrow(|sched: &mut Scheduler| {
1084-
let mut io = None;
1085-
sched.event_loop.io(|i| io = Some(i));
1086-
cast::transmute(io.unwrap())
1087-
})
1083+
let mut sched = Local::borrow(None::<Scheduler>);
1084+
let mut io = None;
1085+
sched.get().event_loop.io(|i| io = Some(i));
1086+
cast::transmute(io.unwrap())
10881087
}
10891088
10901089
let test_function: proc() = proc() {

branches/try/src/librustuv/uvio.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ pub trait HomingIO {
4545

4646
let _f = ForbidUnwind::new("going home");
4747

48-
let current_sched_id = Local::borrow(|sched: &mut Scheduler| {
49-
sched.sched_id()
50-
});
48+
let current_sched_id = {
49+
let mut sched = Local::borrow(None::<Scheduler>);
50+
sched.get().sched_id()
51+
};
5152

5253
// Only need to invoke a context switch if we're not on the right
5354
// scheduler.
@@ -59,9 +60,10 @@ pub trait HomingIO {
5960
});
6061
})
6162
}
62-
let current_sched_id = Local::borrow(|sched: &mut Scheduler| {
63-
sched.sched_id()
64-
});
63+
let current_sched_id = {
64+
let mut sched = Local::borrow(None::<Scheduler>);
65+
sched.get().sched_id()
66+
};
6567
assert!(current_sched_id == self.home().sched_id);
6668

6769
self.home().sched_id
@@ -96,7 +98,8 @@ struct HomingMissile {
9698

9799
impl HomingMissile {
98100
pub fn check(&self, msg: &'static str) {
99-
let local_id = Local::borrow(|sched: &mut Scheduler| sched.sched_id());
101+
let mut sched = Local::borrow(None::<Scheduler>);
102+
let local_id = sched.get().sched_id();
100103
assert!(local_id == self.io_home, "{}", msg);
101104
}
102105
}

branches/try/src/libstd/at_vec.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ pub mod raw {
169169
use at_vec::capacity;
170170
use cast;
171171
use cast::{transmute, transmute_copy};
172+
use option::None;
172173
use ptr;
173174
use mem;
174175
use uint;
@@ -259,9 +260,8 @@ pub mod raw {
259260
use rt::local::Local;
260261
use rt::task::Task;
261262

262-
Local::borrow(|task: &mut Task| {
263-
task.heap.realloc(ptr as *mut Box<()>, size) as *()
264-
})
263+
let mut task = Local::borrow(None::<Task>);
264+
task.get().heap.realloc(ptr as *mut Box<()>, size) as *()
265265
}
266266
}
267267

branches/try/src/libstd/rt/borrowck.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use cell::Cell;
1211
use c_str::{ToCStr, CString};
1312
use libc::{c_char, size_t};
1413
use option::{Option, None, Some};
@@ -35,7 +34,8 @@ pub struct BorrowRecord {
3534
}
3635

3736
fn try_take_task_borrow_list() -> Option<~[BorrowRecord]> {
38-
Local::borrow(|task: &mut Task| task.borrow_list.take())
37+
let mut task = Local::borrow(None::<Task>);
38+
task.get().borrow_list.take()
3939
}
4040

4141
fn swap_task_borrow_list(f: |~[BorrowRecord]| -> ~[BorrowRecord]) {
@@ -44,8 +44,9 @@ fn swap_task_borrow_list(f: |~[BorrowRecord]| -> ~[BorrowRecord]) {
4444
None => ~[]
4545
};
4646
let borrows = f(borrows);
47-
let borrows = Cell::new(borrows);
48-
Local::borrow(|task: &mut Task| task.borrow_list = Some(borrows.take()))
47+
48+
let mut task = Local::borrow(None::<Task>);
49+
task.get().borrow_list = Some(borrows)
4950
}
5051

5152
pub fn clear_task_borrow_list() {

branches/try/src/libstd/rt/comm.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use unstable::sync::UnsafeArc;
2525
use util;
2626
use util::Void;
2727
use comm::{GenericChan, GenericSmartChan, GenericPort, Peekable, SendDeferred};
28-
use cell::{Cell, RefCell};
28+
use cell::RefCell;
2929
use clone::Clone;
3030
use tuple::ImmutableTuple;
3131

@@ -169,10 +169,8 @@ impl<T: Send> ChanOne<T> {
169169
Scheduler::run_task(woken_task);
170170
});
171171
} else {
172-
let recvr = Cell::new(recvr);
173-
Local::borrow(|sched: &mut Scheduler| {
174-
sched.enqueue_blocked_task(recvr.take());
175-
})
172+
let mut sched = Local::borrow(None::<Scheduler>);
173+
sched.get().enqueue_blocked_task(recvr);
176174
}
177175
}
178176
}
@@ -230,9 +228,8 @@ impl<T: Send> SelectInner for PortOne<T> {
230228
// The optimistic check is never necessary for correctness. For testing
231229
// purposes, making it randomly return false simulates a racing sender.
232230
use rand::{Rand};
233-
let actually_check = Local::borrow(|sched: &mut Scheduler| {
234-
Rand::rand(&mut sched.rng)
235-
});
231+
let mut sched = Local::borrow(None::<Scheduler>);
232+
let actually_check = Rand::rand(&mut sched.get().rng);
236233
if actually_check {
237234
unsafe { (*self.packet()).state.load(Acquire) == STATE_ONE }
238235
} else {

branches/try/src/libstd/rt/local.rs

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,28 @@ use option::{Option, Some, None};
1212
use rt::sched::Scheduler;
1313
use rt::task::Task;
1414
use rt::local_ptr;
15-
use cell::Cell;
1615

17-
pub trait Local {
16+
/// Encapsulates some task-local data.
17+
pub trait Local<Borrowed> {
1818
fn put(value: ~Self);
1919
fn take() -> ~Self;
2020
fn exists(unused_value: Option<Self>) -> bool;
21-
fn borrow<T>(f: |&mut Self| -> T) -> T;
21+
fn borrow(unused_value: Option<Self>) -> Borrowed;
2222
unsafe fn unsafe_take() -> ~Self;
2323
unsafe fn unsafe_borrow() -> *mut Self;
2424
unsafe fn try_unsafe_borrow() -> Option<*mut Self>;
2525
}
2626

27-
impl Local for Task {
27+
impl Local<local_ptr::Borrowed<Task>> for Task {
2828
#[inline]
2929
fn put(value: ~Task) { unsafe { local_ptr::put(value) } }
3030
#[inline]
3131
fn take() -> ~Task { unsafe { local_ptr::take() } }
3232
fn exists(_: Option<Task>) -> bool { local_ptr::exists() }
33-
fn borrow<T>(f: |&mut Task| -> T) -> T {
34-
let mut res: Option<T> = None;
35-
let res_ptr: *mut Option<T> = &mut res;
33+
#[inline]
34+
fn borrow(_: Option<Task>) -> local_ptr::Borrowed<Task> {
3635
unsafe {
37-
local_ptr::borrow(|task| {
38-
let result = f(task);
39-
*res_ptr = Some(result);
40-
})
41-
}
42-
match res {
43-
Some(r) => { r }
44-
None => { rtabort!("function failed in local_borrow") }
36+
local_ptr::borrow::<Task>()
4537
}
4638
}
4739
#[inline]
@@ -54,13 +46,35 @@ impl Local for Task {
5446
}
5547
}
5648

57-
impl Local for Scheduler {
49+
/// Encapsulates a temporarily-borrowed scheduler.
50+
pub struct BorrowedScheduler {
51+
priv task: local_ptr::Borrowed<Task>,
52+
}
53+
54+
impl BorrowedScheduler {
55+
fn new(mut task: local_ptr::Borrowed<Task>) -> BorrowedScheduler {
56+
if task.get().sched.is_none() {
57+
rtabort!("no scheduler")
58+
} else {
59+
BorrowedScheduler {
60+
task: task,
61+
}
62+
}
63+
}
64+
65+
#[inline]
66+
pub fn get<'a>(&'a mut self) -> &'a mut ~Scheduler {
67+
match self.task.get().sched {
68+
None => rtabort!("no scheduler"),
69+
Some(ref mut sched) => sched,
70+
}
71+
}
72+
}
73+
74+
impl Local<BorrowedScheduler> for Scheduler {
5875
fn put(value: ~Scheduler) {
59-
let value = Cell::new(value);
60-
Local::borrow(|task: &mut Task| {
61-
let task = task;
62-
task.sched = Some(value.take());
63-
});
76+
let mut task = Local::borrow(None::<Task>);
77+
task.get().sched = Some(value);
6478
}
6579
#[inline]
6680
fn take() -> ~Scheduler {
@@ -71,24 +85,12 @@ impl Local for Scheduler {
7185
}
7286
}
7387
fn exists(_: Option<Scheduler>) -> bool {
74-
Local::borrow(|task: &mut Task| {
75-
match task.sched {
76-
Some(ref _task) => true,
77-
None => false
78-
}
79-
})
88+
let mut task = Local::borrow(None::<Task>);
89+
task.get().sched.is_some()
8090
}
81-
fn borrow<T>(f: |&mut Scheduler| -> T) -> T {
82-
Local::borrow(|task: &mut Task| {
83-
match task.sched {
84-
Some(~ref mut task) => {
85-
f(task)
86-
}
87-
None => {
88-
rtabort!("no scheduler")
89-
}
90-
}
91-
})
91+
#[inline]
92+
fn borrow(_: Option<Scheduler>) -> BorrowedScheduler {
93+
BorrowedScheduler::new(Local::borrow(None::<Task>))
9294
}
9395
unsafe fn unsafe_take() -> ~Scheduler { rtabort!("unimpl") }
9496
unsafe fn unsafe_borrow() -> *mut Scheduler {
@@ -182,11 +184,11 @@ mod test {
182184
let task = ~Task::new_root(&mut sched.stack_pool, None, proc(){});
183185
Local::put(task);
184186

185-
let res = Local::borrow(|_task: &mut Task| {
186-
true
187-
});
188-
assert!(res)
189-
let task: ~Task = Local::take();
187+
{
188+
let _ = Local::borrow(None::<Task>);
189+
}
190+
191+
let task: ~Task = Local::take();
190192
cleanup_task(task);
191193
}
192194
}

branches/try/src/libstd/rt/local_heap.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,8 @@ pub unsafe fn local_free(ptr: *libc::c_char) {
304304
}
305305

306306
pub fn live_allocs() -> *mut Box {
307-
Local::borrow(|task: &mut Task| task.heap.live_allocs)
307+
let mut task = Local::borrow(None::<Task>);
308+
task.get().heap.live_allocs
308309
}
309310

310311
#[cfg(test)]

0 commit comments

Comments
 (0)