2 // Mono.Unix/UnixSignal.cs
5 // Jonathan Pryor (jpryor@novell.com)
6 // Tim Jenks (tim.jenks@realtimeworlds.com)
8 // (C) 2008 Novell, Inc.
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Runtime.InteropServices;
32 using System.Threading;
34 using Mono.Unix.Native;
38 public class UnixSignal : WaitHandle {
40 private IntPtr signal_info;
44 Stdlib.VersionCheck ();
47 public UnixSignal (Signum signum)
49 this.signum = NativeConvert.FromSignum (signum);
50 this.signal_info = install (this.signum);
51 if (this.signal_info == IntPtr.Zero) {
52 throw new ArgumentException ("Unable to handle signal", "signum");
56 public UnixSignal (Mono.Unix.Native.RealTimeSignum rtsig)
58 signum = NativeConvert.FromRealTimeSignum (rtsig);
59 this.signal_info = install (this.signum);
60 Native.Errno err = Native.Stdlib.GetLastError ();
61 if (this.signal_info == IntPtr.Zero) {
62 if (err == Native.Errno.EADDRINUSE)
63 throw new ArgumentException ("Signal registered outside of Mono.Posix", "signum");
64 throw new ArgumentException ("Unable to handle signal", "signum");
68 public Signum Signum {
71 throw new InvalidOperationException ("This signal is a RealTimeSignum");
72 return NativeConvert.ToSignum (signum);
76 public RealTimeSignum RealTimeSignum {
78 if (!IsRealTimeSignal)
79 throw new InvalidOperationException ("This signal is not a RealTimeSignum");
80 return NativeConvert.ToRealTimeSignum (signum-GetSIGRTMIN ());
84 public bool IsRealTimeSignal {
87 int sigrtmin = GetSIGRTMIN ();
90 return signum >= sigrtmin;
94 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
95 EntryPoint="Mono_Unix_UnixSignal_install", SetLastError=true)]
96 private static extern IntPtr install (int signum);
98 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
99 EntryPoint="Mono_Unix_UnixSignal_uninstall")]
100 private static extern int uninstall (IntPtr info);
102 [UnmanagedFunctionPointer (CallingConvention.Cdecl)]
103 delegate int Mono_Posix_RuntimeIsShuttingDown ();
104 static Mono_Posix_RuntimeIsShuttingDown ShuttingDown = RuntimeShuttingDownCallback;
106 static int RuntimeShuttingDownCallback ()
108 return Environment.HasShutdownStarted ? 1 : 0;
111 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
112 EntryPoint="Mono_Unix_UnixSignal_WaitAny")]
113 private static extern int WaitAny (IntPtr[] infos, int count, int timeout, Mono_Posix_RuntimeIsShuttingDown shutting_down);
115 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
116 EntryPoint="Mono_Posix_SIGRTMIN")]
117 internal static extern int GetSIGRTMIN ();
119 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
120 EntryPoint="Mono_Posix_SIGRTMAX")]
121 internal static extern int GetSIGRTMAX ();
123 private void AssertValid ()
125 if (signal_info == IntPtr.Zero)
126 throw new ObjectDisposedException (GetType().FullName);
129 private unsafe SignalInfo* Info {
132 return (SignalInfo*) signal_info;
142 public unsafe bool Reset ()
144 int n = Interlocked.Exchange (ref Info->count, 0);
148 public unsafe int Count {
149 get {return Info->count;}
150 set {Interlocked.Exchange (ref Info->count, value);}
153 // signum, count, write_fd, pipecnt, and pipelock are read from a signal handler thread
154 // count and pipelock are both read and written from the signal handler thread
157 public int signum, count, read_fd, write_fd, pipecnt, pipelock, have_handler;
158 public IntPtr handler; // Backed-up handler to restore when signal unregistered
161 #region WaitHandle overrides
162 protected unsafe override void Dispose (bool disposing)
164 base.Dispose (disposing);
165 if (signal_info == IntPtr.Zero)
167 uninstall (signal_info);
168 signal_info = IntPtr.Zero;
171 public override bool WaitOne ()
173 return WaitOne (-1, false);
176 public override bool WaitOne (TimeSpan timeout, bool exitContext)
178 long ms = (long) timeout.TotalMilliseconds;
179 if (ms < -1 || ms > Int32.MaxValue)
180 throw new ArgumentOutOfRangeException ("timeout");
181 return WaitOne ((int) ms, exitContext);
184 public override bool WaitOne (int millisecondsTimeout, bool exitContext)
188 throw new InvalidOperationException ("exitContext is not supported");
189 return WaitAny (new UnixSignal[]{this}, millisecondsTimeout) == 0;
193 public static int WaitAny (UnixSignal[] signals)
195 return WaitAny (signals, -1);
198 public static int WaitAny (UnixSignal[] signals, TimeSpan timeout)
200 long ms = (long) timeout.TotalMilliseconds;
201 if (ms < -1 || ms > Int32.MaxValue)
202 throw new ArgumentOutOfRangeException ("timeout");
203 return WaitAny (signals, (int) ms);
207 public static unsafe int WaitAny (UnixSignal[] signals, int millisecondsTimeout)
210 throw new ArgumentNullException ("signals");
211 if (millisecondsTimeout < -1)
212 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
213 IntPtr[] infos = new IntPtr [signals.Length];
214 for (int i = 0; i < signals.Length; ++i) {
215 infos [i] = signals [i].signal_info;
216 if (infos [i] == IntPtr.Zero)
217 throw new InvalidOperationException ("Disposed UnixSignal");
219 return WaitAny (infos, infos.Length, millisecondsTimeout, ShuttingDown);