00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __CORE_UTILS_LOCK_HASHSET_H_
00025 #define __CORE_UTILS_LOCK_HASHSET_H_
00026
00027 #include <core/threading/mutex.h>
00028 #include <core/utils/refptr.h>
00029
00030 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)
00031 # include <tr1/unordered_set>
00032 #else
00033 # include <ext/hash_set>
00034 #endif
00035
00036 namespace fawkes {
00037
00038
00039 template <class KeyType,
00040 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)
00041 class HashFunction = std::tr1::hash<KeyType>,
00042 class EqualKey = std::equal_to<KeyType> >
00043 class LockHashSet : public std::tr1::unordered_set<KeyType, HashFunction, EqualKey>
00044 #else
00045 class HashFunction = __gnu_cxx::hash<KeyType>,
00046 class EqualKey = std::equal_to<KeyType> >
00047 class LockHashSet : public __gnu_cxx::hash_set<KeyType, HashFunction, EqualKey>
00048 #endif
00049 {
00050 public:
00051 LockHashSet();
00052 LockHashSet(const LockHashSet<KeyType, HashFunction, EqualKey> &lh);
00053 virtual ~LockHashSet();
00054
00055 void lock() const;
00056 bool try_lock() const;
00057 void unlock() const;
00058 RefPtr<Mutex> mutex() const;
00059
00060 void insert_locked(const KeyType& x);
00061
00062 LockHashSet<KeyType, HashFunction, EqualKey> &
00063 operator=(const LockHashSet<KeyType, HashFunction, EqualKey> &ll);
00064
00065 private:
00066 mutable RefPtr<Mutex> __mutex;
00067
00068 };
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 template <class KeyType, class HashFunction, class EqualKey>
00084 LockHashSet<KeyType, HashFunction, EqualKey>::LockHashSet()
00085 : __mutex(new Mutex())
00086 {}
00087
00088
00089
00090
00091
00092 template <class KeyType, class HashFunction, class EqualKey>
00093 LockHashSet<KeyType, HashFunction, EqualKey>::LockHashSet(const LockHashSet<KeyType, HashFunction, EqualKey> &lh)
00094 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)
00095 : std::tr1::unordered_set<KeyType, HashFunction, EqualKey>::unordered_set(lh),
00096 #else
00097 : __gnu_cxx::hash_set<KeyType, HashFunction, EqualKey>::hash_set(lh),
00098 #endif
00099 __mutex(new Mutex())
00100 {}
00101
00102
00103
00104 template <class KeyType, class HashFunction, class EqualKey>
00105 LockHashSet<KeyType, HashFunction, EqualKey>::~LockHashSet()
00106 {}
00107
00108
00109
00110 template <class KeyType, class HashFunction, class EqualKey>
00111 void
00112 LockHashSet<KeyType, HashFunction, EqualKey>::lock() const
00113 {
00114 __mutex->lock();
00115 }
00116
00117
00118
00119
00120
00121 template <class KeyType, class HashFunction, class EqualKey>
00122 bool
00123 LockHashSet<KeyType, HashFunction, EqualKey>::try_lock() const
00124 {
00125 return __mutex->try_lock();
00126 }
00127
00128
00129
00130 template <class KeyType, class HashFunction, class EqualKey>
00131 void
00132 LockHashSet<KeyType, HashFunction, EqualKey>::unlock() const
00133 {
00134 return __mutex->unlock();
00135 }
00136
00137
00138
00139
00140
00141 template <class KeyType, class HashFunction, class EqualKey>
00142 void
00143 LockHashSet<KeyType, HashFunction, EqualKey>::insert_locked(const KeyType& x)
00144 {
00145 __mutex->lock();
00146 insert(x);
00147 __mutex->unlock();
00148 }
00149
00150
00151
00152
00153
00154
00155 template <typename KeyType, class HashFunction, class EqualKey>
00156 RefPtr<Mutex>
00157 LockHashSet<KeyType, HashFunction, EqualKey>::mutex() const
00158 {
00159 return __mutex;
00160 }
00161
00162
00163
00164
00165
00166
00167
00168 template <typename KeyType, class HashFunction, class EqualKey>
00169 LockHashSet<KeyType, HashFunction, EqualKey> &
00170 LockHashSet<KeyType, HashFunction, EqualKey>::operator=(
00171 const LockHashSet<KeyType, HashFunction, EqualKey> &ll)
00172 {
00173 __mutex->lock();
00174 ll.lock();
00175 this->clear();
00176 typename LockHashSet<KeyType, HashFunction, EqualKey>::const_iterator i;
00177 for (i = ll.begin(); i != ll.end(); ++i) {
00178 this->insert(*i);
00179 }
00180 ll.unlock();
00181 __mutex->unlock();
00182
00183 return *this;
00184 }
00185
00186
00187 }
00188
00189 #endif