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