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.
31 using System.Runtime.ConstrainedExecution;
32 using System.Runtime.InteropServices;
33 using System.Security.AccessControl;
34 using System.Runtime.CompilerServices;
37 namespace System.Threading {
40 public sealed class Semaphore : WaitHandle {
43 [MethodImplAttribute(MethodImplOptions.InternalCall)]
44 private static extern IntPtr CreateSemaphore_internal (
45 int initialCount, int maximumCount, string name,
48 [MethodImplAttribute(MethodImplOptions.InternalCall)]
49 private static extern int ReleaseSemaphore_internal (
50 IntPtr handle, int releaseCount, out bool fail);
52 [MethodImplAttribute (MethodImplOptions.InternalCall)]
53 private static extern IntPtr OpenSemaphore_internal (string name, SemaphoreRights rights, out MonoIOError error);
56 private Semaphore (IntPtr handle)
61 public Semaphore (int initialCount, int maximumCount)
62 : this (initialCount, maximumCount, null)
66 public Semaphore (int initialCount, int maximumCount, string name)
69 throw new ArgumentOutOfRangeException ("initialCount", "< 0");
71 throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
72 if (initialCount > maximumCount)
73 throw new ArgumentException ("initialCount > maximumCount");
77 Handle = CreateSemaphore_internal (initialCount,
82 public Semaphore (int initialCount, int maximumCount, string name, out bool createdNew)
83 : this (initialCount, maximumCount, name, out createdNew, null)
87 [MonoTODO ("Implement access control")]
88 public Semaphore (int initialCount, int maximumCount, string name, out bool createdNew,
89 SemaphoreSecurity semaphoreSecurity)
92 throw new ArgumentOutOfRangeException ("initialCount", "< 0");
94 throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
95 if (initialCount > maximumCount)
96 throw new ArgumentException ("initialCount > maximumCount");
98 Handle = CreateSemaphore_internal (initialCount,
104 public SemaphoreSecurity GetAccessControl ()
106 throw new NotImplementedException ();
110 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
111 public int Release ()
113 return (Release (1));
116 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
117 public int Release (int releaseCount)
120 throw new NotImplementedException ();
122 if (releaseCount < 1)
123 throw new ArgumentOutOfRangeException ("releaseCount");
128 ret = ReleaseSemaphore_internal (Handle, releaseCount,
132 throw new SemaphoreFullException ();
140 public void SetAccessControl (SemaphoreSecurity semaphoreSecurity)
142 if (semaphoreSecurity == null)
143 throw new ArgumentNullException ("semaphoreSecurity");
145 throw new NotImplementedException ();
150 public static Semaphore OpenExisting (string name)
152 return OpenExisting (name, SemaphoreRights.Synchronize | SemaphoreRights.Modify);
155 public static Semaphore OpenExisting (string name, SemaphoreRights rights)
158 throw new ArgumentNullException ("name");
159 if ((name.Length ==0) || (name.Length > 260))
160 throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
164 IntPtr handle = OpenSemaphore_internal (name, rights,
166 if (handle == (IntPtr)null) {
167 if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
168 throw new WaitHandleCannotBeOpenedException (Locale.GetText ("Named Semaphore handle does not exist: ") + name);
169 } else if (error == MonoIOError.ERROR_ACCESS_DENIED) {
170 throw new UnauthorizedAccessException ();
172 throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
176 return(new Semaphore (handle));