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_HASHMAP_H_
00025 #define __CORE_UTILS_LOCK_HASHMAP_H_
00026
00027 #include <core/threading/mutex.h>
00028 #include <core/utils/refptr.h>
00029 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)
00030 # include <tr1/unordered_map>
00031 #else
00032 # include <ext/hash_map>
00033 #endif
00034
00035 namespace fawkes {
00036
00037
00038 template <class KeyType,
00039 class ValueType,
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 LockHashMap : public std::tr1::unordered_map<KeyType, ValueType, HashFunction, EqualKey>
00044 #else
00045 class HashFunction = __gnu_cxx::hash<KeyType>,
00046 class EqualKey = std::equal_to<KeyType> >
00047 class LockHashMap : public __gnu_cxx::hash_map<KeyType, ValueType, HashFunction, EqualKey>
00048 #endif
00049 {
00050 public:
00051 LockHashMap();
00052 LockHashMap(const LockHashMap<KeyType, ValueType, HashFunction, EqualKey> &lh);
00053 virtual ~LockHashMap();
00054
00055 void lock() const;
00056 bool try_lock() const;
00057 void unlock() const;
00058 RefPtr<Mutex> mutex() const;
00059
00060 LockHashMap<KeyType, ValueType, HashFunction, EqualKey> &
00061 operator=(const LockHashMap<KeyType, ValueType, HashFunction, EqualKey> &ll);
00062
00063 private:
00064 mutable RefPtr<Mutex> __mutex;
00065
00066 };
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
00082 LockHashMap<KeyType, ValueType, HashFunction, EqualKey>::LockHashMap()
00083 : __mutex(new Mutex())
00084 {
00085 }
00086
00087
00088
00089
00090
00091 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
00092 LockHashMap<KeyType, ValueType, HashFunction, EqualKey>::LockHashMap(const LockHashMap<KeyType, ValueType, HashFunction, EqualKey> &lh)
00093 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)
00094 : std::tr1::unordered_map<KeyType, ValueType, HashFunction, EqualKey>::unordered_map(lh)
00095 #else
00096 : __gnu_cxx::hash_map<KeyType, ValueType, HashFunction, EqualKey>::hash_map(lh)
00097 #endif
00098 , __mutex(new Mutex())
00099 {
00100 }
00101
00102
00103
00104 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
00105 LockHashMap<KeyType, ValueType, HashFunction, EqualKey>::~LockHashMap()
00106 {
00107 }
00108
00109
00110
00111 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
00112 void
00113 LockHashMap<KeyType, ValueType, HashFunction, EqualKey>::lock() const
00114 {
00115 __mutex->lock();
00116 }
00117
00118
00119
00120
00121
00122 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
00123 bool
00124 LockHashMap<KeyType, ValueType, HashFunction, EqualKey>::try_lock() const
00125 {
00126 return __mutex->try_lock();
00127 }
00128
00129
00130
00131 template <class KeyType, class ValueType, class HashFunction, class EqualKey>
00132 void
00133 LockHashMap<KeyType, ValueType, HashFunction, EqualKey>::unlock() const
00134 {
00135 return __mutex->unlock();
00136 }
00137
00138
00139
00140
00141
00142
00143 template <typename KeyType, typename ValueType, class HashFunction, typename EqualKey>
00144 RefPtr<Mutex>
00145 LockHashMap<KeyType, ValueType, HashFunction, EqualKey>::mutex() const
00146 {
00147 return __mutex;
00148 }
00149
00150
00151
00152
00153
00154
00155
00156
00157 template <typename KeyType, typename ValueType, class HashFunction, typename EqualKey>
00158 LockHashMap<KeyType, ValueType, HashFunction, EqualKey> &
00159 LockHashMap<KeyType, ValueType, HashFunction, EqualKey>::operator=(
00160 const LockHashMap<KeyType, ValueType, HashFunction, EqualKey> &ll)
00161 {
00162 __mutex->lock();
00163 ll.lock();
00164 this->clear();
00165 typename LockHashMap<KeyType, ValueType, HashFunction, EqualKey>::const_iterator i;
00166 for (i = ll.begin(); i != ll.end(); ++i) {
00167 this->insert(*i);
00168 }
00169 ll.unlock();
00170 __mutex->unlock();
00171
00172 return *this;
00173 }
00174
00175 }
00176
00177 #endif