105 static constexpr size_t SIZE_OF_COMBINDED_INDEX_AND_DESCRIPTOR =
sizeof(uint32_t);
106 static constexpr size_t SIZE_OF_SIGVAL_INT =
sizeof(int);
107 static_assert(SIZE_OF_SIGVAL_INT >= SIZE_OF_COMBINDED_INDEX_AND_DESCRIPTOR,
"size of sigval_int is to low");
109 static constexpr uint32_t MAX_NUMBER_OF_CALLBACK_HANDLES = 100u;
110 static_assert(MAX_NUMBER_OF_CALLBACK_HANDLES <= std::numeric_limits<uint8_t>::max(),
111 "number of callback handles exceeds max index value");
113 struct OsTimerCallbackHandle
115 static constexpr uint32_t MAX_DESCRIPTOR_VALUE{(1u << 24u) - 1u};
116 static sigval indexAndDescriptorToSigval(uint8_t index, uint32_t descriptor)
noexcept;
117 static uint8_t sigvalToIndex(sigval intVal)
noexcept;
118 static uint32_t sigvalToDescriptor(sigval intVal)
noexcept;
120 void incrementDescriptor() noexcept;
122 std::mutex m_accessMutex;
126 std::atomic<uint32_t> m_descriptor{0u};
128 std::atomic_flag m_callbackIsAboutToBeExecuted = ATOMIC_FLAG_INIT;
130 std::atomic<bool> m_inUse{
false};
131 std::atomic<bool> m_isTimerActive{
false};
132 std::atomic<uint64_t> m_timerInvocationCounter{0u};
134 OsTimer* m_timer{
nullptr};
141 static constexpr timer_t INVALID_TIMER_ID = 0;
143 static constexpr timer_t INVALID_TIMER_ID =
nullptr;
147 static void callbackHelper(sigval data)
noexcept;
149 OsTimer(
const units::Duration timeToWait,
const std::function<
void()>& callback)
noexcept;
151 OsTimer(
const OsTimer&) =
delete;
152 OsTimer(OsTimer&&) =
delete;
153 OsTimer& operator=(
const OsTimer&) =
delete;
154 OsTimer& operator=(OsTimer&&) =
delete;
157 virtual ~OsTimer() noexcept;
167 cxx::expected<TimerError> start(const RunMode runMode, const
CatchUpPolicy catchUpPolicy) noexcept;
172 cxx::expected<TimerError> stop() noexcept;
179 cxx::expected<TimerError>
180 restart(const units::Duration timeToWait, const RunMode runMode, const
CatchUpPolicy catchUpPolicy) noexcept;
184 cxx::expected<units::Duration, TimerError> timeUntilExpiration() noexcept;
189 cxx::expected<uint64_t, TimerError> getOverruns() noexcept;
192 bool hasError() const noexcept;
195 TimerError getError() const noexcept;
200 void executeCallback() noexcept;
204 units::Duration m_timeToWait;
207 std::function<
void()> m_callback;
210 timer_t m_timerId{INVALID_TIMER_ID};
212 uint8_t m_callbackHandleIndex{0u};
216 bool m_isInitialized{
false};
220 TimerError m_errorValue{TimerError::NO_ERROR};
222 static OsTimerCallbackHandle s_callbackHandlePool[MAX_NUMBER_OF_CALLBACK_HANDLES];
235 Timer(
const units::Duration timeToWait)
noexcept;
245 Timer(
const units::Duration timeToWait,
const std::function<
void()>& callback)
noexcept;
250 static cxx::expected<units::Duration, TimerError>
now() noexcept;
265 virtual ~
Timer() noexcept = default;
279 cxx::expected<TimerError>
stop() noexcept;
286 cxx::expected<TimerError>
305 cxx::optional<OsTimer> m_osTimer;
308 static cxx::error<TimerError> createErrorFromErrno(const int32_t errnum) noexcept;
311 units::Duration m_timeToWait;
314 units::Duration m_creationTime;
317 TimerError m_errorValue{TimerError::NO_ERROR};