1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#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(osg::ref_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();
}
osg::ref_ptr<WorkItem> WorkQueue::removeWorkItem()
{
std::unique_lock<std::mutex> lock(mMutex);
while (mQueue.empty() && !mIsReleased)
{
mCondition.wait(lock);
}
if (!mQueue.empty())
{
osg::ref_ptr<WorkItem> item = std::move(mQueue.front());
mQueue.pop_front();
return item;
}
return nullptr;
}
unsigned int WorkQueue::getNumItems() const
{
std::unique_lock<std::mutex> lock(mMutex);
return mQueue.size();
}
unsigned int 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)
{
osg::ref_ptr<WorkItem> item = mWorkQueue->removeWorkItem();
if (!item)
return;
mActive = true;
item->doWork();
item->signalDone();
mActive = false;
}
}
bool WorkThread::isActive() const
{
return mActive;
}
}
|