Restructure repository to include all source folders
Move git root from Client/ to src/ to track all source code: - Client: Game client source (moved to Client/Client/) - Server: Game server source - GameTools: Development tools - CryptoSource: Encryption utilities - database: Database scripts - Script: Game scripts - rylCoder_16.02.2008_src: Legacy coder tools - GMFont, Game: Additional resources 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
126
Server/RylServerProject/BaseLibrary/Thread/ThreadMgr.cpp
Normal file
126
Server/RylServerProject/BaseLibrary/Thread/ThreadMgr.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
#include "stdafx.h"
|
||||
#include "Thread.h"
|
||||
#include "ThreadMgr.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include "GMMemory.h"
|
||||
|
||||
// constructor and destructor
|
||||
CThreadMgr::CThreadMgr()
|
||||
: m_nThreadNum(0), m_bJoinStarted(FALSE)
|
||||
{
|
||||
std::fill_n(m_lpThreads, size_t(MAX_THREAD_NUM), reinterpret_cast<CThread*>(0));
|
||||
std::fill_n(m_hThreads, size_t(MAX_THREAD_NUM), INVALID_HANDLE_VALUE);
|
||||
}
|
||||
|
||||
CThreadMgr::~CThreadMgr()
|
||||
{
|
||||
JoinThread();
|
||||
}
|
||||
|
||||
|
||||
bool CThreadMgr::RegisterAndRun(CThread* llpThread)
|
||||
{
|
||||
unsigned int nThreadID = 0;
|
||||
unsigned int nThreadIndex = 0;
|
||||
|
||||
if(0 == llpThread)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Lock
|
||||
{
|
||||
ThreadLock::Syncronize sync(m_ThreadLock);
|
||||
|
||||
if(63 <= m_nThreadNum || TRUE == m_bJoinStarted)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
nThreadIndex = m_nThreadNum;
|
||||
++m_nThreadNum;
|
||||
}
|
||||
|
||||
m_lpThreads[nThreadIndex] = llpThread;
|
||||
m_hThreads[nThreadIndex] = reinterpret_cast<HANDLE>(_beginthreadex(0, 0,
|
||||
CThread::ThreadFunc, llpThread, 0, &nThreadID));
|
||||
|
||||
return (0 != m_hThreads[nThreadIndex]);
|
||||
}
|
||||
|
||||
bool CThreadMgr::JoinThread()
|
||||
{
|
||||
|
||||
{
|
||||
ThreadLock::Syncronize sync(m_ThreadLock);
|
||||
if(0 == m_nThreadNum)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>۵Ǹ<DBB5>, <20><><EFBFBD>̻<EFBFBD><CCBB><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
||||
m_bJoinStarted = TRUE;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> & <20><><EFBFBD><EFBFBD>.
|
||||
std::for_each(&m_lpThreads[0], &m_lpThreads[m_nThreadNum], std::mem_fun(&CThread::End));
|
||||
WaitForMultipleObjects(m_nThreadNum, m_hThreads, TRUE, INFINITE);
|
||||
|
||||
CThread** lppPastEndThread = m_lpThreads + MAX_THREAD_NUM;
|
||||
HANDLE* lppPastEndHandle = m_hThreads + MAX_THREAD_NUM;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>Ҹ<EFBFBD>.
|
||||
for(CThread** lplpThread = m_lpThreads;
|
||||
lplpThread != lppPastEndThread; ++lplpThread)
|
||||
{
|
||||
delete *lplpThread;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ڵ<EFBFBD> <20>Ҹ<EFBFBD>.
|
||||
for(HANDLE* lppHandle = m_hThreads;
|
||||
lppHandle != lppPastEndHandle; ++lppHandle)
|
||||
{
|
||||
CloseHandle(*lppHandle);
|
||||
}
|
||||
|
||||
{
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD>.
|
||||
ThreadLock::Syncronize sync(m_ThreadLock);
|
||||
m_nThreadNum = 0;
|
||||
m_bJoinStarted = FALSE;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
HANDLE CThreadMgr::Run(CThread* lpThread)
|
||||
{
|
||||
unsigned int nThreadID = 0;
|
||||
HANDLE hThread = reinterpret_cast<HANDLE>(_beginthreadex(0,
|
||||
0, CThread::ThreadFunc, lpThread, 0, &nThreadID));
|
||||
|
||||
lpThread->SetHandle(hThread);
|
||||
return hThread;
|
||||
}
|
||||
|
||||
bool CThreadMgr::Stop(CThread* lpThread, unsigned long dwTimeout)
|
||||
{
|
||||
if(0 == lpThread)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
HANDLE hThread = lpThread->GetHandle();
|
||||
if(INVALID_HANDLE_VALUE == hThread)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
lpThread->SetHandle(INVALID_HANDLE_VALUE);
|
||||
lpThread->End();
|
||||
WaitForSingleObject(hThread, dwTimeout);
|
||||
return (TRUE == CloseHandle(hThread));
|
||||
}
|
||||
Reference in New Issue
Block a user