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
30#include <list>
31
32namespace fawkes {
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 */
43template <typename Type>
44class LockList : public std::list<Type>
45{
46public:
47 /** Constructor. */
49
50 /** Copy constructor.
51 * @param ll LockList to copy
52 */
54
55 /** Destructor. */
56 virtual ~LockList()
57 {
58 }
59
60 /** Lock list. */
61 virtual void lock() const;
62
63 /** Try to lock list.
64 * @return true, if the lock has been aquired, false otherwise.
65 */
66 virtual bool try_lock() const;
67
68 /** Unlock list. */
69 virtual void unlock() const;
70
71 /** Get access to the internal mutex.
72 * Can be used with MutexLocker.
73 * @return internal mutex
74 */
76
77 /** Push element to list at back with lock protection.
78 * @param x element to add
79 */
80 void push_back_locked(const Type &x);
81
82 /** Push element to list at front with lock protection.
83 * @param x element to add
84 */
85 void push_front_locked(const Type &x);
86
87 /** Remove element from list with lock protection.
88 * @param x element to remove
89 */
90 void remove_locked(const Type &x);
91
92 /** Copy values from another LockList.
93 * Copies the values one by one. Both instances are locked during the copying and
94 * this instance is cleared before copying.
95 * @param ll list to copy
96 * @return reference to this instance
97 */
99
100 /** Copy values from a standard list.
101 * Copies the values one by one. This instance is locked during the copying and
102 * cleared.
103 * @param l list to copy
104 * @return reference to this instance
105 */
106 LockList<Type> &operator=(const std::list<Type> &l);
107
108private:
109 mutable RefPtr<Mutex> mutex_;
110};
111
112template <typename Type>
114{
115}
116
117template <typename Type>
118LockList<Type>::LockList(const LockList<Type> &ll) : std::list<Type>::list(ll), mutex_(new Mutex())
119{
120}
121
122template <typename Type>
123void
125{
126 mutex_->lock();
127}
128
129template <typename Type>
130bool
132{
133 return mutex_->try_lock();
134}
135
136template <typename Type>
137void
139{
140 return mutex_->unlock();
141}
142
143template <typename Type>
144void
146{
147 mutex_->lock();
148 std::list<Type>::push_back(x);
149 mutex_->unlock();
150}
151
152template <typename Type>
153void
155{
156 mutex_->lock();
157 std::list<Type>::push_front(x);
158 mutex_->unlock();
159}
160
161template <typename Type>
162void
164{
165 mutex_->lock();
166 std::list<Type>::remove(x);
167 mutex_->unlock();
168}
169
170template <typename Type>
173{
174 return mutex_;
175}
176
177template <typename Type>
180{
181 mutex_->lock();
182 ll.lock();
183 this->clear();
185 for (i = ll.begin(); i != ll.end(); ++i) {
186 this->push_back(*i);
187 }
188 ll.unlock();
189 mutex_->unlock();
190
191 return *this;
192}
193
194template <typename Type>
196LockList<Type>::operator=(const std::list<Type> &l)
197{
198 mutex_->lock();
199 this->clear();
200 typename std::list<Type>::const_iterator i;
201 for (i = l.begin(); i != l.end(); ++i) {
202 this->push_back(*i);
203 }
204 mutex_->unlock();
205
206 return *this;
207}
208
209} // end namespace fawkes
210
211#endif
List with a lock.
Definition: lock_list.h:45
void push_front_locked(const Type &x)
Push element to list at front with lock protection.
Definition: lock_list.h:154
LockList< Type > & operator=(const LockList< Type > &ll)
Copy values from another LockList.
Definition: lock_list.h:179
virtual void unlock() const
Unlock list.
Definition: lock_list.h:138
void push_back_locked(const Type &x)
Push element to list at back with lock protection.
Definition: lock_list.h:145
virtual void lock() const
Lock list.
Definition: lock_list.h:124
RefPtr< Mutex > mutex() const
Get access to the internal mutex.
Definition: lock_list.h:172
LockList()
Constructor.
Definition: lock_list.h:113
void remove_locked(const Type &x)
Remove element from list with lock protection.
Definition: lock_list.h:163
virtual ~LockList()
Destructor.
Definition: lock_list.h:56
virtual bool try_lock() const
Try to lock list.
Definition: lock_list.h:131
LockList< Type > & operator=(const std::list< Type > &l)
Copy values from a standard list.
Definition: lock_list.h:196
LockList(const LockList< Type > &ll)
Copy constructor.
Definition: lock_list.h:118
Mutex mutual exclusion lock.
Definition: mutex.h:33
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:50
Fawkes library namespace.