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
 Default constructor to create an empty LockedQueue.
 
virtual ~LockedQueue ()=default
 Destructor for LockedQueue.
 
void add (const T &item)
 Adds an item to the back of the queue.
 
template<class Iterator >
void readd (Iterator begin, Iterator end)
 Adds a range of items to the front of the queue.
 
bool next (T &result)
 Gets the next item in the queue and removes it.
 
template<class Checker >
bool next (T &result, Checker &check)
 Retrieves the next item from the queue if it satisfies the provided checker.
 
T & peek ()
 Peeks at the top of the queue without removing it.
 
void cancel ()
 Cancels the queue, preventing further processing of items.
 
bool cancelled () const
 Checks if the queue has been canceled.
 
bool empty () const
 Checks if the queue is empty.
 
void pop_front ()
 Removes the item at the front of the queue.
 

Private Attributes

std::mutex _lock
 Mutex to protect access to the queue.
 
std::atomic< bool > _canceled {false}
 Flag indicating if the queue is canceled.
 
StorageType _queue
 Storage container for the queue.
 

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

Default constructor to create an empty LockedQueue.

◆ ~LockedQueue()

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

Destructor for 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 back of the queue.

Parameters
itemThe item to be added to the queue.
53 {
54 std::lock_guard<std::mutex> lock(_lock);
55 _queue.push_back(std::move(item));
56 }
StorageType _queue
Storage container for the queue.
Definition LockedQueue.h:33
std::mutex _lock
Mutex to protect access to the queue.
Definition LockedQueue.h:29

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

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

◆ cancel()

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

Cancels the queue, preventing further processing of items.

131 {
132 _canceled.store(true, std::memory_order_release);
133 }
std::atomic< bool > _canceled
Flag indicating if the queue is canceled.
Definition LockedQueue.h:31

References LockedQueue< T, StorageType >::_canceled.

◆ cancelled()

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

Checks if the queue has been canceled.

Returns
true if the queue is canceled, false otherwise.
141 {
142 return _canceled.load(std::memory_order_acquire);
143 }

References LockedQueue< T, StorageType >::_canceled.

◆ empty()

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

Checks if the queue is empty.

Returns
true if the queue is empty, false otherwise.
151 {
152 std::lock_guard<std::mutex> lock(_lock);
153 return _queue.empty();
154 }

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

◆ next() [1/2]

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

Gets the next item in the queue and removes it.

Parameters
resultThe variable where the next item will be stored.
Returns
true if an item was retrieved and removed, false if the queue is empty.
78 {
79 std::lock_guard<std::mutex> lock(_lock);
80 if (_queue.empty())
81 {
82 return false;
83 }
84
85 result = std::move(_queue.front());
86 _queue.pop_front();
87 return true;
88 }

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

Referenced by World::ProcessCliCommands(), AuctionHouseWorkerThread::ProcessSearchUpdates(), WorldSession::Update(), WorldSessionMgr::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

Retrieves the next item from the queue if it satisfies the provided checker.

Parameters
resultThe variable where the next item will be stored.
checkA checker object that will be used to validate the item.
Returns
true if an item was retrieved, checked, and removed; false otherwise.
99 {
100 std::lock_guard<std::mutex> lock(_lock);
101 if (_queue.empty())
102 {
103 return false;
104 }
105
106 result = std::move(_queue.front());
107 if (!check.Process(result))
108 {
109 return false;
110 }
111
112 _queue.pop_front();
113 return true;
114 }

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

◆ peek()

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

Peeks at the top of the queue without removing it.

Returns
A reference to the item at the front of the queue, assuming there's an item in the queue (as per previous implementation)
122 {
123 std::lock_guard<std::mutex> lock(_lock);
124 return _queue.front();
125 }

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

◆ pop_front()

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

Removes the item at the front of the queue.

160 {
161 std::lock_guard<std::mutex> lock(_lock);
162 _queue.pop_front();
163 }

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

◆ readd()

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

Adds a range of items to the front of the queue.

Parameters
beginIterator pointing to the beginning of the range of items to be added.
endIterator pointing to the end of the range of items to be added.
66 {
67 std::lock_guard<std::mutex> lock(_lock);
68 _queue.insert(_queue.begin(), begin, end);
69 }

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

Referenced by WorldSession::Update().

Member Data Documentation

◆ _canceled

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

Flag indicating if the queue is canceled.

31{false};

Referenced by LockedQueue< T, StorageType >::cancel(), and LockedQueue< T, StorageType >::cancelled().

◆ _lock

◆ _queue


The documentation for this class was generated from the following file: