Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -126,7 +126,7 @@ class SOFA_SIMULATION_CORE_API DefaultTaskScheduler : public TaskScheduler

std::atomic<bool> m_workerThreadsIdle;

bool m_isClosing;
std::atomic<bool> m_isClosing;

unsigned m_threadCount;

Expand Down
12 changes: 6 additions & 6 deletions Sofa/framework/Simulation/Core/src/sofa/simulation/task/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

namespace sofa::simulation
{
Task::Allocator* Task::_allocator = nullptr;
std::atomic<Task::Allocator*> Task::_allocator { nullptr };

Task::Task(int scheduledThread)
: m_scheduledThread(scheduledThread)
Expand All @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <sofa/simulation/config.h>

#include <atomic>
#include <cstddef>

namespace sofa::simulation
Expand Down Expand Up @@ -100,7 +101,7 @@ class SOFA_SIMULATION_CORE_API Task

private:

static Task::Allocator * _allocator;
static std::atomic<Task::Allocator*> _allocator;
};

} // namespace sofa::simulation
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Loading