2011-08-10 18:48:57 -07:00
|
|
|
import cast = unsafe::reinterpret_cast;
|
2011-08-16 16:39:47 -07:00
|
|
|
import comm;
|
2011-09-12 16:13:28 -07:00
|
|
|
import option::{some, none};
|
2011-08-16 16:39:47 -07:00
|
|
|
import option = option::t;
|
2011-08-17 14:42:28 -07:00
|
|
|
import ptr;
|
2011-08-10 18:48:57 -07:00
|
|
|
|
2011-08-25 11:20:43 -07:00
|
|
|
export task;
|
|
|
|
export joinable_task;
|
|
|
|
export sleep;
|
|
|
|
export yield;
|
|
|
|
export task_notification;
|
|
|
|
export join;
|
|
|
|
export unsupervise;
|
|
|
|
export pin;
|
|
|
|
export unpin;
|
|
|
|
export set_min_stack;
|
|
|
|
export spawn;
|
|
|
|
export spawn_notify;
|
|
|
|
export spawn_joinable;
|
|
|
|
export task_result;
|
|
|
|
export tr_success;
|
|
|
|
export tr_failure;
|
|
|
|
export get_task_id;
|
|
|
|
|
2010-08-11 21:23:34 -07:00
|
|
|
native "rust" mod rustrt {
|
2011-07-27 14:19:39 +02:00
|
|
|
fn task_sleep(time_in_us: uint);
|
2011-05-31 17:44:54 -07:00
|
|
|
fn task_yield();
|
2011-08-11 10:46:57 -07:00
|
|
|
fn task_join(t: task_id) -> int;
|
2011-07-14 20:54:57 -07:00
|
|
|
fn unsupervise();
|
2011-06-29 18:47:47 -07:00
|
|
|
fn pin_task();
|
|
|
|
fn unpin_task();
|
2011-08-09 16:07:49 -07:00
|
|
|
fn get_task_id() -> task_id;
|
2011-07-21 12:11:05 -07:00
|
|
|
|
|
|
|
type rust_chan;
|
2011-07-25 15:02:43 -07:00
|
|
|
|
|
|
|
fn set_min_stack(stack_size: uint);
|
2011-08-10 18:48:57 -07:00
|
|
|
|
|
|
|
fn new_task() -> task_id;
|
2011-08-19 15:16:48 -07:00
|
|
|
fn drop_task(task: *rust_task);
|
|
|
|
fn get_task_pointer(id: task_id) -> *rust_task;
|
2011-08-11 10:46:57 -07:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
fn migrate_alloc(alloc: *u8, target: task_id);
|
2011-09-06 14:03:20 -07:00
|
|
|
fn start_task(id: task_id, closure: *u8);
|
2010-08-11 21:23:34 -07:00
|
|
|
}
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
type rust_task =
|
|
|
|
{id: task,
|
2011-08-25 11:20:43 -07:00
|
|
|
mutable notify_enabled: u32,
|
|
|
|
mutable notify_chan: comm::chan<task_notification>,
|
2011-09-06 14:03:20 -07:00
|
|
|
mutable stack_ptr: *u8};
|
2011-08-19 15:16:48 -07:00
|
|
|
|
|
|
|
resource rust_task_ptr(task: *rust_task) { rustrt::drop_task(task); }
|
2011-08-17 14:42:28 -07:00
|
|
|
|
2011-08-16 16:39:47 -07:00
|
|
|
type task = int;
|
|
|
|
type task_id = task;
|
2011-08-25 11:20:43 -07:00
|
|
|
type joinable_task = (task_id, comm::port<task_notification>);
|
2011-08-09 16:07:49 -07:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
fn get_task_id() -> task_id { rustrt::get_task_id() }
|
2011-08-09 16:07:49 -07:00
|
|
|
|
2010-08-11 21:23:34 -07:00
|
|
|
/**
|
|
|
|
* Hints the scheduler to yield this task for a specified ammount of time.
|
|
|
|
*
|
|
|
|
* arg: time_in_us maximum number of microseconds to yield control for
|
|
|
|
*/
|
2011-07-27 14:19:39 +02:00
|
|
|
fn sleep(time_in_us: uint) { ret rustrt::task_sleep(time_in_us); }
|
2010-09-22 15:44:13 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn yield() { ret rustrt::task_yield(); }
|
2011-05-31 17:44:54 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
tag task_result { tr_success; tr_failure; }
|
2011-07-14 19:39:53 -07:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
tag task_notification { exit(task, task_result); }
|
2011-08-16 16:39:47 -07:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
fn join(task_port: (task_id, comm::port<task_notification>)) -> task_result {
|
2011-08-17 15:07:19 -07:00
|
|
|
let (id, port) = task_port;
|
2011-08-17 17:13:11 -07:00
|
|
|
alt comm::recv::<task_notification>(port) {
|
|
|
|
exit(_id, res) {
|
2011-08-19 15:16:48 -07:00
|
|
|
if _id == id {
|
|
|
|
ret res
|
2011-09-01 18:49:10 -07:00
|
|
|
} else { fail #fmt["join received id %d, expected %d", _id, id] }
|
2011-08-17 17:13:11 -07:00
|
|
|
}
|
2011-08-17 15:07:19 -07:00
|
|
|
}
|
2011-08-11 10:46:57 -07:00
|
|
|
}
|
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
fn join_id(t: task_id) -> task_result {
|
2011-07-27 14:19:39 +02:00
|
|
|
alt rustrt::task_join(t) { 0 { tr_success } _ { tr_failure } }
|
2011-05-31 17:44:54 -07:00
|
|
|
}
|
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn unsupervise() { ret rustrt::unsupervise(); }
|
2011-07-14 17:56:59 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn pin() { rustrt::pin_task(); }
|
2011-06-29 18:47:47 -07:00
|
|
|
|
2011-07-27 14:19:39 +02:00
|
|
|
fn unpin() { rustrt::unpin_task(); }
|
2011-06-29 18:47:47 -07:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
fn set_min_stack(stack_size: uint) { rustrt::set_min_stack(stack_size); }
|
2011-07-25 15:02:43 -07:00
|
|
|
|
2011-09-12 12:39:38 +02:00
|
|
|
fn spawn(-thunk: fn()) -> task { spawn_inner(thunk, none) }
|
2011-08-16 16:39:47 -07:00
|
|
|
|
2011-09-12 12:39:38 +02:00
|
|
|
fn spawn_notify(-thunk: fn(), notify: comm::chan<task_notification>) -> task {
|
2011-08-16 16:39:47 -07:00
|
|
|
spawn_inner(thunk, some(notify))
|
|
|
|
}
|
|
|
|
|
2011-09-12 12:39:38 +02:00
|
|
|
fn spawn_joinable(-thunk: fn()) -> joinable_task {
|
2011-08-17 15:07:19 -07:00
|
|
|
let p = comm::port::<task_notification>();
|
|
|
|
let id = spawn_notify(thunk, comm::chan::<task_notification>(p));
|
|
|
|
ret (id, p);
|
|
|
|
}
|
|
|
|
|
2011-08-10 18:48:57 -07:00
|
|
|
// FIXME: make this a fn~ once those are supported.
|
2011-09-12 12:39:38 +02:00
|
|
|
fn spawn_inner(-thunk: fn(), notify: option<comm::chan<task_notification>>) ->
|
2011-10-12 13:31:41 -07:00
|
|
|
task_id unsafe {
|
2011-08-10 18:48:57 -07:00
|
|
|
let id = rustrt::new_task();
|
|
|
|
|
2011-09-06 14:03:20 -07:00
|
|
|
let raw_thunk: {code: u32, env: u32} = cast(thunk);
|
2011-08-10 18:48:57 -07:00
|
|
|
|
|
|
|
// set up the task pointer
|
2011-09-26 21:57:42 -07:00
|
|
|
let task_ptr <- rust_task_ptr(rustrt::get_task_pointer(id));
|
2011-08-17 14:42:28 -07:00
|
|
|
|
2011-08-19 15:16:48 -07:00
|
|
|
assert (ptr::null() != (**task_ptr).stack_ptr);
|
2011-08-10 18:48:57 -07:00
|
|
|
|
2011-09-06 14:03:20 -07:00
|
|
|
// copy the thunk from our stack to the new stack
|
|
|
|
let sp: uint = cast((**task_ptr).stack_ptr);
|
|
|
|
let ptrsize = sys::size_of::<*u8>();
|
|
|
|
let thunkfn: *mutable uint = cast(sp - ptrsize * 2u);
|
|
|
|
let thunkenv: *mutable uint = cast(sp - ptrsize);
|
2011-09-12 11:27:30 +02:00
|
|
|
*thunkfn = cast(raw_thunk.code);;
|
|
|
|
*thunkenv = cast(raw_thunk.env);;
|
2011-09-06 14:03:20 -07:00
|
|
|
// align the stack to 16 bytes
|
|
|
|
(**task_ptr).stack_ptr = cast(sp - ptrsize * 4u);
|
2011-08-10 18:48:57 -07:00
|
|
|
|
2011-08-16 16:39:47 -07:00
|
|
|
// set up notifications if they are enabled.
|
|
|
|
alt notify {
|
|
|
|
some(c) {
|
2011-08-25 11:20:43 -07:00
|
|
|
(**task_ptr).notify_enabled = 1u32;;
|
|
|
|
(**task_ptr).notify_chan = c;
|
2011-08-16 16:39:47 -07:00
|
|
|
}
|
2011-08-19 15:16:48 -07:00
|
|
|
none { }
|
2011-09-12 11:27:30 +02:00
|
|
|
}
|
2011-08-16 16:39:47 -07:00
|
|
|
|
2011-09-06 14:03:20 -07:00
|
|
|
// give the thunk environment's allocation to the new task
|
2011-08-12 16:36:17 -07:00
|
|
|
rustrt::migrate_alloc(cast(raw_thunk.env), id);
|
2011-09-06 14:03:20 -07:00
|
|
|
rustrt::start_task(id, cast(thunkfn));
|
|
|
|
// don't cleanup the thunk in this task
|
2011-08-22 18:05:34 -07:00
|
|
|
unsafe::leak(thunk);
|
2011-08-10 18:48:57 -07:00
|
|
|
ret id;
|
|
|
|
}
|
|
|
|
|
2010-09-22 15:44:13 -07:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-06-15 12:01:19 -07:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-09-22 15:44:13 -07:00
|
|
|
// End:
|