fix bootstrap
[mcs.git] / class / System / System.Threading / Semaphore.cs
blobec5e2abbe45387d4a8b76aaf69ebf4a6c30792a2
1 //
2 // System.Threading.Semaphore.cs
3 //
4 // Author:
5 // Sebastien Pouliot <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
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:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
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 #if NET_2_0
31 using System.Runtime.ConstrainedExecution;
32 using System.Runtime.InteropServices;
33 using System.Security.AccessControl;
34 using System.Runtime.CompilerServices;
35 using System.IO;
37 namespace System.Threading {
39 [ComVisible (false)]
40 public sealed class Semaphore : WaitHandle {
42 [MethodImplAttribute(MethodImplOptions.InternalCall)]
43 private static extern IntPtr CreateSemaphore_internal (
44 int initialCount, int maximumCount, string name,
45 out bool createdNew);
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)
56 Handle = handle;
59 public Semaphore (int initialCount, int maximumCount)
60 : this (initialCount, maximumCount, null)
64 public Semaphore (int initialCount, int maximumCount, string name)
66 if (initialCount < 0)
67 throw new ArgumentOutOfRangeException ("initialCount", "< 0");
68 if (maximumCount < 1)
69 throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
70 if (initialCount > maximumCount)
71 throw new ArgumentException ("initialCount > maximumCount");
73 bool created;
75 Handle = CreateSemaphore_internal (initialCount,
76 maximumCount, name,
77 out created);
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)
89 if (initialCount < 0)
90 throw new ArgumentOutOfRangeException ("initialCount", "< 0");
91 if (maximumCount < 1)
92 throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
93 if (initialCount > maximumCount)
94 throw new ArgumentException ("initialCount > maximumCount");
96 Handle = CreateSemaphore_internal (initialCount,
97 maximumCount, name,
98 out createdNew);
101 [MonoTODO]
102 public SemaphoreSecurity GetAccessControl ()
104 throw new NotImplementedException ();
107 [PrePrepareMethod]
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");
120 int ret;
121 bool fail;
123 ret = ReleaseSemaphore_internal (Handle, releaseCount,
124 out fail);
126 if (fail) {
127 throw new SemaphoreFullException ();
130 return (ret);
133 [MonoTODO]
134 public void SetAccessControl (SemaphoreSecurity semaphoreSecurity)
136 if (semaphoreSecurity == null)
137 throw new ArgumentNullException ("semaphoreSecurity");
139 throw new NotImplementedException ();
142 // static methods
144 public static Semaphore OpenExisting (string name)
146 return OpenExisting (name, SemaphoreRights.Synchronize | SemaphoreRights.Modify);
149 public static Semaphore OpenExisting (string name, SemaphoreRights rights)
151 if (name == null)
152 throw new ArgumentNullException ("name");
153 if ((name.Length ==0) || (name.Length > 260))
154 throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
156 MonoIOError error;
157 IntPtr handle = OpenSemaphore_internal (name, rights,
158 out error);
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 ();
164 } else {
165 throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
169 return(new Semaphore (handle));
174 #endif