2010-05-27 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Mono.Posix / Mono.Unix / UnixSignal.cs
blob86a855aeecb40de1b2e5a9ad78ceb11637303933
1 //
2 // Mono.Unix/UnixSignal.cs
3 //
4 // Authors:
5 // Jonathan Pryor (jpryor@novell.com)
6 // Tim Jenks (tim.jenks@realtimeworlds.com)
7 //
8 // (C) 2008 Novell, Inc.
9 //
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:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
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.
30 using System;
31 using System.Runtime.InteropServices;
32 using System.Threading;
34 using Mono.Unix.Native;
36 namespace Mono.Unix {
38 public class UnixSignal : WaitHandle {
39 private int signum;
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 {
64 get {
65 if (IsRealTimeSignal)
66 throw new InvalidOperationException ("This signal is a RealTimeSignum");
67 return NativeConvert.ToSignum (signum);
71 public RealTimeSignum RealTimeSignum {
72 get {
73 if (!IsRealTimeSignal)
74 throw new InvalidOperationException ("This signal is not a RealTimeSignum");
75 return NativeConvert.ToRealTimeSignum (signum-GetSIGRTMIN ());
79 public bool IsRealTimeSignal {
80 get {
81 AssertValid ();
82 int sigrtmin = GetSIGRTMIN ();
83 if (sigrtmin == -1)
84 return false;
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 ();
100 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
101 EntryPoint="Mono_Unix_UnixSignal_WaitAny")]
102 private static extern int WaitAny (IntPtr[] infos, int count, int timeout, Mono_Posix_RuntimeIsShuttingDown shutting_down);
104 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
105 EntryPoint="Mono_Posix_SIGRTMIN")]
106 internal static extern int GetSIGRTMIN ();
108 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
109 EntryPoint="Mono_Posix_SIGRTMAX")]
110 internal static extern int GetSIGRTMAX ();
112 private void AssertValid ()
114 if (signal_info == IntPtr.Zero)
115 throw new ObjectDisposedException (GetType().FullName);
118 private unsafe SignalInfo* Info {
119 get {
120 AssertValid ();
121 return (SignalInfo*) signal_info;
125 public bool IsSet {
126 get {
127 return Count > 0;
131 public unsafe bool Reset ()
133 int n = Interlocked.Exchange (ref Info->count, 0);
134 return n != 0;
137 public unsafe int Count {
138 get {return Info->count;}
139 set {Interlocked.Exchange (ref Info->count, value);}
142 [Map]
143 struct SignalInfo {
144 public int signum, count, read_fd, write_fd, have_handler, pipecnt;
145 public IntPtr handler;
148 #region WaitHandle overrides
149 protected unsafe override void Dispose (bool disposing)
151 base.Dispose (disposing);
152 if (signal_info == IntPtr.Zero)
153 return;
154 uninstall (signal_info);
155 signal_info = IntPtr.Zero;
158 public override bool WaitOne ()
160 return WaitOne (-1, false);
163 public override bool WaitOne (TimeSpan timeout, bool exitContext)
165 long ms = (long) timeout.TotalMilliseconds;
166 if (ms < -1 || ms > Int32.MaxValue)
167 throw new ArgumentOutOfRangeException ("timeout");
168 return WaitOne ((int) ms, exitContext);
171 public override bool WaitOne (int millisecondsTimeout, bool exitContext)
173 AssertValid ();
174 if (exitContext)
175 throw new InvalidOperationException ("exitContext is not supported");
176 return WaitAny (new UnixSignal[]{this}, millisecondsTimeout) == 0;
178 #endregion
180 public static int WaitAny (UnixSignal[] signals)
182 return WaitAny (signals, -1);
185 public static int WaitAny (UnixSignal[] signals, TimeSpan timeout)
187 long ms = (long) timeout.TotalMilliseconds;
188 if (ms < -1 || ms > Int32.MaxValue)
189 throw new ArgumentOutOfRangeException ("timeout");
190 return WaitAny (signals, (int) ms);
193 public static unsafe int WaitAny (UnixSignal[] signals, int millisecondsTimeout)
195 if (signals == null)
196 throw new ArgumentNullException ("signals");
197 if (millisecondsTimeout < -1)
198 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
199 IntPtr[] infos = new IntPtr [signals.Length];
200 for (int i = 0; i < signals.Length; ++i) {
201 infos [i] = signals [i].signal_info;
202 if (infos [i] == IntPtr.Zero)
203 throw new InvalidOperationException ("Disposed UnixSignal");
205 return WaitAny (infos, infos.Length, millisecondsTimeout, () => Environment.HasShutdownStarted ? 1 : 0);