2011-08-10 20:48:57 -05:00
|
|
|
import cast = unsafe::reinterpret_cast;
|
2011-08-16 18:39:47 -05:00
|
|
|
import comm;
|
|
|
|
import option::some;
|
|
|
|
import option::none;
|
|
|
|
import option = option::t;
|
2011-08-17 16:42:28 -05:00
|
|
|
import ptr;
|
2011-08-10 20:48:57 -05:00
|
|
|
|
2010-08-11 23:23:34 -05:00
|
|
|
native "rust" mod rustrt {
|
2011-07-27 07:19:39 -05:00
|
|
|
fn task_sleep(time_in_us: uint);
|
2011-05-31 19:44:54 -05:00
|
|
|
fn task_yield();
|
2011-08-11 12:46:57 -05:00
|
|
|
fn task_join(t: task_id) -> int;
|
2011-07-14 22:54:57 -05:00
|
|
|
fn unsupervise();
|
2011-06-29 20:47:47 -05:00
|
|
|
fn pin_task();
|
|
|
|
fn unpin_task();
|
2011-08-09 18:07:49 -05:00
|
|
|
fn get_task_id() -> task_id;
|
2011-07-21 14:11:05 -05:00
|
|
|
|
|
|
|
type rust_chan;
|
2011-07-25 17:02:43 -05:00
|
|
|
|
|
|
|
fn set_min_stack(stack_size: uint);
|
2011-08-10 20:48:57 -05:00
|
|
|
|
|
|
|
fn new_task() -> task_id;
|
2011-08-17 16:42:28 -05:00
|
|
|
fn drop_task(task : *rust_task);
|
2011-08-10 20:48:57 -05:00
|
|
|
fn get_task_pointer(id : task_id) -> *rust_task;
|
|
|
|
fn start_task(id : task_id);
|
|
|
|
fn get_task_trampoline() -> u32;
|
2011-08-11 12:46:57 -05:00
|
|
|
|
2011-08-12 18:36:17 -05:00
|
|
|
fn migrate_alloc(alloc : *u8, target : task_id);
|
|
|
|
|
2011-08-12 08:37:10 -05:00
|
|
|
fn leak<@T>(thing : -T);
|
2010-08-11 23:23:34 -05:00
|
|
|
}
|
|
|
|
|
2011-08-16 18:39:47 -05:00
|
|
|
type rust_task = {
|
2011-08-17 16:42:28 -05:00
|
|
|
id : task,
|
2011-08-16 18:39:47 -05:00
|
|
|
mutable notify_enabled : u8,
|
2011-08-17 19:13:11 -05:00
|
|
|
mutable notify_chan : comm::chan_handle<task_notification>,
|
2011-08-17 16:42:28 -05:00
|
|
|
ctx : task_context,
|
|
|
|
stack_ptr : *u8
|
2011-08-16 18:39:47 -05:00
|
|
|
};
|
|
|
|
|
2011-08-17 16:42:28 -05:00
|
|
|
type task_context = {
|
|
|
|
regs : x86_registers,
|
|
|
|
next : *u8
|
|
|
|
};
|
|
|
|
|
|
|
|
resource rust_task_ptr(task : *rust_task) {
|
|
|
|
rustrt::drop_task(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_task_ptr(id : task) -> rust_task_ptr {
|
|
|
|
ret rust_task_ptr(rustrt::get_task_pointer(id));
|
|
|
|
}
|
|
|
|
|
2011-08-16 18:39:47 -05:00
|
|
|
type task = int;
|
|
|
|
type task_id = task;
|
2011-08-09 18:07:49 -05:00
|
|
|
|
|
|
|
fn get_task_id() -> task_id {
|
|
|
|
rustrt::get_task_id()
|
|
|
|
}
|
|
|
|
|
2010-08-11 23:23:34 -05: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 07:19:39 -05:00
|
|
|
fn sleep(time_in_us: uint) { ret rustrt::task_sleep(time_in_us); }
|
2010-09-22 17:44:13 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn yield() { ret rustrt::task_yield(); }
|
2011-05-31 19:44:54 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
tag task_result { tr_success; tr_failure; }
|
2011-07-14 21:39:53 -05:00
|
|
|
|
2011-08-16 18:39:47 -05:00
|
|
|
tag task_notification {
|
|
|
|
exit(task, task_result);
|
|
|
|
}
|
|
|
|
|
2011-08-17 17:07:19 -05:00
|
|
|
fn join(task_port : (task_id, comm::port<task_notification>))
|
|
|
|
-> task_result {
|
|
|
|
let (id, port) = task_port;
|
2011-08-17 19:13:11 -05:00
|
|
|
alt comm::recv::<task_notification>(port) {
|
|
|
|
exit(_id, res) {
|
|
|
|
if _id == id { ret res }
|
|
|
|
else { fail #fmt("join received id %d, expected %d", _id, id) }
|
|
|
|
}
|
2011-08-17 17:07:19 -05:00
|
|
|
}
|
2011-08-11 12:46:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn join_id(t : task_id) -> task_result {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt rustrt::task_join(t) { 0 { tr_success } _ { tr_failure } }
|
2011-05-31 19:44:54 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn unsupervise() { ret rustrt::unsupervise(); }
|
2011-07-14 19:56:59 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn pin() { rustrt::pin_task(); }
|
2011-06-29 20:47:47 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn unpin() { rustrt::unpin_task(); }
|
2011-06-29 20:47:47 -05:00
|
|
|
|
2011-07-28 12:41:48 -05:00
|
|
|
fn set_min_stack(stack_size : uint) {
|
2011-07-25 17:02:43 -05:00
|
|
|
rustrt::set_min_stack(stack_size);
|
|
|
|
}
|
|
|
|
|
2011-08-17 13:44:35 -05:00
|
|
|
fn _spawn(thunk : -fn() -> ()) -> task {
|
2011-08-16 18:39:47 -05:00
|
|
|
spawn(thunk)
|
|
|
|
}
|
|
|
|
|
2011-08-17 13:44:35 -05:00
|
|
|
fn spawn(thunk : -fn() -> ()) -> task {
|
2011-08-16 18:39:47 -05:00
|
|
|
spawn_inner(thunk, none)
|
|
|
|
}
|
|
|
|
|
2011-08-17 19:13:11 -05:00
|
|
|
fn spawn_notify(thunk : -fn() -> (), notify : comm::chan<task_notification>)
|
2011-08-16 18:39:47 -05:00
|
|
|
-> task {
|
|
|
|
spawn_inner(thunk, some(notify))
|
|
|
|
}
|
|
|
|
|
2011-08-17 17:07:19 -05:00
|
|
|
fn spawn_joinable(thunk : -fn()) -> (task_id, comm::port<task_notification>) {
|
|
|
|
let p = comm::port::<task_notification>();
|
|
|
|
let id = spawn_notify(thunk, comm::chan::<task_notification>(p));
|
|
|
|
ret (id, p);
|
|
|
|
}
|
|
|
|
|
2011-08-10 20:48:57 -05:00
|
|
|
// FIXME: make this a fn~ once those are supported.
|
2011-08-17 13:44:35 -05:00
|
|
|
fn spawn_inner(thunk : -fn() -> (),
|
2011-08-17 19:13:11 -05:00
|
|
|
notify : option<comm::chan<task_notification>>)
|
2011-08-16 18:39:47 -05:00
|
|
|
-> task_id {
|
2011-08-10 20:48:57 -05:00
|
|
|
let id = rustrt::new_task();
|
|
|
|
|
|
|
|
// the order of arguments are outptr, taskptr, envptr.
|
2011-08-17 16:42:28 -05:00
|
|
|
// LLVM fastcall puts the first two in ecx, edx, and the rest on the
|
2011-08-10 20:48:57 -05:00
|
|
|
// stack.
|
|
|
|
|
|
|
|
// set up the task pointer
|
2011-08-17 16:42:28 -05:00
|
|
|
let task_ptr = get_task_ptr(id);
|
|
|
|
let regs = ptr::addr_of((**task_ptr).ctx.regs);
|
|
|
|
(*regs).edx = cast(*task_ptr);
|
|
|
|
(*regs).esp = cast((**task_ptr).stack_ptr);
|
|
|
|
|
|
|
|
assert ptr::null() != (**task_ptr).stack_ptr;
|
2011-08-10 20:48:57 -05:00
|
|
|
|
|
|
|
let raw_thunk : { code: u32, env: u32 } = cast(thunk);
|
|
|
|
(*regs).eip = raw_thunk.code;
|
|
|
|
|
2011-08-16 18:39:47 -05:00
|
|
|
// set up notifications if they are enabled.
|
|
|
|
alt notify {
|
|
|
|
some(c) {
|
2011-08-17 16:42:28 -05:00
|
|
|
(**task_ptr).notify_enabled = 1u8;
|
2011-08-17 19:13:11 -05:00
|
|
|
(**task_ptr).notify_chan = *c;
|
2011-08-16 18:39:47 -05:00
|
|
|
}
|
|
|
|
none {}
|
|
|
|
};
|
|
|
|
|
2011-08-10 20:48:57 -05:00
|
|
|
// okay, now we align the stack and add the environment pointer and a fake
|
|
|
|
// return address.
|
|
|
|
|
|
|
|
// -12 for the taskm output location, the env pointer
|
|
|
|
// -4 for the return address.
|
|
|
|
(*regs).esp = align_down((*regs).esp - 12u32) - 4u32;
|
|
|
|
|
|
|
|
let ra : *mutable u32 = cast((*regs).esp);
|
|
|
|
let env : *mutable u32 = cast((*regs).esp+4u32);
|
|
|
|
let tptr : *mutable u32 = cast((*regs).esp+12u32);
|
|
|
|
|
|
|
|
// put the return pointer in ecx.
|
|
|
|
(*regs).ecx = (*regs).esp + 8u32;
|
|
|
|
|
2011-08-17 16:42:28 -05:00
|
|
|
*tptr = cast(*task_ptr);
|
2011-08-10 20:48:57 -05:00
|
|
|
*env = raw_thunk.env;
|
|
|
|
*ra = rustrt::get_task_trampoline();
|
|
|
|
|
2011-08-12 18:36:17 -05:00
|
|
|
rustrt::migrate_alloc(cast(raw_thunk.env), id);
|
2011-08-10 20:48:57 -05:00
|
|
|
rustrt::start_task(id);
|
|
|
|
|
2011-08-11 12:46:57 -05:00
|
|
|
rustrt::leak(thunk);
|
|
|
|
|
2011-08-10 20:48:57 -05:00
|
|
|
ret id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Who says we can't write an operating system in Rust?
|
|
|
|
type x86_registers = {
|
|
|
|
// This needs to match the structure in context.h
|
|
|
|
mutable eax : u32,
|
|
|
|
mutable ebx : u32,
|
|
|
|
mutable ecx : u32,
|
|
|
|
mutable edx : u32,
|
|
|
|
mutable ebp : u32,
|
|
|
|
mutable esi : u32,
|
|
|
|
mutable edi : u32,
|
|
|
|
mutable esp : u32,
|
|
|
|
|
|
|
|
mutable cs : u16,
|
|
|
|
mutable ds : u16,
|
|
|
|
mutable ss : u16,
|
|
|
|
mutable es : u16,
|
|
|
|
mutable fs : u16,
|
|
|
|
mutable gs : u16,
|
|
|
|
|
|
|
|
mutable eflags : u32,
|
|
|
|
mutable eip : u32
|
|
|
|
};
|
|
|
|
|
|
|
|
fn align_down(x : u32) -> u32 {
|
|
|
|
// Aligns x down to 16 bytes
|
|
|
|
x & !(15u32)
|
|
|
|
}
|
|
|
|
|
2010-09-22 17:44:13 -05: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 14:01:19 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-09-22 17:44:13 -05:00
|
|
|
// End:
|