1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Tasks implemented on top of OS threads
13 //! This module contains the implementation of the 1:1 threading module required
14 //! by rust tasks. This implements the necessary API traits laid out by std::rt
15 //! in order to spawn new tasks and deschedule the current task.
19 use std::rt::bookkeeping;
21 use std::rt::local::Local;
24 use std::rt::task::{Task, BlockedTask, SendMessage};
25 use std::rt::thread::Thread;
27 use std::task::TaskOpts;
28 use std::unstable::mutex::NativeMutex;
33 /// Creates a new Task which is ready to execute as a 1:1 task.
34 pub fn new(stack_bounds: (uint, uint)) -> Box<Task> {
35 let mut task = box Task::new();
37 ops.stack_bounds = stack_bounds;
38 task.put_runtime(ops);
42 fn ops() -> Box<Ops> {
44 lock: unsafe { NativeMutex::new() },
46 io: io::IoFactory::new(),
47 // these *should* get overwritten
52 /// Spawns a function with the default configuration
53 pub fn spawn(f: proc():Send) {
54 spawn_opts(TaskOpts::new(), f)
57 /// Spawns a new task given the configuration options and a procedure to run
59 pub fn spawn_opts(opts: TaskOpts, f: proc():Send) {
61 notify_chan, name, stack_size,
65 let mut task = box Task::new();
70 Some(chan) => { task.death.on_exit = Some(SendMessage(chan)); }
74 let stack = stack_size.unwrap_or(env::min_stack());
78 // Note that this increment must happen *before* the spawn in order to
79 // guarantee that if this task exits it will always end up waiting for the
80 // spawned task to exit.
81 bookkeeping::increment();
83 // Spawning a new OS thread guarantees that __morestack will never get
84 // triggered, but we must manually set up the actual stack bounds once this
85 // function starts executing. This raises the lower limit by a bit because
86 // by the time that this function is executing we've already consumed at
87 // least a little bit of stack (we don't know the exact byte address at
88 // which our stack started).
89 Thread::spawn_stack(stack, proc() {
90 let something_around_the_top_of_the_stack = 1;
91 let addr = &something_around_the_top_of_the_stack as *int;
92 let my_stack = addr as uint;
94 stack::record_stack_bounds(my_stack - stack + 1024, my_stack);
97 ops.stack_bounds = (my_stack - stack + 1024, my_stack);
101 task.put_runtime(ops);
102 let t = task.run(|| { f.take_unwrap()() });
104 bookkeeping::decrement();
108 // This structure is the glue between channels and the 1:1 scheduling mode. This
109 // structure is allocated once per task.
111 lock: NativeMutex, // native synchronization
112 awoken: bool, // used to prevent spurious wakeups
113 io: io::IoFactory, // local I/O factory
115 // This field holds the known bounds of the stack in (lo, hi) form. Not all
116 // native tasks necessarily know their precise bounds, hence this is
118 stack_bounds: (uint, uint),
121 impl rt::Runtime for Ops {
122 fn yield_now(~self, mut cur_task: Box<Task>) {
123 // put the task back in TLS and then invoke the OS thread yield
124 cur_task.put_runtime(self);
125 Local::put(cur_task);
129 fn maybe_yield(~self, mut cur_task: Box<Task>) {
130 // just put the task back in TLS, on OS threads we never need to
131 // opportunistically yield b/c the OS will do that for us (preemption)
132 cur_task.put_runtime(self);
133 Local::put(cur_task);
136 fn wrap(~self) -> Box<Any> {
140 fn stack_bounds(&self) -> (uint, uint) { self.stack_bounds }
142 fn can_block(&self) -> bool { true }
144 // This function gets a little interesting. There are a few safety and
145 // ownership violations going on here, but this is all done in the name of
146 // shared state. Additionally, all of the violations are protected with a
147 // mutex, so in theory there are no races.
149 // The first thing we need to do is to get a pointer to the task's internal
150 // mutex. This address will not be changing (because the task is allocated
151 // on the heap). We must have this handle separately because the task will
152 // have its ownership transferred to the given closure. We're guaranteed,
153 // however, that this memory will remain valid because *this* is the current
154 // task's execution thread.
156 // The next weird part is where ownership of the task actually goes. We
157 // relinquish it to the `f` blocking function, but upon returning this
158 // function needs to replace the task back in TLS. There is no communication
159 // from the wakeup thread back to this thread about the task pointer, and
160 // there's really no need to. In order to get around this, we cast the task
161 // to a `uint` which is then used at the end of this function to cast back
162 // to a `Box<Task>` object. Naturally, this looks like it violates
163 // ownership semantics in that there may be two `Box<Task>` objects.
165 // The fun part is that the wakeup half of this implementation knows to
166 // "forget" the task on the other end. This means that the awakening half of
167 // things silently relinquishes ownership back to this thread, but not in a
168 // way that the compiler can understand. The task's memory is always valid
169 // for both tasks because these operations are all done inside of a mutex.
171 // You'll also find that if blocking fails (the `f` function hands the
172 // BlockedTask back to us), we will `mem::forget` the handles. The
173 // reasoning for this is the same logic as above in that the task silently
174 // transfers ownership via the `uint`, not through normal compiler
177 // On a mildly unrelated note, it should also be pointed out that OS
178 // condition variables are susceptible to spurious wakeups, which we need to
179 // be ready for. In order to accomodate for this fact, we have an extra
180 // `awoken` field which indicates whether we were actually woken up via some
181 // invocation of `reawaken`. This flag is only ever accessed inside the
182 // lock, so there's no need to make it atomic.
183 fn deschedule(mut ~self, times: uint, mut cur_task: Box<Task>,
184 f: |BlockedTask| -> Result<(), BlockedTask>) {
185 let me = &mut *self as *mut Ops;
186 cur_task.put_runtime(self);
189 let cur_task_dupe = &*cur_task as *Task;
190 let task = BlockedTask::block(cur_task);
193 let guard = (*me).lock.lock();
194 (*me).awoken = false;
197 while !(*me).awoken {
201 Err(task) => { mem::forget(task.wake()); }
204 let iter = task.make_selectable(times);
205 let guard = (*me).lock.lock();
206 (*me).awoken = false;
208 // Apply the given closure to all of the "selectable tasks",
209 // bailing on the first one that produces an error. Note that
210 // care must be taken such that when an error is occurred, we
211 // may not own the task, so we may still have to wait for the
212 // task to become available. In other words, if task.wake()
213 // returns `None`, then someone else has ownership and we must
214 // wait for their signal.
215 match iter.map(f).filter_map(|a| a.err()).next() {
227 while !(*me).awoken {
231 // re-acquire ownership of the task
232 cur_task = mem::transmute(cur_task_dupe);
235 // put the task back in TLS, and everything is as it once was.
236 Local::put(cur_task);
239 // See the comments on `deschedule` for why the task is forgotten here, and
240 // why it's valid to do so.
241 fn reawaken(mut ~self, mut to_wake: Box<Task>) {
243 let me = &mut *self as *mut Ops;
244 to_wake.put_runtime(self);
245 mem::forget(to_wake);
246 let guard = (*me).lock.lock();
252 fn spawn_sibling(~self,
253 mut cur_task: Box<Task>,
256 cur_task.put_runtime(self);
257 Local::put(cur_task);
259 task::spawn_opts(opts, f);
262 fn local_io<'a>(&'a mut self) -> Option<rtio::LocalIo<'a>> {
263 Some(rtio::LocalIo::new(&mut self.io as &mut rtio::IoFactory))
269 use std::rt::local::Local;
270 use std::rt::task::Task;
272 use std::task::TaskOpts;
273 use super::{spawn, spawn_opts, Ops};
277 let (tx, rx) = channel();
286 let (tx, rx) = channel::<()>();
291 assert_eq!(rx.recv_opt(), Err(()));
296 let mut opts = TaskOpts::new();
297 opts.name = Some("test".into_maybe_owned());
298 opts.stack_size = Some(20 * 4096);
299 let (tx, rx) = channel();
300 opts.notify_chan = Some(tx);
301 spawn_opts(opts, proc() {});
302 assert!(rx.recv().is_ok());
306 fn smoke_opts_fail() {
307 let mut opts = TaskOpts::new();
308 let (tx, rx) = channel();
309 opts.notify_chan = Some(tx);
310 spawn_opts(opts, proc() { fail!() });
311 assert!(rx.recv().is_err());
316 let (tx, rx) = channel();
318 for _ in range(0, 10) { task::deschedule(); }
325 fn spawn_children() {
326 let (tx1, rx) = channel();
328 let (tx2, rx) = channel();
330 let (tx3, rx) = channel();
344 fn spawn_inherits() {
345 let (tx, rx) = channel();
348 let mut task: Box<Task> = Local::take();
349 match task.maybe_take_runtime::<Ops>() {
351 task.put_runtime(ops);