Skip to content

Commit 0bda87d

Browse files
committed
Runtime removal: refactor timer
This patch continues runtime removal by moving out timer-related code into `sys`. Because this eliminates APIs in `libnative` and `librustrt`, it is a: [breaking-change] This functionality is likely to be available publicly, in some form, from `std` in the future.
1 parent 44b4da0 commit 0bda87d

File tree

6 files changed

+60
-83
lines changed

6 files changed

+60
-83
lines changed

src/libnative/io/mod.rs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,6 @@ use std::os;
2929
use std::rt::rtio::{mod, IoResult, IoError};
3030
use std::num;
3131

32-
#[cfg(any(target_os = "macos",
33-
target_os = "ios",
34-
target_os = "freebsd",
35-
target_os = "dragonfly",
36-
target_os = "android",
37-
target_os = "linux"))]
38-
#[path = "timer_unix.rs"]
39-
pub mod timer;
40-
41-
#[cfg(target_os = "windows")]
42-
#[path = "timer_windows.rs"]
43-
pub mod timer;
44-
4532
#[cfg(windows)]
4633
#[path = "tty_windows.rs"]
4734
mod tty;
@@ -112,10 +99,6 @@ impl IoFactory {
11299
}
113100

114101
impl rtio::IoFactory for IoFactory {
115-
// misc
116-
fn timer_init(&mut self) -> IoResult<Box<rtio::RtioTimer + Send>> {
117-
timer::Timer::new().map(|t| box t as Box<rtio::RtioTimer + Send>)
118-
}
119102
#[cfg(unix)]
120103
fn tty_open(&mut self, fd: c_int, _readable: bool)
121104
-> IoResult<Box<rtio::RtioTTY + Send>> {

src/libstd/io/timer.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ and create receivers which will receive notifications after a period of time.
2121

2222
use comm::{Receiver, Sender, channel};
2323
use time::Duration;
24-
use io::{IoResult, IoError};
25-
use kinds::Send;
26-
use boxed::Box;
27-
use rt::rtio::{IoFactory, LocalIo, RtioTimer, Callback};
24+
use io::IoResult;
25+
use sys::timer::Callback;
26+
use sys::timer::Timer as TimerImp;
2827

2928
/// A synchronous timer object
3029
///
@@ -69,7 +68,7 @@ use rt::rtio::{IoFactory, LocalIo, RtioTimer, Callback};
6968
/// # }
7069
/// ```
7170
pub struct Timer {
72-
obj: Box<RtioTimer + Send>,
71+
inner: TimerImp,
7372
}
7473

7574
struct TimerCallback { tx: Sender<()> }
@@ -90,9 +89,7 @@ impl Timer {
9089
/// for a number of milliseconds, or to possibly create channels which will
9190
/// get notified after an amount of time has passed.
9291
pub fn new() -> IoResult<Timer> {
93-
LocalIo::maybe_raise(|io| {
94-
io.timer_init().map(|t| Timer { obj: t })
95-
}).map_err(IoError::from_rtio_error)
92+
TimerImp::new().map(|t| Timer { inner: t })
9693
}
9794

9895
/// Blocks the current task for the specified duration.
@@ -106,7 +103,7 @@ impl Timer {
106103
// Short-circuit the timer backend for 0 duration
107104
let ms = in_ms_u64(duration);
108105
if ms == 0 { return }
109-
self.obj.sleep(ms);
106+
self.inner.sleep(ms);
110107
}
111108

112109
/// Creates a oneshot receiver which will have a notification sent when
@@ -152,7 +149,7 @@ impl Timer {
152149
let (tx, rx) = channel();
153150
// Short-circuit the timer backend for 0 duration
154151
if in_ms_u64(duration) != 0 {
155-
self.obj.oneshot(in_ms_u64(duration), box TimerCallback { tx: tx });
152+
self.inner.oneshot(in_ms_u64(duration), box TimerCallback { tx: tx });
156153
} else {
157154
tx.send(());
158155
}
@@ -213,7 +210,7 @@ impl Timer {
213210
// not clear what use a 0ms period is anyway...
214211
let ms = if ms == 0 { 1 } else { ms };
215212
let (tx, rx) = channel();
216-
self.obj.period(ms, box TimerCallback { tx: tx });
213+
self.inner.period(ms, box TimerCallback { tx: tx });
217214
return rx
218215
}
219216
}

src/libstd/sys/unix/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub mod udp;
3434
pub mod pipe;
3535
pub mod helper_signal;
3636
pub mod process;
37+
pub mod timer;
3738

3839
pub mod addrinfo {
3940
pub use sys_common::net::get_host_addresses;

src/libnative/io/timer_unix.rs renamed to src/libstd/sys/unix/timer.rs

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -47,27 +47,30 @@
4747
//! Note that all time units in this file are in *milliseconds*.
4848
4949
use libc;
50-
use std::mem;
51-
use std::os;
52-
use std::ptr;
53-
use std::rt::rtio;
54-
use std::rt::rtio::IoResult;
55-
use std::sync::atomic;
56-
use std::comm;
57-
58-
use io::c;
59-
use platform_imp::fs::FileDesc;
60-
use io::helper_thread::Helper;
50+
use mem;
51+
use os;
52+
use ptr;
53+
use sync::atomic;
54+
use comm;
55+
use sys::c;
56+
use sys::fs::FileDesc;
57+
use sys_common::helper_thread::Helper;
58+
use prelude::*;
59+
use io::IoResult;
6160

6261
helper_init!(static HELPER: Helper<Req>)
6362

63+
pub trait Callback {
64+
fn call(&mut self);
65+
}
66+
6467
pub struct Timer {
6568
id: uint,
6669
inner: Option<Box<Inner>>,
6770
}
6871

6972
pub struct Inner {
70-
cb: Option<Box<rtio::Callback + Send>>,
73+
cb: Option<Box<Callback + Send>>,
7174
interval: u64,
7275
repeat: bool,
7376
target: u64,
@@ -190,11 +193,11 @@ fn helper(input: libc::c_int, messages: Receiver<Req>, _: ()) {
190193

191194
// drain the file descriptor
192195
let mut buf = [0];
193-
assert_eq!(fd.inner_read(buf).ok().unwrap(), 1);
196+
assert_eq!(fd.read(buf).ok().unwrap(), 1);
194197
}
195198

196-
-1 if os::errno() == libc::EINTR as int => {}
197-
n => panic!("helper thread panicked in select() with error: {} ({})",
199+
-1 if os::errno() == libc::EINTR as uint => {}
200+
n => panic!("helper thread failed in select() with error: {} ({})",
198201
n, os::last_os_error())
199202
}
200203
}
@@ -220,7 +223,11 @@ impl Timer {
220223
})
221224
}
222225

223-
pub fn sleep(ms: u64) {
226+
pub fn sleep(&mut self, ms: u64) {
227+
let mut inner = self.inner();
228+
inner.cb = None; // cancel any previous request
229+
self.inner = Some(inner);
230+
224231
let mut to_sleep = libc::timespec {
225232
tv_sec: (ms / 1000) as libc::time_t,
226233
tv_nsec: ((ms % 1000) * 1000000) as libc::c_long,
@@ -232,28 +239,7 @@ impl Timer {
232239
}
233240
}
234241

235-
fn inner(&mut self) -> Box<Inner> {
236-
match self.inner.take() {
237-
Some(i) => i,
238-
None => {
239-
let (tx, rx) = channel();
240-
HELPER.send(RemoveTimer(self.id, tx));
241-
rx.recv()
242-
}
243-
}
244-
}
245-
}
246-
247-
impl rtio::RtioTimer for Timer {
248-
fn sleep(&mut self, msecs: u64) {
249-
let mut inner = self.inner();
250-
inner.cb = None; // cancel any previous request
251-
self.inner = Some(inner);
252-
253-
Timer::sleep(msecs);
254-
}
255-
256-
fn oneshot(&mut self, msecs: u64, cb: Box<rtio::Callback + Send>) {
242+
pub fn oneshot(&mut self, msecs: u64, cb: Box<Callback + Send>) {
257243
let now = now();
258244
let mut inner = self.inner();
259245

@@ -265,7 +251,7 @@ impl rtio::RtioTimer for Timer {
265251
HELPER.send(NewTimer(inner));
266252
}
267253

268-
fn period(&mut self, msecs: u64, cb: Box<rtio::Callback + Send>) {
254+
pub fn period(&mut self, msecs: u64, cb: Box<Callback + Send>) {
269255
let now = now();
270256
let mut inner = self.inner();
271257

@@ -276,6 +262,17 @@ impl rtio::RtioTimer for Timer {
276262

277263
HELPER.send(NewTimer(inner));
278264
}
265+
266+
fn inner(&mut self) -> Box<Inner> {
267+
match self.inner.take() {
268+
Some(i) => i,
269+
None => {
270+
let (tx, rx) = channel();
271+
HELPER.send(RemoveTimer(self.id, tx));
272+
rx.recv()
273+
}
274+
}
275+
}
279276
}
280277

281278
impl Drop for Timer {

src/libstd/sys/windows/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub mod udp;
4141
pub mod pipe;
4242
pub mod helper_signal;
4343
pub mod process;
44+
pub mod timer;
4445

4546
pub mod addrinfo {
4647
pub use sys_common::net::get_host_addresses;

src/libnative/io/timer_windows.rs renamed to src/libstd/sys/windows/timer.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,21 @@
2121
//! the other two implementations of timers with nothing *that* new showing up.
2222
2323
use libc;
24-
use std::ptr;
25-
use std::rt::rtio;
26-
use std::rt::rtio::{IoResult, Callback};
27-
use std::comm;
24+
use ptr;
25+
use comm;
2826

29-
use io::helper_thread::Helper;
27+
use sys::c;
28+
use sys::fs::FileDesc;
29+
use sys_common::helper_thread::Helper;
30+
use prelude::*;
31+
use io::IoResult;
3032

3133
helper_init!(static HELPER: Helper<Req>)
3234

35+
pub trait Callback {
36+
fn call(&mut self);
37+
}
38+
3339
pub struct Timer {
3440
obj: libc::HANDLE,
3541
on_worker: bool,
@@ -116,12 +122,6 @@ impl Timer {
116122
}
117123
}
118124

119-
pub fn sleep(ms: u64) {
120-
use std::rt::rtio::RtioTimer;
121-
let mut t = Timer::new().ok().expect("must allocate a timer!");
122-
t.sleep(ms);
123-
}
124-
125125
fn remove(&mut self) {
126126
if !self.on_worker { return }
127127

@@ -131,10 +131,8 @@ impl Timer {
131131

132132
self.on_worker = false;
133133
}
134-
}
135134

136-
impl rtio::RtioTimer for Timer {
137-
fn sleep(&mut self, msecs: u64) {
135+
pub fn sleep(&mut self, msecs: u64) {
138136
self.remove();
139137

140138
// there are 10^6 nanoseconds in a millisecond, and the parameter is in
@@ -148,7 +146,7 @@ impl rtio::RtioTimer for Timer {
148146
let _ = unsafe { imp::WaitForSingleObject(self.obj, libc::INFINITE) };
149147
}
150148

151-
fn oneshot(&mut self, msecs: u64, cb: Box<Callback + Send>) {
149+
pub fn oneshot(&mut self, msecs: u64, cb: Box<Callback + Send>) {
152150
self.remove();
153151

154152
// see above for the calculation
@@ -162,7 +160,7 @@ impl rtio::RtioTimer for Timer {
162160
self.on_worker = true;
163161
}
164162

165-
fn period(&mut self, msecs: u64, cb: Box<Callback + Send>) {
163+
pub fn period(&mut self, msecs: u64, cb: Box<Callback + Send>) {
166164
self.remove();
167165

168166
// see above for the calculation

0 commit comments

Comments
 (0)