mirror of
https://github.com/tribes2/engine.git
synced 2026-03-06 05:50:24 +00:00
t2 engine svn checkout
This commit is contained in:
commit
ff569bd2ae
988 changed files with 394180 additions and 0 deletions
97
platformX86UNIX/x86UNIXThread.cc
Normal file
97
platformX86UNIX/x86UNIXThread.cc
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// V12 Engine
|
||||
//
|
||||
// Copyright (c) 2001 GarageGames.Com
|
||||
// Portions Copyright (c) 2001 by Sierra Online, Inc.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include "platform/platformThread.h"
|
||||
#include "platformX86UNIX/platformX86UNIX.h"
|
||||
#include "platform/platformSemaphore.h"
|
||||
#include <pthread.h>
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
struct x86UNIXThreadData
|
||||
{
|
||||
ThreadRunFunction mRunFunc;
|
||||
S32 mRunArg;
|
||||
Thread * mThread;
|
||||
void * mSemaphore;
|
||||
|
||||
x86UNIXThreadData()
|
||||
{
|
||||
mRunFunc = 0;
|
||||
mRunArg = 0;
|
||||
mThread = 0;
|
||||
mSemaphore = 0;
|
||||
};
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
Thread::Thread(ThreadRunFunction func, S32 arg, bool start_thread)
|
||||
{
|
||||
x86UNIXThreadData * threadData = new x86UNIXThreadData();
|
||||
threadData->mRunFunc = func;
|
||||
threadData->mRunArg = arg;
|
||||
threadData->mThread = this;
|
||||
threadData->mSemaphore = Semaphore::createSemaphore();
|
||||
|
||||
mData = reinterpret_cast<void*>(threadData);
|
||||
if (start_thread)
|
||||
start();
|
||||
}
|
||||
|
||||
Thread::~Thread()
|
||||
{
|
||||
join();
|
||||
|
||||
x86UNIXThreadData * threadData = reinterpret_cast<x86UNIXThreadData*>(mData);
|
||||
Semaphore::destroySemaphore(threadData->mSemaphore);
|
||||
delete threadData;
|
||||
}
|
||||
|
||||
static void *ThreadRunHandler(void * arg)
|
||||
{
|
||||
x86UNIXThreadData * threadData = reinterpret_cast<x86UNIXThreadData*>(arg);
|
||||
|
||||
threadData->mThread->run(threadData->mRunArg);
|
||||
Semaphore::releaseSemaphore(threadData->mSemaphore);
|
||||
}
|
||||
|
||||
void Thread::start()
|
||||
{
|
||||
if(isAlive())
|
||||
return;
|
||||
|
||||
x86UNIXThreadData * threadData = reinterpret_cast<x86UNIXThreadData*>(mData);
|
||||
Semaphore::acquireSemaphore(threadData->mSemaphore);
|
||||
|
||||
pthread_t threadID;
|
||||
pthread_create(&threadID, NULL, ThreadRunHandler, mData);
|
||||
}
|
||||
|
||||
bool Thread::join()
|
||||
{
|
||||
if(!isAlive())
|
||||
return(false);
|
||||
|
||||
x86UNIXThreadData * threadData = reinterpret_cast<x86UNIXThreadData*>(mData);
|
||||
return(Semaphore::acquireSemaphore(threadData->mSemaphore));
|
||||
}
|
||||
|
||||
void Thread::run(S32 arg)
|
||||
{
|
||||
x86UNIXThreadData * threadData = reinterpret_cast<x86UNIXThreadData*>(mData);
|
||||
if(threadData->mRunFunc)
|
||||
threadData->mRunFunc(arg);
|
||||
}
|
||||
|
||||
bool Thread::isAlive()
|
||||
{
|
||||
x86UNIXThreadData * threadData = reinterpret_cast<x86UNIXThreadData*>(mData);
|
||||
|
||||
bool signal = Semaphore::acquireSemaphore(threadData->mSemaphore, false);
|
||||
if(signal)
|
||||
Semaphore::releaseSemaphore(threadData->mSemaphore);
|
||||
return(!signal);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue