LCOV - code coverage report
Current view: top level - /home/ubuntu/zephyrproject/zephyr/kernel/timeout - list.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 44.7 % 47 21
Test Date: 2026-11-06 14:43:19 Functions: 55.6 % 9 5
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 18.2 % 22 4

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2018 Intel Corporation
       3                 :             :  *
       4                 :             :  * SPDX-License-Identifier: Apache-2.0
       5                 :             :  */
       6                 :             : 
       7                 :             : #ifndef ZEPHYR_KERNEL_TIMEOUT_LIST_H_
       8                 :             : #define ZEPHYR_KERNEL_TIMEOUT_LIST_H_
       9                 :             : 
      10                 :             : /**
      11                 :             :  * @file
      12                 :             :  * @brief Sorted delta-list timeout backend (implementation).
      13                 :             :  *
      14                 :             :  * This is the original (and default) timeout queue: a doubly linked list
      15                 :             :  * sorted by expiry, where each node stores its expiry as a delta (dticks)
      16                 :             :  * relative to its predecessor. Insertion is O(n), head removal is O(1).
      17                 :             :  *
      18                 :             :  * Included only by kernel/timeout.c, after its shared state, so the queue
      19                 :             :  * instance and the z_timeout_q_*() operations below are private to that
      20                 :             :  * translation unit. The per-node helpers live in kernel/include/timeout_q.h.
      21                 :             :  */
      22                 :             : 
      23                 :             : #include <zephyr/sys/dlist.h>
      24                 :             : 
      25                 :             : static sys_dlist_t timeout_list = SYS_DLIST_STATIC_INIT(&timeout_list);
      26                 :             : 
      27                 :         313 : static inline struct _timeout *z_timeout_q_first(void)
      28                 :             : {
      29                 :         313 :         sys_dnode_t *t = sys_dlist_peek_head(&timeout_list);
      30                 :             : 
      31                 :         313 :         return (t == NULL) ? NULL : CONTAINER_OF(t, struct _timeout, node);
      32                 :             : }
      33                 :             : 
      34                 :          62 : static inline struct _timeout *z_timeout_q_next(struct _timeout *t)
      35                 :             : {
      36                 :          62 :         sys_dnode_t *n = sys_dlist_peek_next(&timeout_list, &t->node);
      37                 :             : 
      38                 :          62 :         return (n == NULL) ? NULL : CONTAINER_OF(n, struct _timeout, node);
      39                 :             : }
      40                 :             : 
      41                 :             : /* Insert @to so that it expires @dticks ticks after curr_tick. Returns true
      42                 :             :  * if @to is now the earliest pending timeout.
      43                 :             :  */
      44                 :          63 : static inline bool z_timeout_q_insert(struct _timeout *to, k_ticks_t dticks)
      45                 :             : {
      46                 :          63 :         struct _timeout *t;
      47                 :             : 
      48                 :          63 :         to->dticks = dticks;
      49         [ -  + ]:          63 :         for (t = z_timeout_q_first(); t != NULL; t = z_timeout_q_next(t)) {
      50         [ #  # ]:           0 :                 if (t->dticks > to->dticks) {
      51                 :           0 :                         t->dticks -= to->dticks;
      52                 :           0 :                         sys_dlist_insert(&t->node, &to->node);
      53                 :           0 :                         return z_timeout_q_first() == to;
      54                 :             :                 }
      55                 :           0 :                 to->dticks -= t->dticks;
      56                 :             :         }
      57                 :             : 
      58                 :          63 :         sys_dlist_append(&timeout_list, &to->node);
      59                 :          63 :         return z_timeout_q_first() == to;
      60                 :             : }
      61                 :             : 
      62                 :             : /* Remove an arbitrary timeout from the queue, absorbing its delta into the
      63                 :             :  * successor so the remaining deltas stay valid. Returns true if @to was the
      64                 :             :  * earliest pending timeout. Used only by the abort path, never from announce.
      65                 :             :  */
      66                 :          62 : static inline bool z_timeout_q_remove(struct _timeout *to)
      67                 :             : {
      68                 :          62 :         bool was_first = (z_timeout_q_first() == to);
      69                 :          62 :         struct _timeout *n = z_timeout_q_next(to);
      70                 :             : 
      71         [ -  + ]:          62 :         if (n != NULL) {
      72                 :           0 :                 n->dticks += to->dticks;
      73                 :             :         }
      74                 :          62 :         sys_dlist_remove(&to->node);
      75                 :             : 
      76                 :          62 :         return was_first;
      77                 :             : }
      78                 :             : 
      79                 :             : /* Ticks from curr_tick until @to expires. Caller holds timeout_lock and has
      80                 :             :  * verified that @to is active.
      81                 :             :  */
      82                 :           0 : static inline k_ticks_t z_timeout_q_remainder(const struct _timeout *to)
      83                 :             : {
      84                 :           0 :         k_ticks_t ticks = 0;
      85                 :             : 
      86         [ #  # ]:           0 :         for (struct _timeout *t = z_timeout_q_first(); t != NULL;
      87                 :           0 :              t = z_timeout_q_next(t)) {
      88                 :           0 :                 ticks += t->dticks;
      89         [ #  # ]:           0 :                 if (to == t) {
      90                 :             :                         break;
      91                 :             :                 }
      92                 :             :         }
      93                 :             : 
      94                 :           0 :         return ticks;
      95                 :             : }
      96                 :             : 
      97                 :             : /* Ticks from (curr_tick + ticks_elapsed) until the next expiry, clamped to
      98                 :             :  * int32 / SYS_CLOCK_MAX_WAIT. Empty queue yields SYS_CLOCK_MAX_WAIT.
      99                 :             :  */
     100                 :             : /* Ticks from curr_tick to the earliest pending timeout, or K_TICKS_FOREVER if
     101                 :             :  * the queue is empty. The announce-range cap is applied centrally by
     102                 :             :  * next_timeout() in timeout.c.
     103                 :             :  */
     104                 :         125 : static inline k_ticks_t z_timeout_q_next_expiry(void)
     105                 :             : {
     106                 :         125 :         struct _timeout *to = z_timeout_q_first();
     107                 :             : 
     108         [ +  + ]:         125 :         return (to == NULL) ? K_TICKS_FOREVER : to->dticks;
     109                 :             : }
     110                 :             : 
     111                 :             : /* --- announce-loop helpers (see sys_clock_announce_locked() in timeout.c) --- */
     112                 :             : 
     113                 :             : /* Ticks from curr_tick until the next event the announce loop must act on
     114                 :             :  * (here: the earliest expiry). A value larger than any announce window when
     115                 :             :  * the queue is empty.
     116                 :             :  */
     117                 :           0 : static inline int32_t z_timeout_q_next_gap(void)
     118                 :             : {
     119                 :           0 :         struct _timeout *t = z_timeout_q_first();
     120                 :             : 
     121   [ #  #  #  # ]:           0 :         return (t == NULL) ? INT32_MAX : (int32_t)MIN((int64_t)t->dticks, INT32_MAX);
     122                 :             : }
     123                 :             : 
     124                 :             : /* Advance backend state by @dt ticks. For the delta list this shifts the head
     125                 :             :  * closer to expiry; a head whose gap is fully consumed reads dticks == 0 and
     126                 :             :  * is then drained by z_timeout_q_pop_due(). The caller advances curr_tick.
     127                 :             :  */
     128                 :           0 : static inline void z_timeout_q_advance(int32_t dt)
     129                 :             : {
     130                 :           0 :         struct _timeout *t = z_timeout_q_first();
     131                 :             : 
     132         [ #  # ]:           0 :         if (t != NULL) {
     133                 :           0 :                 t->dticks -= dt;
     134                 :             :         }
     135                 :           0 : }
     136                 :             : 
     137                 :             : /* Pop and return one timeout that is due at the current curr_tick, or NULL if
     138                 :             :  * none is due right now. Strict head-only removal with no delta absorption:
     139                 :             :  * z_timeout_q_advance() has already brought the head's dticks to 0, and
     140                 :             :  * curr_tick was advanced to match, so the successor's stored delta stays
     141                 :             :  * valid without propagation.
     142                 :             :  */
     143                 :           0 : static inline struct _timeout *z_timeout_q_pop_due(void)
     144                 :             : {
     145                 :           0 :         struct _timeout *t = z_timeout_q_first();
     146                 :             : 
     147   [ #  #  #  # ]:           0 :         if ((t == NULL) || (t->dticks != 0)) {
     148                 :             :                 return NULL;
     149                 :             :         }
     150                 :             : 
     151                 :           0 :         sys_dlist_remove(&t->node);
     152                 :             : 
     153                 :           0 :         return t;
     154                 :             : }
     155                 :             : 
     156                 :             : #endif /* ZEPHYR_KERNEL_TIMEOUT_LIST_H_ */
        

Generated by: LCOV version 2.0-1