2013-10-22 15:13:18 -07:00
|
|
|
// 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.
|
|
|
|
|
2014-02-26 12:58:41 -05:00
|
|
|
use libc::c_int;
|
2013-11-10 22:46:32 -08:00
|
|
|
use std::io::signal::Signum;
|
2013-11-01 11:13:22 -07:00
|
|
|
use std::rt::rtio::RtioSignal;
|
2013-10-22 15:13:18 -07:00
|
|
|
|
2013-12-12 17:47:48 -08:00
|
|
|
use homing::{HomingIO, HomeHandle};
|
|
|
|
use super::{UvError, UvHandle};
|
2013-10-22 15:13:18 -07:00
|
|
|
use uvll;
|
2013-12-12 17:47:48 -08:00
|
|
|
use uvio::UvIoFactory;
|
2013-10-22 15:13:18 -07:00
|
|
|
|
2013-11-01 11:13:22 -07:00
|
|
|
pub struct SignalWatcher {
|
|
|
|
handle: *uvll::uv_signal_t,
|
2013-12-12 17:47:48 -08:00
|
|
|
home: HomeHandle,
|
2013-10-22 15:13:18 -07:00
|
|
|
|
2014-03-09 14:58:32 -07:00
|
|
|
channel: Sender<Signum>,
|
2013-11-01 11:13:22 -07:00
|
|
|
signal: Signum,
|
|
|
|
}
|
2013-10-22 15:13:18 -07:00
|
|
|
|
|
|
|
impl SignalWatcher {
|
2013-12-12 17:47:48 -08:00
|
|
|
pub fn new(io: &mut UvIoFactory, signum: Signum,
|
2014-03-09 14:58:32 -07:00
|
|
|
channel: Sender<Signum>) -> Result<~SignalWatcher, UvError> {
|
2013-11-09 11:02:16 -08:00
|
|
|
let s = ~SignalWatcher {
|
|
|
|
handle: UvHandle::alloc(None::<SignalWatcher>, uvll::UV_SIGNAL),
|
2013-12-12 17:47:48 -08:00
|
|
|
home: io.make_handle(),
|
2013-11-09 11:02:16 -08:00
|
|
|
channel: channel,
|
|
|
|
signal: signum,
|
|
|
|
};
|
2013-11-01 11:13:22 -07:00
|
|
|
assert_eq!(unsafe {
|
2013-12-12 17:47:48 -08:00
|
|
|
uvll::uv_signal_init(io.uv_loop(), s.handle)
|
2013-11-01 11:13:22 -07:00
|
|
|
}, 0);
|
2013-10-22 15:13:18 -07:00
|
|
|
|
2013-11-09 11:02:16 -08:00
|
|
|
match unsafe {
|
|
|
|
uvll::uv_signal_start(s.handle, signal_cb, signum as c_int)
|
|
|
|
} {
|
|
|
|
0 => Ok(s.install()),
|
|
|
|
n => Err(UvError(n)),
|
2013-10-22 15:13:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-01 11:13:22 -07:00
|
|
|
extern fn signal_cb(handle: *uvll::uv_signal_t, signum: c_int) {
|
|
|
|
let s: &mut SignalWatcher = unsafe { UvHandle::from_uv_handle(&handle) };
|
|
|
|
assert_eq!(signum as int, s.signal as int);
|
2013-12-13 18:25:26 -08:00
|
|
|
s.channel.try_send(s.signal);
|
2013-11-01 11:13:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl HomingIO for SignalWatcher {
|
2013-12-12 17:47:48 -08:00
|
|
|
fn home<'r>(&'r mut self) -> &'r mut HomeHandle { &mut self.home }
|
2013-11-01 11:13:22 -07:00
|
|
|
}
|
2013-10-22 15:13:18 -07:00
|
|
|
|
2013-11-01 11:13:22 -07:00
|
|
|
impl UvHandle<uvll::uv_signal_t> for SignalWatcher {
|
|
|
|
fn uv_handle(&self) -> *uvll::uv_signal_t { self.handle }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RtioSignal for SignalWatcher {}
|
|
|
|
|
|
|
|
impl Drop for SignalWatcher {
|
|
|
|
fn drop(&mut self) {
|
2013-11-05 23:29:11 -08:00
|
|
|
let _m = self.fire_homing_missile();
|
2013-12-18 09:57:58 -08:00
|
|
|
self.close();
|
2013-10-22 15:13:18 -07:00
|
|
|
}
|
|
|
|
}
|
2013-11-08 21:59:50 -08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::super::local_loop;
|
2013-11-10 22:46:32 -08:00
|
|
|
use std::io::signal;
|
2013-12-13 11:30:59 -08:00
|
|
|
use super::SignalWatcher;
|
2013-11-08 21:59:50 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn closing_channel_during_drop_doesnt_kill_everything() {
|
|
|
|
// see issue #10375, relates to timers as well.
|
2014-03-09 14:58:32 -07:00
|
|
|
let (tx, rx) = channel();
|
2013-11-08 21:59:50 -08:00
|
|
|
let _signal = SignalWatcher::new(local_loop(), signal::Interrupt,
|
2014-03-09 14:58:32 -07:00
|
|
|
tx);
|
2013-11-08 21:59:50 -08:00
|
|
|
|
2014-01-26 22:57:42 -05:00
|
|
|
spawn(proc() {
|
2014-03-09 14:58:32 -07:00
|
|
|
let _ = rx.recv_opt();
|
2014-01-26 22:57:42 -05:00
|
|
|
});
|
2013-11-08 21:59:50 -08:00
|
|
|
|
|
|
|
// when we drop the SignalWatcher we're going to destroy the channel,
|
|
|
|
// which must wake up the task on the other end
|
|
|
|
}
|
|
|
|
}
|