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 unsafe static extern IntPtr
CreateMutex_icall (bool initiallyOwned
, char *name
,
46 int name_length
, out bool created
);
48 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
49 private unsafe static extern IntPtr
OpenMutex_icall (char *name
, int name_length
,
50 MutexRights rights
, out MonoIOError error
);
52 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
53 private static extern bool ReleaseMutex_internal(IntPtr handle
);
55 private unsafe static IntPtr
CreateMutex_internal (bool initiallyOwned
,
56 string name
, out bool created
)
58 fixed (char *fixed_name
= name
)
59 return CreateMutex_icall (initiallyOwned
, fixed_name
,
60 name
?.Length
?? 0, out created
);
63 private unsafe static IntPtr
OpenMutex_internal (string name
, MutexRights rights
, out MonoIOError error
)
65 fixed (char *fixed_name
= name
)
66 return OpenMutex_icall (fixed_name
, name
?.Length
?? 0, rights
, out error
);
69 private Mutex (IntPtr handle
)
74 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.MayFail
)]
78 Handle
=CreateMutex_internal(false, null, out created
);
81 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.MayFail
)]
82 public Mutex(bool initiallyOwned
) {
85 Handle
=CreateMutex_internal(initiallyOwned
, null,
90 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.MayFail
)]
91 [SecurityPermission (SecurityAction
.LinkDemand
, UnmanagedCode
= true)]
92 public Mutex (bool initiallyOwned
, string name
)
95 Handle
= CreateMutex_internal (initiallyOwned
, name
, out created
);
98 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.MayFail
)]
99 [SecurityPermission (SecurityAction
.LinkDemand
, UnmanagedCode
= true)]
100 public Mutex (bool initiallyOwned
, string name
, out bool createdNew
)
102 Handle
= CreateMutex_internal (initiallyOwned
, name
, out createdNew
);
105 [MonoTODO ("Use MutexSecurity in CreateMutex_internal")]
106 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.MayFail
)]
107 public Mutex (bool initiallyOwned
, string name
, out bool createdNew
, MutexSecurity mutexSecurity
)
109 Handle
= CreateMutex_internal (initiallyOwned
, name
, out createdNew
);
112 public MutexSecurity
GetAccessControl ()
114 return new MutexSecurity (SafeWaitHandle
,
115 AccessControlSections
.Owner
|
116 AccessControlSections
.Group
|
117 AccessControlSections
.Access
);
120 public static Mutex
OpenExisting (string name
)
122 return(OpenExisting (name
, MutexRights
.Synchronize
|
123 MutexRights
.Modify
));
126 public static Mutex
OpenExisting (string name
,
130 throw new ArgumentNullException ("name");
132 if ((name
.Length
== 0) ||
133 (name
.Length
> 260)) {
134 throw new ArgumentException ("name", Locale
.GetText ("Invalid length [1-260]."));
138 IntPtr handle
= OpenMutex_internal (name
, rights
,
140 if (handle
== (IntPtr
)null) {
141 if (error
== MonoIOError
.ERROR_FILE_NOT_FOUND
) {
142 throw new WaitHandleCannotBeOpenedException (Locale
.GetText ("Named Mutex handle does not exist: ") + name
);
143 } else if (error
== MonoIOError
.ERROR_ACCESS_DENIED
) {
144 throw new UnauthorizedAccessException ();
146 throw new IOException (Locale
.GetText ("Win32 IO error: ") + error
.ToString ());
150 return(new Mutex (handle
));
153 public static bool TryOpenExisting (string name
, out Mutex result
)
155 return TryOpenExisting (name
, MutexRights
.Synchronize
| MutexRights
.Modify
, out result
);
158 public static bool TryOpenExisting (string name
, MutexRights rights
, out Mutex result
)
161 throw new ArgumentNullException ("name");
163 if ((name
.Length
== 0) || (name
.Length
> 260)) {
164 throw new ArgumentException ("name", Locale
.GetText ("Invalid length [1-260]."));
168 IntPtr handle
= OpenMutex_internal (name
, rights
, out error
);
169 if (handle
== (IntPtr
)null) {
174 result
= new Mutex (handle
);
178 public Mutex (bool initiallyOwned
, string name
)
180 throw new NotSupportedException ();
183 public Mutex (bool initiallyOwned
, string name
, out bool createdNew
)
185 throw new NotSupportedException ();
188 public Mutex (bool initiallyOwned
, string name
, out bool createdNew
, MutexSecurity mutexSecurity
)
190 throw new NotSupportedException ();
193 public static Mutex
OpenExisting (string name
)
195 throw new NotSupportedException ();
198 public static Mutex
OpenExisting (string name
, MutexRights rights
)
200 throw new NotSupportedException ();
203 public static bool TryOpenExisting (string name
, out Mutex result
)
205 throw new NotSupportedException ();
208 public static bool TryOpenExisting (string name
, MutexRights rights
, out Mutex result
)
210 throw new NotSupportedException ();
214 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.MayFail
)]
215 public void ReleaseMutex() {
216 bool success
= ReleaseMutex_internal(Handle
);
218 throw new ApplicationException ("Mutex is not owned");
223 public void SetAccessControl (MutexSecurity mutexSecurity
)
225 if (null == mutexSecurity
)
226 throw new ArgumentNullException ("mutexSecurity");
228 mutexSecurity
.PersistModifications (SafeWaitHandle
);