Skip to content

Commit

Permalink
Implement native timers
Browse files Browse the repository at this point in the history
Native timers are a much hairier thing to deal with than green timers due to the
interface that we would like to expose (both a blocking sleep() and a
channel-based interface). I ended up implementing timers in three different ways
for the various platforms that we supports.

In all three of the implementations, there is a worker thread which does send()s
on channels for timers. This worker thread is initialized once and then
communicated to in a platform-specific manner, but there's always a shared
channel available for sending messages to the worker thread.

* Windows - I decided to use windows kernel timer objects via
  CreateWaitableTimer and SetWaitableTimer in order to provide sleeping
  capabilities. The worker thread blocks via WaitForMultipleObjects where one of
  the objects is an event that is used to wake up the helper thread (which then
  drains the incoming message channel for requests).

* Linux/(Android?) - These have the ideal interface for implementing timers,
  timerfd_create. Each timer corresponds to a timerfd, and the helper thread
  uses epoll to wait for all active timers and then send() for the next one that
  wakes up. The tricky part in this implementation is updating a timerfd, but
  see the implementation for the fun details

* OSX/FreeBSD - These obviously don't have the windows APIs, and sadly don't
  have the timerfd api available to them, so I have thrown together a solution
  which uses select() plus a timeout in order to ad-hoc-ly implement a timer
  solution for threads. The implementation is backed by a sorted array of timers
  which need to fire. As I said, this is an ad-hoc solution which is certainly
  not accurate timing-wise. I have done this implementation due to the lack of
  other primitives to provide an implementation, and I've done it the best that
  I could, but I'm sure that there's room for improvement.

I'm pretty happy with how these implementations turned out. In theory we could
drop the timerfd implementation and have linux use the select() + timeout
implementation, but it's so inaccurate that I would much rather continue to use
timerfd rather than my ad-hoc select() implementation.

The only change that I would make to the API in general is to have a generic
sleep() method on an IoFactory which doesn't require allocating a Timer object.
For everything but windows it's super-cheap to request a blocking sleep for a
set amount of time, and it's probably worth it to provide a sleep() which
doesn't do something like allocate a file descriptor on linux.
  • Loading branch information
alexcrichton committed Jan 23, 2014
1 parent 530909f commit b8e4383
Show file tree
Hide file tree
Showing 10 changed files with 1,149 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/libnative/bookkeeping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ pub fn wait_for_other_tasks() {
TASK_LOCK.wait();
}
TASK_LOCK.unlock();
TASK_LOCK.destroy();
}
}
18 changes: 17 additions & 1 deletion src/libnative/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ pub mod file;
pub mod process;
pub mod net;

#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
#[path = "timer_other.rs"]
pub mod timer;

#[cfg(target_os = "linux")]
#[cfg(target_os = "android")]
#[path = "timer_timerfd.rs"]
pub mod timer;

#[cfg(target_os = "win32")]
#[path = "timer_win32.rs"]
pub mod timer;

mod timer_helper;

type IoResult<T> = Result<T, IoError>;

