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