Fawkes API  Fawkes Development Version
lock_queue.h
1 
2 /***************************************************************************
3  * lock_queue.h - Lockable queue
4  *
5  * Created: Mon Nov 20 15:40:40 2006
6  * Copyright 2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #ifndef _CORE_UTILS_LOCK_QUEUE_H_
25 #define _CORE_UTILS_LOCK_QUEUE_H_
26 
27 #include <core/threading/mutex.h>
28 #include <core/utils/refptr.h>
29 
30 #include <queue>
31 
32 namespace fawkes {
33 
34 /** @class LockQueue <core/utils/lock_queue.h>
35  * Queue with a lock.
36  * This class provides a queue that has an intrinsic lock. The lock can be applied
37  * with the regular locking methods.
38  *
39  * @see Mutex
40  * @ingroup FCL
41  * @author Tim Niemueller
42  */
43 template <typename Type>
44 class LockQueue : public std::queue<Type>
45 {
46 public:
47  /** Constructor. */
48  LockQueue();
49 
50  /** Copy constructor.
51  * @param ll LockQueue to copy
52  */
53  LockQueue(const LockQueue<Type> &ll);
54 
55  /** Destructor. */
56  virtual ~LockQueue();
57 
58  /** Lock queue. */
59  void lock() const;
60 
61  /** Try to lock queue.
62  * @return true, if the lock has been aquired, false otherwise.
63  */
64  bool try_lock() const;
65 
66  /** Unlock list. */
67  void unlock() const;
68 
69  /** Get access to the internal mutex.
70  * Can be used with MutexLocker.
71  * @return internal mutex
72  */
74  mutex() const
75  {
76  return mutex_;
77  }
78 
79  /** Push element to queue with lock protection.
80  * @param x element to add
81  */
82  void push_locked(const Type &x);
83 
84  /** Pop element from queue with lock protection. */
85  void pop_locked();
86 
87  /** Clear the queue. */
88  void clear();
89 
90  // not needed, no change to mutex required (thus "incomplete" BigThree)
91  //LockList<Type> & operator=(const LockList<Type> &ll);
92 private:
93  mutable RefPtr<Mutex> mutex_;
94 };
95 
96 template <typename Type>
98 {
99 }
100 
101 template <typename Type>
103 : std::queue<Type>::queue(ll), mutex_(new Mutex())
104 {
105 }
106 
107 template <typename Type>
109 {
110 }
111 
112 template <typename Type>
113 void
115 {
116  mutex_->lock();
117 }
118 
119 template <typename Type>
120 bool
122 {
123  return mutex_->try_lock();
124 }
125 
126 template <typename Type>
127 void
129 {
130  return mutex_->unlock();
131 }
132 
133 template <typename Type>
134 void
136 {
137  mutex_->lock();
138  std::queue<Type>::push(x);
139  mutex_->unlock();
140 }
141 
142 template <typename Type>
143 void
145 {
146  mutex_->lock();
147  std::queue<Type>::pop();
148  mutex_->unlock();
149 }
150 
151 template <typename Type>
152 void
154 {
155  mutex_->lock();
156  while (!std::queue<Type>::empty()) {
157  std::queue<Type>::pop();
158  }
159  mutex_->unlock();
160 }
161 
162 } // end namespace fawkes
163 
164 #endif
void clear()
Clear the queue.
Definition: lock_queue.h:153
bool try_lock() const
Try to lock queue.
Definition: lock_queue.h:121
void unlock() const
Unlock list.
Definition: lock_queue.h:128
Fawkes library namespace.
RefPtr< Mutex > mutex() const
Get access to the internal mutex.
Definition: lock_queue.h:74
virtual ~LockQueue()
Destructor.
Definition: lock_queue.h:108
Queue with a lock.
Definition: lock_queue.h:44
LockQueue()
Constructor.
Definition: lock_queue.h:97
void pop_locked()
Pop element from queue with lock protection.
Definition: lock_queue.h:144
RefPtr<> is a reference-counting shared smartpointer.
Definition: refptr.h:49
void push_locked(const Type &x)
Push element to queue with lock protection.
Definition: lock_queue.h:135
void lock() const
Lock queue.
Definition: lock_queue.h:114
Mutex mutual exclusion lock.
Definition: mutex.h:32