root/trunk/dep/include/zthread/LockedQueue.h @ 2

Revision 2, 4.4 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1/*
2 * Copyright (c) 2005, Eric Crahen
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
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.
20 *
21 */
22
23#ifndef __ZTLOCKEDQUEUE_H__
24#define __ZTLOCKEDQUEUE_H__
25
26#include "zthread/Guard.h"
27#include "zthread/Queue.h"
28
29#include <deque>
30
31namespace ZThread {
32
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
38   *
39   * A LockedQueue is the simple Queue implementation that provides
40   * serialized access to the values added to it.
41   */
42  template <class T, class LockType, typename StorageType=std::deque<T> >
43    class LockedQueue : public Queue<T> {
44
45      //! Serialize access to the Queue
46      LockType _lock;
47
48      //! Storage backing the queue
49      StorageType _queue;
50
51      //! Cancellation flag
52      volatile bool _canceled;
53
54      public:
55
56      //! Create a LockedQueue
57      LockedQueue() : _canceled(false) {}
58
59      //! Destroy a LockedQueue
60      virtual ~LockedQueue() { }
61
62      /**
63       * @see Queue::add(const T& item)
64       */
65      virtual void add(const T& item) {
66
67        Guard<LockType> g(_lock);
68   
69        if(_canceled)
70          throw Cancellation_Exception();
71
72        _queue.push_back(item);
73
74      }
75
76      /**
77       * @see Queue::add(const T& item, unsigned long timeout)
78       */
79      virtual bool add(const T& item, unsigned long timeout) {
80     
81        try {
82
83          Guard<LockType> g(_lock, timeout);
84     
85          if(_canceled)
86            throw Cancellation_Exception();
87     
88          _queue.push_back(item);
89
90        } catch(Timeout_Exception&) { return false; }
91 
92        return true;
93
94      }
95
96      /**
97       * @see Queue::next()
98       */
99      virtual T next() {
100   
101        Guard<LockType> g(_lock);
102
103        if(_queue.empty() && _canceled)
104          throw Cancellation_Exception();
105   
106        if(_queue.empty())
107          throw NoSuchElement_Exception();
108
109        T item = _queue.front();
110        _queue.pop_front();
111   
112        return item;
113
114      }
115
116     
117      /**
118       * @see Queue::next(unsigned long timeout)
119       */
120      virtual T next(unsigned long timeout) {
121
122        Guard<LockType> g(_lock, timeout);
123
124        if(_queue.empty() && _canceled)
125          throw Cancellation_Exception();
126   
127        if(_queue.empty())
128          throw NoSuchElement_Exception();
129
130        T item = _queue.front();
131        _queue.pop_front();
132     
133        return item;
134     
135      }
136
137      virtual T front()
138      {
139          Guard<LockType> g(_lock);
140
141          if(_queue.empty())
142              throw NoSuchElement_Exception();
143
144          return _queue.front();
145      }
146
147      /**
148       * @see Queue::cancel()
149       */
150      virtual void cancel() {
151 
152        Guard<LockType> g(_lock);
153
154        _canceled = true;
155
156      }
157
158      /**
159       * @see Queue::isCanceled()
160       */
161      virtual bool isCanceled() {
162   
163        // Faster check since the queue will not become un-canceled
164        if(_canceled)
165          return true;
166     
167        Guard<LockType> g(_lock);
168
169        return _canceled;
170
171      }
172
173      /**
174       * @see Queue::size()
175       */
176      virtual size_t size() {
177
178        Guard<LockType> g(_lock);
179        return _queue.size();
180
181      }
182
183      /**
184       * @see Queue::size(unsigned long timeout)
185       */
186      virtual size_t size(unsigned long timeout) {
187
188        Guard<LockType> g(_lock, timeout);
189        return _queue.size();
190
191      }
192
193    }; /* LockedQueue */
194
195} // namespace ZThread
196
197#endif // __ZTLOCKEDQUEUE_H__
Note: See TracBrowser for help on using the browser.