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 
32 namespace fawkes {
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 /** @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. */
71 template <typename Type>
73 {
74  rwlock_ = new ReadWriteLock();
75 }
76 
77 /** Copy constructor.
78  * @param ll RWLockQueue to copy
79  */
80 template <typename Type>
81 RWLockQueue<Type>::RWLockQueue(const RWLockQueue<Type> &ll) : std::queue<Type>::queue(ll)
82 {
83  rwlock_ = new ReadWriteLock();
84 }
85 
86 /** Destructor. */
87 template <typename Type>
89 {
90  delete rwlock_;
91 }
92 
93 /** Lock queue for reading. */
94 template <typename Type>
95 void
97 {
98  rwlock_->lock_for_read();
99 }
100 
101 /** Lock queue for writing. */
102 template <typename Type>
103 void
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  */
112 template <typename Type>
113 bool
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  */
122 template <typename Type>
123 bool
125 {
126  return rwlock_->try_lock_for_write();
127 }
128 
129 /** Unlock list. */
130 template <typename Type>
131 void
133 {
134  return rwlock_->unlock();
135 }
136 
137 /** Push element to queue with lock protection.
138  * @param x element to add
139  */
140 template <typename Type>
141 void
142 RWLockQueue<Type>::push_locked(const Type &x)
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  */
151 template <typename Type>
152 void
154 {
155  rwlock_->lock_for_write();
156  std::queue<Type>::pop();
157  rwlock_->unlock();
158 }
159 
160 /** Clear the queue. */
161 template <typename Type>
162 void
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  */
176 template <typename Type>
179 {
180  return rwlock_;
181 }
182 
183 } // end namespace fawkes
184 
185 #endif
fawkes::RWLockQueue::pop_locked
void pop_locked()
Pop element from queue with lock protection.
Definition: rwlock_queue.h:159
fawkes::RefPtr
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:57
fawkes::RWLockQueue::lock_for_write
void lock_for_write()
Lock queue for writing.
Definition: rwlock_queue.h:110
fawkes::RWLockQueue::rwlock
RefPtr< ReadWriteLock > rwlock() const
Get access to the internal rwlock.
Definition: rwlock_queue.h:184
fawkes::RWLockQueue::~RWLockQueue
virtual ~RWLockQueue()
Destructor.
Definition: rwlock_queue.h:94
fawkes::RWLockQueue::try_lock_for_read
bool try_lock_for_read()
Try to lock queue for reading.
Definition: rwlock_queue.h:120
fawkes::ReadWriteLock
Definition: read_write_lock.h:37
fawkes::RWLockQueue::RWLockQueue
RWLockQueue()
Constructor.
Definition: rwlock_queue.h:78
fawkes
fawkes::RWLockQueue::try_lock_for_write
bool try_lock_for_write()
Try to lock queue for writing.
Definition: rwlock_queue.h:130
fawkes::RWLockQueue::clear
void clear()
Clear the queue.
Definition: rwlock_queue.h:169
fawkes::RWLockQueue::lock_for_read
void lock_for_read()
Lock queue for reading.
Definition: rwlock_queue.h:102
fawkes::RWLockQueue::unlock
void unlock()
Unlock list.
Definition: rwlock_queue.h:138
fawkes::RWLockQueue::push_locked
void push_locked(const Type &x)
Push element to queue with lock protection.
Definition: rwlock_queue.h:148
fawkes::RWLockQueue
Definition: rwlock_queue.h:41