Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
GrayLib::cThreadFiber Class Reference

#include <cThreadFiber.h>

Inheritance diagram for GrayLib::cThreadFiber:
GrayLib::cThreadNamed< cThreadFiber > GrayLib::cThreadChecked GrayLib::cThread GrayLib::cThreadBase Gray::cRefBase Gray::cThreadState Gray::CObject Gray::cThreadId IUnknown GrayLib::cJobFiber

Public Member Functions

 cThreadFiber () noexcept
 
virtual ~cThreadFiber ()
 
 STDMETHOD_ (cString, get_Name)() const override
 
bool IsFiberRunning () const noexcept
 
HRESULT RunFiber (TIMESYSD_t dwTimeMin=0, TIMESYSD_t dwTimeWait=0, TIMESYSD_t dwTimeHardMax=cTimeSys::k_INF)
 
virtual void onThreadExit (THREAD_EXITCODE_t nExitCode) override
 OnExit. More...
 
virtual HRESULT SleepThread (TIMESYSD_t dwWaitMillisec, bool bAlertable=false) override
 
virtual bool ThreadTick () override
 
 UNITTEST2_PREDEF (cThreadFiber)
 
- Public Member Functions inherited from GrayLib::cThreadNamed< cThreadFiber >
virtual HRESULT CreateThread (DWORD dwCreationFlags=0) override
 
- Public Member Functions inherited from GrayLib::cThreadChecked
 cThreadChecked ()
 
virtual ~cThreadChecked ()
 
virtual bool CheckStuckThread (TIMESYSD_t iMilliSecTilStuck, bool bTerminateAndRestart)
 called by external thread. More...
 
UINT get_ThreadTicks () const
 
bool isCriticalWait () const
 
void ChangeCriticalWait (int iDelta)
 
- Public Member Functions inherited from GrayLib::cThread
 cThread ()
 
virtual ~cThread ()
 
bool isValidThreadHandle () const noexcept
 
THREADID_t get_HashCode () const noexcept
 
virtual bool isValidCheck () const noexcept
 < memory allocation and structure definitions are valid. More...
 
 STDMETHOD_ (cString, get_Name)() const
 override memory allocation and structure definitions are valid. More...
 
bool isThreadSleeping () const noexcept
 
THREAD_EXITCODE_t get_ExitCodeThread () const
 
int get_ThreadPriority () const
 
bool put_ThreadPriority (int nPriority)
 
bool SuspendThread ()
 
bool ResumeThread ()
 
virtual bool RequestStopThread (bool bWillWait=false) noexcept override
 
bool ExitCurrentThread (THREAD_EXITCODE_t nExitCode=THREAD_EXITCODE_OK)
 
bool WaitForThreadExit (TIMESYSD_t nMilliSec=cTimeSys::k_FREQ)
 
THREAD_EXITCODE_t WaitForThreadExitCode (TIMESYSD_t iTimeMSec=cTimeSys::k_FREQ)
 
bool TerminateThread (THREAD_EXITCODE_t iExitCode=THREAD_EXITCODE_ERR, TIMESYSD_t nMilliSec=0)
 
void AttachToCurrentThread ()
 
HRESULT CreateThread (THREAD_FUNC_t pEntryProc, void *pArgs, DWORD dwCreationFlags=0)
 
void CloseThread ()
 
- Public Member Functions inherited from GrayLib::cThreadBase
 cThreadBase () noexcept
 
virtual ~cThreadBase ()
 
- Public Member Functions inherited from Gray::CObject
virtual ~CObject ()
 
virtual void AssertValid () const
 < memory allocation and structure definitions are valid. More...
 
virtual void Serialize (cArchive &a)
 
- Public Member Functions inherited from Gray::cThreadId
 cThreadId (THREADID_t nThreadId=k_NULL) noexcept
 
THREADID_t GetThreadId () const noexcept
 
THREADID_t get_HashCode () const noexcept
 
bool isCurrentThread () const noexcept
 
bool isValidId () const noexcept
 
void InitCurrentId () noexcept
 
- Public Member Functions inherited from Gray::cRefBase
 cRefBase (int iRefCount=0) noexcept
 
virtual ~cRefBase ()
 
int get_RefCount () const noexcept
 
HASHCODE_t get_HashCode () const noexcept
 
 STDMETHOD_ (HASHCODE_t, get_HashCodeX)() const noexcept
 
virtual void onFinalRelease ()
 
bool isValidObj () const noexcept
 
 STDMETHOD_ (ULONG, AddRef)(void) override
 
 STDMETHOD_ (ULONG, Release)(void) override
 
