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.
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.
12 use std::io::signal::Signum;
13 use std::rt::rtio::RtioSignal;
15 use homing::{HomingIO, HomeHandle};
16 use super::{UvError, UvHandle};
18 use uvio::UvIoFactory;
20 pub struct SignalWatcher {
21 handle: *uvll::uv_signal_t,
24 channel: Sender<Signum>,
29 pub fn new(io: &mut UvIoFactory, signum: Signum, channel: Sender<Signum>)
30 -> Result<Box<SignalWatcher>, UvError> {
31 let s = box SignalWatcher {
32 handle: UvHandle::alloc(None::<SignalWatcher>, uvll::UV_SIGNAL),
33 home: io.make_handle(),
38 uvll::uv_signal_init(io.uv_loop(), s.handle)
42 uvll::uv_signal_start(s.handle, signal_cb, signum as c_int)
51 extern fn signal_cb(handle: *uvll::uv_signal_t, signum: c_int) {
52 let s: &mut SignalWatcher = unsafe { UvHandle::from_uv_handle(&handle) };
53 assert_eq!(signum as int, s.signal as int);
54 let _ = s.channel.send_opt(s.signal);
57 impl HomingIO for SignalWatcher {
58 fn home<'r>(&'r mut self) -> &'r mut HomeHandle { &mut self.home }
61 impl UvHandle<uvll::uv_signal_t> for SignalWatcher {
62 fn uv_handle(&self) -> *uvll::uv_signal_t { self.handle }
65 impl RtioSignal for SignalWatcher {}
67 impl Drop for SignalWatcher {
69 let _m = self.fire_homing_missile();
76 use super::super::local_loop;
78 use super::SignalWatcher;
81 fn closing_channel_during_drop_doesnt_kill_everything() {
82 // see issue #10375, relates to timers as well.
83 let (tx, rx) = channel();
84 let _signal = SignalWatcher::new(local_loop(), signal::Interrupt,
88 let _ = rx.recv_opt();
91 // when we drop the SignalWatcher we're going to destroy the channel,
92 // which must wake up the task on the other end