]> git.lizzy.rs Git - rust.git/blob - src/librustuv/signal.rs
auto merge of #13967 : richo/rust/features/ICE-fails, r=alexcrichton
[rust.git] / src / librustuv / signal.rs
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 std::io::signal::Signum;
13 use std::rt::rtio::RtioSignal;
14
15 use homing::{HomingIO, HomeHandle};
16 use super::{UvError, UvHandle};
17 use uvll;
18 use uvio::UvIoFactory;
19
20 pub struct SignalWatcher {
21     handle: *uvll::uv_signal_t,
22     home: HomeHandle,
23
24     channel: Sender<Signum>,
25     signal: Signum,
26 }
27
28 impl SignalWatcher {
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(),
34             channel: channel,
35             signal: signum,
36         };
37         assert_eq!(unsafe {
38             uvll::uv_signal_init(io.uv_loop(), s.handle)
39         }, 0);
40
41         match unsafe {
42             uvll::uv_signal_start(s.handle, signal_cb, signum as c_int)
43         } {
44             0 => Ok(s.install()),
45             n => Err(UvError(n)),
46         }
47
48     }
49 }
50
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);
55 }
56
57 impl HomingIO for SignalWatcher {
58     fn home<'r>(&'r mut self) -> &'r mut HomeHandle { &mut self.home }
59 }
60
61 impl UvHandle<uvll::uv_signal_t> for SignalWatcher {
62     fn uv_handle(&self) -> *uvll::uv_signal_t { self.handle }
63 }
64
65 impl RtioSignal for SignalWatcher {}
66
67 impl Drop for SignalWatcher {
68     fn drop(&mut self) {
69         let _m = self.fire_homing_missile();
70         self.close();
71     }
72 }
73
74 #[cfg(test)]
75 mod test {
76     use super::super::local_loop;
77     use std::io::signal;
78     use super::SignalWatcher;
79
80     #[test]
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,
85                                          tx);
86
87         spawn(proc() {
88             let _ = rx.recv_opt();
89         });
90
91         // when we drop the SignalWatcher we're going to destroy the channel,
92         // which must wake up the task on the other end
93     }
94 }