STDMETHOD() QueryInterface (const IID &riid, void __RPC_FAR *__RPC_FAR *ppvObject) override
 
void IncRefCount ()
 
void DecRefCount ()
 
bool isStaticConstruct () const noexcept
 
void StaticConstruct ()
 
void StaticDestruct ()
 
bool isDestructing () noexcept
 
void SetDestructing ()
 
- Public Member Functions inherited from Gray::cThreadState
 cThreadState () noexcept
 
bool isThreadRunning () const noexcept
 
bool isThreadStopping () const noexcept
 

Static Public Member Functions

static cThreadFiber *GRAYCALL GetCurrentFiber ()
 
- Static Public Member Functions inherited from GrayLib::cThreadChecked
static cThreadChecked *GRAYCALL GetCurrentThreadChecked ()
 
- Static Public Member Functions inherited from GrayLib::cThread
static cThread *GRAYCALL GetCurrentThread ()
 
static bool GRAYCALL IsCurrentThreadStopping ()
 
static void GRAYCALL RequestStopThreadArray (bool bWillWait, cThread **ppThreads, size_t nThreads)
 
static void GRAYCALL TerminateThreadArray (THREAD_EXITCODE_t nExitCode, TIMESYSD_t iTimeMSec, cThread **ppThreads, size_t nThreads)
 
- Static Public Member Functions inherited from Gray::cThreadId
static THREADID_t GetCurrentId () noexcept
 
static bool IsValidId (THREADID_t id) noexcept
 
static bool IsEqualId (THREADID_t a, THREADID_t b) noexcept
 
static void SleepCurrent (TIMESYS_t uMs=cTimeSys::k_FREQ) noexcept
 

Protected Member Functions

void ClearTimeRun () noexcept
 
HRESULT YieldFiber (TIMESYSD_t dwTimeWait=cTimeSys::k_INF)
 force fiber to sleep. called inside ThreadTick() More...
 
virtual void WakeThread () override
 
- Protected Member Functions inherited from GrayLib::cThreadChecked
virtual void onThreadCreate () override
 Notification that the thread has started or stopped. More...
 
- Protected Member Functions inherited from GrayLib::cThread
virtual THREAD_EXITCODE_t Run ()
 WorkerThreadProc. More...
 
THREAD_EXITCODE_t RunDirectly ()
 
void onThreadTerminated (THREAD_EXITCODE_t nExitCode)
 

Static Protected Member Functions

static HRESULT GRAYCALL SignalObjectAndWaitX (cSysEvent &Ready1, cSysEvent &Wait1, TIMESYSD_t mSecTimeout)
 
- Static Protected Member Functions inherited from GrayLib::cThreadNamed< cThreadFiber >
static THREAD_EXITCODE_t _stdcall EntryProc (void *pThisThread)
 
- Static Protected Member Functions inherited from GrayLib::cThread
static THREAD_EXITCODE_t _stdcall EntryProc (void *pThisThread)
 _WorkerThreadProc More...
 

Additional Inherited Members

- Public Attributes inherited from GrayLib::cThreadChecked
UINT m_nThreadTicksLast
 checked periodically by outside observer. More...
 
cTimeSys m_tStart
 When did thread first run ? More...
 
TIMESYS_t m_timeLastChanged
 Last outside detected tick change time. More...
 
UINT m_nRecoveryStage
 What have we done about being stuck ? More...
 
- Static Public Attributes inherited from Gray::cThreadId
static const THREADID_t k_NULL = 0
 Not a valid thread Id. More...
 
- Protected Attributes inherited from GrayLib::cThreadChecked
UINT m_nThreadTicks
 periodically increment this to make sure the thread is alive! More...
 
- Protected Attributes inherited from GrayLib::cThread
bool m_bThreadSleeping
 Idle worker threads waiting for something to do. inside Sleep() or WaitForSingleObject() More...
 
bool m_bThreadSuspended
 May be resumed. inside Suspend() -> Resume() More...
 
- Protected Attributes inherited from GrayLib::cThreadBase
THREADHANDLE_t m_hThread
 there may be many handles to the same THREADID_t in _WIN32. I must call CloseThread() on this. More...
 
- Protected Attributes inherited from Gray::cThreadId
THREADID_t m_dwThreadId
 unique thread id. i.e. stack base pointer. (Use the MFC name) More...
 
- Protected Attributes inherited from Gray::cThreadState
bool m_bThreadRunning
 called CreateThread() onThreadCreate(), and inside Run(), until onThreadExit() More...
 
volatile bool m_bThreadStopping
 trying to stop the thread nicely. Do this before TerminateThread() More...
 

Detailed Description

