merge -r 60439:60440
[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 #if NET_2_0
35 using System.Runtime.ConstrainedExecution;
36 using System.Security.AccessControl;
37 using System.IO;
38 #endif
39
40 namespace System.Threading
41 {
42         public sealed class Mutex : WaitHandle 
43         {
44                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
45                 private static extern IntPtr  CreateMutex_internal(
46                                                          bool initiallyOwned,
47                                                          string name,
48                                                          out bool created);
49
50                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
51                 private static extern void ReleaseMutex_internal(IntPtr handle);
52
53 #if NET_2_0
54                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
55                 private static extern IntPtr OpenMutex_internal (string name, MutexRights rights, out MonoIOError error);
56                 
57                 private Mutex (IntPtr handle)
58                 {
59                         Handle = handle;
60                 }
61 #endif
62                 
63 #if NET_2_0
64                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
65 #endif
66                 public Mutex() {
67                         bool created;
68                         
69                         Handle=CreateMutex_internal(false, null, out created);
70                 }
71                 
72 #if NET_2_0
73                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
74 #endif
75                 public Mutex(bool initiallyOwned) {
76                         bool created;
77                         
78                         Handle=CreateMutex_internal(initiallyOwned, null,
79                                                     out created);
80                 }
81
82 #if NET_2_0
83                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
84 #endif
85                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
86                 public Mutex (bool initiallyOwned, string name)
87                 {
88                         bool created;
89                         Handle = CreateMutex_internal (initiallyOwned, name, out created);
90                 }
91
92 #if NET_2_0
93                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
94 #endif
95                 [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
96                 public Mutex (bool initiallyOwned, string name, out bool createdNew)
97                 {
98                         Handle = CreateMutex_internal (initiallyOwned, name, out createdNew);
99                 }
100
101 #if NET_2_0
102                 [MonoTODO ("Implement MutexSecurity")]
103                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
104                 public Mutex (bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity)
105                 {
106                         Handle = CreateMutex_internal (initiallyOwned, name, out createdNew);
107                 }
108
109                 public static Mutex OpenExisting (string name)
110                 {
111                         return(OpenExisting (name, MutexRights.Synchronize |
112                                              MutexRights.Modify));
113                 }
114                 
115                 public static Mutex OpenExisting (string name,
116                                                   MutexRights rights)
117                 {
118                         if (name == null) {
119                                 throw new ArgumentNullException ("name");
120                         }
121                         if ((name.Length == 0) ||
122                             (name.Length > 260)) {
123                                 throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
124                         }
125                         
126                         MonoIOError error;
127                         IntPtr handle = OpenMutex_internal (name, rights,
128                                                             out error);
129                         if (handle == (IntPtr)null) {
130                                 if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
131                                         throw new WaitHandleCannotBeOpenedException (Locale.GetText ("Named Mutex handle does not exist: ") + name);
132                                 } else if (error == MonoIOError.ERROR_ACCESS_DENIED) {
133                                         throw new UnauthorizedAccessException ();
134                                 } else {
135                                         throw new IOException (Locale.GetText ("Win32 IO error: ") +  error.ToString ());
136                                 }
137                         }
138                         
139                         return(new Mutex (handle));
140                 }
141 #endif
142
143 #if NET_2_0
144                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.MayFail)]
145 #endif  
146                 public void ReleaseMutex() {
147                         ReleaseMutex_internal(Handle);
148                 }
149         }
150 }