2 // System.Threading.Mutex.cs
5 // Dick Porter (dick@ximian.com)
6 // Veronica De Santis (veron78@interfree.it)
8 // (C) Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Runtime.CompilerServices;
32 using System.Security.Permissions;
34 using System.Runtime.ConstrainedExecution;
36 using System.Runtime.InteropServices;
37 using System.Security.AccessControl;
39 namespace System.Threading
42 public sealed class Mutex : WaitHandle
44 [MethodImplAttribute(MethodImplOptions.InternalCall)]
45 private static extern bool ReleaseMutex_internal(IntPtr handle);
47 [MethodImplAttribute(MethodImplOptions.InternalCall)]
48 private static extern IntPtr CreateMutex_internal(
53 [MethodImplAttribute (MethodImplOptions.InternalCall)]
54 private static extern IntPtr OpenMutex_internal (string name, MutexRights rights, out MonoIOError error);
56 private Mutex (IntPtr handle)
61 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
65 Handle=CreateMutex_internal(false, null, out created);
68 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
69 public Mutex(bool initiallyOwned) {
72 Handle=CreateMutex_internal(initiallyOwned, null,
77 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
78 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
79 public Mutex (bool initiallyOwned, string name)
82 Handle = CreateMutex_internal (initiallyOwned, name, out created);
85 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
86 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
87 public Mutex (bool initiallyOwned, string name, out bool createdNew)
89 Handle = CreateMutex_internal (initiallyOwned, name, out createdNew);
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)
96 Handle = CreateMutex_internal (initiallyOwned, name, out createdNew);
99 public MutexSecurity GetAccessControl ()
101 return new MutexSecurity (SafeWaitHandle,
102 AccessControlSections.Owner |
103 AccessControlSections.Group |
104 AccessControlSections.Access);
107 public static Mutex OpenExisting (string name)
109 return(OpenExisting (name, MutexRights.Synchronize |
110 MutexRights.Modify));
113 public static Mutex OpenExisting (string name,
117 throw new ArgumentNullException ("name");
119 if ((name.Length == 0) ||
120 (name.Length > 260)) {
121 throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
125 IntPtr handle = OpenMutex_internal (name, rights,
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 ();
133 throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
137 return(new Mutex (handle));
140 public static bool TryOpenExisting (string name, out Mutex result)
142 return TryOpenExisting (name, MutexRights.Synchronize | MutexRights.Modify, out result);
145 public static bool TryOpenExisting (string name, MutexRights rights, out Mutex result)
148 throw new ArgumentNullException ("name");
150 if ((name.Length == 0) || (name.Length > 260)) {
151 throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
155 IntPtr handle = OpenMutex_internal (name, rights, out error);
156 if (handle == (IntPtr)null) {
161 result = new Mutex (handle);
165 public Mutex (bool initiallyOwned, string name)
167 throw new NotSupportedException ();
170 public Mutex (bool initiallyOwned, string name, out bool createdNew)
172 throw new NotSupportedException ();
175 public Mutex (bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity)
177 throw new NotSupportedException ();
180 public static Mutex OpenExisting (string name)
182 throw new NotSupportedException ();
185 public static Mutex OpenExisting (string name, MutexRights rights)
187 throw new NotSupportedException ();
190 public static bool TryOpenExisting (string name, out Mutex result)
192 throw new NotSupportedException ();
195 public static bool TryOpenExisting (string name, MutexRights rights, out Mutex result)
197 throw new NotSupportedException ();
201 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
202 public void ReleaseMutex() {
203 bool success = ReleaseMutex_internal(Handle);
205 throw new ApplicationException ("Mutex is not owned");
210 public void SetAccessControl (MutexSecurity mutexSecurity)
212 if (null == mutexSecurity)
213 throw new ArgumentNullException ("mutexSecurity");
215 mutexSecurity.PersistModifications (SafeWaitHandle);