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 #include <set>
31 
32 namespace fawkes {
33 
34 template <typename KeyType,
35  typename LessKey = std::less<KeyType> >
36 class LockSet : public std::set<KeyType, LessKey>
37 {
38  public:
39  LockSet();
41  virtual ~LockSet();
42 
43  void lock() const;
44  bool try_lock() const;
45  void unlock() const;
46  RefPtr<Mutex> mutex() 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 
57  private:
58  mutable RefPtr<Mutex> __mutex;
59 
60 };
61 
62 
63 /** @class LockSet <core/utils/lock_set.h>
64  * Set with a lock.
65  * This class provides a set that has an intrinsic lock. The lock can be applied
66  * with the regular locking methods.
67  *
68  * @see Mutex
69  * @ingroup FCL
70  * @author Tim Niemueller
71  */
72 
73 
74 /** Constructor. */
75 template <typename KeyType, typename LessKey>
77  : __mutex(new Mutex())
78 {}
79 
80 
81 /** Copy constructor.
82  * @param lm LockSet to copy
83  */
84 template <typename KeyType, typename LessKey>
86  : std::set<KeyType, LessKey>::set(lm),
87  __mutex(new Mutex())
88 {}
89 
90 
91 /** Destructor. */
92 template <typename KeyType, typename LessKey>
94 {}
95 
96 
97 /** Lock list. */
98 template <typename KeyType, typename LessKey>
99 void
101 {
102  __mutex->lock();
103 }
104 
105 
106 /** Try to lock list.
107  * @return true, if the lock has been aquired, false otherwise.
108  */
109 template <typename KeyType, typename LessKey>
110 bool
112 {
113  return __mutex->try_lock();
114 }
115 
116 
117 /** Unlock list. */
118 template <typename KeyType, typename LessKey>
119 void
121 {
122  return __mutex->unlock();
123 }
124 
125 
126 /** Insert item with lock.
127  * The set is automatically locked and unlocked during the removal.
128  * @param key key of the value to insert
129  * @return iterator to inserted item
130  */
131 template <typename KeyType, typename LessKey>
132 std::pair<typename LockSet<KeyType, LessKey>::iterator, bool>
134 {
135  __mutex->lock();
136  std::pair<iterator, bool> ret =
137  std::set<KeyType, LessKey>::insert(key);
138  __mutex->unlock();
139  return ret;
140 }
141 
142 /** Remove item with lock.
143  * The set is automatically locked and unlocked during the removal.
144  * @param key key of the value to erase
145  */
146 template <typename KeyType, typename LessKey>
147 void
149 {
150  __mutex->lock();
151  std::set<KeyType, LessKey>::erase(key);
152  __mutex->unlock();
153 }
154 
155 
156 /** Get access to the internal mutex.
157  * Can be used with MutexLocker.
158  * @return internal mutex
159  */
160 template <typename KeyType, typename LessKey>
163 {
164  return __mutex;
165 }
166 
167 
168 /** Copy values from another LockSet.
169  * Copies the values one by one. Both instances are locked during the copying and
170  * this instance is cleared before copying.
171  * @param ll lock set to copy
172  * @return reference to this instance
173  */
174 template <typename KeyType, typename LessKey>
177 {
178  __mutex->lock();
179  ll.lock();
180  this->clear();
182  for (i = ll.begin(); i != ll.end(); ++i) {
183  this->insert(*i);
184  }
185  ll.unlock();
186  __mutex->unlock();
187 
188  return *this;
189 }
190 
191 
192 /** Copy values from a standard set.
193  * Copies the values one by one. This instance is locked during the copying and
194  * cleared.
195  * @param l set to copy
196  * @return reference to this instance
197  */
198 template <typename KeyType, typename LessKey>
200 LockSet<KeyType, LessKey>::operator=(const std::set<KeyType, LessKey> &l)
201 {
202  __mutex->lock();
203  this->clear();
204  typename std::set<KeyType, LessKey>::const_iterator i;
205  for (i = l.begin(); i != l.end(); ++i) {
206  this->insert(*i);
207  }
208  __mutex->unlock();
209 
210  return *this;
211 }
212 
213 
214 } // end namespace fawkes
215 
216 #endif
std::pair< iterator, bool > insert_locked(const KeyType &key)
Insert item with lock.
Definition: lock_set.h:133
Fawkes library namespace.
void lock() const
Lock list.
Definition: lock_set.h:100
STL namespace.
LockSet< KeyType, LessKey > & operator=(const LockSet< KeyType, LessKey > &ll)
Copy values from another LockSet.
Definition: lock_set.h:176
RefPtr< Mutex > mutex() const
Get access to the internal mutex.
Definition: lock_set.h:162
bool try_lock() const
Try to lock list.
Definition: lock_set.h:111
Set with a lock.
Definition: lock_set.h:36
void erase_locked(const KeyType &key)
Remove item with lock.
Definition: lock_set.h:148
void unlock() const
Unlock list.
Definition: lock_set.h:120
virtual ~LockSet()
Destructor.
Definition: lock_set.h:93
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:49
std::set< KeyType, LessKey >::iterator iterator
Iterator.
Definition: lock_set.h:49
Mutex mutual exclusion lock.
Definition: mutex.h:32
LockSet()
Constructor.
Definition: lock_set.h:76