lock_list.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 template <typename Type>
00071 LockList<Type>::LockList()
00072 : __mutex(new Mutex())
00073 {}
00074
00075
00076
00077
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
00086 template <typename Type>
00087 LockList<Type>::~LockList()
00088 {}
00089
00090
00091
00092 template <typename Type>
00093 void
00094 LockList<Type>::lock() const
00095 {
00096 __mutex->lock();
00097 }
00098
00099
00100
00101
00102
00103 template <typename Type>
00104 bool
00105 LockList<Type>::try_lock() const
00106 {
00107 return __mutex->try_lock();
00108 }
00109
00110
00111
00112 template <typename Type>
00113 void
00114 LockList<Type>::unlock() const
00115 {
00116 return __mutex->unlock();
00117 }
00118
00119
00120
00121
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
00134
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
00147
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
00160
00161
00162
00163 template <typename Type>
00164 RefPtr<Mutex>
00165 LockList<Type>::mutex() const
00166 {
00167 return __mutex;
00168 }
00169
00170
00171
00172
00173
00174
00175
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
00196
00197
00198
00199
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 }
00217
00218 #endif