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 #include <vector>
30 
31 namespace fawkes {
32 #if 0 /* just to make Emacs auto-indent happy */
33 }
34 #endif
35 
36 template <typename Type>
37 class RWLockVector : public std::vector<Type>
38 {
39  public:
40  RWLockVector();
42  virtual ~RWLockVector();
43  virtual void lock_for_read() const;
44  virtual void lock_for_write() const;
45  virtual bool try_lock_for_read() const;
46  virtual bool try_lock_for_write() const;
47  virtual void unlock() const;
49 
50  void push_back_locked(const Type& x);
51  void pop_back_locked();
52  void erase_locked(typename std::vector<Type>::iterator pos);
53  void erase_locked(typename std::vector<Type>::iterator first,
54  typename std::vector<Type>::iterator last);
55 
57  RWLockVector<Type> & operator=(const std::vector<Type> &v);
58  private:
59  mutable RefPtr<ReadWriteLock> __rwlock;
60 
61 };
62 
63 
64 /** @class RWLockVector <core/utils/rwlock_vector.h>
65  * Vector with a lock.
66  * This class provides a vector that has an intrinsic lock. The lock can be applied
67  * with the regular locking methods.
68  *
69  * @see ReadWriteLock
70  * @ingroup FCL
71  * @author Tim Niemueller
72  */
73 
74 
75 /** Constructor. */
76 template <typename Type>
78  : __rwlock(new ReadWriteLock())
79 {}
80 
81 
82 /** Copy constructor.
83  * @param lv RWLockVector to copy
84  */
85 template <typename Type>
87  : std::vector<Type>::vector(lv), __rwlock(new ReadWriteLock())
88 {}
89 
90 
91 /** Destructor. */
92 template <typename Type>
94 {}
95 
96 
97 /** Lock vector for reading. */
98 template <typename Type>
99 void
101 {
102  __rwlock->lock_for_read();
103 }
104 
105 
106 /** Lock vector for writing. */
107 template <typename Type>
108 void
110 {
111  __rwlock->lock_for_write();
112 }
113 
114 
115 /** Try to lock vector for reading.
116  * @return true, if the lock has been aquired, false otherwise.
117  */
118 template <typename Type>
119 bool
121 {
122  return __rwlock->try_lock_for_read();
123 }
124 
125 
126 /** Try to lock vector for writing.
127  * @return true, if the lock has been aquired, false otherwise.
128  */
129 template <typename Type>
130 bool
132 {
133  return __rwlock->try_lock_for_write();
134 }
135 
136 
137 /** Unlock vector. */
138 template <typename Type>
139 void
141 {
142  return __rwlock->unlock();
143 }
144 
145 
146 /** Push element to vector at back with lock protection.
147  * @param x element to add
148  */
149 template <typename Type>
150 void
152 {
153  __rwlock->lock_for_write();
154  std::vector<Type>::push_back(x);
155  __rwlock->unlock();
156 }
157 
158 
159 /** Remove last element with lock protection. */
160 template <typename Type>
161 void
163 {
164  __rwlock->lock_for_write();
165  std::vector<Type>::pop_back();
166  __rwlock->unlock();
167 }
168 
169 
170 /** Erase given element with lock protection.
171  * @param pos iterator for the object position to remove
172  */
173 template <typename Type>
174 void
175 RWLockVector<Type>::erase_locked(typename std::vector<Type>::iterator pos)
176 {
177  __rwlock->lock_for_write();
178  std::vector<Type>::erase(pos);
179  __rwlock->unlock();
180 }
181 
182 /** Erase given element range with lock protection.
183  * @param first iterator to first element to erase
184  * @param last iterator to first element not to erase
185  */
186 template <typename Type>
187 void
188 RWLockVector<Type>::erase_locked(typename std::vector<Type>::iterator first,
189  typename std::vector<Type>::iterator last)
190 {
191  __rwlock->lock_for_write();
192  std::vector<Type>::erase(first, last);
193  __rwlock->unlock();
194 }
195 
196 
197 /** Get access to the internal read/write lock.
198  * @return internal read/write lock
199  */
200 template <typename Type>
203 {
204  return __rwlock;
205 }
206 
207 
208 /** Copy values from another RWLockVector.
209  * Copies the values one by one. Both instances are locked during the copying and
210  * this instance is cleared before copying.
211  * @param lv vector to copy
212  * @return reference to this instance
213  */
214 template <typename Type>
217 {
218  __rwlock->lock_for_write();
219  lv.lock_for_read();
220  this->clear();
222  for (i = lv.begin(); i != lv.end(); ++i) {
223  this->push_back(*i);
224  }
225  lv.unlock();
226  __rwlock->unlock();
227 
228  return *this;
229 }
230 
231 
232 /** Copy values from a standard vector.
233  * Copies the values one by one. This instance is locked during the copying and
234  * cleared.
235  * @param v vector to copy
236  * @return reference to this instance
237  */
238 template <typename Type>
240 RWLockVector<Type>::operator=(const std::vector<Type> &v)
241 {
242  __rwlock->lock_for_write();
243  this->clear();
244  typename std::vector<Type>::const_iterator i;
245  for (i = v.begin(); i != v.end(); ++i) {
246  this->push_back(*i);
247  }
248  __rwlock->unlock();
249 
250  return *this;
251 }
252 
253 } // end namespace fawkes
254 
255 #endif
virtual void lock_for_read() const
Lock vector for reading.
Fawkes library namespace.
RefPtr< ReadWriteLock > rwlock() const
Get access to the internal read/write lock.
STL namespace.
virtual void unlock() const
Unlock vector.
virtual bool try_lock_for_write() const
Try to lock vector for writing.
virtual ~RWLockVector()
Destructor.
Definition: rwlock_vector.h:93
RWLockVector< Type > & operator=(const RWLockVector< Type > &lv)
Copy values from another RWLockVector.
Read/write lock to allow multiple readers but only a single writer on the resource at a time...
virtual void lock_for_write() const
Lock vector for writing.
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:49
void erase_locked(typename std::vector< Type >::iterator pos)
Erase given element with lock protection.
void pop_back_locked()
Remove last element with lock protection.
virtual bool try_lock_for_read() const
Try to lock vector for reading.
RWLockVector()
Constructor.
Definition: rwlock_vector.h:77
void push_back_locked(const Type &x)
Push element to vector at back with lock protection.
Vector with a lock.
Definition: rwlock_vector.h:37