/usr/include/zthread/BoundedQueue.h is in libzthread-dev 2.3.2-7ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 | /*
* Copyright (c) 2005, Eric Crahen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef __ZTBOUNDEDQUEUE_H__
#define __ZTBOUNDEDQUEUE_H__
#include "zthread/Condition.h"
#include "zthread/Guard.h"
#include "zthread/Queue.h"
#include <deque>
namespace ZThread {
/**
* @class BoundedQueue
*
* @author Eric Crahen <http://www.code-foo.com>
* @date <2003-07-16T13:54:04-0400>
* @version 2.3.0
*
* A BoundedQueue provides serialized access to a set of values. It differs from other
* Queues by adding a maximum capacity, giving it the following properties:
*
* - Threads calling the empty() methods will be blocked until the BoundedQueue becomes empty.
* - Threads calling the next() methods will be blocked until the BoundedQueue has a value to
* return.
* - Threads calling the add() methods will be blocked until the number of values in the
* Queue drops below the maximum capacity.
*
* @see Queue
*/
template <class T, class LockType, typename StorageType=std::deque<T> >
class BoundedQueue : public Queue<T>, public Lockable {
//! Maximum capacity for the Queue
size_t _capacity;
//! Serialize access
LockType _lock;
//! Signaled if not full
Condition _notFull;
//! Signaled if not empty
Condition _notEmpty;
//! Signaled if empty
Condition _isEmpty;
//! Storage backing the queue
StorageType _queue;
//! Cancellation flag
volatile bool _canceled;
public:
/**
* Create a BoundedQueue with the given capacity.
*
* @param capacity maximum number of values to allow in the Queue at
* at any time
*/
BoundedQueue(size_t capacity)
: _notFull(_lock), _notEmpty(_lock), _isEmpty(_lock),
_capacity(capacity), _canceled(false) {}
//! Destroy this Queue
virtual ~BoundedQueue() { }
/**
* Get the maximum capacity of this Queue.
*
* @return <i>size_t</i> maximum capacity
*/
size_t capacity() {
return _capacity;
}
/**
* Add a value to this Queue.
*
* If the number of values in the queue matches the value returned by <i>capacity</i>()
* then the calling thread will be blocked until at least one value is removed from
* the Queue.
*
* @param item value to be added to the Queue
*
* @exception Cancellation_Exception thrown if this Queue has been canceled.
* @exception Interrupted_Exception thrown if the thread was interrupted while waiting
* to add a value
*
* @pre The Queue should not have been canceled prior to the invocation of this function.
* @post If no exception is thrown, a copy of <i>item</i> will have been added to the Queue.
*
* @see Queue::add(const T& item)
*/
virtual void add(const T& item) {
Guard<LockType> g(_lock);
// Wait for the capacity of the Queue to drop
while ((_queue.size() == _capacity) && !_canceled)
_notFull.wait();
if(_canceled)
throw Cancellation_Exception();
_queue.push_back(item);
_notEmpty.signal(); // Wake any waiters
}
/**
* Add a value to this Queue.
*
* If the number of values in the queue matches the value returned by <i>capacity</i>()
* then the calling thread will be blocked until at least one value is removed from
* the Queue.
*
* @param item value to be added to the Queue
* @param timeout maximum amount of time (milliseconds) this method may block
* the calling thread.
*
* @return
* - <em>true</em> if a copy of <i>item</i> can be added before <i>timeout</i>
* milliseconds elapse.
* - <em>false</em> otherwise.
*
* @exception Cancellation_Exception thrown if this Queue has been canceled.
* @exception Interrupted_Exception thrown if the thread was interrupted while waiting
* to add a value
*
* @pre The Queue should not have been canceled prior to the invocation of this function.
* @post If no exception is thrown, a copy of <i>item</i> will have been added to the Queue.
*
* @see Queue::add(const T& item, unsigned long timeout)
*/
virtual bool add(const T& item, unsigned long timeout) {
try {
Guard<LockType> g(_lock, timeout);
// Wait for the capacity of the Queue to drop
while ((_queue.size() == _capacity) && !_canceled)
if(!_notFull.wait(timeout))
return false;
if(_canceled)
throw Cancellation_Exception();
_queue.push_back(item);
_notEmpty.signal(); // Wake any waiters
} catch(Timeout_Exception&) { return false; }
return true;
}
/**
* Retrieve and remove a value from this Queue.
*
* If invoked when there are no values present to return then the calling thread
* will be blocked until a value arrives in the Queue.
*
* @return <em>T</em> next available value
*
* @exception Cancellation_Exception thrown if this Queue has been canceled.
* @exception Interrupted_Exception thrown if the thread was interrupted while waiting
* to retrieve a value
*
* @pre The Queue should not have been canceled prior to the invocation of this function.
* @post The value returned will have been removed from the Queue.
*/
virtual T next() {
Guard<LockType> g(_lock);
while ( _queue.size() == 0 && !_canceled)
_notEmpty.wait();
if( _queue.size() == 0) // Queue canceled
throw Cancellation_Exception();
T item = _queue.front();
_queue.pop_front();
_notFull.signal(); // Wake any thread trying to add
if(_queue.size() == 0) // Wake empty waiters
_isEmpty.broadcast();
return item;
}
/**
* Retrieve and remove a value from this Queue.
*
* If invoked when there are no values present to return then the calling thread
* will be blocked until a value arrives in the Queue.
*
* @param timeout maximum amount of time (milliseconds) this method may block
* the calling thread.
*
* @return <em>T</em> next available value
*
* @exception Cancellation_Exception thrown if this Queue has been canceled.
* @exception Timeout_Exception thrown if the timeout expires before a value
* can be retrieved.
*
* @pre The Queue should not have been canceled prior to the invocation of this function.
* @post The value returned will have been removed from the Queue.
*/
virtual T next(unsigned long timeout) {
Guard<LockType> g(_lock, timeout);
// Wait for items to be added
while (_queue.size() == 0 && !_canceled) {
if(!_notEmpty.wait(timeout))
throw Timeout_Exception();
}
if(_queue.size() == 0) // Queue canceled
throw Cancellation_Exception();
T item = _queue.front();
_queue.pop_front();
_notFull.signal(); // Wake add() waiters
if(_queue.size() == 0) // Wake empty() waiters
_isEmpty.broadcast();
return item;
}
/**
* Cancel this queue.
*
* @post Any threads blocked by an add() function will throw a Cancellation_Exception.
* @post Any threads blocked by a next() function will throw a Cancellation_Exception.
*
* @see Queue::cancel()
*/
virtual void cancel() {
Guard<LockType> g(_lock);
_canceled = true;
_notEmpty.broadcast(); // Wake next() waiters
}
/**
* @see Queue::isCanceled()
*/
virtual bool isCanceled() {
// Faster check since the Queue will not become un-canceled
if(_canceled)
return true;
Guard<LockType> g(_lock);
return _canceled;
}
/**
* @see Queue::size()
*/
virtual size_t size() {
Guard<LockType> g(_lock);
return _queue.size();
}
/**
* @see Queue::size(unsigned long timeout)
*/
virtual size_t size(unsigned long timeout) {
Guard<LockType> g(_lock, timeout);
return _queue.size();
}
/**
* Test whether any values are available in this Queue.
*
* The calling thread is blocked until there are no values present
* in the Queue.
*
* @return
* - <em>true</em> if there are no values available.
* - <em>false</em> if there <i>are</i> values available.
*
* @see Queue::empty()
*/
virtual bool empty() {
Guard<LockType> g(_lock);
while(_queue.size() > 0) // Wait for an empty signal
_isEmpty.wait();
return true;
}
/**
* Test whether any values are available in this Queue.
*
* The calling thread is blocked until there are no values present
* in the Queue.
*
* @param timeout maximum amount of time (milliseconds) this method may block
* the calling thread.
*
* @return
* - <em>true</em> if there are no values available.
* - <em>false</em> if there <i>are</i> values available.
*
* @exception Timeout_Exception thrown if <i>timeout</i> milliseconds
* expire before a value becomes available
*
* @see Queue::empty()
*/
virtual bool empty(unsigned long timeout) {
Guard<LockType> g(_lock, timeout);
while(_queue.size() > 0) // Wait for an empty signal
_isEmpty.wait(timeout);
return true;
}
public:
virtual void acquire() {
_lock.acquire();
}
virtual bool tryAcquire(unsigned long timeout) {
return _lock.tryAcquire(timeout);
}
virtual void release() {
_lock.release();
}
}; /* BoundedQueue */
} // namespace ZThread
#endif // __ZTBOUNDEDQUEUE_H__
|