AzerothCore 3.3.5a
OpenSource WoW Emulator
Loading...
Searching...
No Matches
LockedQueue< T, StorageType > Class Template Reference

#include "LockedQueue.h"

Public Member Functions

 LockedQueue ()=default
 Create a LockedQueue. More...
 
virtual ~LockedQueue ()=default
 Destroy a LockedQueue. More...
 
void add (const T &item)
 Adds an item to the queue. More...
 
template<class Iterator >
void readd (Iterator begin, Iterator end)
 Adds items back to front of the queue. More...
 
bool next (T &result)
 Gets the next result in the queue, if any. More...
 
template<class Checker >
bool next (T &result, Checker &check)
 
T & peek (bool autoUnlock=false)
 Peeks at the top of the queue. Check if the queue is empty before calling! Remember to unlock after use if autoUnlock == false. More...
 
void cancel ()
 Cancels the queue. More...
 
bool cancelled ()
 Checks if the queue is cancelled. More...
 
void lock ()
 Locks the queue for access. More...
 
void unlock ()
 Unlocks the queue. More...
 
void pop_front ()
 ! Calls pop_front of the queue More...
 
bool empty ()
 ! Checks if we're empty or not with locks held More...
 

Private Attributes

std::mutex _lock
 Lock access to the queue. More...
 
StorageType _queue
 Storage backing the queue. More...
 
volatile bool _canceled {false}
 Cancellation flag. More...
 

Detailed Description

template<class T, typename StorageType = std::deque<T>>
class LockedQueue< T, StorageType >

Constructor & Destructor Documentation

◆ LockedQueue()

template<class T , typename StorageType = std::deque<T>>
LockedQueue< T, StorageType >::LockedQueue ( )
default

Create a LockedQueue.

◆ ~LockedQueue()

template<class T , typename StorageType = std::deque<T>>
virtual LockedQueue< T, StorageType >::~LockedQueue ( )
virtualdefault

Destroy a LockedQueue.

Member Function Documentation

◆ add()

template<class T , typename StorageType = std::deque<T>>
void LockedQueue< T, StorageType >::add ( const T &  item)
inline

Adds an item to the queue.

46 {
47 lock();
48
49 _queue.push_back(item);
50
51 unlock();
52 }
void unlock()
Unlocks the queue.
Definition: LockedQueue.h:135
StorageType _queue
Storage backing the queue.
Definition: LockedQueue.h:31
void lock()
Locks the queue for access.
Definition: LockedQueue.h:129

References LockedQueue< T, StorageType >::_queue, LockedQueue< T, StorageType >::lock(), and LockedQueue< T, StorageType >::unlock().

Referenced by World::AddSession(), World::QueueCliCommand(), and WorldSession::QueuePacket().

◆ cancel()

template<class T , typename StorageType = std::deque<T>>
void LockedQueue< T, StorageType >::cancel ( )
inline

Cancels the queue.

115 {
116 std::lock_guard<std::mutex> lock(_lock);
117
118 _canceled = true;
119 }
volatile bool _canceled
Cancellation flag.
Definition: LockedQueue.h:34
std::mutex _lock
Lock access to the queue.
Definition: LockedQueue.h:28

References LockedQueue< T, StorageType >::_canceled, LockedQueue< T, StorageType >::_lock, and LockedQueue< T, StorageType >::lock().

◆ cancelled()

template<class T , typename StorageType = std::deque<T>>
bool LockedQueue< T, StorageType >::cancelled ( )
inline

Checks if the queue is cancelled.

123 {
124 std::lock_guard<std::mutex> lock(_lock);
125 return _canceled;
126 }

References LockedQueue< T, StorageType >::_canceled, LockedQueue< T, StorageType >::_lock, and LockedQueue< T, StorageType >::lock().

◆ empty()

template<class T , typename StorageType = std::deque<T>>
bool LockedQueue< T, StorageType >::empty ( )
inline

