From 15ece0c23ef9b2e696ea4e81bf088e37fedc5d01 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 18 Apr 2013 18:38:12 -0700 Subject: [PATCH] core: Wire up `spawn` to the new scheduler It will check which scheduler it is running under and create the correct type of task as appropriate. Most options aren't supported but basic spawning works. --- src/libcore/rt/mod.rs | 21 +++++++++++++++++++++ src/libcore/task/mod.rs | 9 +++++++++ src/libcore/task/spawn.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/src/libcore/rt/mod.rs b/src/libcore/rt/mod.rs index ba61b49d14a4..2b9f147bf627 100644 --- a/src/libcore/rt/mod.rs +++ b/src/libcore/rt/mod.rs @@ -160,3 +160,24 @@ fn test_context() { sched.run(); } } + +// For setting up tests of the new scheduler +#[cfg(test)] +pub fn run_in_newsched_task(f: ~fn()) { + use cell::Cell; + use unstable::run_in_bare_thread; + use self::sched::{Scheduler, Task}; + use self::uvio::UvEventLoop; + + let f = Cell(Cell(f)); + + do run_in_bare_thread { + let mut sched = ~UvEventLoop::new_scheduler(); + let f = f.take(); + let task = ~do Task::new(&mut sched.stack_pool) { + (f.take())(); + }; + sched.task_queue.push_back(task); + sched.run(); + } +} diff --git a/src/libcore/task/mod.rs b/src/libcore/task/mod.rs index 9a0063b746a9..a6c03638713e 100644 --- a/src/libcore/task/mod.rs +++ b/src/libcore/task/mod.rs @@ -1226,3 +1226,12 @@ fn test_spawn_thread_on_demand() { port.recv(); } + +#[test] +fn test_simple_newsched_spawn() { + use rt::run_in_newsched_task; + + do run_in_newsched_task { + spawn(||()) + } +} diff --git a/src/libcore/task/spawn.rs b/src/libcore/task/spawn.rs index c71f7d26d40f..47e386029955 100644 --- a/src/libcore/task/spawn.rs +++ b/src/libcore/task/spawn.rs @@ -531,6 +531,35 @@ fn gen_child_taskgroup(linked: bool, supervised: bool) } pub fn spawn_raw(opts: TaskOpts, f: ~fn()) { + use rt::*; + + match context() { + OldTaskContext => { + spawn_raw_oldsched(opts, f) + } + TaskContext => { + spawn_raw_newsched(opts, f) + } + SchedulerContext => { + fail!(~"can't spawn from scheduler context") + } + GlobalContext => { + fail!(~"can't spawn from global context") + } + } +} + +fn spawn_raw_newsched(opts: TaskOpts, f: ~fn()) { + use rt::sched::*; + + // XXX: How to schedule a new task is a policy decision that shouldn't be made here + let mut sched = Scheduler::take_local(); + let task = ~Task::new(&mut sched.stack_pool, f); + sched.resume_task_from_running_task_direct(task); +} + +fn spawn_raw_oldsched(opts: TaskOpts, f: ~fn()) { + let (child_tg, ancestors, is_main) = gen_child_taskgroup(opts.linked, opts.supervised);