⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 winthread.cc

📁 五行MMORPG引擎系统V1.0
💻 CC
字号:
//-----------------------------------------------------------------------------
// Torque Game Engine
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------

#include "platform/platformThread.h"
#include "platformWin32/platformWin32.h"
#include "platform/platformSemaphore.h"

#define THREAD_WAIT_TIMEOUT	5000

//--------------------------------------------------------------------------
struct WinThreadData
{
   ThreadRunFunction    mRunFunc;
   S32                  mRunArg;
   Thread *             mThread;
   void *               mSemaphore;
//#ifdef TGE_RPG
	HANDLE					hThread;
	BOOL						bAutoDel;
	BOOL						bWorking;
//#endif
};



static DWORD WINAPI ThreadRunHandler(void * arg)
{
   WinThreadData * threadData = reinterpret_cast<WinThreadData*>(arg);

   threadData->mThread->run(threadData->mRunArg);
	//释放信号,解除join阻塞
   Semaphore::releaseSemaphore(threadData->mSemaphore);
	
	threadData->bWorking		= FALSE;

	if(threadData->bAutoDel)
		delete threadData->mThread;

   return(0);
}


//--------------------------------------------------------------------------
Thread::Thread(ThreadRunFunction func, S32 arg, bool start_thread)
{
   WinThreadData * threadData = new WinThreadData();
   threadData->mRunFunc		= func;
   threadData->mRunArg		= arg;
   threadData->mThread		= this;
   threadData->mSemaphore	= Semaphore::createSemaphore();
   threadData->bAutoDel		= FALSE;
   threadData->hThread		= 0;
	threadData->bWorking		= TRUE;

   mData = reinterpret_cast<void*>(threadData);
   if (start_thread)
      start();
}

Thread::~Thread()
{
   join();

   WinThreadData * threadData = reinterpret_cast<WinThreadData*>(mData);

	//while(Semaphore::acquireSemaphore(threadData->mSemaphore,false))
	//   Semaphore::releaseSemaphore(threadData->mSemaphore);

   Semaphore::destroySemaphore(threadData->mSemaphore);
   delete threadData;
}



void Thread::setFunc(ThreadRunFunction func, S32 arg)
{
   if(!isAlive())
      return;
   WinThreadData * threadData = reinterpret_cast<WinThreadData*>(mData);
	threadData->mRunArg	= arg;
	threadData->mRunFunc	= func;
}

//#ifdef TGE_RPG
void Thread::setAutoDel(BOOL bSet)
{
   WinThreadData * threadData = reinterpret_cast<WinThreadData*>(mData);
	threadData->bAutoDel = bSet;
}

void Thread::setPriority(S32 nPrio)
{
   if(!isAlive())
      return;
   WinThreadData * threadData = reinterpret_cast<WinThreadData*>(mData);
	::SetThreadPriority(threadData->hThread, nPrio);
}
//#endif


bool Thread::start(S32 nPrio)
{
   if(isAlive())
      return true;

   WinThreadData * threadData = reinterpret_cast<WinThreadData*>(mData);
   Semaphore::acquireSemaphore(threadData->mSemaphore);

   DWORD		threadID;
	HANDLE	hThread;
	hThread = CreateThread(0, 0, ThreadRunHandler, mData, 0, &threadID);
	if(hThread != NULL)
	{
		if(nPrio != PRI_NORMAL)
			::SetThreadPriority(hThread, nPrio);
		threadData->hThread	= hThread;
	}
	else
	{
		Semaphore::releaseSemaphore(threadData->mSemaphore);
	}

	return hThread != NULL;
}


bool Thread::join()
{
   if(!isAlive())
      return(false);

	//阻塞调用者,以等待线程结束
   WinThreadData * threadData = reinterpret_cast<WinThreadData*>(mData);
   return(Semaphore::acquireSemaphore(threadData->mSemaphore));
}

bool Thread::waitForExit()
{
   if(!isAlive())
      return(false);

   WinThreadData * threadData = reinterpret_cast<WinThreadData*>(mData);
	return WaitForSingleObject(threadData->hThread,THREAD_WAIT_TIMEOUT) == WAIT_OBJECT_0;
}


void Thread::run(S32 arg)
{
   WinThreadData * threadData = reinterpret_cast<WinThreadData*>(mData);
   if(threadData->mRunFunc)
      threadData->mRunFunc(arg);
}

bool Thread::isAlive()
{
   WinThreadData * threadData = reinterpret_cast<WinThreadData*>(mData);
	if(!threadData->bWorking || threadData->hThread == NULL)
		return false;

   bool signal = Semaphore::acquireSemaphore(threadData->mSemaphore, false);
   if(signal)
      Semaphore::releaseSemaphore(threadData->mSemaphore);
   return(!signal);
}

U32 Thread::getCurrentThreadId()
{
   return GetCurrentThreadId();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -