Skip to content

Commit 2042696

Browse files
committed
core::rt: Implement Local for Task
1 parent 2f99fb8 commit 2042696

File tree

7 files changed

+60
-49
lines changed

7 files changed

+60
-49
lines changed

src/libcore/logging.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ pub fn log_type<T>(level: u32, object: &T) {
6666
}
6767

6868
fn newsched_log_str(msg: ~str) {
69+
use rt::task::Task;
70+
use rt::local::Local;
71+
6972
unsafe {
70-
match rt::task::unsafe_try_borrow_local_task() {
73+
match Local::try_unsafe_borrow::<Task>() {
7174
Some(local) => {
7275
// Use the available logger
7376
(*local).logger.log(Left(msg));

src/libcore/rt/local.rs

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

11+
use option::{Option, Some, None};
1112
use rt::sched::Scheduler;
13+
use rt::task::Task;
1214
use rt::local_ptr;
1315

1416
pub trait Local {
@@ -17,6 +19,7 @@ pub trait Local {
1719
fn exists() -> bool;
1820
fn borrow(f: &fn(&mut Self));
1921
unsafe fn unsafe_borrow() -> *mut Self;
22+
unsafe fn try_unsafe_borrow() -> Option<*mut Self>;
2023
}
2124

2225
impl Local for Scheduler {
@@ -25,6 +28,44 @@ impl Local for Scheduler {
2528
fn exists() -> bool { local_ptr::exists() }
2629
fn borrow(f: &fn(&mut Scheduler)) { unsafe { local_ptr::borrow(f) } }
2730
unsafe fn unsafe_borrow() -> *mut Scheduler { local_ptr::unsafe_borrow() }
31+
unsafe fn try_unsafe_borrow() -> Option<*mut Scheduler> { abort!("unimpl") }
32+
}
33+
34+
impl Local for Task {
35+
fn put(value: ~Task) { abort!("unimpl") }
36+
fn take() -> ~Task { abort!("unimpl") }
37+
fn exists() -> bool { abort!("unimpl") }
38+
fn borrow(f: &fn(&mut Task)) {
39+
do Local::borrow::<Scheduler> |sched| {
40+
match sched.current_task {
41+
Some(~ref mut task) => {
42+
f(&mut *task.task)
43+
}
44+
None => {
45+
abort!("no scheduler")
46+
}
47+
}
48+
}
49+
}
50+
unsafe fn unsafe_borrow() -> *mut Task {
51+
match (*Local::unsafe_borrow::<Scheduler>()).current_task {
52+
Some(~ref mut task) => {
53+
let s: *mut Task = &mut *task.task;
54+
return s;
55+
}
56+
None => {
57+
// Don't fail. Infinite recursion
58+
abort!("no scheduler")
59+
}
60+
}
61+
}
62+
unsafe fn try_unsafe_borrow() -> Option<*mut Task> {
63+
if Local::exists::<Scheduler>() {
64+
Some(Local::unsafe_borrow())
65+
} else {
66+
None
67+
}
68+
}
2869
}
2970

3071
#[cfg(test)]

src/libcore/rt/task.rs

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl Task {
6363
pub fn run(&mut self, f: &fn()) {
6464
// This is just an assertion that `run` was called unsafely
6565
// and this instance of Task is still accessible.
66-
do borrow_local_task |task| {
66+
do Local::borrow::<Task> |task| {
6767
assert!(ptr::ref_eq(task, self));
6868
}
6969

@@ -88,7 +88,7 @@ impl Task {
8888
fn destroy(&mut self) {
8989
// This is just an assertion that `destroy` was called unsafely
9090
// and this instance of Task is still accessible.
91-
do borrow_local_task |task| {
91+
do Local::borrow::<Task> |task| {
9292
assert!(ptr::ref_eq(task, self));
9393
}
9494
match self.storage {
@@ -150,42 +150,6 @@ impl Unwinder {
150150
}
151151
}
152152

153-
/// Borrow a pointer to the installed local services.
154-
/// Fails (likely aborting the process) if local services are not available.
155-
pub fn borrow_local_task(f: &fn(&mut Task)) {
156-
do Local::borrow::<Scheduler> |sched| {
157-
match sched.current_task {
158-
Some(~ref mut task) => {
159-
f(&mut *task.task)
160-
}
161-
None => {
162-
fail!("no local services for schedulers yet")
163-
}
164-
}
165-
}
166-
}
167-
168-
pub unsafe fn unsafe_borrow_local_task() -> *mut Task {
169-
match (*Local::unsafe_borrow::<Scheduler>()).current_task {
170-
Some(~ref mut task) => {
171-
let s: *mut Task = &mut *task.task;
172-
return s;
173-
}
174-
None => {
175-
// Don't fail. Infinite recursion
176-
abort!("no local services for schedulers yet")
177-
}
178-
}
179-
}
180-
181-
pub unsafe fn unsafe_try_borrow_local_task() -> Option<*mut Task> {
182-
if Local::exists::<Scheduler>() {
183-
Some(unsafe_borrow_local_task())
184-
} else {
185-
None
186-
}
187-
}
188-
189153
#[cfg(test)]
190154
mod test {
191155
use rt::test::*;

src/libcore/sys.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ impl FailWithCause for &'static str {
204204
pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
205205
use option::Option;
206206
use rt::{context, OldTaskContext, TaskContext};
207-
use rt::task::{unsafe_borrow_local_task, Unwinder};
207+
use rt::task::{Task, Unwinder};
208+
use rt::local::Local;
208209

209210
let context = context();
210211
match context {
@@ -233,7 +234,7 @@ pub fn begin_unwind_(msg: *c_char, file: *c_char, line: size_t) -> ! {
233234

234235
gc::cleanup_stack_for_failure();
235236

236-
let task = unsafe_borrow_local_task();
237+
let task = Local::unsafe_borrow::<Task>();
237238
let unwinder: &mut Option<Unwinder> = &mut (*task).unwinder;
238239
match *unwinder {
239240
Some(ref mut unwinder) => unwinder.begin_unwind(),

src/libcore/task/local_data_priv.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use task::rt;
1818
use local_data::LocalDataKey;
1919

2020
use super::rt::rust_task;
21-
use rt::task::LocalStorage;
21+
use rt::task::{Task, LocalStorage};
2222

2323
pub enum Handle {
2424
OldHandle(*rust_task),
@@ -28,14 +28,14 @@ pub enum Handle {
2828
impl Handle {
2929
pub fn new() -> Handle {
3030
use rt::{context, OldTaskContext};
31-
use rt::task::unsafe_borrow_local_task;
31+
use rt::local::Local;
3232
unsafe {
3333
match context() {
3434
OldTaskContext => {
3535
OldHandle(rt::rust_get_task())
3636
}
3737
_ => {
38-
let task = unsafe_borrow_local_task();
38+
let task = Local::unsafe_borrow::<Task>();
3939
NewHandle(&mut (*task).storage)
4040
}
4141
}

src/libcore/task/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,8 @@ pub fn failing() -> bool {
504504
//! True if the running task has failed
505505
506506
use rt::{context, OldTaskContext};
507-
use rt::task::borrow_local_task;
507+
use rt::local::Local;
508+
use rt::task::Task;
508509

509510
match context() {
510511
OldTaskContext => {
@@ -514,7 +515,7 @@ pub fn failing() -> bool {
514515
}
515516
_ => {
516517
let mut unwinding = false;
517-
do borrow_local_task |local| {
518+
do Local::borrow::<Task> |local| {
518519
unwinding = match local.unwinder {
519520
Some(unwinder) => {
520521
unwinder.unwinding

src/libcore/unstable/lang.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use managed::raw::BoxRepr;
1717
use str;
1818
use sys;
1919
use rt::{context, OldTaskContext};
20-
use rt::task::borrow_local_task;
20+
use rt::task::Task;
21+
use rt::local::Local;
2122
use option::{Option, Some, None};
2223
use io;
2324
use rt::global_heap;
@@ -243,7 +244,7 @@ pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
243244
}
244245
_ => {
245246
let mut alloc = ::ptr::null();
246-
do borrow_local_task |task| {
247+
do Local::borrow::<Task> |task| {
247248
alloc = task.heap.alloc(td as *c_void, size as uint) as *c_char;
248249
}
249250
return alloc;
@@ -261,7 +262,7 @@ pub unsafe fn local_free(ptr: *c_char) {
261262
rustrt::rust_upcall_free_noswitch(ptr);
262263
}
263264
_ => {
264-
do borrow_local_task |task| {
265+
do Local::borrow::<Task> |task| {
265266
task.heap.free(ptr as *c_void);
266267
}
267268
}

0 commit comments

Comments
 (0)