Merge pull request #2651 from akoeplinger/dmcs-warning
[mono.git] / mcs / class / corlib / System.Threading / EventWaitHandle.cs
1 //
2 // System.Threading.EventWaitHandle.cs
3 //
4 // Author:
5 //      Dick Porter (dick@ximian.com)
6 //
7 // (C) Ximian, Inc.     (http://www.ximian.com)
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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.IO;
31 using System.Runtime.InteropServices;
32 using System.Security.AccessControl;
33
34 namespace System.Threading
35 {
36         [ComVisible (true)]
37         public class EventWaitHandle : WaitHandle
38         {
39                 private EventWaitHandle (IntPtr handle)
40                 {
41                         Handle = handle;
42                 }
43
44                 static bool IsManualReset (EventResetMode mode)
45                 {
46                         if ((mode < EventResetMode.AutoReset) || (mode > EventResetMode.ManualReset))
47                                 throw new ArgumentException ("mode");
48                         return (mode == EventResetMode.ManualReset);
49                 }
50                 
51                 public EventWaitHandle (bool initialState, EventResetMode mode)
52                 {
53                         bool created;
54                         bool manual = IsManualReset (mode);
55                         Handle = NativeEventCalls.CreateEvent_internal (manual, initialState, null, out created);
56                 }
57                 
58 #if !MOBILE
59                 
60                 public EventWaitHandle (bool initialState, EventResetMode mode,
61                                         string name)
62                 {
63                         bool created;
64                         bool manual = IsManualReset (mode);
65                         Handle = NativeEventCalls.CreateEvent_internal (manual, initialState, name, out created);
66                 }
67                 
68                 public EventWaitHandle (bool initialState, EventResetMode mode,
69                                         string name, out bool createdNew)
70                 {
71                         bool manual = IsManualReset (mode);
72                         Handle = NativeEventCalls.CreateEvent_internal (manual, initialState, name, out createdNew);
73                 }
74
75
76                 [MonoTODO ("Use access control in CreateEvent_internal")]
77                 public EventWaitHandle (bool initialState, EventResetMode mode,
78                                         string name, out bool createdNew,
79                                         EventWaitHandleSecurity eventSecurity)
80                 {
81                         bool manual = IsManualReset (mode);
82                         Handle = NativeEventCalls.CreateEvent_internal (manual, initialState, name, out createdNew);
83                 }
84                 
85                 public EventWaitHandleSecurity GetAccessControl ()
86                 {
87                         return new EventWaitHandleSecurity (SafeWaitHandle,
88                                                             AccessControlSections.Owner |
89                                                             AccessControlSections.Group |
90                                                             AccessControlSections.Access);
91
92                 }
93
94                 public static EventWaitHandle OpenExisting (string name)
95                 {
96                         return(OpenExisting (name, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify));
97                 }
98
99                 public static EventWaitHandle OpenExisting (string name, EventWaitHandleRights rights)
100                 {
101                         if (name == null) {
102                                 throw new ArgumentNullException ("name");
103                         }
104                         if ((name.Length == 0) ||
105                             (name.Length > 260)) {
106                                 throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
107                         }
108                         
109                         MonoIOError error;
110                         IntPtr handle = NativeEventCalls.OpenEvent_internal (name, rights, out error);
111                         if (handle == (IntPtr)null) {
112                                 if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
113                                         throw new WaitHandleCannotBeOpenedException (Locale.GetText ("Named Event handle does not exist: ") + name);
114                                 } else if (error == MonoIOError.ERROR_ACCESS_DENIED) {
115                                         throw new UnauthorizedAccessException ();
116                                 } else {
117                                         throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
118                                 }
119                         }
120                         
121                         return(new EventWaitHandle (handle));
122                 }
123
124                 public static bool TryOpenExisting (string name, out EventWaitHandle result)
125                 {
126                         return TryOpenExisting (
127                                 name, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify, out result);
128                 }
129
130                 public static bool TryOpenExisting (string name, EventWaitHandleRights rights,
131                                                     out EventWaitHandle result)
132                 {
133                         if (name == null) {
134                                 throw new ArgumentNullException ("name");
135                         }
136                         if ((name.Length == 0) || (name.Length > 260)) {
137                                 throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
138                         }
139                         
140                         MonoIOError error;
141                         IntPtr handle = NativeEventCalls.OpenEvent_internal (name, rights, out error);
142                         if (handle == (IntPtr)null) {
143                                 result = null;
144                                 return false;
145                         }
146
147                         result = new EventWaitHandle (handle);
148                         return true;
149                 }
150 #else
151                 public EventWaitHandle (bool initialState, EventResetMode mode, string name)
152                 {
153                         throw new NotSupportedException ();
154                 }
155                 
156                 public EventWaitHandle (bool initialState, EventResetMode mode,
157                                         string name, out bool createdNew)
158                 {
159                         throw new NotSupportedException ();
160                 }
161                 
162                 
163                 public EventWaitHandle (bool initialState, EventResetMode mode,
164                                         string name, out bool createdNew,
165                                         EventWaitHandleSecurity eventSecurity)
166                 {
167                         throw new NotSupportedException ();
168                 }
169
170                 public static EventWaitHandle OpenExisting (string name)
171                 {
172                         throw new NotSupportedException (); 
173                 }
174
175                 public static EventWaitHandle OpenExisting (string name, EventWaitHandleRights rights)
176                 {
177                         throw new NotSupportedException (); 
178                 }
179
180                 public static bool TryOpenExisting (string name, out EventWaitHandle result)
181                 {
182                         throw new NotSupportedException (); 
183                 }
184
185                 public static bool TryOpenExisting (string name, EventWaitHandleRights rights,
186                                                     out EventWaitHandle result)
187                 {
188                         throw new NotSupportedException (); 
189                 }
190 #endif
191
192                 public bool Reset ()
193                 {
194                         /* This needs locking since another thread could dispose the handle */
195                         lock (this) {
196                                 CheckDisposed ();
197                         
198                                 return (NativeEventCalls.ResetEvent_internal (Handle));
199                         }
200                 }
201                 
202                 public bool Set ()
203                 {
204                         lock (this) {
205                                 CheckDisposed ();
206                         
207                                 return (NativeEventCalls.SetEvent_internal (Handle));
208                         }
209                 }
210 #if !NET_2_1
211                 public void SetAccessControl (EventWaitHandleSecurity eventSecurity)
212                 {
213                         if (null == eventSecurity)
214                                 throw new ArgumentNullException ("eventSecurity");
215                                 
216                         eventSecurity.PersistModifications (SafeWaitHandle);
217
218                 }
219 #endif
220         }
221 }