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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user