Code-style consistency improvement:
Apply clang-format-all.sh using the _clang-format file through all the cpp/.h files. make sure not to apply it to certain serialization structures, since some parser expects the * as part of the name, instead of type. This commit contains no other changes aside from adding and applying clang-format-all.sh
This commit is contained in:
@@ -15,13 +15,13 @@
|
||||
#include "../CommonInterfaces/CommonParameterInterface.h"
|
||||
|
||||
#include "LinearMath/btAlignedObjectArray.h"
|
||||
#define stdvector btAlignedObjectArray
|
||||
#define stdvector btAlignedObjectArray
|
||||
|
||||
#define MAGIC_RESET_NUMBER 64738
|
||||
|
||||
void SampleThreadFunc(void* userPtr,void* lsMemory);
|
||||
void* SamplelsMemoryFunc();
|
||||
void SamplelsMemoryReleaseFunc(void* ptr);
|
||||
void SampleThreadFunc(void* userPtr, void* lsMemory);
|
||||
void* SamplelsMemoryFunc();
|
||||
void SamplelsMemoryReleaseFunc(void* ptr);
|
||||
|
||||
#include <stdio.h>
|
||||
//#include "BulletMultiThreaded/PlatformDefinitions.h"
|
||||
@@ -32,298 +32,265 @@ void SamplelsMemoryReleaseFunc(void* ptr);
|
||||
b3ThreadSupportInterface* createThreadSupport(int numThreads)
|
||||
{
|
||||
b3PosixThreadSupport::ThreadConstructionInfo constructionInfo("testThreads",
|
||||
SampleThreadFunc,
|
||||
SamplelsMemoryFunc,
|
||||
SamplelsMemoryReleaseFunc,
|
||||
numThreads);
|
||||
b3ThreadSupportInterface* threadSupport = new b3PosixThreadSupport(constructionInfo);
|
||||
SampleThreadFunc,
|
||||
SamplelsMemoryFunc,
|
||||
SamplelsMemoryReleaseFunc,
|
||||
numThreads);
|
||||
b3ThreadSupportInterface* threadSupport = new b3PosixThreadSupport(constructionInfo);
|
||||
|
||||
return threadSupport;
|
||||
|
||||
}
|
||||
|
||||
|
||||
#elif defined( _WIN32)
|
||||
#elif defined(_WIN32)
|
||||
#include "b3Win32ThreadSupport.h"
|
||||
|
||||
b3ThreadSupportInterface* createThreadSupport(int numThreads)
|
||||
{
|
||||
b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads",SampleThreadFunc,SamplelsMemoryFunc,SamplelsMemoryReleaseFunc,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;
|
||||
virtual void executeJob(int threadIndex) = 0;
|
||||
};
|
||||
|
||||
struct SampleJob1 : public SampleJobInterface
|
||||
struct SampleJob1 : public SampleJobInterface
|
||||
{
|
||||
float m_fakeWork;
|
||||
int m_jobId;
|
||||
|
||||
SampleJob1(int jobId)
|
||||
:m_fakeWork(0),
|
||||
m_jobId(jobId)
|
||||
{
|
||||
}
|
||||
virtual ~SampleJob1() {}
|
||||
float m_fakeWork;
|
||||
int m_jobId;
|
||||
|
||||
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);
|
||||
}
|
||||
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
|
||||
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();
|
||||
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;
|
||||
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;
|
||||
SampleArgs args;
|
||||
struct SampleThreadLocalStorage
|
||||
{
|
||||
int threadId;
|
||||
};
|
||||
|
||||
|
||||
void SampleThreadFunc(void* userPtr,void* lsMemory)
|
||||
void SampleThreadFunc(void* userPtr, void* lsMemory)
|
||||
{
|
||||
printf("SampleThreadFunc thread started\n");
|
||||
|
||||
SampleThreadLocalStorage* localStorage = (SampleThreadLocalStorage*) lsMemory;
|
||||
SampleThreadLocalStorage* localStorage = (SampleThreadLocalStorage*)lsMemory;
|
||||
|
||||
SampleArgs* args = (SampleArgs*)userPtr;
|
||||
|
||||
SampleArgs* args = (SampleArgs*) userPtr;
|
||||
|
||||
bool requestExit = false;
|
||||
while (!requestExit)
|
||||
{
|
||||
SampleJobInterface* job = args->consumeJob();
|
||||
if (job)
|
||||
{
|
||||
job->executeJob(localStorage->threadId);
|
||||
}
|
||||
|
||||
{
|
||||
job->executeJob(localStorage->threadId);
|
||||
}
|
||||
|
||||
b3Clock::usleep(250);
|
||||
|
||||
args->m_cs->lock();
|
||||
int exitMagicNumber = args->m_cs->getSharedParam(1);
|
||||
requestExit = (exitMagicNumber==MAGIC_RESET_NUMBER);
|
||||
requestExit = (exitMagicNumber == MAGIC_RESET_NUMBER);
|
||||
args->m_cs->unlock();
|
||||
|
||||
}
|
||||
|
||||
printf("finished\n");
|
||||
//do nothing
|
||||
}
|
||||
|
||||
|
||||
void* SamplelsMemoryFunc()
|
||||
void* SamplelsMemoryFunc()
|
||||
{
|
||||
//don't create local store memory, just return 0
|
||||
return new SampleThreadLocalStorage;
|
||||
}
|
||||
|
||||
void SamplelsMemoryReleaseFunc(void* ptr)
|
||||
void SamplelsMemoryReleaseFunc(void* ptr)
|
||||
{
|
||||
SampleThreadLocalStorage* p = (SampleThreadLocalStorage*) ptr;
|
||||
SampleThreadLocalStorage* p = (SampleThreadLocalStorage*)ptr;
|
||||
delete p;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
class MultiThreadingExample : public CommonExampleInterface
|
||||
{
|
||||
CommonGraphicsApp* m_app;
|
||||
b3ThreadSupportInterface* m_threadSupport;
|
||||
btAlignedObjectArray<SampleJob1*> m_jobs;
|
||||
int m_numThreads;
|
||||
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)
|
||||
{
|
||||
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 ~MultiThreadingExample()
|
||||
{
|
||||
}
|
||||
|
||||
virtual void renderScene()
|
||||
{
|
||||
}
|
||||
virtual void initPhysics()
|
||||
{
|
||||
b3Printf("initPhysics");
|
||||
|
||||
}
|
||||
|
||||
|
||||
virtual void renderScene()
|
||||
{
|
||||
}
|
||||
virtual void initPhysics()
|
||||
{
|
||||
b3Printf("initPhysics");
|
||||
|
||||
|
||||
m_threadSupport = createThreadSupport(m_numThreads);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
for (int i=0;i<m_threadSupport->getNumTasks();i++)
|
||||
{
|
||||
SampleThreadLocalStorage* storage = (SampleThreadLocalStorage*)m_threadSupport->getThreadLocalMemory(i);
|
||||
b3Assert(storage);
|
||||
storage->threadId = i;
|
||||
}
|
||||
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();
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
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..");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
int i;
|
||||
for (i=0;i<m_numThreads;i++)
|
||||
{
|
||||
m_threadSupport->runTask(B3_THREAD_SCHEDULE_TASK, (void*) &args, i);
|
||||
}
|
||||
b3Printf("Threads started");
|
||||
delete m_threadSupport;
|
||||
|
||||
}
|
||||
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);
|
||||
b3Printf("Threads stopped");
|
||||
for (int i = 0; i < m_jobs.size(); i++)
|
||||
{
|
||||
delete m_jobs[i];
|
||||
}
|
||||
m_jobs.clear();
|
||||
}
|
||||
|
||||
} 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 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 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())
|
||||
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]);
|
||||
m_app->m_renderer->getActiveCamera()->setCameraTargetPosition(targetPos[0], targetPos[1], targetPos[2]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
class CommonExampleInterface* MultiThreadingExampleCreateFunc(struct CommonExampleOptions& options)
|
||||
class CommonExampleInterface* MultiThreadingExampleCreateFunc(struct CommonExampleOptions& options)
|
||||
{
|
||||
return new MultiThreadingExample(options.m_guiHelper, options.m_option);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
|
||||
enum EnumMultiThreadingExampleTypes
|
||||
{
|
||||
SINGLE_SIM_THREAD=0,
|
||||
SINGLE_SIM_THREAD = 0,
|
||||
};
|
||||
|
||||
class CommonExampleInterface* MultiThreadingExampleCreateFunc(struct CommonExampleOptions& options);
|
||||
class CommonExampleInterface* MultiThreadingExampleCreateFunc(struct CommonExampleOptions& options);
|
||||
|
||||
#endif //MULTI_THREADING_EXAMPLE_H
|
||||
#endif //MULTI_THREADING_EXAMPLE_H
|
||||
|
||||
@@ -19,16 +19,15 @@ subject to the following restrictions:
|
||||
#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); \
|
||||
}
|
||||
#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);
|
||||
@@ -40,30 +39,29 @@ b3PosixThreadSupport::~b3PosixThreadSupport()
|
||||
stopThreads();
|
||||
}
|
||||
|
||||
#if (defined (__APPLE__))
|
||||
#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);
|
||||
/// 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
|
||||
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
|
||||
/// Named semaphore end
|
||||
#else
|
||||
sem_t* tempSem = new sem_t;
|
||||
checkPThreadFunction(sem_init(tempSem, 0, 0));
|
||||
@@ -81,38 +79,36 @@ static void destroySem(sem_t* semaphore)
|
||||
#endif
|
||||
}
|
||||
|
||||
static void *threadFunction(void *argument)
|
||||
static void* threadFunction(void* argument)
|
||||
{
|
||||
|
||||
b3PosixThreadSupport::b3ThreadStatus* status = (b3PosixThreadSupport::b3ThreadStatus*)argument;
|
||||
|
||||
|
||||
while (1)
|
||||
{
|
||||
checkPThreadFunction(sem_wait(status->startSemaphore));
|
||||
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_userThreadFunc(userPtr, status->m_lsMemory);
|
||||
status->m_status = 2;
|
||||
checkPThreadFunction(sem_post(status->m_mainSemaphore));
|
||||
status->threadUsed++;
|
||||
} else {
|
||||
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);
|
||||
printf("Thread with taskId %i exiting\n", status->m_taskId);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
printf("Thread TERMINATED\n");
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
///send messages to SPUs
|
||||
@@ -122,13 +118,11 @@ void b3PosixThreadSupport::runTask(int uiCommand, void* uiArgument0, int taskId)
|
||||
|
||||
///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:
|
||||
case B3_THREAD_SCHEDULE_TASK:
|
||||
{
|
||||
b3ThreadStatus& spuStatus = m_activeThreadStatus[taskId];
|
||||
b3ThreadStatus& spuStatus = m_activeThreadStatus[taskId];
|
||||
b3Assert(taskId >= 0);
|
||||
b3Assert(taskId < m_activeThreadStatus.size());
|
||||
|
||||
@@ -140,76 +134,74 @@ void b3PosixThreadSupport::runTask(int uiCommand, void* uiArgument0, int taskId)
|
||||
checkPThreadFunction(sem_post(spuStatus.startSemaphore));
|
||||
break;
|
||||
}
|
||||
default:
|
||||
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)
|
||||
bool b3PosixThreadSupport::isTaskCompleted(int* puiArgument0, int* puiArgument1, int timeOutInMilliseconds)
|
||||
{
|
||||
|
||||
b3Assert(m_activeThreadStatus.size());
|
||||
|
||||
// wait for any of the threads to finish
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
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];
|
||||
b3ThreadStatus& spuStatus = m_activeThreadStatus[last];
|
||||
|
||||
b3Assert(spuStatus.m_status > 1);
|
||||
spuStatus.m_status = 0;
|
||||
b3Assert(spuStatus.m_status > 1);
|
||||
spuStatus.m_status = 0;
|
||||
|
||||
// need to find an active spu
|
||||
b3Assert(last >= 0);
|
||||
// need to find an active spu
|
||||
b3Assert(last >= 0);
|
||||
|
||||
*puiArgument0 = spuStatus.m_taskId;
|
||||
*puiArgument1 = spuStatus.m_status;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
*puiArgument0 = spuStatus.m_taskId;
|
||||
*puiArgument1 = spuStatus.m_status;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
///check for messages from SPUs
|
||||
void b3PosixThreadSupport::waitForResponse( int *puiArgument0, int *puiArgument1)
|
||||
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
|
||||
// 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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
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];
|
||||
|
||||
@@ -223,27 +215,25 @@ void b3PosixThreadSupport::waitForResponse( int *puiArgument0, int *puiArgument
|
||||
*puiArgument1 = spuStatus.m_status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void b3PosixThreadSupport::startThreads(ThreadConstructionInfo& threadConstructionInfo)
|
||||
{
|
||||
printf("%s creating %i threads.\n", __FUNCTION__, threadConstructionInfo.m_numThreads);
|
||||
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++)
|
||||
for (int i = 0; i < threadConstructionInfo.m_numThreads; i++)
|
||||
{
|
||||
printf("starting thread %d\n",i);
|
||||
printf("starting thread %d\n", i);
|
||||
|
||||
b3ThreadStatus& spuStatus = m_activeThreadStatus[i];
|
||||
b3ThreadStatus& spuStatus = m_activeThreadStatus[i];
|
||||
|
||||
spuStatus.startSemaphore = createSem("threadLocal");
|
||||
|
||||
checkPThreadFunction(pthread_create(&spuStatus.thread, NULL, &threadFunction, (void*)&spuStatus));
|
||||
checkPThreadFunction(pthread_create(&spuStatus.thread, NULL, &threadFunction, (void*)&spuStatus));
|
||||
|
||||
spuStatus.m_userPtr=0;
|
||||
spuStatus.m_userPtr = 0;
|
||||
|
||||
spuStatus.m_taskId = i;
|
||||
spuStatus.m_commandId = 0;
|
||||
@@ -254,40 +244,35 @@ void b3PosixThreadSupport::startThreads(ThreadConstructionInfo& threadConstructi
|
||||
spuStatus.m_lsMemoryReleaseFunc = threadConstructionInfo.m_lsMemoryReleaseFunc;
|
||||
spuStatus.threadUsed = 0;
|
||||
|
||||
printf("started thread %d \n",i);
|
||||
|
||||
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)
|
||||
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);
|
||||
b3ThreadStatus& spuStatus = m_activeThreadStatus[t];
|
||||
|
||||
spuStatus.m_userPtr = 0;
|
||||
checkPThreadFunction(sem_post(spuStatus.startSemaphore));
|
||||
checkPThreadFunction(sem_wait(m_mainSemaphore));
|
||||
// printf("%s: Thread %i used: %ld\n", __FUNCTION__, int(t), spuStatus.threadUsed);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
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);
|
||||
destroySem(m_mainSemaphore);
|
||||
printf("main semaphore destroyed\n");
|
||||
m_activeThreadStatus.clear();
|
||||
}
|
||||
@@ -310,24 +295,26 @@ public:
|
||||
|
||||
virtual unsigned int getSharedParam(int i)
|
||||
{
|
||||
if (i<32)
|
||||
{
|
||||
return mCommonBuff[i];
|
||||
} else
|
||||
{
|
||||
b3Assert(0);
|
||||
}
|
||||
return 0;
|
||||
if (i < 32)
|
||||
{
|
||||
return mCommonBuff[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
b3Assert(0);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
virtual void setSharedParam(int i,unsigned int p)
|
||||
virtual void setSharedParam(int i, unsigned int p)
|
||||
{
|
||||
if (i<32)
|
||||
{
|
||||
mCommonBuff[i] = p;
|
||||
} else
|
||||
{
|
||||
b3Assert(0);
|
||||
}
|
||||
if (i < 32)
|
||||
{
|
||||
mCommonBuff[i] = p;
|
||||
}
|
||||
else
|
||||
{
|
||||
b3Assert(0);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void lock()
|
||||
@@ -340,24 +327,25 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#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() {
|
||||
: 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)
|
||||
if (rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD)
|
||||
{
|
||||
printf("Could not wait on barrier\n");
|
||||
exit(-1);
|
||||
@@ -367,9 +355,9 @@ public:
|
||||
{
|
||||
int result = pthread_barrier_init(&m_barr, NULL, numThreads);
|
||||
m_numThreads = numThreads;
|
||||
b3Assert(result==0);
|
||||
b3Assert(result == 0);
|
||||
}
|
||||
virtual int getMaxCount()
|
||||
virtual int getMaxCount()
|
||||
{
|
||||
return m_numThreads;
|
||||
}
|
||||
@@ -382,16 +370,16 @@ class b3PosixBarrier : public b3Barrier
|
||||
pthread_cond_t m_cond;
|
||||
|
||||
int m_numThreads;
|
||||
int m_called;
|
||||
int m_called;
|
||||
|
||||
public:
|
||||
b3PosixBarrier()
|
||||
:m_numThreads(0)
|
||||
: m_numThreads(0)
|
||||
{
|
||||
}
|
||||
virtual ~b3PosixBarrier()
|
||||
{
|
||||
if (m_numThreads>0)
|
||||
if (m_numThreads > 0)
|
||||
{
|
||||
pthread_mutex_destroy(&m_mutex);
|
||||
pthread_cond_destroy(&m_cond);
|
||||
@@ -402,36 +390,36 @@ public:
|
||||
{
|
||||
pthread_mutex_lock(&m_mutex);
|
||||
m_called++;
|
||||
if (m_called == m_numThreads) {
|
||||
if (m_called == m_numThreads)
|
||||
{
|
||||
m_called = 0;
|
||||
pthread_cond_broadcast(&m_cond);
|
||||
} else {
|
||||
pthread_cond_wait(&m_cond,&m_mutex);
|
||||
}
|
||||
else
|
||||
{
|
||||
pthread_cond_wait(&m_cond, &m_mutex);
|
||||
}
|
||||
pthread_mutex_unlock(&m_mutex);
|
||||
|
||||
}
|
||||
virtual void setMaxCount(int numThreads)
|
||||
{
|
||||
if (m_numThreads>0)
|
||||
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);
|
||||
pthread_mutex_init(&m_mutex, NULL);
|
||||
pthread_cond_init(&m_cond, NULL);
|
||||
m_numThreads = numThreads;
|
||||
}
|
||||
virtual int getMaxCount()
|
||||
virtual int getMaxCount()
|
||||
{
|
||||
return m_numThreads;
|
||||
}
|
||||
};
|
||||
|
||||
#endif//_POSIX_BARRIERS
|
||||
|
||||
|
||||
#endif //_POSIX_BARRIERS
|
||||
|
||||
b3Barrier* b3PosixThreadSupport::createBarrier()
|
||||
{
|
||||
@@ -445,7 +433,7 @@ b3CriticalSection* b3PosixThreadSupport::createCriticalSection()
|
||||
return new b3PosixCriticalSection();
|
||||
}
|
||||
|
||||
void b3PosixThreadSupport::deleteBarrier(b3Barrier* barrier)
|
||||
void b3PosixThreadSupport::deleteBarrier(b3Barrier* barrier)
|
||||
{
|
||||
delete barrier;
|
||||
}
|
||||
@@ -454,4 +442,4 @@ void b3PosixThreadSupport::deleteCriticalSection(b3CriticalSection* cs)
|
||||
{
|
||||
delete cs;
|
||||
}
|
||||
#endif //_WIN32
|
||||
#endif //_WIN32
|
||||
|
||||
@@ -16,24 +16,19 @@ subject to the following restrictions:
|
||||
#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
|
||||
#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 (*b3PosixThreadFunc)(void* userPtr, void* lsMemory);
|
||||
typedef void* (*b3PosixlsMemorySetupFunc)();
|
||||
typedef void (*b3PosixlsMemoryReleaseFunc)(void* ptr);
|
||||
|
||||
@@ -41,90 +36,84 @@ typedef void (*b3PosixlsMemoryReleaseFunc)(void* ptr);
|
||||
class b3PosixThreadSupport : public b3ThreadSupportInterface
|
||||
{
|
||||
public:
|
||||
typedef enum sStatus {
|
||||
STATUS_BUSY,
|
||||
STATUS_READY,
|
||||
STATUS_FINISHED
|
||||
} Status;
|
||||
typedef enum sStatus
|
||||
{
|
||||
STATUS_BUSY,
|
||||
STATUS_READY,
|
||||
STATUS_FINISHED
|
||||
} Status;
|
||||
|
||||
// placeholder, until libspe2 support is there
|
||||
struct b3ThreadStatus
|
||||
struct b3ThreadStatus
|
||||
{
|
||||
int m_taskId;
|
||||
int m_commandId;
|
||||
int m_status;
|
||||
int m_taskId;
|
||||
int m_commandId;
|
||||
int m_status;
|
||||
|
||||
b3PosixThreadFunc m_userThreadFunc;
|
||||
void* m_userPtr; //for taskDesc etc
|
||||
b3PosixThreadFunc m_userThreadFunc;
|
||||
void* m_userPtr; //for taskDesc etc
|
||||
b3PosixlsMemoryReleaseFunc m_lsMemoryReleaseFunc;
|
||||
|
||||
void* m_lsMemory; //initialized using PosixLocalStoreMemorySetupFunc
|
||||
void* m_lsMemory; //initialized using PosixLocalStoreMemorySetupFunc
|
||||
|
||||
pthread_t thread;
|
||||
//each tread will wait until this signal to start its work
|
||||
sem_t* startSemaphore;
|
||||
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;
|
||||
// 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;
|
||||
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
|
||||
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)
|
||||
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;
|
||||
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();
|
||||
///cleanup/shutdown Libspe2
|
||||
virtual ~b3PosixThreadSupport();
|
||||
|
||||
void startThreads(ThreadConstructionInfo& threadInfo);
|
||||
void startThreads(ThreadConstructionInfo& threadInfo);
|
||||
|
||||
virtual void runTask(int uiCommand, void* uiArgument0, int uiArgument1);
|
||||
|
||||
virtual void runTask(int uiCommand, void* uiArgument0, int uiArgument1);
|
||||
virtual void waitForResponse(int* puiArgument0, int* puiArgument1);
|
||||
|
||||
virtual void waitForResponse(int *puiArgument0, int *puiArgument1);
|
||||
|
||||
|
||||
///tell the task scheduler we are done with the SPU tasks
|
||||
virtual void stopThreads();
|
||||
///tell the task scheduler we are done with the SPU tasks
|
||||
virtual void stopThreads();
|
||||
|
||||
virtual void setNumTasks(int numTasks) {}
|
||||
|
||||
@@ -134,8 +123,7 @@ public:
|
||||
}
|
||||
|
||||
///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 bool isTaskCompleted(int* puiArgument0, int* puiArgument1, int timeOutInMilliseconds);
|
||||
|
||||
virtual b3Barrier* createBarrier();
|
||||
|
||||
@@ -145,15 +133,10 @@ public:
|
||||
|
||||
virtual void deleteCriticalSection(b3CriticalSection* criticalSection);
|
||||
|
||||
|
||||
virtual void* getThreadLocalMemory(int taskId)
|
||||
virtual void* getThreadLocalMemory(int taskId)
|
||||
{
|
||||
return m_activeThreadStatus[taskId].m_lsMemory;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif // B3_POSIX_THREAD_SUPPORT_H
|
||||
|
||||
|
||||
#endif // B3_POSIX_THREAD_SUPPORT_H
|
||||
|
||||
@@ -17,6 +17,4 @@ subject to the following restrictions:
|
||||
|
||||
b3ThreadSupportInterface::~b3ThreadSupportInterface()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -18,24 +18,26 @@ subject to the following restrictions:
|
||||
|
||||
enum
|
||||
{
|
||||
B3_THREAD_SCHEDULE_TASK=1,
|
||||
B3_THREAD_SCHEDULE_TASK = 1,
|
||||
};
|
||||
|
||||
#include "Bullet3Common/b3Scalar.h" //for B3_ATTRIBUTE_ALIGNED16
|
||||
#include "Bullet3Common/b3Scalar.h" //for B3_ATTRIBUTE_ALIGNED16
|
||||
//#include "PlatformDefinitions.h"
|
||||
//#include "PpuAddressSpace.h"
|
||||
|
||||
class b3Barrier {
|
||||
class b3Barrier
|
||||
{
|
||||
public:
|
||||
b3Barrier() {}
|
||||
virtual ~b3Barrier() {}
|
||||
|
||||
virtual void sync() = 0;
|
||||
virtual void setMaxCount(int n) = 0;
|
||||
virtual int getMaxCount() = 0;
|
||||
virtual int getMaxCount() = 0;
|
||||
};
|
||||
|
||||
class b3CriticalSection {
|
||||
class b3CriticalSection
|
||||
{
|
||||
public:
|
||||
b3CriticalSection() {}
|
||||
virtual ~b3CriticalSection() {}
|
||||
@@ -43,48 +45,40 @@ public:
|
||||
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 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 runTask(int uiCommand, void* uiArgument0, int uiArgument1) =0;
|
||||
|
||||
|
||||
virtual void waitForResponse(int *puiArgument0, int *puiArgument1) =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 bool isTaskCompleted(int* puiArgument0, int* puiArgument1, int timeOutInMilliseconds) = 0;
|
||||
|
||||
|
||||
virtual void stopThreads()=0;
|
||||
virtual void stopThreads() = 0;
|
||||
|
||||
///tell the task scheduler to use no more than numTasks tasks
|
||||
virtual void setNumTasks(int numTasks)=0;
|
||||
virtual void setNumTasks(int numTasks) = 0;
|
||||
|
||||
virtual int getNumTasks() const = 0;
|
||||
virtual int getNumTasks() const = 0;
|
||||
|
||||
virtual b3Barrier* createBarrier() = 0;
|
||||
virtual b3Barrier* createBarrier() = 0;
|
||||
|
||||
virtual b3CriticalSection* createCriticalSection() = 0;
|
||||
|
||||
virtual void deleteBarrier(b3Barrier* barrier)=0;
|
||||
virtual void deleteBarrier(b3Barrier* barrier) = 0;
|
||||
|
||||
virtual void deleteCriticalSection(b3CriticalSection* criticalSection)=0;
|
||||
|
||||
virtual void* getThreadLocalMemory(int taskId) { return 0; }
|
||||
virtual void deleteCriticalSection(b3CriticalSection* criticalSection) = 0;
|
||||
|
||||
virtual void* getThreadLocalMemory(int taskId) { return 0; }
|
||||
};
|
||||
|
||||
#endif //B3_THREAD_SUPPORT_INTERFACE_H
|
||||
|
||||
#endif //B3_THREAD_SUPPORT_INTERFACE_H
|
||||
|
||||
@@ -16,18 +16,14 @@ subject to the following restrictions:
|
||||
|
||||
#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)
|
||||
b3Win32ThreadSupport::b3Win32ThreadSupport(const Win32ThreadConstructionInfo& threadConstructionInfo)
|
||||
{
|
||||
m_maxNumTasks = threadConstructionInfo.m_numThreads;
|
||||
startThreads(threadConstructionInfo);
|
||||
@@ -39,73 +35,62 @@ b3Win32ThreadSupport::~b3Win32ThreadSupport()
|
||||
stopThreads();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
DWORD WINAPI Thread_no_1( LPVOID lpParam )
|
||||
DWORD WINAPI Thread_no_1(LPVOID lpParam)
|
||||
{
|
||||
|
||||
b3Win32ThreadSupport::b3ThreadStatus* status = (b3Win32ThreadSupport::b3ThreadStatus*)lpParam;
|
||||
|
||||
|
||||
while (1)
|
||||
{
|
||||
WaitForSingleObject(status->m_eventStartHandle,INFINITE);
|
||||
|
||||
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_userThreadFunc(userPtr, status->m_lsMemory);
|
||||
status->m_status = 2;
|
||||
SetEvent(status->m_eventCompletetHandle);
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
//exit Thread
|
||||
status->m_status = 3;
|
||||
printf("Thread with taskId %i with handle %p exiting\n",status->m_taskId, status->m_threadHandle);
|
||||
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
|
||||
|
||||
|
||||
///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:
|
||||
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;
|
||||
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());
|
||||
b3ThreadStatus& threadStatus = m_activeThreadStatus[taskId];
|
||||
b3Assert(taskId >= 0);
|
||||
b3Assert(int(taskId) < m_activeThreadStatus.size());
|
||||
|
||||
threadStatus.m_commandId = uiCommand;
|
||||
threadStatus.m_status = 1;
|
||||
@@ -114,31 +99,24 @@ void b3Win32ThreadSupport::runTask(int uiCommand, void* uiArgument0, int taskId)
|
||||
///fire event to start new task
|
||||
SetEvent(threadStatus.m_eventStartHandle);
|
||||
|
||||
#endif //CollisionTask_LocalStoreMemory
|
||||
|
||||
|
||||
#endif //CollisionTask_LocalStoreMemory
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
default:
|
||||
{
|
||||
///not implemented
|
||||
b3Assert(0);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
///check for messages from SPUs
|
||||
void b3Win32ThreadSupport::waitForResponse(int *puiArgument0, int *puiArgument1)
|
||||
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'
|
||||
|
||||
///A possible response can be 'yes, SPU handled it', or 'no, please do a PPU fallback'
|
||||
|
||||
b3Assert(m_activeThreadStatus.size());
|
||||
|
||||
@@ -157,39 +135,32 @@ void b3Win32ThreadSupport::waitForResponse(int *puiArgument0, int *puiArgument1)
|
||||
threadStatus.m_status = 0;
|
||||
|
||||
///need to find an active spu
|
||||
b3Assert(last>=0);
|
||||
b3Assert(last >= 0);
|
||||
|
||||
#else
|
||||
last=0;
|
||||
last = 0;
|
||||
b3ThreadStatus& threadStatus = m_activeThreadStatus[last];
|
||||
#endif //SINGLE_THREADED
|
||||
|
||||
|
||||
#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)
|
||||
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'
|
||||
|
||||
///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;
|
||||
|
||||
@@ -202,25 +173,22 @@ bool b3Win32ThreadSupport::isTaskCompleted(int *puiArgument0, int *puiArgument1,
|
||||
threadStatus.m_status = 0;
|
||||
|
||||
///need to find an active spu
|
||||
b3Assert(last>=0);
|
||||
b3Assert(last >= 0);
|
||||
|
||||
#else
|
||||
last=0;
|
||||
b3ThreadStatus& threadStatus = m_activeThreadStatus[last];
|
||||
#endif //SINGLE_THREADED
|
||||
|
||||
|
||||
#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;
|
||||
@@ -230,58 +198,56 @@ void b3Win32ThreadSupport::startThreads(const Win32ThreadConstructionInfo& threa
|
||||
|
||||
m_maxNumTasks = threadConstructionInfo.m_numThreads;
|
||||
|
||||
for (int i=0;i<threadConstructionInfo.m_numThreads;i++)
|
||||
for (int i = 0; i < threadConstructionInfo.m_numThreads; i++)
|
||||
{
|
||||
printf("starting thread %d\n",i);
|
||||
printf("starting thread %d\n", i);
|
||||
|
||||
b3ThreadStatus& threadStatus = m_activeThreadStatus[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;
|
||||
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;
|
||||
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_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);
|
||||
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)
|
||||
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:
|
||||
{
|
||||
|
||||
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 << 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)
|
||||
// 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;
|
||||
@@ -291,25 +257,22 @@ void b3Win32ThreadSupport::startThreads(const Win32ThreadConstructionInfo& threa
|
||||
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);
|
||||
|
||||
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++)
|
||||
for (i = 0; i < m_activeThreadStatus.size(); i++)
|
||||
{
|
||||
b3ThreadStatus& threadStatus = m_activeThreadStatus[i];
|
||||
if (threadStatus.m_status>0)
|
||||
if (threadStatus.m_status > 0)
|
||||
{
|
||||
WaitForSingleObject(threadStatus.m_eventCompletetHandle, INFINITE);
|
||||
}
|
||||
@@ -318,7 +281,7 @@ void b3Win32ThreadSupport::stopThreads()
|
||||
{
|
||||
threadStatus.m_lsMemoryReleaseFunc(threadStatus.m_lsMemory);
|
||||
}
|
||||
|
||||
|
||||
threadStatus.m_userPtr = 0;
|
||||
SetEvent(threadStatus.m_eventStartHandle);
|
||||
WaitForSingleObject(threadStatus.m_eventCompletetHandle, INFINITE);
|
||||
@@ -326,23 +289,19 @@ void b3Win32ThreadSupport::stopThreads()
|
||||
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;
|
||||
HANDLE mRunEvent, mNotifyEvent;
|
||||
int mCounter, mEnableCounter;
|
||||
int mMaxCount;
|
||||
|
||||
public:
|
||||
@@ -353,8 +312,8 @@ public:
|
||||
mEnableCounter = 0;
|
||||
InitializeCriticalSection(&mExternalCriticalSection);
|
||||
InitializeCriticalSection(&mLocalCriticalSection);
|
||||
mRunEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
|
||||
mNotifyEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
|
||||
mRunEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
||||
mNotifyEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
||||
}
|
||||
|
||||
virtual ~b3Win32Barrier()
|
||||
@@ -373,31 +332,35 @@ public:
|
||||
|
||||
//PFX_PRINTF("enter taskId %d count %d stage %d phase %d mEnableCounter %d\n",taskId,mCounter,debug&0xff,debug>>16,mEnableCounter);
|
||||
|
||||
if(mEnableCounter > 0) {
|
||||
if (mEnableCounter > 0)
|
||||
{
|
||||
ResetEvent(mNotifyEvent);
|
||||
LeaveCriticalSection(&mExternalCriticalSection);
|
||||
WaitForSingleObject(mNotifyEvent,INFINITE);
|
||||
WaitForSingleObject(mNotifyEvent, INFINITE);
|
||||
EnterCriticalSection(&mExternalCriticalSection);
|
||||
}
|
||||
|
||||
eventId = mCounter;
|
||||
mCounter++;
|
||||
|
||||
if(eventId == mMaxCount-1) {
|
||||
if (eventId == mMaxCount - 1)
|
||||
{
|
||||
SetEvent(mRunEvent);
|
||||
|
||||
mEnableCounter = mCounter-1;
|
||||
mEnableCounter = mCounter - 1;
|
||||
mCounter = 0;
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
ResetEvent(mRunEvent);
|
||||
LeaveCriticalSection(&mExternalCriticalSection);
|
||||
WaitForSingleObject(mRunEvent,INFINITE);
|
||||
WaitForSingleObject(mRunEvent, INFINITE);
|
||||
EnterCriticalSection(&mExternalCriticalSection);
|
||||
mEnableCounter--;
|
||||
}
|
||||
|
||||
if(mEnableCounter == 0) {
|
||||
if (mEnableCounter == 0)
|
||||
{
|
||||
SetEvent(mNotifyEvent);
|
||||
}
|
||||
|
||||
@@ -406,8 +369,8 @@ public:
|
||||
LeaveCriticalSection(&mExternalCriticalSection);
|
||||
}
|
||||
|
||||
virtual void setMaxCount(int n) {mMaxCount = n;}
|
||||
virtual int getMaxCount() {return mMaxCount;}
|
||||
virtual void setMaxCount(int n) { mMaxCount = n; }
|
||||
virtual int getMaxCount() { return mMaxCount; }
|
||||
};
|
||||
|
||||
class b3Win32CriticalSection : public b3CriticalSection
|
||||
@@ -428,14 +391,14 @@ public:
|
||||
|
||||
unsigned int getSharedParam(int i)
|
||||
{
|
||||
b3Assert(i>=0&&i<31);
|
||||
return mCommonBuff[i+1];
|
||||
b3Assert(i >= 0 && i < 31);
|
||||
return mCommonBuff[i + 1];
|
||||
}
|
||||
|
||||
void setSharedParam(int i,unsigned int p)
|
||||
void setSharedParam(int i, unsigned int p)
|
||||
{
|
||||
b3Assert(i>=0&&i<31);
|
||||
mCommonBuff[i+1] = p;
|
||||
b3Assert(i >= 0 && i < 31);
|
||||
mCommonBuff[i + 1] = p;
|
||||
}
|
||||
|
||||
void lock()
|
||||
@@ -451,19 +414,18 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
b3Barrier* b3Win32ThreadSupport::createBarrier()
|
||||
b3Barrier* b3Win32ThreadSupport::createBarrier()
|
||||
{
|
||||
unsigned char* mem = (unsigned char*)b3AlignedAlloc(sizeof(b3Win32Barrier),16);
|
||||
b3Win32Barrier* barrier = new(mem) b3Win32Barrier();
|
||||
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();
|
||||
unsigned char* mem = (unsigned char*)b3AlignedAlloc(sizeof(b3Win32CriticalSection), 16);
|
||||
b3Win32CriticalSection* cs = new (mem) b3Win32CriticalSection();
|
||||
return cs;
|
||||
}
|
||||
|
||||
@@ -479,9 +441,4 @@ void b3Win32ThreadSupport::deleteCriticalSection(b3CriticalSection* criticalSect
|
||||
b3AlignedFree(criticalSection);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif //_WIN32
|
||||
|
||||
#endif //_WIN32
|
||||
|
||||
@@ -15,7 +15,6 @@ subject to the following restrictions:
|
||||
|
||||
#include "Bullet3Common/b3Scalar.h"
|
||||
|
||||
|
||||
#ifndef BT_WIN32_THREAD_SUPPORT_H
|
||||
#define BT_WIN32_THREAD_SUPPORT_H
|
||||
|
||||
@@ -23,103 +22,94 @@ subject to the following restrictions:
|
||||
|
||||
#include "b3ThreadSupportInterface.h"
|
||||
|
||||
|
||||
typedef void (*b3Win32ThreadFunc)(void* userPtr,void* lsMemory);
|
||||
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
|
||||
class b3Win32ThreadSupport : public b3ThreadSupportInterface
|
||||
{
|
||||
public:
|
||||
///placeholder, until libspe2 support is there
|
||||
struct b3ThreadStatus
|
||||
struct b3ThreadStatus
|
||||
{
|
||||
int m_taskId;
|
||||
int m_commandId;
|
||||
int m_status;
|
||||
int m_taskId;
|
||||
int m_commandId;
|
||||
int m_status;
|
||||
|
||||
b3Win32ThreadFunc m_userThreadFunc;
|
||||
void* m_userPtr; //for taskDesc etc
|
||||
void* m_lsMemory; //initialized using Win32LocalStoreMemorySetupFunc
|
||||
b3Win32ThreadFunc m_userThreadFunc;
|
||||
void* m_userPtr; //for taskDesc etc
|
||||
void* m_lsMemory; //initialized using Win32LocalStoreMemorySetupFunc
|
||||
|
||||
b3Win32lsMemoryReleaseFunc m_lsMemoryReleaseFunc;
|
||||
b3Win32lsMemoryReleaseFunc m_lsMemoryReleaseFunc;
|
||||
|
||||
void* m_threadHandle; //this one is calling 'Win32ThreadFunc'
|
||||
void* m_threadHandle; //this one is calling 'Win32ThreadFunc'
|
||||
|
||||
void* m_eventStartHandle;
|
||||
char m_eventStartHandleName[32];
|
||||
|
||||
void* m_eventCompletetHandle;
|
||||
char m_eventCompletetHandleName[32];
|
||||
|
||||
void* m_eventStartHandle;
|
||||
char m_eventStartHandleName[32];
|
||||
|
||||
void* m_eventCompletetHandle;
|
||||
char m_eventCompletetHandleName[32];
|
||||
};
|
||||
private:
|
||||
|
||||
b3AlignedObjectArray<b3ThreadStatus> m_activeThreadStatus;
|
||||
b3AlignedObjectArray<void*> m_completeHandles;
|
||||
|
||||
private:
|
||||
b3AlignedObjectArray<b3ThreadStatus> m_activeThreadStatus;
|
||||
b3AlignedObjectArray<void*> m_completeHandles;
|
||||
|
||||
int m_maxNumTasks;
|
||||
|
||||
public:
|
||||
///Setup and initialize SPU/CELL/Libspe2
|
||||
|
||||
struct Win32ThreadConstructionInfo
|
||||
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)
|
||||
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;
|
||||
|
||||
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();
|
||||
///cleanup/shutdown Libspe2
|
||||
virtual ~b3Win32ThreadSupport();
|
||||
|
||||
void startThreads(const Win32ThreadConstructionInfo& threadInfo);
|
||||
void startThreads(const Win32ThreadConstructionInfo& threadInfo);
|
||||
|
||||
///send messages to SPUs
|
||||
virtual void runTask(int uiCommand, void* uiArgument0, int uiArgument1);
|
||||
|
||||
///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);
|
||||
|
||||
///check for messages from SPUs
|
||||
virtual void waitForResponse(int *puiArgument0, int *puiArgument1);
|
||||
virtual bool isTaskCompleted(int* puiArgument0, int* puiArgument1, int timeOutInMilliseconds);
|
||||
|
||||
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();
|
||||
|
||||
///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();
|
||||
|
||||
///tell the task scheduler we are done with the SPU tasks
|
||||
virtual void stopThreads();
|
||||
|
||||
virtual void setNumTasks(int numTasks)
|
||||
virtual void setNumTasks(int numTasks)
|
||||
{
|
||||
m_maxNumTasks = numTasks;
|
||||
}
|
||||
@@ -129,19 +119,17 @@ public:
|
||||
return m_maxNumTasks;
|
||||
}
|
||||
|
||||
virtual void* getThreadLocalMemory(int taskId)
|
||||
virtual void* getThreadLocalMemory(int taskId)
|
||||
{
|
||||
return m_activeThreadStatus[taskId].m_lsMemory;
|
||||
}
|
||||
virtual b3Barrier* createBarrier();
|
||||
virtual b3Barrier* createBarrier();
|
||||
|
||||
virtual b3CriticalSection* createCriticalSection();
|
||||
|
||||
virtual void deleteBarrier(b3Barrier* barrier);
|
||||
|
||||
virtual void deleteCriticalSection(b3CriticalSection* criticalSection);
|
||||
virtual void deleteCriticalSection(b3CriticalSection* criticalSection);
|
||||
};
|
||||
|
||||
#endif //BT_WIN32_THREAD_SUPPORT_H
|
||||
|
||||
|
||||
#endif //BT_WIN32_THREAD_SUPPORT_H
|
||||
|
||||
@@ -20,8 +20,8 @@ subject to the following restrictions:
|
||||
/// June 2010
|
||||
/// New: critical section/barriers and non-blocking polling for completion
|
||||
|
||||
void SampleThreadFunc(void* userPtr,void* lsMemory);
|
||||
void* SamplelsMemoryFunc();
|
||||
void SampleThreadFunc(void* userPtr, void* lsMemory);
|
||||
void* SamplelsMemoryFunc();
|
||||
|
||||
#include <stdio.h>
|
||||
//#include "BulletMultiThreaded/PlatformDefinitions.h"
|
||||
@@ -32,34 +32,29 @@ void* SamplelsMemoryFunc();
|
||||
b3ThreadSupportInterface* createThreadSupport(int numThreads)
|
||||
{
|
||||
b3PosixThreadSupport::ThreadConstructionInfo constructionInfo("testThreads",
|
||||
SampleThreadFunc,
|
||||
SamplelsMemoryFunc,
|
||||
numThreads);
|
||||
b3ThreadSupportInterface* threadSupport = new b3PosixThreadSupport(constructionInfo);
|
||||
SampleThreadFunc,
|
||||
SamplelsMemoryFunc,
|
||||
numThreads);
|
||||
b3ThreadSupportInterface* threadSupport = new b3PosixThreadSupport(constructionInfo);
|
||||
|
||||
return threadSupport;
|
||||
|
||||
}
|
||||
|
||||
|
||||
#elif defined( _WIN32)
|
||||
#elif defined(_WIN32)
|
||||
#include "b3Win32ThreadSupport.h"
|
||||
|
||||
b3ThreadSupportInterface* createThreadSupport(int numThreads)
|
||||
{
|
||||
b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads",SampleThreadFunc,SamplelsMemoryFunc,numThreads);
|
||||
b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads", SampleThreadFunc, SamplelsMemoryFunc, numThreads);
|
||||
b3Win32ThreadSupport* threadSupport = new b3Win32ThreadSupport(threadConstructionInfo);
|
||||
return threadSupport;
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
struct SampleArgs
|
||||
struct SampleArgs
|
||||
{
|
||||
SampleArgs()
|
||||
:m_fakeWork(1)
|
||||
: m_fakeWork(1)
|
||||
{
|
||||
}
|
||||
b3CriticalSection* m_cs;
|
||||
@@ -71,104 +66,90 @@ struct SampleThreadLocalStorage
|
||||
int threadId;
|
||||
};
|
||||
|
||||
|
||||
void SampleThreadFunc(void* userPtr,void* lsMemory)
|
||||
void SampleThreadFunc(void* userPtr, void* lsMemory)
|
||||
{
|
||||
printf("thread started\n");
|
||||
|
||||
SampleThreadLocalStorage* localStorage = (SampleThreadLocalStorage*) lsMemory;
|
||||
SampleThreadLocalStorage* localStorage = (SampleThreadLocalStorage*)lsMemory;
|
||||
|
||||
SampleArgs* args = (SampleArgs*) userPtr;
|
||||
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->setSharedParam(0, count - 1);
|
||||
args->m_cs->unlock();
|
||||
if (count>0)
|
||||
if (count > 0)
|
||||
{
|
||||
printf("thread %d processed number %d\n",localStorage->threadId, count);
|
||||
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;
|
||||
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()
|
||||
void* SamplelsMemoryFunc()
|
||||
{
|
||||
//don't create local store memory, just return 0
|
||||
return new SampleThreadLocalStorage;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc,char** argv)
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int numThreads = 8;
|
||||
|
||||
b3ThreadSupportInterface* threadSupport = createThreadSupport(numThreads);
|
||||
|
||||
|
||||
|
||||
for (int i=0;i<threadSupport->getNumTasks();i++)
|
||||
for (int i = 0; i < threadSupport->getNumTasks(); i++)
|
||||
{
|
||||
SampleThreadLocalStorage* storage = (SampleThreadLocalStorage*)threadSupport->getThreadLocalMemory(i);
|
||||
b3Assert(storage);
|
||||
storage->threadId = i;
|
||||
}
|
||||
|
||||
|
||||
SampleArgs args;
|
||||
SampleArgs args;
|
||||
args.m_cs = threadSupport->createCriticalSection();
|
||||
args.m_cs->setSharedParam(0,100);
|
||||
args.m_cs->setSharedParam(0, 100);
|
||||
|
||||
|
||||
int arg0,arg1;
|
||||
int arg0, arg1;
|
||||
int i;
|
||||
for (i=0;i<numThreads;i++)
|
||||
for (i = 0; i < numThreads; i++)
|
||||
{
|
||||
threadSupport->runTask(B3_THREAD_SCHEDULE_TASK, (void*) &args, i);
|
||||
threadSupport->runTask(B3_THREAD_SCHEDULE_TASK, (void*)&args, i);
|
||||
}
|
||||
|
||||
bool blockingWait =false;
|
||||
bool blockingWait = false;
|
||||
if (blockingWait)
|
||||
{
|
||||
for (i=0;i<numThreads;i++)
|
||||
for (i = 0; i < numThreads; i++)
|
||||
{
|
||||
threadSupport->waitForResponse(&arg0,&arg1);
|
||||
printf("finished waiting for response: %d %d\n", arg0,arg1);
|
||||
threadSupport->waitForResponse(&arg0, &arg1);
|
||||
printf("finished waiting for response: %d %d\n", arg0, arg1);
|
||||
}
|
||||
} else
|
||||
}
|
||||
else
|
||||
{
|
||||
int numActiveThreads = numThreads;
|
||||
while (numActiveThreads)
|
||||
{
|
||||
if (threadSupport->isTaskCompleted(&arg0,&arg1,0))
|
||||
if (threadSupport->isTaskCompleted(&arg0, &arg1, 0))
|
||||
{
|
||||
numActiveThreads--;
|
||||
printf("numActiveThreads = %d\n",numActiveThreads);
|
||||
|
||||
} else
|
||||
printf("numActiveThreads = %d\n", numActiveThreads);
|
||||
}
|
||||
else
|
||||
{
|
||||
// printf("polling..");
|
||||
// printf("polling..");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
printf("stopping threads\n");
|
||||
printf("stopping threads\n");
|
||||
|
||||
delete threadSupport;
|
||||
printf("Press ENTER to quit\n");
|
||||
|
||||
Reference in New Issue
Block a user