From 626e0329315e6ec3037210dc0fa00cdd0755754c Mon Sep 17 00:00:00 2001 From: elsid Date: Wed, 5 May 2021 23:25:13 +0200 Subject: [PATCH] Do not store callback inside Misc::Barrier The only wait method can be provided with it so pass it as a template parameter there. --- apps/openmw/mwphysics/mtphysics.cpp | 82 ++++++++++++++++------------- apps/openmw/mwphysics/mtphysics.hpp | 3 ++ components/misc/barrier.hpp | 13 ++--- 3 files changed, 52 insertions(+), 46 deletions(-) diff --git a/apps/openmw/mwphysics/mtphysics.cpp b/apps/openmw/mwphysics/mtphysics.cpp index 95a741825d..82214b6287 100644 --- a/apps/openmw/mwphysics/mtphysics.cpp +++ b/apps/openmw/mwphysics/mtphysics.cpp @@ -177,43 +177,11 @@ namespace MWPhysics mDeferAabbUpdate = false; } - mPreStepBarrier = std::make_unique(mNumThreads, [&]() - { - if (mDeferAabbUpdate) - updateAabbs(); - if (!mRemainingSteps) - return; - for (auto& data : mActorsFrameData) - if (data.mActor.lock()) - { - std::unique_lock lock(mCollisionWorldMutex); - MovementSolver::unstuck(data, mCollisionWorld); - } - }); + mPreStepBarrier = std::make_unique(mNumThreads); - mPostStepBarrier = std::make_unique(mNumThreads, [&]() - { - if (mRemainingSteps) - { - --mRemainingSteps; - updateActorsPositions(); - } - mNextJob.store(0, std::memory_order_release); - }); + mPostStepBarrier = std::make_unique(mNumThreads); - mPostSimBarrier = std::make_unique(mNumThreads, [&]() - { - mNewFrame = false; - if (mLOSCacheExpiry >= 0) - { - std::unique_lock lock(mLOSCacheMutex); - mLOSCache.erase( - std::remove_if(mLOSCache.begin(), mLOSCache.end(), - [](const LOSRequest& req) { return req.mStale; }), - mLOSCache.end()); - } - mTimeEnd = mTimer->tick(); - }); + mPostSimBarrier = std::make_unique(mNumThreads); } PhysicsTaskScheduler::~PhysicsTaskScheduler() @@ -525,7 +493,7 @@ namespace MWPhysics if (!mNewFrame) mHasJob.wait(lock, [&]() { return mQuit || mNewFrame; }); - mPreStepBarrier->wait(); + mPreStepBarrier->wait([this] { afterPreStep(); }); int job = 0; while (mRemainingSteps && (job = mNextJob.fetch_add(1, std::memory_order_relaxed)) < mNumJobs) @@ -537,7 +505,7 @@ namespace MWPhysics } } - mPostStepBarrier->wait(); + mPostStepBarrier->wait([this] { afterPostStep(); }); if (!mRemainingSteps) { @@ -552,7 +520,7 @@ namespace MWPhysics if (mLOSCacheExpiry >= 0) refreshLOSCache(); - mPostSimBarrier->wait(); + mPostSimBarrier->wait([this] { afterPostSim(); }); } } } @@ -633,4 +601,42 @@ namespace MWPhysics std::shared_lock lock(mCollisionWorldMutex); mDebugDrawer->step(); } + + void PhysicsTaskScheduler::afterPreStep() + { + if (mDeferAabbUpdate) + updateAabbs(); + if (!mRemainingSteps) + return; + for (auto& data : mActorsFrameData) + if (data.mActor.lock()) + { + std::unique_lock lock(mCollisionWorldMutex); + MovementSolver::unstuck(data, mCollisionWorld); + } + } + + void PhysicsTaskScheduler::afterPostStep() + { + if (mRemainingSteps) + { + --mRemainingSteps; + updateActorsPositions(); + } + mNextJob.store(0, std::memory_order_release); + } + + void PhysicsTaskScheduler::afterPostSim() + { + mNewFrame = false; + if (mLOSCacheExpiry >= 0) + { + std::unique_lock lock(mLOSCacheMutex); + mLOSCache.erase( + std::remove_if(mLOSCache.begin(), mLOSCache.end(), + [](const LOSRequest& req) { return req.mStale; }), + mLOSCache.end()); + } + mTimeEnd = mTimer->tick(); + } } diff --git a/apps/openmw/mwphysics/mtphysics.hpp b/apps/openmw/mwphysics/mtphysics.hpp index 6d2c392c09..3fd4d5a693 100644 --- a/apps/openmw/mwphysics/mtphysics.hpp +++ b/apps/openmw/mwphysics/mtphysics.hpp @@ -66,6 +66,9 @@ namespace MWPhysics void updatePtrAabb(const std::weak_ptr& ptr); void updateStats(osg::Timer_t frameStart, unsigned int frameNumber, osg::Stats& stats); std::tuple calculateStepConfig(float timeAccum) const; + void afterPreStep(); + void afterPostStep(); + void afterPostSim(); std::unique_ptr mWorldFrameData; std::vector mActorsFrameData; diff --git a/components/misc/barrier.hpp b/components/misc/barrier.hpp index a5af9f5650..b3fe944b04 100644 --- a/components/misc/barrier.hpp +++ b/components/misc/barrier.hpp @@ -2,7 +2,6 @@ #define OPENMW_BARRIER_H #include -#include #include namespace Misc @@ -11,15 +10,14 @@ namespace Misc class Barrier { public: - using BarrierCallback = std::function; /// @param count number of threads to wait on - /// @param func callable to be executed once after all threads have met - Barrier(int count, BarrierCallback&& func) : mThreadCount(count), mRendezvousCount(0), mGeneration(0) - , mFunc(std::forward(func)) + explicit Barrier(int count) : mThreadCount(count), mRendezvousCount(0), mGeneration(0) {} /// @brief stop execution of threads until count distinct threads reach this point - void wait() + /// @param func callable to be executed once after all threads have met + template + void wait(Callback&& func) { std::unique_lock lock(mMutex); @@ -29,7 +27,7 @@ namespace Misc { ++mGeneration; mRendezvousCount = 0; - mFunc(); + func(); mRendezvous.notify_all(); } else @@ -44,7 +42,6 @@ namespace Misc int mGeneration; mutable std::mutex mMutex; std::condition_variable mRendezvous; - BarrierCallback mFunc; }; }