Merge pull request #347 from JamesB7/master
[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.Runtime.CompilerServices;
33 using System.IO;
34
35 namespace System.Threading {
36
37         [ComVisible (false)]
38         public sealed class Semaphore : WaitHandle {
39
40                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
41                 private static extern IntPtr CreateSemaphore_internal (
42                         int initialCount, int maximumCount, string name,
43                         out bool createdNew);
44
45                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
46                 private static extern int ReleaseSemaphore_internal (
47                         IntPtr handle, int releaseCount, out bool fail);
48
49                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
50                 private static extern IntPtr OpenSemaphore_internal (string name, SemaphoreRights rights, out MonoIOError error);
51                 
52                 private Semaphore (IntPtr handle)
53                 {
54                         Handle = handle;
55                 }
56                 
57                 public Semaphore (int initialCount, int maximumCount)
58                         : this (initialCount, maximumCount, null)
59                 {
60                 }
61
62                 public Semaphore (int initialCount, int maximumCount, string name)
63                 {
64                         if (initialCount < 0)
65                                 throw new ArgumentOutOfRangeException ("initialCount", "< 0");
66                         if (maximumCount < 1)
67                                 throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
68                         if (initialCount > maximumCount)
69                                 throw new ArgumentException ("initialCount > maximumCount");
70
71                         bool created;
72                         
73                         Handle = CreateSemaphore_internal (initialCount,
74                                                            maximumCount, name,
75                                                            out created);
76                 }
77
78                 public Semaphore (int initialCount, int maximumCount, string name, out bool createdNew)
79                         : this (initialCount, maximumCount, name, out createdNew, null)
80                 {
81                 }
82
83                 [MonoTODO ("Does not support access control, semaphoreSecurity is ignored")]
84                 public Semaphore (int initialCount, int maximumCount, string name, out bool createdNew, 
85                         SemaphoreSecurity semaphoreSecurity)
86                 {
87                         if (initialCount < 0)
88                                 throw new ArgumentOutOfRangeException ("initialCount", "< 0");
89                         if (maximumCount < 1)
90                                 throw new ArgumentOutOfRangeException ("maximumCount", "< 1");
91                         if (initialCount > maximumCount)
92                                 throw new ArgumentException ("initialCount > maximumCount");
93
94                         Handle = CreateSemaphore_internal (initialCount,
95                                                            maximumCount, name,
96                                                            out createdNew);
97                 }
98
99                 [MonoTODO]
100                 public SemaphoreSecurity GetAccessControl ()
101                 {
102                         throw new NotImplementedException ();
103                 }
104
105                 [PrePrepareMethod]
106                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
107                 public int Release ()
108                 {
109                         return (Release (1));
110                 }
111
112                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
113                 public int Release (int releaseCount)
114                 {
115                         if (releaseCount < 1)
116                                 throw new ArgumentOutOfRangeException ("releaseCount");
117
118                         int ret;
119                         bool fail;
120                         
121                         ret = ReleaseSemaphore_internal (Handle, releaseCount,
122                                                          out fail);
123
124                         if (fail) {
125                                 throw new SemaphoreFullException ();
126                         }
127
128                         return (ret);
129                 }
130
131                 [MonoTODO]
132                 public void SetAccessControl (SemaphoreSecurity semaphoreSecurity)
133                 {
134                         if (semaphoreSecurity == null)
135                                 throw new ArgumentNullException ("semaphoreSecurity");
136
137                         throw new NotImplementedException ();
138                 }
139
140                 // static methods
141
142                 public static Semaphore OpenExisting (string name)
143                 {
144                         return OpenExisting (name, SemaphoreRights.Synchronize | SemaphoreRights.Modify);
145                 }
146
147                 public static Semaphore OpenExisting (string name, SemaphoreRights rights)
148                 {
149                         if (name == null)
150                                 throw new ArgumentNullException ("name");
151                         if ((name.Length ==0) || (name.Length > 260))
152                                 throw new ArgumentException ("name", Locale.GetText ("Invalid length [1-260]."));
153
154                         MonoIOError error;
155                         IntPtr handle = OpenSemaphore_internal (name, rights,
156                                                                 out error);
157                         if (handle == (IntPtr)null) {
158                                 if (error == MonoIOError.ERROR_FILE_NOT_FOUND) {
159                                         throw new WaitHandleCannotBeOpenedException (Locale.GetText ("Named Semaphore handle does not exist: ") + name);
160                                 } else if (error == MonoIOError.ERROR_ACCESS_DENIED) {
161                                         throw new UnauthorizedAccessException ();
162                                 } else {
163                                         throw new IOException (Locale.GetText ("Win32 IO error: ") + error.ToString ());
164                                 }
165                         }
166                         
167                         return(new Semaphore (handle));
168                 }
169         }
170 }
171