add basic CPU multi threading files from Bullet 2.x (still need to make it work)

This commit is contained in:
erwincoumans
2013-07-16 18:16:54 -07:00
parent 20f9e41ff0
commit b448e56a51
10 changed files with 1762 additions and 0 deletions

View File

@@ -0,0 +1,409 @@
/*
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 "PosixThreadSupport.h"
#ifdef USE_PTHREADS
#include <errno.h>
#include <unistd.h>
#include "SpuCollisionTaskProcess.h"
#include "SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.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.
// PosixThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
// Setup and initialize SPU/CELL/Libspe2
PosixThreadSupport::PosixThreadSupport(ThreadConstructionInfo& threadConstructionInfo)
{
startThreads(threadConstructionInfo);
}
// cleanup/shutdown Libspe2
PosixThreadSupport::~PosixThreadSupport()
{
stopSPU();
}
#if (defined (__APPLE__))
#define NAMED_SEMAPHORES
#endif
// this semaphore will signal, if and how many threads are finished with their work
static sem_t* mainSemaphore=0;
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 != 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)
{
PosixThreadSupport::btSpuStatus* status = (PosixThreadSupport::btSpuStatus*)argument;
while (1)
{
checkPThreadFunction(sem_wait(status->startSemaphore));
void* userPtr = status->m_userPtr;
if (userPtr)
{
btAssert(status->m_status);
status->m_userThreadFunc(userPtr,status->m_lsMemory);
status->m_status = 2;
checkPThreadFunction(sem_post(mainSemaphore));
status->threadUsed++;
} else {
//exit Thread
status->m_status = 3;
checkPThreadFunction(sem_post(mainSemaphore));
printf("Thread with taskId %i exiting\n",status->m_taskId);
break;
}
}
printf("Thread TERMINATED\n");
return 0;
}
///send messages to SPUs
void PosixThreadSupport::sendRequest(uint32_t uiCommand, ppu_address_t uiArgument0, uint32_t taskId)
{
/// gMidphaseSPU.sendRequest(CMD_GATHER_AND_PROCESS_PAIRLIST, (uint32_t) &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 CMD_GATHER_AND_PROCESS_PAIRLIST:
{
btSpuStatus& spuStatus = m_activeSpuStatus[taskId];
btAssert(taskId >= 0);
btAssert(taskId < m_activeSpuStatus.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
btAssert(0);
}
};
}
///check for messages from SPUs
void PosixThreadSupport::waitForResponse(unsigned int *puiArgument0, unsigned 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'
btAssert(m_activeSpuStatus.size());
// wait for any of the threads to finish
checkPThreadFunction(sem_wait(mainSemaphore));
// get at least one thread which has finished
size_t last = -1;
for(size_t t=0; t < size_t(m_activeSpuStatus.size()); ++t) {
if(2 == m_activeSpuStatus[t].m_status) {
last = t;
break;
}
}
btSpuStatus& spuStatus = m_activeSpuStatus[last];
btAssert(spuStatus.m_status > 1);
spuStatus.m_status = 0;
// need to find an active spu
btAssert(last >= 0);
*puiArgument0 = spuStatus.m_taskId;
*puiArgument1 = spuStatus.m_status;
}
void PosixThreadSupport::startThreads(ThreadConstructionInfo& threadConstructionInfo)
{
printf("%s creating %i threads.\n", __FUNCTION__, threadConstructionInfo.m_numThreads);
m_activeSpuStatus.resize(threadConstructionInfo.m_numThreads);
mainSemaphore = createSem("main");
//checkPThreadFunction(sem_wait(mainSemaphore));
for (int i=0;i < threadConstructionInfo.m_numThreads;i++)
{
printf("starting thread %d\n",i);
btSpuStatus& spuStatus = m_activeSpuStatus[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_lsMemory = threadConstructionInfo.m_lsMemoryFunc();
spuStatus.m_userThreadFunc = threadConstructionInfo.m_userThreadFunc;
spuStatus.threadUsed = 0;
printf("started thread %d \n",i);
}
}
void PosixThreadSupport::startSPU()
{
}
///tell the task scheduler we are done with the SPU tasks
void PosixThreadSupport::stopSPU()
{
for(size_t t=0; t < size_t(m_activeSpuStatus.size()); ++t)
{
btSpuStatus& spuStatus = m_activeSpuStatus[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(mainSemaphore));
printf("destroy semaphore\n");
destroySem(spuStatus.startSemaphore);
printf("semaphore destroyed\n");
checkPThreadFunction(pthread_join(spuStatus.thread,0));
}
printf("destroy main semaphore\n");
destroySem(mainSemaphore);
printf("main semaphore destroyed\n");
m_activeSpuStatus.clear();
}
class PosixCriticalSection : public btCriticalSection
{
pthread_mutex_t m_mutex;
public:
PosixCriticalSection()
{
pthread_mutex_init(&m_mutex, NULL);
}
virtual ~PosixCriticalSection()
{
pthread_mutex_destroy(&m_mutex);
}
ATTRIBUTE_ALIGNED16(unsigned int mCommonBuff[32]);
virtual unsigned int getSharedParam(int i)
{
return mCommonBuff[i];
}
virtual void setSharedParam(int i,unsigned int p)
{
mCommonBuff[i] = p;
}
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 PosixBarrier : public btBarrier
{
pthread_barrier_t m_barr;
int m_numThreads;
public:
PosixBarrier()
:m_numThreads(0) { }
virtual ~PosixBarrier() {
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;
btAssert(result==0);
}
virtual int getMaxCount()
{
return m_numThreads;
}
};
#else
/* Not OK to use barriers on this platform - insert alternate code here */
class PosixBarrier : public btBarrier
{
pthread_mutex_t m_mutex;
pthread_cond_t m_cond;
int m_numThreads;
int m_called;
public:
PosixBarrier()
:m_numThreads(0)
{
}
virtual ~PosixBarrier()
{
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
btBarrier* PosixThreadSupport::createBarrier()
{
PosixBarrier* barrier = new PosixBarrier();
barrier->setMaxCount(getNumTasks());
return barrier;
}
btCriticalSection* PosixThreadSupport::createCriticalSection()
{
return new PosixCriticalSection();
}
void PosixThreadSupport::deleteBarrier(btBarrier* barrier)
{
delete barrier;
}
void PosixThreadSupport::deleteCriticalSection(btCriticalSection* cs)
{
delete cs;
}
#endif // USE_PTHREADS

View File

@@ -0,0 +1,147 @@
/*
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 BT_POSIX_THREAD_SUPPORT_H
#define BT_POSIX_THREAD_SUPPORT_H
#include "LinearMath/btScalar.h"
#include "PlatformDefinitions.h"
#ifdef USE_PTHREADS //platform specifc defines are defined in PlatformDefinitions.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 "LinearMath/btAlignedObjectArray.h"
#include "btThreadSupportInterface.h"
typedef void (*PosixThreadFunc)(void* userPtr,void* lsMemory);
typedef void* (*PosixlsMemorySetupFunc)();
// PosixThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
class PosixThreadSupport : public btThreadSupportInterface
{
public:
typedef enum sStatus {
STATUS_BUSY,
STATUS_READY,
STATUS_FINISHED
} Status;
// placeholder, until libspe2 support is there
struct btSpuStatus
{
uint32_t m_taskId;
uint32_t m_commandId;
uint32_t m_status;
PosixThreadFunc m_userThreadFunc;
void* m_userPtr; //for taskDesc etc
void* m_lsMemory; //initialized using PosixLocalStoreMemorySetupFunc
pthread_t thread;
sem_t* startSemaphore;
unsigned long threadUsed;
};
private:
btAlignedObjectArray<btSpuStatus> m_activeSpuStatus;
public:
///Setup and initialize SPU/CELL/Libspe2
struct ThreadConstructionInfo
{
ThreadConstructionInfo(const char* uniqueName,
PosixThreadFunc userThreadFunc,
PosixlsMemorySetupFunc lsMemoryFunc,
int numThreads=1,
int threadStackSize=65535
)
:m_uniqueName(uniqueName),
m_userThreadFunc(userThreadFunc),
m_lsMemoryFunc(lsMemoryFunc),
m_numThreads(numThreads),
m_threadStackSize(threadStackSize)
{
}
const char* m_uniqueName;
PosixThreadFunc m_userThreadFunc;
PosixlsMemorySetupFunc m_lsMemoryFunc;
int m_numThreads;
int m_threadStackSize;
};
PosixThreadSupport(ThreadConstructionInfo& threadConstructionInfo);
///cleanup/shutdown Libspe2
virtual ~PosixThreadSupport();
void startThreads(ThreadConstructionInfo& threadInfo);
///send messages to SPUs
virtual void sendRequest(uint32_t uiCommand, ppu_address_t uiArgument0, uint32_t uiArgument1);
///check for messages from SPUs
virtual void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1);
///start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
virtual void startSPU();
///tell the task scheduler we are done with the SPU tasks
virtual void stopSPU();
virtual void setNumTasks(int numTasks) {}
virtual int getNumTasks() const
{
return m_activeSpuStatus.size();
}
virtual btBarrier* createBarrier();
virtual btCriticalSection* createCriticalSection();
virtual void deleteBarrier(btBarrier* barrier);
virtual void deleteCriticalSection(btCriticalSection* criticalSection);
virtual void* getThreadLocalMemory(int taskId)
{
return m_activeSpuStatus[taskId].m_lsMemory;
}
};
#endif // USE_PTHREADS
#endif // BT_POSIX_THREAD_SUPPORT_H

View File

@@ -0,0 +1,179 @@
/*
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 "b3SequentialThreadSupport.h"
b3SequentialThreadSupport::b3SequentialThreadSupport(SequentialThreadConstructionInfo& threadConstructionInfo)
{
startThreads(threadConstructionInfo);
}
///cleanup/shutdown Libspe2
b3SequentialThreadSupport::~b3SequentialThreadSupport()
{
stopThreads();
}
#include <stdio.h>
///send messages to SPUs
void b3SequentialThreadSupport::sendRequest(int uiCommand, void* uiArgument0, int taskId)
{
switch (uiCommand)
{
case B3_THREAD_SCHEDULE_TASK:
{
btSpuStatus& spuStatus = m_activeSpuStatus[0];
spuStatus.m_userPtr=(void*)uiArgument0;
spuStatus.m_userThreadFunc(spuStatus.m_userPtr,spuStatus.m_lsMemory);
}
break;
default:
{
///not implemented
b3Assert(0 && "Not implemented");
}
};
}
///check for messages from SPUs
void b3SequentialThreadSupport::waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1)
{
b3Assert(m_activeSpuStatus.size());
btSpuStatus& spuStatus = m_activeSpuStatus[0];
*puiArgument0 = spuStatus.m_taskId;
*puiArgument1 = spuStatus.m_status;
}
void b3SequentialThreadSupport::startThreads(SequentialThreadConstructionInfo& threadConstructionInfo)
{
m_activeSpuStatus.resize(1);
printf("STS: Not starting any threads\n");
btSpuStatus& spuStatus = m_activeSpuStatus[0];
spuStatus.m_userPtr = 0;
spuStatus.m_taskId = 0;
spuStatus.m_commandId = 0;
spuStatus.m_status = 0;
spuStatus.m_lsMemory = threadConstructionInfo.m_lsMemoryFunc();
spuStatus.m_userThreadFunc = threadConstructionInfo.m_userThreadFunc;
printf("STS: Created local store at %p for task %s\n", spuStatus.m_lsMemory, threadConstructionInfo.m_uniqueName);
}
void b3SequentialThreadSupport::startThreads()
{
}
void b3SequentialThreadSupport::stopThreads()
{
m_activeSpuStatus.clear();
}
void b3SequentialThreadSupport::setNumTasks(int numTasks)
{
printf("b3SequentialThreadSupport::setNumTasks(%d) is not implemented and has no effect\n",numTasks);
}
class btDummyBarrier : public b3Barrier
{
private:
public:
btDummyBarrier()
{
}
virtual ~btDummyBarrier()
{
}
void sync()
{
}
virtual void setMaxCount(int n) {}
virtual int getMaxCount() {return 1;}
};
class btDummyCriticalSection : public b3CriticalSection
{
public:
btDummyCriticalSection()
{
}
virtual ~btDummyCriticalSection()
{
}
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()
{
mCommonBuff[0] = 1;
}
void unlock()
{
mCommonBuff[0] = 0;
}
};
b3Barrier* b3SequentialThreadSupport::createBarrier()
{
return new btDummyBarrier();
}
b3CriticalSection* b3SequentialThreadSupport::createCriticalSection()
{
return new btDummyCriticalSection();
}
void b3SequentialThreadSupport::deleteBarrier(b3Barrier* barrier)
{
delete barrier;
}
void b3SequentialThreadSupport::deleteCriticalSection(b3CriticalSection* criticalSection)
{
delete criticalSection;
}

View File

@@ -0,0 +1,100 @@
/*
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 B3_SEQUENTIAL_THREAD_SUPPORT_H
#define B3_SEQUENTIAL_THREAD_SUPPORT_H
#include "Bullet3Common/b3AlignedObjectArray.h"
#include "b3ThreadSupportInterface.h"
typedef void (*SequentialThreadFunc)(void* userPtr,void* lsMemory);
typedef void* (*SequentiallsMemorySetupFunc)();
///The b3SequentialThreadSupport is a portable non-parallel implementation of the btThreadSupportInterface
///This is useful for debugging and porting SPU Tasks to other platforms.
class b3SequentialThreadSupport : public b3ThreadSupportInterface
{
public:
struct btSpuStatus
{
int m_taskId;
int m_commandId;
int m_status;
SequentialThreadFunc m_userThreadFunc;
void* m_userPtr; //for taskDesc etc
void* m_lsMemory; //initialized using SequentiallsMemorySetupFunc
};
private:
b3AlignedObjectArray<btSpuStatus> m_activeSpuStatus;
b3AlignedObjectArray<void*> m_completeHandles;
public:
struct SequentialThreadConstructionInfo
{
SequentialThreadConstructionInfo (const char* uniqueName,
SequentialThreadFunc userThreadFunc,
SequentiallsMemorySetupFunc lsMemoryFunc
)
:m_uniqueName(uniqueName),
m_userThreadFunc(userThreadFunc),
m_lsMemoryFunc(lsMemoryFunc)
{
}
const char* m_uniqueName;
SequentialThreadFunc m_userThreadFunc;
SequentiallsMemorySetupFunc m_lsMemoryFunc;
};
b3SequentialThreadSupport(SequentialThreadConstructionInfo& threadConstructionInfo);
virtual ~b3SequentialThreadSupport();
void startThreads(SequentialThreadConstructionInfo& threadInfo);
///send messages to SPUs
virtual void sendRequest(int uiCommand, void* uiArgument0, int uiArgument1);
///check for messages from SPUs
virtual void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1);
///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);
virtual int getNumTasks() const
{
return 1;
}
virtual b3Barrier* createBarrier();
virtual b3CriticalSection* createCriticalSection();
virtual void deleteBarrier(b3Barrier* barrier);
virtual void deleteCriticalSection(b3CriticalSection* criticalSection);
};
#endif //B3_SEQUENTIAL_THREAD_SUPPORT_H

View File

@@ -0,0 +1,22 @@
/*
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()
{
}

View File

@@ -0,0 +1,91 @@
/*
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 sendRequest(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 startThreads() =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

View File

@@ -0,0 +1,454 @@
/*
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::sendRequest(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)
{
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,"eventStart%s%d",threadConstructionInfo.m_uniqueName,i);
threadStatus.m_eventStartHandle = CreateEventA (0,false,false,threadStatus.m_eventStartHandleName);
sprintf(threadStatus.m_eventCompletetHandleName,"eventComplete%s%d",threadConstructionInfo.m_uniqueName,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);
//SetThreadPriority(handle,THREAD_PRIORITY_HIGHEST);
SetThreadPriority(handle,THREAD_PRIORITY_TIME_CRITICAL);
SetThreadAffinityMask(handle, 1<<i);
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;
printf("started thread %d with threadHandle %p\n",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);
}
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);
}

View File

@@ -0,0 +1,139 @@
/*
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)();
///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
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,
int numThreads=1,
int threadStackSize=65535
)
:m_uniqueName(uniqueName),
m_userThreadFunc(userThreadFunc),
m_lsMemoryFunc(lsMemoryFunc),
m_numThreads(numThreads),
m_threadStackSize(threadStackSize)
{
}
const char* m_uniqueName;
b3Win32ThreadFunc m_userThreadFunc;
b3Win32lsMemorySetupFunc m_lsMemoryFunc;
int m_numThreads;
int m_threadStackSize;
};
b3Win32ThreadSupport(const Win32ThreadConstructionInfo& threadConstructionInfo);
///cleanup/shutdown Libspe2
virtual ~b3Win32ThreadSupport();
void startThreads(const Win32ThreadConstructionInfo& threadInfo);
///send messages to SPUs
virtual void sendRequest(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

View File

@@ -0,0 +1,185 @@
/*
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 pollingn for completion, currently Windows only
void SampleThreadFunc(void* userPtr,void* lsMemory);
void* SamplelsMemoryFunc();
#include <stdio.h>
//#include "BulletMultiThreaded/PlatformDefinitions.h"
#ifdef USE_PTHREADS
//#ifdef __APPLE__
#include "BulletMultiThreaded/PosixThreadSupport.h"
b3ThreadSupportInterface* createThreadSupport(int numThreads)
{
PosixThreadSupport::ThreadConstructionInfo constructionInfo("testThreads",
SampleThreadFunc,
SamplelsMemoryFunc,
numThreads);
b3ThreadSupportInterface* threadSupport = new PosixThreadSupport(constructionInfo);
return threadSupport;
}
#elif defined( _WIN32)
#include "b3Win32ThreadSupport.h"
b3ThreadSupportInterface* createThreadSupport(int numThreads)
{
b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads",SampleThreadFunc,SamplelsMemoryFunc,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;
}
int main(int argc,char** argv)
{
int numThreads = 8;
b3ThreadSupportInterface* threadSupport = createThreadSupport(numThreads);
threadSupport->startThreads();
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->sendRequest(B3_THREAD_SCHEDULE_TASK, (void*) &args, i);
}
bool blockingWait =true;
if (blockingWait)
{
for (i=0;i<numThreads;i++)
{
threadSupport->waitForResponse(&arg0,&arg1);
printf("finished waiting for response: %d %d\n", arg0,arg1);
}
} else
{
#if _WIN32
int numActiveThreads = numThreads;
while (numActiveThreads)
{
if (((b3Win32ThreadSupport*)threadSupport)->isTaskCompleted(&arg0,&arg1,0))
{
numActiveThreads--;
printf("numActiveThreads = %d\n",numActiveThreads);
} else
{
printf("polling\n");
}
};
#else
btAssert(0);
printf("non-blocking wait is not supported on this platform\n");
exit(0);
#endif
}
printf("stopping threads\n");
delete threadSupport;
printf("Press ENTER to quit\n");
getchar();
return 0;
}

View File

@@ -0,0 +1,36 @@
project "App_ThreadingTest"
kind "ConsoleApp"
-- defines { }
targetdir "../../bin"
includedirs
{
".","../../src"
}
links { "Bullet3Common" }
files {
"**.cpp",
"**.h"
}
if os.is("Windows") then
--links {"winmm"}
--defines {"__WINDOWS_MM__", "WIN32"}
end
if os.is("Linux") then
links {"pthread"}
end
if os.is("MacOSX") then
links {"pthread"}
--links{"CoreAudio.framework", "coreMIDI.framework", "Cocoa.framework"}
--defines {"__MACOSX_CORE__"}
end