Initial Patch of Auction House bot rev. 135
[auctionmangos.git] / dep / include / zthread / LockedQueue.h
bloba1f0df264319000d461f3afd512dffd466a2a202
1 /*
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"
29 #include <deque>
31 namespace ZThread {
33 /**
34 * @class LockedQueue
35 * @author Eric Crahen <http://www.code-foo.com>
36 * @date <2003-07-16T11:42:33-0400>
37 * @version 2.3.0
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
46 LockType _lock;
48 //! Storage backing the queue
49 StorageType _queue;
51 //! Cancellation flag
52 volatile bool _canceled;
54 public:
56 //! Create a LockedQueue
57 LockedQueue() : _canceled(false) {}
59 //! Destroy a LockedQueue
60 virtual ~LockedQueue() { }
62 /**
63 * @see Queue::add(const T& item)
65 virtual void add(const T& item) {
67 Guard<LockType> g(_lock);
69 if(_canceled)
70 throw Cancellation_Exception();
72 _queue.push_back(item);
76 /**
77 * @see Queue::add(const T& item, unsigned long timeout)
79 virtual bool add(const T& item, unsigned long timeout) {
81 try {
83 Guard<LockType> g(_lock, timeout);
85 if(_canceled)
86 throw Cancellation_Exception();
88 _queue.push_back(item);
90 } catch(Timeout_Exception&) { return false; }
92 return true;
96 /**
97 * @see Queue::next()
99 virtual T next() {
101 Guard<LockType> g(_lock);
103 if(_queue.empty() && _canceled)
104 throw Cancellation_Exception();
106 if(_queue.empty())
107 throw NoSuchElement_Exception();
109 T item = _queue.front();
110 _queue.pop_front();
112 return item;
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();
127 if(_queue.empty())
128 throw NoSuchElement_Exception();
130 T item = _queue.front();
131 _queue.pop_front();
133 return item;
137 virtual T front()
139 Guard<LockType> g(_lock);
141 if(_queue.empty())
142 throw NoSuchElement_Exception();
144 return _queue.front();
148 * @see Queue::cancel()
150 virtual void cancel() {
152 Guard<LockType> g(_lock);
154 _canceled = true;
159 * @see Queue::isCanceled()
161 virtual bool isCanceled() {
163 // Faster check since the queue will not become un-canceled
164 if(_canceled)
165 return true;
167 Guard<LockType> g(_lock);
169 return _canceled;
174 * @see Queue::size()
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();
193 }; /* LockedQueue */
195 } // namespace ZThread
197 #endif // __ZTLOCKEDQUEUE_H__