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
30#include <queue>
31
32namespace fawkes {
33
34template <typename Type>
35class RWLockQueue : public std::queue<Type>
36{
37public:
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);
56private:
58};
59
60/** @class RWLockQueue <core/utils/rwlock_queue.h>
61 * Queue with a read/write lock.
62 * This class provides a queue that has an intrinsic lock. The lock can be applied
63 * with the regular locking methods.
64 *
65 * @see ReadWriteLock
66 * @ingroup FCL
67 * @author Tim Niemueller
68 */
69
70/** Constructor. */
71template <typename Type>
73{
74 rwlock_ = new ReadWriteLock();
75}
76
77/** Copy constructor.
78 * @param ll RWLockQueue to copy
79 */
80template <typename Type>
81RWLockQueue<Type>::RWLockQueue(const RWLockQueue<Type> &ll) : std::queue<Type>::queue(ll)
82{
83 rwlock_ = new ReadWriteLock();
84}
85
86/** Destructor. */
87template <typename Type>
89{
90 delete rwlock_;
91}
92
93/** Lock queue for reading. */
94template <typename Type>
95void
97{
98 rwlock_->lock_for_read();
99}
100
101/** Lock queue for writing. */
102template <typename Type>
103void
105{
106 rwlock_->lock_for_write();
107}
108
109/** Try to lock queue for reading.
110 * @return true, if the lock has been aquired, false otherwise.
111 */
112template <typename Type>
113bool
115{
116 return rwlock_->try_lock_for_read();
117}
118
119/** Try to lock queue for writing.
120 * @return true, if the lock has been aquired, false otherwise.
121 */
122template <typename Type>
123bool
125{
126 return rwlock_->try_lock_for_write();
127}
128
129/** Unlock list. */
130template <typename Type>
131void
133{
134 return rwlock_->unlock();
135}
136
137/** Push element to queue with lock protection.
138 * @param x element to add
139 */
140template <typename Type>
141void
143{
144 rwlock_->lock_for_write();
145 std::queue<Type>::push(x);
146 rwlock_->unlock();
147}
148
149/** Pop element from queue with lock protection.
150 */
151template <typename Type>
152void
154{
155 rwlock_->lock_for_write();
156 std::queue<Type>::pop();
157 rwlock_->unlock();
158}
159
160/** Clear the queue. */
161template <typename Type>
162void
164{
165 rwlock_->lock_for_write();
166 while (!std::queue<Type>::empty()) {
167 std::queue<Type>::pop();
168 }
169 rwlock_->unlock();
170}
171
172/** Get access to the internal rwlock.
173 * Can be used with RwlockLocker.
174 * @return internal rwlock
175 */
176template <typename Type>
179{
180 return rwlock_;
181}
182
183} // end namespace fawkes
184
185#endif
Queue with a read/write lock.
Definition: rwlock_queue.h:36
void pop_locked()
Pop element from queue with lock protection.
Definition: rwlock_queue.h:153
void push_locked(const Type &x)
Push element to queue with lock protection.
Definition: rwlock_queue.h:142
RefPtr< ReadWriteLock > rwlock() const
Get access to the internal rwlock.
Definition: rwlock_queue.h:178
void clear()
Clear the queue.
Definition: rwlock_queue.h:163
void lock_for_write()
Lock queue for writing.
Definition: rwlock_queue.h:104
void lock_for_read()
Lock queue for reading.
Definition: rwlock_queue.h:96
bool try_lock_for_write()
Try to lock queue for writing.
Definition: rwlock_queue.h:124
void unlock()
Unlock list.
Definition: rwlock_queue.h:132
virtual ~RWLockQueue()
Destructor.
Definition: rwlock_queue.h:88
RWLockQueue()
Constructor.
Definition: rwlock_queue.h:72
bool try_lock_for_read()
Try to lock queue for reading.
Definition: rwlock_queue.h:114
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:50
Fawkes library namespace.