Merge pull request #309 from i59/patch-1
[mono.git] / mcs / class / Mono.Posix / Mono.Unix / UnixSignal.cs
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.
28 //
29
30 using System;
31 using System.Runtime.InteropServices;
32 using System.Threading;
33
34 using Mono.Unix.Native;
35
36 namespace Mono.Unix {
37
38         public class UnixSignal : WaitHandle {
39                 private int signum;
40                 private IntPtr signal_info;
41
42                 public UnixSignal (Signum signum)
43                 {
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");
48                         }
49                 }
50
51                 public UnixSignal (Mono.Unix.Native.RealTimeSignum rtsig)
52                 {
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");
60                         }
61                 }
62
63                 public Signum Signum {
64                         get {
65                                 if (IsRealTimeSignal)
66                                         throw new InvalidOperationException ("This signal is a RealTimeSignum");
67                                 return NativeConvert.ToSignum (signum); 
68                         }
69                 }
70
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 ());
76                         }
77                 }
78
79                 public bool IsRealTimeSignal {
80                         get {
81                                 AssertValid ();
82                                 int sigrtmin = GetSIGRTMIN ();
83                                 if (sigrtmin == -1)
84                                         return false;
85                                 return signum >= sigrtmin;
86                         }
87                 }
88
89                 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
90                                 EntryPoint="Mono_Unix_UnixSignal_install", SetLastError=true)]
91                 private static extern IntPtr install (int signum);
92
93                 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
94                                 EntryPoint="Mono_Unix_UnixSignal_uninstall")]
95                 private static extern int uninstall (IntPtr info);
96
97                 [UnmanagedFunctionPointer (CallingConvention.Cdecl)]
98                 delegate int Mono_Posix_RuntimeIsShuttingDown ();
99                 static Mono_Posix_RuntimeIsShuttingDown ShuttingDown = RuntimeShuttingDownCallback;
100                 
101                 static int RuntimeShuttingDownCallback ()
102                 {
103                         return Environment.HasShutdownStarted ? 1 : 0;
104                 }
105
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);
109
110                 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
111                                 EntryPoint="Mono_Posix_SIGRTMIN")]
112                 internal static extern int GetSIGRTMIN ();
113
114                 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
115                                 EntryPoint="Mono_Posix_SIGRTMAX")]
116                 internal static extern int GetSIGRTMAX ();
117
118                 private void AssertValid ()
119                 {
120                         if (signal_info == IntPtr.Zero)
121                                 throw new ObjectDisposedException (GetType().FullName);
122                 }
123
124                 private unsafe SignalInfo* Info {
125                         get {
126                                 AssertValid ();
127                                 return (SignalInfo*) signal_info;
128                         }
129                 }
130
131                 public bool IsSet {
132                         get {
133                                 return Count > 0;
134                         }
135                 }
136
137                 public unsafe bool Reset ()
138                 {
139                         int n = Interlocked.Exchange (ref Info->count, 0);
140                         return n != 0;
141                 }
142
143                 public unsafe int Count {
144                         get {return Info->count;}
145                         set {Interlocked.Exchange (ref Info->count, value);}
146                 }
147
148                 [Map]
149                 struct SignalInfo {
150                         public int signum, count, read_fd, write_fd, have_handler, pipecnt;
151                         public IntPtr handler;
152                 }
153
154                 #region WaitHandle overrides
155                 protected unsafe override void Dispose (bool disposing)
156                 {
157                         base.Dispose (disposing);
158                         if (signal_info == IntPtr.Zero)
159                                 return;
160                         uninstall (signal_info);
161                         signal_info = IntPtr.Zero;
162                 }
163
164                 public override bool WaitOne ()
165                 {
166                         return WaitOne (-1, false);
167                 }
168
169                 public override bool WaitOne (TimeSpan timeout, bool exitContext)
170                 {
171                         long ms = (long) timeout.TotalMilliseconds;
172                         if (ms < -1 || ms > Int32.MaxValue)
173                                 throw new ArgumentOutOfRangeException ("timeout");
174                         return WaitOne ((int) ms, exitContext);
175                 }
176
177                 public override bool WaitOne (int millisecondsTimeout, bool exitContext)
178                 {
179                         AssertValid ();
180                         if (exitContext)
181                                 throw new InvalidOperationException ("exitContext is not supported");
182                         return WaitAny (new UnixSignal[]{this}, millisecondsTimeout) == 0;
183                 }
184                 #endregion
185
186                 public static int WaitAny (UnixSignal[] signals)
187                 {
188                         return WaitAny (signals, -1);
189                 }
190
191                 public static int WaitAny (UnixSignal[] signals, TimeSpan timeout)
192                 {
193                         long ms = (long) timeout.TotalMilliseconds;
194                         if (ms < -1 || ms > Int32.MaxValue)
195                                 throw new ArgumentOutOfRangeException ("timeout");
196                         return WaitAny (signals, (int) ms);
197                 }
198
199                         
200                 public static unsafe int WaitAny (UnixSignal[] signals, int millisecondsTimeout)
201                 {
202                         if (signals == null)
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");
211                         }
212                         return WaitAny (infos, infos.Length, millisecondsTimeout, ShuttingDown);
213                 }
214         }
215 }
216