Merge pull request #2247 from ivmai/match-ext-libgc-api
[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                 static UnixSignal ()
43                 {
44                         Stdlib.VersionCheck ();
45                 }
46
47                 public UnixSignal (Signum signum)
48                 {
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");
53                         }
54                 }
55
56                 public UnixSignal (Mono.Unix.Native.RealTimeSignum rtsig)
57                 {
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");
65                         }
66                 }
67
68                 public Signum Signum {
69                         get {
70                                 if (IsRealTimeSignal)
71                                         throw new InvalidOperationException ("This signal is a RealTimeSignum");
72                                 return NativeConvert.ToSignum (signum); 
73                         }
74                 }
75
76                 public RealTimeSignum RealTimeSignum {
77                         get {
78                                 if (!IsRealTimeSignal)
79                                         throw new InvalidOperationException ("This signal is not a RealTimeSignum");
80                                 return NativeConvert.ToRealTimeSignum (signum-GetSIGRTMIN ());
81                         }
82                 }
83
84                 public bool IsRealTimeSignal {
85                         get {
86                                 AssertValid ();
87                                 int sigrtmin = GetSIGRTMIN ();
88                                 if (sigrtmin == -1)
89                                         return false;
90                                 return signum >= sigrtmin;
91                         }
92                 }
93
94                 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
95                                 EntryPoint="Mono_Unix_UnixSignal_install", SetLastError=true)]
96                 private static extern IntPtr install (int signum);
97
98                 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
99                                 EntryPoint="Mono_Unix_UnixSignal_uninstall")]
100                 private static extern int uninstall (IntPtr info);
101
102                 [UnmanagedFunctionPointer (CallingConvention.Cdecl)]
103                 delegate int Mono_Posix_RuntimeIsShuttingDown ();
104                 static Mono_Posix_RuntimeIsShuttingDown ShuttingDown = RuntimeShuttingDownCallback;
105                 
106                 static int RuntimeShuttingDownCallback ()
107                 {
108                         return Environment.HasShutdownStarted ? 1 : 0;
109                 }
110
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);
114
115                 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
116                                 EntryPoint="Mono_Posix_SIGRTMIN")]
117                 internal static extern int GetSIGRTMIN ();
118
119                 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
120                                 EntryPoint="Mono_Posix_SIGRTMAX")]
121                 internal static extern int GetSIGRTMAX ();
122
123                 private void AssertValid ()
124                 {
125                         if (signal_info == IntPtr.Zero)
126                                 throw new ObjectDisposedException (GetType().FullName);
127                 }
128
129                 private unsafe SignalInfo* Info {
130                         get {
131                                 AssertValid ();
132                                 return (SignalInfo*) signal_info;
133                         }
134                 }
135
136                 public bool IsSet {
137                         get {
138                                 return Count > 0;
139                         }
140                 }
141
142                 public unsafe bool Reset ()
143                 {
144                         int n = Interlocked.Exchange (ref Info->count, 0);
145                         return n != 0;
146                 }
147
148                 public unsafe int Count {
149                         get {return Info->count;}
150                         set {Interlocked.Exchange (ref Info->count, value);}
151                 }
152
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
155                 [Map]
156                 struct SignalInfo {
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
159                 }
160
161                 #region WaitHandle overrides
162                 protected unsafe override void Dispose (bool disposing)
163                 {
164                         base.Dispose (disposing);
165                         if (signal_info == IntPtr.Zero)
166                                 return;
167                         uninstall (signal_info);
168                         signal_info = IntPtr.Zero;
169                 }
170
171                 public override bool WaitOne ()
172                 {
173                         return WaitOne (-1, false);
174                 }
175
176                 public override bool WaitOne (TimeSpan timeout, bool exitContext)
177                 {
178                         long ms = (long) timeout.TotalMilliseconds;
179                         if (ms < -1 || ms > Int32.MaxValue)
180                                 throw new ArgumentOutOfRangeException ("timeout");
181                         return WaitOne ((int) ms, exitContext);
182                 }
183
184                 public override bool WaitOne (int millisecondsTimeout, bool exitContext)
185                 {
186                         AssertValid ();
187                         if (exitContext)
188                                 throw new InvalidOperationException ("exitContext is not supported");
189                         return WaitAny (new UnixSignal[]{this}, millisecondsTimeout) == 0;
190                 }
191                 #endregion
192
193                 public static int WaitAny (UnixSignal[] signals)
194                 {
195                         return WaitAny (signals, -1);
196                 }
197
198                 public static int WaitAny (UnixSignal[] signals, TimeSpan timeout)
199                 {
200                         long ms = (long) timeout.TotalMilliseconds;
201                         if (ms < -1 || ms > Int32.MaxValue)
202                                 throw new ArgumentOutOfRangeException ("timeout");
203                         return WaitAny (signals, (int) ms);
204                 }
205
206                         
207                 public static unsafe int WaitAny (UnixSignal[] signals, int millisecondsTimeout)
208                 {
209                         if (signals == null)
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");
218                         }
219                         return WaitAny (infos, infos.Length, millisecondsTimeout, ShuttingDown);
220                 }
221         }
222 }
223