OregonCore  revision 3611e8a-git
Your Favourite TBC server
LockedQueue.h
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef LOCKEDQUEUE_H
19 #define LOCKEDQUEUE_H
20 
21 #include <ace/Guard_T.h>
22 #include <ace/Thread_Mutex.h>
23 #include <deque>
24 #include <assert.h>
25 #include "Errors.h"
26 
27 namespace ACE_Based
28 {
29 template <class T, class LockType, typename StorageType = std::deque<T> >
31 {
32  // Lock access to the queue.
34 
35  // Storage backing the queue.
36  StorageType _queue;
37 
38  // Cancellation flag.
39  volatile bool _canceled;
40 
41  public:
42 
43  // Create a LockedQueue.
45  : _canceled(false)
46  {
47  }
48 
49  // Destroy a LockedQueue.
50  virtual ~LockedQueue()
51  {
52  }
53 
54  // Adds an item to the queue.
55  void add(const T& item)
56  {
57  lock();
58 
59  //ASSERT(!this->_canceled);
60  //throw Cancellation_Exception();
61 
62  _queue.push_back(item);
63 
64  unlock();
65  }
66 
67  // Gets the next result in the queue, if any.
68  bool next(T& result)
69  {
70  // ACE_Guard<LockType> g(this->_lock);
71  ACE_GUARD_RETURN (LockType, g, this->_lock, false);
72 
73  if (_queue.empty())
74  return false;
75 
76  //ASSERT (!_queue.empty() || !this->_canceled);
77  //throw Cancellation_Exception();
78  result = _queue.front();
79  _queue.pop_front();
80 
81  return true;
82  }
83 
84  // Peeks at the top of the queue. Remember to unlock after use.
85  T& peek()
86  {
87  lock();
88 
89  T& result = _queue.front();
90 
91  return result;
92  }
93 
94  // Cancels the queue.
95  void cancel()
96  {
97  lock();
98 
99  _canceled = true;
100 
101  unlock();
102  }
103 
104  // Checks if the queue is cancelled.
105  bool cancelled()
106  {
107  ACE_Guard<LockType> g(this->_lock);
108  return _canceled;
109  }
110 
111  // Locks the queue for access.
112  void lock()
113  {
114  this->_lock.acquire();
115  }
116 
117  // Unlocks the queue.
118  void unlock()
119  {
120  this->_lock.release();
121  }
122 
123  // Calls pop_front of the queue
124  void pop_front()
125  {
126  ACE_GUARD (LockType, g, this->_lock);
127  _queue.pop_front();
128  }
129 
130  // Checks if we're empty or not with locks held
131  bool empty()
132  {
133  ACE_GUARD_RETURN (LockType, g, this->_lock, false);
134  return _queue.empty();
135  }
136 };
137 }
138 #endif
139 
bool next(T &result)
Definition: LockedQueue.h:68
NULL Dbg ErrDB Arena Chat Char Map MMap false
Definition: Log.cpp:556
LockType
volatile bool _canceled
Definition: LockedQueue.h:39
void add(const T &item)
Definition: LockedQueue.h:55