13 #include "../GrayLibBase.h"
31 typedef HANDLE THREADHANDLE_t;
32 static const THREADHANDLE_t THREADHANDLE_NULL =
HANDLE_NULL;
33 #elif defined(__linux__)
34 typedef THREADID_t THREADHANDLE_t;
55 : m_hThread(THREADHANDLE_NULL)
81 THREAD_EXITCODE_t m_nExitCode;
93 return !isThreadStopping();
97 virtual THREAD_EXITCODE_t Run();
99 THREAD_EXITCODE_t RunDirectly();
102 static THREAD_EXITCODE_t _stdcall EntryProc(
void* pThisThread);
105 virtual void onThreadCreate();
106 virtual void onThreadExit(THREAD_EXITCODE_t nExitCode);
107 void onThreadTerminated(THREAD_EXITCODE_t nExitCode);
114 static bool GRAYCALL IsCurrentThreadStopping();
120 return this->m_hThread != THREADHANDLE_NULL;
126 return this->m_dwThreadId;
129 bool isCurrentThread()
const
136 virtual bool isValidCheck() const noexcept;
138 #if defined(_WIN32) && defined(_MSC_VER) && ! defined(UNDER_CE)
139 static void GRAYCALL SetThreadName(THREADID_t dwThreadID,
const char* pszThreadName);
140 bool put_ThreadName(
const char* pszThreadName);
146 return m_bThreadSleeping;
148 THREAD_EXITCODE_t get_ExitCodeThread()
const;
150 int get_ThreadPriority()
const;
151 bool put_ThreadPriority(
int nPriority);
153 bool SuspendThread();
156 virtual HRESULT SleepThread(
TIMESYSD_t dwWaitMillisec = 1,
bool bAlertable =
false);
157 virtual void WakeThread();
159 virtual bool RequestStopThread(
bool bWillWait =
false) noexcept override;
160 bool ExitCurrentThread(THREAD_EXITCODE_t nExitCode = THREAD_EXITCODE_OK);
165 bool TerminateThread(THREAD_EXITCODE_t iExitCode = THREAD_EXITCODE_ERR,
TIMESYSD_t nMilliSec = 0);
167 static
void GRAYCALL RequestStopThreadArray(
bool bWillWait,
cThread** ppThreads,
size_t nThreads);
168 static
void GRAYCALL TerminateThreadArray(THREAD_EXITCODE_t nExitCode,
TIMESYSD_t iTimeMSec,
cThread** ppThreads,
size_t nThreads);
170 void AttachToCurrentThread();
174 virtual
HRESULT CreateThread(DWORD dwCreationFlags = 0)
178 return CreateThread(EntryProc,
this, dwCreationFlags);
184 bool GetStatTimes(FILETIME* pKernelTime, FILETIME* pUserTime)
const
188 return ::GetThreadTimes(m_hThread,
nullptr,
nullptr, pKernelTime, pUserTime) ? true :
false;
216 UINT m_nCriticalWaitCount;
225 return SUPER_t::ThreadTick();
227 virtual void onThreadCreate()
override;
233 virtual bool CheckStuckThread(
TIMESYSD_t iMilliSecTilStuck,
bool bTerminateAndRestart);
237 return m_nThreadTicks;
242 return m_nCriticalWaitCount ? true :
false;
247 ASSERT(isCurrentThread());
248 m_nCriticalWaitCount += iDelta;
254 template <
class _TYPE>
264 static THREAD_EXITCODE_t _stdcall
EntryProc(
void* pThisThread)
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define ASSERT(exp)
Definition: cDebugAssert.h:87
#define HANDLE_NULL
Invalid OS handle for _WIN32. Not invalid OS handle for linux.
Definition: cOSHandle.h:21
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
virtual ~cThreadBase()
Definition: cThread.h:58
cThreadBase() noexcept
Definition: cThread.h:54
THREADHANDLE_t m_hThread
there may be many handles to the same THREADID_t in _WIN32. I must call CloseThread() on this.
Definition: cThread.h:51
Definition: cThread.h:198
cTimeSys m_tStart
When did thread first run ?
Definition: cThread.h:207
UINT m_nThreadTicksLast
checked periodically by outside observer.
Definition: cThread.h:206
UINT m_nThreadTicks
periodically increment this to make sure the thread is alive!
Definition: cThread.h:212
UINT m_nRecoveryStage
What have we done about being stuck ?
Definition: cThread.h:209
virtual bool ThreadTick() override
Definition: cThread.h:219
UINT get_ThreadTicks() const
Definition: cThread.h:235
void ChangeCriticalWait(int iDelta)
Definition: cThread.h:244
bool isCriticalWait() const
Definition: cThread.h:239
TIMESYS_t m_timeLastChanged
Last outside detected tick change time.
Definition: cThread.h:208
Definition: cThreadMgr.h:26
Definition: cThread.h:256
static THREAD_EXITCODE_t _stdcall EntryProc(void *pThisThread)
Definition: cThread.h:264
virtual HRESULT CreateThread(DWORD dwCreationFlags=0) override
Definition: cThread.h:270
bool isValidThreadHandle() const noexcept
Definition: cThread.h:116
STDMETHOD_(cString, get_Name)() const
override memory allocation and structure definitions are valid.
static THREAD_EXITCODE_t _stdcall EntryProc(void *pThisThread)
_WorkerThreadProc
Definition: cThread.cpp:693
bool m_bThreadSleeping
Idle worker threads waiting for something to do. inside Sleep() or WaitForSingleObject()
Definition: cThread.h:77
virtual bool ThreadTick()
Definition: cThread.h:85
HRESULT CreateThread(THREAD_FUNC_t pEntryProc, void *pArgs, DWORD dwCreationFlags=0)
Definition: cThread.cpp:167
THREADID_t get_HashCode() const noexcept
Definition: cThread.h:123
bool m_bThreadSuspended
May be resumed. inside Suspend() -> Resume()
Definition: cThread.h:78
bool isThreadSleeping() const noexcept
Definition: cThread.h:144
Definition: cRefPtr.h:225
Definition: cThreadLock.h:55
static THREADID_t GetCurrentId() noexcept
Definition: cThreadLock.h:97
static bool IsEqualId(THREADID_t a, THREADID_t b) noexcept
Definition: cThreadLock.h:113
static const THREADID_t k_NULL
Not a valid thread Id.
Definition: cThreadLock.h:64
Definition: cThreadLock.h:137
Definition: cTimeSys.h:93
cRefPtr< cThread > cThreadPtr
Definition: cThread.h:64
UNITTEST2_PREDEF(cQuadtree)
INT32 TIMESYSD_t
Time delta. signed milli-Seconds Span. cTimeSys::k_DMAX, cTimeSys::k_INF = MAILSLOT_WAIT_FOREVER.
Definition: cTimeSys.h:28
THREAD_EXITCODE_t(_stdcall * THREAD_FUNC_t)(void *)
Definition: cThreadLock.h:52
UINT32 TIMESYS_t
TIMESYS_t = The normal system tick timer. milli-seconds since start of system/app ?
Definition: cTimeSys.h:27