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