Fawkes API Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * lock_list.h - Lockable list 00004 * 00005 * Created: Tue Oct 31 18:25:03 2006 00006 * Copyright 2006 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_LIST_H_ 00025 #define __CORE_UTILS_LOCK_LIST_H_ 00026 00027 #include <core/threading/mutex.h> 00028 #include <core/utils/refptr.h> 00029 #include <list> 00030 00031 namespace fawkes { 00032 00033 00034 template <typename Type> 00035 class LockList : public std::list<Type> 00036 { 00037 public: 00038 LockList(); 00039 LockList(const LockList<Type> &ll); 00040 virtual ~LockList(); 00041 virtual void lock() const; 00042 virtual bool try_lock() const; 00043 virtual void unlock() const; 00044 RefPtr<Mutex> mutex() const; 00045 00046 void push_back_locked(const Type& x); 00047 void push_front_locked(const Type& x); 00048 void remove_locked(const Type& x); 00049 00050 LockList<Type> & operator=(const LockList<Type> &ll); 00051 LockList<Type> & operator=(const std::list<Type> &l); 00052 private: 00053 mutable RefPtr<Mutex> __mutex; 00054 00055 }; 00056 00057 00058 /** @class LockList <core/utils/lock_list.h> 00059 * List with a lock. 00060 * This class provides a list that has an intrinsic lock. The lock can be applied 00061 * with the regular locking methods. 00062 * 00063 * @see Mutex 00064 * @ingroup FCL 00065 * @author Tim Niemueller 00066 */ 00067 00068 00069 /** Constructor. */ 00070 template <typename Type> 00071 LockList<Type>::LockList() 00072 : __mutex(new Mutex()) 00073 {} 00074 00075 00076 /** Copy constructor. 00077 * @param ll LockList to copy 00078 */ 00079 template <typename Type> 00080 LockList<Type>::LockList(const LockList<Type> &ll) 00081 : std::list<Type>::list(ll), __mutex(new Mutex()) 00082 {} 00083 00084 00085 /** Destructor. */ 00086 template <typename Type> 00087 LockList<Type>::~LockList() 00088 {} 00089 00090 00091 /** Lock list. */ 00092 template <typename Type> 00093 void 00094 LockList<Type>::lock() const 00095 { 00096 __mutex->lock(); 00097 } 00098 00099 00100 /** Try to lock list. 00101 * @return true, if the lock has been aquired, false otherwise. 00102 */ 00103 template <typename Type> 00104 bool 00105 LockList<Type>::try_lock() const 00106 { 00107 return __mutex->try_lock(); 00108 } 00109 00110 00111 /** Unlock list. */ 00112 template <typename Type> 00113 void 00114 LockList<Type>::unlock() const 00115 { 00116 return __mutex->unlock(); 00117 } 00118 00119 00120 /** Push element to list at back with lock protection. 00121 * @param x element to add 00122 */ 00123 template <typename Type> 00124 void 00125 LockList<Type>::push_back_locked(const Type& x) 00126 { 00127 __mutex->lock(); 00128 std::list<Type>::push_back(x); 00129 __mutex->unlock(); 00130 } 00131 00132 00133 /** Push element to list at front with lock protection. 00134 * @param x element to add 00135 */ 00136 template <typename Type> 00137 void 00138 LockList<Type>::push_front_locked(const Type& x) 00139 { 00140 __mutex->lock(); 00141 std::list<Type>::push_front(x); 00142 __mutex->unlock(); 00143 } 00144 00145 00146 /** Remove element from list with lock protection. 00147 * @param x element to remove 00148 */ 00149 template <typename Type> 00150 void 00151 LockList<Type>::remove_locked(const Type& x) 00152 { 00153 __mutex->lock(); 00154 std::list<Type>::remove(x); 00155 __mutex->unlock(); 00156 } 00157 00158 00159 /** Get access to the internal mutex. 00160 * Can be used with MutexLocker. 00161 * @return internal mutex 00162 */ 00163 template <typename Type> 00164 RefPtr<Mutex> 00165 LockList<Type>::mutex() const 00166 { 00167 return __mutex; 00168 } 00169 00170 00171 /** Copy values from another LockList. 00172 * Copies the values one by one. Both instances are locked during the copying and 00173 * this instance is cleared before copying. 00174 * @param ll list to copy 00175 * @return reference to this instance 00176 */ 00177 template <typename Type> 00178 LockList<Type> & 00179 LockList<Type>::operator=(const LockList<Type> &ll) 00180 { 00181 __mutex->lock(); 00182 ll.lock(); 00183 this->clear(); 00184 typename LockList<Type>::const_iterator i; 00185 for (i = ll.begin(); i != ll.end(); ++i) { 00186 this->push_back(*i); 00187 } 00188 ll.unlock(); 00189 __mutex->unlock(); 00190 00191 return *this; 00192 } 00193 00194 00195 /** Copy values from a standard list. 00196 * Copies the values one by one. This instance is locked during the copying and 00197 * cleared. 00198 * @param l list to copy 00199 * @return reference to this instance 00200 */ 00201 template <typename Type> 00202 LockList<Type> & 00203 LockList<Type>::operator=(const std::list<Type> &l) 00204 { 00205 __mutex->lock(); 00206 this->clear(); 00207 typename std::list<Type>::const_iterator i; 00208 for (i = l.begin(); i != l.end(); ++i) { 00209 this->push_back(*i); 00210 } 00211 __mutex->unlock(); 00212 00213 return *this; 00214 } 00215 00216 } // end namespace fawkes 00217 00218 #endif