* Mono.Posix.dll.sources: Add Mono.Unix/UnixSignal.cs.
[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 = Info->count;
92                         Info->count = 0;
93                         return n != 0;
94                 }
95
96                 public unsafe int Count {
97                         get {return Info->count;}
98                         set {Info->count = value;}
99                 }
100
101                 [Map]
102                 struct SignalInfo {
103                         public int signum, count, read_fd, write_fd, have_handler;
104                         public IntPtr handler;
105                 }
106
107                 #region WaitHandle overrides
108                 protected unsafe override void Dispose (bool disposing)
109                 {
110                         base.Dispose (disposing);
111                         if (signal_info == IntPtr.Zero)
112                                 return;
113                         uninstall (signal_info);
114                         signal_info = IntPtr.Zero;
115                 }
116
117                 public override bool WaitOne ()
118                 {
119                         return WaitOne (-1, false);
120                 }
121
122                 public override bool WaitOne (TimeSpan timeout, bool exitContext)
123                 {
124                         long ms = (long) timeout.TotalMilliseconds;
125                         if (ms < -1 || ms > Int32.MaxValue)
126                                 throw new ArgumentOutOfRangeException ("timeout");
127                         return WaitOne ((int) ms, exitContext);
128                 }
129
130                 public override bool WaitOne (int millisecondsTimeout, bool exitContext)
131                 {
132                         AssertValid ();
133                         if (exitContext)
134                                 throw new InvalidOperationException ("exitContext is not supported");
135                         return WaitAny (new UnixSignal[]{this}, millisecondsTimeout);
136                 }
137                 #endregion
138
139                 public static bool WaitAny (UnixSignal[] signals)
140                 {
141                         return WaitAny (signals, -1);
142                 }
143
144                 public static bool WaitAny (UnixSignal[] signals, TimeSpan timeout)
145                 {
146                         long ms = (long) timeout.TotalMilliseconds;
147                         if (ms < -1 || ms > Int32.MaxValue)
148                                 throw new ArgumentOutOfRangeException ("timeout");
149                         return WaitAny (signals, (int) ms);
150                 }
151
152                 public static unsafe bool WaitAny (UnixSignal[] signals, int millisecondsTimeout)
153                 {
154                         IntPtr[] infos = new IntPtr [signals.Length];
155                         for (int i = 0; i < signals.Length; ++i) {
156                                 infos [i] = signals [i].signal_info;
157                                 if (infos [i] == IntPtr.Zero)
158                                         throw new InvalidOperationException ("Disposed UnixSignal");
159                         }
160                         int r = WaitAny (infos, infos.Length, millisecondsTimeout);
161                         if (r > 0)
162                                 return true;
163                         return false;
164                 }
165         }
166 }
167