mirror of
https://github.com/TES3MP/openmw-tes3mp.git
synced 2025-01-16 19:19:56 +00:00
Merge branch 'barrier' into 'master'
Do not store callback inside Misc::Barrier See merge request OpenMW/openmw!821
This commit is contained in:
commit
caf382c19f
3 changed files with 52 additions and 46 deletions
|
@ -177,43 +177,11 @@ namespace MWPhysics
|
||||||
mDeferAabbUpdate = false;
|
mDeferAabbUpdate = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
mPreStepBarrier = std::make_unique<Misc::Barrier>(mNumThreads, [&]()
|
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);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
mPostStepBarrier = std::make_unique<Misc::Barrier>(mNumThreads, [&]()
|
mPostStepBarrier = std::make_unique<Misc::Barrier>(mNumThreads);
|
||||||
{
|
|
||||||
if (mRemainingSteps)
|
|
||||||
{
|
|
||||||
--mRemainingSteps;
|
|
||||||
updateActorsPositions();
|
|
||||||
}
|
|
||||||
mNextJob.store(0, std::memory_order_release);
|
|
||||||
});
|
|
||||||
|
|
||||||
mPostSimBarrier = 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();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PhysicsTaskScheduler::~PhysicsTaskScheduler()
|
PhysicsTaskScheduler::~PhysicsTaskScheduler()
|
||||||
|
@ -525,7 +493,7 @@ namespace MWPhysics
|
||||||
if (!mNewFrame)
|
if (!mNewFrame)
|
||||||
mHasJob.wait(lock, [&]() { return mQuit || mNewFrame; });
|
mHasJob.wait(lock, [&]() { return mQuit || mNewFrame; });
|
||||||
|
|
||||||
mPreStepBarrier->wait();
|
mPreStepBarrier->wait([this] { afterPreStep(); });
|
||||||
|
|
||||||
int job = 0;
|
int job = 0;
|
||||||
while (mRemainingSteps && (job = mNextJob.fetch_add(1, std::memory_order_relaxed)) < mNumJobs)
|
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)
|
if (!mRemainingSteps)
|
||||||
{
|
{
|
||||||
|
@ -552,7 +520,7 @@ namespace MWPhysics
|
||||||
|
|
||||||
if (mLOSCacheExpiry >= 0)
|
if (mLOSCacheExpiry >= 0)
|
||||||
refreshLOSCache();
|
refreshLOSCache();
|
||||||
mPostSimBarrier->wait();
|
mPostSimBarrier->wait([this] { afterPostSim(); });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -633,4 +601,42 @@ namespace MWPhysics
|
||||||
std::shared_lock lock(mCollisionWorldMutex);
|
std::shared_lock lock(mCollisionWorldMutex);
|
||||||
mDebugDrawer->step();
|
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 updatePtrAabb(const std::weak_ptr<PtrHolder>& ptr);
|
||||||
void updateStats(osg::Timer_t frameStart, unsigned int frameNumber, osg::Stats& stats);
|
void updateStats(osg::Timer_t frameStart, unsigned int frameNumber, osg::Stats& stats);
|
||||||
std::tuple<int, float> calculateStepConfig(float timeAccum) const;
|
std::tuple<int, float> calculateStepConfig(float timeAccum) const;
|
||||||
|
void afterPreStep();
|
||||||
|
void afterPostStep();
|
||||||
|
void afterPostSim();
|
||||||
|
|
||||||
std::unique_ptr<WorldFrameData> mWorldFrameData;
|
std::unique_ptr<WorldFrameData> mWorldFrameData;
|
||||||
std::vector<ActorFrameData> mActorsFrameData;
|
std::vector<ActorFrameData> mActorsFrameData;
|
||||||
|
|
|
@ -2,7 +2,6 @@
|
||||||
#define OPENMW_BARRIER_H
|
#define OPENMW_BARRIER_H
|
||||||
|
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <functional>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
namespace Misc
|
namespace Misc
|
||||||
|
@ -11,15 +10,14 @@ namespace Misc
|
||||||
class Barrier
|
class Barrier
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using BarrierCallback = std::function<void(void)>;
|
|
||||||
/// @param count number of threads to wait on
|
/// @param count number of threads to wait on
|
||||||
/// @param func callable to be executed once after all threads have met
|
explicit Barrier(int count) : mThreadCount(count), mRendezvousCount(0), mGeneration(0)
|
||||||
Barrier(int count, BarrierCallback&& func) : mThreadCount(count), mRendezvousCount(0), mGeneration(0)
|
|
||||||
, mFunc(std::forward<BarrierCallback>(func))
|
|
||||||
{}
|
{}
|
||||||
|
|
||||||
/// @brief stop execution of threads until count distinct threads reach this point
|
/// @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);
|
std::unique_lock lock(mMutex);
|
||||||
|
|
||||||
|
@ -29,7 +27,7 @@ namespace Misc
|
||||||
{
|
{
|
||||||
++mGeneration;
|
++mGeneration;
|
||||||
mRendezvousCount = 0;
|
mRendezvousCount = 0;
|
||||||
mFunc();
|
func();
|
||||||
mRendezvous.notify_all();
|
mRendezvous.notify_all();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -44,7 +42,6 @@ namespace Misc
|
||||||
int mGeneration;
|
int mGeneration;
|
||||||
mutable std::mutex mMutex;
|
mutable std::mutex mMutex;
|
||||||
std::condition_variable mRendezvous;
|
std::condition_variable mRendezvous;
|
||||||
BarrierCallback mFunc;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue