TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LockedQueue.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef LOCKEDQUEUE_H
20 #define LOCKEDQUEUE_H
21 
22 #include <deque>
23 #include <mutex>
24 
25 template <class T, typename StorageType = std::deque<T> >
27 {
29  std::mutex _lock;
30 
32  StorageType _queue;
33 
35  volatile bool _canceled;
36 
37 public:
38 
41  : _canceled(false)
42  {
43  }
44 
46  virtual ~LockedQueue()
47  {
48  }
49 
51  void add(const T& item)
52  {
53  lock();
54 
55  _queue.push_back(item);
56 
57  unlock();
58  }
59 
61  template<class Iterator>
62  void readd(Iterator begin, Iterator end)
63  {
64  std::lock_guard<std::mutex> lock(_lock);
65  _queue.insert(_queue.begin(), begin, end);
66  }
67 
69  bool next(T& result)
70  {
71  std::lock_guard<std::mutex> lock(_lock);
72 
73  if (_queue.empty())
74  return false;
75 
76  result = _queue.front();
77  _queue.pop_front();
78 
79  return true;
80  }
81 
82  template<class Checker>
83  bool next(T& result, Checker& check)
84  {
85  std::lock_guard<std::mutex> lock(_lock);
86 
87  if (_queue.empty())
88  return false;
89 
90  result = _queue.front();
91  if (!check.Process(result))
92  return false;
93 
94  _queue.pop_front();
95  return true;
96  }
97 
99  T& peek(bool autoUnlock = false)
100  {
101  lock();
102 
103  T& result = _queue.front();
104 
105  if (autoUnlock)
106  unlock();
107 
108  return result;
109  }
110 
112  void cancel()
113  {
114  std::lock_guard<std::mutex> lock(_lock);
115 
116  _canceled = true;
117  }
118 
120  bool cancelled()
121  {
122  std::lock_guard<std::mutex> lock(_lock);
123  return _canceled;
124  }
125 
127  void lock()
128  {
129  this->_lock.lock();
130  }
131 
133  void unlock()
134  {
135  this->_lock.unlock();
136  }
137 
139  void pop_front()
140  {
141  std::lock_guard<std::mutex> lock(_lock);
142  _queue.pop_front();
143  }
144 
146  bool empty()
147  {
148  std::lock_guard<std::mutex> lock(_lock);
149  return _queue.empty();
150  }
151 };
152 #endif
void unlock()
Unlocks the queue.
Definition: LockedQueue.h:133
void pop_front()
! Calls pop_front of the queue
Definition: LockedQueue.h:139
bool cancelled()
Checks if the queue is cancelled.
Definition: LockedQueue.h:120
#define false
Definition: CascPort.h:18
T & peek(bool autoUnlock=false)
Peeks at the top of the queue. Check if the queue is empty before calling! Remember to unlock after u...
Definition: LockedQueue.h:99
virtual ~LockedQueue()
Destroy a LockedQueue.
Definition: LockedQueue.h:46
StorageType _queue
Storage backing the queue.
Definition: LockedQueue.h:32
bool empty()
! Checks if we're empty or not with locks held
Definition: LockedQueue.h:146
bool next(T &result)
Gets the next result in the queue, if any.
Definition: LockedQueue.h:69
void cancel()
Cancels the queue.
Definition: LockedQueue.h:112
bool next(T &result, Checker &check)
Definition: LockedQueue.h:83
void add(const T &item)
Adds an item to the queue.
Definition: LockedQueue.h:51
void readd(Iterator begin, Iterator end)
Adds items back to front of the queue.
Definition: LockedQueue.h:62
void lock()
Locks the queue for access.
Definition: LockedQueue.h:127
T check(T value)
Definition: format.h:305
volatile bool _canceled
Cancellation flag.
Definition: LockedQueue.h:35
Definition: LockedQueue.h:26
LockedQueue()
Create a LockedQueue.
Definition: LockedQueue.h:40
std::mutex _lock
Lock access to the queue.
Definition: LockedQueue.h:29