TrinityCore
LockedQueue.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore 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 <deque>
22#include <mutex>
23
24template <class T, typename StorageType = std::deque<T> >
26{
28 std::mutex _lock;
29
31 StorageType _queue;
32
34 volatile bool _canceled;
35
36public:
37
40 : _canceled(false)
41 {
42 }
43
45 virtual ~LockedQueue()
46 {
47 }
48
50 void add(const T& item)
51 {
52 lock();
53
54 _queue.push_back(item);
55
56 unlock();
57 }
58
60 template<class Iterator>
61 void readd(Iterator begin, Iterator end)
62 {
63 std::lock_guard<std::mutex> lock(_lock);
64 _queue.insert(_queue.begin(), begin, end);
65 }
66
68 bool next(T& result)
69 {
70 std::lock_guard<std::mutex> lock(_lock);
71
72 if (_queue.empty())
73 return false;
74
75 result = _queue.front();
76 _queue.pop_front();
77
78 return true;
79 }
80
81 template<class Checker>
82 bool next(T& result, Checker& check)
83 {
84 std::lock_guard<std::mutex> lock(_lock);
85
86 if (_queue.empty())
87 return false;
88
89 result = _queue.front();
90 if (!check.Process(result))
91 return false;
92
93 _queue.pop_front();
94 return true;
95 }
96
98 T& peek(bool autoUnlock = false)
99 {
100 lock();
101
102 T& result = _queue.front();
103
104 if (autoUnlock)
105 unlock();
106
107 return result;
108 }
109
111 void cancel()
112 {
113 std::lock_guard<std::mutex> lock(_lock);
114
115 _canceled = true;
116 }
117
120 {
121 std::lock_guard<std::mutex> lock(_lock);
122 return _canceled;
123 }
124
126 void lock()
127 {
128 this->_lock.lock();
129 }
130
132 void unlock()
133 {
134 this->_lock.unlock();
135 }
136
139 {
140 std::lock_guard<std::mutex> lock(_lock);
141 _queue.pop_front();
142 }
143
145 bool empty()
146 {
147 std::lock_guard<std::mutex> lock(_lock);
148 return _queue.empty();
149 }
150};
151#endif
void add(const T &item)
Adds an item to the queue.
Definition: LockedQueue.h:50
volatile bool _canceled
Cancellation flag.
Definition: LockedQueue.h:34
void unlock()
Unlocks the queue.
Definition: LockedQueue.h:132
void readd(Iterator begin, Iterator end)
Adds items back to front of the queue.
Definition: LockedQueue.h:61
StorageType _queue
Storage backing the queue.
Definition: LockedQueue.h:31
std::mutex _lock
Lock access to the queue.
Definition: LockedQueue.h:28
bool cancelled()
Checks if the queue is cancelled.
Definition: LockedQueue.h:119
bool empty()
! Checks if we're empty or not with locks held
Definition: LockedQueue.h:145
void lock()
Locks the queue for access.
Definition: LockedQueue.h:126
void pop_front()
! Calls pop_front of the queue
Definition: LockedQueue.h:138
void cancel()
Cancels the queue.
Definition: LockedQueue.h:111
bool next(T &result)
Gets the next result in the queue, if any.
Definition: LockedQueue.h:68
bool next(T &result, Checker &check)
Definition: LockedQueue.h:82
virtual ~LockedQueue()
Destroy a LockedQueue.
Definition: LockedQueue.h:45
LockedQueue()
Create a LockedQueue.
Definition: LockedQueue.h:39
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:98