A manually scheduled thread. (Not same as an _WIN32 'Fiber' or cFiber) a thread that suspends the thread that schedules it. then resumes it when the TickCheck() or Wait() call is called. It uses cThreadLocalType normally as a thread.

Note
There can be multiple concurrent normal threads each with its own cThreadFiber active.
Cant this can get pooled for cheap re-use ?
This can be used in NON _MT compiled programs !! since it is never concurrent.

Constructor & Destructor Documentation

◆ cThreadFiber()

GrayLib::cThreadFiber::cThreadFiber ( )
noexcept

◆ ~cThreadFiber()

GrayLib::cThreadFiber::~cThreadFiber ( )
virtual

Member Function Documentation

◆ ClearTimeRun()

void GrayLib::cThreadFiber::ClearTimeRun ( )
inlineprotectednoexcept

◆ GetCurrentFiber()

cThreadFiber *GRAYCALL GrayLib::cThreadFiber::GetCurrentFiber ( )
static
Note
Use DYNPTR_CAST in case the GetCurrentThread isn't really a cThreadFiber

◆ IsFiberRunning()

bool GrayLib::cThreadFiber::IsFiberRunning ( ) const
inlinenoexcept

◆ onThreadExit()

void GrayLib::cThreadFiber::onThreadExit ( THREAD_EXITCODE_t  nExitCode)
overridevirtual

OnExit.

Fiber is exiting, clean up properly. nExitCode = THREAD_EXITCODE_OK

still can still use m_hThread to get the threads return value!

Note
m_hThread is still valid.

Reimplemented from GrayLib::cThread.

Reimplemented in GrayLib::cJobFiber.

◆ RunFiber()

HRESULT GrayLib::cThreadFiber::RunFiber ( TIMESYSD_t  dwTimeMin = 0,
TIMESYSD_t  dwTimeWait = 0,
TIMESYSD_t  dwTimeHardMax = cTimeSys::k_INF 
)
  • dwTimeMin = the amount of time i want to give to this fiber. dwTimeWait = the amount of time i DONT want to give to this fiber. (allow foreground some time) dwTimeHardMax = fail the fiber after this long.
    Note
    Called ONLY by parent thread. NEVER by self.
    Returns
    S_FALSE = was not schedule now.

◆ SignalObjectAndWaitX()

HRESULT GRAYCALL GrayLib::cThreadFiber::SignalObjectAndWaitX ( cSysEvent Ready1,
cSysEvent Wait1,
TIMESYSD_t  mSecTimeout 
)
staticprotected

proper SignalObjectAndWait is not supported for win9x or linux (it just fails) Basically a thread 'swap', one thread stops and the other starts.

Returns
HRESULT_WIN32_C(ERROR_WAIT_TIMEOUT) = time expired. do both threads run??? S_OK = the other side gave it back.

◆ SleepThread()

HRESULT GrayLib::cThreadFiber::SleepThread ( TIMESYSD_t  dwWaitMillisec,
bool  bAlertable = false 
)
overridevirtual

The fiber wants to force itself back to sleep for X mSec. Equiv to Sleep() more or less but special for fibers. Re-schedule the fiber to run again when it would wake up.

Reimplemented from GrayLib::cThread.

◆ STDMETHOD_()

GrayLib::cThreadFiber::STDMETHOD_ ( cString  ,
get_Name   
) const
inlineoverride

◆ ThreadTick()

bool GrayLib::cThreadFiber::ThreadTick ( )
overridevirtual

This fiber may or may not be running. Give up my time slice. back to the thread that scheduled me.

Note
Called by fiber thread or any outside thread.
Returns
true = thread should keep running. false = thread should exit. Fiber/Thread is dead.

Reimplemented from GrayLib::cThreadChecked.

Reimplemented in GrayLib::cJobFiber.

◆ UNITTEST2_PREDEF()

GrayLib::cThreadFiber::UNITTEST2_PREDEF ( cThreadFiber  )

◆ WakeThread()

void GrayLib::cThreadFiber::WakeThread ( )
overrideprotectedvirtual

release YieldFiber() if m_bThreadSleeping Usually just to wake up to stop the thread nicely.

Reimplemented from GrayLib::cThread.

◆ YieldFiber()

HRESULT GrayLib::cThreadFiber::YieldFiber ( TIMESYSD_t  dwTimeWait = cTimeSys::k_INF)
protected

force fiber to sleep. called inside ThreadTick()

Allow the fiber to give some time back.

Note
This can get signaled to exit prematurely.
Returns
S_OK = 0 = signaled normally.
Note
Called by fiber thread ONLY internally.

The documentation for this class was generated from the following files: