lock_hashset.h

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();
00056   bool           try_lock();
00057   void           unlock();
00058   RefPtr<Mutex>  mutex() const;
00059
00060   void           insert_locked(const KeyType& x);
00061
00062  private:
00063   RefPtr<Mutex> __mutex;
00064
00065 };
00066
00067 
00068 /** @class LockHashSet core/utils/lock_hashset.h
00069  * Hash set with a lock.
00070  * This class provides a hash set that has an intrinsic lock. The lock can be applied
00071  * with the regular locking methods.
00072  *
00073  * @see Mutex
00074  * @ingroup FCL
00075  * @author Tim Niemueller
00076  */
00077
00078 
00079 /** Constructor. */
00080 template <class KeyType, class HashFunction, class EqualKey>
00081 LockHashSet<KeyType, HashFunction, EqualKey>::LockHashSet()
00082   : __mutex(new Mutex())
00083 {}
00084
00085 
00086 /** Copy constructor.
00087  * @param lh LockHashSet to copy
00088  */
00089 template <class KeyType, class HashFunction, class EqualKey>
00090 LockHashSet<KeyType, HashFunction, EqualKey>::LockHashSet(const LockHashSet<KeyType, HashFunction, EqualKey> &lh)
00091 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2)
00092   : std::tr1::unordered_set<KeyType, HashFunction, EqualKey>::unordered_set(lh),
00093 #else
00094   : __gnu_cxx::hash_set<KeyType, HashFunction, EqualKey>::hash_set(lh),
00095 #endif
00096     __mutex(new Mutex())
00097 {}
00098
00099 
00100 /** Destructor. */
00101 template <class KeyType, class HashFunction, class EqualKey>
00102 LockHashSet<KeyType, HashFunction, EqualKey>::~LockHashSet()
00103 {}
00104
00105 
00106 /** Lock list. */
00107 template <class KeyType, class HashFunction, class EqualKey>
00108 void
00109 LockHashSet<KeyType, HashFunction, EqualKey>::lock()
00110 {
00111   __mutex->lock();
00112 }
00113
00114 
00115 /** Try to lock list.
00116  * @return true, if the lock has been aquired, false otherwise.
00117  */
00118 template <class KeyType, class HashFunction, class EqualKey>
00119 bool
00120 LockHashSet<KeyType, HashFunction, EqualKey>::try_lock()
00121 {
00122   return __mutex->try_lock();
00123 }
00124
00125 
00126 /** Unlock list. */
00127 template <class KeyType, class HashFunction, class EqualKey>
00128 void
00129 LockHashSet<KeyType, HashFunction, EqualKey>::unlock()
00130 {
00131   return __mutex->unlock();
00132 }
00133
00134 
00135 /** Insert element to hash set with lock protection.
00136  * @param x element to add
00137  */
00138 template <class KeyType, class HashFunction, class EqualKey>
00139 void
00140 LockHashSet<KeyType, HashFunction, EqualKey>::insert_locked(const KeyType& x)
00141 {
00142   __mutex->lock();
00143   insert(x);
00144   __mutex->unlock();
00145 }
00146
00147 
00148 /** Get access to the internal mutex.
00149  * Can be used with MutexLocker.
00150  * @return internal mutex
00151  */
00152 template <typename KeyType, class HashFunction, class EqualKey>
00153 RefPtr<Mutex>
00154 LockHashSet<KeyType, HashFunction, EqualKey>::mutex() const
00155 {
00156   return __mutex;
00157 }
00158
00159 } // end namespace fawkes
00160
00161 #endif