2 // Mono.Unix/UnixSignal.cs
5 // Jonathan Pryor (jpryor@novell.com)
7 // (C) 2008 Novell, Inc.
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 using System.Runtime.InteropServices;
31 using System.Threading;
33 using Mono.Unix.Native;
36 public class UnixSignal : WaitHandle {
37 private Signum signum;
38 private IntPtr signal_info;
40 public UnixSignal (Signum signum)
43 // ensure signum is a valid signal
44 int _signum = NativeConvert.FromSignum (signum);
45 this.signal_info = install (_signum);
46 if (this.signal_info == IntPtr.Zero) {
47 throw new ArgumentException ("Unable to handle signal", "signum");
51 public Signum Signum {
58 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
59 EntryPoint="Mono_Unix_UnixSignal_install")]
60 private static extern IntPtr install (int signum);
62 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
63 EntryPoint="Mono_Unix_UnixSignal_uninstall")]
64 private static extern int uninstall (IntPtr info);
66 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
67 EntryPoint="Mono_Unix_UnixSignal_WaitAny")]
68 private static extern int WaitAny (IntPtr[] infos, int count, int timeout);
70 private void AssertValid ()
72 if (signal_info == IntPtr.Zero)
73 throw new ObjectDisposedException (GetType().FullName);
76 private unsafe SignalInfo* Info {
79 return (SignalInfo*) signal_info;
89 public unsafe bool Reset ()
96 public unsafe int Count {
97 get {return Info->count;}
98 set {Info->count = value;}
103 public int signum, count, read_fd, write_fd, have_handler;
104 public IntPtr handler;
107 #region WaitHandle overrides
108 protected unsafe override void Dispose (bool disposing)
110 base.Dispose (disposing);
111 if (signal_info == IntPtr.Zero)
113 uninstall (signal_info);
114 signal_info = IntPtr.Zero;
117 public override bool WaitOne ()
119 return WaitOne (-1, false);
122 public override bool WaitOne (TimeSpan timeout, bool exitContext)
124 long ms = (long) timeout.TotalMilliseconds;
125 if (ms < -1 || ms > Int32.MaxValue)
126 throw new ArgumentOutOfRangeException ("timeout");
127 return WaitOne ((int) ms, exitContext);
130 public override bool WaitOne (int millisecondsTimeout, bool exitContext)
134 throw new InvalidOperationException ("exitContext is not supported");
135 return WaitAny (new UnixSignal[]{this}, millisecondsTimeout);
139 public static bool WaitAny (UnixSignal[] signals)
141 return WaitAny (signals, -1);
144 public static bool WaitAny (UnixSignal[] signals, TimeSpan timeout)
146 long ms = (long) timeout.TotalMilliseconds;
147 if (ms < -1 || ms > Int32.MaxValue)
148 throw new ArgumentOutOfRangeException ("timeout");
149 return WaitAny (signals, (int) ms);
152 public static unsafe bool WaitAny (UnixSignal[] signals, int millisecondsTimeout)
154 IntPtr[] infos = new IntPtr [signals.Length];
155 for (int i = 0; i < signals.Length; ++i) {
156 infos [i] = signals [i].signal_info;
157 if (infos [i] == IntPtr.Zero)
158 throw new InvalidOperationException ("Disposed UnixSignal");
160 int r = WaitAny (infos, infos.Length, millisecondsTimeout);