fix PosixThreadSupport for Mac OSX, thanks to David Guthrie,
http://code.google.com/p/bullet/issues/detail?id=111
This commit is contained in:
@@ -15,16 +15,16 @@ subject to the following restrictions:
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "PosixThreadSupport.h"
|
#include "PosixThreadSupport.h"
|
||||||
|
|
||||||
|
|
||||||
#ifdef USE_PTHREADS
|
#ifdef USE_PTHREADS
|
||||||
|
#include <errno.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "SpuCollisionTaskProcess.h"
|
#include "SpuCollisionTaskProcess.h"
|
||||||
#include "SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h"
|
#include "SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h"
|
||||||
|
|
||||||
#define checkPThreadFunction(returnValue) \
|
#define checkPThreadFunction(returnValue) \
|
||||||
if(0 != returnValue) { \
|
if(0 != returnValue) { \
|
||||||
printf("PThread problem at line %i in file %s: %i\n", __LINE__, __FILE__, 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
|
// The number of threads should be equal to the number of available cores
|
||||||
@@ -43,8 +43,45 @@ PosixThreadSupport::~PosixThreadSupport()
|
|||||||
stopSPU();
|
stopSPU();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define NAMED_SEMAPHORES
|
||||||
|
|
||||||
// this semaphore will signal, if and how many threads are finished with their work
|
// this semaphore will signal, if and how many threads are finished with their work
|
||||||
static sem_t mainSemaphore;
|
static sem_t* mainSemaphore;
|
||||||
|
|
||||||
|
static sem_t* createSem(const char* baseName)
|
||||||
|
{
|
||||||
|
static int semCount = 0;
|
||||||
|
#ifdef NAMED_SEMAPHORES
|
||||||
|
/// Named semaphore begin
|
||||||
|
char name[32];
|
||||||
|
snprintf(name, 32, "/%s-%d-%4.4d", baseName, getpid(), semCount++);
|
||||||
|
sem_t* tempSem = sem_open(name, O_CREAT, 0600, 0);
|
||||||
|
if (tempSem != SEM_FAILED)
|
||||||
|
{
|
||||||
|
//printf("Created \"%s\" Semaphore %x\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)
|
static void *threadFunction(void *argument)
|
||||||
{
|
{
|
||||||
@@ -54,7 +91,7 @@ static void *threadFunction(void *argument)
|
|||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
checkPThreadFunction(sem_wait(&status->startSemaphore));
|
checkPThreadFunction(sem_wait(status->startSemaphore));
|
||||||
|
|
||||||
void* userPtr = status->m_userPtr;
|
void* userPtr = status->m_userPtr;
|
||||||
|
|
||||||
@@ -63,13 +100,12 @@ static void *threadFunction(void *argument)
|
|||||||
btAssert(status->m_status);
|
btAssert(status->m_status);
|
||||||
status->m_userThreadFunc(userPtr,status->m_lsMemory);
|
status->m_userThreadFunc(userPtr,status->m_lsMemory);
|
||||||
status->m_status = 2;
|
status->m_status = 2;
|
||||||
checkPThreadFunction(sem_post(&mainSemaphore));
|
checkPThreadFunction(sem_post(mainSemaphore));
|
||||||
|
status->threadUsed++;
|
||||||
status->threadUsed++;
|
|
||||||
} else {
|
} else {
|
||||||
//exit Thread
|
//exit Thread
|
||||||
status->m_status = 3;
|
status->m_status = 3;
|
||||||
checkPThreadFunction(sem_post(&mainSemaphore));
|
checkPThreadFunction(sem_post(mainSemaphore));
|
||||||
printf("Thread with taskId %i exiting\n",status->m_taskId);
|
printf("Thread with taskId %i exiting\n",status->m_taskId);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -103,7 +139,7 @@ void PosixThreadSupport::sendRequest(uint32_t uiCommand, uint32_t uiArgument0, u
|
|||||||
spuStatus.m_userPtr = (void*)uiArgument0;
|
spuStatus.m_userPtr = (void*)uiArgument0;
|
||||||
|
|
||||||
// fire event to start new task
|
// fire event to start new task
|
||||||
checkPThreadFunction(sem_post(&spuStatus.startSemaphore));
|
checkPThreadFunction(sem_post(spuStatus.startSemaphore));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@@ -129,7 +165,7 @@ void PosixThreadSupport::waitForResponse(unsigned int *puiArgument0, unsigned in
|
|||||||
btAssert(m_activeSpuStatus.size());
|
btAssert(m_activeSpuStatus.size());
|
||||||
|
|
||||||
// wait for any of the threads to finish
|
// wait for any of the threads to finish
|
||||||
checkPThreadFunction(sem_wait(&mainSemaphore));
|
checkPThreadFunction(sem_wait(mainSemaphore));
|
||||||
|
|
||||||
// get at least one thread which has finished
|
// get at least one thread which has finished
|
||||||
size_t last = -1;
|
size_t last = -1;
|
||||||
@@ -160,15 +196,16 @@ void PosixThreadSupport::startThreads(ThreadConstructionInfo& threadConstruction
|
|||||||
printf("%s creating %i threads.\n", __FUNCTION__, threadConstructionInfo.m_numThreads);
|
printf("%s creating %i threads.\n", __FUNCTION__, threadConstructionInfo.m_numThreads);
|
||||||
m_activeSpuStatus.resize(threadConstructionInfo.m_numThreads);
|
m_activeSpuStatus.resize(threadConstructionInfo.m_numThreads);
|
||||||
|
|
||||||
checkPThreadFunction(sem_init(&mainSemaphore, 0, 0));
|
mainSemaphore = createSem("main");
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
btSpuStatus& spuStatus = m_activeSpuStatus[i];
|
btSpuStatus& spuStatus = m_activeSpuStatus[i];
|
||||||
|
|
||||||
|
spuStatus.startSemaphore = createSem("threadLocal");
|
||||||
|
|
||||||
checkPThreadFunction(sem_init(&spuStatus.startSemaphore, 0, 0));
|
|
||||||
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;
|
||||||
@@ -196,13 +233,12 @@ void PosixThreadSupport::stopSPU()
|
|||||||
{
|
{
|
||||||
for(size_t t=0; t < m_activeSpuStatus.size(); ++t) {
|
for(size_t t=0; t < m_activeSpuStatus.size(); ++t) {
|
||||||
btSpuStatus& spuStatus = m_activeSpuStatus[t];
|
btSpuStatus& spuStatus = m_activeSpuStatus[t];
|
||||||
printf("%s: Thread %i used: %ld\n", __FUNCTION__, t, spuStatus.threadUsed);
|
printf("%s: Thread %i used: %ld\n", __FUNCTION__, t, spuStatus.threadUsed);
|
||||||
|
|
||||||
|
destroySem(spuStatus.startSemaphore);
|
||||||
checkPThreadFunction(sem_destroy(&spuStatus.startSemaphore));
|
|
||||||
checkPThreadFunction(pthread_cancel(spuStatus.thread));
|
checkPThreadFunction(pthread_cancel(spuStatus.thread));
|
||||||
}
|
}
|
||||||
checkPThreadFunction(sem_destroy(&mainSemaphore));
|
destroySem(mainSemaphore);
|
||||||
|
|
||||||
m_activeSpuStatus.clear();
|
m_activeSpuStatus.clear();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ subject to the following restrictions:
|
|||||||
typedef void (*PosixThreadFunc)(void* userPtr,void* lsMemory);
|
typedef void (*PosixThreadFunc)(void* userPtr,void* lsMemory);
|
||||||
typedef void* (*PosixlsMemorySetupFunc)();
|
typedef void* (*PosixlsMemorySetupFunc)();
|
||||||
|
|
||||||
///The PosixThreadSupport implements the btThreadSupportInterface using pthreads, to help porting SPU Tasks.
|
// PosixThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
|
||||||
class PosixThreadSupport : public btThreadSupportInterface
|
class PosixThreadSupport : public btThreadSupportInterface
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -54,7 +54,7 @@ public:
|
|||||||
void* m_lsMemory; //initialized using PosixLocalStoreMemorySetupFunc
|
void* m_lsMemory; //initialized using PosixLocalStoreMemorySetupFunc
|
||||||
|
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
sem_t startSemaphore;
|
sem_t* startSemaphore;
|
||||||
|
|
||||||
unsigned long threadUsed;
|
unsigned long threadUsed;
|
||||||
};
|
};
|
||||||
@@ -111,9 +111,8 @@ public:
|
|||||||
///tell the task scheduler we are done with the SPU tasks
|
///tell the task scheduler we are done with the SPU tasks
|
||||||
virtual void stopSPU();
|
virtual void stopSPU();
|
||||||
|
|
||||||
virtual void setNumTasks(int numTasks)
|
virtual void setNumTasks(int numTasks) {}
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user