Fawkes API Fawkes Development Version
rwlock_list.h
1
2/***************************************************************************
3 * rwlock_list.h - List with read/write lock
4 *
5 * Created: Tue Jan 13 16:33:35 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_LIST_H_
25#define _CORE_UTILS_RWLOCK_LIST_H_
26
27#include <core/threading/read_write_lock.h>
28#include <core/utils/refptr.h>
29
30#include <list>
31
32namespace fawkes {
33
34template <typename Type>
35class RWLockList : public std::list<Type>
36{
37public:
38 RWLockList();
40 virtual ~RWLockList();
41 virtual void lock_for_read();
42 virtual void lock_for_write();
43 virtual bool try_lock_for_read();
44 virtual bool try_lock_for_write();
45 virtual void unlock();
47
48 void push_back_locked(const Type &x);
49 void push_front_locked(const Type &x);
50 void remove_locked(const Type &x);
51
53 RWLockList<Type> &operator=(const std::list<Type> &l);
54
55private:
57};
58
59/** @class RWLockList <core/utils/rwlock_list.h>
60 * List with a read/write lock.
61 * This class provides a list that has an intrinsic lock. The lock can be applied
62 * with the regular locking methods.
63 *
64 * @see ReadWriteLock
65 * @ingroup FCL
66 * @author Tim Niemueller
67 */
68
69/** Constructor. */
70template <typename Type>
72{
73}
74
75/** Copy constructor.
76 * @param ll RWLockList to copy
77 */
78template <typename Type>
80: std::list<Type>::list(ll), rwlock_(new ReadWriteLock())
81{
82}
83
84/** Destructor. */
85template <typename Type>
87{
88}
89
90/** Lock list for reading. */
91template <typename Type>
92void
94{
95 rwlock_->lock_for_read();
96}
97
98/** Lock list for writing. */
99template <typename Type>
100void
102{
103 rwlock_->lock_for_write();
104}
105
106/** Try to lock list for reading.
107 * @return true, if the lock has been aquired, false otherwise.
108 */
109template <typename Type>
110bool
112{
113 return rwlock_->try_lock_for_read();
114}
115
116/** Try to lock list for writing.
117 * @return true, if the lock has been aquired, false otherwise.
118 */
119template <typename Type>
120bool
122{
123 return rwlock_->try_lock_for_write();
124}
125
126/** Unlock list. */
127template <typename Type>
128void
130{
131 return rwlock_->unlock();
132}
133
134/** Push element to list at back with lock protection.
135 * @param x element to add
136 */
137template <typename Type>
138void
140{
141 rwlock_->lock_for_write();
142 std::list<Type>::push_back(x);
143 rwlock_->unlock();
144}
145
146/** Push element to list at front with lock protection.
147 * @param x element to add
148 */
149template <typename Type>
150void
152{
153 rwlock_->lock_for_write();
154 std::list<Type>::push_front(x);
155 rwlock_->unlock();
156}
157
158/** Remove element from list with lock protection.
159 * @param x element to remove
160 */
161template <typename Type>
162void
164{
165 rwlock_->lock_for_write();
166 std::list<Type>::remove(x);
167 rwlock_->unlock();
168}
169
170/** Get access to the internal read/write lock
171 * @return internal rwlock
172 */
173template <typename Type>
176{
177 return rwlock_;
178}
179
180/** Copy values from another RWLockList.
181 * Copies the values one by one. Both instances are locked during the copying and
182 * this instance is cleared before copying.
183 * @param ll list to copy
184 * @return reference to this instance
185 */
186template <typename Type>
189{
190 rwlock_->lock_for_write();
191 ll.lock_for_read();
192 this->clear();
194 for (i = ll.begin(); i != ll.end(); ++i) {
195 this->push_back(*i);
196 }
197 ll.unlock();
198 rwlock_->unlock();
199
200 return *this;
201}
202
203/** Copy values from a standard list.
204 * Copies the values one by one. This instance is locked during the copying and
205 * cleared.
206 * @param l list to copy
207 * @return reference to this instance
208 */
209template <typename Type>
211RWLockList<Type>::operator=(const std::list<Type> &l)
212{
213 rwlock_->lock_for_write();
214 this->clear();
215 typename std::list<Type>::const_iterator i;
216 for (i = l.begin(); i != l.end(); ++i) {
217 this->push_back(*i);
218 }
219 rwlock_->unlock();
220
221 return *this;
222}
223
224} // end namespace fawkes
225
226#endif
List with a read/write lock.
Definition: rwlock_list.h:36
RWLockList< Type > & operator=(const RWLockList< Type > &ll)
Copy values from another RWLockList.
Definition: rwlock_list.h:188
RWLockList()
Constructor.
Definition: rwlock_list.h:71
virtual bool try_lock_for_read()
Try to lock list for reading.
Definition: rwlock_list.h:111
void remove_locked(const Type &x)
Remove element from list with lock protection.
Definition: rwlock_list.h:163
RefPtr< ReadWriteLock > rwlock() const
Get access to the internal read/write lock.
Definition: rwlock_list.h:175
void push_back_locked(const Type &x)
Push element to list at back with lock protection.
Definition: rwlock_list.h:139
virtual bool try_lock_for_write()
Try to lock list for writing.
Definition: rwlock_list.h:121
virtual void lock_for_write()
Lock list for writing.
Definition: rwlock_list.h:101
void push_front_locked(const Type &x)
Push element to list at front with lock protection.
Definition: rwlock_list.h:151
virtual ~RWLockList()
Destructor.
Definition: rwlock_list.h:86
virtual void lock_for_read()
Lock list for reading.
Definition: rwlock_list.h:93
virtual void unlock()
Unlock list.
Definition: rwlock_list.h:129
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.