Halve the stack depth for both the new single step tests
[mono.git] / mcs / class / System / System.Threading / Semaphore.cs
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.
27 //
28
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;
34 using System.IO;
35
36 namespace System.Threading {
37
38         [ComVisible (false)]
39         public sealed class Semaphore : WaitHandle {
40
41                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
42                 private static extern IntPtr CreateSemaphore_internal (
43                         int initialCount, int maximumCount, string name,
44                         out bool createdNew);
45
46                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
47                 private static extern int ReleaseSemaphore_internal (
48                         IntPtr handle, int releaseCount, out bool fail);
49
50                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
51                 private static extern IntPtr OpenSemaphore_internal (string name, SemaphoreRights rights, out MonoIOError error);
52                 
53                 private Semaphore (IntPtr handle)
54                 {
55                         Handle = handle;
56                 }
57                 
58                 public Semaphore (int initialCount, int maximumCount)
59                         : this (initialCount, maximumCount, null)
60                 {
61                 }
62
63                 public Semaphore (int initialCount, int maximumCount, string name)
64                 {
65                         if (initialCount < 0)
66                                 throw new ArgumentOutOfRangeException ("initialCount", "< 0");
67                         if (maximumCount < 1)
68                                 throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
69                         if (initialCount > maximumCount)
70                                 throw new ArgumentException ("initialCount > maximumCount");
71
72                         bool created;
73                         
74                         Handle = CreateSemaphore_internal (initialCount,
75                                                            maximumCount, name,
76                                                            out created);
77                 }
78
79                 public Semaphore (int initialCount, int maximumCount, string name, out bool createdNew)
80                         : this (initialCount, maximumCount, name, out createdNew, null)
81                 {
82                 }
83
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)
87                 {
88                         if (initialCount < 0)
89                                 throw new ArgumentOutOfRangeException ("initialCount", "< 0");
90                         if (maximumCount < 1)
91                                 throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
92                         if (initialCount > maximumCount)
93                                 throw new ArgumentException ("initialCount > maximumCount");
94
95                         Handle = CreateSemaphore_internal (initialCount,
96                                                            maximumCount, name,
97                                                            out createdNew);
98                 }
99
100                 public SemaphoreSecurity GetAccessControl ()
101                 {
102                         return new SemaphoreSecurity (SafeWaitHandle,
103                                                       AccessControlSections.Owner |
104                                                       AccessControlSections.Group |
105                                                       AccessControlSections.Access);
106                 }
107
108                 [PrePrepareMethod]
109                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
110                 public int Release ()
111                 {
112                         return (Release (1));
113                 }
114
115                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
116                 public int Release (int releaseCount)
117                 {
118                         if (releaseCount < 1)
119                                 throw new ArgumentOutOfRangeException ("releaseCount");
120
121                         int ret;
122                         bool fail;
123                         
124                         ret = ReleaseSemaphore_internal (Handle, releaseCount,
125                                                          out fail);
126
127                         if (fail) {
128                                 throw new SemaphoreFullException ();
129                         }
130
131                         return (ret);
132                 }
133
134                 public void SetAccessControl (SemaphoreSecurity semaphoreSecurity)
135                 {
136                         if (semaphoreSecurity == null)
137                                 throw new ArgumentNullException ("semaphoreSecurity");
138                                 
139                         semaphoreSecurity.PersistModifications (SafeWaitHandle);
140                 }
141
142                 // static methods
143
144 #if !MOBILE
145                 public static Semaphore OpenExisting (string name)
146                 {
147                         return OpenExisting (name, SemaphoreRights.Synchronize | SemaphoreRights.Modify);
148                 }
149
150                 public static Semaphore OpenExisting (string name, SemaphoreRights rights)
151                 {
152                         if (name == null)
153                                 throw new ArgumentNullException ("name");
154                         if ((name.Length ==0) || (name.Length > 260))
155                                 throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
156
157                         MonoIOError error;
158                         IntPtr handle = OpenSemaphore_internal (name, rights,
159                                                                 out error);
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 ();
165                                 } else {
166                                         throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
167                                 }
168                         }
169                         
170                         return(new Semaphore (handle));
171                 }
172
173                 [SecurityPermissionAttribute (SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
174                 public static bool TryOpenExisting (string name, out Semaphore result)
175                 {
176                         return TryOpenExisting (name, SemaphoreRights.Synchronize | SemaphoreRights.Modify, out result);
177                 }
178
179                 [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
180                 public static bool TryOpenExisting (string name, SemaphoreRights rights, out Semaphore result)
181                 {
182                         if (name == null)
183                                 throw new ArgumentNullException ("name");
184                         if ((name.Length == 0) || (name.Length > 260))
185                                 throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
186                         
187                         MonoIOError error;
188                         IntPtr handle = OpenSemaphore_internal (name, rights, out error);
189
190                         if (handle == (IntPtr)null) {
191                                 result = null;
192                                 return false;
193                         }
194
195                         result = new Semaphore (handle);
196                         return true;
197                 }
198 #else
199                 public static Semaphore OpenExisting (string name)
200                 {
201                         throw new NotSupportedException ();
202                 }
203
204                 public static Semaphore OpenExisting (string name, SemaphoreRights rights)
205                 {
206                         throw new NotSupportedException ();
207                 }
208
209                 public static bool TryOpenExisting (string name, out Semaphore result)
210                 {
211                         throw new NotSupportedException ();
212                 }
213
214                 public static bool TryOpenExisting (string name, SemaphoreRights rights, out Semaphore result)
215                 {
216                         throw new NotSupportedException ();
217                 }
218 #endif
219         }
220 }
221