2 * Copyright (c) 2005, Eric Crahen
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is furnished
9 * to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #ifndef __ZTLOCKEDQUEUE_H__
24 #define __ZTLOCKEDQUEUE_H__
26 #include "zthread/Guard.h"
27 #include "zthread/Queue.h"
35 * @author Eric Crahen <http://www.code-foo.com>
36 * @date <2003-07-16T11:42:33-0400>
39 * A LockedQueue is the simple Queue implementation that provides
40 * serialized access to the values added to it.
42 template <class T
, class LockType
, typename StorageType
=std::deque
<T
> >
43 class LockedQueue
: public Queue
<T
> {
45 //! Serialize access to the Queue
48 //! Storage backing the queue
52 volatile bool _canceled
;
56 //! Create a LockedQueue
57 LockedQueue() : _canceled(false) {}
59 //! Destroy a LockedQueue
60 virtual ~LockedQueue() { }
63 * @see Queue::add(const T& item)
65 virtual void add(const T
& item
) {
67 Guard
<LockType
> g(_lock
);
70 throw Cancellation_Exception();
72 _queue
.push_back(item
);
77 * @see Queue::add(const T& item, unsigned long timeout)
79 virtual bool add(const T
& item
, unsigned long timeout
) {
83 Guard
<LockType
> g(_lock
, timeout
);
86 throw Cancellation_Exception();
88 _queue
.push_back(item
);
90 } catch(Timeout_Exception
&) { return false; }
101 Guard
<LockType
> g(_lock
);
103 if(_queue
.empty() && _canceled
)
104 throw Cancellation_Exception();
107 throw NoSuchElement_Exception();
109 T item
= _queue
.front();
118 * @see Queue::next(unsigned long timeout)
120 virtual T
next(unsigned long timeout
) {
122 Guard
<LockType
> g(_lock
, timeout
);
124 if(_queue
.empty() && _canceled
)
125 throw Cancellation_Exception();
128 throw NoSuchElement_Exception();
130 T item
= _queue
.front();
139 Guard
<LockType
> g(_lock
);
142 throw NoSuchElement_Exception();
144 return _queue
.front();
148 * @see Queue::cancel()
150 virtual void cancel() {
152 Guard
<LockType
> g(_lock
);
159 * @see Queue::isCanceled()
161 virtual bool isCanceled() {
163 // Faster check since the queue will not become un-canceled
167 Guard
<LockType
> g(_lock
);
176 virtual size_t size() {
178 Guard
<LockType
> g(_lock
);
179 return _queue
.size();
184 * @see Queue::size(unsigned long timeout)
186 virtual size_t size(unsigned long timeout
) {
188 Guard
<LockType
> g(_lock
, timeout
);
189 return _queue
.size();
195 } // namespace ZThread
197 #endif // __ZTLOCKEDQUEUE_H__