Skip to content

Commit dbf8966

Browse files
committed
core::rt: Move the implementation of IdleWatcher to its own file
1 parent 9138fea commit dbf8966

File tree

3 files changed

+86
-69
lines changed

3 files changed

+86
-69
lines changed

src/libcore/rt/uv/idle.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use libc::c_int;
12+
use option::{Option, Some, None};
13+
use rt::uv::uvll;
14+
use rt::uv::{Watcher, Callback, Loop, UvError, NativeHandle};
15+
use rt::uv::status_to_maybe_uv_error;
16+
17+
pub struct IdleWatcher(*uvll::uv_idle_t);
18+
impl Watcher for IdleWatcher { }
19+
20+
pub type IdleCallback = ~fn(IdleWatcher, Option<UvError>);
21+
impl Callback for IdleCallback { }
22+
23+
pub impl IdleWatcher {
24+
fn new(loop_: &mut Loop) -> IdleWatcher {
25+
unsafe {
26+
let handle = uvll::idle_new();
27+
assert!(handle.is_not_null());
28+
assert!(0 == uvll::idle_init(loop_.native_handle(), handle));
29+
let mut watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
30+
watcher.install_watcher_data();
31+
return watcher
32+
}
33+
}
34+
35+
fn start(&mut self, cb: IdleCallback) {
36+
{
37+
let data = self.get_watcher_data();
38+
data.idle_cb = Some(cb);
39+
}
40+
41+
unsafe {
42+
assert!(0 == uvll::idle_start(self.native_handle(), idle_cb))
43+
};
44+
45+
extern fn idle_cb(handle: *uvll::uv_idle_t, status: c_int) {
46+
let mut idle_watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
47+
let data = idle_watcher.get_watcher_data();
48+
let cb: &IdleCallback = data.idle_cb.get_ref();
49+
let status = status_to_maybe_uv_error(handle, status);
50+
(*cb)(idle_watcher, status);
51+
}
52+
}
53+
54+
fn stop(&mut self) {
55+
// NB: Not resetting the Rust idle_cb to None here because `stop` is likely
56+
// called from *within* the idle callback, causing a use after free
57+
58+
unsafe {
59+
assert!(0 == uvll::idle_stop(self.native_handle()));
60+
}
61+
}
62+
63+
fn close(self) {
64+
unsafe { uvll::close(self.native_handle(), close_cb) };
65+
66+
extern fn close_cb(handle: *uvll::uv_idle_t) {
67+
unsafe {
68+
let mut idle_watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
69+
idle_watcher.drop_watcher_data();
70+
uvll::idle_delete(handle);
71+
}
72+
}
73+
}
74+
}
75+
76+
impl NativeHandle<*uvll::uv_idle_t> for IdleWatcher {
77+
fn from_native_handle(handle: *uvll::uv_idle_t) -> IdleWatcher {
78+
IdleWatcher(handle)
79+
}
80+
fn native_handle(&self) -> *uvll::uv_idle_t {
81+
match self { &IdleWatcher(ptr) => ptr }
82+
}
83+
}

src/libcore/rt/uv/mod.rs

Lines changed: 2 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use rt::io::IoError;
5454
pub use self::file::{FsRequest, FsCallback};
5555
pub use self::net::{StreamWatcher, TcpWatcher};
5656
pub use self::net::{ReadCallback, AllocCallback, ConnectionCallback, ConnectCallback};
57-
57+
pub use self::idle::{IdleWatcher, IdleCallback};
5858

5959
/// The implementation of `rtio` for libuv
6060
pub mod uvio;
@@ -64,6 +64,7 @@ pub mod uvll;
6464

6565
pub mod file;
6666
pub mod net;
67+
pub mod idle;
6768

6869
/// A trait for callbacks to implement. Provides a little extra type safety
6970
/// for generic, unsafe interop functions like `set_watcher_callback`.
@@ -120,74 +121,6 @@ impl NativeHandle<*uvll::uv_loop_t> for Loop {
120121
}
121122
}
122123

123-
pub struct IdleWatcher(*uvll::uv_idle_t);
124-
impl Watcher for IdleWatcher { }
125-
126-
pub type IdleCallback = ~fn(IdleWatcher, Option<UvError>);
127-
impl Callback for IdleCallback { }
128-
129-
pub impl IdleWatcher {
130-
fn new(loop_: &mut Loop) -> IdleWatcher {
131-
unsafe {
132-
let handle = uvll::idle_new();
133-
assert!(handle.is_not_null());
134-
assert!(0 == uvll::idle_init(loop_.native_handle(), handle));
135-
let mut watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
136-
watcher.install_watcher_data();
137-
return watcher
138-
}
139-
}
140-
141-
fn start(&mut self, cb: IdleCallback) {
142-
{
143-
let data = self.get_watcher_data();
144-
data.idle_cb = Some(cb);
145-
}
146-
147-
unsafe {
148-
assert!(0 == uvll::idle_start(self.native_handle(), idle_cb))
149-
};
150-
151-
extern fn idle_cb(handle: *uvll::uv_idle_t, status: c_int) {
152-
let mut idle_watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
153-
let data = idle_watcher.get_watcher_data();
154-
let cb: &IdleCallback = data.idle_cb.get_ref();
155-
let status = status_to_maybe_uv_error(handle, status);
156-
(*cb)(idle_watcher, status);
157-
}
158-
}
159-
160-
fn stop(&mut self) {
161-
// NB: Not resetting the Rust idl_cb to None here because `stop` is likely
162-
// called from *within* the idle callback, which would cause a use after free
163-
164-
unsafe {
165-
assert!(0 == uvll::idle_stop(self.native_handle()));
166-
}
167-
}
168-
169-
fn close(self) {
170-
unsafe { uvll::close(self.native_handle(), close_cb) };
171-
172-
extern fn close_cb(handle: *uvll::uv_idle_t) {
173-
unsafe {
174-
let mut idle_watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
175-
idle_watcher.drop_watcher_data();
176-
uvll::idle_delete(handle);
177-
}
178-
}
179-
}
180-
}
181-
182-
impl NativeHandle<*uvll::uv_idle_t> for IdleWatcher {
183-
fn from_native_handle(handle: *uvll::uv_idle_t) -> IdleWatcher {
184-
IdleWatcher(handle)
185-
}
186-
fn native_handle(&self) -> *uvll::uv_idle_t {
187-
match self { &IdleWatcher(ptr) => ptr }
188-
}
189-
}
190-
191124
/// Callbacks used by StreamWatchers, set as custom data on the foreign handle
192125
struct WatcherData {
193126
read_cb: Option<ReadCallback>,

src/libcore/rt/uv/uvio.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use cast::transmute;
1717
use rt::io::IoError;
1818
use rt::io::net::ip::IpAddr;
1919
use rt::uv::*;
20+
use rt::uv::idle::IdleWatcher;
2021
use rt::rtio::*;
2122
use rt::sched::{Scheduler, local_sched};
2223
use rt::io::{standard_error, OtherIoError};

0 commit comments

Comments
 (0)