Files
ashlar/components/sceneutil/workqueue.cpp
T
Bret Curtis 28b8274aee Own work queue items with std::shared_ptr instead of osg::Referenced
SceneUtil::WorkQueue is a thread pool with no scene graph in it, so neither it
nor WorkItem needs an OSG base class to be reference counted; both now use
std::shared_ptr and workqueue.hpp, workqueue.cpp and generatenavmeshtile.hpp
end up with no reference to osg at all. Because osg::ref_ptr can re-adopt a raw
pointer and std::shared_ptr cannot without minting a second control block, the
places that passed the queue around as WorkQueue* now pass const
std::shared_ptr<WorkQueue>& instead, which is RenderingManager::getWorkQueue
and the World, WindowManager, MapWindow and GlobalMap constructors. The
non-owning WorkQueue& parameters in UnrefQueue, WorkThread and navmeshtool are
unchanged, and WorkItem gains the virtual destructor it used to inherit.
2026-09-20 12:33:25 +02:00

146 lines
3.1 KiB
C++

#include "workqueue.hpp"
#include <components/debug/debuglog.hpp>
#include <numeric>
namespace SceneUtil
{
void WorkItem::waitTillDone()
{
if (mDone)
return;
std::unique_lock<std::mutex> lock(mMutex);
while (!mDone)
{
mCondition.wait(lock);
}
}
void WorkItem::signalDone()
{
{
std::unique_lock<std::mutex> lock(mMutex);
mDone = true;
}
mCondition.notify_all();
}
bool WorkItem::isDone() const
{
return mDone;
}
WorkQueue::WorkQueue(std::size_t workerThreads)
: mIsReleased(false)
{
start(workerThreads);
}
WorkQueue::~WorkQueue()
{
stop();
}
void WorkQueue::start(std::size_t workerThreads)
{
{
const std::lock_guard lock(mMutex);
mIsReleased = false;
}
while (mThreads.size() < workerThreads)
mThreads.emplace_back(std::make_unique<WorkThread>(*this));
}
void WorkQueue::stop()
{
{
std::unique_lock<std::mutex> lock(mMutex);
while (!mQueue.empty())
mQueue.pop_back();
mIsReleased = true;
mCondition.notify_all();
}
mThreads.clear();
}
void WorkQueue::addWorkItem(std::shared_ptr<WorkItem> item, bool front)
{
if (item->isDone())
{
Log(Debug::Error) << "Error: trying to add a work item that is already completed";
return;
}
std::unique_lock<std::mutex> lock(mMutex);
if (front)
mQueue.push_front(std::move(item));
else
mQueue.push_back(std::move(item));
mCondition.notify_one();
}
std::shared_ptr<WorkItem> WorkQueue::removeWorkItem()
{
std::unique_lock<std::mutex> lock(mMutex);
while (mQueue.empty() && !mIsReleased)
{
mCondition.wait(lock);
}
if (!mQueue.empty())
{
std::shared_ptr<WorkItem> item = std::move(mQueue.front());
mQueue.pop_front();
return item;
}
return nullptr;
}
size_t WorkQueue::getNumItems() const
{
std::unique_lock<std::mutex> lock(mMutex);
return mQueue.size();
}
size_t WorkQueue::getNumActiveThreads() const
{
return std::accumulate(
mThreads.begin(), mThreads.end(), 0u, [](auto r, const auto& t) { return r + t->isActive(); });
}
WorkThread::WorkThread(WorkQueue& workQueue)
: mWorkQueue(&workQueue)
, mActive(false)
, mThread([this] { run(); })
{
}
WorkThread::~WorkThread()
{
mThread.join();
}
void WorkThread::run()
{
while (true)
{
std::shared_ptr<WorkItem> item = mWorkQueue->removeWorkItem();
if (!item)
return;
mActive = true;
item->doWork();
item->signalDone();
mActive = false;
}
}
bool WorkThread::isActive() const
{
return mActive;
}
}