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 ()
91 int n = Interlocked.Exchange (ref Info->count, 0);
95 public unsafe int Count {
96 get {return Info->count;}
97 set {Interlocked.Exchange (ref Info->count, value);}
102 public int signum, count, read_fd, write_fd, have_handler;
103 public IntPtr handler;
106 #region WaitHandle overrides
107 protected unsafe override void Dispose (bool disposing)
109 base.Dispose (disposing);
110 if (signal_info == IntPtr.Zero)
112 uninstall (signal_info);
113 signal_info = IntPtr.Zero;
116 public override bool WaitOne ()
118 return WaitOne (-1, false);
121 public override bool WaitOne (TimeSpan timeout, bool exitContext)
123 long ms = (long) timeout.TotalMilliseconds;
124 if (ms < -1 || ms > Int32.MaxValue)
125 throw new ArgumentOutOfRangeException ("timeout");
126 return WaitOne ((int) ms, exitContext);
129 public override bool WaitOne (int millisecondsTimeout, bool exitContext)
133 throw new InvalidOperationException ("exitContext is not supported");
134 return WaitAny (new UnixSignal[]{this}, millisecondsTimeout) == 0;
138 public static int WaitAny (UnixSignal[] signals)
140 return WaitAny (signals, -1);
143 public static int WaitAny (UnixSignal[] signals, TimeSpan timeout)
145 long ms = (long) timeout.TotalMilliseconds;
146 if (ms < -1 || ms > Int32.MaxValue)
147 throw new ArgumentOutOfRangeException ("timeout");
148 return WaitAny (signals, (int) ms);
151 public static unsafe int WaitAny (UnixSignal[] signals, int millisecondsTimeout)
154 throw new ArgumentNullException ("signals");
155 if (millisecondsTimeout < -1)
156 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
157 IntPtr[] infos = new IntPtr [signals.Length];
158 for (int i = 0; i < signals.Length; ++i) {
159 infos [i] = signals [i].signal_info;
160 if (infos [i] == IntPtr.Zero)
161 throw new InvalidOperationException ("Disposed UnixSignal");
163 return WaitAny (infos, infos.Length, millisecondsTimeout);