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 #include <list>
30 
31 namespace fawkes {
32 
33 
34 template <typename Type>
35 class RWLockList : public std::list<Type>
36 {
37  public:
38  RWLockList();
39  RWLockList(const RWLockList<Type> &ll);
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  private:
55  RefPtr<ReadWriteLock> __rwlock;
56 
57 };
58 
59 
60 /** @class RWLockList <core/utils/rwlock_list.h>
61  * List with a read/write lock.
62  * This class provides a list 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 
71 /** Constructor. */
72 template <typename Type>
74  : __rwlock(new ReadWriteLock())
75 {}
76 
77 
78 /** Copy constructor.
79  * @param ll RWLockList to copy
80  */
81 template <typename Type>
83  : std::list<Type>::list(ll), __rwlock(new ReadWriteLock())
84 {}
85 
86 
87 /** Destructor. */
88 template <typename Type>
90 {}
91 
92 
93 /** Lock list for reading. */
94 template <typename Type>
95 void
97 {
98  __rwlock->lock_for_read();
99 }
100 
101 
102 /** Lock list for writing. */
103 template <typename Type>
104 void
106 {
107  __rwlock->lock_for_write();
108 }
109 
110 
111 /** Try to lock list for reading.
112  * @return true, if the lock has been aquired, false otherwise.
113  */
114 template <typename Type>
115 bool
117 {
118  return __rwlock->try_lock_for_read();
119 }
120 
121 
122 /** Try to lock list for writing.
123  * @return true, if the lock has been aquired, false otherwise.
124  */
125 template <typename Type>
126 bool
128 {
129  return __rwlock->try_lock_for_write();
130 }
131 
132 
133 /** Unlock list. */
134 template <typename Type>
135 void
137 {
138  return __rwlock->unlock();
139 }
140 
141 
142 /** Push element to list at back with lock protection.
143  * @param x element to add
144  */
145 template <typename Type>
146 void
148 {
149  __rwlock->lock_for_write();
150  std::list<Type>::push_back(x);
151  __rwlock->unlock();
152 }
153 
154 
155 /** Push element to list at front with lock protection.
156  * @param x element to add
157  */
158 template <typename Type>
159 void
161 {
162  __rwlock->lock_for_write();
163  std::list<Type>::push_front(x);
164  __rwlock->unlock();
165 }
166 
167 
168 /** Remove element from list with lock protection.
169  * @param x element to remove
170  */
171 template <typename Type>
172 void
174 {
175  __rwlock->lock_for_write();
176  std::list<Type>::remove(x);
177  __rwlock->unlock();
178 }
179 
180 
181 /** Get access to the internal read/write lock
182  * @return internal rwlock
183  */
184 template <typename Type>
187 {
188  return __rwlock;
189 }
190 
191 
192 /** Copy values from another RWLockList.
193  * Copies the values one by one. Both instances are locked during the copying and
194  * this instance is cleared before copying.
195  * @param ll list to copy
196  * @return reference to this instance
197  */
198 template <typename Type>
201 {
202  __rwlock->lock_for_write();
203  ll.lock_for_read();
204  this->clear();
206  for (i = ll.begin(); i != ll.end(); ++i) {
207  this->push_back(*i);
208  }
209  ll.unlock();
210  __rwlock->unlock();
211 
212  return *this;
213 }
214 
215 
216 /** Copy values from a standard list.
217  * Copies the values one by one. This instance is locked during the copying and
218  * cleared.
219  * @param l list to copy
220  * @return reference to this instance
221  */
222 template <typename Type>
224 RWLockList<Type>::operator=(const std::list<Type> &l)
225 {
226  __rwlock->lock_for_write();
227  this->clear();
228  typename std::list<Type>::const_iterator i;
229  for (i = l.begin(); i != l.end(); ++i) {
230  this->push_back(*i);
231  }
232  __rwlock->unlock();
233 
234  return *this;
235 }
236 
237 } // end namespace fawkes
238 
239 #endif
RWLockList< Type > & operator=(const RWLockList< Type > &ll)
Copy values from another RWLockList.
Definition: rwlock_list.h:200
void push_back_locked(const Type &x)
Push element to list at back with lock protection.
Definition: rwlock_list.h:147
Fawkes library namespace.
void remove_locked(const Type &x)
Remove element from list with lock protection.
Definition: rwlock_list.h:173
STL namespace.
virtual bool try_lock_for_read()
Try to lock list for reading.
Definition: rwlock_list.h:116
virtual void lock_for_read()
Lock list for reading.
Definition: rwlock_list.h:96
virtual void unlock()
Unlock list.
Definition: rwlock_list.h:136
Read/write lock to allow multiple readers but only a single writer on the resource at a time...
virtual void lock_for_write()
Lock list for writing.
Definition: rwlock_list.h:105
virtual bool try_lock_for_write()
Try to lock list for writing.
Definition: rwlock_list.h:127
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:49
RefPtr< ReadWriteLock > rwlock() const
Get access to the internal read/write lock.
Definition: rwlock_list.h:186
RWLockList()
Constructor.
Definition: rwlock_list.h:73
virtual ~RWLockList()
Destructor.
Definition: rwlock_list.h:89
void push_front_locked(const Type &x)
Push element to list at front with lock protection.
Definition: rwlock_list.h:160
List with a read/write lock.
Definition: rwlock_list.h:35