2 // System.Threading.Semaphore.cs
5 // Sebastien Pouliot <sebastien@ximian.com>
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 using System.Runtime.ConstrainedExecution;
30 using System.Runtime.InteropServices;
31 using System.Security.AccessControl;
32 using System.Security.Permissions;
33 using System.Runtime.CompilerServices;
36 namespace System.Threading {
39 public sealed class Semaphore : WaitHandle {
41 [MethodImplAttribute(MethodImplOptions.InternalCall)]
42 private static extern IntPtr CreateSemaphore_internal (
43 int initialCount, int maximumCount, string name,
46 [MethodImplAttribute(MethodImplOptions.InternalCall)]
47 private static extern int ReleaseSemaphore_internal (
48 IntPtr handle, int releaseCount, out bool fail);
50 [MethodImplAttribute (MethodImplOptions.InternalCall)]
51 private static extern IntPtr OpenSemaphore_internal (string name, SemaphoreRights rights, out MonoIOError error);
53 private Semaphore (IntPtr handle)
58 public Semaphore (int initialCount, int maximumCount)
59 : this (initialCount, maximumCount, null)
63 public Semaphore (int initialCount, int maximumCount, string name)
66 throw new ArgumentOutOfRangeException ("initialCount", "< 0");
68 throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
69 if (initialCount > maximumCount)
70 throw new ArgumentException ("initialCount > maximumCount");
74 Handle = CreateSemaphore_internal (initialCount,
79 public Semaphore (int initialCount, int maximumCount, string name, out bool createdNew)
80 : this (initialCount, maximumCount, name, out createdNew, null)
84 [MonoTODO ("CreateSemaphore_internal does not support access control, semaphoreSecurity is ignored")]
85 public Semaphore (int initialCount, int maximumCount, string name, out bool createdNew,
86 SemaphoreSecurity semaphoreSecurity)
89 throw new ArgumentOutOfRangeException ("initialCount", "< 0");
91 throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
92 if (initialCount > maximumCount)
93 throw new ArgumentException ("initialCount > maximumCount");
95 Handle = CreateSemaphore_internal (initialCount,
100 public SemaphoreSecurity GetAccessControl ()
102 return new SemaphoreSecurity (SafeWaitHandle,
103 AccessControlSections.Owner |
104 AccessControlSections.Group |
105 AccessControlSections.Access);
109 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
110 public int Release ()
112 return (Release (1));
115 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
116 public int Release (int releaseCount)
118 if (releaseCount < 1)
119 throw new ArgumentOutOfRangeException ("releaseCount");
124 ret = ReleaseSemaphore_internal (Handle, releaseCount,
128 throw new SemaphoreFullException ();
134 public void SetAccessControl (SemaphoreSecurity semaphoreSecurity)
136 if (semaphoreSecurity == null)
137 throw new ArgumentNullException ("semaphoreSecurity");
139 semaphoreSecurity.PersistModifications (SafeWaitHandle);
145 public static Semaphore OpenExisting (string name)
147 return OpenExisting (name, SemaphoreRights.Synchronize | SemaphoreRights.Modify);
150 public static Semaphore OpenExisting (string name, SemaphoreRights rights)
153 throw new ArgumentNullException ("name");
154 if ((name.Length ==0) || (name.Length > 260))
155 throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
158 IntPtr handle = OpenSemaphore_internal (name, rights,
160 if (handle == (IntPtr)null) {
161 if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
162 throw new WaitHandleCannotBeOpenedException (Locale.GetText ("Named Semaphore handle does not exist: ") + name);
163 } else if (error == MonoIOError.ERROR_ACCESS_DENIED) {
164 throw new UnauthorizedAccessException ();
166 throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
170 return(new Semaphore (handle));
173 [SecurityPermissionAttribute (SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
174 public static bool TryOpenExisting (string name, out Semaphore result)
176 return TryOpenExisting (name, SemaphoreRights.Synchronize | SemaphoreRights.Modify, out result);
179 [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
180 public static bool TryOpenExisting (string name, SemaphoreRights rights, out Semaphore result)
183 throw new ArgumentNullException ("name");
184 if ((name.Length == 0) || (name.Length > 260))
185 throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
188 IntPtr handle = OpenSemaphore_internal (name, rights, out error);
190 if (handle == (IntPtr)null) {
195 result = new Semaphore (handle);
199 public static Semaphore OpenExisting (string name)
201 throw new NotSupportedException ();
204 public static Semaphore OpenExisting (string name, SemaphoreRights rights)
206 throw new NotSupportedException ();
209 public static bool TryOpenExisting (string name, out Semaphore result)
211 throw new NotSupportedException ();
214 public static bool TryOpenExisting (string name, SemaphoreRights rights, out Semaphore result)
216 throw new NotSupportedException ();