#include "ofxsSupportPrivate.h"
namespace OFX {
namespace MultiThread {
Processor::Processor(void)
{
}
Processor::~Processor()
{
}
void Processor::staticMultiThreadFunction(unsigned int threadIndex, unsigned int threadMax, void *customArg)
{
Processor *me = (Processor *) customArg;
me->multiThreadFunction(threadIndex, threadMax);
}
void Processor::multiThread(unsigned int nCPUs)
{
if(nCPUs == 0)
nCPUs = OFX::MultiThread::getNumCPUs();
if(nCPUs == 1) {
multiThreadFunction(0, 1);
}
else {
OfxStatus stat = kOfxStatFailed;
if(OFX::Private::gThreadSuite){
stat = OFX::Private::gThreadSuite->multiThread(staticMultiThreadFunction, nCPUs, (void *)this);
}
throwSuiteStatusException(stat);
}
}
bool isSpawnedThread(void)
{
if(OFX::Private::gThreadSuite){
int v = OFX::Private::gThreadSuite->multiThreadIsSpawnedThread();
return v != 0;
}else{
return false;
}
}
unsigned int getNumCPUs(void)
{
unsigned int n = 1;
OfxStatus stat = OFX::Private::gThreadSuite ? OFX::Private::gThreadSuite->multiThreadNumCPUs(&n) : kOfxStatFailed;
if(stat != kOfxStatOK) n = 1;
return n;
}
unsigned int getThreadIndex(void)
{
unsigned int n = 0;
OfxStatus stat = OFX::Private::gThreadSuite ? OFX::Private::gThreadSuite->multiThreadIndex(&n) : kOfxStatFailed;
if(stat != kOfxStatOK) n = 0;
return n;
}
Mutex::Mutex(int lockCount)
: _handle(0)
{
OfxStatus stat = OFX::Private::gThreadSuite ? OFX::Private::gThreadSuite->mutexCreate(&_handle, lockCount) : kOfxStatReplyDefault;
throwSuiteStatusException(stat);
}
Mutex::~Mutex(void)
{
OfxStatus stat = OFX::Private::gThreadSuite ? OFX::Private::gThreadSuite->mutexDestroy(_handle) : kOfxStatReplyDefault;
(void)stat;
}
void Mutex::lock()
{
OfxStatus stat = OFX::Private::gThreadSuite ? OFX::Private::gThreadSuite->mutexLock(_handle) : kOfxStatReplyDefault;
throwSuiteStatusException(stat);
}
void Mutex::unlock()
{
OfxStatus stat = OFX::Private::gThreadSuite ? OFX::Private::gThreadSuite->mutexUnLock(_handle) : kOfxStatReplyDefault;
throwSuiteStatusException(stat);
}
bool Mutex::tryLock()
{
OfxStatus stat = OFX::Private::gThreadSuite ? OFX::Private::gThreadSuite->mutexTryLock(_handle) : kOfxStatReplyDefault;
return stat == kOfxStatOK;
}
};
};