-/*
-Function: spawn
-
-Creates and executes a new child task
-
-Sets up a new task with its own call stack and schedules it to be executed.
-Upon execution the new task will call function `f` with the provided
-argument `data`.
-
-Function `f` is a bare function, meaning it may not close over any data, as do
-shared functions (fn@) and lambda blocks. `data` must be a uniquely owned
-type; it is moved into the new task and thus can no longer be accessed
-locally.
-
-Parameters:
-
-data - A unique-type value to pass to the new task
-f - A function to execute in the new task
-
-Returns:
-
-A handle to the new task
-*/
-fn spawn<T: send>(-data: T, f: fn(T)) -> task {
- spawn_inner(data, f, none)
-}
-
-/*
-Function: spawn_notify
-
-Create and execute a new child task, requesting notification upon its
-termination
-
-Immediately before termination, either on success or failure, the spawned
-task will send a <task_notification> message on the provided channel.
-*/
-fn spawn_notify<T: send>(-data: T, f: fn(T),
- notify: comm::chan<task_notification>) -> task {
- spawn_inner(data, f, some(notify))
-}
-
-/*
-Function: spawn_joinable
-
-Create and execute a task which can later be joined with the <join> function
-
-This is a convenience wrapper around spawn_notify which, when paired
-with <join> can be easily used to spawn a task then wait for it to
-complete.
-*/
-fn spawn_joinable<T: send>(-data: T, f: fn(T)) -> joinable_task {
- let p = comm::port::<task_notification>();
- let id = spawn_notify(data, f, comm::chan::<task_notification>(p));
- ret (id, p);
-}
-
-// FIXME: To transition from the unsafe spawn that spawns a shared closure to
-// the safe spawn that spawns a bare function we're going to write
-// barefunc-spawn on top of unsafe-spawn. Sadly, bind does not work reliably
-// enough to suite our needs (#1034, probably others yet to be discovered), so
-// we're going to copy the bootstrap data into a unique pointer, cast it to an
-// unsafe pointer then wrap up the bare function and the unsafe pointer in a
-// shared closure to spawn.
-//
-// After the transition this should all be rewritten.
-
-fn spawn_inner<T: send>(-data: T, f: fn(T),
- notify: option<comm::chan<task_notification>>)
- -> task unsafe {
-
- fn wrapper<T: send>(data: *u8, f: fn(T)) unsafe {
- let data: ~T = unsafe::reinterpret_cast(data);
- f(*data);
- }
-
- let data = ~data;
- let dataptr: *u8 = unsafe::reinterpret_cast(data);
- unsafe::leak(data);
- let wrapped = bind wrapper(dataptr, f);
- ret unsafe_spawn_inner(wrapped, notify);
-}
-
-// FIXME: This is the old spawn function that spawns a shared closure.
-// It is a hack and needs to be rewritten.
-fn unsafe_spawn_inner(-thunk: fn@(),
- notify: option<comm::chan<task_notification>>) ->
- task unsafe {
- let id = rustrt::new_task();
-
- let raw_thunk: {code: uint, env: uint} = cast(thunk);
-
- // set up the task pointer
- let task_ptr <- rust_task_ptr(rustrt::get_task_pointer(id));
-
- assert (ptr::null() != (**task_ptr).stack_ptr);
-
- // 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);
- *thunkfn = cast(raw_thunk.code);;
- *thunkenv = cast(raw_thunk.env);;
- // Advance the stack pointer. No need to align because
- // the native code will do that for us
- (**task_ptr).stack_ptr = cast(sp - ptrsize * 2u);
-
- // set up notifications if they are enabled.
- alt notify {
- some(c) {
- (**task_ptr).notify_enabled = 1;
- (**task_ptr).notify_chan = c;
- }
- none { }
- }
-
- // give the thunk environment's allocation to the new task
- rustrt::migrate_alloc(cast(raw_thunk.env), id);
- rustrt::start_task(id, cast(thunkfn));
- // don't cleanup the thunk in this task
- unsafe::leak(thunk);
- ret id;
-}
-