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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use self::Req::*;
use libc;
use mem;
use os;
use ptr;
use sync::atomic;
use comm;
use sys::c;
use sys::fs::FileDesc;
use sys_common::helper_thread::Helper;
use prelude::*;
use io::IoResult;
helper_init! { static HELPER: Helper<Req> }
pub trait Callback {
fn call(&mut self);
}
pub struct Timer {
id: uint,
inner: Option<Box<Inner>>,
}
pub struct Inner {
cb: Option<Box<Callback + Send>>,
interval: u64,
repeat: bool,
target: u64,
id: uint,
}
pub enum Req {
NewTimer(Box<Inner>),
RemoveTimer(uint, Sender<Box<Inner>>),
}
pub fn now() -> u64 {
unsafe {
let mut now: libc::timeval = mem::zeroed();
assert_eq!(c::gettimeofday(&mut now, ptr::null_mut()), 0);
return (now.tv_sec as u64) * 1000 + (now.tv_usec as u64) / 1000;
}
}
fn helper(input: libc::c_int, messages: Receiver<Req>, _: ()) {
let mut set: c::fd_set = unsafe { mem::zeroed() };
let mut fd = FileDesc::new(input, true);
let mut timeout: libc::timeval = unsafe { mem::zeroed() };
let mut active: Vec<Box<Inner>> = vec![];
let mut dead = vec![];
fn insert(t: Box<Inner>, active: &mut Vec<Box<Inner>>) {
match active.iter().position(|tm| tm.target > t.target) {
Some(pos) => { active.insert(pos, t); }
None => { active.push(t); }
}
}
fn signal(active: &mut Vec<Box<Inner>>,
dead: &mut Vec<(uint, Box<Inner>)>) {
if active.is_empty() { return }
let mut timer = active.remove(0);
let mut cb = timer.cb.take().unwrap();
cb.call();
if timer.repeat {
timer.cb = Some(cb);
timer.target += timer.interval;
insert(timer, active);
} else {
dead.push((timer.id, timer));
}
}
'outer: loop {
let timeout = if active.len() == 0 {
ptr::null_mut()
} else {
let now = now();
if active[0].target <= now {
signal(&mut active, &mut dead);
continue;
}
let tm = active[0].target - now;
timeout.tv_sec = (tm / 1000) as libc::time_t;
timeout.tv_usec = ((tm % 1000) * 1000) as libc::suseconds_t;
&mut timeout as *mut libc::timeval
};
c::fd_set(&mut set, input);
match unsafe {
c::select(input + 1, &mut set, ptr::null_mut(),
ptr::null_mut(), timeout)
} {
0 => signal(&mut active, &mut dead),
1 => {
loop {
match messages.try_recv() {
Err(comm::Disconnected) => {
assert!(active.len() == 0);
break 'outer;
}
Ok(NewTimer(timer)) => insert(timer, &mut active),
Ok(RemoveTimer(id, ack)) => {
match dead.iter().position(|&(i, _)| id == i) {
Some(i) => {
let (_, i) = dead.remove(i);
ack.send(i);
continue
}
None => {}
}
let i = active.iter().position(|i| i.id == id);
let i = i.expect("no timer found");
let t = active.remove(i);
ack.send(t);
}
Err(..) => break
}
}
let mut buf = [0];
assert_eq!(fd.read(&mut buf).ok().unwrap(), 1);
}
-1 if os::errno() == libc::EINTR as uint => {}
n => panic!("helper thread failed in select() with error: {} ({})",
n, os::last_os_error())
}
}
}
impl Timer {
pub fn new() -> IoResult<Timer> {
HELPER.boot(|| {}, helper);
static ID: atomic::AtomicUint = atomic::INIT_ATOMIC_UINT;
let id = ID.fetch_add(1, atomic::Relaxed);
Ok(Timer {
id: id,
inner: Some(box Inner {
cb: None,
interval: 0,
target: 0,
repeat: false,
id: id,
})
})
}
pub fn sleep(&mut self, ms: u64) {
let mut inner = self.inner();
inner.cb = None;
self.inner = Some(inner);
let mut to_sleep = libc::timespec {
tv_sec: (ms / 1000) as libc::time_t,
tv_nsec: ((ms % 1000) * 1000000) as libc::c_long,
};
while unsafe { libc::nanosleep(&to_sleep, &mut to_sleep) } != 0 {
if os::errno() as int != libc::EINTR as int {
panic!("failed to sleep, but not because of EINTR?");
}
}
}
pub fn oneshot(&mut self, msecs: u64, cb: Box<Callback + Send>) {
let now = now();
let mut inner = self.inner();
inner.repeat = false;
inner.cb = Some(cb);
inner.interval = msecs;
inner.target = now + msecs;
HELPER.send(NewTimer(inner));
}
pub fn period(&mut self, msecs: u64, cb: Box<Callback + Send>) {
let now = now();
let mut inner = self.inner();
inner.repeat = true;
inner.cb = Some(cb);
inner.interval = msecs;
inner.target = now + msecs;
HELPER.send(NewTimer(inner));
}
fn inner(&mut self) -> Box<Inner> {
match self.inner.take() {
Some(i) => i,
None => {
let (tx, rx) = channel();
HELPER.send(RemoveTimer(self.id, tx));
rx.recv()
}
}
}
}
impl Drop for Timer {
fn drop(&mut self) {
self.inner = Some(self.inner());
}
}