Skip to content

Instantly share code, notes, and snippets.

@flaper87
Last active December 23, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save flaper87/6703281 to your computer and use it in GitHub Desktop.
Save flaper87/6703281 to your computer and use it in GitHub Desktop.
UvTimer selected.
impl RtioTimer for UvTimer {
fn sleep(&mut self, msecs: u64) {
self.sleep_uv(msecs);
if !self.optimistic_check() {
rtdebug!("sleep: Optimistic check failed, blocking on");
let scheduler: ~Scheduler = Local::take();
do scheduler.deschedule_running_task_and_then |sched, task| {
self.block_on(sched, task);
}
}
rtdebug!("sleep: stopping watcher");
do self.home_for_io_with_sched |self_, _| {
self_.watcher.stop();
}
}
fn sleep_uv(&mut self, msecs: u64) {
do self.home_for_io_with_sched |self_, _| {
rtdebug!("sleep_uv: entered scheduler context");
let state_cell = Cell::new((*self).state);
do self_.watcher.start(msecs, 0) |_, status| {
assert!(status.is_none());
state_cell.take().compare_and_swap(false, true, SeqCst);
}
rtdebug!("sleep_uv: watcher started");
}
}
}
impl SelectInner for UvTimer {
fn optimistic_check(&mut self) -> bool {
self.state.load(Acquire)
}
fn block_on(&mut self, sched: &mut Scheduler, task: BlockedTask) -> bool {
unsafe {
let task_as_state = task.cast_to_uint();
match (*self).state.load(Acquire) {
false => {
rtdebug!("timer: Timer is still running");
false
}
true => {
rtdebug!("timer: Timer ran out");
let _task = BlockedTask::cast_from_uint(task_as_state);
sched.enqueue_blocked_task(_task);
true
}
}
}
}
fn unblock_from(&mut self) -> bool {
rtdebug!("timer: unblock from");
false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment