Merge branch 'barrier' into 'master'

Do not store callback inside Misc::Barrier

See merge request OpenMW/openmw!821
pull/593/head
Frederic Chardon 4 years ago
commit caf382c19f

@ -177,43 +177,11 @@ namespace MWPhysics
mDeferAabbUpdate = false;
}
mPreStepBarrier = std::make_unique<Misc::Barrier>(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<Misc::Barrier>(mNumThreads);
mPostStepBarrier = std::make_unique<Misc::Barrier>(mNumThreads, [&]()
{
if (mRemainingSteps)
{
--mRemainingSteps;
updateActorsPositions();
}
mNextJob.store(0, std::memory_order_release);
});
mPostStepBarrier = std::make_unique<Misc::Barrier>(mNumThreads);
mPostSimBarrier = std::make_unique<Misc::Barrier>(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<Misc::Barrier>(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();
}
}

@ -66,6 +66,9 @@ namespace MWPhysics
void updatePtrAabb(const std::weak_ptr<PtrHolder>& ptr);
void updateStats(osg::Timer_t frameStart, unsigned int frameNumber, osg::Stats& stats);
std::tuple<int, float> calculateStepConfig(float timeAccum) const;
void afterPreStep();
void afterPostStep();
void afterPostSim();
std::unique_ptr<WorldFrameData> mWorldFrameData;
std::vector<ActorFrameData> mActorsFrameData;

@ -2,7 +2,6 @@
#define OPENMW_BARRIER_H
#include <condition_variable>
#include <functional>
#include <mutex>
namespace Misc
@ -11,15 +10,14 @@ namespace Misc
class Barrier
{
public:
using BarrierCallback = std::function<void(void)>;
/// @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<BarrierCallback>(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 <class Callback>
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;
};
}

Loading…
Cancel
Save