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 #include <queue>
30 
31 namespace fawkes {
32 
33 /** @class LockQueue <core/utils/lock_queue.h>
34  * Queue with a lock.
35  * This class provides a queue that has an intrinsic lock. The lock can be applied
36  * with the regular locking methods.
37  *
38  * @see Mutex
39  * @ingroup FCL
40  * @author Tim Niemueller
41  */
42 template <typename Type>
43 class LockQueue : public std::queue<Type>
44 {
45  public:
46  /** Constructor. */
47  LockQueue();
48 
49  /** Copy constructor.
50  * @param ll LockQueue to copy
51  */
52  LockQueue(const LockQueue<Type> &ll);
53 
54  /** Destructor. */
55  virtual ~LockQueue();
56 
57  /** Lock queue. */
58  void lock() const;
59 
60  /** Try to lock queue.
61  * @return true, if the lock has been aquired, false otherwise.
62  */
63  bool try_lock() const;
64 
65  /** Unlock list. */
66  void unlock() const;
67 
68  /** Get access to the internal mutex.
69  * Can be used with MutexLocker.
70  * @return internal mutex
71  */
73  { return __mutex; }
74 
75  /** Push element to queue with lock protection.
76  * @param x element to add
77  */
78  void push_locked(const Type& x);
79 
80  /** Pop element from queue with lock protection. */
81  void pop_locked();
82 
83  /** Clear the queue. */
84  void clear();
85 
86  // not needed, no change to mutex required (thus "incomplete" BigThree)
87  //LockList<Type> & operator=(const LockList<Type> &ll);
88  private:
89  mutable RefPtr<Mutex> __mutex;
90 
91 };
92 
93 
94 
95 
96 template <typename Type>
98  : __mutex(new Mutex())
99 {}
100 
101 
102 template <typename Type>
104  : std::queue<Type>::queue(ll), __mutex(new Mutex())
105 {}
106 
107 
108 template <typename Type>
110 {}
111 
112 
113 template <typename Type>
114 void
116 {
117  __mutex->lock();
118 }
119 
120 
121 template <typename Type>
122 bool
124 {
125  return __mutex->try_lock();
126 }
127 
128 
129 template <typename Type>
130 void
132 {
133  return __mutex->unlock();
134 }
135 
136 
137 template <typename Type>
138 void
140 {
141  __mutex->lock();
142  std::queue<Type>::push(x);
143  __mutex->unlock();
144 }
145 
146 
147 template <typename Type>
148 void
150 {
151  __mutex->lock();
152  std::queue<Type>::pop();
153  __mutex->unlock();
154 }
155 
156 template <typename Type>
157 void
159 {
160  __mutex->lock();
161  while ( ! std::queue<Type>::empty() ) {
162  std::queue<Type>::pop();
163  }
164  __mutex->unlock();
165 }
166 
167 
168 } // end namespace fawkes
169 
170 #endif
void clear()
Clear the queue.
Definition: lock_queue.h:158
bool try_lock() const
Try to lock queue.
Definition: lock_queue.h:123
void unlock() const
Unlock list.
Definition: lock_queue.h:131
Fawkes library namespace.
STL namespace.
RefPtr< Mutex > mutex() const
Get access to the internal mutex.
Definition: lock_queue.h:72
virtual ~LockQueue()
Destructor.
Definition: lock_queue.h:109
Queue with a lock.
Definition: lock_queue.h:43
LockQueue()
Constructor.
Definition: lock_queue.h:97
void pop_locked()
Pop element from queue with lock protection.
Definition: lock_queue.h:149
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:139
void lock() const
Lock queue.
Definition: lock_queue.h:115
Mutex mutual exclusion lock.
Definition: mutex.h:32