fn unimpl() -> IoError {
Expand Down Expand Up @@ -249,7 +265,7 @@ impl rtio::IoFactory for IoFactory {

// misc
fn timer_init(&mut self) -> IoResult<~RtioTimer> {
Err(unimpl())
timer::Timer::new().map(|t| ~t as ~RtioTimer)
}
fn spawn(&mut self, config: ProcessConfig)
-> IoResult<(~RtioProcess, ~[Option<~RtioPipe>])> {
Expand Down
143 changes: 143 additions & 0 deletions src/libnative/io/timer_helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Implementation of the helper thread for the timer module
//!
//! This module contains the management necessary for the timer worker thread.
//! This thread is responsible for performing the send()s on channels for timers
//! that are using channels instead of a blocking call.
//!
//! The timer thread is lazily initialized, and it's shut down via the
//! `shutdown` function provided. It must be maintained as an invariant that
//! `shutdown` is only called when the entire program is finished. No new timers
//! can be created in the future and there must be no active timers at that
//! time.

use std::cast;
use std::rt;
use std::unstable::mutex::{Once, ONCE_INIT};

use bookkeeping;
use io::timer::{Req, Shutdown};
use task;

// You'll note that these variables are *not* protected by a lock. These
// variables are initialized with a Once before any Timer is created and are
// only torn down after everything else has exited. This means that these
// variables are read-only during use (after initialization) and both of which
// are safe to use concurrently.
static mut HELPER_CHAN: *mut SharedChan<Req> = 0 as *mut SharedChan<Req>;
static mut HELPER_SIGNAL: imp::signal = 0 as imp::signal;

pub fn boot(helper: fn(imp::signal, Port<Req>)) {
static mut INIT: Once = ONCE_INIT;

unsafe {
INIT.doit(|| {
let (msgp, msgc) = SharedChan::new();
HELPER_CHAN = cast::transmute(~msgc);
let (receive, send) = imp::new();
HELPER_SIGNAL = send;

do task::spawn {
bookkeeping::decrement();
helper(receive, msgp);
}

rt::at_exit(proc() { shutdown() });
})
}
}

pub fn send(req: Req) {
unsafe {
assert!(!HELPER_CHAN.is_null());
(*HELPER_CHAN).send(req);
imp::signal(HELPER_SIGNAL);
}
}

fn shutdown() {
// We want to wait for the entire helper task to exit, and in doing so it
// will attempt to decrement the global task count. When the helper was
// created, it decremented the count so it wouldn't count towards preventing
// the program to exit, so here we pair that manual decrement with a manual
// increment. We will then wait for the helper thread to exit by calling
// wait_for_other_tasks.
bookkeeping::increment();

// Request a shutdown, and then wait for the task to exit
send(Shutdown);
bookkeeping::wait_for_other_tasks();

// Clean up after ther helper thread
unsafe {
imp::close(HELPER_SIGNAL);
let _chan: ~SharedChan<Req> = cast::transmute(HELPER_CHAN);
HELPER_CHAN = 0 as *mut SharedChan<Req>;
HELPER_SIGNAL = 0 as imp::signal;
}
}

#[cfg(unix)]
mod imp {
use std::libc;
use std::os;

use io::file::FileDesc;

pub type signal = libc::c_int;

pub fn new() -> (signal, signal) {
let pipe = os::pipe();
(pipe.input, pipe.out)
}

pub fn signal(fd: libc::c_int) {
FileDesc::new(fd, false).inner_write([0]);
}

pub fn close(fd: libc::c_int) {
let _fd = FileDesc::new(fd, true);
}
}

#[cfg(windows)]
mod imp {
use std::libc::{BOOL, LPCSTR, HANDLE, LPSECURITY_ATTRIBUTES, CloseHandle};
use std::ptr;
use std::libc;

pub type signal = HANDLE;

pub fn new() -> (HANDLE, HANDLE) {
unsafe {
let handle = CreateEventA(ptr::mut_null(), libc::FALSE, libc::FALSE,
ptr::null());
(handle, handle)
}
}

pub fn signal(handle: HANDLE) {
unsafe { SetEvent(handle); }
}

pub fn close(handle: HANDLE) {
unsafe { CloseHandle(handle); }
}

extern "system" {
fn CreateEventA(lpSecurityAttributes: LPSECURITY_ATTRIBUTES,
bManualReset: BOOL,
bInitialState: BOOL,
lpName: LPCSTR) -> HANDLE;
fn SetEvent(hEvent: HANDLE) -> BOOL;
}
}
Loading

9 comments on commit b8e4383

@bors
Copy link
Contributor

@bors bors commented on b8e4383 Jan 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at alexcrichton@b8e4383

@bors
Copy link
Contributor

@bors bors commented on b8e4383 Jan 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging alexcrichton/rust/native-timer = b8e4383 into auto

@bors
Copy link
Contributor

@bors bors commented on b8e4383 Jan 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alexcrichton/rust/native-timer = b8e4383 merged ok, testing candidate = 5b98bbee

@bors
Copy link
Contributor

@bors bors commented on b8e4383 Jan 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from brson
at alexcrichton@b8e4383

@bors
Copy link
Contributor

@bors bors commented on b8e4383 Jan 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging alexcrichton/rust/native-timer = b8e4383 into auto

@bors
Copy link
Contributor

@bors bors commented on b8e4383 Jan 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alexcrichton/rust/native-timer = b8e4383 merged ok, testing candidate = 7ea063e

@bors
Copy link
Contributor

@bors bors commented on b8e4383 Jan 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on b8e4383 Jan 23, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 7ea063e

Please sign in to comment.