Skip to content

Local::borrow tweak and basic task pinning. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 16, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/libstd/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ macro_rules! rtassert (
} )
)


// The do_abort function was originally inside the abort macro, but
// this was ICEing the compiler so it has been moved outside. Now this
// seems to work?
pub fn do_abort() -> ! {
unsafe { ::libc::abort(); }
}

macro_rules! abort(
($( $msg:expr),+) => ( {
rtdebug!($($msg),+);

do_abort();

// NB: This is in a fn to avoid putting the `unsafe` block in a macro,
// which causes spurious 'unnecessary unsafe block' warnings.
fn do_abort() -> ! {
unsafe { ::libc::abort(); }
}
::macros::do_abort();
} )
)

4 changes: 2 additions & 2 deletions src/libstd/rt/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,13 @@ impl<T> ChanOne<T> {
match oldstate {
STATE_BOTH => {
// Port is not waiting yet. Nothing to do
do Local::borrow::<Scheduler> |sched| {
do Local::borrow::<Scheduler, ()> |sched| {
rtdebug!("non-rendezvous send");
sched.metrics.non_rendezvous_sends += 1;
}
}
STATE_ONE => {
do Local::borrow::<Scheduler> |sched| {
do Local::borrow::<Scheduler, ()> |sched| {
rtdebug!("rendezvous send");
sched.metrics.rendezvous_sends += 1;
}
Expand Down
35 changes: 30 additions & 5 deletions src/libstd/rt/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub trait Local {
fn put(value: ~Self);
fn take() -> ~Self;
fn exists() -> bool;
fn borrow(f: &fn(&mut Self));
fn borrow<T>(f: &fn(&mut Self) -> T) -> T;
unsafe fn unsafe_borrow() -> *mut Self;
unsafe fn try_unsafe_borrow() -> Option<*mut Self>;
}
Expand All @@ -27,7 +27,20 @@ impl Local for Scheduler {
fn put(value: ~Scheduler) { unsafe { local_ptr::put(value) }}
fn take() -> ~Scheduler { unsafe { local_ptr::take() } }
fn exists() -> bool { local_ptr::exists() }
fn borrow(f: &fn(&mut Scheduler)) { unsafe { local_ptr::borrow(f) } }
fn borrow<T>(f: &fn(&mut Scheduler) -> T) -> T {
let mut res: Option<T> = None;
let res_ptr: *mut Option<T> = &mut res;
unsafe {
do local_ptr::borrow |sched| {
let result = f(sched);
*res_ptr = Some(result);
}
}
match res {
Some(r) => { r }
None => abort!("function failed!")
}
}
unsafe fn unsafe_borrow() -> *mut Scheduler { local_ptr::unsafe_borrow() }
unsafe fn try_unsafe_borrow() -> Option<*mut Scheduler> { abort!("unimpl") }
}
Expand All @@ -36,8 +49,8 @@ impl Local for Task {
fn put(_value: ~Task) { abort!("unimpl") }
fn take() -> ~Task { abort!("unimpl") }
fn exists() -> bool { abort!("unimpl") }
fn borrow(f: &fn(&mut Task)) {
do Local::borrow::<Scheduler> |sched| {
fn borrow<T>(f: &fn(&mut Task) -> T) -> T {
do Local::borrow::<Scheduler, T> |sched| {
match sched.current_task {
Some(~ref mut task) => {
f(&mut *task.task)
Expand Down Expand Up @@ -74,7 +87,7 @@ impl Local for IoFactoryObject {
fn put(_value: ~IoFactoryObject) { abort!("unimpl") }
fn take() -> ~IoFactoryObject { abort!("unimpl") }
fn exists() -> bool { abort!("unimpl") }
fn borrow(_f: &fn(&mut IoFactoryObject)) { abort!("unimpl") }
fn borrow<T>(_f: &fn(&mut IoFactoryObject) -> T) -> T { abort!("unimpl") }
unsafe fn unsafe_borrow() -> *mut IoFactoryObject {
let sched = Local::unsafe_borrow::<Scheduler>();
let io: *mut IoFactoryObject = (*sched).event_loop.io().unwrap();
Expand Down Expand Up @@ -115,4 +128,16 @@ mod test {
}
let _scheduler: ~Scheduler = Local::take();
}

#[test]
fn borrow_with_return() {
let scheduler = ~new_test_uv_sched();
Local::put(scheduler);
let res = do Local::borrow::<Scheduler,bool> |_sched| {
true
};
assert!(res)
let _scheduler: ~Scheduler = Local::take();
}

}
2 changes: 1 addition & 1 deletion src/libstd/rt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub fn context() -> RuntimeContext {
} else {
if Local::exists::<Scheduler>() {
let context = ::cell::empty_cell();
do Local::borrow::<Scheduler> |sched| {
do Local::borrow::<Scheduler, ()> |sched| {
if sched.in_task_context() {
context.put_back(TaskContext);
} else {
Expand Down
Loading