mirror of
https://github.com/mod-playerbots/azerothcore-wotlk.git
synced 2026-01-17 02:50:29 +00:00
feat(Core/Threading): replace ace threading (#4821)
This commit is contained in:
@@ -7,137 +7,134 @@
|
||||
#ifndef LOCKEDQUEUE_H
|
||||
#define LOCKEDQUEUE_H
|
||||
|
||||
#include "Debugging/Errors.h"
|
||||
#include <ace/Guard_T.h>
|
||||
#include <ace/Thread_Mutex.h>
|
||||
#include <cassert>
|
||||
#include <deque>
|
||||
#include <mutex>
|
||||
|
||||
namespace ACE_Based
|
||||
template <class T, typename StorageType = std::deque<T> >
|
||||
class LockedQueue
|
||||
{
|
||||
template <class T, class LockType, typename StorageType = std::deque<T>>
|
||||
class LockedQueue
|
||||
//! Lock access to the queue.
|
||||
std::mutex _lock;
|
||||
|
||||
//! Storage backing the queue.
|
||||
StorageType _queue;
|
||||
|
||||
//! Cancellation flag.
|
||||
volatile bool _canceled;
|
||||
|
||||
public:
|
||||
|
||||
//! Create a LockedQueue.
|
||||
LockedQueue()
|
||||
: _canceled(false)
|
||||
{
|
||||
//! Lock access to the queue.
|
||||
LockType _lock;
|
||||
}
|
||||
|
||||
//! Storage backing the queue.
|
||||
StorageType _queue;
|
||||
//! Destroy a LockedQueue.
|
||||
virtual ~LockedQueue()
|
||||
{
|
||||
}
|
||||
|
||||
//! Cancellation flag.
|
||||
volatile bool _canceled{false};
|
||||
//! Adds an item to the queue.
|
||||
void add(const T& item)
|
||||
{
|
||||
lock();
|
||||
|
||||
public:
|
||||
//! Create a LockedQueue.
|
||||
LockedQueue()
|
||||
_queue.push_back(item);
|
||||
|
||||
{
|
||||
}
|
||||
unlock();
|
||||
}
|
||||
|
||||
//! Destroy a LockedQueue.
|
||||
virtual ~LockedQueue() = default;
|
||||
//! Adds items back to front of the queue
|
||||
template<class Iterator>
|
||||
void readd(Iterator begin, Iterator end)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
_queue.insert(_queue.begin(), begin, end);
|
||||
}
|
||||
|
||||
//! Adds an item to the queue.
|
||||
void add(const T& item)
|
||||
{
|
||||
lock();
|
||||
//! Gets the next result in the queue, if any.
|
||||
bool next(T& result)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
|
||||
//ASSERT(!this->_canceled);
|
||||
// throw Cancellation_Exception();
|
||||
if (_queue.empty())
|
||||
return false;
|
||||
|
||||
_queue.push_back(item);
|
||||
result = _queue.front();
|
||||
_queue.pop_front();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<class Checker>
|
||||
bool next(T& result, Checker& check)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
|
||||
if (_queue.empty())
|
||||
return false;
|
||||
|
||||
result = _queue.front();
|
||||
if (!check.Process(result))
|
||||
return false;
|
||||
|
||||
_queue.pop_front();
|
||||
return true;
|
||||
}
|
||||
|
||||
//! Peeks at the top of the queue. Check if the queue is empty before calling! Remember to unlock after use if autoUnlock == false.
|
||||
T& peek(bool autoUnlock = false)
|
||||
{
|
||||
lock();
|
||||
|
||||
T& result = _queue.front();
|
||||
|
||||
if (autoUnlock)
|
||||
unlock();
|
||||
}
|
||||
|
||||
//! Gets the next result in the queue, if any.
|
||||
bool next(T& result)
|
||||
{
|
||||
// ACE_Guard<LockType> g(this->_lock);
|
||||
ACE_GUARD_RETURN (LockType, g, this->_lock, false);
|
||||
return result;
|
||||
}
|
||||
|
||||
if (_queue.empty())
|
||||
return false;
|
||||
//! Cancels the queue.
|
||||
void cancel()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
|
||||
//ASSERT (!_queue.empty() || !this->_canceled);
|
||||
// throw Cancellation_Exception();
|
||||
result = _queue.front();
|
||||
_queue.pop_front();
|
||||
_canceled = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//! Checks if the queue is cancelled.
|
||||
bool cancelled()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
return _canceled;
|
||||
}
|
||||
|
||||
template<class Checker>
|
||||
bool next(T& result, Checker& check)
|
||||
{
|
||||
ACE_Guard<LockType> g(this->_lock);
|
||||
//! Locks the queue for access.
|
||||
void lock()
|
||||
{
|
||||
this->_lock.lock();
|
||||
}
|
||||
|
||||
if (_queue.empty())
|
||||
return false;
|
||||
//! Unlocks the queue.
|
||||
void unlock()
|
||||
{
|
||||
this->_lock.unlock();
|
||||
}
|
||||
|
||||
result = _queue.front();
|
||||
if (!check.Process(result))
|
||||
return false;
|
||||
///! Calls pop_front of the queue
|
||||
void pop_front()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
_queue.pop_front();
|
||||
}
|
||||
|
||||
_queue.pop_front();
|
||||
return true;
|
||||
}
|
||||
|
||||
//! Peeks at the top of the queue. Check if the queue is empty before calling! Remember to unlock after use if autoUnlock == false.
|
||||
T& peek(bool autoUnlock = false)
|
||||
{
|
||||
lock();
|
||||
|
||||
T& result = _queue.front();
|
||||
|
||||
if (autoUnlock)
|
||||
unlock();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//! Cancels the queue.
|
||||
void cancel()
|
||||
{
|
||||
lock();
|
||||
|
||||
_canceled = true;
|
||||
|
||||
unlock();
|
||||
}
|
||||
|
||||
//! Checks if the queue is cancelled.
|
||||
bool cancelled()
|
||||
{
|
||||
ACE_Guard<LockType> g(this->_lock);
|
||||
return _canceled;
|
||||
}
|
||||
|
||||
//! Locks the queue for access.
|
||||
void lock()
|
||||
{
|
||||
this->_lock.acquire();
|
||||
}
|
||||
|
||||
//! Unlocks the queue.
|
||||
void unlock()
|
||||
{
|
||||
this->_lock.release();
|
||||
}
|
||||
|
||||
///! Calls pop_front of the queue
|
||||
void pop_front()
|
||||
{
|
||||
ACE_GUARD (LockType, g, this->_lock);
|
||||
_queue.pop_front();
|
||||
}
|
||||
|
||||
///! Checks if we're empty or not with locks held
|
||||
bool empty()
|
||||
{
|
||||
ACE_GUARD_RETURN (LockType, g, this->_lock, false);
|
||||
return _queue.empty();
|
||||
}
|
||||
};
|
||||
}
|
||||
///! Checks if we're empty or not with locks held
|
||||
bool empty()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_lock);
|
||||
return _queue.empty();
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user