Optimize common path
[mono.git] / mcs / class / corlib / System.Threading / Mutex.cs
1 //
2 // System.Threading.Mutex.cs
3 //
4 // Author:
5 //   Dick Porter (dick@ximian.com)
6 //   Veronica De Santis (veron78@interfree.it)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Runtime.CompilerServices;
32 using System.Security.Permissions;
33
34 using System.Runtime.ConstrainedExecution;
35 using System.IO;
36 using System.Runtime.InteropServices;
37 using System.Security.AccessControl;
38
39 namespace System.Threading
40 {
41         [ComVisible (true)]
42         public sealed class Mutex : WaitHandle 
43         {
44                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
45                 private static extern bool ReleaseMutex_internal(IntPtr handle);
46                 
47                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
48                 private static extern IntPtr  CreateMutex_internal(
49                                                          bool initiallyOwned,
50                                                          string name,
51                                                          out bool created);
52
53                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
54                 private static extern IntPtr OpenMutex_internal (string name, MutexRights rights, out MonoIOError error);
55
56                 private Mutex (IntPtr handle)
57                 {
58                         Handle = handle;
59                 }
60
61                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
62                 public Mutex() {
63                         bool created;
64                         
65                         Handle=CreateMutex_internal(false, null, out created);
66                 }
67                 
68                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
69                 public Mutex(bool initiallyOwned) {
70                         bool created;
71                         
72                         Handle=CreateMutex_internal(initiallyOwned, null,
73                                                     out created);
74                 }
75
76 #if !MOBILE
77                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
78                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
79                 public Mutex (bool initiallyOwned, string name)
80                 {
81                         bool created;
82                         Handle = CreateMutex_internal (initiallyOwned, name, out created);
83                 }
84
85                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
86                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
87                 public Mutex (bool initiallyOwned, string name, out bool createdNew)
88                 {
89                         Handle = CreateMutex_internal (initiallyOwned, name, out createdNew);
90                 }
91
92                 [MonoTODO ("Use MutexSecurity in CreateMutex_internal")]
93                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
94                 public Mutex (bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity)
95                 {
96                         Handle = CreateMutex_internal (initiallyOwned, name, out createdNew);
97                 }
98
99                 public MutexSecurity GetAccessControl ()
100                 {
101                         return new MutexSecurity (SafeWaitHandle,
102                                                   AccessControlSections.Owner |
103                                                   AccessControlSections.Group |
104                                                   AccessControlSections.Access);
105                 }
106
107                 public static Mutex OpenExisting (string name)
108                 {
109                         return(OpenExisting (name, MutexRights.Synchronize |
110                                              MutexRights.Modify));
111                 }
112                 
113                 public static Mutex OpenExisting (string name,
114                                                   MutexRights rights)
115                 {
116                         if (name == null) {
117                                 throw new ArgumentNullException ("name");
118                         }
119                         if ((name.Length == 0) ||
120                             (name.Length > 260)) {
121                                 throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
122                         }
123                         
124                         MonoIOError error;
125                         IntPtr handle = OpenMutex_internal (name, rights,
126                                                             out error);
127                         if (handle == (IntPtr)null) {
128                                 if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
129                                         throw new WaitHandleCannotBeOpenedException (Locale.GetText ("Named Mutex handle does not exist: ") + name);
130                                 } else if (error == MonoIOError.ERROR_ACCESS_DENIED) {
131                                         throw new UnauthorizedAccessException ();
132                                 } else {
133                                         throw new IOException (Locale.GetText ("Win32 IO error: ") +  error.ToString ());
134                                 }
135                         }
136                         
137                         return(new Mutex (handle));
138                 }
139
140                 public static bool TryOpenExisting (string name, out Mutex result)
141                 {
142                         return TryOpenExisting (name, MutexRights.Synchronize | MutexRights.Modify, out result);
143                 }
144
145                 public static bool TryOpenExisting (string name, MutexRights rights, out Mutex result)
146                 {
147                         if (name == null) {
148                                 throw new ArgumentNullException ("name");
149                         }
150                         if ((name.Length == 0) || (name.Length > 260)) {
151                                 throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
152                         }
153                         
154                         MonoIOError error;
155                         IntPtr handle = OpenMutex_internal (name, rights, out error);
156                         if (handle == (IntPtr)null) {
157                                 result = null;
158                                 return false;
159                         }
160
161                         result = new Mutex (handle);
162                         return true;
163                 }
164 #else
165                 public Mutex (bool initiallyOwned, string name)
166                 {
167                         throw new NotSupportedException ();
168                 }
169                 
170                 public Mutex (bool initiallyOwned, string name, out bool createdNew)
171                 {
172                         throw new NotSupportedException ();
173                 }
174                 
175                 public Mutex (bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity)
176                 {
177                         throw new NotSupportedException ();
178                 }
179
180                 public static Mutex OpenExisting (string name)
181                 {
182                         throw new NotSupportedException ();
183                 }
184
185                 public static Mutex OpenExisting (string name, MutexRights rights)
186                 {
187                         throw new NotSupportedException ();
188                 }
189
190                 public static bool TryOpenExisting (string name, out Mutex result)
191                 {
192                         throw new NotSupportedException ();
193                 }
194
195                 public static bool TryOpenExisting (string name, MutexRights rights, out Mutex result)
196                 {
197                         throw new NotSupportedException ();
198                 }
199 #endif
200
201                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
202                 public void ReleaseMutex() {
203                         bool success = ReleaseMutex_internal(Handle);
204                         if (!success) {
205                                 throw new ApplicationException ("Mutex is not owned");
206                         }
207                 }
208
209 #if !NET_2_1
210                 public void SetAccessControl (MutexSecurity mutexSecurity)
211                 {
212                         if (null == mutexSecurity)
213                                 throw new ArgumentNullException ("mutexSecurity");
214                                 
215                         mutexSecurity.PersistModifications (SafeWaitHandle);
216                 }
217 #endif
218         }
219 }