Skip to content

Commit

Permalink
reverted user defined thread defintion
Browse files Browse the repository at this point in the history
  • Loading branch information
JulioJerez committed Oct 29, 2024
1 parent 201f875 commit 2bb0406
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 86 deletions.
2 changes: 1 addition & 1 deletion newton-4.00/sdk/dCollision/ndBvhNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ void ndBvhSceneManager::BuildBvhGenerateLayerGrids(ndThreadPool& threadPool)
ndAssert(root->m_depthLevel == 0);
class StackLevel
{
public:
public:
ndBvhInternalNode* m_node;
ndInt32 m_depthLevel;
};
Expand Down
21 changes: 16 additions & 5 deletions newton-4.00/sdk/dCollision/ndScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ ndScene::ndScene()
,m_sceneBodyArray(1024)
,m_activeConstraintArray(1024)
,m_specialUpdateList()
,m_backgroundThread()
,m_newPairs(1024)
,m_lock()
,m_rootNode(nullptr)
,m_sentinelBody(nullptr)
,m_contactNotifyCallback(new ndContactNotify(nullptr))
,m_backgroundThread(nullptr)
,m_timestep(ndFloat32 (0.0f))
,m_lru(D_CONTACT_DELAY_FRAMES)
,m_frameNumber(0)
Expand All @@ -88,12 +88,12 @@ ndScene::ndScene(const ndScene& src)
,m_sceneBodyArray()
,m_activeConstraintArray()
,m_specialUpdateList()
,m_backgroundThread()
,m_newPairs(1024)
,m_lock()
,m_rootNode(nullptr)
,m_sentinelBody(nullptr)
,m_contactNotifyCallback(nullptr)
,m_backgroundThread(nullptr)
,m_timestep(ndFloat32(0.0f))
,m_lru(src.m_lru)
,m_frameNumber(src.m_frameNumber)
Expand All @@ -103,7 +103,7 @@ ndScene::ndScene(const ndScene& src)
ndScene* const stealData = (ndScene*)&src;

SetThreadCount(src.GetThreadCount());
m_backgroundThread.SetThreadCount(m_backgroundThread.GetThreadCount());
//m_backgroundThread.SetThreadCount(m_backgroundThread.GetThreadCount());

