rwlock_map.h

00001 
00002 /***************************************************************************
00003  *  rwlock_map.h - Map with read/write lock
00004  *
00005  *  Created: Tue Jan 13 16:29:33 2009
00006  *  Copyright  2006-2009  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_RWLOCK_MAP_H_
00025 #define __CORE_UTILS_RWLOCK_MAP_H_
00026 
00027 #include <core/threading/read_write_lock.h>
00028 #include <core/utils/refptr.h>
00029 #include <map>
00030 
00031 namespace fawkes {
00032 
00033 
00034 template <typename KeyType,
00035           typename ValueType,
00036           typename LessKey     = std::less<KeyType> >
00037 class RWLockMap : public std::map<KeyType, ValueType, LessKey>
00038 {
00039  public:
00040   RWLockMap();
00041   RWLockMap(const RWLockMap<KeyType, ValueType, LessKey> &lm);
00042   virtual ~RWLockMap();
00043 
00044   void                   lock_for_read();
00045   void                   lock_for_write();
00046   bool                   try_lock_for_read();
00047   bool                   try_lock_for_write();
00048   void                   unlock();
00049   RefPtr<ReadWriteLock>  rwlock() const;
00050 
00051   void     erase_locked(const KeyType &key);
00052 
00053  private:
00054   RefPtr<ReadWriteLock> __rwlock;
00055 
00056 };
00057 
00058 
00059 /** @class RWLockMap core/utils/rwlock_map.h
00060  * Hash map with a lock.
00061  * This class provides a map that has an intrinsic read/write lock. The lock can
00062  * be applied with the regular locking methods.
00063  *
00064  * @see ReadWriteLock
00065  * @ingroup FCL
00066  * @author Tim Niemueller
00067  */
00068 
00069 
00070 /** Constructor. */
00071 template <typename KeyType, typename ValueType, typename LessKey>
00072 RWLockMap<KeyType, ValueType, LessKey>::RWLockMap()
00073   : __rwlock(new ReadWriteLock())
00074 {}
00075 
00076 
00077 /** Copy constructor.
00078  * @param lm RWLockMap to copy
00079  */
00080 template <typename KeyType, typename ValueType, typename LessKey>
00081 RWLockMap<KeyType, ValueType, LessKey>::RWLockMap(const RWLockMap<KeyType, ValueType, LessKey> &lm)
00082   : std::map<KeyType, ValueType, LessKey>::map(lm), __rwlock(new ReadWriteLock())
00083 {}
00084 
00085 
00086 /** Destructor. */
00087 template <typename KeyType, typename ValueType, typename LessKey>
00088 RWLockMap<KeyType, ValueType, LessKey>::~RWLockMap()
00089 {}
00090 
00091 
00092 /** Lock list for reading. */
00093 template <typename KeyType, typename ValueType, typename LessKey>
00094 void
00095 RWLockMap<KeyType, ValueType, LessKey>::lock_for_read()
00096 {
00097   __rwlock->lock_for_read();
00098 }
00099 
00100 
00101 /** Lock list for writing. */
00102 template <typename KeyType, typename ValueType, typename LessKey>
00103 void
00104 RWLockMap<KeyType, ValueType, LessKey>::lock_for_write()
00105 {
00106   __rwlock->lock_for_write();
00107 }
00108 
00109 
00110 /** Try to lock list for reading.
00111  * @return true, if the lock has been aquired, false otherwise.
00112  */
00113 template <typename KeyType, typename ValueType, typename LessKey>
00114 bool
00115 RWLockMap<KeyType, ValueType, LessKey>::try_lock_for_read()
00116 {
00117   return __rwlock->try_lock_for_read();
00118 }
00119 
00120 
00121 /** Try to lock list for writing.
00122  * @return true, if the lock has been aquired, false otherwise.
00123  */
00124 template <typename KeyType, typename ValueType, typename LessKey>
00125 bool
00126 RWLockMap<KeyType, ValueType, LessKey>::try_lock_for_write()
00127 {
00128   return __rwlock->try_lock_for_write();
00129 }
00130 
00131 
00132 /** Unlock list. */
00133 template <typename KeyType, typename ValueType, typename LessKey>
00134 void
00135 RWLockMap<KeyType, ValueType, LessKey>::unlock()
00136 {
00137   return __rwlock->unlock();
00138 }
00139 
00140 
00141 /** Remove item with lock.
00142  * The map is automatically locked and unlocked during the removal.
00143  * @param key key of the value to erase
00144  */
00145 template <typename KeyType, typename ValueType, typename LessKey>
00146 void
00147 RWLockMap<KeyType, ValueType, LessKey>::erase_locked(const KeyType &key)
00148 {
00149   __rwlock->lock_for_write();
00150   std::map<KeyType, ValueType, LessKey>::erase(key);
00151   __rwlock->unlock();
00152 }
00153 
00154 
00155 /** Get access to the internal rwlock.
00156  * Can be used with RwlockLocker.
00157  * @return internal rwlock
00158  */
00159 template <typename KeyType, typename ValueType, typename LessKey>
00160 RefPtr<ReadWriteLock>
00161 RWLockMap<KeyType, ValueType, LessKey>::rwlock() const
00162 {
00163   return __rwlock;
00164 }
00165 
00166 
00167 } // end namespace fawkes
00168 
00169 #endif