TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
LockedQueue< T, StorageType > Class Template Reference

#include <LockedQueue.h>

Public Member Functions

 LockedQueue ()
 Create a LockedQueue. More...
 
virtual ~LockedQueue ()
 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
 Cancellation flag. More...
 

Constructor & Destructor Documentation

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

Create a LockedQueue.

41  : _canceled(false)
42  {
43  }
volatile bool _canceled
Cancellation flag.
Definition: LockedQueue.h:35
template<class T, typename StorageType = std::deque<T>>
virtual LockedQueue< T, StorageType >::~LockedQueue ( )
inlinevirtual

Destroy a LockedQueue.

47  {
48  }

Member Function Documentation

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

Adds an item to the queue.

52  {
53  lock();
54 
55  _queue.push_back(item);
56 
57  unlock();
58  }
void unlock()
Unlocks the queue.
Definition: LockedQueue.h:133
StorageType _queue
Storage backing the queue.
Definition: LockedQueue.h:32
void lock()
Locks the queue for access.
Definition: LockedQueue.h:127

+ Here is the caller graph for this function:

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

Cancels the queue.

113  {
114  std::lock_guard<std::mutex> lock(_lock);
115 
116  _canceled = true;
117  }
void lock()
Locks the queue for access.
Definition: LockedQueue.h:127
volatile bool _canceled
Cancellation flag.
Definition: LockedQueue.h:35
std::mutex _lock
Lock access to the queue.
Definition: LockedQueue.h:29
template<class T, typename StorageType = std::deque<T>>
bool LockedQueue< T, StorageType >::cancelled ( )
inline

Checks if the queue is cancelled.

121  {
122  std::lock_guard<std::mutex> lock(_lock);
123  return _canceled;
124  }
void lock()
Locks the queue for access.
Definition: LockedQueue.h:127
volatile bool _canceled
Cancellation flag.
Definition: LockedQueue.h:35
std::mutex _lock
Lock access to the queue.
Definition: LockedQueue.h:29
template<class T, typename StorageType = std::deque<T>>
bool LockedQueue< T, StorageType >::empty ( )
inline

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

147  {
148  std::lock_guard<std::mutex> lock(_lock);
149  return _queue.empty();
150  }
StorageType _queue
Storage backing the queue.
Definition: LockedQueue.h:32
void lock()
Locks the queue for access.
Definition: LockedQueue.h:127
std::mutex _lock
Lock access to the queue.
Definition: LockedQueue.h:29
template<class T, typename StorageType = std::deque<T>>
void LockedQueue< T, StorageType >::lock ( )
inline

Locks the queue for access.

128  {
129  this->_lock.lock();
130  }
std::mutex _lock
Lock access to the queue.
Definition: LockedQueue.h:29

+ Here is the caller graph for this function:

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.

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  }
StorageType _queue
Storage backing the queue.
Definition: LockedQueue.h:32
void lock()
Locks the queue for access.
Definition: LockedQueue.h:127
std::mutex _lock
Lock access to the queue.
Definition: LockedQueue.h:29

+ Here is the caller graph for this function:

template<class T, typename StorageType = std::deque<T>>
template<class Checker >
bool LockedQueue< T, StorageType >::next ( T &  result,
Checker &  check 
)
inline
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  }
StorageType _queue
Storage backing the queue.
Definition: LockedQueue.h:32
void lock()
Locks the queue for access.
Definition: LockedQueue.h:127
T check(T value)
Definition: format.h:305
std::mutex _lock
Lock access to the queue.
Definition: LockedQueue.h:29
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  unlock();
107 
108  return result;
109  }
void unlock()
Unlocks the queue.
Definition: LockedQueue.h:133
StorageType _queue
Storage backing the queue.
Definition: LockedQueue.h:32
void lock()
Locks the queue for access.
Definition: LockedQueue.h:127
template<class T, typename StorageType = std::deque<T>>
void LockedQueue< T, StorageType >::pop_front ( )
inline

! Calls pop_front of the queue

140  {
141  std::lock_guard<std::mutex> lock(_lock);
142  _queue.pop_front();
143  }
StorageType _queue
Storage backing the queue.
Definition: LockedQueue.h:32
void lock()
Locks the queue for access.
Definition: LockedQueue.h:127
std::mutex _lock
Lock access to the queue.
Definition: LockedQueue.h:29
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.

63  {
64  std::lock_guard<std::mutex> lock(_lock);
65  _queue.insert(_queue.begin(), begin, end);
66  }
StorageType _queue
Storage backing the queue.
Definition: LockedQueue.h:32
void lock()
Locks the queue for access.
Definition: LockedQueue.h:127
std::mutex _lock
Lock access to the queue.
Definition: LockedQueue.h:29

+ Here is the caller graph for this function:

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

Unlocks the queue.

134  {
135  this->_lock.unlock();
136  }
std::mutex _lock
Lock access to the queue.
Definition: LockedQueue.h:29

+ Here is the caller graph for this function:

Member Data Documentation

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

Cancellation flag.

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

Lock access to the queue.

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

Storage backing the queue.


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