kernel/timeout: Add absolute timeout APIs

Add support for "absolute" timeouts, which are expressed relative to
system uptime instead of deltas from current time.  These allow for
more race-resistant code to be written by allowing application code to
do a single timeout computation, once, and then reuse the timeout
value even if the thread wakes up and needs to suspend again later.

Signed-off-by: Andy Ross <andrew.j.ross@intel.com>
This commit is contained in:
Andy Ross 2020-03-09 09:35:35 -07:00 committed by Anas Nashif
commit 4c7b77a716
6 changed files with 106 additions and 8 deletions

View file

@ -1606,6 +1606,32 @@ const char *k_thread_state_str(k_tid_t thread_id);
*/
#define K_FOREVER Z_FOREVER
/**
* @brief Generates an absolute/uptime timeout value in ticks
*
* This macro generates a timeout delay that represents an expiration
* at the absolute uptime value specified, in ticks. That is, the
* timeout will expire immediately after the system uptime reaches the
* specified tick count.
*
* @param t Tick uptime value
* @return Timeout delay value
*/
#define K_TIMEOUT_ABS_TICKS(t) Z_TIMEOUT_TICKS(Z_TICK_ABS(MAX(t, 0)))
/**
* @brief Generates an absolute/uptime timeout value in ms
*
* This macro generates a timeout delay that represents an expiration
* at the absolute uptime value specified, in milliseconds. That is,
* the timeout will expire immediately after the system uptime reaches
* the specified tick count.
*
* @param t Millisecond uptime value
* @return Timeout delay value
*/
#define K_TIMEOUT_ABS_MS(t) K_TIMEOUT_ABS_TICKS(k_ms_to_ticks_ceil64(t))
/**
* @}
*/

View file

@ -77,6 +77,16 @@ typedef struct {
#define Z_TIMEOUT_NS(t) Z_TIMEOUT_TICKS(k_ns_to_ticks_ceil32(MAX(t, 0)))
#define Z_TIMEOUT_CYC(t) Z_TIMEOUT_TICKS(k_cyc_to_ticks_ceil32(MAX(t, 0)))
/* Converts between absolute timeout expiration values (packed into
* the negative space below K_TICKS_FOREVER) and (non-negative) delta
* timeout values. If the result of Z_TICK_ABS(t) is >= 0, then the
* value was an absolute timeout with the returend expiration time.
* Note that this macro is bidirectional: Z_TICK_ABS(Z_TICK_ABS(t)) ==
* t for all inputs, and that the representation of K_TICKS_FOREVER is
* the same value in both spaces! Clever, huh?
*/
#define Z_TICK_ABS(t) (K_TICKS_FOREVER - 1 - (t))
#else
/* Legacy timeout API */