diff --git a/newton-4.00/sdk/dCollision/ndBvhNode.cpp b/newton-4.00/sdk/dCollision/ndBvhNode.cpp index 5c7f052c0..674c171f7 100644 --- a/newton-4.00/sdk/dCollision/ndBvhNode.cpp +++ b/newton-4.00/sdk/dCollision/ndBvhNode.cpp @@ -1296,7 +1296,7 @@ void ndBvhSceneManager::BuildBvhGenerateLayerGrids(ndThreadPool& threadPool) ndAssert(root->m_depthLevel == 0); class StackLevel { - public: + public: ndBvhInternalNode* m_node; ndInt32 m_depthLevel; }; diff --git a/newton-4.00/sdk/dCollision/ndScene.cpp b/newton-4.00/sdk/dCollision/ndScene.cpp index 12a97e8b4..5b236c6da 100644 --- a/newton-4.00/sdk/dCollision/ndScene.cpp +++ b/newton-4.00/sdk/dCollision/ndScene.cpp @@ -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) @@ -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) @@ -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); @@ -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; @@ -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) diff --git a/newton-4.00/sdk/dCollision/ndScene.h b/newton-4.00/sdk/dCollision/ndScene.h index 0c5e437b8..3ae3c2a77 100644 --- a/newton-4.00/sdk/dCollision/ndScene.h +++ b/newton-4.00/sdk/dCollision/ndScene.h @@ -162,7 +162,6 @@ class ndScene : public ndClassAlloc, public ndThreadPool ndArray m_sceneBodyArray; ndArray m_activeConstraintArray; ndSpecialList m_specialUpdateList; - ndThreadBackgroundWorker m_backgroundThread; ndArray m_newPairs; ndArray m_partialNewPairs[D_MAX_THREADS_COUNT]; ndPolygonMeshDesc::ndStaticMeshFaceQuery m_staticMeshQuery[D_MAX_THREADS_COUNT]; @@ -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; diff --git a/newton-4.00/sdk/dCore/ndThread.cpp b/newton-4.00/sdk/dCore/ndThread.cpp index 403a6e8b6..592f78e13 100644 --- a/newton-4.00/sdk/dCore/ndThread.cpp +++ b/newton-4.00/sdk/dCore/ndThread.cpp @@ -33,7 +33,6 @@ ndThreadInterface::ndThreadInterface() :ndClassAlloc() - ,ndSemaphore() { } @@ -44,6 +43,7 @@ ndThreadInterface::~ndThreadInterface() ndThread::ndThread() :ndThreadInterface() + ,ndSemaphore() #ifndef D_USE_THREAD_EMULATION ,ndAtomic(true) ,std::condition_variable() diff --git a/newton-4.00/sdk/dCore/ndThread.h b/newton-4.00/sdk/dCore/ndThread.h index 0e9b0b0ee..1982b358f 100644 --- a/newton-4.00/sdk/dCore/ndThread.h +++ b/newton-4.00/sdk/dCore/ndThread.h @@ -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(); @@ -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 ,public std::condition_variable diff --git a/newton-4.00/sdk/dCore/ndThreadPool.cpp b/newton-4.00/sdk/dCore/ndThreadPool.cpp index 86db9499b..0473e38c0 100644 --- a/newton-4.00/sdk/dCore/ndThreadPool.cpp +++ b/newton-4.00/sdk/dCore/ndThreadPool.cpp @@ -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) @@ -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() @@ -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(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[size_t(count)]; + m_workers = new ndWorker[size_t(count)]; for (ndInt32 i = 0; i < count; ++i) { char name[256]; - m_workers[i] = ndSharedPtr(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); } } } @@ -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) @@ -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 } @@ -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) @@ -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 @@ -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) diff --git a/newton-4.00/sdk/dCore/ndThreadPool.h b/newton-4.00/sdk/dCore/ndThreadPool.h index 3aa3e15f7..1578e765e 100644 --- a/newton-4.00/sdk/dCore/ndThreadPool.h +++ b/newton-4.00/sdk/dCore/ndThreadPool.h @@ -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 @@ -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(); @@ -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 void ParallelExecute(const Function& ndFunction); @@ -121,11 +110,10 @@ class ndThreadPool: public ndSyncMutex D_CORE_API virtual void Release(); D_CORE_API virtual void WaitForWorkers(); - ndThreadInterface* m_main; - ndSharedPtr* m_workers; + ndWorker* m_workers; ndInt32 m_count; char m_baseName[32]; -}D_GCC_NEWTON_ALIGN_32; +}; inline ndInt32 ndThreadPool::GetThreadCount() const { @@ -218,8 +206,7 @@ void ndThreadPool::ParallelExecute(const Function& callback) for (ndInt32 i = 0; i < m_count; ++i) { ndTaskImplement* const job = &jobsArray[i + 1]; - //m_workers[i].ExecuteTask(job); - m_workers[i]->ExecuteTask(job); + m_workers[i].ExecuteTask(job); } ndTaskImplement* const job = &jobsArray[0]; diff --git a/newton-4.00/sdk/dNewton/ndWorld.cpp b/newton-4.00/sdk/dNewton/ndWorld.cpp index e3efb4936..83abc50ea 100644 --- a/newton-4.00/sdk/dNewton/ndWorld.cpp +++ b/newton-4.00/sdk/dNewton/ndWorld.cpp @@ -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); @@ -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