rt: Export a scheduler API

This commit is contained in:
Brian Anderson 2012-02-07 17:43:54 -08:00
parent f2a1aa2649
commit 2f4e7c157e
5 changed files with 72 additions and 3 deletions

View File

@ -431,16 +431,43 @@ nano_time(uint64_t *ns) {
*ns = t.time_ns();
}
extern "C" CDECL rust_sched_id
rust_get_sched_id() {
rust_task *task = rust_task_thread::get_task();
return task->sched->get_id();
}
extern "C" CDECL rust_sched_id
rust_new_sched(size_t threads) {
rust_task *task = rust_task_thread::get_task();
A(task->thread, threads > 0,
"Can't create a scheduler with no threads, silly!");
return task->kernel->create_scheduler(threads);
}
extern "C" CDECL rust_task_id
get_task_id() {
rust_task *task = rust_task_thread::get_task();
return task->user.id;
}
static rust_task_id
new_task_common(rust_scheduler *sched, rust_task *parent) {
return sched->create_task(parent, NULL);
}
extern "C" CDECL rust_task_id
new_task() {
rust_task *task = rust_task_thread::get_task();
return task->sched->create_task(task, NULL);
return new_task_common(task->sched, task);
}
extern "C" CDECL rust_task_id
rust_new_task_in_sched(rust_sched_id id) {
rust_task *task = rust_task_thread::get_task();
rust_scheduler *sched = task->kernel->get_scheduler_by_id(id);
// FIXME: What if we didn't get the scheduler?
return new_task_common(sched, task);
}
extern "C" CDECL void

View File

@ -59,10 +59,11 @@ void rust_kernel::free(void *mem) {
rust_sched_id
rust_kernel::create_scheduler(size_t num_threads) {
I(this, !sched_lock.lock_held_by_current_thread());
rust_sched_id id;
rust_scheduler *sched;
{
scoped_lock with(sched_lock);
rust_sched_id id = max_sched_id++;
id = max_sched_id++;
K(srv, id != INTPTR_MAX, "Hit the maximum scheduler id");
sched = new (this, "rust_scheduler")
rust_scheduler(this, srv, num_threads, id);
@ -72,7 +73,7 @@ rust_kernel::create_scheduler(size_t num_threads) {
live_schedulers++;
}
sched->start_task_threads();
return 0;
return id;
}
rust_scheduler *

View File

@ -49,6 +49,8 @@ public:
// Called by each thread when it terminates. When all threads
// terminate the scheduler does as well.
void release_task_thread();
rust_sched_id get_id() { return id; }
};
#endif /* RUST_SCHEDULER_H */

View File

@ -24,6 +24,9 @@ rand_free
rand_new
rand_next
refcount
rust_get_sched_id
rust_new_sched
rust_new_task_in_sched
rust_path_is_dir
rust_path_exists
rust_getcwd

View File

@ -0,0 +1,36 @@
// Tests of the runtime's scheduler interface
type sched_id = int;
type task_id = int;
type task = *ctypes::void;
type closure = *ctypes::void;
native mod rustrt {
fn rust_new_sched(num_threads: ctypes::size_t) -> sched_id;
fn rust_get_sched_id() -> sched_id;
fn rust_new_task_in_sched(id: sched_id) -> task_id;
fn start_task(id: task_id, f: closure);
}
fn main() unsafe {
let po = comm::port();
let ch = comm::chan(po);
let parent_sched_id = rustrt::rust_get_sched_id();
#error("parent %?", parent_sched_id);
let num_threads = 1u;
let new_sched_id = rustrt::rust_new_sched(num_threads);
#error("new_sched_id %?", new_sched_id);
let new_task_id = rustrt::rust_new_task_in_sched(new_sched_id);
let f = fn~() {
let child_sched_id = rustrt::rust_get_sched_id();
#error("child_sched_id %?", child_sched_id);
assert child_sched_id != parent_sched_id;
assert child_sched_id == new_sched_id;
comm::send(ch, ());
};
let fptr = unsafe::reinterpret_cast(ptr::addr_of(f));
rustrt::start_task(new_task_id, fptr);
unsafe::leak(f);
comm::recv(po);
}