This commit is contained in:
liangdc 2024-01-02 20:00:08 +08:00
parent ea51f5ca66
commit f766613db8
8 changed files with 58 additions and 21 deletions

View File

@ -76,6 +76,7 @@
#define ERROR_NOT_SUPPORTED EPERM
#define ERROR_BAD_FORMAT EBADMSG
#define ERROR_BUFFER_OVERFLOW E2BIG
#define ERROR_OUT_OF_RANGE ERANGE
#define ERROR_DESTINATION_ELEMENT_FULL EXFULL
#define ERROR_ALREADY_INITIALIZED EALREADY

View File

@ -364,6 +364,8 @@ BOOL CHPThreadPool::CreateWorkerThreads(DWORD dwThreadCount)
PVOID CHPThreadPool::ThreadProc(LPVOID pv)
{
::SetDefaultPoolThreadName(SELF_THREAD_ID);
CHPThreadPool* pThis = (CHPThreadPool*)pv;
pThis->FireWorkerThreadStart();

View File

@ -351,21 +351,30 @@ BOOL SetDefaultWorkerThreadName(THR_ID tid)
{
static volatile UINT _s_uiSeq = 0;
UINT uiSequence = InterlockedIncrement(&_s_uiSeq);
return SetWorkerThreadName(tid, uiSequence);
return SetSequenceThreadName(tid, DEFAULT_WORKER_THREAD_PREFIX, _s_uiSeq);
}
BOOL SetWorkerThreadName(THR_ID tid, UINT uiSequence)
BOOL SetDefaultPoolThreadName(THR_ID tid)
{
return SetThreadName(tid, DEFAULT_WORKER_THREAD_PREFIX, uiSequence);
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);
return SetThreadName(tid, lpszPrefix, uiSequence);
}
BOOL SetThreadName(THR_ID tid, LPCSTR lpszPrefix, UINT uiSequence)
{
int iMaxSeqLength = (int)(MAX_WORKER_THREAD_NAME_LENGTH - strlen(DEFAULT_WORKER_THREAD_PREFIX));
int iMaxSeqLength = (int)(MAX_WORKER_THREAD_NAME_LENGTH - strlen(lpszPrefix));
if(iMaxSeqLength <= 0)
ASSERT(iMaxSeqLength > 0 && iMaxSeqLength <= 10);
if(iMaxSeqLength <= 0 || iMaxSeqLength > 10)
{
::SetLastError(ERROR_OUT_OF_RANGE);
return FALSE;

View File

@ -319,12 +319,15 @@ void ABORT(int iErrno = -1, LPCSTR lpszFile = nullptr, int iLine = 0, LPCSTR lps
/* ĬÈϹ¤×÷Ïß³Ìǰ׺ */
#define DEFAULT_WORKER_THREAD_PREFIX "HP-Worker-"
/* ĬÈÏÏ̳߳ØÏß³Ìǰ׺ */
#define DEFAULT_POOL_THREAD_PREFIX "HP-Pool-"
/* ĬÈϹ¤×÷Ïß³Ìǰ׺ */
#define MAX_WORKER_THREAD_NAME_LENGTH 15
BOOL SetDefaultWorkerThreadName(THR_ID tid);
BOOL SetWorkerThreadName(THR_ID tid, UINT uiSequence);
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);

View File

@ -69,29 +69,38 @@ void ABORT(int iErrno, LPCSTR lpszFile, int iLine, LPCSTR lpszFunc, LPCSTR lpszT
}
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
::SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
return FALSE;
#else
static volatile UINT _s_uiSeq = 0;
UINT uiSequence = ::InterlockedIncrement(&vuiSeq);
UINT uiSequence = ::InterlockedIncrement(&_s_uiSeq);
return SetWorkerThreadName(hThread, uiSequence);
return SetThreadName(hThread, lpszPrefix, uiSequence);
#endif
}
BOOL SetWorkerThreadName(HANDLE hThread, UINT uiSequence)
{
return SetThreadName(hThread, DEFAULT_WORKER_THREAD_PREFIX, uiSequence);
}
BOOL SetThreadName(HANDLE hThread, LPCTSTR lpszPrefix, UINT uiSequence)
{
TCHAR szName[MAX_PATH];
_stprintf(szName, _T("%s%u"), lpszPrefix, uiSequence);
ASSERT(_tcslen(szName) < MAX_PATH);
return SetThreadName(hThread, szName);
}

View File

@ -145,8 +145,11 @@ void ABORT(int iErrno = -1, LPCSTR lpszFile = nullptr, int iLine = 0, LPCSTR lps
/* 默认工作线程前缀 */
#define DEFAULT_WORKER_THREAD_PREFIX _T("HP-Worker-")
/* 默认线程池线程前缀 */
#define DEFAULT_POOL_THREAD_PREFIX _T("HP-Pool-")
BOOL SetDefaultWorkerThreadName(HANDLE hThread);
BOOL SetWorkerThreadName(HANDLE hThread, UINT uiSequence);
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

@ -25,7 +25,6 @@
#include "HPThreadPool.h"
#include "Common/WaitFor.h"
#include "Common/FuncHelper.h"
LPTSocketTask CreateSocketTaskObj( Fn_SocketTaskProc fnTaskProc,
PVOID pSender, CONNID dwConnID,

View File

@ -34,6 +34,7 @@
#pragma pop_macro("_ATL_NO_DEFAULT_LIBS")
#include "../Include/HPSocket/SocketInterface.h"
#include "Common/FuncHelper.h"
#include "Common/SysHelper.h"
#include "InternalDef.h"
@ -92,10 +93,20 @@ class CHPThreadPool : public IHPThreadPool
CHPThreadPool* m_pthPool;
};
typedef CThreadPool<CWorker> CInnerThreadPool;
friend class CWorker;
class CInnerThreadPool : public CThreadPool<CWorker>
{
protected:
DWORD ThreadProc() throw()
{
::SetDefaultPoolThreadName(SELF_THREAD);
return __super::ThreadProc();
}
};
private:
enum EnSubmitResult{SUBMIT_OK, SUBMIT_FULL, SUBMIT_ERROR};