svn path=/trunk/mcs/; revision=104772
[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 //
7 // (C) 2008 Novell, Inc.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Runtime.InteropServices;
31 using System.Threading;
32
33 using Mono.Unix.Native;
34
35 namespace Mono.Unix {
36         public class UnixSignal : WaitHandle {
37                 private Signum signum;
38                 private IntPtr signal_info;
39
40                 public UnixSignal (Signum signum)
41                 {
42                         this.signum = signum;
43                         // ensure signum is a valid signal
44                         int _signum = NativeConvert.FromSignum (signum);
45                         this.signal_info = install (_signum);
46                         if (this.signal_info == IntPtr.Zero) {
47                                 throw new ArgumentException ("Unable to handle signal", "signum");
48                         }
49                 }
50
51                 public Signum Signum {
52                         get {
53                                 AssertValid ();
54                                 return signum; 
55                         }
56                 }
57
58                 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
59                                 EntryPoint="Mono_Unix_UnixSignal_install")]
60                 private static extern IntPtr install (int signum);
61
62                 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
63                                 EntryPoint="Mono_Unix_UnixSignal_uninstall")]
64                 private static extern int uninstall (IntPtr info);
65
66                 [DllImport (Stdlib.MPH, CallingConvention=CallingConvention.Cdecl,
67                                 EntryPoint="Mono_Unix_UnixSignal_WaitAny")]
68                 private static extern int WaitAny (IntPtr[] infos, int count, int timeout);
69
70                 private void AssertValid ()
71                 {
72                         if (signal_info == IntPtr.Zero)
73                                 throw new ObjectDisposedException (GetType().FullName);
74                 }
75
76                 private unsafe SignalInfo* Info {
77                         get {
78                                 AssertValid ();
79                                 return (SignalInfo*) signal_info;
80                         }
81                 }
82
83                 public bool IsSet {
84                         get {
85                                 return Count > 0;
86                         }
87                 }
88
89                 public unsafe bool Reset ()
90                 {
91                         int n = Interlocked.Exchange (ref Info->count, 0);
92                         return n != 0;
93                 }
94
95                 public unsafe int Count {
96                         get {return Info->count;}
97                         set {Interlocked.Exchange (ref Info->count, value);}
98                 }
99
100                 [Map]
101                 struct SignalInfo {
102                         public int signum, count, read_fd, write_fd, have_handler;
103                         public IntPtr handler;
104                 }
105
106                 #region WaitHandle overrides
107                 protected unsafe override void Dispose (bool disposing)
108                 {
109                         base.Dispose (disposing);
110                         if (signal_info == IntPtr.Zero)
111                                 return;
112                         uninstall (signal_info);
113                         signal_info = IntPtr.Zero;
114                 }
115
116                 public override bool WaitOne ()
117                 {
118                         return WaitOne (-1, false);
119                 }
120
121                 public override bool WaitOne (TimeSpan timeout, bool exitContext)
122                 {
123                         long ms = (long) timeout.TotalMilliseconds;
124                         if (ms < -1 || ms > Int32.MaxValue)
125                                 throw new ArgumentOutOfRangeException ("timeout");
126                         return WaitOne ((int) ms, exitContext);
127                 }
128
129                 public override bool WaitOne (int millisecondsTimeout, bool exitContext)
130                 {
131                         AssertValid ();
132                         if (exitContext)
133                                 throw new InvalidOperationException ("exitContext is not supported");
134                         return WaitAny (new UnixSignal[]{this}, millisecondsTimeout) == 0;
135                 }
136                 #endregion
137
138                 public static int WaitAny (UnixSignal[] signals)
139                 {
140                         return WaitAny (signals, -1);
141                 }
142
143                 public static int WaitAny (UnixSignal[] signals, TimeSpan timeout)
144                 {
145                         long ms = (long) timeout.TotalMilliseconds;
146                         if (ms < -1 || ms > Int32.MaxValue)
147                                 throw new ArgumentOutOfRangeException ("timeout");
148                         return WaitAny (signals, (int) ms);
149                 }
150
151                 public static unsafe int WaitAny (UnixSignal[] signals, int millisecondsTimeout)
152                 {
153                         if (signals == null)
154                                 throw new ArgumentNullException ("signals");
155                         if (millisecondsTimeout < -1)
156                                 throw new ArgumentOutOfRangeException ("millisecondsTimeout");
157                         IntPtr[] infos = new IntPtr [signals.Length];
158                         for (int i = 0; i < signals.Length; ++i) {
159                                 infos [i] = signals [i].signal_info;
160                                 if (infos [i] == IntPtr.Zero)
161                                         throw new InvalidOperationException ("Disposed UnixSignal");
162                         }
163                         return WaitAny (infos, infos.Length, millisecondsTimeout);
164                 }
165         }
166 }
167