1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-30 03:32:36 +00:00
OpenMW/apps/openmw/mwlua/worker.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

100 lines
2.6 KiB
C++
Raw Normal View History

2022-10-06 01:45:45 +02:00
#include "worker.hpp"
#include "luamanagerimp.hpp"
#include "apps/openmw/profile.hpp"
2022-10-06 01:45:45 +02:00
2023-07-31 15:58:39 +04:00
#include <components/debug/debuglog.hpp>
#include <components/settings/values.hpp>
2022-10-06 01:45:45 +02:00
#include <cassert>
2022-10-06 01:45:45 +02:00
namespace MWLua
{
Worker::Worker(LuaManager& manager)
2022-10-06 01:45:45 +02:00
: mManager(manager)
{
if (Settings::lua().mLuaNumThreads > 0)
2022-10-06 01:45:45 +02:00
mThread = std::thread([this] { run(); });
}
Worker::~Worker()
{
if (mThread && mThread->joinable())
{
Log(Debug::Error)
<< "Unexpected destruction of LuaWorker; likely there is an unhandled exception in the main thread.";
join();
}
}
void Worker::allowUpdate(osg::Timer_t frameStart, unsigned frameNumber, osg::Stats& stats)
2022-10-06 01:45:45 +02:00
{
if (!mThread)
return;
{
std::lock_guard<std::mutex> lk(mMutex);
mUpdateRequest = UpdateRequest{ .mFrameStart = frameStart, .mFrameNumber = frameNumber, .mStats = &stats };
2022-10-06 01:45:45 +02:00
}
mCV.notify_one();
}
void Worker::finishUpdate(osg::Timer_t frameStart, unsigned frameNumber, osg::Stats& stats)
2022-10-06 01:45:45 +02:00
{
if (mThread)
{
std::unique_lock<std::mutex> lk(mMutex);
mCV.wait(lk, [&] { return !mUpdateRequest.has_value(); });
2022-10-06 01:45:45 +02:00
}
else
update(frameStart, frameNumber, stats);
2022-10-06 01:45:45 +02:00
}
void Worker::join()
{
if (mThread)
{
{
std::lock_guard<std::mutex> lk(mMutex);
mJoinRequest = true;
}
mCV.notify_one();
mThread->join();
}
}
void Worker::update(osg::Timer_t frameStart, unsigned frameNumber, osg::Stats& stats)
2022-10-06 01:45:45 +02:00
{
const osg::Timer* const timer = osg::Timer::instance();
OMW::ScopedProfile<OMW::UserStatsType::Lua> profile(frameStart, frameNumber, *timer, stats);
2022-10-06 01:45:45 +02:00
mManager.update();
}
void Worker::run() noexcept
{
while (true)
{
std::unique_lock<std::mutex> lk(mMutex);
mCV.wait(lk, [&] { return mUpdateRequest.has_value() || mJoinRequest; });
2022-10-06 01:45:45 +02:00
if (mJoinRequest)
break;
assert(mUpdateRequest.has_value());
2023-07-31 15:58:39 +04:00
try
{
update(mUpdateRequest->mFrameStart, mUpdateRequest->mFrameNumber, *mUpdateRequest->mStats);
2023-07-31 15:58:39 +04:00
}
catch (const std::exception& e)
2023-07-31 15:58:39 +04:00
{
Log(Debug::Error) << "Failed to update LuaManager: " << e.what();
}
2022-10-06 01:45:45 +02:00
mUpdateRequest.reset();
2022-10-06 01:45:45 +02:00
lk.unlock();
mCV.notify_one();
}
}
}