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