LCOV - code coverage report
Current view: top level - /home/ubuntu/zephyrproject/zephyr/kernel/include - timeout_q.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 13 13
Test Date: 2026-11-06 14:43:19 Functions: 100.0 % 5 5
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2015 Wind River Systems, Inc.
       3                 :             :  *
       4                 :             :  * SPDX-License-Identifier: Apache-2.0
       5                 :             :  */
       6                 :             : 
       7                 :             : #ifndef ZEPHYR_KERNEL_INCLUDE_TIMEOUT_Q_H_
       8                 :             : #define ZEPHYR_KERNEL_INCLUDE_TIMEOUT_Q_H_
       9                 :             : 
      10                 :             : /**
      11                 :             :  * @file
      12                 :             :  * @brief timeout queue for threads on kernel objects
      13                 :             :  */
      14                 :             : 
      15                 :             : #include <zephyr/kernel.h>
      16                 :             : 
      17                 :             : #include <stdbool.h>
      18                 :             : 
      19                 :             : #ifdef __cplusplus
      20                 :             : extern "C" {
      21                 :             : #endif
      22                 :             : 
      23                 :             : #ifdef CONFIG_SYS_CLOCK_EXISTS
      24                 :             : 
      25                 :             : /*
      26                 :             :  * Per-node timeout helpers (Kconfig choice TIMEOUT_BACKEND).
      27                 :             :  *
      28                 :             :  * z_init_timeout() and z_is_inactive_timeout() operate on a single
      29                 :             :  * struct _timeout and are needed tree-wide (timer.c, work.c, poll.c, ...),
      30                 :             :  * so they live here and depend only on the chosen backend's struct _timeout
      31                 :             :  * fields (see kernel_structs.h). The queue itself (the z_timeout_q_*()
      32                 :             :  * operations and the backend instance) is private to kernel/timeout.c, which
      33                 :             :  * includes the matching backend implementation header directly.
      34                 :             :  */
      35                 :             : #if defined(CONFIG_TIMEOUT_BACKEND_MINHEAP)
      36                 :             : 
      37                 :             : static inline void z_init_timeout(struct _timeout *to)
      38                 :             : {
      39                 :             :         to->heap_handle.idx = 0U;
      40                 :             :         to->abs_ticks = 0;
      41                 :             : }
      42                 :             : 
      43                 :             : static inline bool z_is_inactive_timeout(const struct _timeout *to)
      44                 :             : {
      45                 :             :         return to->heap_handle.idx == 0U;
      46                 :             : }
      47                 :             : 
      48                 :             : #elif defined(CONFIG_TIMEOUT_BACKEND_WHEEL)
      49                 :             : 
      50                 :             : static inline void z_init_timeout(struct _timeout *to)
      51                 :             : {
      52                 :             :         sys_dnode_init(&to->node);
      53                 :             :         to->flags = 0U;
      54                 :             :         to->dticks = 0;
      55                 :             : }
      56                 :             : 
      57                 :             : static inline bool z_is_inactive_timeout(const struct _timeout *to)
      58                 :             : {
      59                 :             :         return !sys_dnode_is_linked(&to->node);
      60                 :             : }
      61                 :             : 
      62                 :             : #elif defined(CONFIG_TIMEOUT_BACKEND_SKIPLIST)
      63                 :             : 
      64                 :             : static inline void z_init_timeout(struct _timeout *to)
      65                 :             : {
      66                 :             :         uint8_t i;
      67                 :             : 
      68                 :             :         to->height = 0;
      69                 :             :         to->abs_ticks = 0;
      70                 :             : 
      71                 :             :         for (i = 0; i < CONFIG_TIMEOUT_SKIPLIST_MAX_LEVEL; i++) {
      72                 :             :                 to->forward[i] = NULL;
      73                 :             :         }
      74                 :             : }
      75                 :             : 
      76                 :             : static inline bool z_is_inactive_timeout(const struct _timeout *to)
      77                 :             : {
      78                 :             :         return to->height == 0;
      79                 :             : }
      80                 :             : 
      81                 :             : #else /* CONFIG_TIMEOUT_BACKEND_DLIST or _BUCKET */
      82                 :             : 
      83                 :          56 : static inline void z_init_timeout(struct _timeout *to)
      84                 :             : {
      85                 :          56 :         sys_dnode_init(&to->node);
      86                 :          56 :         to->dticks = 0;
      87                 :          56 : }
      88                 :             : 
      89                 :         302 : static inline bool z_is_inactive_timeout(const struct _timeout *to)
      90                 :             : {
      91                 :         302 :         return !sys_dnode_is_linked(&to->node);
      92                 :             : }
      93                 :             : 
      94                 :             : #endif
      95                 :             : 
      96                 :             : /* Adds the timeout to the queue.
      97                 :             :  *
      98                 :             :  * @return Absolute tick value when timeout will expire.
      99                 :             :  */
     100                 :             : k_ticks_t z_add_timeout(struct _timeout *to, _timeout_func_t fn, k_timeout_t timeout);
     101                 :             : 
     102                 :             : /* Attempt to abort a timeout. Safe to use for callers that need to be
     103                 :             :  * certain the handler is not in flight (e.g. when about to free the
     104                 :             :  * timeout's storage), as long as the caller is prepared to drop any
     105                 :             :  * locks the handler may need and retry.
     106                 :             :  *
     107                 :             :  * Returns:
     108                 :             :  *   0       — the timeout was active and has been removed from the queue.
     109                 :             :  *   -EINVAL — the timeout was not active (never linked, already done, or
     110                 :             :  *             a same-CPU IRQ aborted while the handler was paused on this
     111                 :             :  *             CPU; in that case the in-flight slot is marked superseded
     112                 :             :  *             so handlers that need to bail may check via
     113                 :             :  *             z_timeout_inflight_superseded()).
     114                 :             :  *   -EAGAIN — the handler is currently in flight on another CPU. A copy it
     115                 :             :  *             re-queued, if any, has been removed. A short arch_spin_relax()
     116                 :             :  *             has already been done before this return.
     117                 :             :  *
     118                 :             :  * Callers that must not proceed while the handler runs (e.g. before freeing
     119                 :             :  * the timeout) drop any lock the handler may need and call again on -EAGAIN;
     120                 :             :  * after any other return the handler is not in flight on another CPU.
     121                 :             :  * Callers that only need the timeout cancelled may ignore -EAGAIN.
     122                 :             :  */
     123                 :             : int z_try_abort_timeout(struct _timeout *to);
     124                 :             : 
     125                 :             : /* True if @to is the currently in-flight timeout and a same-CPU aborter
     126                 :             :  * has marked the slot as superseded. Handlers with non-idempotent side
     127                 :             :  * effects (e.g. z_thread_timeout()) should call this at entry, after
     128                 :             :  * taking their own lock, and bail if it returns true.
     129                 :             :  */
     130                 :             : bool z_timeout_inflight_superseded(const struct _timeout *to);
     131                 :             : 
     132                 :          56 : static inline void z_init_thread_timeout(struct _thread_base *thread_base)
     133                 :             : {
     134                 :          56 :         z_init_timeout(&thread_base->timeout);
     135                 :          56 : }
     136                 :             : 
     137                 :             : extern void z_thread_timeout(struct _timeout *timeout);
     138                 :             : 
     139                 :          60 : static inline k_ticks_t z_add_thread_timeout(struct k_thread *thread, k_timeout_t ticks)
     140                 :             : {
     141                 :          60 :         return z_add_timeout(&thread->base.timeout, z_thread_timeout, ticks);
     142                 :             : }
     143                 :             : 
     144                 :         118 : static inline int z_try_abort_thread_timeout(struct k_thread *thread)
     145                 :             : {
     146                 :         118 :         return z_try_abort_timeout(&thread->base.timeout);
     147                 :             : }
     148                 :             : 
     149                 :             : uint32_t z_get_next_timeout_expiry(void);
     150                 :             : 
     151                 :             : k_ticks_t z_timeout_remaining(const struct _timeout *timeout);
     152                 :             : 
     153                 :             : #else
     154                 :             : 
     155                 :             : /* Stubs when !CONFIG_SYS_CLOCK_EXISTS */
     156                 :             : #define z_init_thread_timeout(thread_base) do {} while (false)
     157                 :             : #define z_try_abort_thread_timeout(to) 0
     158                 :             : #define z_is_inactive_timeout(to) 1
     159                 :             : #define z_get_next_timeout_expiry() ((uint32_t) K_TICKS_FOREVER)
     160                 :             : #define z_set_timeout_expiry(ticks, is_idle) do {} while (false)
     161                 :             : 
     162                 :             : static inline k_ticks_t z_add_thread_timeout(struct k_thread *thread, k_timeout_t ticks)
     163                 :             : {
     164                 :             :         ARG_UNUSED(thread);
     165                 :             :         ARG_UNUSED(ticks);
     166                 :             :         return 0;
     167                 :             : }
     168                 :             : 
     169                 :             : #endif /* CONFIG_SYS_CLOCK_EXISTS */
     170                 :             : 
     171                 :             : #ifdef __cplusplus
     172                 :             : }
     173                 :             : #endif
     174                 :             : 
     175                 :             : #endif /* ZEPHYR_KERNEL_INCLUDE_TIMEOUT_Q_H_ */
        

Generated by: LCOV version 2.0-1