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
40namespace fawkes {
41
42template <class KeyType,
43#if __cplusplus >= 201103L || defined(_LIBCPP_VERSION)
44 class HashFunction = std::hash<KeyType>,
45 class EqualKey = std::equal_to<KeyType>>
46class 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>>
50class 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>>
54class LockHashSet : public gnu_cxx_::hash_set<KeyType, HashFunction, EqualKey>
55#endif
56{
57public:
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
72private:
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. */
87template <class KeyType, class HashFunction, class EqualKey>
89{
90}
91
92/** Copy constructor.
93 * @param lh LockHashSet to copy
94 */
95template <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. */
110template <class KeyType, class HashFunction, class EqualKey>
112{
113}
114
115/** Lock set. */
116template <class KeyType, class HashFunction, class EqualKey>
117void
119{
120 mutex_->lock();
121}
122
123/** Try to lock set.
124 * @return true, if the lock has been aquired, false otherwise.
125 */
126template <class KeyType, class HashFunction, class EqualKey>
127bool
129{
130 return mutex_->try_lock();
131}
132
133/** Unlock set. */
134template <class KeyType, class HashFunction, class EqualKey>
135void
137{
138 return mutex_->unlock();
139}
140
141/** Insert element to hash set with lock protection.
142 * @param x element to add
143 */
144template <class KeyType, class HashFunction, class EqualKey>
145void
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 */
157template <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 */
170template <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
Hash set with a lock.
Definition: lock_hashset.h:56
LockHashSet()
Constructor.
Definition: lock_hashset.h:88
void insert_locked(const KeyType &x)
Insert element to hash set with lock protection.
Definition: lock_hashset.h:146
virtual ~LockHashSet()
Destructor.
Definition: lock_hashset.h:111
void lock() const
Lock set.
Definition: lock_hashset.h:118
RefPtr< Mutex > mutex() const
Get access to the internal mutex.
Definition: lock_hashset.h:159
void unlock() const
Unlock set.
Definition: lock_hashset.h:136
LockHashSet(const LockHashSet< KeyType, HashFunction, EqualKey > &lh)
Copy constructor.
Definition: lock_hashset.h:96
bool try_lock() const
Try to lock set.
Definition: lock_hashset.h:128
LockHashSet< KeyType, HashFunction, EqualKey > & operator=(const LockHashSet< KeyType, HashFunction, EqualKey > &ll)
Copy values from another LockHashSet.
Definition: lock_hashset.h:172
Mutex mutual exclusion lock.
Definition: mutex.h:33
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:50
Fawkes library namespace.