From 3a5f778b70ad16a4ebbbb03b93f4784b8beb451c Mon Sep 17 00:00:00 2001 From: erwincoumans Date: Wed, 2 May 2018 14:32:43 -0700 Subject: [PATCH 1/2] expose lsMemory destroy function to release thread memory --- examples/ExampleBrowser/InProcessExampleBrowser.cpp | 11 ++++++++++- examples/MultiThreading/b3Win32ThreadSupport.cpp | 8 ++++++-- examples/MultiThreading/b3Win32ThreadSupport.h | 6 ++++++ examples/SharedMemory/PhysicsClientUDP.cpp | 10 +++++++++- examples/SharedMemory/PhysicsServerExample.cpp | 10 +++++++++- 5 files changed, 40 insertions(+), 5 deletions(-) diff --git a/examples/ExampleBrowser/InProcessExampleBrowser.cpp b/examples/ExampleBrowser/InProcessExampleBrowser.cpp index caf32a51a..c08b27d53 100644 --- a/examples/ExampleBrowser/InProcessExampleBrowser.cpp +++ b/examples/ExampleBrowser/InProcessExampleBrowser.cpp @@ -18,6 +18,8 @@ void ExampleBrowserThreadFunc(void* userPtr,void* lsMemory); void* ExampleBrowserMemoryFunc(); +void ExampleBrowserMemoryReleaseFunc(void* ptr); + #include //#include "BulletMultiThreaded/PlatformDefinitions.h" @@ -42,6 +44,7 @@ static b3ThreadSupportInterface* createExampleBrowserThreadSupport(int numThread b3PosixThreadSupport::ThreadConstructionInfo constructionInfo("testThreads", ExampleBrowserThreadFunc, ExampleBrowserMemoryFunc, + ExampleBrowserMemoryReleaseFunc, numThreads); b3ThreadSupportInterface* threadSupport = new b3PosixThreadSupport(constructionInfo); @@ -56,7 +59,7 @@ static b3ThreadSupportInterface* createExampleBrowserThreadSupport(int numThread b3ThreadSupportInterface* createExampleBrowserThreadSupport(int numThreads) { - b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads",ExampleBrowserThreadFunc,ExampleBrowserMemoryFunc,numThreads); + b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads",ExampleBrowserThreadFunc,ExampleBrowserMemoryFunc,ExampleBrowserMemoryReleaseFunc,numThreads); b3Win32ThreadSupport* threadSupport = new b3Win32ThreadSupport(threadConstructionInfo); return threadSupport; @@ -303,6 +306,12 @@ void* ExampleBrowserMemoryFunc() return new ExampleBrowserThreadLocalStorage; } +void ExampleBrowserMemoryReleaseFunc(void* ptr) +{ + ExampleBrowserThreadLocalStorage* p = (ExampleBrowserThreadLocalStorage*) ptr; + delete ptr; +} + diff --git a/examples/MultiThreading/b3Win32ThreadSupport.cpp b/examples/MultiThreading/b3Win32ThreadSupport.cpp index c0987beab..37cb5886a 100644 --- a/examples/MultiThreading/b3Win32ThreadSupport.cpp +++ b/examples/MultiThreading/b3Win32ThreadSupport.cpp @@ -289,6 +289,7 @@ void b3Win32ThreadSupport::startThreads(const Win32ThreadConstructionInfo& threa threadStatus.m_threadHandle = handle; threadStatus.m_lsMemory = threadConstructionInfo.m_lsMemoryFunc(); threadStatus.m_userThreadFunc = threadConstructionInfo.m_userThreadFunc; + threadStatus.m_lsMemoryReleaseFunc = threadConstructionInfo.m_lsMemoryReleaseFunc; printf("started %s thread %d with threadHandle %p\n",threadConstructionInfo.m_uniqueName,i,handle); @@ -312,9 +313,12 @@ void b3Win32ThreadSupport::stopThreads() { WaitForSingleObject(threadStatus.m_eventCompletetHandle, INFINITE); } - - delete threadStatus.m_lsMemory; + if (threadStatus.m_lsMemoryReleaseFunc) + { + threadStatus.m_lsMemoryReleaseFunc(threadStatus.m_lsMemory); + } + threadStatus.m_userPtr = 0; SetEvent(threadStatus.m_eventStartHandle); WaitForSingleObject(threadStatus.m_eventCompletetHandle, INFINITE); diff --git a/examples/MultiThreading/b3Win32ThreadSupport.h b/examples/MultiThreading/b3Win32ThreadSupport.h index df9f3d7ea..4d487a732 100644 --- a/examples/MultiThreading/b3Win32ThreadSupport.h +++ b/examples/MultiThreading/b3Win32ThreadSupport.h @@ -26,6 +26,7 @@ subject to the following restrictions: typedef void (*b3Win32ThreadFunc)(void* userPtr,void* lsMemory); typedef void* (*b3Win32lsMemorySetupFunc)(); +typedef void (*b3Win32lsMemoryReleaseFunc)(void*); ///b3Win32ThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication @@ -43,6 +44,8 @@ public: void* m_userPtr; //for taskDesc etc void* m_lsMemory; //initialized using Win32LocalStoreMemorySetupFunc + b3Win32lsMemoryReleaseFunc m_lsMemoryReleaseFunc; + void* m_threadHandle; //this one is calling 'Win32ThreadFunc' void* m_eventStartHandle; @@ -67,12 +70,14 @@ public: Win32ThreadConstructionInfo(const char* uniqueName, b3Win32ThreadFunc userThreadFunc, b3Win32lsMemorySetupFunc lsMemoryFunc, + b3Win32lsMemoryReleaseFunc lsMemoryReleaseFunc, int numThreads=1, int threadStackSize=65535 ) :m_uniqueName(uniqueName), m_userThreadFunc(userThreadFunc), m_lsMemoryFunc(lsMemoryFunc), + m_lsMemoryReleaseFunc(lsMemoryReleaseFunc), m_numThreads(numThreads), m_threadStackSize(threadStackSize), m_priority(0) @@ -83,6 +88,7 @@ public: const char* m_uniqueName; b3Win32ThreadFunc m_userThreadFunc; b3Win32lsMemorySetupFunc m_lsMemoryFunc; + b3Win32lsMemoryReleaseFunc m_lsMemoryReleaseFunc; int m_numThreads; int m_threadStackSize; int m_priority; diff --git a/examples/SharedMemory/PhysicsClientUDP.cpp b/examples/SharedMemory/PhysicsClientUDP.cpp index f3b316723..c9b9afe38 100644 --- a/examples/SharedMemory/PhysicsClientUDP.cpp +++ b/examples/SharedMemory/PhysicsClientUDP.cpp @@ -11,6 +11,8 @@ #include "../MultiThreading/b3ThreadSupportInterface.h" void UDPThreadFunc(void* userPtr, void* lsMemory); void* UDPlsMemoryFunc(); +void UDPlsMemoryReleaseFunc(void* ptr); + bool gVerboseNetworkMessagesClient = false; #ifndef _WIN32 @@ -21,6 +23,7 @@ b3ThreadSupportInterface* createUDPThreadSupport(int numThreads) b3PosixThreadSupport::ThreadConstructionInfo constructionInfo("UDPThread", UDPThreadFunc, UDPlsMemoryFunc, + UDPlsMemoryReleaseFunc, numThreads); b3ThreadSupportInterface* threadSupport = new b3PosixThreadSupport(constructionInfo); @@ -34,7 +37,7 @@ b3ThreadSupportInterface* createUDPThreadSupport(int numThreads) b3ThreadSupportInterface* createUDPThreadSupport(int numThreads) { - b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("UDPThread", UDPThreadFunc, UDPlsMemoryFunc, numThreads); + b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("UDPThread", UDPThreadFunc, UDPlsMemoryFunc,UDPlsMemoryReleaseFunc, numThreads); b3Win32ThreadSupport* threadSupport = new b3Win32ThreadSupport(threadConstructionInfo); return threadSupport; @@ -436,6 +439,11 @@ void* UDPlsMemoryFunc() return new UDPThreadLocalStorage; } +void UDPlsMemoryReleaseFunc(void* ptr) +{ + UDPThreadLocalStorage* p = (UDPThreadLocalStorage*) ptr; + delete p; +} diff --git a/examples/SharedMemory/PhysicsServerExample.cpp b/examples/SharedMemory/PhysicsServerExample.cpp index 72b0d0598..8be1be05b 100644 --- a/examples/SharedMemory/PhysicsServerExample.cpp +++ b/examples/SharedMemory/PhysicsServerExample.cpp @@ -92,6 +92,7 @@ static void saveCurrentSettingsVR(const btVector3& VRTeleportPos1) bool gDebugRenderToggle = false; void MotionThreadFunc(void* userPtr,void* lsMemory); void* MotionlsMemoryFunc(); +void MotionlsMemoryReleaseFunc(void* ptr); #define MAX_MOTION_NUM_THREADS 1 enum { @@ -150,6 +151,7 @@ b3ThreadSupportInterface* createMotionThreadSupport(int numThreads) b3PosixThreadSupport::ThreadConstructionInfo constructionInfo("MotionThreads", MotionThreadFunc, MotionlsMemoryFunc, + MotionlsMemoryReleaseFunc, numThreads); b3ThreadSupportInterface* threadSupport = new b3PosixThreadSupport(constructionInfo); @@ -163,7 +165,7 @@ b3ThreadSupportInterface* createMotionThreadSupport(int numThreads) b3ThreadSupportInterface* createMotionThreadSupport(int numThreads) { - b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("MotionThreads",MotionThreadFunc,MotionlsMemoryFunc,numThreads); + b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("MotionThreads",MotionThreadFunc,MotionlsMemoryFunc,MotionlsMemoryReleaseFunc,numThreads); b3Win32ThreadSupport* threadSupport = new b3Win32ThreadSupport(threadConstructionInfo); return threadSupport; @@ -486,6 +488,12 @@ void* MotionlsMemoryFunc() return new MotionThreadLocalStorage; } +void MotionlsMemoryReleaseFunc(void* ptr) +{ + MotionThreadLocalStorage* p = (MotionThreadLocalStorage*) ptr; + delete p; +} + struct UserDebugDrawLine From 4a8ad1a54eeceefb67024dd84623c4407f492719 Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Wed, 2 May 2018 15:39:16 -0700 Subject: [PATCH 2/2] fix posix memory release issues fix compile problems on Mac OSX reduce shared memory size on Mac (>512*1024 fails to allocate) --- examples/ExampleBrowser/InProcessExampleBrowser.cpp | 2 +- .../Importers/ImportMJCFDemo/BulletMJCFImporter.cpp | 10 ++++++++++ .../Wavefront2GLInstanceGraphicsShape.cpp | 3 ++- examples/MultiThreading/MultiThreadingExample.cpp | 11 ++++++++--- examples/MultiThreading/b3PosixThreadSupport.cpp | 10 ++++++++-- examples/MultiThreading/b3PosixThreadSupport.h | 7 +++++++ .../SharedMemory/PhysicsServerCommandProcessor.cpp | 3 +++ examples/SharedMemory/PhysicsServerSharedMemory.cpp | 2 +- examples/SharedMemory/SharedMemoryCommands.h | 6 +++++- 9 files changed, 45 insertions(+), 9 deletions(-) diff --git a/examples/ExampleBrowser/InProcessExampleBrowser.cpp b/examples/ExampleBrowser/InProcessExampleBrowser.cpp index c08b27d53..6a6d413a1 100644 --- a/examples/ExampleBrowser/InProcessExampleBrowser.cpp +++ b/examples/ExampleBrowser/InProcessExampleBrowser.cpp @@ -309,7 +309,7 @@ void* ExampleBrowserMemoryFunc() void ExampleBrowserMemoryReleaseFunc(void* ptr) { ExampleBrowserThreadLocalStorage* p = (ExampleBrowserThreadLocalStorage*) ptr; - delete ptr; + delete p; } diff --git a/examples/Importers/ImportMJCFDemo/BulletMJCFImporter.cpp b/examples/Importers/ImportMJCFDemo/BulletMJCFImporter.cpp index c8995f5b0..95e1d4a34 100644 --- a/examples/Importers/ImportMJCFDemo/BulletMJCFImporter.cpp +++ b/examples/Importers/ImportMJCFDemo/BulletMJCFImporter.cpp @@ -1032,6 +1032,11 @@ struct BulletMJCFImporterInternalData //todo break; } + case URDF_GEOM_CDF: + { + //todo + break; + } case URDF_GEOM_CYLINDER: case URDF_GEOM_CAPSULE: { @@ -2541,6 +2546,11 @@ class btCompoundShape* BulletMJCFImporter::convertLinkCollisionShapes( int linkI } break; } + case URDF_GEOM_CDF: + { + //todo + break; + } case URDF_GEOM_UNKNOWN: { break; diff --git a/examples/Importers/ImportObjDemo/Wavefront2GLInstanceGraphicsShape.cpp b/examples/Importers/ImportObjDemo/Wavefront2GLInstanceGraphicsShape.cpp index 8bdfe77b7..9768dd71e 100644 --- a/examples/Importers/ImportObjDemo/Wavefront2GLInstanceGraphicsShape.cpp +++ b/examples/Importers/ImportObjDemo/Wavefront2GLInstanceGraphicsShape.cpp @@ -2,7 +2,8 @@ #include "../../OpenGLWindow/GLInstancingRenderer.h" #include "../../OpenGLWindow/GLInstanceGraphicsShape.h" -#include "btBulletDynamicsCommon.h" +//#include "btBulletDynamicsCommon.h" +#include "LinearMath/btVector3.h" #include "../../OpenGLWindow/SimpleOpenGL3App.h" #include "Wavefront2GLInstanceGraphicsShape.h" #include "../../OpenGLWindow/GLInstancingRenderer.h" diff --git a/examples/MultiThreading/MultiThreadingExample.cpp b/examples/MultiThreading/MultiThreadingExample.cpp index d0ad1ba51..49fdfe2c9 100644 --- a/examples/MultiThreading/MultiThreadingExample.cpp +++ b/examples/MultiThreading/MultiThreadingExample.cpp @@ -21,7 +21,7 @@ void SampleThreadFunc(void* userPtr,void* lsMemory); void* SamplelsMemoryFunc(); - +void SamplelsMemoryReleaseFunc(void* ptr); #include //#include "BulletMultiThreaded/PlatformDefinitions.h" @@ -34,6 +34,7 @@ b3ThreadSupportInterface* createThreadSupport(int numThreads) b3PosixThreadSupport::ThreadConstructionInfo constructionInfo("testThreads", SampleThreadFunc, SamplelsMemoryFunc, + SamplelsMemoryReleaseFunc, numThreads); b3ThreadSupportInterface* threadSupport = new b3PosixThreadSupport(constructionInfo); @@ -47,7 +48,7 @@ b3ThreadSupportInterface* createThreadSupport(int numThreads) b3ThreadSupportInterface* createThreadSupport(int numThreads) { - b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads",SampleThreadFunc,SamplelsMemoryFunc,numThreads); + b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads",SampleThreadFunc,SamplelsMemoryFunc,SamplelsMemoryReleaseFunc,numThreads); b3Win32ThreadSupport* threadSupport = new b3Win32ThreadSupport(threadConstructionInfo); return threadSupport; @@ -155,7 +156,11 @@ void* SamplelsMemoryFunc() return new SampleThreadLocalStorage; } - +void SamplelsMemoryReleaseFunc(void* ptr) +{ + SampleThreadLocalStorage* p = (SampleThreadLocalStorage*) ptr; + delete p; +} diff --git a/examples/MultiThreading/b3PosixThreadSupport.cpp b/examples/MultiThreading/b3PosixThreadSupport.cpp index 269579ab5..dfec90df6 100644 --- a/examples/MultiThreading/b3PosixThreadSupport.cpp +++ b/examples/MultiThreading/b3PosixThreadSupport.cpp @@ -251,7 +251,8 @@ void b3PosixThreadSupport::startThreads(ThreadConstructionInfo& threadConstructi spuStatus.m_mainSemaphore = m_mainSemaphore; spuStatus.m_lsMemory = threadConstructionInfo.m_lsMemoryFunc(); spuStatus.m_userThreadFunc = threadConstructionInfo.m_userThreadFunc; - spuStatus.threadUsed = 0; + spuStatus.m_lsMemoryReleaseFunc = threadConstructionInfo.m_lsMemoryReleaseFunc; + spuStatus.threadUsed = 0; printf("started thread %d \n",i); @@ -277,7 +278,12 @@ void b3PosixThreadSupport::stopThreads() destroySem(spuStatus.startSemaphore); printf("semaphore destroyed\n"); checkPThreadFunction(pthread_join(spuStatus.thread,0)); - delete spuStatus.m_lsMemory; + + if (spuStatus.m_lsMemoryReleaseFunc) + { + spuStatus.m_lsMemoryReleaseFunc( spuStatus.m_lsMemory); + } + } printf("destroy main semaphore\n"); destroySem(m_mainSemaphore); diff --git a/examples/MultiThreading/b3PosixThreadSupport.h b/examples/MultiThreading/b3PosixThreadSupport.h index 3f54d4ab5..7ed95a262 100644 --- a/examples/MultiThreading/b3PosixThreadSupport.h +++ b/examples/MultiThreading/b3PosixThreadSupport.h @@ -35,6 +35,7 @@ subject to the following restrictions: typedef void (*b3PosixThreadFunc)(void* userPtr,void* lsMemory); typedef void* (*b3PosixlsMemorySetupFunc)(); +typedef void (*b3PosixlsMemoryReleaseFunc)(void* ptr); // b3PosixThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication class b3PosixThreadSupport : public b3ThreadSupportInterface @@ -55,6 +56,8 @@ public: b3PosixThreadFunc m_userThreadFunc; void* m_userPtr; //for taskDesc etc + b3PosixlsMemoryReleaseFunc m_lsMemoryReleaseFunc; + void* m_lsMemory; //initialized using PosixLocalStoreMemorySetupFunc pthread_t thread; @@ -83,12 +86,14 @@ public: ThreadConstructionInfo(const char* uniqueName, b3PosixThreadFunc userThreadFunc, b3PosixlsMemorySetupFunc lsMemoryFunc, + b3PosixlsMemoryReleaseFunc lsMemoryReleaseFunc, int numThreads=1, int threadStackSize=65535 ) :m_uniqueName(uniqueName), m_userThreadFunc(userThreadFunc), m_lsMemoryFunc(lsMemoryFunc), + m_lsMemoryReleaseFunc(lsMemoryReleaseFunc), m_numThreads(numThreads), m_threadStackSize(threadStackSize) { @@ -98,6 +103,8 @@ public: const char* m_uniqueName; b3PosixThreadFunc m_userThreadFunc; b3PosixlsMemorySetupFunc m_lsMemoryFunc; + b3PosixlsMemoryReleaseFunc m_lsMemoryReleaseFunc; + int m_numThreads; int m_threadStackSize; diff --git a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp index 11041e3a2..018463307 100644 --- a/examples/SharedMemory/PhysicsServerCommandProcessor.cpp +++ b/examples/SharedMemory/PhysicsServerCommandProcessor.cpp @@ -9693,10 +9693,13 @@ void PhysicsServerCommandProcessor::stepSimulationRealTime(double dtInSec,const void PhysicsServerCommandProcessor::resetSimulation() { //clean up all data + +#ifndef SKIP_SOFT_BODY_MULTI_BODY_DYNAMICS_WORLD if (m_data && m_data->m_dynamicsWorld) { m_data->m_dynamicsWorld->getWorldInfo().m_sparsesdf.Reset(); } +#endif if (m_data && m_data->m_guiHelper) { m_data->m_guiHelper->removeAllGraphicsInstances(); diff --git a/examples/SharedMemory/PhysicsServerSharedMemory.cpp b/examples/SharedMemory/PhysicsServerSharedMemory.cpp index 4413a5aab..dcdd996b3 100644 --- a/examples/SharedMemory/PhysicsServerSharedMemory.cpp +++ b/examples/SharedMemory/PhysicsServerSharedMemory.cpp @@ -170,7 +170,7 @@ bool PhysicsServerSharedMemory::connectSharedMemory( struct GUIHelperInterface* } } else { - b3Error("Cannot connect to shared memory"); + //b3Error("Cannot connect to shared memory"); m_data->m_areConnected[block] = false; } } while (counter++ < 10 && !m_data->m_areConnected[block]); diff --git a/examples/SharedMemory/SharedMemoryCommands.h b/examples/SharedMemory/SharedMemoryCommands.h index 9331c87d9..891b07d90 100644 --- a/examples/SharedMemory/SharedMemoryCommands.h +++ b/examples/SharedMemory/SharedMemoryCommands.h @@ -23,7 +23,11 @@ typedef unsigned long long int smUint64_t; #endif -#define SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE (8*1024*1024) +#ifdef __APPLE__ + #define SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE (512*1024) +#else + #define SHARED_MEMORY_MAX_STREAM_CHUNK_SIZE (8*1024*1024) +#endif #define SHARED_MEMORY_SERVER_TEST_C #define MAX_DEGREE_OF_FREEDOM 128