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 
32 namespace fawkes {
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 
55 private:
56  RefPtr<ReadWriteLock> rwlock_;
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. */
70 template <typename Type>
72 {
73 }
74 
75 /** Copy constructor.
76  * @param ll RWLockList to copy
77  */
78 template <typename Type>
80 : std::list<Type>::list(ll), rwlock_(new ReadWriteLock())
81 {
82 }
83 
84 /** Destructor. */
85 template <typename Type>
87 {
88 }
89 
90 /** Lock list for reading. */
91 template <typename Type>
92 void
94 {
95  rwlock_->lock_for_read();
96 }
97 
98 /** Lock list for writing. */
99 template <typename Type>
100 void
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  */
109 template <typename Type>
110 bool
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  */
119 template <typename Type>
120 bool
122 {
123  return rwlock_->try_lock_for_write();
124 }
125 
126 /** Unlock list. */
127 template <typename Type>
128 void
130 {
131  return rwlock_->unlock();
132 }
133 
134 /** Push element to list at back with lock protection.
135  * @param x element to add
136  */
137 template <typename Type>
138 void
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  */
149 template <typename Type>
150 void
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  */
161 template <typename Type>
162 void
163 RWLockList<Type>::remove_locked(const Type &x)
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  */
173 template <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  */
186 template <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  */
209 template <typename Type>
210 RWLockList<Type> &
211 RWLockList<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
fawkes::RWLockList::lock_for_read
virtual void lock_for_read()
Lock list for reading.
Definition: rwlock_list.h:99
fawkes::RWLockList::RWLockList
RWLockList()
Constructor.
Definition: rwlock_list.h:77
fawkes::RWLockList::~RWLockList
virtual ~RWLockList()
Destructor.
Definition: rwlock_list.h:92
fawkes::RefPtr
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:57
fawkes::RWLockList::lock_for_write
virtual void lock_for_write()
Lock list for writing.
Definition: rwlock_list.h:107
fawkes::RWLockList::rwlock
RefPtr< ReadWriteLock > rwlock() const
Get access to the internal read/write lock.
Definition: rwlock_list.h:181
fawkes::RWLockList::push_front_locked
void push_front_locked(const Type &x)
Push element to list at front with lock protection.
Definition: rwlock_list.h:157
fawkes::ReadWriteLock
Definition: read_write_lock.h:37
fawkes::RWLockList::operator=
RWLockList< Type > & operator=(const RWLockList< Type > &ll)
Copy values from another RWLockList.
Definition: rwlock_list.h:194
fawkes
fawkes::RWLockList::push_back_locked
void push_back_locked(const Type &x)
Push element to list at back with lock protection.
Definition: rwlock_list.h:145
fawkes::RWLockList::remove_locked
void remove_locked(const Type &x)
Remove element from list with lock protection.
Definition: rwlock_list.h:169
fawkes::RWLockList
Definition: rwlock_list.h:41
fawkes::RWLockList::try_lock_for_read
virtual bool try_lock_for_read()
Try to lock list for reading.
Definition: rwlock_list.h:117
fawkes::RWLockList::unlock
virtual void unlock()
Unlock list.
Definition: rwlock_list.h:135
fawkes::RWLockList::try_lock_for_write
virtual bool try_lock_for_write()
Try to lock list for writing.
Definition: rwlock_list.h:127