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

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2018 Intel Corporation
       3                 :             :  *
       4                 :             :  * SPDX-License-Identifier: Apache-2.0
       5                 :             :  */
       6                 :             : 
       7                 :             : /**
       8                 :             :  * @file
       9                 :             :  * @brief Thread sleep/wake APIs
      10                 :             :  *
      11                 :             :  * Implements k_sleep_ticks(), the primitive that k_sleep(), k_msleep() and
      12                 :             :  * k_usleep() are built upon.  It suspends the calling thread for a specified
      13                 :             :  * duration and relies on the scheduler (via z_sched_unready_locked()) and the
      14                 :             :  * timeout subsystem (via z_add_thread_timeout()) to do the heavy lifting.  The
      15                 :             :  * millisecond and microsecond flavours are inlined in sleep.h on top of it.
      16                 :             :  */
      17                 :             : 
      18                 :             : #include <zephyr/kernel.h>
      19                 :             : #include <ksched.h>
      20                 :             : #include <kthread.h>
      21                 :             : #include <kswap.h>
      22                 :             : #include <timeout_q.h>
      23                 :             : #include <zephyr/internal/syscall_handler.h>
      24                 :             : #include <zephyr/tracing/tracing.h>
      25                 :             : #include <zephyr/logging/log.h>
      26                 :             : #include <zephyr/sys/math_extras.h>
      27                 :             : #include <zephyr/sys/minmax.h>
      28                 :             : #include <zephyr/sys/util.h>
      29                 :             : 
      30                 :             : /* pending_current is owned by timeslicing.c; we reference it here to avoid
      31                 :             :  * a spurious timeslice when the thread that is going to sleep gets picked as
      32                 :             :  * the next thread to run while z_swap() is in progress.
      33                 :             :  */
      34                 :             : #if defined(CONFIG_SWAP_NONATOMIC) && defined(CONFIG_TIMESLICING)
      35                 :             : extern struct k_thread *pending_current;
      36                 :             : #endif
      37                 :             : 
      38                 :             : /**
      39                 :             :  * @brief Sleep the current thread for the given number of ticks.
      40                 :             :  *
      41                 :             :  * Removes _current from the run queue, arms a timeout for the requested
      42                 :             :  * duration, then context-switches away.  Returns the number of ticks
      43                 :             :  * remaining if the sleep was cut short by k_wakeup(), or 0 on normal
      44                 :             :  * wakeup.
      45                 :             :  *
      46                 :             :  * @param timeout Sleep duration.
      47                 :             :  * @return Ticks remaining when woken early, 0 on normal expiry, or
      48                 :             :  *         K_TICKS_FOREVER if @a timeout was K_FOREVER.
      49                 :             :  */
      50                 :           0 : k_ticks_t z_impl_k_sleep_ticks(k_timeout_t timeout)
      51                 :             : {
      52                 :           0 :         uint32_t expected_wakeup_ticks;
      53                 :             : 
      54         [ #  # ]:           0 :         __ASSERT(!arch_is_in_isr(), "");
      55                 :             : 
      56                 :           0 :         SYS_PORT_TRACING_FUNC_ENTER(k_thread, sleep_ticks, timeout);
      57                 :             : 
      58                 :             :         /* K_NO_WAIT is treated as a 'yield' */
      59         [ #  # ]:           0 :         if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
      60                 :           0 :                 k_yield();
      61                 :           0 :                 SYS_PORT_TRACING_FUNC_EXIT(k_thread, sleep_ticks, timeout, 0);
      62                 :           0 :                 return 0;
      63                 :             :         }
      64                 :             : 
      65                 :           0 :         k_spinlock_key_t key = z_sched_spinlock_lock();
      66                 :             : 
      67                 :             : #if defined(CONFIG_TIMESLICING) && defined(CONFIG_SWAP_NONATOMIC)
      68                 :             :         pending_current = _current;
      69                 :             : #endif /* CONFIG_TIMESLICING && CONFIG_SWAP_NONATOMIC */
      70                 :           0 :         z_sched_unready_locked(_current);
      71                 :           0 :         expected_wakeup_ticks = (uint32_t)z_add_thread_timeout(_current, timeout);
      72                 :           0 :         z_mark_thread_as_sleeping(_current);
      73                 :             : 
      74                 :           0 :         (void)z_swap_locked(key);
      75                 :             : 
      76                 :             :         /* There is no meaningful remainder to report for K_FOREVER: reaching
      77                 :             :          * this point at all means a k_wakeup() cut the sleep short.
      78                 :             :          */
      79         [ #  # ]:           0 :         if (K_TIMEOUT_EQ(timeout, K_FOREVER)) {
      80                 :             :                 SYS_PORT_TRACING_FUNC_EXIT(k_thread, sleep_ticks, timeout, K_TICKS_FOREVER);
      81                 :             :                 return K_TICKS_FOREVER;
      82                 :             :         }
      83                 :             : 
      84                 :             :         /* We require a 32 bit unsigned subtraction to handle a wraparound.
      85                 :             :          * A normal timeout-driven wakeup leaves zero (or a slightly negative)
      86                 :             :          * remainder; only an early k_wakeup() yields a positive value.
      87                 :             :          */
      88                 :           0 :         uint32_t left_ticks = expected_wakeup_ticks - sys_clock_tick_get_32();
      89                 :             : 
      90                 :             :         /* Use signed comparison so past-due wakeups (negative remainder) return 0.
      91                 :             :          * k_ticks_t may be uint32_t (!CONFIG_TIMEOUT_64BIT), so comparing ticks > 0
      92                 :             :          * directly would be an unsigned comparison and would misinterpret a negative
      93                 :             :          * remainder as a large positive value.
      94                 :             :          */
      95                 :           0 :         int32_t signed_left = (int32_t)left_ticks;
      96                 :             : 
      97                 :           0 :         if (signed_left < 0) {
      98                 :             :                 signed_left = 0;
      99                 :             :         }
     100                 :             : 
     101                 :           0 :         SYS_PORT_TRACING_FUNC_EXIT(k_thread, sleep_ticks, timeout, signed_left);
     102                 :           0 :         return (k_ticks_t)signed_left;
     103                 :             : }
     104                 :             : 
     105                 :             : #ifdef CONFIG_USERSPACE
     106                 :             : static inline k_ticks_t z_vrfy_k_sleep_ticks(k_timeout_t timeout)
     107                 :             : {
     108                 :             :         return z_impl_k_sleep_ticks(timeout);
     109                 :             : }
     110                 :             : #include <zephyr/syscalls/k_sleep_ticks_mrsh.c>
     111                 :             : #endif /* CONFIG_USERSPACE */
        

Generated by: LCOV version 2.0-1