61
61
62
62
namespace RabbitMQ . Util {
63
63
///<summary>A thread-safe shared queue implementation.</summary>
64
- public class SharedQueue : IDisposable {
64
+ public class SharedQueue {
65
65
///<summary>The shared queue.</summary>
66
66
///<remarks>
67
67
///Subclasses must ensure appropriate locking discipline when
@@ -77,23 +77,17 @@ public class SharedQueue: IDisposable {
77
77
public SharedQueue ( ) {
78
78
}
79
79
80
- ///<summary>Close the queue. Causes all waiting threads to be
81
- ///interrupted (with EndOfStreamException) and all further
82
- ///Enqueue() and Dequeue() operations to throw
83
- ///EndOfStreamException.</summary>
80
+ ///<summary>Close the queue. Causes all further Enqueue()
81
+ ///operations to throw EndOfStreamException, and all pending
82
+ ///or subsequent Dequeue() operations to throw an
83
+ ///EndOfStreamException once the queue is empty .</summary>
84
84
public void Close ( ) {
85
85
lock ( m_queue ) {
86
86
m_isOpen = false ;
87
87
Monitor . PulseAll ( m_queue ) ;
88
88
}
89
89
}
90
90
91
- ///<summary>Implement IDisposable.Dispose. Delegates directly
92
- ///to Close().</summary>
93
- public void Dispose ( ) {
94
- Close ( ) ;
95
- }
96
-
97
91
///<summary>Call only when the lock on m_queue is held.</summary>
98
92
/// <exception cref="EndOfStreamException" />
99
93
private void EnsureIsOpen ( ) {
@@ -121,12 +115,11 @@ public void Enqueue(object o) {
121
115
///<remarks>
122
116
///Callers of Dequeue() will block if no items are available
123
117
///until some other thread calls Enqueue() or the queue is
124
- ///closed. If the queue is closed (by a call to Close()), this
125
- ///method will throw EndOfStreamException.
118
+ ///closed. In the latter case this method will throw
119
+ ///EndOfStreamException.
126
120
///</remarks>
127
121
public object Dequeue ( ) {
128
122
lock ( m_queue ) {
129
- EnsureIsOpen ( ) ;
130
123
while ( m_queue . Count == 0 ) {
131
124
EnsureIsOpen ( ) ;
132
125
Monitor . Wait ( m_queue ) ;
@@ -151,15 +144,15 @@ public object Dequeue() {
151
144
/// whereas Dequeue() will.
152
145
///</para>
153
146
///<para>
154
- /// If, at the time of call, the queue is in a closed state
155
- /// (by a call to Close()), this method will throw
156
- /// EndOfStreamException.
147
+ /// If at the time of call the queue is empty and in a
148
+ /// closed state (following a call to Close()), then this
149
+ /// method will throw EndOfStreamException.
157
150
///</para>
158
151
///</remarks>
159
152
public object DequeueNoWait ( object defaultValue ) {
160
153
lock ( m_queue ) {
161
- EnsureIsOpen ( ) ;
162
154
if ( m_queue . Count == 0 ) {
155
+ EnsureIsOpen ( ) ;
163
156
return defaultValue ;
164
157
} else {
165
158
return m_queue . Dequeue ( ) ;
@@ -197,7 +190,8 @@ public object DequeueNoWait(object defaultValue) {
197
190
/// System.Threading.Monitor.Wait(object,int).
198
191
///</para>
199
192
///<para>
200
- /// If, at any time during the call, the queue is in or
193
+ /// If no items are present and the queue is in a closed
194
+ /// state, or if at any time while waiting the queue
201
195
/// transitions to a closed state (by a call to Close()), this
202
196
/// method will throw EndOfStreamException.
203
197
///</para>
@@ -210,8 +204,8 @@ public bool Dequeue(int millisecondsTimeout, out object result) {
210
204
211
205
DateTime startTime = DateTime . Now ;
212
206
lock ( m_queue ) {
213
- EnsureIsOpen ( ) ;
214
207
while ( m_queue . Count == 0 ) {
208
+ EnsureIsOpen ( ) ;
215
209
int elapsedTime = ( int ) ( ( DateTime . Now - startTime ) . TotalMilliseconds ) ;
216
210
int remainingTime = millisecondsTimeout - elapsedTime ;
217
211
if ( remainingTime <= 0 ) {
@@ -220,7 +214,6 @@ public bool Dequeue(int millisecondsTimeout, out object result) {
220
214
}
221
215
222
216
Monitor . Wait ( m_queue , remainingTime ) ;
223
- EnsureIsOpen ( ) ;
224
217
}
225
218
226
219
result = m_queue . Dequeue ( ) ;
0 commit comments