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
{
42 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
43 private static extern IntPtr
CreateSemaphore_internal (
44 int initialCount
, int maximumCount
, string name
,
47 [MethodImplAttribute(MethodImplOptions
.InternalCall
)]
48 private static extern int ReleaseSemaphore_internal (
49 IntPtr handle
, int releaseCount
, out bool fail
);
51 [MethodImplAttribute (MethodImplOptions
.InternalCall
)]
52 private static extern IntPtr
OpenSemaphore_internal (string name
, SemaphoreRights rights
, out MonoIOError error
);
54 private Semaphore (IntPtr handle
)
59 public Semaphore (int initialCount
, int maximumCount
)
60 : this (initialCount
, maximumCount
, null)
64 public Semaphore (int initialCount
, int maximumCount
, string name
)
67 throw new ArgumentOutOfRangeException ("initialCount", "< 0");
69 throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
70 if (initialCount
> maximumCount
)
71 throw new ArgumentException ("initialCount > maximumCount");
75 Handle
= CreateSemaphore_internal (initialCount
,
80 public Semaphore (int initialCount
, int maximumCount
, string name
, out bool createdNew
)
81 : this (initialCount
, maximumCount
, name
, out createdNew
, null)
85 [MonoTODO ("Does not support access control, semaphoreSecurity is ignored")]
86 public Semaphore (int initialCount
, int maximumCount
, string name
, out bool createdNew
,
87 SemaphoreSecurity semaphoreSecurity
)
90 throw new ArgumentOutOfRangeException ("initialCount", "< 0");
92 throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
93 if (initialCount
> maximumCount
)
94 throw new ArgumentException ("initialCount > maximumCount");
96 Handle
= CreateSemaphore_internal (initialCount
,
102 public SemaphoreSecurity
GetAccessControl ()
104 throw new NotImplementedException ();
108 [ReliabilityContract (Consistency
.WillNotCorruptState
, Cer
.Success
)]
109 public int Release ()
111 return (Release (1));
114 [ReliabilityContract (Consistency
.WillNotCorruptState
, Cer
.Success
)]
115 public int Release (int releaseCount
)
117 if (releaseCount
< 1)
118 throw new ArgumentOutOfRangeException ("releaseCount");
123 ret
= ReleaseSemaphore_internal (Handle
, releaseCount
,
127 throw new SemaphoreFullException ();
134 public void SetAccessControl (SemaphoreSecurity semaphoreSecurity
)
136 if (semaphoreSecurity
== null)
137 throw new ArgumentNullException ("semaphoreSecurity");
139 throw new NotImplementedException ();
144 public static Semaphore
OpenExisting (string name
)
146 return OpenExisting (name
, SemaphoreRights
.Synchronize
| SemaphoreRights
.Modify
);
149 public static Semaphore
OpenExisting (string name
, SemaphoreRights rights
)
152 throw new ArgumentNullException ("name");
153 if ((name
.Length
==0) || (name
.Length
> 260))
154 throw new ArgumentException ("name", Locale
.GetText ("Invalid length [1-260]."));
157 IntPtr handle
= OpenSemaphore_internal (name
, rights
,
159 if (handle
== (IntPtr
)null) {
160 if (error
== MonoIOError
.ERROR_FILE_NOT_FOUND
) {
161 throw new WaitHandleCannotBeOpenedException (Locale
.GetText ("Named Semaphore handle does not exist: ") + name
);
162 } else if (error
== MonoIOError
.ERROR_ACCESS_DENIED
) {
163 throw new UnauthorizedAccessException ();
165 throw new IOException (Locale
.GetText ("Win32 IO error: ") + error
.ToString ());
169 return(new Semaphore (handle
));