lock_queue.h

00001 
00002 /***************************************************************************
00003  *  lock_queue.h - Lockable queue
00004  *
00005  *  Created: Mon Nov 20 15:40:40 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_QUEUE_H_
00025 #define __CORE_UTILS_LOCK_QUEUE_H_
00026 
00027 #include <core/threading/mutex.h>
00028 #include <core/utils/refptr.h>
00029 #include <queue>
00030 
00031 namespace fawkes {
00032 
00033 template <typename Type>
00034 class LockQueue : public std::queue<Type>
00035 {
00036  public:
00037   LockQueue();
00038   LockQueue(const LockQueue<Type> &ll);
00039   virtual ~LockQueue();
00040 
00041   void           lock() const;
00042   bool           try_lock() const;
00043   void           unlock() const;
00044   RefPtr<Mutex>  mutex() const;
00045 
00046   void     push_locked(const Type& x);
00047   void     pop_locked();
00048 
00049   void clear();
00050 
00051   // not needed, no change to mutex required (thus "incomplete" BigThree)
00052   //LockList<Type> &  operator=(const LockList<Type> &ll);
00053  private:
00054   mutable RefPtr<Mutex> __mutex;
00055 
00056 };
00057 
00058 
00059 /** @class LockQueue core/utils/lock_queue.h
00060  * Queue with a lock.
00061  * This class provides a queue that has an intrinsic lock. The lock can be applied
00062  * with the regular locking methods.
00063  *
00064  * @see Mutex
00065  * @ingroup FCL
00066  * @author Tim Niemueller
00067  */
00068 
00069 
00070 /** Constructor. */
00071 template <typename Type>
00072 LockQueue<Type>::LockQueue()
00073   : __mutex(new Mutex())
00074 {}
00075 
00076 
00077 /** Copy constructor.
00078  * @param ll LockQueue to copy
00079  */
00080 template <typename Type>
00081 LockQueue<Type>::LockQueue(const LockQueue<Type> &ll)
00082   : std::queue<Type>::queue(ll), __mutex(new Mutex())
00083 {}
00084 
00085 
00086 /** Destructor. */
00087 template <typename Type>
00088 LockQueue<Type>::~LockQueue()
00089 {}
00090 
00091 
00092 /** Lock queue. */
00093 template <typename Type>
00094 void
00095 LockQueue<Type>::lock() const
00096 {
00097   __mutex->lock();
00098 }
00099 
00100 
00101 /** Try to lock queue.
00102  * @return true, if the lock has been aquired, false otherwise.
00103  */
00104 template <typename Type>
00105 bool
00106 LockQueue<Type>::try_lock() const
00107 {
00108   return __mutex->try_lock();
00109 }
00110 
00111 
00112 /** Unlock list. */
00113 template <typename Type>
00114 void
00115 LockQueue<Type>::unlock() const
00116 {
00117   return __mutex->unlock();
00118 }
00119 
00120 
00121 /** Push element to queue with lock protection.
00122  * @param x element to add
00123  */
00124 template <typename Type>
00125 void
00126 LockQueue<Type>::push_locked(const Type& x)
00127 {
00128   __mutex->lock();
00129   std::queue<Type>::push(x);
00130   __mutex->unlock();
00131 }
00132 
00133 
00134 /** Pop element from queue with lock protection.
00135  */
00136 template <typename Type>
00137 void
00138 LockQueue<Type>::pop_locked()
00139 {
00140   __mutex->lock();
00141   std::queue<Type>::pop();
00142   __mutex->unlock();
00143 }
00144 
00145 /** Clear the queue. */
00146 template <typename Type>
00147 void
00148 LockQueue<Type>::clear()
00149 {
00150   __mutex->lock();
00151   while ( ! std::queue<Type>::empty() ) {
00152     std::queue<Type>::pop();
00153   }
00154   __mutex->unlock();
00155 }
00156 
00157 
00158 /** Get access to the internal mutex.
00159  * Can be used with MutexLocker.
00160  * @return internal mutex
00161  */
00162 template <typename Type>
00163 RefPtr<Mutex>
00164 LockQueue<Type>::mutex() const
00165 {
00166   return __mutex;
00167 }
00168 
00169 
00170 } // end namespace fawkes
00171 
00172 #endif

Generated on 1 Mar 2011 for Fawkes API by  doxygen 1.6.1