Timers – three scenarios
Using timers can be difficult at times. There are these issues to be addressed:
- RtSleep (and also, it’s counterpart RtSleepEx and knRtSleep) starts counting down ticks only when it is called. If you need to execute some function at a fixed interval, you should subtract the function’s execution time from the sleep interval to get the desired interval. If this is not feasible, you can use an alarm which counts independent of the thread it wakes up.
- the RtSleep tick length of 10 milliseconds is too long: there are alternate APIs with RtSleepEx and knRtSleep that use system timer ticks. System timer ticks can be configured, default is a tick every 500 microseconds.
- the timers run at fixed intervals, so for example it is not possible to wait for a given time starting in the middle of a tick. If the system timer tick does not help by its higher resolution, the only solution is to add hardware that can be programmed to provide an interrupt after the required time.
Be aware that a thread calling RtSleep (and its counterparts) immediately will be transitioned from state “running” to “asleep” by the INtime kernel and will not wake up until the requested sleep period expired. If you intend to just wait for some IPC event to happen (e.g., a message to arrive at a mailbox) you should request the event and specify a timeout you are willing to wait.