Merge branch 'master' of github.com:mono/mono
[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
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);
103
104                 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
105                                 EntryPoint="Mono_Posix_SIGRTMIN")]
106                 internal static extern int GetSIGRTMIN ();
107
108                 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
109                                 EntryPoint="Mono_Posix_SIGRTMAX")]
110                 internal static extern int GetSIGRTMAX ();
111
112                 private void AssertValid ()
113                 {
114                         if (signal_info == IntPtr.Zero)
115                                 throw new ObjectDisposedException (GetType().FullName);
116                 }
117
118                 private unsafe SignalInfo* Info {
119                         get {
120                                 AssertValid ();
121                                 return (SignalInfo*) signal_info;
122                         }
123                 }
124
125                 public bool IsSet {
126                         get {
127                                 return Count > 0;
128                         }
129                 }
130
131                 public unsafe bool Reset ()
132                 {
133                         int n = Interlocked.Exchange (ref Info->count, 0);
134                         return n != 0;
135                 }
136
137                 public unsafe int Count {
138                         get {return Info->count;}
139                         set {Interlocked.Exchange (ref Info->count, value);}
140                 }
141
142                 [Map]
143                 struct SignalInfo {
144                         public int signum, count, read_fd, write_fd, have_handler, pipecnt;
145                         public IntPtr handler;
146                 }
147
148                 #region WaitHandle overrides
149                 protected unsafe override void Dispose (bool disposing)
150                 {
151                         base.Dispose (disposing);
152                         if (signal_info == IntPtr.Zero)
153                                 return;
154                         uninstall (signal_info);
155                         signal_info = IntPtr.Zero;
156                 }
157
158                 public override bool WaitOne ()
159                 {
160                         return WaitOne (-1, false);
161                 }
162
163                 public override bool WaitOne (TimeSpan timeout, bool exitContext)
164                 {
165                         long ms = (long) timeout.TotalMilliseconds;
166                         if (ms < -1 || ms > Int32.MaxValue)
167                                 throw new ArgumentOutOfRangeException ("timeout");
168                         return WaitOne ((int) ms, exitContext);
169                 }
170
171                 public override bool WaitOne (int millisecondsTimeout, bool exitContext)
172                 {
173                         AssertValid ();
174                         if (exitContext)
175                                 throw new InvalidOperationException ("exitContext is not supported");
176                         return WaitAny (new UnixSignal[]{this}, millisecondsTimeout) == 0;
177                 }
178                 #endregion
179
180                 public static int WaitAny (UnixSignal[] signals)
181                 {
182                         return WaitAny (signals, -1);
183                 }
184
185                 public static int WaitAny (UnixSignal[] signals, TimeSpan timeout)
186                 {
187                         long ms = (long) timeout.TotalMilliseconds;
188                         if (ms < -1 || ms > Int32.MaxValue)
189                                 throw new ArgumentOutOfRangeException ("timeout");
190                         return WaitAny (signals, (int) ms);
191                 }
192
193                 public static unsafe int WaitAny (UnixSignal[] signals, int millisecondsTimeout)
194                 {
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");
204                         }
205                         return WaitAny (infos, infos.Length, millisecondsTimeout, () => Environment.HasShutdownStarted ? 1 : 0);
206                 }
207         }
208 }
209