about summary refs log tree commit diff
path: root/src/libstd/rt/uv/timer.rs
blob: eaa5e77a6da2fe71b3a67a11105dc5acee18c4f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// 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.

use libc::{c_void, c_int};
use option::Some;
use rt::uv::uvll;
use rt::uv::{Watcher, Loop, NativeHandle, TimerCallback, NullCallback};
use rt::uv::status_to_maybe_uv_error;

pub struct TimerWatcher(*uvll::uv_timer_t);
impl Watcher for TimerWatcher { }

impl TimerWatcher {
    pub fn new(loop_: &mut Loop) -> TimerWatcher {
        unsafe {
            let handle = uvll::malloc_handle(uvll::UV_TIMER);
            assert!(handle.is_not_null());
            assert!(0 == uvll::timer_init(loop_.native_handle(), handle));
            let mut watcher: TimerWatcher = NativeHandle::from_native_handle(handle);
            watcher.install_watcher_data();
            return watcher;
        }
    }

    pub fn start(&mut self, timeout: u64, repeat: u64, cb: TimerCallback) {
        {
            let data = self.get_watcher_data();
            data.timer_cb = Some(cb);
        }

        unsafe {
            uvll::timer_start(self.native_handle(), timer_cb, timeout, repeat);
        }

        extern fn timer_cb(handle: *uvll::uv_timer_t, status: c_int) {
            let mut watcher: TimerWatcher = NativeHandle::from_native_handle(handle);
            let data = watcher.get_watcher_data();
            let cb = data.timer_cb.get_ref();
            let status = status_to_maybe_uv_error(watcher, status);
            (*cb)(watcher, status);
        }
    }

    pub fn stop(&mut self) {
        unsafe {
            uvll::timer_stop(self.native_handle());
        }
    }

    pub fn close(self, cb: NullCallback) {
        let mut watcher = self;
        {
            let data = watcher.get_watcher_data();
            assert!(data.close_cb.is_none());
            data.close_cb = Some(cb);
        }

        unsafe {
            uvll::close(watcher.native_handle(), close_cb);
        }

        extern fn close_cb(handle: *uvll::uv_timer_t) {
            let mut watcher: TimerWatcher = NativeHandle::from_native_handle(handle);
            {
                let data = watcher.get_watcher_data();
                data.close_cb.take_unwrap()();
            }
            watcher.drop_watcher_data();
            unsafe {
                uvll::free_handle(handle as *c_void);
            }
        }
    }
}

impl NativeHandle<*uvll::uv_timer_t> for TimerWatcher {
    fn from_native_handle(handle: *uvll::uv_timer_t) -> TimerWatcher {
        TimerWatcher(handle)
    }
    fn native_handle(&self) -> *uvll::uv_idle_t {
        match self { &TimerWatcher(ptr) => ptr }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use rt::uv::Loop;
    use unstable::run_in_bare_thread;

    #[test]
    fn smoke_test() {
        do run_in_bare_thread {
            let mut count = 0;
            let count_ptr: *mut int = &mut count;
            let mut loop_ = Loop::new();
            let mut timer = TimerWatcher::new(&mut loop_);
            do timer.start(10, 0) |timer, status| {
                assert!(status.is_none());
                unsafe { *count_ptr += 1 };
                timer.close(||());
            }
            loop_.run();
            loop_.close();
            assert!(count == 1);
        }
    }

    #[test]
    fn start_twice() {
        do run_in_bare_thread {
            let mut count = 0;
            let count_ptr: *mut int = &mut count;
            let mut loop_ = Loop::new();
            let mut timer = TimerWatcher::new(&mut loop_);
            do timer.start(10, 0) |timer, status| {
                let mut timer = timer;
                assert!(status.is_none());
                unsafe { *count_ptr += 1 };
                do timer.start(10, 0) |timer, status| {
                    assert!(status.is_none());
                    unsafe { *count_ptr += 1 };
                    timer.close(||());
                }
            }
            loop_.run();
            loop_.close();
            assert!(count == 2);
        }
    }

    #[test]
    fn repeat_stop() {
        do run_in_bare_thread {
            let mut count = 0;
            let count_ptr: *mut int = &mut count;
            let mut loop_ = Loop::new();
            let mut timer = TimerWatcher::new(&mut loop_);
            do timer.start(1, 2) |timer, status| {
                assert!(status.is_none());
                unsafe {
                    *count_ptr += 1;

                    if *count_ptr == 10 {

                        // Stop the timer and do something else
                        let mut timer = timer;
                        timer.stop();
                        // Freeze timer so it can be captured
                        let timer = timer;

                        let mut loop_ = timer.event_loop();
                        let mut timer2 = TimerWatcher::new(&mut loop_);
                        do timer2.start(10, 0) |timer2, _| {

                            *count_ptr += 1;

                            timer2.close(||());

                            // Restart the original timer
                            let mut timer = timer;
                            do timer.start(1, 0) |timer, _| {
                                *count_ptr += 1;
                                timer.close(||());
                            }
                        }
                    }
                };
            }
            loop_.run();
            loop_.close();
            assert!(count == 12);
        }
    }

}