#include <dali/devel-api/common/stage-devel.h>
#include <dali/devel-api/update/frame-callback-interface.h>
+thread_local int gLocked{0};
+
namespace Dali::Toolkit::Physics::Internal
{
/**
Dali::DevelStage::RemoveFrameCallback(Dali::Stage::GetCurrent(), *mFrameCallback);
}
-Dali::Mutex& PhysicsWorld::GetMutex()
-{
- return mMutex;
-}
-
bool PhysicsWorld::OnUpdate(Dali::UpdateProxy& updateProxy, float elapsedSeconds)
{
- Dali::Mutex::ScopedLock lock(mMutex);
+ ScopedLock lock(*this);
// Process command queue
if(mNotifySyncPoint != Dali::UpdateProxy::INVALID_SYNC &&
return mPhysicsTimeStep;
}
-void PhysicsWorld::Queue(std::function<void(void)> function)
+/**
+ * Lock the mutex.
+ */
+void PhysicsWorld::Lock()
{
- if(!mMutex.IsLocked()) // Annoyingly, the dali mutex scoped lock doesn't prevent relocking in the same thread.
- {
- Dali::Mutex::ScopedLock lock(mMutex);
- commandQueue.push(function);
- }
- else
+ //@todo Could replace the mutex with an atomic flag, if it's not set,
+ // the queue and integration step could be skipped
+ if(!gLocked)
{
- commandQueue.push(function);
+ gLocked = true;
+ mMutex.lock();
}
}
+/**
+ * Unlock the mutex
+ */
+void PhysicsWorld::Unlock()
+{
+ mMutex.unlock();
+ gLocked = false;
+}
+
+void PhysicsWorld::Queue(std::function<void(void)> function)
+{
+ ScopedLock lock(*this);
+ commandQueue.push(function);
+}
+
void PhysicsWorld::CreateSyncPoint()
{
mNotifySyncPoint = Dali::DevelStage::NotifyFrameCallback(Dali::Stage::GetCurrent(), *mFrameCallback);