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