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 
33 namespace fawkes {
34 
35 template <typename KeyType, typename LessKey = std::less<KeyType>>
36 class LockSet : public std::set<KeyType, LessKey>
37 {
38 public:
39  LockSet();
40  LockSet(const LockSet<KeyType, LessKey> &lm);
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 /** @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. */
72 template <typename KeyType, typename LessKey>
74 {
75 }
76 
77 /** Copy constructor.
78  * @param lm LockSet to copy
79  */
80 template <typename KeyType, typename LessKey>
82 : std::set<KeyType, LessKey>::set(lm), mutex_(new Mutex())
83 {
84 }
85 
86 /** Destructor. */
87 template <typename KeyType, typename LessKey>
89 {
90 }
91 
92 /** Lock list. */
93 template <typename KeyType, typename LessKey>
94 void
96 {
97  mutex_->lock();
98 }
99 
100 /** Try to lock list.
101  * @return true, if the lock has been aquired, false otherwise.
102  */
103 template <typename KeyType, typename LessKey>
104 bool
106 {
107  return mutex_->try_lock();
108 }
109 
110 /** Unlock list. */
111 template <typename KeyType, typename LessKey>
112 void
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  */
123 template <typename KeyType, typename LessKey>
124 std::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  */
137 template <typename KeyType, typename LessKey>
138 void
139 LockSet<KeyType, LessKey>::erase_locked(const KeyType &key)
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  */
150 template <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  */
163 template <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  */
186 template <typename KeyType, typename LessKey>
187 LockSet<KeyType, LessKey> &
188 LockSet<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
fawkes::LockSet::LockSet
LockSet()
Constructor.
Definition: lock_set.h:78
fawkes::Mutex
Definition: mutex.h:36
fawkes::LockSet::mutex
RefPtr< Mutex > mutex() const
Get access to the internal mutex.
Definition: lock_set.h:157
fawkes::RefPtr
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:55
fawkes::LockSet
Definition: lock_set.h:41
fawkes::LockSet::try_lock
bool try_lock() const
Try to lock list.
Definition: lock_set.h:110
fawkes::LockSet::insert_locked
std::pair< iterator, bool > insert_locked(const KeyType &key)
Insert item with lock.
Definition: lock_set.h:130
fawkes::LockSet::operator=
LockSet< KeyType, LessKey > & operator=(const LockSet< KeyType, LessKey > &ll)
Copy values from another LockSet.
Definition: lock_set.h:170
fawkes::LockSet::lock
void lock() const
Lock list.
Definition: lock_set.h:100
fawkes
fawkes::LockSet::erase_locked
void erase_locked(const KeyType &key)
Remove item with lock.
Definition: lock_set.h:144
fawkes::LockSet::unlock
void unlock() const
Unlock list.
Definition: lock_set.h:118
fawkes::LockSet::iterator
std::set< KeyType, LessKey >::iterator iterator
Iterator.
Definition: lock_set.h:60
fawkes::LockSet::~LockSet
virtual ~LockSet()
Destructor.
Definition: lock_set.h:93