Fawkes API  Fawkes Development Version
lock_hashset.h
1 
2 /***************************************************************************
3  * lock_hashset.h - Lockable hash set
4  *
5  * Created: Sat May 12 13:06:31 2007
6  * Copyright 2006-2007 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_HASHSET_H_
25 #define _CORE_UTILS_LOCK_HASHSET_H_
26 
27 #include <core/threading/mutex.h>
28 #include <core/utils/refptr.h>
29 
30 #include <cstdlib>
31 #if __cplusplus >= 201103L || defined(_LIBCPP_VERSION)
32 # include <functional>
33 # include <unordered_set>
34 #elif __GLIBCXX__ > 20080305
35 # include <tr1/unordered_set>
36 #else
37 # include <ext/hash_set>
38 #endif
39 
40 namespace fawkes {
41 
42 template <class KeyType,
43 #if __cplusplus >= 201103L || defined(_LIBCPP_VERSION)
44  class HashFunction = std::hash<KeyType>,
45  class EqualKey = std::equal_to<KeyType>>
46 class LockHashSet : public std::unordered_set<KeyType, HashFunction, EqualKey>
47 #elif __GLIBCXX__ > 20080305
48  class HashFunction = std::tr1::hash<KeyType>,
49  class EqualKey = std::equal_to<KeyType>>
50 class LockHashSet : public std::tr1::unordered_set<KeyType, HashFunction, EqualKey>
51 #else
52  class HashFunction = gnu_cxx_::hash<KeyType>,
53  class EqualKey = std::equal_to<KeyType>>
54 class LockHashSet : public gnu_cxx_::hash_set<KeyType, HashFunction, EqualKey>
55 #endif
56 {
57 public:
60  virtual ~LockHashSet();
61 
62  void lock() const;
63  bool try_lock() const;
64  void unlock() const;
66 
67  void insert_locked(const KeyType &x);
68 
71 
72 private:
73  mutable RefPtr<Mutex> mutex_;
74 };
75 
76 /** @class LockHashSet core/utils/lock_hashset.h
77  * Hash set with a lock.
78  * This class provides a hash set that has an intrinsic lock. The lock can be applied
79  * with the regular locking methods.
80  *
81  * @see Mutex
82  * @ingroup FCL
83  * @author Tim Niemueller
84  */
85 
86 /** Constructor. */
87 template <class KeyType, class HashFunction, class EqualKey>
89 {
90 }
91 
92 /** Copy constructor.
93  * @param lh LockHashSet to copy
94  */
95 template <class KeyType, class HashFunction, class EqualKey>
98 #if __cplusplus >= 201103L || defined(_LIBCPP_VERSION)
99 : std::unordered_set<KeyType, HashFunction, EqualKey>::unordered_set(lh),
100 #elif __GLIBCXX__ > 20080305
101 : std::tr1::unordered_set<KeyType, HashFunction, EqualKey>::unordered_set(lh),
102 #else
103 : gnu_cxx_::hash_set<KeyType, HashFunction, EqualKey>::hash_set(lh),
104 #endif
105  mutex_(new Mutex())
106 {
107 }
108 
109 /** Destructor. */
110 template <class KeyType, class HashFunction, class EqualKey>
112 {
113 }
114 
115 /** Lock set. */
116 template <class KeyType, class HashFunction, class EqualKey>
117 void
119 {
120  mutex_->lock();
121 }
122 
123 /** Try to lock set.
124  * @return true, if the lock has been aquired, false otherwise.
125  */
126 template <class KeyType, class HashFunction, class EqualKey>
127 bool
129 {
130  return mutex_->try_lock();
131 }
132 
133 /** Unlock set. */
134 template <class KeyType, class HashFunction, class EqualKey>
135 void
137 {
138  return mutex_->unlock();
139 }
140 
141 /** Insert element to hash set with lock protection.
142  * @param x element to add
143  */
144 template <class KeyType, class HashFunction, class EqualKey>
145 void
147 {
148  mutex_->lock();
149  insert(x);
150  mutex_->unlock();
151 }
152 
153 /** Get access to the internal mutex.
154  * Can be used with MutexLocker.
155  * @return internal mutex
156  */
157 template <typename KeyType, class HashFunction, class EqualKey>
160 {
161  return mutex_;
162 }
163 
164 /** Copy values from another LockHashSet.
165  * Copies the values one by one. Both instances are locked during the copying and
166  * this instance is cleared before copying.
167  * @param ll lock hash set to copy
168  * @return reference to this instance
169  */
170 template <typename KeyType, class HashFunction, class EqualKey>
174 {
175  mutex_->lock();
176  ll.lock();
177  this->clear();
179  for (i = ll.begin(); i != ll.end(); ++i) {
180  this->insert(*i);
181  }
182  ll.unlock();
183  mutex_->unlock();
184 
185  return *this;
186 }
187 
188 } // end namespace fawkes
189 
190 #endif
fawkes::LockHashSet::LockHashSet
LockHashSet()
Constructor.
Definition: lock_hashset.h:88
fawkes::Mutex
Mutex mutual exclusion lock.
Definition: mutex.h:33
fawkes::LockHashSet::operator=
LockHashSet< KeyType, HashFunction, EqualKey > & operator=(const LockHashSet< KeyType, HashFunction, EqualKey > &ll)
Copy values from another LockHashSet.
Definition: lock_hashset.h:172
fawkes::RefPtr
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:50
fawkes::LockHashSet::unlock
void unlock() const
Unlock set.
Definition: lock_hashset.h:136
fawkes::LockHashSet::try_lock
bool try_lock() const
Try to lock set.
Definition: lock_hashset.h:128
fawkes::LockHashSet::lock
void lock() const
Lock set.
Definition: lock_hashset.h:118
fawkes::LockHashSet::~LockHashSet
virtual ~LockHashSet()
Destructor.
Definition: lock_hashset.h:111
fawkes::LockHashSet::LockHashSet
LockHashSet(const LockHashSet< KeyType, HashFunction, EqualKey > &lh)
Copy constructor.
Definition: lock_hashset.h:96
fawkes
Fawkes library namespace.
fawkes::LockHashSet::mutex
RefPtr< Mutex > mutex() const
Get access to the internal mutex.
Definition: lock_hashset.h:159
fawkes::LockHashSet
Hash set with a lock.
Definition: lock_hashset.h:56
fawkes::LockHashSet::insert_locked
void insert_locked(const KeyType &x)
Insert element to hash set with lock protection.
Definition: lock_hashset.h:146