Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SofaSimulationCore] Move CpuTask class into its own file #1993

Merged
merged 5 commits into from
Apr 21, 2021
Merged
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
2 changes: 2 additions & 0 deletions SofaKernel/modules/SofaSimulationCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(HEADER_FILES
${SRC_ROOT}/CollisionEndEvent.h
${SRC_ROOT}/CollisionVisitor.h
${SRC_ROOT}/Colors.h
${SRC_ROOT}/CpuTask.h
${SRC_ROOT}/DeactivatedNodeVisitor.h
${SRC_ROOT}/DefaultAnimationLoop.h
${SRC_ROOT}/DefaultVisualManagerLoop.h
Expand Down Expand Up @@ -93,6 +94,7 @@ set(SOURCE_FILES
${SRC_ROOT}/CollisionBeginEvent.cpp
${SRC_ROOT}/CollisionEndEvent.cpp
${SRC_ROOT}/CollisionVisitor.cpp
${SRC_ROOT}/CpuTask.cpp
${SRC_ROOT}/DeactivatedNodeVisitor.cpp
${SRC_ROOT}/DefaultAnimationLoop.cpp
${SRC_ROOT}/DefaultVisualManagerLoop.cpp
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <sofa/simulation/Task.h>
#include <thread>
#include <sofa/simulation/CpuTask.h>

namespace sofa
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "TaskSchedulerTestTasks.h"

#include <sofa/simulation/TaskScheduler.h>
#include <sofa/simulation/CpuTask.h>
#include <sofa/simulation/DefaultTaskScheduler.h>
#include <sofa/helper/testing/BaseTest.h>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <sofa/simulation/CpuTask.h>

#include <thread>


namespace sofa::simulation
{
CpuTask::CpuTask(CpuTask::Status* status, int scheduledThread)
: Task(scheduledThread)
, m_status(status)
{
}

CpuTask::~CpuTask()
{
}

CpuTask::Status *CpuTask::getStatus(void) const
{
return m_status;
}

bool CpuTask::Status::isBusy() const
{
return (m_busy.load(std::memory_order_relaxed) > 0);
}

int CpuTask::Status::setBusy(bool busy)
{
if (busy)
{
return m_busy.fetch_add(1, std::memory_order_relaxed);
}
else
{
return m_busy.fetch_sub(1, std::memory_order_relaxed);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#pragma once

#include <sofa/simulation/config.h>

#include <sofa/simulation/Task.h>

namespace sofa::simulation
{
/** Base class to implement a CPU task
* all the tasks running on the CPU should inherits from this class
*/
class SOFA_SIMULATION_CORE_API CpuTask : public Task
{
public:

/** CPU Task Status class definition:
* used to synchronize CPU tasks */
class SOFA_SIMULATION_CORE_API Status : public Task::Status
{
public:
Status() : m_busy(0) {}

bool isBusy() const override final;

int setBusy(bool busy) override final;

private:
std::atomic<int> m_busy;
};


CpuTask::Status* getStatus(void) const override final;


public:

CpuTask(CpuTask::Status* status, int scheduledThread = -1);

virtual ~CpuTask();

private:
CpuTask::Status* m_status;
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <sofa/core/visual/VisualParams.h>
#include <sofa/helper/AdvancedTimer.h>
#include <sofa/core/ConstraintParams.h>
#include <sofa/simulation/TaskScheduler.h>

#include <thread>

namespace sofa
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#ifndef InitTasks_h__
#define InitTasks_h__

#include <sofa/simulation/TaskScheduler.h>
#include <sofa/simulation/CpuTask.h>

namespace sofa
{
Expand Down
127 changes: 61 additions & 66 deletions SofaKernel/modules/SofaSimulationCore/src/sofa/simulation/Task.cpp
Original file line number Diff line number Diff line change
@@ -1,73 +1,68 @@
/******************************************************************************
* SOFA, Simulation Open-Framework Architecture *
* (c) 2006 INRIA, USTL, UJF, CNRS, MGH *
* *
* This program is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by *
* the Free Software Foundation; either version 2.1 of the License, or (at *
* your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: contact@sofa-framework.org *
******************************************************************************/
#include <sofa/simulation/Task.h>

#include <cassert>
#include <thread>
namespace sofa::simulation
{
Task::Allocator* Task::_allocator = nullptr;

Task::Task(int scheduledThread)
: m_scheduledThread(scheduledThread)
, m_id(0)
{
}

namespace sofa
{
namespace simulation
{


Task::Allocator* Task::_allocator = nullptr;


Task::Task(int scheduledThread)
: m_scheduledThread(scheduledThread)
, m_id(0)
{
}

Task::~Task()
{
}


CpuTask::CpuTask(CpuTask::Status* status, int scheduledThread)
: Task(scheduledThread)
, m_status(status)
{
}

CpuTask::~CpuTask()
{
}




ThreadSpecificTask::ThreadSpecificTask(std::atomic<int>* atomicCounter, std::mutex* mutex, CpuTask::Status* status )
: CpuTask(status)
, m_atomicCounter(atomicCounter)
, m_threadSpecificMutex(mutex)
{}

ThreadSpecificTask::~ThreadSpecificTask()
{
}

Task::MemoryAlloc ThreadSpecificTask::run()
{

runThreadSpecific();

{
std::lock_guard<std::mutex> lock(*m_threadSpecificMutex);
runCriticalThreadSpecific();
}

m_atomicCounter->fetch_sub(1, std::memory_order_acq_rel);

while(m_atomicCounter->load(std::memory_order_relaxed) > 0)
{
// yield while waiting
std::this_thread::yield();
}
return Task::MemoryAlloc::Stack;
}
Task::~Task()
{
}

void *Task::operator new(std::size_t sz)
{
return _allocator->allocate(sz);
}

void Task::operator delete(void *ptr)
{
_allocator->free(ptr, 0);
}

void Task::operator delete(void *ptr, std::size_t sz)
{
_allocator->free(ptr, sz);
}

int Task::getScheduledThread() const
{
return m_scheduledThread;
}

Task::Allocator *Task::getAllocator()
{
return _allocator;
}


} // namespace simulation
void Task::setAllocator(Task::Allocator *allocator)
{
_allocator = allocator;
}

} // namespace sofa
Loading