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