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
43class 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)
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)
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)
145};
146
147// --- CINOS1ms handler --------------------------------------------------------
148
172class 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);
238 void RemoveHandler(CINOSxmsHandlerBase* aHandler, bool aDelete = true);
239
242 void eAddTimer(uint32 aTimerPtr);
244 void AddTimer(int32* aTimer);
247 { return g_pTarget->GetTickCount();};
249 int32 GetTickDelta(int32 aiTickCount)
250 { return aiTickCount-g_pTarget->GetTickCount();};
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;
416 static CINOS1ms* pInstance();
417};
418
419//--------------------------------------------------------------------------
420// 1ms handler
421//--------------------------------------------------------------------------
422
425#define pINOS1ms CINOS1ms::pInstance()
428#define g_pINOS1ms CINOS1ms::pInstance()
429
430#endif // __INOS_1MS_H
431
The CINOSBusHook class.
#define DECLARE_DYNAMIC_PLACEMENT(aClass)
Definition cinospartitionmemory.h:348
#define DECLARE_DYNAMIC(aClass)
Definition cinospartitionmemory.h:328
Definition inos_1ms.h:173
virtual void Action() override
Task action loop, needs to be overwritten by the user. For more info, see section Creation.
void RemoveHandler(CINOSxmsHandlerBase *aHandler, bool aDelete=true)
bool KickExactly(CINOSTask *apSource=0) INOS_COMPILE_OPTIMIZED
uint32 GetSystemTicksAtLast1msTick()
Definition inos_1ms.h:258
void AddHandler(CINOSxmsHandlerBase *aHandler)
int32 GetTickCount()
Definition inos_1ms.h:246
bool KickSporadically() INOS_COMPILE_OPTIMIZED
void ClearInterval(tIntervalId &aIntervalId)
Definition inos_1ms.h:286
void SetTimeout(TFct aFct, uint32 aTimeoutMs, tTimeoutId &aTimeoutId)
Definition inos_1ms.h:310
void SetInterval(TFct aFct, uint32 aIntervalMs, tIntervalId &aIntervalId)
Definition inos_1ms.h:277
static CINOSMutex m_Mutex
public
Definition inos_1ms.h:411
int32 GetTickDelta(int32 aiTickCount)
Definition inos_1ms.h:249
static CINOS1ms & Instance()
singleton
static uint32 m_uCoreMask
core mask
Definition inos_1ms.h:413
CINOSxmsHandler * AddHandler(void *aHandler, void *aObject, int32 aMS, bool aActive=true, bool aSingle=false)
void eAddTimer(uint32 aTimerPtr)
bool HasHandlers(uint32 auNumber)
Definition inos_1ms.h:328
CINOSxmsHandlerFunction * AddHandler(uintptr_t &aTimeoutId, const CINOSxmsHandlerFunction::tFunction &aFunction, int32 aMS, bool aActive=true, bool aSingle=false)
void AddTimer(int32 *aTimer)
int32 * GetTickAddress()
Definition inos_1ms.h:252
void SetTickAddress(int32 *apAddress)
Definition inos_1ms.h:255
void ClearTimeout(tTimeoutId &aTimeoutId)
Definition inos_1ms.h:319
Definition cinosbushooks.h:145
void SetIntervalCmd(uint32 auInterval)
Set hook interval.
Definition cinosbushooks.h:163
uint32 GetIntervalAct() const
Get actual hook interval counter.
Definition cinosbushooks.h:180
uint32 GetIntervalCmd()
Get commanded hook interval.
Definition cinosbushooks.h:173
uint32 Disable()
Disable hook.
uint32 Enable()
Enable hook.
Definition cinosbushooks.h:479
bool HasHooks(uint32 auNumber)
Check whether the given number of hooks are still available.
Definition cinosbushooks.h:887
Definition cinosmutex.h:36
Definition cinostask.h:52
Definition inos_1ms.h:54
long GetTimer() const
Definition inos_1ms.h:75
Definition inos_1ms.h:121
virtual void Call() override
Definition inos_1ms.h:134
Definition inos_1ms.h:91
virtual void Call() override
Definition inos_1ms.h:105