rwlock_list.h

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

Generated on 1 Mar 2011 for Fawkes API by  doxygen 1.6.1