Fawkes API  Fawkes Development Version
rwlock_queue.h
1 
2 /***************************************************************************
3  * rwlock_queue.h - Queue with read/write lock
4  *
5  * Created: Tue Jan 13 16:36:52 2009
6  * Copyright 2006-2009 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef __CORE_UTILS_RWLOCK_QUEUE_H_
25 #define __CORE_UTILS_RWLOCK_QUEUE_H_
26 
27 #include <core/threading/read_write_lock.h>
28 #include <core/utils/refptr.h>
29 #include <queue>
30 
31 namespace fawkes {
32 
33 
34 template <typename Type>
35 class RWLockQueue : public std::queue<Type>
36 {
37  public:
38  RWLockQueue();
39  RWLockQueue(const RWLockQueue<Type> &ll);
40  virtual ~RWLockQueue();
41 
42  void lock_for_read();
43  void lock_for_write();
44  bool try_lock_for_read();
45  bool try_lock_for_write();
46  void unlock();
48 
49  void push_locked(const Type& x);
50  void pop_locked();
51 
52  void clear();
53 
54  // not needed, no change to rwlock required (thus "incomplete" BigThree)
55  //LockList<Type> & operator=(const LockList<Type> &ll);
56  private:
57  RefPtr<ReadWriteLock> __rwlock;
58 
59 };
60 
61 
62 /** @class RWLockQueue <core/utils/rwlock_queue.h>
63  * Queue with a read/write lock.
64  * This class provides a queue that has an intrinsic lock. The lock can be applied
65  * with the regular locking methods.
66  *
67  * @see ReadWriteLock
68  * @ingroup FCL
69  * @author Tim Niemueller
70  */
71 
72 
73 /** Constructor. */
74 template <typename Type>
76 {
77  __rwlock = new ReadWriteLock();
78 }
79 
80 
81 /** Copy constructor.
82  * @param ll RWLockQueue to copy
83  */
84 template <typename Type>
86  : std::queue<Type>::queue(ll)
87 {
88  __rwlock = new ReadWriteLock();
89 }
90 
91 
92 /** Destructor. */
93 template <typename Type>
95 {
96  delete __rwlock;
97 }
98 
99 
100 /** Lock queue for reading. */
101 template <typename Type>
102 void
104 {
105  __rwlock->lock_for_read();
106 }
107 
108 
109 /** Lock queue for writing. */
110 template <typename Type>
111 void
113 {
114  __rwlock->lock_for_write();
115 }
116 
117 
118 /** Try to lock queue for reading.
119  * @return true, if the lock has been aquired, false otherwise.
120  */
121 template <typename Type>
122 bool
124 {
125  return __rwlock->try_lock_for_read();
126 }
127 
128 
129 /** Try to lock queue for writing.
130  * @return true, if the lock has been aquired, false otherwise.
131  */
132 template <typename Type>
133 bool
135 {
136  return __rwlock->try_lock_for_write();
137 }
138 
139 
140 /** Unlock list. */
141 template <typename Type>
142 void
144 {
145  return __rwlock->unlock();
146 }
147 
148 
149 /** Push element to queue with lock protection.
150  * @param x element to add
151  */
152 template <typename Type>
153 void
155 {
156  __rwlock->lock_for_write();
157  std::queue<Type>::push(x);
158  __rwlock->unlock();
159 }
160 
161 
162 /** Pop element from queue with lock protection.
163  */
164 template <typename Type>
165 void
167 {
168  __rwlock->lock_for_write();
169  std::queue<Type>::pop();
170  __rwlock->unlock();
171 }
172 
173 
174 /** Clear the queue. */
175 template <typename Type>
176 void
178 {
179  __rwlock->lock_for_write();
180  while ( ! std::queue<Type>::empty() ) {
181  std::queue<Type>::pop();
182  }
183  __rwlock->unlock();
184 }
185 
186 
187 /** Get access to the internal rwlock.
188  * Can be used with RwlockLocker.
189  * @return internal rwlock
190  */
191 template <typename Type>
194 {
195  return __rwlock;
196 }
197 
198 
199 } // end namespace fawkes
200 
201 #endif
void pop_locked()
Pop element from queue with lock protection.
Definition: rwlock_queue.h:166
Queue with a read/write lock.
Definition: rwlock_queue.h:35
bool try_lock_for_write()
Try to lock queue for writing.
Definition: rwlock_queue.h:134
Fawkes library namespace.
STL namespace.
RefPtr< ReadWriteLock > rwlock() const
Get access to the internal rwlock.
Definition: rwlock_queue.h:193
void lock_for_read()
Lock queue for reading.
Definition: rwlock_queue.h:103
virtual ~RWLockQueue()
Destructor.
Definition: rwlock_queue.h:94
bool try_lock_for_read()
Try to lock queue for reading.
Definition: rwlock_queue.h:123
void clear()
Clear the queue.
Definition: rwlock_queue.h:177
void push_locked(const Type &x)
Push element to queue with lock protection.
Definition: rwlock_queue.h:154
void lock_for_write()
Lock queue for writing.
Definition: rwlock_queue.h:112
void unlock()
Unlock list.
Definition: rwlock_queue.h:143
Read/write lock to allow multiple readers but only a single writer on the resource at a time...
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:49
RWLockQueue()
Constructor.
Definition: rwlock_queue.h:75