2009-05-21 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / RabbitMQ.Client / src / util / Semaphore.cs
1 // This source code is dual-licensed under the Apache License, version
2 // 2.0, and the Mozilla Public License, version 1.1.
3 //
4 // The APL v2.0:
5 //
6 //---------------------------------------------------------------------------
7 //   Copyright (C) 2007-2009 LShift Ltd., Cohesive Financial
8 //   Technologies LLC., and Rabbit Technologies Ltd.
9 //
10 //   Licensed under the Apache License, Version 2.0 (the "License");
11 //   you may not use this file except in compliance with the License.
12 //   You may obtain a copy of the License at
13 //
14 //       http://www.apache.org/licenses/LICENSE-2.0
15 //
16 //   Unless required by applicable law or agreed to in writing, software
17 //   distributed under the License is distributed on an "AS IS" BASIS,
18 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 //   See the License for the specific language governing permissions and
20 //   limitations under the License.
21 //---------------------------------------------------------------------------
22 //
23 // The MPL v1.1:
24 //
25 //---------------------------------------------------------------------------
26 //   The contents of this file are subject to the Mozilla Public License
27 //   Version 1.1 (the "License"); you may not use this file except in
28 //   compliance with the License. You may obtain a copy of the License at
29 //   http://www.rabbitmq.com/mpl.html
30 //
31 //   Software distributed under the License is distributed on an "AS IS"
32 //   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
33 //   License for the specific language governing rights and limitations
34 //   under the License.
35 //
36 //   The Original Code is The RabbitMQ .NET Client.
37 //
38 //   The Initial Developers of the Original Code are LShift Ltd,
39 //   Cohesive Financial Technologies LLC, and Rabbit Technologies Ltd.
40 //
41 //   Portions created before 22-Nov-2008 00:00:00 GMT by LShift Ltd,
42 //   Cohesive Financial Technologies LLC, or Rabbit Technologies Ltd
43 //   are Copyright (C) 2007-2008 LShift Ltd, Cohesive Financial
44 //   Technologies LLC, and Rabbit Technologies Ltd.
45 //
46 //   Portions created by LShift Ltd are Copyright (C) 2007-2009 LShift
47 //   Ltd. Portions created by Cohesive Financial Technologies LLC are
48 //   Copyright (C) 2007-2009 Cohesive Financial Technologies
49 //   LLC. Portions created by Rabbit Technologies Ltd are Copyright
50 //   (C) 2007-2009 Rabbit Technologies Ltd.
51 //
52 //   All Rights Reserved.
53 //
54 //   Contributor(s): ______________________________________.
55 //
56 //---------------------------------------------------------------------------
57 using System;
58 using System.Collections;
59 using System.Threading;
60
61 namespace RabbitMQ.Util {
62     ///<summary>A classic counting semaphore.</summary>
63     ///<remarks>
64     /// The .NET framework does not introduce a counting semaphore
65     /// until framework release 2.0. Consequently, we implement one
66     /// here, for the benefit of .NET 1.1.
67     ///</remarks>
68     public class Semaphore {
69         private int m_count;
70
71         ///<summary>Create a Semaphore, with its counter initialized
72         ///to 1.</summary>
73         public Semaphore()
74             : this(1)
75         {}
76
77         ///<summary>Create a Semaphore, with its counter initialized
78         ///to the value passed in.</summary>
79         public Semaphore(int initialCount)
80         {
81             m_count = initialCount;
82         }
83
84         ///<summary>Acquire a single resource, decrementing the count by one.</summary>
85         ///<remarks>
86         /// Not interruptable - will retry forever until a resource comes available.
87         ///</remarks>
88         public void Wait() {
89             lock (this) {
90                 while (true) {
91                     if (m_count > 0) {
92                         m_count--;
93                         return;
94                     } else {
95                         Monitor.Wait(this);
96                     }
97                 }
98             }
99         }
100
101         ///<summary>Acquire a single resource, decrementing the count
102         ///by one and returning true, if a resource is available;
103         ///otherwise, return false immediately.</summary>
104         public bool TryWait() {
105             lock (this) {
106                 if (m_count > 0) {
107                     m_count--;
108                     return true;
109                 } else {
110                     return false;
111                 }
112             }
113         }
114
115         ///<summary>Release a single resource, incrementing the count
116         ///by one.</summary>
117         public void Release() {
118             lock (this) {
119                 m_count++;
120                 Monitor.Pulse(this);
121             }
122         }
123
124         ///<summary>Retrieve the current semaphore value.</summary>
125         ///<remarks>
126         /// Consider carefully whether this property is actually what
127         /// you want - usually, using this property is wrong, and
128         /// either Wait() or TryWait() is the correct choice.
129         ///</remarks>
130         public int Value {
131             get {
132                 lock (this) {
133                     return m_count;
134                 }
135             }
136         }
137     }
138 }