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;
42 public UnixSignal (Signum signum)
44 this.signum = NativeConvert.FromSignum (signum);
45 this.signal_info = install (this.signum);
46 if (this.signal_info == IntPtr.Zero) {
47 throw new ArgumentException ("Unable to handle signal", "signum");
51 public UnixSignal (Mono.Unix.Native.RealTimeSignum rtsig)
53 signum = NativeConvert.FromRealTimeSignum (rtsig);
54 this.signal_info = install (this.signum);
55 Native.Errno err = Native.Stdlib.GetLastError ();
56 if (this.signal_info == IntPtr.Zero) {
57 if (err == Native.Errno.EADDRINUSE)
58 throw new ArgumentException ("Signal registered outside of Mono.Posix", "signum");
59 throw new ArgumentException ("Unable to handle signal", "signum");
63 public Signum Signum {
66 throw new InvalidOperationException ("This signal is a RealTimeSignum");
67 return NativeConvert.ToSignum (signum);
71 public RealTimeSignum RealTimeSignum {
73 if (!IsRealTimeSignal)
74 throw new InvalidOperationException ("This signal is not a RealTimeSignum");
75 return NativeConvert.ToRealTimeSignum (signum-GetSIGRTMIN ());
79 public bool IsRealTimeSignal {
82 int sigrtmin = GetSIGRTMIN ();
85 return signum >= sigrtmin;
89 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
90 EntryPoint="Mono_Unix_UnixSignal_install", SetLastError=true)]
91 private static extern IntPtr install (int signum);
93 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
94 EntryPoint="Mono_Unix_UnixSignal_uninstall")]
95 private static extern int uninstall (IntPtr info);
97 [UnmanagedFunctionPointer (CallingConvention.Cdecl)]
98 delegate int Mono_Posix_RuntimeIsShuttingDown ();
99 static Mono_Posix_RuntimeIsShuttingDown ShuttingDown = RuntimeShuttingDownCallback;
101 static int RuntimeShuttingDownCallback ()
103 return Environment.HasShutdownStarted ? 1 : 0;
106 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
107 EntryPoint="Mono_Unix_UnixSignal_WaitAny")]
108 private static extern int WaitAny (IntPtr[] infos, int count, int timeout, Mono_Posix_RuntimeIsShuttingDown shutting_down);
110 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
111 EntryPoint="Mono_Posix_SIGRTMIN")]
112 internal static extern int GetSIGRTMIN ();
114 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
115 EntryPoint="Mono_Posix_SIGRTMAX")]
116 internal static extern int GetSIGRTMAX ();
118 private void AssertValid ()
120 if (signal_info == IntPtr.Zero)
121 throw new ObjectDisposedException (GetType().FullName);
124 private unsafe SignalInfo* Info {
127 return (SignalInfo*) signal_info;
137 public unsafe bool Reset ()
139 int n = Interlocked.Exchange (ref Info->count, 0);
143 public unsafe int Count {
144 get {return Info->count;}
145 set {Interlocked.Exchange (ref Info->count, value);}
150 public int signum, count, read_fd, write_fd, have_handler, pipecnt;
151 public IntPtr handler;
154 #region WaitHandle overrides
155 protected unsafe override void Dispose (bool disposing)
157 base.Dispose (disposing);
158 if (signal_info == IntPtr.Zero)
160 uninstall (signal_info);
161 signal_info = IntPtr.Zero;
164 public override bool WaitOne ()
166 return WaitOne (-1, false);
169 public override bool WaitOne (TimeSpan timeout, bool exitContext)
171 long ms = (long) timeout.TotalMilliseconds;
172 if (ms < -1 || ms > Int32.MaxValue)
173 throw new ArgumentOutOfRangeException ("timeout");
174 return WaitOne ((int) ms, exitContext);
177 public override bool WaitOne (int millisecondsTimeout, bool exitContext)
181 throw new InvalidOperationException ("exitContext is not supported");
182 return WaitAny (new UnixSignal[]{this}, millisecondsTimeout) == 0;
186 public static int WaitAny (UnixSignal[] signals)
188 return WaitAny (signals, -1);
191 public static int WaitAny (UnixSignal[] signals, TimeSpan timeout)
193 long ms = (long) timeout.TotalMilliseconds;
194 if (ms < -1 || ms > Int32.MaxValue)
195 throw new ArgumentOutOfRangeException ("timeout");
196 return WaitAny (signals, (int) ms);
200 public static unsafe int WaitAny (UnixSignal[] signals, int millisecondsTimeout)
203 throw new ArgumentNullException ("signals");
204 if (millisecondsTimeout < -1)
205 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
206 IntPtr[] infos = new IntPtr [signals.Length];
207 for (int i = 0; i < signals.Length; ++i) {
208 infos [i] = signals [i].signal_info;
209 if (infos [i] == IntPtr.Zero)
210 throw new InvalidOperationException ("Disposed UnixSignal");
212 return WaitAny (infos, infos.Length, millisecondsTimeout, ShuttingDown);