revert 127010 until Jeremie polishes and fixes the integration
[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 #if NET_2_0
31
32 using System.Security.AccessControl;
33 using System.IO;
34 using System.Runtime.InteropServices;
35
36 namespace System.Threading
37 {
38         [ComVisible (true)]
39         public class EventWaitHandle : WaitHandle
40         {
41                 private EventWaitHandle (IntPtr handle)
42                 {
43                         Handle = handle;
44                 }
45                 
46                 public EventWaitHandle (bool initialState, EventResetMode mode)
47                 {
48                         bool created;
49                         
50                         Handle = NativeEventCalls.CreateEvent_internal ((mode == EventResetMode.ManualReset), initialState, null, out created);
51                 }
52                 
53                 public EventWaitHandle (bool initialState, EventResetMode mode,
54                                         string name)
55                 {
56                         bool created;
57                         
58                         Handle = NativeEventCalls.CreateEvent_internal ((mode == EventResetMode.ManualReset), initialState, name, out created);
59                 }
60                 
61                 public EventWaitHandle (bool initialState, EventResetMode mode,
62                                         string name, out bool createdNew)
63                 {
64                         Handle = NativeEventCalls.CreateEvent_internal ((mode == EventResetMode.ManualReset), initialState, name, out createdNew);
65                 }
66                 
67                 [MonoTODO ("Implement access control")]
68                 public EventWaitHandle (bool initialState, EventResetMode mode,
69                                         string name, out bool createdNew,
70                                         EventWaitHandleSecurity eventSecurity)
71                 {
72                         Handle = NativeEventCalls.CreateEvent_internal ((mode == EventResetMode.ManualReset), initialState, name, out createdNew);
73                 }
74                 
75                 [MonoTODO]
76                 public EventWaitHandleSecurity GetAccessControl ()
77                 {
78                         throw new NotImplementedException ();
79                 }
80                 
81                 public static EventWaitHandle OpenExisting (string name)
82                 {
83                         return(OpenExisting (name, EventWaitHandleRights.Synchronize | EventWaitHandleRights.Modify));
84                 }
85
86                 public static EventWaitHandle OpenExisting (string name, EventWaitHandleRights rights)
87                 {
88                         if (name == null) {
89                                 throw new ArgumentNullException ("name");
90                         }
91                         if ((name.Length == 0) ||
92                             (name.Length > 260)) {
93                                 throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
94                         }
95                         
96                         MonoIOError error;
97                         IntPtr handle = NativeEventCalls.OpenEvent_internal (name, rights, out error);
98                         if (handle == (IntPtr)null) {
99                                 if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
100                                         throw new WaitHandleCannotBeOpenedException (Locale.GetText ("Named Event handle does not exist: ") + name);
101                                 } else if (error == MonoIOError.ERROR_ACCESS_DENIED) {
102                                         throw new UnauthorizedAccessException ();
103                                 } else {
104                                         throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
105                                 }
106                         }
107                         
108                         return(new EventWaitHandle (handle));
109                 }
110                 
111                 public bool Reset ()
112                 {
113                         CheckDisposed ();
114                         
115                         return (NativeEventCalls.ResetEvent_internal (Handle));
116                 }
117                 
118                 public bool Set ()
119                 {
120                         CheckDisposed ();
121                         
122                         return (NativeEventCalls.SetEvent_internal (Handle));
123                 }
124
125                 [MonoTODO]
126                 public void SetAccessControl (EventWaitHandleSecurity eventSecurity)
127                 {
128                         throw new NotImplementedException ();
129                 }
130         }
131 }
132
133 #endif