! Checks if we're empty or not with locks held

149 {
150 std::lock_guard<std::mutex> lock(_lock);
151 return _queue.empty();
152 }

References LockedQueue< T, StorageType >::_lock, LockedQueue< T, StorageType >::_queue, and LockedQueue< T, StorageType >::lock().

◆ lock()

◆ next() [1/2]

template<class T , typename StorageType = std::deque<T>>
bool LockedQueue< T, StorageType >::next ( T &  result)
inline

Gets the next result in the queue, if any.

64 {
65 std::lock_guard<std::mutex> lock(_lock);
66
67 if (_queue.empty())
68 {
69 return false;
70 }
71
72 result = _queue.front();
73 _queue.pop_front();
74
75 return true;
76 }

References LockedQueue< T, StorageType >::_lock, LockedQueue< T, StorageType >::_queue, and LockedQueue< T, StorageType >::lock().

Referenced by World::ProcessCliCommands(), WorldSession::Update(), World::UpdateSessions(), World::~World(), and WorldSession::~WorldSession().

◆ next() [2/2]

template<class T , typename StorageType = std::deque<T>>
template<class Checker >
bool LockedQueue< T, StorageType >::next ( T &  result,
Checker &  check 
)
inline
80 {
81 std::lock_guard<std::mutex> lock(_lock);
82
83 if (_queue.empty())
84 {
85 return false;
86 }
87
88 result = _queue.front();
89 if (!check.Process(result))
90 {
91 return false;
92 }
93
94 _queue.pop_front();
95 return true;
96 }

References LockedQueue< T, StorageType >::_lock, LockedQueue< T, StorageType >::_queue, and LockedQueue< T, StorageType >::lock().

◆ peek()

template<class T , typename StorageType = std::deque<T>>
T & LockedQueue< T, StorageType >::peek ( bool  autoUnlock = false)
inline

Peeks at the top of the queue. Check if the queue is empty before calling! Remember to unlock after use if autoUnlock == false.

100 {
101 lock();
102
103 T& result = _queue.front();
104
105 if (autoUnlock)
106 {
107 unlock();
108 }
109
110 return result;
111 }

References LockedQueue< T, StorageType >::_queue, LockedQueue< T, StorageType >::lock(), and LockedQueue< T, StorageType >::unlock().

◆ pop_front()

template<class T , typename StorageType = std::deque<T>>
void LockedQueue< T, StorageType >::pop_front ( )
inline

! Calls pop_front of the queue

142 {
143 std::lock_guard<std::mutex> lock(_lock);
144 _queue.pop_front();
145 }

References LockedQueue< T, StorageType >::_lock, LockedQueue< T, StorageType >::_queue, and LockedQueue< T, StorageType >::lock().

◆ readd()

template<class T , typename StorageType = std::deque<T>>
template<class Iterator >
void LockedQueue< T, StorageType >::readd ( Iterator  begin,
Iterator  end 
)
inline

Adds items back to front of the queue.

57 {
58 std::lock_guard<std::mutex> lock(_lock);
59 _queue.insert(_queue.begin(), begin, end);
60 }

References LockedQueue< T, StorageType >::_lock, LockedQueue< T, StorageType >::_queue, and LockedQueue< T, StorageType >::lock().

Referenced by WorldSession::Update().

◆ unlock()

template<class T , typename StorageType = std::deque<T>>
void LockedQueue< T, StorageType >::unlock ( )
inline

Unlocks the queue.

136 {
137 this->_lock.unlock();
138 }

Referenced by LockedQueue< T, StorageType >::add(), and LockedQueue< T, StorageType >::peek().

Member Data Documentation

◆ _canceled

template<class T , typename StorageType = std::deque<T>>
volatile bool LockedQueue< T, StorageType >::_canceled {false}
private

◆ _lock

◆ _queue

template<class T , typename StorageType = std::deque<T>>
StorageType LockedQueue< T, StorageType >::_queue
private