TrinityCore
Loading...
Searching...
No Matches
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 TRINITYCORE_LOCKED_QUEUE_H
19#define TRINITYCORE_LOCKED_QUEUE_H
20
21#include "Concepts.h"
22#include <deque>
23#include <mutex>
24
25template <class T, typename StorageType = std::deque<T>>
27{
29 std::mutex _lock;
30
32 StorageType _queue;
33
35 bool _canceled = false;
36
37public:
38
40 void add(T const& item)
41 {
42 std::scoped_lock lock(_lock);
43 _queue.push_back(item);
44 }
45
47 void add(T&& item)
48 {
49 std::scoped_lock lock(_lock);
50 _queue.push_back(std::move(item));
51 }
52
54 template<std::input_iterator Iterator>
55 void readd(Iterator begin, Iterator end)
56 {
57 std::scoped_lock lock(_lock);
58 _queue.insert(_queue.begin(), begin, end);
59 }
60
62 bool next(T& result)
63 {
64 std::scoped_lock lock(_lock);
65 if (_queue.empty())
66 return false;
67
68 result = std::move(_queue.front());
69 _queue.pop_front();
70 return true;
71 }
72
73 template<class Checker>
74 bool next(T& result, Checker& check)
75 {
76 std::scoped_lock lock(_lock);
77 if (_queue.empty())
78 return false;
79
80 decltype(auto) front = _queue.front();
81 if (!check.Process(front))
82 return false;
83
84 result = std::move(front);
85 _queue.pop_front();
86 return true;
87 }
88
90 void cancel()
91 {
92 std::scoped_lock lock(_lock);
93 _canceled = true;
94 }
95
97 bool cancelled()
98 {
99 std::scoped_lock lock(_lock);
100 return _canceled;
101 }
102
105 {
106 std::scoped_lock lock(_lock);
107 _queue.pop_front();
108 }
109
111 bool empty()
112 {
113 std::scoped_lock lock(_lock);
114 return _queue.empty();
115 }
116};
117#endif
bool _canceled
Cancellation flag.
Definition LockedQueue.h:35
void add(T const &item)
Adds an item to the queue.
Definition LockedQueue.h:40
void readd(Iterator begin, Iterator end)
Adds items back to front of the queue.
Definition LockedQueue.h:55
StorageType _queue
Storage backing the queue.
Definition LockedQueue.h:32
std::mutex _lock
Lock access to the queue.
Definition LockedQueue.h:29
bool cancelled()
Checks if the queue is cancelled.
Definition LockedQueue.h:97
bool empty()
! Checks if we're empty or not with locks held
void add(T &&item)
Adds an item to the queue.
Definition LockedQueue.h:47
void pop_front()
! Calls pop_front of the queue
void cancel()
Cancels the queue.
Definition LockedQueue.h:90
bool next(T &result)
Gets the next result in the queue, if any.
Definition LockedQueue.h:62
bool next(T &result, Checker &check)
Definition LockedQueue.h:74