Skip to content

Commit

Permalink
fix(Core/Threading): Refactored LockedQueue / MPSCQueue - Improve thr…
Browse files Browse the repository at this point in the history
…ead safety, performance, and memory management (#21127)
  • Loading branch information
nl-saw authored Jan 23, 2025
1 parent 177cd47 commit 3652240
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 88 deletions.
128 changes: 70 additions & 58 deletions src/common/Threading/LockedQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,74 +18,92 @@
#ifndef LOCKEDQUEUE_H
#define LOCKEDQUEUE_H

#include <atomic>
#include <deque>
#include <memory>
#include <mutex>

template <class T, typename StorageType = std::deque<T>>
class LockedQueue
{
//! Lock access to the queue.
std::mutex _lock;
mutable std::mutex _lock; ///< Mutex to protect access to the queue

//! Storage backing the queue.
StorageType _queue;
std::atomic<bool> _canceled{false}; ///< Flag indicating if the queue is canceled

//! Cancellation flag.
volatile bool _canceled{false};
StorageType _queue; ///< Storage container for the queue

public:

//! Create a LockedQueue.
/**
* @brief Default constructor to create an empty LockedQueue.
*/
LockedQueue() = default;

//! Destroy a LockedQueue.
/**
* @brief Destructor for LockedQueue.
*/
virtual ~LockedQueue() = default;

//! Adds an item to the queue.
/**
* @brief Adds an item to the back of the queue.
*
* @param item The item to be added to the queue.
*/
void add(const T& item)
{
lock();

_queue.push_back(item);

unlock();
std::lock_guard<std::mutex> lock(_lock);
_queue.push_back(std::move(item));
}

//! Adds items back to front of the queue
/**
* @brief Adds a range of items to the front of the queue.
*
* @param begin Iterator pointing to the beginning of the range of items to be added.
* @param end Iterator pointing to the end of the range of items to be added.
*/
template<class Iterator>
void readd(Iterator begin, Iterator end)
{
std::lock_guard<std::mutex> lock(_lock);
_queue.insert(_queue.begin(), begin, end);
}

//! Gets the next result in the queue, if any.
/**
* @brief Gets the next item in the queue and removes it.
*
* @param result The variable where the next item will be stored.
* @return true if an item was retrieved and removed, false if the queue is empty.
*/
bool next(T& result)
{
std::lock_guard<std::mutex> lock(_lock);

if (_queue.empty())
{
return false;
}

result = _queue.front();
result = std::move(_queue.front());
_queue.pop_front();

return true;
}

/**
* @brief Retrieves the next item from the queue if it satisfies the provided checker.
*
* @param result The variable where the next item will be stored.
* @param check A checker object that will be used to validate the item.
* @return true if an item was retrieved, checked, and removed; false otherwise.
*/
template<class Checker>
bool next(T& result, Checker& check)
{
std::lock_guard<std::mutex> lock(_lock);

if (_queue.empty())
{
return false;
}

result = _queue.front();
result = std::move(_queue.front());
if (!check.Process(result))
{
return false;
Expand All @@ -95,60 +113,54 @@ class LockedQueue
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()
/**
* @brief Peeks at the top of the queue without removing it.
*
* @return A reference to the item at the front of the queue, assuming there's an item in the queue (as per previous implementation)
*/
T& peek()
{
std::lock_guard<std::mutex> lock(_lock);

_canceled = true;
return _queue.front();
}

//! Checks if the queue is cancelled.
bool cancelled()
/**
* @brief Cancels the queue, preventing further processing of items.
*/
void cancel()
{
std::lock_guard<std::mutex> lock(_lock);
return _canceled;
_canceled.store(true, std::memory_order_release);
}

//! Locks the queue for access.
void lock()
/**
* @brief Checks if the queue has been canceled.
*
* @return true if the queue is canceled, false otherwise.
*/
bool cancelled() const
{
this->_lock.lock();
return _canceled.load(std::memory_order_acquire);
}

//! Unlocks the queue.
void unlock()
/**
* @brief Checks if the queue is empty.
*
* @return true if the queue is empty, false otherwise.
*/
bool empty() const
{
this->_lock.unlock();
std::lock_guard<std::mutex> lock(_lock);
return _queue.empty();
}

///! Calls pop_front of the queue
/**
* @brief Removes the item at the front of the queue.
*/
void pop_front()
{
std::lock_guard<std::mutex> lock(_lock);
_queue.pop_front();
}

///! Checks if we're empty or not with locks held
bool empty()
{
std::lock_guard<std::mutex> lock(_lock);
return _queue.empty();
}
};

#endif
Loading

0 comments on commit 3652240

Please sign in to comment.