INOS
inos_1ms.h
Go to the documentation of this file.
1 //******************************************************************************
27 //******************************************************************************
28 
29 #if !defined( __INOS_1MS_H )
30 #define __INOS_1MS_H
31 
32 //--------------------------------------------------------------------------
33 //--- includes -----------------------------------------------------------------
34 //------------------------------------------------------------------------------
35 #include <functional>
36 
37 //------------------------------------------------------------------------------
38 //--- class definitions --------------------------------------------------------
39 //------------------------------------------------------------------------------
40 
41 // --- CINOSxmsHandler ---------------------------------------------------------
42 
43 class CINOS1ms;
44 
52 #include <cinosbushooks.h>
54 {
55  //--- user interface ---------------------------------------------------
56 
57  public:
58  // constructor
59  CINOSxmsHandlerBase(int32 aInterval, bool aActive=true, bool aSingle=false);
60  virtual ~CINOSxmsHandlerBase() {}
61  // activate handler
62  void Activate()
63  { Enable(); };
64  // deactivate handler
65  void Deactivate()
66  { Disable(); };
67  // set new interval and return old interval
68  long SetInterval(long aiInterval)
69  {
70  int32 iInterval = GetIntervalCmd();
71  SetIntervalCmd((uint32)aiInterval);
72  return iInterval;
73  };
75  long GetTimer() const
76  { return GetIntervalAct(); };
77 
78  //--- internals --------------------------------------------------------
79 
80  // private members
81  private :
82 
83 
84  // allow dynamic object handling (new/delete)
85  DECLARE_DYNAMIC(CINOSxmsHandlerBase);
86 };
87 
91 {
92  //--- user interface ---------------------------------------------------
93 
94  public:
95  // constructor
96  CINOSxmsHandler(void* aHandler, void* aObject, int32 aInterval, bool aActive=true, bool aSingle=false)
97  : CINOSxmsHandlerBase(aInterval, aActive, aSingle)
98  {
99  // init members
100  pHandler = (void(*)(void)) aHandler;
101  pObject = aObject;
102  }
103  virtual ~CINOSxmsHandler() {}
105  virtual void Call() override { INOSCallClassMember_0(pObject, (void *) pHandler); }
106 
107  //--- internals --------------------------------------------------------
108 
109  // private members
110  private :
111  void* pObject; // pointer to object (if member function)
112  void (*pHandler)(); // pointer to handler
113 
114  // allow dynamic object handling (new/delete)
115  DECLARE_DYNAMIC(CINOSxmsHandler);
116 };
117 
121 {
122  //--- user interface ---------------------------------------------------
123 
124  public:
125  typedef std::function<void()> tFunction;
126  // constructor
127  CINOSxmsHandlerFunction(tFunction aFunction, int32 aInterval, bool aActive=true, bool aSingle=false)
128  : CINOSxmsHandlerBase(aInterval, aActive, aSingle),
129  m_Function(aFunction)
130  {
131  }
132  virtual ~CINOSxmsHandlerFunction() {}
134  virtual void Call() override { m_Function(); }
135 
136  //--- internals --------------------------------------------------------
137 
138  // private members
139  private :
140  const tFunction m_Function;
141 
142  // allow dynamic object handling (new/delete)
143  DECLARE_DYNAMIC(CINOSxmsHandlerFunction);
144  DECLARE_DYNAMIC_PLACEMENT(CINOSxmsHandlerFunction);
145 };
146 
147 // --- CINOS1ms handler --------------------------------------------------------
148 
172 class CINOS1ms : public CINOSTask
173 {
174  //--- user interface ---------------------------------------------------
175 
176  public:
208  CINOSxmsHandler* AddHandler(void* aHandler, void* aObject, int32 aMS, bool aActive=true, bool aSingle=false);
222  uintptr_t& aTimeoutId, const CINOSxmsHandlerFunction::tFunction& aFunction, int32 aMS,
223  bool aActive=true, bool aSingle=false);
227  void AddHandler(CINOSxmsHandlerBase* aHandler);
238  void RemoveHandler(CINOSxmsHandlerBase* aHandler, bool aDelete = true);
239 
242  void eAddTimer(uint32 aTimerPtr);
244  void AddTimer(int32* aTimer);
246  int32 GetTickCount()
247  { return g_pTarget->GetTickCount();};
249  int32 GetTickDelta(int32 aiTickCount)
250  { return aiTickCount-g_pTarget->GetTickCount();};
252  int32* GetTickAddress()
253  { return g_pTarget->GetTickAddress();};
255  void SetTickAddress(int32* apAddress)
256  { m_pTickAddress = apAddress; *m_pTickAddress = 0; };
259  { return GetResumeTicks();};
260 
261  typedef uintptr_t tIntervalId;
276  template<typename TFct>
277  void SetInterval(TFct aFct, uint32 aIntervalMs, tIntervalId& aIntervalId) {
278  AddHandler(aIntervalId, aFct, aIntervalMs);
279  }
286  void ClearInterval(tIntervalId& aIntervalId) {
287  if (aIntervalId) {
288  auto p = reinterpret_cast<CINOSxmsHandlerBase*>(aIntervalId);
289  RemoveHandler(p, true);
290  aIntervalId = tIntervalId();
291  }
292  }
293 
294  typedef uintptr_t tTimeoutId;
309  template<typename TFct>
310  void SetTimeout(TFct aFct, uint32 aTimeoutMs, tTimeoutId& aTimeoutId) {
311  AddHandler(aTimeoutId, aFct, aTimeoutMs, true/*active*/, true/*single*/);
312  }
319  void ClearTimeout(tTimeoutId& aTimeoutId) {
320  if (aTimeoutId) {
321  auto p = reinterpret_cast<CINOSxmsHandlerBase*>(aTimeoutId);
322  RemoveHandler(p, true);
323  aTimeoutId = tTimeoutId();
324  }
325  }
326 
328  bool HasHandlers(uint32 auNumber)
329  { return m_Hooks.HasHooks(auNumber); }
330 
331  //--- internals --------------------------------------------------------
332 
333  // private members
334  private :
335  CINOSBusHooks m_Hooks;
336  /* list of timers */
337  TINOSDoubleLinkedList<int32>* p1msTimers;
338 
341  int32* m_pTickAddress = 0;
344  CINOSTask* m_pExactSource = nullptr;
346  uint32 m_uTicksFor1ms = 0;
354  enum {
355  eThresholdMultiplier = 10
356  };
357  uint32 m_uTicksThresholdToSwitchSelected = 0;
360  uint32 m_uCountOfReselection = 0;
363  volatile uint32 m_uMostRecentlyResumedTicks = 0;
366  uint32 m_uOverrun = 0;
370  uint32 m_uWakeUpCount = 0;
371 
373  CINOSxmsHandlerBase* m_pHandlerErr{};
374 
375  // constructor
376  public:
377  CINOS1ms();
378  ~CINOS1ms();
384  bool KickExactly(CINOSTask* apSource = 0) INOS_COMPILE_OPTIMIZED;
396  bool KickSporadically() INOS_COMPILE_OPTIMIZED;
397 
398  // protected members
399  protected:
400  // main task loop
401  virtual void Action() override;
402 
403  private:
406  void iIdleKicker();
407 
409  public:
413  static uint32 m_uCoreMask;
415  static CINOS1ms& Instance();
416  static CINOS1ms* pInstance();
417 };
418 
419 //--------------------------------------------------------------------------
420 // 1ms handler
421 //--------------------------------------------------------------------------
422 
425 #define pINOS1ms CINOS1ms::pInstance()
426 
428 #define g_pINOS1ms CINOS1ms::pInstance()
429 
430 #endif // __INOS_1MS_H
431 
CINOSMutex
Definition: cinosmutex.h:35
CINOS1ms::KickSporadically
bool KickSporadically() INOS_COMPILE_OPTIMIZED
cinosbushooks.h
The CINOSBusHook class.
CINOS1ms::AddHandler
CINOSxmsHandler * AddHandler(void *aHandler, void *aObject, int32 aMS, bool aActive=true, bool aSingle=false)
CINOS1ms::ClearTimeout
void ClearTimeout(tTimeoutId &aTimeoutId)
Definition: inos_1ms.h:319
CINOSBusHook::Enable
uint32 Enable()
Enable hook.
CINOS1ms::GetTickAddress
int32 * GetTickAddress()
Definition: inos_1ms.h:252
CINOS1ms::KickExactly
bool KickExactly(CINOSTask *apSource=0) INOS_COMPILE_OPTIMIZED
CINOSBusHookEx::SetIntervalCmd
void SetIntervalCmd(uint32 auInterval)
Set hook interval.
Definition: cinosbushooks.h:163
CINOSBusHooks
Definition: cinosbushooks.h:478
CINOS1ms::Action
virtual void Action() override
Task action loop, needs to be overwritten by the user. For more info, see section Creation.
CINOS1ms::ClearInterval
void ClearInterval(tIntervalId &aIntervalId)
Definition: inos_1ms.h:286
CINOSxmsHandlerBase::GetTimer
long GetTimer() const
Definition: inos_1ms.h:75
CINOS1ms::SetInterval
void SetInterval(TFct aFct, uint32 aIntervalMs, tIntervalId &aIntervalId)
Definition: inos_1ms.h:277
CINOSBusHookEx::GetIntervalAct
uint32 GetIntervalAct() const
Get actual hook interval counter.
Definition: cinosbushooks.h:180
CINOSBusHookEx
Definition: cinosbushooks.h:144
CINOSBusHookEx::GetIntervalCmd
uint32 GetIntervalCmd()
Get commanded hook interval.
Definition: cinosbushooks.h:173
CINOSxmsHandlerBase
Definition: inos_1ms.h:53
CINOS1ms::HasHandlers
bool HasHandlers(uint32 auNumber)
Definition: inos_1ms.h:328
CINOS1ms::GetTickCount
int32 GetTickCount()
Definition: inos_1ms.h:246
CINOS1ms::m_uCoreMask
static uint32 m_uCoreMask
core mask
Definition: inos_1ms.h:413
CINOS1ms::RemoveHandler
void RemoveHandler(CINOSxmsHandlerBase *aHandler, bool aDelete=true)
CINOSBusHook::Disable
uint32 Disable()
Disable hook.
CINOS1ms::eAddTimer
void eAddTimer(uint32 aTimerPtr)
CINOS1ms
Definition: inos_1ms.h:172
CINOS1ms::GetSystemTicksAtLast1msTick
uint32 GetSystemTicksAtLast1msTick()
Definition: inos_1ms.h:258
CINOS1ms::SetTickAddress
void SetTickAddress(int32 *apAddress)
Definition: inos_1ms.h:255
CINOSxmsHandler::Call
virtual void Call() override
Definition: inos_1ms.h:105
CINOSxmsHandlerFunction
Definition: inos_1ms.h:120
CINOSxmsHandlerFunction::Call
virtual void Call() override
Definition: inos_1ms.h:134
CINOSxmsHandler
Definition: inos_1ms.h:90
CINOS1ms::GetTickDelta
int32 GetTickDelta(int32 aiTickCount)
Definition: inos_1ms.h:249
CINOS1ms::Instance
static CINOS1ms & Instance()
singleton
CINOS1ms::AddTimer
void AddTimer(int32 *aTimer)
CINOS1ms::m_Mutex
static CINOSMutex m_Mutex
public
Definition: inos_1ms.h:411
CINOS1ms::SetTimeout
void SetTimeout(TFct aFct, uint32 aTimeoutMs, tTimeoutId &aTimeoutId)
Definition: inos_1ms.h:310
CINOSBusHooks::HasHooks
bool HasHooks(uint32 auNumber)
Check whether the given number of hooks are still available.
Definition: cinosbushooks.h:887
CINOSTask
Definition: cinostask.h:51