mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-03-15 18:30:53 +00:00
* Adjustment: Update Bullet version to 3.24.
This commit is contained in:
parent
35de012ee7
commit
4a3f31df2a
6148 changed files with 2112532 additions and 56873 deletions
|
|
@ -0,0 +1,297 @@
|
|||
|
||||
#include "MultiThreadingExample.h"
|
||||
#include "../CommonInterfaces/CommonGraphicsAppInterface.h"
|
||||
#include "../CommonInterfaces/CommonRenderInterface.h"
|
||||
|
||||
#include "../CommonInterfaces/CommonExampleInterface.h"
|
||||
#include "LinearMath/btTransform.h"
|
||||
|
||||
#include "../CommonInterfaces/CommonGUIHelperInterface.h"
|
||||
#include "../RenderingExamples/TimeSeriesCanvas.h"
|
||||
#include "stb_image/stb_image.h"
|
||||
#include "Bullet3Common/b3Quaternion.h"
|
||||
#include "Bullet3Common/b3Matrix3x3.h"
|
||||
#include "../Utils/b3Clock.h"
|
||||
#include "../CommonInterfaces/CommonParameterInterface.h"
|
||||
|
||||
#include "LinearMath/btAlignedObjectArray.h"
|
||||
#define stdvector btAlignedObjectArray
|
||||
|
||||
#define MAGIC_RESET_NUMBER 64738
|
||||
|
||||
void SampleThreadFunc(void* userPtr, void* lsMemory);
|
||||
void* SamplelsMemoryFunc();
|
||||
void SamplelsMemoryReleaseFunc(void* ptr);
|
||||
|
||||
#include <stdio.h>
|
||||
//#include "BulletMultiThreaded/PlatformDefinitions.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#include "b3PosixThreadSupport.h"
|
||||
|
||||
|
||||
b3ThreadSupportInterface* createThreadSupport(int numThreads)
|
||||
{
|
||||
b3PosixThreadSupport::ThreadConstructionInfo constructionInfo("testThreads",
|
||||
SampleThreadFunc,
|
||||
SamplelsMemoryFunc,
|
||||
SamplelsMemoryReleaseFunc,
|
||||
numThreads);
|
||||
b3ThreadSupportInterface* threadSupport = new b3PosixThreadSupport(constructionInfo);
|
||||
|
||||
return threadSupport;
|
||||
}
|
||||
|
||||
#elif defined(_WIN32)
|
||||
#include "b3Win32ThreadSupport.h"
|
||||
|
||||
b3ThreadSupportInterface* createThreadSupport(int numThreads)
|
||||
{
|
||||
b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads", SampleThreadFunc, SamplelsMemoryFunc, SamplelsMemoryReleaseFunc, numThreads);
|
||||
b3Win32ThreadSupport* threadSupport = new b3Win32ThreadSupport(threadConstructionInfo);
|
||||
return threadSupport;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct SampleJobInterface
|
||||
{
|
||||
virtual void executeJob(int threadIndex) = 0;
|
||||
};
|
||||
|
||||
struct SampleJob1 : public SampleJobInterface
|
||||
{
|
||||
float m_fakeWork;
|
||||
int m_jobId;
|
||||
|
||||
SampleJob1(int jobId)
|
||||
: m_fakeWork(0),
|
||||
m_jobId(jobId)
|
||||
{
|
||||
}
|
||||
virtual ~SampleJob1() {}
|
||||
|
||||
virtual void executeJob(int threadIndex)
|
||||
{
|
||||
printf("start SampleJob1 %d using threadIndex %d\n", m_jobId, threadIndex);
|
||||
//do some fake work
|
||||
for (int i = 0; i < 1000000; i++)
|
||||
m_fakeWork = b3Scalar(1.21) * m_fakeWork;
|
||||
printf("finished SampleJob1 %d using threadIndex %d\n", m_jobId, threadIndex);
|
||||
}
|
||||
};
|
||||
|
||||
struct SampleArgs
|
||||
{
|
||||
SampleArgs()
|
||||
{
|
||||
}
|
||||
b3CriticalSection* m_cs;
|
||||
|
||||
btAlignedObjectArray<SampleJobInterface*> m_jobQueue;
|
||||
|
||||
void submitJob(SampleJobInterface* job)
|
||||
{
|
||||
m_cs->lock();
|
||||
m_jobQueue.push_back(job);
|
||||
m_cs->unlock();
|
||||
}
|
||||
SampleJobInterface* consumeJob()
|
||||
{
|
||||
SampleJobInterface* job = 0;
|
||||
m_cs->lock();
|
||||
int sz = m_jobQueue.size();
|
||||
if (sz)
|
||||
{
|
||||
job = m_jobQueue[sz - 1];
|
||||
m_jobQueue.pop_back();
|
||||
}
|
||||
m_cs->unlock();
|
||||
return job;
|
||||
}
|
||||
};
|
||||
SampleArgs args;
|
||||
struct SampleThreadLocalStorage
|
||||
{
|
||||
int threadId;
|
||||
};
|
||||
|
||||
void SampleThreadFunc(void* userPtr, void* lsMemory)
|
||||
{
|
||||
printf("SampleThreadFunc thread started\n");
|
||||
|
||||
SampleThreadLocalStorage* localStorage = (SampleThreadLocalStorage*)lsMemory;
|
||||
|
||||
SampleArgs* args = (SampleArgs*)userPtr;
|
||||
|
||||
bool requestExit = false;
|
||||
while (!requestExit)
|
||||
{
|
||||
SampleJobInterface* job = args->consumeJob();
|
||||
if (job)
|
||||
{
|
||||
job->executeJob(localStorage->threadId);
|
||||
}
|
||||
|
||||
b3Clock::usleep(250);
|
||||
|
||||
args->m_cs->lock();
|
||||
int exitMagicNumber = args->m_cs->getSharedParam(1);
|
||||
requestExit = (exitMagicNumber == MAGIC_RESET_NUMBER);
|
||||
args->m_cs->unlock();
|
||||
}
|
||||
|
||||
printf("finished\n");
|
||||
//do nothing
|
||||
}
|
||||
|
||||
void* SamplelsMemoryFunc()
|
||||
{
|
||||
//don't create local store memory, just return 0
|
||||
return new SampleThreadLocalStorage;
|
||||
}
|
||||
|
||||
void SamplelsMemoryReleaseFunc(void* ptr)
|
||||
{
|
||||
SampleThreadLocalStorage* p = (SampleThreadLocalStorage*)ptr;
|
||||
delete p;
|
||||
}
|
||||
|
||||
class MultiThreadingExample : public CommonExampleInterface
|
||||
{
|
||||
CommonGraphicsApp* m_app;
|
||||
b3ThreadSupportInterface* m_threadSupport;
|
||||
btAlignedObjectArray<SampleJob1*> m_jobs;
|
||||
int m_numThreads;
|
||||
|
||||
public:
|
||||
MultiThreadingExample(GUIHelperInterface* guiHelper, int tutorialIndex)
|
||||
: m_app(guiHelper->getAppInterface()),
|
||||
m_threadSupport(0),
|
||||
m_numThreads(8)
|
||||
{
|
||||
//int numBodies = 1;
|
||||
|
||||
m_app->setUpAxis(1);
|
||||
}
|
||||
virtual ~MultiThreadingExample()
|
||||
{
|
||||
}
|
||||
|
||||
virtual void renderScene()
|
||||
{
|
||||
}
|
||||
virtual void initPhysics()
|
||||
{
|
||||
b3Printf("initPhysics");
|
||||
|
||||
m_threadSupport = createThreadSupport(m_numThreads);
|
||||
|
||||
for (int i = 0; i < m_threadSupport->getNumTasks(); i++)
|
||||
{
|
||||
SampleThreadLocalStorage* storage = (SampleThreadLocalStorage*)m_threadSupport->getThreadLocalMemory(i);
|
||||
b3Assert(storage);
|
||||
storage->threadId = i;
|
||||
}
|
||||
|
||||
args.m_cs = m_threadSupport->createCriticalSection();
|
||||
args.m_cs->setSharedParam(0, 100);
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
SampleJob1* job = new SampleJob1(i);
|
||||
args.submitJob(job);
|
||||
}
|
||||
|
||||
int i;
|
||||
for (i = 0; i < m_numThreads; i++)
|
||||
{
|
||||
m_threadSupport->runTask(B3_THREAD_SCHEDULE_TASK, (void*)&args, i);
|
||||
}
|
||||
b3Printf("Threads started");
|
||||
}
|
||||
virtual void exitPhysics()
|
||||
{
|
||||
b3Printf("exitPhysics, stopping threads");
|
||||
bool blockingWait = false;
|
||||
int arg0, arg1;
|
||||
|
||||
args.m_cs->lock();
|
||||
//terminate all threads
|
||||
args.m_cs->setSharedParam(1, MAGIC_RESET_NUMBER);
|
||||
args.m_cs->unlock();
|
||||
|
||||
if (blockingWait)
|
||||
{
|
||||
for (int i = 0; i < m_numThreads; i++)
|
||||
{
|
||||
m_threadSupport->waitForResponse(&arg0, &arg1);
|
||||
printf("finished waiting for response: %d %d\n", arg0, arg1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int numActiveThreads = m_numThreads;
|
||||
while (numActiveThreads)
|
||||
{
|
||||
if (m_threadSupport->isTaskCompleted(&arg0, &arg1, 0))
|
||||
{
|
||||
numActiveThreads--;
|
||||
printf("numActiveThreads = %d\n", numActiveThreads);
|
||||
}
|
||||
else
|
||||
{
|
||||
// printf("polling..");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
delete m_threadSupport;
|
||||
|
||||
b3Printf("Threads stopped");
|
||||
for (int i = 0; i < m_jobs.size(); i++)
|
||||
{
|
||||
delete m_jobs[i];
|
||||
}
|
||||
m_jobs.clear();
|
||||
}
|
||||
|
||||
virtual void stepSimulation(float deltaTime)
|
||||
{
|
||||
}
|
||||
|
||||
virtual void physicsDebugDraw(int debugDrawFlags)
|
||||
{
|
||||
}
|
||||
virtual bool mouseMoveCallback(float x, float y)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
virtual bool mouseButtonCallback(int button, int state, float x, float y)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
virtual bool keyboardCallback(int key, int state)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual void resetCamera()
|
||||
{
|
||||
float dist = 10.5;
|
||||
float pitch = -32;
|
||||
float yaw = 136;
|
||||
float targetPos[3] = {0, 0, 0};
|
||||
if (m_app->m_renderer && m_app->m_renderer->getActiveCamera())
|
||||
{
|
||||
m_app->m_renderer->getActiveCamera()->setCameraDistance(dist);
|
||||
m_app->m_renderer->getActiveCamera()->setCameraPitch(pitch);
|
||||
m_app->m_renderer->getActiveCamera()->setCameraYaw(yaw);
|
||||
m_app->m_renderer->getActiveCamera()->setCameraTargetPosition(targetPos[0], targetPos[1], targetPos[2]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class CommonExampleInterface* MultiThreadingExampleCreateFunc(struct CommonExampleOptions& options)
|
||||
{
|
||||
return new MultiThreadingExample(options.m_guiHelper, options.m_option);
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
#ifndef MULTI_THREADING_EXAMPLE_H
|
||||
#define MULTI_THREADING_EXAMPLE_H
|
||||
|
||||
enum EnumMultiThreadingExampleTypes
|
||||
{
|
||||
SINGLE_SIM_THREAD = 0,
|
||||
};
|
||||
|
||||
class CommonExampleInterface* MultiThreadingExampleCreateFunc(struct CommonExampleOptions& options);
|
||||
|
||||
#endif //MULTI_THREADING_EXAMPLE_H
|
||||
|
|
@ -0,0 +1,445 @@
|
|||
#ifndef _WIN32
|
||||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library
|
||||
Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty.
|
||||
In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it freely,
|
||||
subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "b3PosixThreadSupport.h"
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define checkPThreadFunction(returnValue) \
|
||||
if (0 != returnValue) \
|
||||
{ \
|
||||
printf("PThread problem at line %i in file %s: %i %d\n", __LINE__, __FILE__, returnValue, errno); \
|
||||
}
|
||||
|
||||
// The number of threads should be equal to the number of available cores
|
||||
// Todo: each worker should be linked to a single core, using SetThreadIdealProcessor.
|
||||
|
||||
b3PosixThreadSupport::b3PosixThreadSupport(ThreadConstructionInfo& threadConstructionInfo)
|
||||
{
|
||||
startThreads(threadConstructionInfo);
|
||||
}
|
||||
|
||||
// cleanup/shutdown Libspe2
|
||||
b3PosixThreadSupport::~b3PosixThreadSupport()
|
||||
{
|
||||
stopThreads();
|
||||
}
|
||||
|
||||
#if (defined(__APPLE__))
|
||||
#define NAMED_SEMAPHORES
|
||||
#endif
|
||||
|
||||
static sem_t* createSem(const char* baseName)
|
||||
{
|
||||
static int semCount = 0;
|
||||
#ifdef NAMED_SEMAPHORES
|
||||
/// Named semaphore begin
|
||||
char name[32];
|
||||
snprintf(name, 32, "/%8.s-%4.d-%4.4d", baseName, getpid(), semCount++);
|
||||
sem_t* tempSem = sem_open(name, O_CREAT, 0600, 0);
|
||||
|
||||
if (tempSem != reinterpret_cast<sem_t*>(SEM_FAILED))
|
||||
{
|
||||
// printf("Created \"%s\" Semaphore %p\n", name, tempSem);
|
||||
}
|
||||
else
|
||||
{
|
||||
//printf("Error creating Semaphore %d\n", errno);
|
||||
exit(-1);
|
||||
}
|
||||
/// Named semaphore end
|
||||
#else
|
||||
sem_t* tempSem = new sem_t;
|
||||
checkPThreadFunction(sem_init(tempSem, 0, 0));
|
||||
#endif
|
||||
return tempSem;
|
||||
}
|
||||
|
||||
static void destroySem(sem_t* semaphore)
|
||||
{
|
||||
#ifdef NAMED_SEMAPHORES
|
||||
checkPThreadFunction(sem_close(semaphore));
|
||||
#else
|
||||
checkPThreadFunction(sem_destroy(semaphore));
|
||||
delete semaphore;
|
||||
#endif
|
||||
}
|
||||
|
||||
static void* threadFunction(void* argument)
|
||||
{
|
||||
b3PosixThreadSupport::b3ThreadStatus* status = (b3PosixThreadSupport::b3ThreadStatus*)argument;
|
||||
|
||||
while (1)
|
||||
{
|
||||
checkPThreadFunction(sem_wait(status->startSemaphore));
|
||||
|
||||
void* userPtr = status->m_userPtr;
|
||||
|
||||
if (userPtr)
|
||||
{
|
||||
b3Assert(status->m_status);
|
||||
status->m_userThreadFunc(userPtr, status->m_lsMemory);
|
||||
status->m_status = 2;
|
||||
checkPThreadFunction(sem_post(status->m_mainSemaphore));
|
||||
status->threadUsed++;
|
||||
}
|
||||
else
|
||||
{
|
||||
//exit Thread
|
||||
status->m_status = 3;
|
||||
checkPThreadFunction(sem_post(status->m_mainSemaphore));
|
||||
printf("Thread with taskId %i exiting\n", status->m_taskId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Thread TERMINATED\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
///send messages to SPUs
|
||||
void b3PosixThreadSupport::runTask(int uiCommand, void* uiArgument0, int taskId)
|
||||
{
|
||||
/// gMidphaseSPU.sendRequest(CMD_GATHER_AND_PROCESS_PAIRLIST, (int) &taskDesc);
|
||||
|
||||
///we should spawn an SPU task here, and in 'waitForResponse' it should wait for response of the (one of) the first tasks that finished
|
||||
|
||||
switch (uiCommand)
|
||||
{
|
||||
case B3_THREAD_SCHEDULE_TASK:
|
||||
{
|
||||
b3ThreadStatus& spuStatus = m_activeThreadStatus[taskId];
|
||||
b3Assert(taskId >= 0);
|
||||
b3Assert(taskId < m_activeThreadStatus.size());
|
||||
|
||||
spuStatus.m_commandId = uiCommand;
|
||||
spuStatus.m_status = 1;
|
||||
spuStatus.m_userPtr = (void*)uiArgument0;
|
||||
|
||||
// fire event to start new task
|
||||
checkPThreadFunction(sem_post(spuStatus.startSemaphore));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
///not implemented
|
||||
b3Assert(0);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
///non-blocking test if a task is completed. First implement all versions, and then enable this API
|
||||
bool b3PosixThreadSupport::isTaskCompleted(int* puiArgument0, int* puiArgument1, int timeOutInMilliseconds)
|
||||
{
|
||||
b3Assert(m_activeThreadStatus.size());
|
||||
|
||||
// wait for any of the threads to finish
|
||||
int result = sem_trywait(m_mainSemaphore);
|
||||
if (result == 0)
|
||||
{
|
||||
// get at least one thread which has finished
|
||||
int last = -1;
|
||||
int status = -1;
|
||||
for (int t = 0; t < int(m_activeThreadStatus.size()); ++t)
|
||||
{
|
||||
status = m_activeThreadStatus[t].m_status;
|
||||
if (2 == m_activeThreadStatus[t].m_status)
|
||||
{
|
||||
last = t;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
b3ThreadStatus& spuStatus = m_activeThreadStatus[last];
|
||||
|
||||
b3Assert(spuStatus.m_status > 1);
|
||||
spuStatus.m_status = 0;
|
||||
|
||||
// need to find an active spu
|
||||
b3Assert(last >= 0);
|
||||
|
||||
*puiArgument0 = spuStatus.m_taskId;
|
||||
*puiArgument1 = spuStatus.m_status;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
///check for messages from SPUs
|
||||
void b3PosixThreadSupport::waitForResponse(int* puiArgument0, int* puiArgument1)
|
||||
{
|
||||
///We should wait for (one of) the first tasks to finish (or other SPU messages), and report its response
|
||||
|
||||
///A possible response can be 'yes, SPU handled it', or 'no, please do a PPU fallback'
|
||||
|
||||
b3Assert(m_activeThreadStatus.size());
|
||||
|
||||
// wait for any of the threads to finish
|
||||
checkPThreadFunction(sem_wait(m_mainSemaphore));
|
||||
|
||||
// get at least one thread which has finished
|
||||
size_t last = -1;
|
||||
|
||||
for (size_t t = 0; t < size_t(m_activeThreadStatus.size()); ++t)
|
||||
{
|
||||
if (2 == m_activeThreadStatus[t].m_status)
|
||||
{
|
||||
last = t;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
b3ThreadStatus& spuStatus = m_activeThreadStatus[last];
|
||||
|
||||
b3Assert(spuStatus.m_status > 1);
|
||||
spuStatus.m_status = 0;
|
||||
|
||||
// need to find an active spu
|
||||
b3Assert(last >= 0);
|
||||
|
||||
*puiArgument0 = spuStatus.m_taskId;
|
||||
*puiArgument1 = spuStatus.m_status;
|
||||
}
|
||||
|
||||
void b3PosixThreadSupport::startThreads(ThreadConstructionInfo& threadConstructionInfo)
|
||||
{
|
||||
printf("%s creating %i threads.\n", __FUNCTION__, threadConstructionInfo.m_numThreads);
|
||||
m_activeThreadStatus.resize(threadConstructionInfo.m_numThreads);
|
||||
|
||||
m_mainSemaphore = createSem("main");
|
||||
//checkPThreadFunction(sem_wait(mainSemaphore));
|
||||
|
||||
for (int i = 0; i < threadConstructionInfo.m_numThreads; i++)
|
||||
{
|
||||
printf("starting thread %d\n", i);
|
||||
|
||||
b3ThreadStatus& spuStatus = m_activeThreadStatus[i];
|
||||
|
||||
spuStatus.startSemaphore = createSem("threadLocal");
|
||||
|
||||
checkPThreadFunction(pthread_create(&spuStatus.thread, NULL, &threadFunction, (void*)&spuStatus));
|
||||
|
||||
spuStatus.m_userPtr = 0;
|
||||
|
||||
spuStatus.m_taskId = i;
|
||||
spuStatus.m_commandId = 0;
|
||||
spuStatus.m_status = 0;
|
||||
spuStatus.m_mainSemaphore = m_mainSemaphore;
|
||||
spuStatus.m_lsMemory = threadConstructionInfo.m_lsMemoryFunc();
|
||||
spuStatus.m_userThreadFunc = threadConstructionInfo.m_userThreadFunc;
|
||||
spuStatus.m_lsMemoryReleaseFunc = threadConstructionInfo.m_lsMemoryReleaseFunc;
|
||||
spuStatus.threadUsed = 0;
|
||||
|
||||
printf("started thread %d \n", i);
|
||||
}
|
||||
}
|
||||
|
||||
///tell the task scheduler we are done with the SPU tasks
|
||||
void b3PosixThreadSupport::stopThreads()
|
||||
{
|
||||
for (size_t t = 0; t < size_t(m_activeThreadStatus.size()); ++t)
|
||||
{
|
||||
b3ThreadStatus& spuStatus = m_activeThreadStatus[t];
|
||||
|
||||
// printf("%s: Thread %i used: %ld\n", __FUNCTION__, int(t), spuStatus.threadUsed);
|
||||
|
||||
spuStatus.m_userPtr = 0;
|
||||
checkPThreadFunction(sem_post(spuStatus.startSemaphore));
|
||||
checkPThreadFunction(sem_wait(m_mainSemaphore));
|
||||
|
||||
printf("destroy semaphore\n");
|
||||
destroySem(spuStatus.startSemaphore);
|
||||
printf("semaphore destroyed\n");
|
||||
checkPThreadFunction(pthread_join(spuStatus.thread, 0));
|
||||
|
||||
if (spuStatus.m_lsMemoryReleaseFunc)
|
||||
{
|
||||
spuStatus.m_lsMemoryReleaseFunc(spuStatus.m_lsMemory);
|
||||
}
|
||||
}
|
||||
printf("destroy main semaphore\n");
|
||||
destroySem(m_mainSemaphore);
|
||||
printf("main semaphore destroyed\n");
|
||||
m_activeThreadStatus.clear();
|
||||
}
|
||||
|
||||
class b3PosixCriticalSection : public b3CriticalSection
|
||||
{
|
||||
pthread_mutex_t m_mutex;
|
||||
|
||||
public:
|
||||
b3PosixCriticalSection()
|
||||
{
|
||||
pthread_mutex_init(&m_mutex, NULL);
|
||||
}
|
||||
virtual ~b3PosixCriticalSection()
|
||||
{
|
||||
pthread_mutex_destroy(&m_mutex);
|
||||
}
|
||||
|
||||
B3_ATTRIBUTE_ALIGNED16(unsigned int mCommonBuff[32]);
|
||||
|
||||
virtual unsigned int getSharedParam(int i)
|
||||
{
|
||||
if (i < 32)
|
||||
{
|
||||
return mCommonBuff[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
b3Assert(0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
virtual void setSharedParam(int i, unsigned int p)
|
||||
{
|
||||
if (i < 32)
|
||||
{
|
||||
mCommonBuff[i] = p;
|
||||
}
|
||||
else
|
||||
{
|
||||
b3Assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void lock()
|
||||
{
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
}
|
||||
virtual void unlock()
|
||||
{
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
}
|
||||
};
|
||||
|
||||
#if defined(_POSIX_BARRIERS) && (_POSIX_BARRIERS - 20012L) >= 0
|
||||
/* OK to use barriers on this platform */
|
||||
class b3PosixBarrier : public b3Barrier
|
||||
{
|
||||
pthread_barrier_t m_barr;
|
||||
int m_numThreads;
|
||||
|
||||
public:
|
||||
b3PosixBarrier()
|
||||
: m_numThreads(0) {}
|
||||
virtual ~b3PosixBarrier()
|
||||
{
|
||||
pthread_barrier_destroy(&m_barr);
|
||||
}
|
||||
|
||||
virtual void sync()
|
||||
{
|
||||
int rc = pthread_barrier_wait(&m_barr);
|
||||
if (rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD)
|
||||
{
|
||||
printf("Could not wait on barrier\n");
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
virtual void setMaxCount(int numThreads)
|
||||
{
|
||||
int result = pthread_barrier_init(&m_barr, NULL, numThreads);
|
||||
m_numThreads = numThreads;
|
||||
b3Assert(result == 0);
|
||||
}
|
||||
virtual int getMaxCount()
|
||||
{
|
||||
return m_numThreads;
|
||||
}
|
||||
};
|
||||
#else
|
||||
/* Not OK to use barriers on this platform - insert alternate code here */
|
||||
class b3PosixBarrier : public b3Barrier
|
||||
{
|
||||
pthread_mutex_t m_mutex;
|
||||
pthread_cond_t m_cond;
|
||||
|
||||
int m_numThreads;
|
||||
int m_called;
|
||||
|
||||
public:
|
||||
b3PosixBarrier()
|
||||
: m_numThreads(0)
|
||||
{
|
||||
}
|
||||
virtual ~b3PosixBarrier()
|
||||
{
|
||||
if (m_numThreads > 0)
|
||||
{
|
||||
pthread_mutex_destroy(&m_mutex);
|
||||
pthread_cond_destroy(&m_cond);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void sync()
|
||||
{
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
m_called++;
|
||||
if (m_called == m_numThreads)
|
||||
{
|
||||
m_called = 0;
|
||||
pthread_cond_broadcast(&m_cond);
|
||||
}
|
||||
else
|
||||
{
|
||||
pthread_cond_wait(&m_cond, &m_mutex);
|
||||
}
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
}
|
||||
virtual void setMaxCount(int numThreads)
|
||||
{
|
||||
if (m_numThreads > 0)
|
||||
{
|
||||
pthread_mutex_destroy(&m_mutex);
|
||||
pthread_cond_destroy(&m_cond);
|
||||
}
|
||||
m_called = 0;
|
||||
pthread_mutex_init(&m_mutex, NULL);
|
||||
pthread_cond_init(&m_cond, NULL);
|
||||
m_numThreads = numThreads;
|
||||
}
|
||||
virtual int getMaxCount()
|
||||
{
|
||||
return m_numThreads;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //_POSIX_BARRIERS
|
||||
|
||||
b3Barrier* b3PosixThreadSupport::createBarrier()
|
||||
{
|
||||
b3PosixBarrier* barrier = new b3PosixBarrier();
|
||||
barrier->setMaxCount(getNumTasks());
|
||||
return barrier;
|
||||
}
|
||||
|
||||
b3CriticalSection* b3PosixThreadSupport::createCriticalSection()
|
||||
{
|
||||
return new b3PosixCriticalSection();
|
||||
}
|
||||
|
||||
void b3PosixThreadSupport::deleteBarrier(b3Barrier* barrier)
|
||||
{
|
||||
delete barrier;
|
||||
}
|
||||
|
||||
void b3PosixThreadSupport::deleteCriticalSection(b3CriticalSection* cs)
|
||||
{
|
||||
delete cs;
|
||||
}
|
||||
#endif //_WIN32
|
||||
142
Engine/lib/bullet/examples/MultiThreading/b3PosixThreadSupport.h
Normal file
142
Engine/lib/bullet/examples/MultiThreading/b3PosixThreadSupport.h
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library
|
||||
Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty.
|
||||
In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it freely,
|
||||
subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B3_POSIX_THREAD_SUPPORT_H
|
||||
#define B3_POSIX_THREAD_SUPPORT_H
|
||||
|
||||
#include "Bullet3Common/b3Scalar.h"
|
||||
|
||||
#ifndef _XOPEN_SOURCE
|
||||
#define _XOPEN_SOURCE 600 //for definition of pthread_barrier_t, see http://pages.cs.wisc.edu/~travitch/pthreads_primer.html
|
||||
#endif //_XOPEN_SOURCE
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
|
||||
#include "Bullet3Common/b3AlignedObjectArray.h"
|
||||
|
||||
#include "b3ThreadSupportInterface.h"
|
||||
|
||||
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
|
||||
{
|
||||
public:
|
||||
typedef enum sStatus
|
||||
{
|
||||
STATUS_BUSY,
|
||||
STATUS_READY,
|
||||
STATUS_FINISHED
|
||||
} Status;
|
||||
|
||||
// placeholder, until libspe2 support is there
|
||||
struct b3ThreadStatus
|
||||
{
|
||||
int m_taskId;
|
||||
int m_commandId;
|
||||
int m_status;
|
||||
|
||||
b3PosixThreadFunc m_userThreadFunc;
|
||||
void* m_userPtr; //for taskDesc etc
|
||||
b3PosixlsMemoryReleaseFunc m_lsMemoryReleaseFunc;
|
||||
|
||||
void* m_lsMemory; //initialized using PosixLocalStoreMemorySetupFunc
|
||||
|
||||
pthread_t thread;
|
||||
//each tread will wait until this signal to start its work
|
||||
sem_t* startSemaphore;
|
||||
|
||||
// this is a copy of m_mainSemaphore,
|
||||
//each tread will signal once it is finished with its work
|
||||
sem_t* m_mainSemaphore;
|
||||
unsigned long threadUsed;
|
||||
};
|
||||
|
||||
private:
|
||||
b3AlignedObjectArray<b3ThreadStatus> m_activeThreadStatus;
|
||||
|
||||
// m_mainSemaphoresemaphore will signal, if and how many threads are finished with their work
|
||||
sem_t* m_mainSemaphore;
|
||||
|
||||
public:
|
||||
///Setup and initialize SPU/CELL/Libspe2
|
||||
|
||||
struct ThreadConstructionInfo
|
||||
{
|
||||
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)
|
||||
{
|
||||
}
|
||||
|
||||
const char* m_uniqueName;
|
||||
b3PosixThreadFunc m_userThreadFunc;
|
||||
b3PosixlsMemorySetupFunc m_lsMemoryFunc;
|
||||
b3PosixlsMemoryReleaseFunc m_lsMemoryReleaseFunc;
|
||||
|
||||
int m_numThreads;
|
||||
int m_threadStackSize;
|
||||
};
|
||||
|
||||
b3PosixThreadSupport(ThreadConstructionInfo& threadConstructionInfo);
|
||||
|
||||
///cleanup/shutdown Libspe2
|
||||
virtual ~b3PosixThreadSupport();
|
||||
|
||||
void startThreads(ThreadConstructionInfo& threadInfo);
|
||||
|
||||
virtual void runTask(int uiCommand, void* uiArgument0, int uiArgument1);
|
||||
|
||||
virtual void waitForResponse(int* puiArgument0, int* puiArgument1);
|
||||
|
||||
///tell the task scheduler we are done with the SPU tasks
|
||||
virtual void stopThreads();
|
||||
|
||||
virtual void setNumTasks(int numTasks) {}
|
||||
|
||||
virtual int getNumTasks() const
|
||||
{
|
||||
return m_activeThreadStatus.size();
|
||||
}
|
||||
|
||||
///non-blocking test if a task is completed. First implement all versions, and then enable this API
|
||||
virtual bool isTaskCompleted(int* puiArgument0, int* puiArgument1, int timeOutInMilliseconds);
|
||||
|
||||
virtual b3Barrier* createBarrier();
|
||||
|
||||
virtual b3CriticalSection* createCriticalSection();
|
||||
|
||||
virtual void deleteBarrier(b3Barrier* barrier);
|
||||
|
||||
virtual void deleteCriticalSection(b3CriticalSection* criticalSection);
|
||||
|
||||
virtual void* getThreadLocalMemory(int taskId)
|
||||
{
|
||||
return m_activeThreadStatus[taskId].m_lsMemory;
|
||||
}
|
||||
};
|
||||
|
||||
#endif // B3_POSIX_THREAD_SUPPORT_H
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library
|
||||
Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty.
|
||||
In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it freely,
|
||||
subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include "b3ThreadSupportInterface.h"
|
||||
|
||||
b3ThreadSupportInterface::~b3ThreadSupportInterface()
|
||||
{
|
||||
}
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library
|
||||
Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty.
|
||||
In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it freely,
|
||||
subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B3_THREAD_SUPPORT_INTERFACE_H
|
||||
#define B3_THREAD_SUPPORT_INTERFACE_H
|
||||
|
||||
enum
|
||||
{
|
||||
B3_THREAD_SCHEDULE_TASK = 1,
|
||||
};
|
||||
|
||||
#include "Bullet3Common/b3Scalar.h" //for B3_ATTRIBUTE_ALIGNED16
|
||||
//#include "PlatformDefinitions.h"
|
||||
//#include "PpuAddressSpace.h"
|
||||
|
||||
class b3Barrier
|
||||
{
|
||||
public:
|
||||
b3Barrier() {}
|
||||
virtual ~b3Barrier() {}
|
||||
|
||||
virtual void sync() = 0;
|
||||
virtual void setMaxCount(int n) = 0;
|
||||
virtual int getMaxCount() = 0;
|
||||
};
|
||||
|
||||
class b3CriticalSection
|
||||
{
|
||||
public:
|
||||
b3CriticalSection() {}
|
||||
virtual ~b3CriticalSection() {}
|
||||
|
||||
B3_ATTRIBUTE_ALIGNED16(unsigned int mCommonBuff[32]);
|
||||
|
||||
virtual unsigned int getSharedParam(int i) = 0;
|
||||
virtual void setSharedParam(int i, unsigned int p) = 0;
|
||||
|
||||
virtual void lock() = 0;
|
||||
virtual void unlock() = 0;
|
||||
};
|
||||
|
||||
class b3ThreadSupportInterface
|
||||
{
|
||||
public:
|
||||
virtual ~b3ThreadSupportInterface();
|
||||
|
||||
virtual void runTask(int uiCommand, void* uiArgument0, int uiArgument1) = 0;
|
||||
|
||||
virtual void waitForResponse(int* puiArgument0, int* puiArgument1) = 0;
|
||||
|
||||
///non-blocking test if a task is completed. First implement all versions, and then enable this API
|
||||
virtual bool isTaskCompleted(int* puiArgument0, int* puiArgument1, int timeOutInMilliseconds) = 0;
|
||||
|
||||
virtual void stopThreads() = 0;
|
||||
|
||||
///tell the task scheduler to use no more than numTasks tasks
|
||||
virtual void setNumTasks(int numTasks) = 0;
|
||||
|
||||
virtual int getNumTasks() const = 0;
|
||||
|
||||
virtual b3Barrier* createBarrier() = 0;
|
||||
|
||||
virtual b3CriticalSection* createCriticalSection() = 0;
|
||||
|
||||
virtual void deleteBarrier(b3Barrier* barrier) = 0;
|
||||
|
||||
virtual void deleteCriticalSection(b3CriticalSection* criticalSection) = 0;
|
||||
|
||||
virtual void* getThreadLocalMemory(int taskId) { return 0; }
|
||||
};
|
||||
|
||||
#endif //B3_THREAD_SUPPORT_INTERFACE_H
|
||||
|
|
@ -0,0 +1,444 @@
|
|||
#ifdef _WIN32
|
||||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library
|
||||
Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty.
|
||||
In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it freely,
|
||||
subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include "b3Win32ThreadSupport.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
///The number of threads should be equal to the number of available cores
|
||||
///@todo: each worker should be linked to a single core, using SetThreadIdealProcessor.
|
||||
|
||||
///b3Win32ThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
|
||||
///Setup and initialize SPU/CELL/Libspe2
|
||||
b3Win32ThreadSupport::b3Win32ThreadSupport(const Win32ThreadConstructionInfo& threadConstructionInfo)
|
||||
{
|
||||
m_maxNumTasks = threadConstructionInfo.m_numThreads;
|
||||
startThreads(threadConstructionInfo);
|
||||
}
|
||||
|
||||
///cleanup/shutdown Libspe2
|
||||
b3Win32ThreadSupport::~b3Win32ThreadSupport()
|
||||
{
|
||||
stopThreads();
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
DWORD WINAPI Thread_no_1(LPVOID lpParam)
|
||||
{
|
||||
b3Win32ThreadSupport::b3ThreadStatus* status = (b3Win32ThreadSupport::b3ThreadStatus*)lpParam;
|
||||
|
||||
while (1)
|
||||
{
|
||||
WaitForSingleObject(status->m_eventStartHandle, INFINITE);
|
||||
|
||||
void* userPtr = status->m_userPtr;
|
||||
|
||||
if (userPtr)
|
||||
{
|
||||
b3Assert(status->m_status);
|
||||
status->m_userThreadFunc(userPtr, status->m_lsMemory);
|
||||
status->m_status = 2;
|
||||
SetEvent(status->m_eventCompletetHandle);
|
||||
}
|
||||
else
|
||||
{
|
||||
//exit Thread
|
||||
status->m_status = 3;
|
||||
printf("Thread with taskId %i with handle %p exiting\n", status->m_taskId, status->m_threadHandle);
|
||||
SetEvent(status->m_eventCompletetHandle);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Thread TERMINATED\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
///send messages to SPUs
|
||||
void b3Win32ThreadSupport::runTask(int uiCommand, void* uiArgument0, int taskId)
|
||||
{
|
||||
/// gMidphaseSPU.sendRequest(CMD_GATHER_AND_PROCESS_PAIRLIST, (void*) &taskDesc);
|
||||
|
||||
///we should spawn an SPU task here, and in 'waitForResponse' it should wait for response of the (one of) the first tasks that finished
|
||||
|
||||
switch (uiCommand)
|
||||
{
|
||||
case B3_THREAD_SCHEDULE_TASK:
|
||||
{
|
||||
//#define SINGLE_THREADED 1
|
||||
#ifdef SINGLE_THREADED
|
||||
|
||||
b3ThreadStatus& threadStatus = m_activeThreadStatus[0];
|
||||
threadStatus.m_userPtr = (void*)uiArgument0;
|
||||
threadStatus.m_userThreadFunc(threadStatus.m_userPtr, threadStatus.m_lsMemory);
|
||||
HANDLE handle = 0;
|
||||
#else
|
||||
|
||||
b3ThreadStatus& threadStatus = m_activeThreadStatus[taskId];
|
||||
b3Assert(taskId >= 0);
|
||||
b3Assert(int(taskId) < m_activeThreadStatus.size());
|
||||
|
||||
threadStatus.m_commandId = uiCommand;
|
||||
threadStatus.m_status = 1;
|
||||
threadStatus.m_userPtr = (void*)uiArgument0;
|
||||
|
||||
///fire event to start new task
|
||||
SetEvent(threadStatus.m_eventStartHandle);
|
||||
|
||||
#endif //CollisionTask_LocalStoreMemory
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
///not implemented
|
||||
b3Assert(0);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
///check for messages from SPUs
|
||||
void b3Win32ThreadSupport::waitForResponse(int* puiArgument0, int* puiArgument1)
|
||||
{
|
||||
///We should wait for (one of) the first tasks to finish (or other SPU messages), and report its response
|
||||
|
||||
///A possible response can be 'yes, SPU handled it', or 'no, please do a PPU fallback'
|
||||
|
||||
b3Assert(m_activeThreadStatus.size());
|
||||
|
||||
int last = -1;
|
||||
#ifndef SINGLE_THREADED
|
||||
DWORD res = WaitForMultipleObjects(m_completeHandles.size(), &m_completeHandles[0], FALSE, INFINITE);
|
||||
b3Assert(res != WAIT_FAILED);
|
||||
last = res - WAIT_OBJECT_0;
|
||||
|
||||
b3ThreadStatus& threadStatus = m_activeThreadStatus[last];
|
||||
b3Assert(threadStatus.m_threadHandle);
|
||||
b3Assert(threadStatus.m_eventCompletetHandle);
|
||||
|
||||
//WaitForSingleObject(threadStatus.m_eventCompletetHandle, INFINITE);
|
||||
b3Assert(threadStatus.m_status > 1);
|
||||
threadStatus.m_status = 0;
|
||||
|
||||
///need to find an active spu
|
||||
b3Assert(last >= 0);
|
||||
|
||||
#else
|
||||
last = 0;
|
||||
b3ThreadStatus& threadStatus = m_activeThreadStatus[last];
|
||||
#endif //SINGLE_THREADED
|
||||
|
||||
*puiArgument0 = threadStatus.m_taskId;
|
||||
*puiArgument1 = threadStatus.m_status;
|
||||
}
|
||||
|
||||
///check for messages from SPUs
|
||||
bool b3Win32ThreadSupport::isTaskCompleted(int* puiArgument0, int* puiArgument1, int timeOutInMilliseconds)
|
||||
{
|
||||
///We should wait for (one of) the first tasks to finish (or other SPU messages), and report its response
|
||||
|
||||
///A possible response can be 'yes, SPU handled it', or 'no, please do a PPU fallback'
|
||||
|
||||
b3Assert(m_activeThreadStatus.size());
|
||||
|
||||
int last = -1;
|
||||
#ifndef SINGLE_THREADED
|
||||
DWORD res = WaitForMultipleObjects(m_completeHandles.size(), &m_completeHandles[0], FALSE, timeOutInMilliseconds);
|
||||
|
||||
if ((res != STATUS_TIMEOUT) && (res != WAIT_FAILED))
|
||||
{
|
||||
b3Assert(res != WAIT_FAILED);
|
||||
last = res - WAIT_OBJECT_0;
|
||||
|
||||
b3ThreadStatus& threadStatus = m_activeThreadStatus[last];
|
||||
b3Assert(threadStatus.m_threadHandle);
|
||||
b3Assert(threadStatus.m_eventCompletetHandle);
|
||||
|
||||
//WaitForSingleObject(threadStatus.m_eventCompletetHandle, INFINITE);
|
||||
b3Assert(threadStatus.m_status > 1);
|
||||
threadStatus.m_status = 0;
|
||||
|
||||
///need to find an active spu
|
||||
b3Assert(last >= 0);
|
||||
|
||||
#else
|
||||
last = 0;
|
||||
b3ThreadStatus& threadStatus = m_activeThreadStatus[last];
|
||||
#endif //SINGLE_THREADED
|
||||
|
||||
*puiArgument0 = threadStatus.m_taskId;
|
||||
*puiArgument1 = threadStatus.m_status;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void b3Win32ThreadSupport::startThreads(const Win32ThreadConstructionInfo& threadConstructionInfo)
|
||||
{
|
||||
static int uniqueId = 0;
|
||||
uniqueId++;
|
||||
m_activeThreadStatus.resize(threadConstructionInfo.m_numThreads);
|
||||
m_completeHandles.resize(threadConstructionInfo.m_numThreads);
|
||||
|
||||
m_maxNumTasks = threadConstructionInfo.m_numThreads;
|
||||
|
||||
for (int i = 0; i < threadConstructionInfo.m_numThreads; i++)
|
||||
{
|
||||
printf("starting thread %d\n", i);
|
||||
|
||||
b3ThreadStatus& threadStatus = m_activeThreadStatus[i];
|
||||
|
||||
LPSECURITY_ATTRIBUTES lpThreadAttributes = NULL;
|
||||
SIZE_T dwStackSize = threadConstructionInfo.m_threadStackSize;
|
||||
LPTHREAD_START_ROUTINE lpStartAddress = &Thread_no_1;
|
||||
LPVOID lpParameter = &threadStatus;
|
||||
DWORD dwCreationFlags = 0;
|
||||
LPDWORD lpThreadId = 0;
|
||||
|
||||
threadStatus.m_userPtr = 0;
|
||||
|
||||
sprintf(threadStatus.m_eventStartHandleName, "es%.8s%d%d", threadConstructionInfo.m_uniqueName, uniqueId, i);
|
||||
threadStatus.m_eventStartHandle = CreateEventA(0, false, false, threadStatus.m_eventStartHandleName);
|
||||
|
||||
sprintf(threadStatus.m_eventCompletetHandleName, "ec%.8s%d%d", threadConstructionInfo.m_uniqueName, uniqueId, i);
|
||||
threadStatus.m_eventCompletetHandle = CreateEventA(0, false, false, threadStatus.m_eventCompletetHandleName);
|
||||
|
||||
m_completeHandles[i] = threadStatus.m_eventCompletetHandle;
|
||||
|
||||
HANDLE handle = CreateThread(lpThreadAttributes, dwStackSize, lpStartAddress, lpParameter, dwCreationFlags, lpThreadId);
|
||||
switch (threadConstructionInfo.m_priority)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
SetThreadPriority(handle, THREAD_PRIORITY_HIGHEST);
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL);
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
SetThreadPriority(handle, THREAD_PRIORITY_BELOW_NORMAL);
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
//SetThreadAffinityMask(handle, 1 << 1); // this is what it was doing originally, a complete disaster for threading performance!
|
||||
//SetThreadAffinityMask(handle, 1 << i); // I'm guessing this was the intention, but is still bad for performance due to one of the threads
|
||||
// sometimes unable to execute because it wants to be on the same processor as the main thread (my guess)
|
||||
|
||||
threadStatus.m_taskId = i;
|
||||
threadStatus.m_commandId = 0;
|
||||
threadStatus.m_status = 0;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void b3Win32ThreadSupport::startThreads()
|
||||
{
|
||||
}
|
||||
|
||||
///tell the task scheduler we are done with the SPU tasks
|
||||
void b3Win32ThreadSupport::stopThreads()
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < m_activeThreadStatus.size(); i++)
|
||||
{
|
||||
b3ThreadStatus& threadStatus = m_activeThreadStatus[i];
|
||||
if (threadStatus.m_status > 0)
|
||||
{
|
||||
WaitForSingleObject(threadStatus.m_eventCompletetHandle, INFINITE);
|
||||
}
|
||||
|
||||
if (threadStatus.m_lsMemoryReleaseFunc)
|
||||
{
|
||||
threadStatus.m_lsMemoryReleaseFunc(threadStatus.m_lsMemory);
|
||||
}
|
||||
|
||||
threadStatus.m_userPtr = 0;
|
||||
SetEvent(threadStatus.m_eventStartHandle);
|
||||
WaitForSingleObject(threadStatus.m_eventCompletetHandle, INFINITE);
|
||||
|
||||
CloseHandle(threadStatus.m_eventCompletetHandle);
|
||||
CloseHandle(threadStatus.m_eventStartHandle);
|
||||
CloseHandle(threadStatus.m_threadHandle);
|
||||
}
|
||||
|
||||
m_activeThreadStatus.clear();
|
||||
m_completeHandles.clear();
|
||||
}
|
||||
|
||||
class b3Win32Barrier : public b3Barrier
|
||||
{
|
||||
private:
|
||||
CRITICAL_SECTION mExternalCriticalSection;
|
||||
CRITICAL_SECTION mLocalCriticalSection;
|
||||
HANDLE mRunEvent, mNotifyEvent;
|
||||
int mCounter, mEnableCounter;
|
||||
int mMaxCount;
|
||||
|
||||
public:
|
||||
b3Win32Barrier()
|
||||
{
|
||||
mCounter = 0;
|
||||
mMaxCount = 1;
|
||||
mEnableCounter = 0;
|
||||
InitializeCriticalSection(&mExternalCriticalSection);
|
||||
InitializeCriticalSection(&mLocalCriticalSection);
|
||||
mRunEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
||||
mNotifyEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
||||
}
|
||||
|
||||
virtual ~b3Win32Barrier()
|
||||
{
|
||||
DeleteCriticalSection(&mExternalCriticalSection);
|
||||
DeleteCriticalSection(&mLocalCriticalSection);
|
||||
CloseHandle(mRunEvent);
|
||||
CloseHandle(mNotifyEvent);
|
||||
}
|
||||
|
||||
void sync()
|
||||
{
|
||||
int eventId;
|
||||
|
||||
EnterCriticalSection(&mExternalCriticalSection);
|
||||
|
||||
//PFX_PRINTF("enter taskId %d count %d stage %d phase %d mEnableCounter %d\n",taskId,mCounter,debug&0xff,debug>>16,mEnableCounter);
|
||||
|
||||
if (mEnableCounter > 0)
|
||||
{
|
||||
ResetEvent(mNotifyEvent);
|
||||
LeaveCriticalSection(&mExternalCriticalSection);
|
||||
WaitForSingleObject(mNotifyEvent, INFINITE);
|
||||
EnterCriticalSection(&mExternalCriticalSection);
|
||||
}
|
||||
|
||||
eventId = mCounter;
|
||||
mCounter++;
|
||||
|
||||
if (eventId == mMaxCount - 1)
|
||||
{
|
||||
SetEvent(mRunEvent);
|
||||
|
||||
mEnableCounter = mCounter - 1;
|
||||
mCounter = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
ResetEvent(mRunEvent);
|
||||
LeaveCriticalSection(&mExternalCriticalSection);
|
||||
WaitForSingleObject(mRunEvent, INFINITE);
|
||||
EnterCriticalSection(&mExternalCriticalSection);
|
||||
mEnableCounter--;
|
||||
}
|
||||
|
||||
if (mEnableCounter == 0)
|
||||
{
|
||||
SetEvent(mNotifyEvent);
|
||||
}
|
||||
|
||||
//PFX_PRINTF("leave taskId %d count %d stage %d phase %d mEnableCounter %d\n",taskId,mCounter,debug&0xff,debug>>16,mEnableCounter);
|
||||
|
||||
LeaveCriticalSection(&mExternalCriticalSection);
|
||||
}
|
||||
|
||||
virtual void setMaxCount(int n) { mMaxCount = n; }
|
||||
virtual int getMaxCount() { return mMaxCount; }
|
||||
};
|
||||
|
||||
class b3Win32CriticalSection : public b3CriticalSection
|
||||
{
|
||||
private:
|
||||
CRITICAL_SECTION mCriticalSection;
|
||||
|
||||
public:
|
||||
b3Win32CriticalSection()
|
||||
{
|
||||
InitializeCriticalSection(&mCriticalSection);
|
||||
}
|
||||
|
||||
~b3Win32CriticalSection()
|
||||
{
|
||||
DeleteCriticalSection(&mCriticalSection);
|
||||
}
|
||||
|
||||
unsigned int getSharedParam(int i)
|
||||
{
|
||||
b3Assert(i >= 0 && i < 31);
|
||||
return mCommonBuff[i + 1];
|
||||
}
|
||||
|
||||
void setSharedParam(int i, unsigned int p)
|
||||
{
|
||||
b3Assert(i >= 0 && i < 31);
|
||||
mCommonBuff[i + 1] = p;
|
||||
}
|
||||
|
||||
void lock()
|
||||
{
|
||||
EnterCriticalSection(&mCriticalSection);
|
||||
mCommonBuff[0] = 1;
|
||||
}
|
||||
|
||||
void unlock()
|
||||
{
|
||||
mCommonBuff[0] = 0;
|
||||
LeaveCriticalSection(&mCriticalSection);
|
||||
}
|
||||
};
|
||||
|
||||
b3Barrier* b3Win32ThreadSupport::createBarrier()
|
||||
{
|
||||
unsigned char* mem = (unsigned char*)b3AlignedAlloc(sizeof(b3Win32Barrier), 16);
|
||||
b3Win32Barrier* barrier = new (mem) b3Win32Barrier();
|
||||
barrier->setMaxCount(getNumTasks());
|
||||
return barrier;
|
||||
}
|
||||
|
||||
b3CriticalSection* b3Win32ThreadSupport::createCriticalSection()
|
||||
{
|
||||
unsigned char* mem = (unsigned char*)b3AlignedAlloc(sizeof(b3Win32CriticalSection), 16);
|
||||
b3Win32CriticalSection* cs = new (mem) b3Win32CriticalSection();
|
||||
return cs;
|
||||
}
|
||||
|
||||
void b3Win32ThreadSupport::deleteBarrier(b3Barrier* barrier)
|
||||
{
|
||||
barrier->~b3Barrier();
|
||||
b3AlignedFree(barrier);
|
||||
}
|
||||
|
||||
void b3Win32ThreadSupport::deleteCriticalSection(b3CriticalSection* criticalSection)
|
||||
{
|
||||
criticalSection->~b3CriticalSection();
|
||||
b3AlignedFree(criticalSection);
|
||||
}
|
||||
|
||||
#endif //_WIN32
|
||||
135
Engine/lib/bullet/examples/MultiThreading/b3Win32ThreadSupport.h
Normal file
135
Engine/lib/bullet/examples/MultiThreading/b3Win32ThreadSupport.h
Normal file
|
|
@ -0,0 +1,135 @@
|
|||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library
|
||||
Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty.
|
||||
In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it freely,
|
||||
subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#include "Bullet3Common/b3Scalar.h"
|
||||
|
||||
#ifndef BT_WIN32_THREAD_SUPPORT_H
|
||||
#define BT_WIN32_THREAD_SUPPORT_H
|
||||
|
||||
#include "Bullet3Common/b3AlignedObjectArray.h"
|
||||
|
||||
#include "b3ThreadSupportInterface.h"
|
||||
|
||||
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
|
||||
class b3Win32ThreadSupport : public b3ThreadSupportInterface
|
||||
{
|
||||
public:
|
||||
///placeholder, until libspe2 support is there
|
||||
struct b3ThreadStatus
|
||||
{
|
||||
int m_taskId;
|
||||
int m_commandId;
|
||||
int m_status;
|
||||
|
||||
b3Win32ThreadFunc m_userThreadFunc;
|
||||
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;
|
||||
char m_eventStartHandleName[32];
|
||||
|
||||
void* m_eventCompletetHandle;
|
||||
char m_eventCompletetHandleName[32];
|
||||
};
|
||||
|
||||
private:
|
||||
b3AlignedObjectArray<b3ThreadStatus> m_activeThreadStatus;
|
||||
b3AlignedObjectArray<void*> m_completeHandles;
|
||||
|
||||
int m_maxNumTasks;
|
||||
|
||||
public:
|
||||
///Setup and initialize SPU/CELL/Libspe2
|
||||
|
||||
struct Win32ThreadConstructionInfo
|
||||
{
|
||||
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)
|
||||
{
|
||||
}
|
||||
|
||||
const char* m_uniqueName;
|
||||
b3Win32ThreadFunc m_userThreadFunc;
|
||||
b3Win32lsMemorySetupFunc m_lsMemoryFunc;
|
||||
b3Win32lsMemoryReleaseFunc m_lsMemoryReleaseFunc;
|
||||
int m_numThreads;
|
||||
int m_threadStackSize;
|
||||
int m_priority;
|
||||
};
|
||||
|
||||
b3Win32ThreadSupport(const Win32ThreadConstructionInfo& threadConstructionInfo);
|
||||
|
||||
///cleanup/shutdown Libspe2
|
||||
virtual ~b3Win32ThreadSupport();
|
||||
|
||||
void startThreads(const Win32ThreadConstructionInfo& threadInfo);
|
||||
|
||||
///send messages to SPUs
|
||||
virtual void runTask(int uiCommand, void* uiArgument0, int uiArgument1);
|
||||
|
||||
///check for messages from SPUs
|
||||
virtual void waitForResponse(int* puiArgument0, int* puiArgument1);
|
||||
|
||||
virtual bool isTaskCompleted(int* puiArgument0, int* puiArgument1, int timeOutInMilliseconds);
|
||||
|
||||
///start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
|
||||
virtual void startThreads();
|
||||
|
||||
///tell the task scheduler we are done with the SPU tasks
|
||||
virtual void stopThreads();
|
||||
|
||||
virtual void setNumTasks(int numTasks)
|
||||
{
|
||||
m_maxNumTasks = numTasks;
|
||||
}
|
||||
|
||||
virtual int getNumTasks() const
|
||||
{
|
||||
return m_maxNumTasks;
|
||||
}
|
||||
|
||||
virtual void* getThreadLocalMemory(int taskId)
|
||||
{
|
||||
return m_activeThreadStatus[taskId].m_lsMemory;
|
||||
}
|
||||
virtual b3Barrier* createBarrier();
|
||||
|
||||
virtual b3CriticalSection* createCriticalSection();
|
||||
|
||||
virtual void deleteBarrier(b3Barrier* barrier);
|
||||
|
||||
virtual void deleteCriticalSection(b3CriticalSection* criticalSection);
|
||||
};
|
||||
|
||||
#endif //BT_WIN32_THREAD_SUPPORT_H
|
||||
166
Engine/lib/bullet/examples/MultiThreading/main.cpp
Normal file
166
Engine/lib/bullet/examples/MultiThreading/main.cpp
Normal file
|
|
@ -0,0 +1,166 @@
|
|||
/*
|
||||
Bullet Continuous Collision Detection and Physics Library
|
||||
Copyright (c) 2010 Erwin Coumans http://bulletphysics.org
|
||||
|
||||
This software is provided 'as-is', without any express or implied warranty.
|
||||
In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
Permission is granted to anyone to use this software for any purpose,
|
||||
including commercial applications, and to alter it and redistribute it freely,
|
||||
subject to the following restrictions:
|
||||
|
||||
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
|
||||
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
/// ThreadingDemo shows how to use the cross platform thread support interface.
|
||||
/// You can start threads and perform a blocking wait for completion
|
||||
/// Under Windows it uses Win32 Threads. On Mac and Linux it uses pthreads. On PlayStation 3 Cell SPU it uses SPURS.
|
||||
|
||||
/// June 2010
|
||||
/// New: critical section/barriers and non-blocking polling for completion
|
||||
|
||||
void SampleThreadFunc(void* userPtr, void* lsMemory);
|
||||
void* SamplelsMemoryFunc();
|
||||
void SamplelsMemoryReleaseFunc(void* ptr);
|
||||
#include <stdio.h>
|
||||
//#include "BulletMultiThreaded/PlatformDefinitions.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
#include "b3PosixThreadSupport.h"
|
||||
|
||||
b3ThreadSupportInterface* createThreadSupport(int numThreads)
|
||||
{
|
||||
b3PosixThreadSupport::ThreadConstructionInfo constructionInfo("testThreads",
|
||||
SampleThreadFunc,
|
||||
SamplelsMemoryFunc,
|
||||
SamplelsMemoryReleaseFunc,
|
||||
numThreads);
|
||||
b3ThreadSupportInterface* threadSupport = new b3PosixThreadSupport(constructionInfo);
|
||||
|
||||
return threadSupport;
|
||||
}
|
||||
|
||||
#elif defined(_WIN32)
|
||||
#include "b3Win32ThreadSupport.h"
|
||||
|
||||
b3ThreadSupportInterface* createThreadSupport(int numThreads)
|
||||
{
|
||||
b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads", SampleThreadFunc, SamplelsMemoryFunc, SamplelsMemoryReleaseFunc, numThreads);
|
||||
b3Win32ThreadSupport* threadSupport = new b3Win32ThreadSupport(threadConstructionInfo);
|
||||
return threadSupport;
|
||||
}
|
||||
#endif
|
||||
|
||||
struct SampleArgs
|
||||
{
|
||||
SampleArgs()
|
||||
: m_fakeWork(1)
|
||||
{
|
||||
}
|
||||
b3CriticalSection* m_cs;
|
||||
float m_fakeWork;
|
||||
};
|
||||
|
||||
struct SampleThreadLocalStorage
|
||||
{
|
||||
int threadId;
|
||||
};
|
||||
|
||||
void SampleThreadFunc(void* userPtr, void* lsMemory)
|
||||
{
|
||||
printf("thread started\n");
|
||||
|
||||
SampleThreadLocalStorage* localStorage = (SampleThreadLocalStorage*)lsMemory;
|
||||
|
||||
SampleArgs* args = (SampleArgs*)userPtr;
|
||||
int workLeft = true;
|
||||
while (workLeft)
|
||||
{
|
||||
args->m_cs->lock();
|
||||
int count = args->m_cs->getSharedParam(0);
|
||||
args->m_cs->setSharedParam(0, count - 1);
|
||||
args->m_cs->unlock();
|
||||
if (count > 0)
|
||||
{
|
||||
printf("thread %d processed number %d\n", localStorage->threadId, count);
|
||||
}
|
||||
//do some fake work
|
||||
for (int i = 0; i < 1000000; i++)
|
||||
args->m_fakeWork = b3Scalar(1.21) * args->m_fakeWork;
|
||||
workLeft = count > 0;
|
||||
}
|
||||
printf("finished\n");
|
||||
//do nothing
|
||||
}
|
||||
|
||||
void* SamplelsMemoryFunc()
|
||||
{
|
||||
//don't create local store memory, just return 0
|
||||
return new SampleThreadLocalStorage;
|
||||
}
|
||||
|
||||
void SamplelsMemoryReleaseFunc(void* ptr)
|
||||
{
|
||||
SampleThreadLocalStorage* p = (SampleThreadLocalStorage*)ptr;
|
||||
delete p;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int numThreads = 8;
|
||||
|
||||
b3ThreadSupportInterface* threadSupport = createThreadSupport(numThreads);
|
||||
|
||||
for (int i = 0; i < threadSupport->getNumTasks(); i++)
|
||||
{
|
||||
SampleThreadLocalStorage* storage = (SampleThreadLocalStorage*)threadSupport->getThreadLocalMemory(i);
|
||||
b3Assert(storage);
|
||||
storage->threadId = i;
|
||||
}
|
||||
|
||||
SampleArgs args;
|
||||
args.m_cs = threadSupport->createCriticalSection();
|
||||
args.m_cs->setSharedParam(0, 100);
|
||||
|
||||
int arg0, arg1;
|
||||
int i;
|
||||
for (i = 0; i < numThreads; i++)
|
||||
{
|
||||
threadSupport->runTask(B3_THREAD_SCHEDULE_TASK, (void*)&args, i);
|
||||
}
|
||||
|
||||
bool blockingWait = false;
|
||||
if (blockingWait)
|
||||
{
|
||||
for (i = 0; i < numThreads; i++)
|
||||
{
|
||||
threadSupport->waitForResponse(&arg0, &arg1);
|
||||
printf("finished waiting for response: %d %d\n", arg0, arg1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
int numActiveThreads = numThreads;
|
||||
while (numActiveThreads)
|
||||
{
|
||||
if (threadSupport->isTaskCompleted(&arg0, &arg1, 0))
|
||||
{
|
||||
numActiveThreads--;
|
||||
printf("numActiveThreads = %d\n", numActiveThreads);
|
||||
}
|
||||
else
|
||||
{
|
||||
// printf("polling..");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
printf("stopping threads\n");
|
||||
|
||||
delete threadSupport;
|
||||
printf("Press ENTER to quit\n");
|
||||
//getchar();
|
||||
return 0;
|
||||
}
|
||||
51
Engine/lib/bullet/examples/MultiThreading/premake4.lua
Normal file
51
Engine/lib/bullet/examples/MultiThreading/premake4.lua
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
|
||||
project "App_ThreadingTest"
|
||||
|
||||
kind "ConsoleApp"
|
||||
|
||||
-- defines { }
|
||||
|
||||
|
||||
includedirs
|
||||
{
|
||||
".","../../src"
|
||||
}
|
||||
|
||||
|
||||
links { "Bullet3Common" }
|
||||
|
||||
|
||||
files {
|
||||
"b3ThreadSupportInterface.cpp",
|
||||
"main.cpp",
|
||||
"b3ThreadSupportInterface.h"
|
||||
}
|
||||
if os.is("Windows") then
|
||||
|
||||
files {
|
||||
"b3Win32ThreadSupport.cpp",
|
||||
"b3Win32ThreadSupport.h"
|
||||
}
|
||||
--links {"winmm"}
|
||||
--defines {"__WINDOWS_MM__", "WIN32"}
|
||||
end
|
||||
|
||||
if os.is("Linux") then
|
||||
files {
|
||||
"b3PosixThreadSupport.cpp",
|
||||
"b3PosixThreadSupport.h"
|
||||
}
|
||||
|
||||
links {"pthread"}
|
||||
end
|
||||
|
||||
if os.is("MacOSX") then
|
||||
files {
|
||||
"b3PosixThreadSupport.cpp",
|
||||
"b3PosixThreadSupport.h"
|
||||
}
|
||||
|
||||
links {"pthread"}
|
||||
--links{"CoreAudio.framework", "coreMIDI.framework", "Cocoa.framework"}
|
||||
--defines {"__MACOSX_CORE__"}
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue