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