m_scratchBuffer.Swap(stealData->m_scratchBuffer);
m_sceneBodyArray.Swap(stealData->m_sceneBodyArray);
Expand Down Expand Up @@ -1188,7 +1188,10 @@ void ndScene::BodiesInAabb(ndBodiesInAabbNotify& callback, const ndVector& minBo
void ndScene::Cleanup()
{
Sync();
m_backgroundThread.Terminate();
if (m_backgroundThread)
{
m_backgroundThread->Terminate();
}
PrepareCleanup();

m_frameNumber = 0;
Expand Down Expand Up @@ -1268,7 +1271,15 @@ bool ndScene::ConvexCast(ndConvexCastNotify& callback, const ndShapeInstance& co

void ndScene::SendBackgroundTask(ndBackgroundTask* const job)
{
m_backgroundThread.SendTask(job);
if (m_backgroundThread)
{
m_backgroundThread->SendTask(job);
}
else
{
ndAssert(0);
delete job;
}
}

void ndScene::AddPair(ndBodyKinematic* const body0, ndBodyKinematic* const body1, ndInt32 threadId)
Expand Down
2 changes: 1 addition & 1 deletion newton-4.00/sdk/dCollision/ndScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ class ndScene : public ndClassAlloc, public ndThreadPool
ndArray<ndBodyKinematic*> m_sceneBodyArray;
ndArray<ndConstraint*> m_activeConstraintArray;
ndSpecialList<ndBodyKinematic> m_specialUpdateList;
ndThreadBackgroundWorker m_backgroundThread;
ndArray<ndContactPairs> m_newPairs;
ndArray<ndContactPairs> m_partialNewPairs[D_MAX_THREADS_COUNT];
ndPolygonMeshDesc::ndStaticMeshFaceQuery m_staticMeshQuery[D_MAX_THREADS_COUNT];
Expand All @@ -172,6 +171,7 @@ class ndScene : public ndClassAlloc, public ndThreadPool
ndBvhNode* m_rootNode;
ndBodyKinematic* m_sentinelBody;
ndContactNotify* m_contactNotifyCallback;
ndThreadBackgroundWorker* m_backgroundThread;

ndFloat32 m_timestep;
ndUnsigned32 m_lru;
Expand Down
2 changes: 1 addition & 1 deletion newton-4.00/sdk/dCore/ndThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

ndThreadInterface::ndThreadInterface()
:ndClassAlloc()
,ndSemaphore()
{
}

Expand All @@ -44,6 +43,7 @@ ndThreadInterface::~ndThreadInterface()

ndThread::ndThread()
:ndThreadInterface()
,ndSemaphore()
#ifndef D_USE_THREAD_EMULATION
,ndAtomic<bool>(true)
,std::condition_variable()
Expand Down
5 changes: 3 additions & 2 deletions newton-4.00/sdk/dCore/ndThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ndThreadName
char m_name[32];
};

class ndThreadInterface: public ndClassAlloc, public ndSemaphore
class ndThreadInterface: public ndClassAlloc
{
public:
D_CORE_API ndThreadInterface();
Expand All @@ -53,8 +53,9 @@ class ndThreadInterface: public ndClassAlloc, public ndSemaphore
};

/// Base class for for all multi thread functionality.
class ndThread
class ndThread
:public ndThreadInterface
,public ndSemaphore
#ifndef D_USE_THREAD_EMULATION
,public ndAtomic<bool>
,public std::condition_variable
Expand Down
68 changes: 13 additions & 55 deletions newton-4.00/sdk/dCore/ndThreadPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,6 @@
#include "ndThreadPool.h"
#include "ndThreadSyncUtils.h"

class ndThreadPool::ndMainThread: public ndThread
{
public:
ndMainThread(ndThreadPool* const owner)
:ndThread()
,m_owner(owner)
{
}

void ThreadFunction() override
{
m_owner->ThreadFunction();
}

virtual void Release()
{
m_owner->Release();
}

ndThreadPool* const m_owner;
};

ndThreadPool::ndWorker::ndWorker()
:ndThread()
,m_owner(nullptr)
Expand Down Expand Up @@ -134,21 +112,19 @@ void ndThreadPool::ndWorker::ThreadFunction()

ndThreadPool::ndThreadPool(const char* const baseName)
:ndSyncMutex()
,m_main(nullptr)
,ndThread()
,m_workers(nullptr)
,m_count(0)
{
char name[256];
m_main = new ndMainThread(this);
strncpy(m_baseName, baseName, sizeof (m_baseName));
snprintf(name, sizeof (name), "%s_%d", m_baseName, 0);
m_main->SetName(name);
SetName(name);
}

ndThreadPool::~ndThreadPool()
{
SetThreadCount(0);
delete m_main;
}

ndInt32 ndThreadPool::GetMaxThreads()
Expand All @@ -171,29 +147,21 @@ void ndThreadPool::SetThreadCount(ndInt32 count)
{
if (m_workers)
{
//m_count = 0;
//delete[] m_workers;
//m_workers = nullptr;
for (ndInt32 i = m_count - 1; i >= 0; --i)
{
m_workers[i] = ndSharedPtr<ndWorker>(nullptr);
}
m_count = 0;
delete[] m_workers;
m_workers = nullptr;
}
if (count)
{
m_count = count;
//m_workers = new ndWorker[size_t(count)];
m_workers = new ndSharedPtr<ndWorker>[size_t(count)];
m_workers = new ndWorker[size_t(count)];
for (ndInt32 i = 0; i < count; ++i)
{
char name[256];
m_workers[i] = ndSharedPtr<ndWorker>(new ndWorker);
m_workers[i]->m_owner = this;
m_workers[i]->m_threadIndex = i;
m_workers[i].m_owner = this;
m_workers[i].m_threadIndex = i;
snprintf(name, sizeof(name), "%s_%d", m_baseName, i + 1);
m_workers[i]->SetName(name);
m_workers[i].SetName(name);
}
}
}
Expand All @@ -205,7 +173,7 @@ void ndThreadPool::Begin()
D_TRACKTIME();
for (ndInt32 i = 0; i < m_count; ++i)
{
m_workers[i]->Signal();
m_workers[i].Signal();
}

auto BeginJobs = ndMakeObject::ndFunction([this](ndInt32, ndInt32)
Expand All @@ -220,9 +188,9 @@ void ndThreadPool::End()
#ifndef D_USE_THREAD_EMULATION
for (ndInt32 i = 0; i < m_count; ++i)
{
m_workers[i]->ExecuteTask(nullptr);
m_workers[i].ExecuteTask(nullptr);
#if !defined(D_USE_SYNC_SEMAPHORE)
m_workers[i]->m_begin = 0;
m_workers[i].m_begin = 0;
#endif
}

Expand All @@ -232,7 +200,7 @@ void ndThreadPool::End()
ndUnsigned8 looping = 0;
for (ndInt32 i = 0; i < m_count; ++i)
{
looping = ndUnsigned8(looping | m_workers[i]->m_stillLooping);
looping = ndUnsigned8(looping | m_workers[i].m_stillLooping);
}
stillLooping = ndUnsigned8(stillLooping & looping);
if (m_count)
Expand All @@ -248,20 +216,10 @@ void ndThreadPool::Release()
ndSyncMutex::Release();
}

void ndThreadPool::Finish()
{
m_main->Finish();
}

void ndThreadPool::Signal()
{
m_main->Signal();
}

void ndThreadPool::TickOne()
{
ndSyncMutex::Tick();
m_main->ndSemaphore::Signal();
ndSemaphore::Signal();
#ifdef D_USE_THREAD_EMULATION
ThreadFunction();
#endif
Expand All @@ -276,7 +234,7 @@ void ndThreadPool::WaitForWorkers()
ndUnsigned8 inProgess = 0;
for (ndInt32 i = 0; i < m_count; ++i)
{
inProgess = ndUnsigned8(inProgess | (m_workers[i]->IsTaskInProgress()));
inProgess = ndUnsigned8(inProgess | (m_workers[i].IsTaskInProgress()));
}
jobsInProgress = ndUnsigned8 (jobsInProgress & inProgess);
if (jobsInProgress)
Expand Down
25 changes: 6 additions & 19 deletions newton-4.00/sdk/dCore/ndThreadPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
#include "ndSyncMutex.h"
#include "ndSemaphore.h"
#include "ndClassAlloc.h"
#include "ndThreadSyncUtils.h"
#include "ndContainersAlloc.h"
#include "ndSharedPtr.h"

//#define D_USE_SYNC_SEMAPHORE

Expand Down Expand Up @@ -66,20 +63,16 @@ class ndTask
virtual void Execute() const = 0;
};

D_MSV_NEWTON_ALIGN_32
//class ndThreadPool: public ndSyncMutex, public ndThread
class ndThreadPool: public ndSyncMutex
class ndThreadPool: public ndSyncMutex, public ndThread
{
class ndMainThread;

class ndWorker: public ndThread
{
public:
D_CORE_API ndWorker();
D_CORE_API virtual ~ndWorker();

D_CORE_API virtual ndUnsigned8 IsTaskInProgress() const;
D_CORE_API virtual void ExecuteTask(ndTask* const task);
D_CORE_API ndUnsigned8 IsTaskInProgress() const;
D_CORE_API void ExecuteTask(ndTask* const task);

private:
virtual void ThreadFunction();
Expand Down Expand Up @@ -109,10 +102,6 @@ class ndThreadPool: public ndSyncMutex
D_CORE_API void TickOne();
D_CORE_API void Begin();
D_CORE_API void End();
D_CORE_API void Signal();
D_CORE_API void Finish();

virtual void ThreadFunction() = 0;

template <typename Function>
void ParallelExecute(const Function& ndFunction);
Expand All @@ -121,11 +110,10 @@ class ndThreadPool: public ndSyncMutex
D_CORE_API virtual void Release();
D_CORE_API virtual void WaitForWorkers();

ndThreadInterface* m_main;
ndSharedPtr<ndWorker>* m_workers;
ndWorker* m_workers;
ndInt32 m_count;
char m_baseName[32];
}D_GCC_NEWTON_ALIGN_32;
};

inline ndInt32 ndThreadPool::GetThreadCount() const
{
Expand Down Expand Up @@ -218,8 +206,7 @@ void ndThreadPool::ParallelExecute(const Function& callback)
for (ndInt32 i = 0; i < m_count; ++i)
{
ndTaskImplement<Function>* const job = &jobsArray[i + 1];
//m_workers[i].ExecuteTask(job);
m_workers[i]->ExecuteTask(job);
m_workers[i].ExecuteTask(job);
}

ndTaskImplement<Function>* const job = &jobsArray[0];
Expand Down
9 changes: 7 additions & 2 deletions newton-4.00/sdk/dNewton/ndWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ ndWorld::~ndWorld()
void ndWorld::CleanUp()
{
Sync();
m_scene->m_backgroundThread.Terminate();
//m_scene->m_backgroundThread.Terminate();
m_scene->PrepareCleanup();

m_activeSkeletons.Resize(256);
Expand Down Expand Up @@ -230,7 +230,12 @@ ndInt32 ndWorld::GetThreadCount() const
void ndWorld::SetThreadCount(ndInt32 count)
{
m_scene->SetThreadCount(count);
m_scene->m_backgroundThread.SetThreadCount(count);

if (m_scene->m_backgroundThread)
{
m_scene->m_backgroundThread->Terminate();
m_scene->m_backgroundThread->SetThreadCount(count);
}
}

ndInt32 ndWorld::GetSubSteps() const
Expand Down

0 comments on commit 2bb0406

Please sign in to comment.