Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cThreadFiber.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cThreadFiber_H
7 #define _INC_cThreadFiber_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cThread.h"
13 #include "cSysEvent.h"
15 
16 namespace GrayLib
17 {
19 
20  class GRAYLIB_LINK cThreadFiber : public cThreadNamed<cThreadFiber>
21  {
30 
32 
33  private:
34  TIMESYS_t m_timeRun;
35  TIMESYS_t m_timeNext;
36 
37  cSysEvent m_CallerReady;
38  cSysEvent m_FiberReady;
39 
40  protected:
41  static HRESULT GRAYCALL SignalObjectAndWaitX(cSysEvent& Ready1, cSysEvent& Wait1, TIMESYSD_t mSecTimeout);
42 
43  void ClearTimeRun() noexcept
44  {
45  m_timeRun = cTimeSys::k_CLEAR; // Don't want anymore time after this.
46  }
47  HRESULT YieldFiber(TIMESYSD_t dwTimeWait = cTimeSys::k_INF);
48  virtual void WakeThread() override;
49 
50  public:
51  cThreadFiber() noexcept;
52  virtual ~cThreadFiber();
53 
54  STDMETHOD_(cString, get_Name)() const override // What task is this ?
55  {
56  return "WorkerFiber";
57  }
58 
59  static cThreadFiber* GRAYCALL GetCurrentFiber();
60 
61  bool IsFiberRunning() const noexcept
62  {
63  return isThreadRunning() && !m_bThreadSleeping && !m_bThreadSuspended ;
64  }
65 
66  // Called by parent.
67  // Run this thread fiber and suspend caller.
68  HRESULT RunFiber(TIMESYSD_t dwTimeMin = 0, TIMESYSD_t dwTimeWait = 0, TIMESYSD_t dwTimeHardMax = cTimeSys::k_INF);
69 
70  // Called inside fiber ONLY
71  virtual void onThreadExit(THREAD_EXITCODE_t nExitCode) override;
72 
73  // Called by any
74  // Wait to be rescheduled. Same as Tick but with a long suspend. Called by fiber.
75  virtual HRESULT SleepThread(TIMESYSD_t dwWaitMillisec, bool bAlertable = false) override;
76 
77  // Give up my time slice and resume my caller. Called by fiber mostly.
78  virtual bool ThreadTick() override;
79 
81  };
82 };
83 #endif
#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
Definition: cSysEvent.h:28
Definition: cThreadFiber.h:21
void ClearTimeRun() noexcept
Definition: cThreadFiber.h:43
bool IsFiberRunning() const noexcept
Definition: cThreadFiber.h:61
UNITTEST2_PREDEF(cThreadFiber)
Definition: cThread.h:256
Definition: cThread.h:68
static const TIMESYS_t k_CLEAR
Definition: cTimeSys.h:99
static const TIMESYS_t k_INF
INFINITE in _WIN32. MAILSLOT_WAIT_FOREVER.
Definition: cTimeSys.h:101
Definition: cMesh.h:22
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
UINT32 TIMESYS_t
TIMESYS_t = The normal system tick timer. milli-seconds since start of system/app ?
Definition: cTimeSys.h:27