Fawkes API Fawkes Development Version
lock_set.h
1
2/***************************************************************************
3 * lock_set.h - Lockable set
4 *
5 * Created: Fri Apr 9 15:24:51 2010
6 * Copyright 2006-2010 Tim Niemueller [www.niemueller.de]
7 * 2010 Christoph Schwering
8 *
9 ****************************************************************************/
10
11/* This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version. A runtime exception applies to
15 * this software (see LICENSE.GPL_WRE file mentioned below for details).
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
21 *
22 * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
23 */
24
25#ifndef _CORE_UTILS_LOCK_SET_H_
26#define _CORE_UTILS_LOCK_SET_H_
27
28#include <core/threading/mutex.h>
29#include <core/utils/refptr.h>
30
31#include <set>
32
33namespace fawkes {
34
35template <typename KeyType, typename LessKey = std::less<KeyType>>
36class LockSet : public std::set<KeyType, LessKey>
37{
38public:
41 virtual ~LockSet();
42
43 void lock() const;
44 bool try_lock() const;
45 void unlock() const;
47
48 /** Iterator. */
49 typedef typename std::set<KeyType, LessKey>::iterator iterator;
50
51 std::pair<iterator, bool> insert_locked(const KeyType &key);
52 void erase_locked(const KeyType &key);
53
55 LockSet<KeyType, LessKey> &operator=(const std::set<KeyType, LessKey> &l);
56
57private:
58 mutable RefPtr<Mutex> mutex_;
59};
60
61/** @class LockSet <core/utils/lock_set.h>
62 * Set with a lock.
63 * This class provides a set that has an intrinsic lock. The lock can be applied
64 * with the regular locking methods.
65 *
66 * @see Mutex
67 * @ingroup FCL
68 * @author Tim Niemueller
69 */
70
71/** Constructor. */
72template <typename KeyType, typename LessKey>
74{
75}
76
77/** Copy constructor.
78 * @param lm LockSet to copy
79 */
80template <typename KeyType, typename LessKey>
82: std::set<KeyType, LessKey>::set(lm), mutex_(new Mutex())
83{
84}
85
86/** Destructor. */
87template <typename KeyType, typename LessKey>
89{
90}
91
92/** Lock list. */
93template <typename KeyType, typename LessKey>
94void
96{
97 mutex_->lock();
98}
99
100/** Try to lock list.
101 * @return true, if the lock has been aquired, false otherwise.
102 */
103template <typename KeyType, typename LessKey>
104bool
106{
107 return mutex_->try_lock();
108}
109
110/** Unlock list. */
111template <typename KeyType, typename LessKey>
112void
114{
115 return mutex_->unlock();
116}
117
118/** Insert item with lock.
119 * The set is automatically locked and unlocked during the removal.
120 * @param key key of the value to insert
121 * @return iterator to inserted item
122 */
123template <typename KeyType, typename LessKey>
124std::pair<typename LockSet<KeyType, LessKey>::iterator, bool>
126{
127 mutex_->lock();
128 std::pair<iterator, bool> ret = std::set<KeyType, LessKey>::insert(key);
129 mutex_->unlock();
130 return ret;
131}
132
133/** Remove item with lock.
134 * The set is automatically locked and unlocked during the removal.
135 * @param key key of the value to erase
136 */
137template <typename KeyType, typename LessKey>
138void
140{
141 mutex_->lock();
142 std::set<KeyType, LessKey>::erase(key);
143 mutex_->unlock();
144}
145
146/** Get access to the internal mutex.
147 * Can be used with MutexLocker.
148 * @return internal mutex
149 */
150template <typename KeyType, typename LessKey>
153{
154 return mutex_;
155}
156
157/** Copy values from another LockSet.
158 * Copies the values one by one. Both instances are locked during the copying and
159 * this instance is cleared before copying.
160 * @param ll lock set to copy
161 * @return reference to this instance
162 */
163template <typename KeyType, typename LessKey>
166{
167 mutex_->lock();
168 ll.lock();
169 this->clear();
171 for (i = ll.begin(); i != ll.end(); ++i) {
172 this->insert(*i);
173 }
174 ll.unlock();
175 mutex_->unlock();
176
177 return *this;
178}
179
180/** Copy values from a standard set.
181 * Copies the values one by one. This instance is locked during the copying and
182 * cleared.
183 * @param l set to copy
184 * @return reference to this instance
185 */
186template <typename KeyType, typename LessKey>
188LockSet<KeyType, LessKey>::operator=(const std::set<KeyType, LessKey> &l)
189{
190 mutex_->lock();
191 this->clear();
192 typename std::set<KeyType, LessKey>::const_iterator i;
193 for (i = l.begin(); i != l.end(); ++i) {
194 this->insert(*i);
195 }
196 mutex_->unlock();
197
198 return *this;
199}
200
201} // end namespace fawkes
202
203#endif
Set with a lock.
Definition: lock_set.h:37
void lock() const
Lock list.
Definition: lock_set.h:95
void unlock() const
Unlock list.
Definition: lock_set.h:113
RefPtr< Mutex > mutex() const
Get access to the internal mutex.
Definition: lock_set.h:152
std::set< KeyType, LessKey >::iterator iterator
Iterator.
Definition: lock_set.h:49
LockSet(const LockSet< KeyType, LessKey > &lm)
Copy constructor.
Definition: lock_set.h:81
LockSet()
Constructor.
Definition: lock_set.h:73
bool try_lock() const
Try to lock list.
Definition: lock_set.h:105
LockSet< KeyType, LessKey > & operator=(const LockSet< KeyType, LessKey > &ll)
Copy values from another LockSet.
Definition: lock_set.h:165
std::pair< iterator, bool > insert_locked(const KeyType &key)
Insert item with lock.
Definition: lock_set.h:125
virtual ~LockSet()
Destructor.
Definition: lock_set.h:88
LockSet< KeyType, LessKey > & operator=(const std::set< KeyType, LessKey > &l)
Copy values from a standard set.
Definition: lock_set.h:188
void erase_locked(const KeyType &key)
Remove item with lock.
Definition: lock_set.h:139
Mutex mutual exclusion lock.
Definition: mutex.h:33
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:50
Fawkes library namespace.