System.Drawing: added email to icon and test file headers
[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 #if NET_2_0
30
31 using System.Runtime.ConstrainedExecution;
32 using System.Runtime.InteropServices;
33 using System.Security.AccessControl;
34 using System.Runtime.CompilerServices;
35 using System.IO;
36
37 namespace System.Threading {
38
39         [ComVisible (false)]
40         public sealed class Semaphore : WaitHandle {
41
42                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
43                 private static extern IntPtr CreateSemaphore_internal (
44                         int initialCount, int maximumCount, string name,
45                         out bool createdNew);
46
47                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
48                 private static extern int ReleaseSemaphore_internal (
49                         IntPtr handle, int releaseCount, out bool fail);
50
51                 [MethodImplAttribute (MethodImplOptions.InternalCall)]
52                 private static extern IntPtr OpenSemaphore_internal (string name, SemaphoreRights rights, out MonoIOError error);
53                 
54                 private Semaphore (IntPtr handle)
55                 {
56                         Handle = handle;
57                 }
58                 
59                 public Semaphore (int initialCount, int maximumCount)
60                         : this (initialCount, maximumCount, null)
61                 {
62                 }
63
64                 public Semaphore (int initialCount, int maximumCount, string name)
65                 {
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");
72
73                         bool created;
74                         
75                         Handle = CreateSemaphore_internal (initialCount,
76                                                            maximumCount, name,
77                                                            out created);
78                 }
79
80                 public Semaphore (int initialCount, int maximumCount, string name, out bool createdNew)
81                         : this (initialCount, maximumCount, name, out createdNew, null)
82                 {
83                 }
84
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)
88                 {
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");
95
96                         Handle = CreateSemaphore_internal (initialCount,
97                                                            maximumCount, name,
98                                                            out createdNew);
99                 }
100
101                 [MonoTODO]
102                 public SemaphoreSecurity GetAccessControl ()
103                 {
104                         throw new NotImplementedException ();
105                 }
106
107                 [PrePrepareMethod]
108                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
109                 public int Release ()
110                 {
111                         return (Release (1));
112                 }
113
114                 [ReliabilityContract (Consistency.WillNotCorruptState, Cer.Success)]
115                 public int Release (int releaseCount)
116                 {
117                         if (releaseCount < 1)
118                                 throw new ArgumentOutOfRangeException ("releaseCount");
119
120                         int ret;
121                         bool fail;
122                         
123                         ret = ReleaseSemaphore_internal (Handle, releaseCount,
124                                                          out fail);
125
126                         if (fail) {
127                                 throw new SemaphoreFullException ();
128                         }
129
130                         return (ret);
131                 }
132
133                 [MonoTODO]
134                 public void SetAccessControl (SemaphoreSecurity semaphoreSecurity)
135                 {
136                         if (semaphoreSecurity == null)
137                                 throw new ArgumentNullException ("semaphoreSecurity");
138
139                         throw new NotImplementedException ();
140                 }
141
142                 // static methods
143
144                 public static Semaphore OpenExisting (string name)
145                 {
146                         return OpenExisting (name, SemaphoreRights.Synchronize | SemaphoreRights.Modify);
147                 }
148
149                 public static Semaphore OpenExisting (string name, SemaphoreRights rights)
150                 {
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]."));
155
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 ());
166                                 }
167                         }
168                         
169                         return(new Semaphore (handle));
170                 }
171         }
172 }
173
174 #endif