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 //! Implementation of the helper thread for the timer module
13 //! This module contains the management necessary for the timer worker thread.
14 //! This thread is responsible for performing the send()s on channels for timers
15 //! that are using channels instead of a blocking call.
17 //! The timer thread is lazily initialized, and it's shut down via the
18 //! `shutdown` function provided. It must be maintained as an invariant that
19 //! `shutdown` is only called when the entire program is finished. No new timers
20 //! can be created in the future and there must be no active timers at that
26 use std::rt::bookkeeping;
27 use std::rt::mutex::StaticNativeMutex;
33 /// A structure for management of a helper thread.
35 /// This is generally a static structure which tracks the lifetime of a helper
38 /// The fields of this helper are all public, but they should not be used, this
39 /// is for static initialization.
40 pub struct Helper<M> {
41 /// Internal lock which protects the remaining fields
42 pub lock: StaticNativeMutex,
44 // You'll notice that the remaining fields are Unsafe<T>, and this is
45 // because all helper thread operations are done through &self, but we need
46 // these to be mutable (once `lock` is held).
48 /// Lazily allocated channel to send messages to the helper thread.
49 pub chan: Unsafe<*mut Sender<M>>,
51 /// OS handle used to wake up a blocked helper thread
52 pub signal: Unsafe<uint>,
54 /// Flag if this helper thread has booted and been initialized yet.
55 pub initialized: Unsafe<bool>,
58 macro_rules! helper_init( (static mut $name:ident: Helper<$m:ty>) => (
59 static mut $name: Helper<$m> = Helper {
60 lock: ::std::rt::mutex::NATIVE_MUTEX_INIT,
61 chan: ::std::ty::Unsafe {
62 value: 0 as *mut Sender<$m>,
63 marker1: ::std::kinds::marker::InvariantType,
65 signal: ::std::ty::Unsafe {
67 marker1: ::std::kinds::marker::InvariantType,
69 initialized: ::std::ty::Unsafe {
71 marker1: ::std::kinds::marker::InvariantType,
76 impl<M: Send> Helper<M> {
77 /// Lazily boots a helper thread, becoming a no-op if the helper has already
80 /// This function will check to see if the thread has been initialized, and
81 /// if it has it returns quickly. If initialization has not happened yet,
82 /// the closure `f` will be run (inside of the initialization lock) and
83 /// passed to the helper thread in a separate task.
85 /// This function is safe to be called many times.
86 pub fn boot<T: Send>(&'static self,
88 helper: fn(imp::signal, Receiver<M>, T)) {
90 let _guard = self.lock.lock();
91 if !*self.initialized.get() {
92 let (tx, rx) = channel();
93 *self.chan.get() = mem::transmute(box tx);
94 let (receive, send) = imp::new();
95 *self.signal.get() = send as uint;
99 bookkeeping::decrement();
100 helper(receive, rx, t);
101 self.lock.lock().signal()
104 rt::at_exit(proc() { self.shutdown() });
105 *self.initialized.get() = true;
110 /// Sends a message to a spawned worker thread.
112 /// This is only valid if the worker thread has previously booted
113 pub fn send(&'static self, msg: M) {
115 let _guard = self.lock.lock();
117 // Must send and *then* signal to ensure that the child receives the
118 // message. Otherwise it could wake up and go to sleep before we
120 assert!(!self.chan.get().is_null());
121 (**self.chan.get()).send(msg);
122 imp::signal(*self.signal.get() as imp::signal);
126 fn shutdown(&'static self) {
128 // Shut down, but make sure this is done inside our lock to ensure
129 // that we'll always receive the exit signal when the thread
131 let guard = self.lock.lock();
133 // Close the channel by destroying it
134 let chan: Box<Sender<M>> = mem::transmute(*self.chan.get());
135 *self.chan.get() = 0 as *mut Sender<M>;
137 imp::signal(*self.signal.get() as imp::signal);
139 // Wait for the child to exit
143 // Clean up after ourselves
145 imp::close(*self.signal.get() as imp::signal);
146 *self.signal.get() = 0;
156 use io::file::FileDesc;
158 pub type signal = libc::c_int;
160 pub fn new() -> (signal, signal) {
161 let pipe = os::pipe();
162 (pipe.input, pipe.out)
165 pub fn signal(fd: libc::c_int) {
166 FileDesc::new(fd, false).inner_write([0]).ok().unwrap();
169 pub fn close(fd: libc::c_int) {
170 let _fd = FileDesc::new(fd, true);
176 use libc::{BOOL, LPCSTR, HANDLE, LPSECURITY_ATTRIBUTES, CloseHandle};
180 pub type signal = HANDLE;
182 pub fn new() -> (HANDLE, HANDLE) {
184 let handle = CreateEventA(ptr::mut_null(), libc::FALSE, libc::FALSE,
190 pub fn signal(handle: HANDLE) {
191 assert!(unsafe { SetEvent(handle) != 0 });
194 pub fn close(handle: HANDLE) {
195 assert!(unsafe { CloseHandle(handle) != 0 });
199 fn CreateEventA(lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
202 lpName: LPCSTR) -> HANDLE;
203 fn SetEvent(hEvent: HANDLE) -> BOOL;