Fawkes API
Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * lock_hashset.h - Lockable hash set 00004 * 00005 * Created: Sat May 12 13:06:31 2007 00006 * Copyright 2006-2007 Tim Niemueller [www.niemueller.de] 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 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 /** @class LockHashSet core/utils/lock_hashset.h 00072 * Hash set with a lock. 00073 * This class provides a hash set that has an intrinsic lock. The lock can be applied 00074 * with the regular locking methods. 00075 * 00076 * @see Mutex 00077 * @ingroup FCL 00078 * @author Tim Niemueller 00079 */ 00080 00081 00082 /** Constructor. */ 00083 template <class KeyType, class HashFunction, class EqualKey> 00084 LockHashSet<KeyType, HashFunction, EqualKey>::LockHashSet() 00085 : __mutex(new Mutex()) 00086 {} 00087 00088 00089 /** Copy constructor. 00090 * @param lh LockHashSet to copy 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 /** Destructor. */ 00104 template <class KeyType, class HashFunction, class EqualKey> 00105 LockHashSet<KeyType, HashFunction, EqualKey>::~LockHashSet() 00106 {} 00107 00108 00109 /** Lock set. */ 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 /** Try to lock set. 00119 * @return true, if the lock has been aquired, false otherwise. 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 /** Unlock set. */ 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 /** Insert element to hash set with lock protection. 00139 * @param x element to add 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 /** Get access to the internal mutex. 00152 * Can be used with MutexLocker. 00153 * @return internal mutex 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 /** Copy values from another LockHashSet. 00163 * Copies the values one by one. Both instances are locked during the copying and 00164 * this instance is cleared before copying. 00165 * @param ll lock hash set to copy 00166 * @return reference to this instance 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 } // end namespace fawkes 00188 00189 #endif