diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/task/CpuTaskStatus.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/task/CpuTaskStatus.cpp index d6a08a68d21..25963c58d6e 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/task/CpuTaskStatus.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/task/CpuTaskStatus.cpp @@ -28,7 +28,9 @@ CpuTaskStatus::CpuTaskStatus(): m_busy(0) bool CpuTaskStatus::isBusy() const { - return (m_busy.load(std::memory_order_relaxed) > 0); + // Acquire pairs with the release in setBusy(false): once a thread sees the + // status idle, it also sees everything the finished tasks wrote. + return (m_busy.load(std::memory_order_acquire) > 0); } int CpuTaskStatus::setBusy(bool busy) @@ -39,7 +41,8 @@ int CpuTaskStatus::setBusy(bool busy) } else { - return m_busy.fetch_sub(1, std::memory_order_relaxed); + // Release publishes the task's writes to whoever waits on this status. + return m_busy.fetch_sub(1, std::memory_order_acq_rel); } } } diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/task/DefaultTaskScheduler.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/task/DefaultTaskScheduler.cpp index b25e0e51ad3..6c054fafd7c 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/task/DefaultTaskScheduler.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/task/DefaultTaskScheduler.cpp @@ -58,7 +58,7 @@ DefaultTaskScheduler::DefaultTaskScheduler() { m_isInitialized = false; m_threadCount = 0; - m_isClosing = false; + m_isClosing.store(false, std::memory_order_release); // init global static thread local var { @@ -108,7 +108,7 @@ void DefaultTaskScheduler::start(const unsigned int NbThread ) { stop(); - m_isClosing = false; + m_isClosing.store(false, std::memory_order_release); m_workerThreadsIdle = true; m_mainTaskStatus = nullptr; @@ -137,7 +137,7 @@ void DefaultTaskScheduler::start(const unsigned int NbThread ) void DefaultTaskScheduler::stop() { - m_isClosing = true; + m_isClosing.store(true, std::memory_order_release); if ( m_isInitialized ) { diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/task/DefaultTaskScheduler.h b/Sofa/framework/Simulation/Core/src/sofa/simulation/task/DefaultTaskScheduler.h index 440eb1b63c5..f5bb7b68214 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/task/DefaultTaskScheduler.h +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/task/DefaultTaskScheduler.h @@ -82,7 +82,7 @@ class SOFA_SIMULATION_CORE_API DefaultTaskScheduler : public TaskScheduler bool isInitialized() const { return m_isInitialized; } - bool isClosing() const { return m_isClosing; } + bool isClosing() const { return m_isClosing.load(std::memory_order_acquire); } void WaitForWorkersToBeReady(); @@ -126,7 +126,7 @@ class SOFA_SIMULATION_CORE_API DefaultTaskScheduler : public TaskScheduler std::atomic m_workerThreadsIdle; - bool m_isClosing; + std::atomic m_isClosing; unsigned m_threadCount; diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/task/Task.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/task/Task.cpp index 3a9248ae516..d6393ba7cd0 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/task/Task.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/task/Task.cpp @@ -23,7 +23,7 @@ namespace sofa::simulation { -Task::Allocator* Task::_allocator = nullptr; +std::atomic Task::_allocator { nullptr }; Task::Task(int scheduledThread) : m_scheduledThread(scheduledThread) @@ -33,17 +33,17 @@ Task::Task(int scheduledThread) void *Task::operator new(std::size_t sz) { - return _allocator->allocate(sz); + return _allocator.load(std::memory_order_acquire)->allocate(sz); } void Task::operator delete(void *ptr) { - _allocator->free(ptr, 0); + _allocator.load(std::memory_order_acquire)->free(ptr, 0); } void Task::operator delete(void *ptr, std::size_t sz) { - _allocator->free(ptr, sz); + _allocator.load(std::memory_order_acquire)->free(ptr, sz); } int Task::getScheduledThread() const @@ -53,12 +53,12 @@ int Task::getScheduledThread() const Task::Allocator *Task::getAllocator() { - return _allocator; + return _allocator.load(std::memory_order_acquire); } void Task::setAllocator(Task::Allocator *allocator) { - _allocator = allocator; + _allocator.store(allocator, std::memory_order_release); } } // namespace sofa diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/task/Task.h b/Sofa/framework/Simulation/Core/src/sofa/simulation/task/Task.h index 037de2d6f0d..a7a44f95457 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/task/Task.h +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/task/Task.h @@ -23,6 +23,7 @@ #include +#include #include namespace sofa::simulation @@ -100,7 +101,7 @@ class SOFA_SIMULATION_CORE_API Task private: - static Task::Allocator * _allocator; + static std::atomic _allocator; }; } // namespace sofa::simulation diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/task/WorkerThread.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/task/WorkerThread.cpp index e3e70a3a90e..2440d3845c5 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/task/WorkerThread.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/task/WorkerThread.cpp @@ -52,7 +52,7 @@ WorkerThread::~WorkerThread() bool WorkerThread::isFinished() const { - return m_finished.load(std::memory_order_relaxed); + return m_finished.load(std::memory_order_acquire); } bool WorkerThread::start(DefaultTaskScheduler *const &taskScheduler) @@ -101,7 +101,9 @@ void WorkerThread::run(void) } } - m_finished.store(true, std::memory_order_relaxed); + // Release pairs with the acquire in isFinished(). Deleting the worker is + // ordered by the join() in its destructor, not by this flag. + m_finished.store(true, std::memory_order_release); } const std::thread::id WorkerThread::getId() const