This commit is contained in:
liangdc 2024-01-03 14:00:18 +08:00
parent f766613db8
commit aa61c27839
25 changed files with 141 additions and 92 deletions

View File

@ -22,7 +22,6 @@
*/
#include "HPThreadPool.h"
#include "common/FuncHelper.h"
#include <pthread.h>
@ -67,6 +66,9 @@ void DestroySocketTaskObj(LPTSocketTask pTask)
}
}
volatile UINT CHPThreadPool::sm_uiNum = 0;
LPCTSTR CHPThreadPool::POOLED_THREAD_PREFIX = _T("hp-pool-");
BOOL CHPThreadPool::Start(DWORD dwThreadCount, DWORD dwMaxQueueSize, EnRejectedPolicy enRejectedPolicy, DWORD dwStackSize)
{
if(!CheckStarting())
@ -364,10 +366,10 @@ BOOL CHPThreadPool::CreateWorkerThreads(DWORD dwThreadCount)
PVOID CHPThreadPool::ThreadProc(LPVOID pv)
{
::SetDefaultPoolThreadName(SELF_THREAD_ID);
CHPThreadPool* pThis = (CHPThreadPool*)pv;
::SetSequenceThreadName(SELF_THREAD_ID, pThis->m_strPrefix, pThis->m_uiSeq);
pThis->FireWorkerThreadStart();
PVOID rs = (PVOID)(UINT_PTR)(pThis->WorkerProc());
@ -474,6 +476,7 @@ BOOL CHPThreadPool::CheckStoping()
void CHPThreadPool::Reset(BOOL bSetWaitEvent)
{
m_uiSeq = 0;
m_dwStackSize = 0;
m_dwTaskCount = 0;
m_dwThreadCount = 0;
@ -484,3 +487,10 @@ void CHPThreadPool::Reset(BOOL bSetWaitEvent)
if(bSetWaitEvent)
m_evWait.SyncNotifyAll();
}
void CHPThreadPool::MakePrefix()
{
UINT uiNumber = ::InterlockedIncrement(&sm_uiNum);
m_strPrefix.Format(_T("%s%u-"), POOLED_THREAD_PREFIX, uiNumber);
}

View File

@ -124,6 +124,7 @@ public:
CHPThreadPool(IHPThreadPoolListener* pListener = nullptr)
: m_pListener(pListener)
{
MakePrefix();
Reset(FALSE);
}
@ -134,6 +135,14 @@ public:
private:
void Reset(BOOL bSetWaitEvent = TRUE);
void MakePrefix();
private:
static LPCTSTR POOLED_THREAD_PREFIX;
static volatile UINT sm_uiNum;
volatile UINT m_uiSeq;
CString m_strPrefix;
private:
IHPThreadPoolListener* m_pListener;

View File

@ -46,6 +46,18 @@ static const int s_iUdpCloseNotifySize = ARRAY_SIZE(s_szUdpCloseNotify);
const hp_addr hp_addr::ANY_ADDR4(AF_INET, TRUE);
const hp_addr hp_addr::ANY_ADDR6(AF_INET6, TRUE);
BOOL SetCurrentWorkerThreadName()
{
return SetWorkerThreadDefaultName(0);
}
BOOL SetWorkerThreadDefaultName(THR_ID tid)
{
static volatile UINT _s_uiSeq = 0;
return ::SetSequenceThreadName(tid, DEFAULT_WORKER_THREAD_PREFIX, _s_uiSeq);
}
LPCTSTR GetSocketErrorDesc(EnSocketError enCode)
{
switch(enCode)

View File

@ -705,6 +705,14 @@ struct TClientCloseContext
/******************************************** 公共帮助方法 ********************************************/
/*****************************************************************************************************/
/* 默认工作线程前缀 */
#define DEFAULT_WORKER_THREAD_PREFIX "hp-worker-"
/* 设置当前工作线程名称 */
BOOL SetCurrentWorkerThreadName();
/* 设置工作线程默认名称 */
BOOL SetWorkerThreadDefaultName(THR_ID tid);
/* 获取错误描述文本 */
LPCTSTR GetSocketErrorDesc(EnSocketError enCode);
/* 确定地址簇 */

View File

@ -287,7 +287,7 @@ BOOL CTcpClient::CreateWorkerThread()
UINT WINAPI CTcpClient::WorkerThreadProc(LPVOID pv)
{
::SetDefaultWorkerThreadName(SELF_THREAD_ID);
::SetCurrentWorkerThreadName();
TRACE("---------------> Client Worker Thread 0x%08X started <---------------", SELF_THREAD_ID);

View File

@ -289,7 +289,7 @@ BOOL CUdpCast::CreateWorkerThread()
UINT WINAPI CUdpCast::WorkerThreadProc(LPVOID pv)
{
::SetDefaultWorkerThreadName(SELF_THREAD_ID);
::SetCurrentWorkerThreadName();
TRACE("---------------> Cast Worker Thread 0x%08X started <---------------", SELF_THREAD_ID);

View File

@ -297,7 +297,7 @@ BOOL CUdpClient::CreateWorkerThread()
UINT WINAPI CUdpClient::WorkerThreadProc(LPVOID pv)
{
::SetDefaultWorkerThreadName(SELF_THREAD_ID);
::SetCurrentWorkerThreadName();
TRACE("---------------> Client Worker Thread 0x%08X started <---------------", SELF_THREAD_ID);

View File

@ -347,20 +347,6 @@ void ABORT(int iErrno, LPCSTR lpszFile, int iLine, LPCSTR lpszFunc, LPCSTR lpszT
__EXIT_FN_((void (*)(int))abort, "abort", nullptr, iErrno, lpszFile, iLine, lpszFunc, lpszTitle);
}
BOOL SetDefaultWorkerThreadName(THR_ID tid)
{
static volatile UINT _s_uiSeq = 0;
return SetSequenceThreadName(tid, DEFAULT_WORKER_THREAD_PREFIX, _s_uiSeq);
}
BOOL SetDefaultPoolThreadName(THR_ID tid)
{
static volatile UINT _s_uiSeq = 0;
return SetSequenceThreadName(tid, DEFAULT_POOL_THREAD_PREFIX, _s_uiSeq);
}
BOOL SetSequenceThreadName(THR_ID tid, LPCTSTR lpszPrefix, volatile UINT& vuiSeq)
{
UINT uiSequence = InterlockedIncrement(&vuiSeq);
@ -368,39 +354,39 @@ BOOL SetSequenceThreadName(THR_ID tid, LPCTSTR lpszPrefix, volatile UINT& vuiSeq
return SetThreadName(tid, lpszPrefix, uiSequence);
}
BOOL SetThreadName(THR_ID tid, LPCSTR lpszPrefix, UINT uiSequence)
BOOL SetThreadName(THR_ID tid, LPCTSTR lpszPrefix, UINT uiSequence)
{
int iMaxSeqLength = (int)(MAX_WORKER_THREAD_NAME_LENGTH - strlen(lpszPrefix));
int iMaxSeqLength = (int)(MAX_THREAD_NAME_LENGTH - lstrlen(lpszPrefix));
ASSERT(iMaxSeqLength > 0 && iMaxSeqLength <= 10);
ASSERT(iMaxSeqLength > 0);
if(iMaxSeqLength <= 0 || iMaxSeqLength > 10)
if(iMaxSeqLength <= 0)
{
::SetLastError(ERROR_OUT_OF_RANGE);
return FALSE;
}
UINT uiDiv = 1;
ULONGLONG uiDiv = 1;
for(int i = 0; i < iMaxSeqLength; i++)
uiDiv *= 10;
uiSequence %= uiDiv;
uiSequence = (UINT)(uiSequence % uiDiv);
TCHAR szName[MAX_WORKER_THREAD_NAME_LENGTH + 1];
sprintf(szName, "%s%u", lpszPrefix, uiSequence);
CStringA strName;
strName.Format(_T("%s%u"), lpszPrefix, uiSequence);
return SetThreadName(tid, szName);
return SetThreadName(tid, strName);
}
BOOL SetThreadName(THR_ID tid, LPCSTR lpszName)
BOOL SetThreadName(THR_ID tid, LPCTSTR lpszName)
{
ASSERT(strlen(lpszName) <= MAX_WORKER_THREAD_NAME_LENGTH);
ASSERT(lstrlen(lpszName) <= MAX_THREAD_NAME_LENGTH);
if(tid == 0)
tid = SELF_THREAD_ID;
int rs = pthread_setname_np(tid, lpszName);
int rs = pthread_setname_np(tid, CT2A(lpszName));
CHECK_ERROR_CODE(rs)

View File

@ -71,7 +71,7 @@ using namespace std;
#if defined(DEBUG) && defined(DEBUG_TRACE)
#define TRACE(fmt, ...) PRINTLN("> TRC (0x%zX, %d) " fmt, (SIZE_T)SELF_THREAD_ID, SELF_NATIVE_THREAD_ID, ##__VA_ARGS__)
#define ASSERT(expr) ((expr) ? TRUE : (::PrintStackTrace(), assert((FALSE)), FALSE))
#define ASSERT(expr) ((expr) ? TRUE : (::PrintStackTrace(), assert(FALSE), FALSE))
#else
#define TRACE(fmt, ...)
#define ASSERT(expr) assert(expr)
@ -317,19 +317,12 @@ void EXIT(int iExitCode = 0, int iErrno = -1, LPCSTR lpszFile = nullptr, int iLi
void _EXIT(int iExitCode = 0, int iErrno = -1, LPCSTR lpszFile = nullptr, int iLine = 0, LPCSTR lpszFunc = nullptr, LPCSTR lpszTitle = nullptr);
void ABORT(int iErrno = -1, LPCSTR lpszFile = nullptr, int iLine = 0, LPCSTR lpszFunc = nullptr, LPCSTR lpszTitle = nullptr);
/* ĬÈϹ¤×÷Ïß³Ìǰ׺ */
#define DEFAULT_WORKER_THREAD_PREFIX "HP-Worker-"
/* ĬÈÏÏ̳߳ØÏß³Ìǰ׺ */
#define DEFAULT_POOL_THREAD_PREFIX "HP-Pool-"
/* 工作线程名称最大长度 */
#define MAX_THREAD_NAME_LENGTH 15
/* ĬÈϹ¤×÷Ïß³Ìǰ׺ */
#define MAX_WORKER_THREAD_NAME_LENGTH 15
BOOL SetDefaultWorkerThreadName(THR_ID tid);
BOOL SetDefaultPoolThreadName(THR_ID tid);
BOOL SetSequenceThreadName(THR_ID tid, LPCSTR lpszPrefix, volatile UINT& vuiSeq);
BOOL SetThreadName(THR_ID tid, LPCSTR lpszPrefix, UINT uiSequence);
BOOL SetThreadName(THR_ID tid, LPCSTR lpszName);
BOOL SetSequenceThreadName(THR_ID tid, LPCTSTR lpszPrefix, volatile UINT& vuiSeq);
BOOL SetThreadName(THR_ID tid, LPCTSTR lpszPrefix, UINT uiSequence);
BOOL SetThreadName(THR_ID tid, LPCTSTR lpszName);
template<typename T, typename = enable_if_t<is_integral<T>::value>>
inline bool IS_INFINITE(T v)

View File

@ -27,6 +27,9 @@
#include <signal.h>
#include <pthread.h>
volatile UINT CIODispatcher::sm_uiNum = 0;
LPCTSTR CIODispatcher::WORKER_THREAD_PREFIX = _T("io-disp-");
BOOL CIODispatcher::Start(IIOHandler* pHandler, int iWorkerMaxEvents, int iWorkers, LLONG llTimerInterval)
{
ASSERT_CHECK_EINVAL(pHandler && iWorkerMaxEvents >= 0 && iWorkers >= 0);
@ -130,6 +133,7 @@ BOOL CIODispatcher::Stop(BOOL bCheck)
VOID CIODispatcher::Reset()
{
m_uiSeq = 0;
m_iWorkers = 0;
m_iMaxEvents= 0;
m_pHandler = nullptr;
@ -140,6 +144,14 @@ VOID CIODispatcher::Reset()
m_evTimer = INVALID_FD;
}
void CIODispatcher::MakePrefix()
{
UINT uiNumber = ::InterlockedIncrement(&sm_uiNum);
m_strPrefix.Format(_T("%s%u-"), WORKER_THREAD_PREFIX, uiNumber);
}
BOOL CIODispatcher::SendCommand(USHORT t, UINT_PTR wp, UINT_PTR lp)
{
return SendCommand(TDispCommand::Construct(t, wp, lp));
@ -159,7 +171,7 @@ BOOL CIODispatcher::CtlFD(FD fd, int op, UINT mask, PVOID pv)
int CIODispatcher::WorkerProc(PVOID pv)
{
::SetDefaultWorkerThreadName(SELF_THREAD_ID);
::SetSequenceThreadName(SELF_THREAD_ID, m_strPrefix, m_uiSeq);
m_pHandler->OnDispatchThreadStart(SELF_THREAD_ID);

View File

@ -164,16 +164,24 @@ private:
BOOL ProcessTimer(UINT events);
BOOL ProcessCommand(UINT events);
BOOL DoProcessIo(PVOID pv, UINT events);
VOID Reset();
VOID MakePrefix();
public:
BOOL HasStarted() {return m_pHandler && m_pWorkers;}
const CWorkerThread* GetWorkerThreads() {return m_pWorkers.get();}
CIODispatcher() {Reset();}
CIODispatcher() {MakePrefix(); Reset();}
~CIODispatcher() {if(HasStarted()) Stop();}
private:
static LPCTSTR WORKER_THREAD_PREFIX;
static volatile UINT sm_uiNum;
volatile UINT m_uiSeq;
CString m_strPrefix;
private:
IIOHandler* m_pHandler;
FD m_epoll;

View File

@ -68,20 +68,6 @@ void ABORT(int iErrno, LPCSTR lpszFile, int iLine, LPCSTR lpszFunc, LPCSTR lpszT
__EXIT_FN_((void (*)(int))abort, "abort", nullptr, iErrno, lpszFile, iLine, lpszFunc, lpszTitle);
}
BOOL SetDefaultWorkerThreadName(HANDLE hThread)
{
static volatile UINT _s_uiSeq = 0;
return SetSequenceThreadName(hThread, DEFAULT_WORKER_THREAD_PREFIX, _s_uiSeq);
}
BOOL SetDefaultPoolThreadName(HANDLE hThread)
{
static volatile UINT _s_uiSeq = 0;
return SetSequenceThreadName(hThread, DEFAULT_POOL_THREAD_PREFIX, _s_uiSeq);
}
BOOL SetSequenceThreadName(HANDLE hThread, LPCTSTR lpszPrefix, volatile UINT& vuiSeq)
{
#if _WIN32_WINNT < _WIN32_WINNT_WIN10
@ -96,12 +82,12 @@ BOOL SetSequenceThreadName(HANDLE hThread, LPCTSTR lpszPrefix, volatile UINT& vu
BOOL SetThreadName(HANDLE hThread, LPCTSTR lpszPrefix, UINT uiSequence)
{
TCHAR szName[MAX_PATH];
_stprintf(szName, _T("%s%u"), lpszPrefix, uiSequence);
CString strName;
strName.Format(_T("%s%u"), lpszPrefix, uiSequence);
ASSERT(_tcslen(szName) < MAX_PATH);
ASSERT(strName.GetLength() < MAX_PATH);
return SetThreadName(hThread, szName);
return SetThreadName(hThread, strName);
}
BOOL SetThreadName(HANDLE hThread, LPCTSTR lpszName)

View File

@ -143,13 +143,6 @@ void EXIT(int iExitCode = 0, int iErrno = -1, LPCSTR lpszFile = nullptr, int iLi
void _EXIT(int iExitCode = 0, int iErrno = -1, LPCSTR lpszFile = nullptr, int iLine = 0, LPCSTR lpszFunc = nullptr, LPCSTR lpszTitle = nullptr);
void ABORT(int iErrno = -1, LPCSTR lpszFile = nullptr, int iLine = 0, LPCSTR lpszFunc = nullptr, LPCSTR lpszTitle = nullptr);
/* 默认工作线程前缀 */
#define DEFAULT_WORKER_THREAD_PREFIX _T("HP-Worker-")
/* 默认线程池线程前缀 */
#define DEFAULT_POOL_THREAD_PREFIX _T("HP-Pool-")
BOOL SetDefaultWorkerThreadName(HANDLE hThread);
BOOL SetDefaultPoolThreadName(HANDLE hThread);
BOOL SetSequenceThreadName(HANDLE hThread, LPCTSTR lpszPrefix, volatile UINT& vuiSeq);
BOOL SetThreadName(HANDLE hThread, LPCTSTR lpszPrefix, UINT uiSequence);
BOOL SetThreadName(HANDLE hThread, LPCTSTR lpszName);

View File

@ -140,7 +140,7 @@ _WIN32_WINNT_WIN10 0x0A00
#ifndef _WIN32_WINNT
#if defined (_WIN64)
#define _WIN32_WINNT _WIN32_WINNT_WIN7
#define _WIN32_WINNT _WIN32_WINNT_WIN7
#else
#if _MSC_VER >= 1910
#define _WIN32_WINNT _WIN32_WINNT_WIN7

View File

@ -66,12 +66,17 @@ void DestroySocketTaskObj(LPTSocketTask pTask)
}
}
volatile UINT CHPThreadPool::sm_uiNum = 0;
LPCTSTR CHPThreadPool::POOLED_THREAD_PREFIX = _T("hp-pool-");
BOOL CHPThreadPool::CWorker::Initialize(PVOID pvWorkerParam)
{
ASSERT(pvWorkerParam != nullptr);
m_pthPool = (CHPThreadPool*)pvWorkerParam;
::SetSequenceThreadName(SELF_THREAD, m_pthPool->m_strPrefix, m_pthPool->m_uiSeq);
m_pthPool->FireWorkerThreadStart();
return TRUE;
@ -373,6 +378,7 @@ BOOL CHPThreadPool::CheckStoping()
void CHPThreadPool::Reset(BOOL bSetWaitEvent)
{
m_uiSeq = 0;
m_dwQueueSize = 0;
m_dwTaskCount = 0;
m_dwMaxQueueSize = 0;
@ -382,3 +388,10 @@ void CHPThreadPool::Reset(BOOL bSetWaitEvent)
if(bSetWaitEvent)
m_evWait.Set();
}
void CHPThreadPool::MakePrefix()
{
UINT uiNumber = ::InterlockedIncrement(&sm_uiNum);
m_strPrefix.Format(_T("%s%u-"), POOLED_THREAD_PREFIX, uiNumber);
}

View File

@ -95,17 +95,7 @@ class CHPThreadPool : public IHPThreadPool
friend class CWorker;
class CInnerThreadPool : public CThreadPool<CWorker>
{
protected:
DWORD ThreadProc() throw()
{
::SetDefaultPoolThreadName(SELF_THREAD);
return __super::ThreadProc();
}
};
typedef CThreadPool<CWorker> CInnerThreadPool;
private:
enum EnSubmitResult{SUBMIT_OK, SUBMIT_FULL, SUBMIT_ERROR};
@ -153,6 +143,7 @@ public:
: m_pListener(pListener)
, m_evWait(TRUE, TRUE)
{
MakePrefix();
Reset(FALSE);
}
@ -163,6 +154,14 @@ public:
private:
void Reset(BOOL bSetWaitEvent = TRUE);
void MakePrefix();
private:
static LPCTSTR POOLED_THREAD_PREFIX;
static volatile UINT sm_uiNum;
volatile UINT m_uiSeq;
CString m_strPrefix;
private:
IHPThreadPoolListener* m_pListener;

View File

@ -1133,6 +1133,18 @@ BOOL SetMultiCastSocketOptions(SOCKET sock, const HP_SOCKADDR& bindAddr, const H
///////////////////////////////////////////////////////////////////////////////////////////////////////
BOOL SetCurrentWorkerThreadName()
{
return SetWorkerThreadDefaultName(nullptr);
}
BOOL SetWorkerThreadDefaultName(HANDLE hThread)
{
static volatile UINT _s_uiSeq = 0;
return ::SetSequenceThreadName(hThread, DEFAULT_WORKER_THREAD_PREFIX, _s_uiSeq);
}
LPCTSTR GetSocketErrorDesc(EnSocketError enCode)
{
switch(enCode)

View File

@ -770,6 +770,14 @@ struct TClientCloseContext
/******************************************** 公共帮助方法 ********************************************/
/*****************************************************************************************************/
/* 默认工作线程前缀 */
#define DEFAULT_WORKER_THREAD_PREFIX _T("hp-worker-")
/* 设置当前工作线程名称 */
BOOL SetCurrentWorkerThreadName();
/* 设置工作线程默认名称 */
BOOL SetWorkerThreadDefaultName(HANDLE hThread);
/* 获取错误描述文本 */
LPCTSTR GetSocketErrorDesc(EnSocketError enCode);
/* 确定地址簇 */

View File

@ -838,7 +838,7 @@ void CTcpAgent::CloseCompletePort()
UINT WINAPI CTcpAgent::WorkerThreadProc(LPVOID pv)
{
::SetDefaultWorkerThreadName(SELF_THREAD);
::SetCurrentWorkerThreadName();
CTcpAgent* pAgent = (CTcpAgent*)pv;
pAgent->OnWorkerThreadStart(SELF_THREAD_ID);

View File

@ -232,7 +232,7 @@ BOOL CTcpClient::CreateWorkerThread()
UINT WINAPI CTcpClient::WorkerThreadProc(LPVOID pv)
{
::SetDefaultWorkerThreadName(SELF_THREAD);
::SetCurrentWorkerThreadName();
TRACE("---------------> Client Worker Thread 0x%08X started <---------------\n", SELF_THREAD_ID);

View File

@ -885,7 +885,7 @@ BOOL CTcpServer::DoAccept()
UINT WINAPI CTcpServer::WorkerThreadProc(LPVOID pv)
{
::SetDefaultWorkerThreadName(SELF_THREAD);
::SetCurrentWorkerThreadName();
CTcpServer* pServer = (CTcpServer*)pv;
pServer->OnWorkerThreadStart(SELF_THREAD_ID);

View File

@ -234,7 +234,7 @@ BOOL CUdpCast::CreateWorkerThread()
UINT WINAPI CUdpCast::WorkerThreadProc(LPVOID pv)
{
::SetDefaultWorkerThreadName(SELF_THREAD);
::SetCurrentWorkerThreadName();
TRACE("---------------> Client Worker Thread 0x%08X started <---------------\n", SELF_THREAD_ID);

View File

@ -234,7 +234,7 @@ BOOL CUdpClient::CreateWorkerThread()
UINT WINAPI CUdpClient::WorkerThreadProc(LPVOID pv)
{
::SetDefaultWorkerThreadName(SELF_THREAD);
::SetCurrentWorkerThreadName();
TRACE("---------------> Client Worker Thread 0x%08X started <---------------\n", SELF_THREAD_ID);

View File

@ -534,7 +534,7 @@ int CUdpNode::SendInternal(HP_SOCKADDR& addrRemote, TUdpBufferObjPtr& bufPtr)
UINT WINAPI CUdpNode::WorkerThreadProc(LPVOID pv)
{
::SetDefaultWorkerThreadName(SELF_THREAD);
::SetCurrentWorkerThreadName();
CUdpNode* pNode = (CUdpNode*)pv;
pNode->OnWorkerThreadStart(SELF_THREAD_ID);

View File

@ -895,7 +895,7 @@ void CUdpServer::CloseCompletePort()
UINT WINAPI CUdpServer::WorkerThreadProc(LPVOID pv)
{
::SetDefaultWorkerThreadName(SELF_THREAD);
::SetCurrentWorkerThreadName();
CUdpServer* pServer = (CUdpServer*)pv;
pServer->OnWorkerThreadStart(SELF_THREAD_ID);