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

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2016-2017 Wind River Systems, Inc.
       3                 :             :  *
       4                 :             :  * SPDX-License-Identifier: Apache-2.0
       5                 :             :  */
       6                 :             : 
       7                 :             : #ifndef ZEPHYR_KERNEL_INCLUDE_KSCHED_H_
       8                 :             : #define ZEPHYR_KERNEL_INCLUDE_KSCHED_H_
       9                 :             : 
      10                 :             : #include <zephyr/kernel_structs.h>
      11                 :             : #include <kspinlock.h>
      12                 :             : #include <kernel_internal.h>
      13                 :             : #include <timeout_q.h>
      14                 :             : #include <kthread.h>
      15                 :             : #include <zephyr/tracing/tracing.h>
      16                 :             : #include <stdbool.h>
      17                 :             : #include <priority_q.h>
      18                 :             : 
      19                 :             : BUILD_ASSERT(K_LOWEST_APPLICATION_THREAD_PRIO
      20                 :             :              >= K_HIGHEST_APPLICATION_THREAD_PRIO);
      21                 :             : 
      22                 :             : #ifdef CONFIG_MULTITHREADING
      23                 :             : #define Z_VALID_PRIO(prio, entry_point)                              \
      24                 :             :         (((prio) == K_IDLE_PRIO && z_is_idle_thread_entry(entry_point)) || \
      25                 :             :          ((K_LOWEST_APPLICATION_THREAD_PRIO                          \
      26                 :             :            >= K_HIGHEST_APPLICATION_THREAD_PRIO)                  \
      27                 :             :           && (prio) >= K_HIGHEST_APPLICATION_THREAD_PRIO          \
      28                 :             :           && (prio) <= K_LOWEST_APPLICATION_THREAD_PRIO))
      29                 :             : 
      30                 :             : #define Z_ASSERT_VALID_PRIO(prio, entry_point) do { \
      31                 :             :         __ASSERT(Z_VALID_PRIO((prio), (entry_point)), \
      32                 :             :                  "invalid priority (%d); allowed range: %d to %d", \
      33                 :             :                  (prio), \
      34                 :             :                  K_LOWEST_APPLICATION_THREAD_PRIO, \
      35                 :             :                  K_HIGHEST_APPLICATION_THREAD_PRIO); \
      36                 :             :         } while (false)
      37                 :             : #else
      38                 :             : #define Z_VALID_PRIO(prio, entry_point) ((prio) == -1)
      39                 :             : #define Z_ASSERT_VALID_PRIO(prio, entry_point) __ASSERT((prio) == -1, "")
      40                 :             : #endif /* CONFIG_MULTITHREADING */
      41                 :             : 
      42                 :             : #ifdef __cplusplus
      43                 :             : extern "C" {
      44                 :             : #endif
      45                 :             : 
      46                 :             : extern struct k_thread _thread_dummy;
      47                 :             : 
      48                 :             : /**
      49                 :             :  * @brief Unpend thread, do not abort its timeout (if it exists).
      50                 :             :  */
      51                 :             : void z_unpend_thread_no_timeout(struct k_thread *thread);
      52                 :             : 
      53                 :             : /**
      54                 :             :  * @brief Unpend thread and abort its timeout (if it exists).
      55                 :             :  */
      56                 :             : void z_unpend_thread(struct k_thread *thread);
      57                 :             : struct k_thread *z_unpend1_no_timeout(_wait_q_t *wait_q);
      58                 :             : int z_pend_curr(struct k_spinlock *lock, k_spinlock_key_t key,
      59                 :             :                _wait_q_t *wait_q, k_timeout_t timeout);
      60                 :             : void z_pend_thread(struct k_thread *thread, _wait_q_t *wait_q,
      61                 :             :                    k_timeout_t timeout);
      62                 :             : void z_reschedule(struct k_spinlock *lock, k_spinlock_key_t key);
      63                 :             : void z_reschedule_locked(k_spinlock_key_t key);
      64                 :             : void z_reschedule_irqlock(uint32_t key);
      65                 :             : int z_unpend_all(_wait_q_t *wait_q);
      66                 :             : bool z_thread_prio_set(struct k_thread *thread, int prio);
      67                 :             : void z_thread_suspend_current(struct k_thread *thread);
      68                 :             : 
      69                 :             : /**
      70                 :             :  * @brief Determine next thread to execute upon completion of an interrupt
      71                 :             :  *
      72                 :             :  * Thread preemption is performed by context switching after the completion
      73                 :             :  * of a non-recursed interrupt. This function determines which thread to
      74                 :             :  * switch to if any. This function accepts as @p interrupted either:
      75                 :             :  *
      76                 :             :  * - The handle for the interrupted thread in which case the thread's context
      77                 :             :  *   must already be fully saved and ready to be picked up by a different CPU.
      78                 :             :  *
      79                 :             :  * - NULL if more work is required to fully save the thread's state after
      80                 :             :  *   it is known that a new thread is to be scheduled. It is up to the caller
      81                 :             :  *   to store the handle resulting from the thread that is being switched out
      82                 :             :  *   in that thread's "switch_handle" field after its
      83                 :             :  *   context has fully been saved, following the same requirements as with
      84                 :             :  *   the @ref arch_switch() function.
      85                 :             :  *
      86                 :             :  * If a new thread needs to be scheduled then its handle is returned.
      87                 :             :  * Otherwise the same value provided as @p interrupted is returned back.
      88                 :             :  * Those handles are the same opaque types used by the @ref arch_switch()
      89                 :             :  * function.
      90                 :             :  *
      91                 :             :  * The uniprocessor implementation is in kernel/sched.c, the SMP one in
      92                 :             :  * kernel/smp/sched_smp.c.
      93                 :             :  *
      94                 :             :  * @warning
      95                 :             :  * The _current value may have changed after this call and not refer
      96                 :             :  * to the interrupted thread anymore. It might be necessary to make a local
      97                 :             :  * copy before calling this function.
      98                 :             :  *
      99                 :             :  * @param interrupted Handle for the thread that was interrupted or NULL.
     100                 :             :  * @return Handle for the next thread to execute, or @p interrupted when
     101                 :             :  *         no new thread is to be scheduled.
     102                 :             :  */
     103                 :             : void *z_get_next_switch_handle(void *interrupted);
     104                 :             : 
     105                 :             : /* Just a wrapper around z_current_thread_set(xxx) with tracing.
     106                 :             :  *
     107                 :             :  * _current is not guaranteed to report @a new_thread until the switch to it
     108                 :             :  * has happened: see z_current_thread_set(). Code between this call and the
     109                 :             :  * switch must use @a new_thread directly.
     110                 :             :  */
     111                 :             : static inline void z_sched_set_current(struct k_thread *new_thread)
     112                 :             : {
     113                 :             :         /* If the new thread is the same as the current thread, we
     114                 :             :          * don't need to do anything.
     115                 :             :          */
     116                 :             :         if (IS_ENABLED(CONFIG_INSTRUMENT_THREAD_SWITCHING) && new_thread != _current) {
     117                 :             :                 z_thread_mark_switched_out();
     118                 :             :         }
     119                 :             :         z_current_thread_set(new_thread);
     120                 :             : }
     121                 :             : 
     122                 :             : /* Wrapper around z_get_next_switch_handle() for the benefit of
     123                 :             :  * non-SMP platforms that always pass a NULL interrupted handle.
     124                 :             :  * Exposes the (extremely) common early exit case in a context that
     125                 :             :  * can be (much) better optimized in surrounding C code.  Takes a
     126                 :             :  * _current pointer that the outer scope surely already has to avoid
     127                 :             :  * having to refetch it across a lock.
     128                 :             :  *
     129                 :             :  * Mystic arts for cycle nerds, basically.  Replace with
     130                 :             :  * z_get_next_switch_handle() if this becomes a maintenance hassle.
     131                 :             :  */
     132                 :             : static ALWAYS_INLINE void *z_sched_next_handle(struct k_thread *curr)
     133                 :             : {
     134                 :             : #ifndef CONFIG_SMP
     135                 :             :         if (curr == _kernel.ready_q.cache) {
     136                 :             :                 return NULL;
     137                 :             :         }
     138                 :             : #endif
     139                 :             :         return z_get_next_switch_handle(NULL);
     140                 :             : }
     141                 :             : 
     142                 :             : void z_sched_start(struct k_thread *thread);
     143                 :             : void z_ready_thread(struct k_thread *thread);
     144                 :             : void move_current_to_end_of_prio_q(void);
     145                 :             : 
     146                 :             : /*
     147                 :             :  * Internal scheduler functions exposed for use by thread lifecycle code
     148                 :             :  * (thread.c). These operate under the scheduler's spinlock and must not be
     149                 :             :  * called without holding it.
     150                 :             :  */
     151                 :             : 
     152                 :             : /**
     153                 :             :  * @brief Halt a thread, suspending or terminating it.
     154                 :             :  *
     155                 :             :  * Shared implementation for k_thread_suspend() and k_thread_abort(). The
     156                 :             :  * caller must hold the scheduler's spinlock; this function releases it before
     157                 :             :  * returning (possibly after a context switch).
     158                 :             :  *
     159                 :             :  * @param thread  Thread to halt.
     160                 :             :  * @param key     Scheduler spinlock key held by the caller.
     161                 :             :  * @param terminate true to abort (kill) the thread, false to suspend it.
     162                 :             :  */
     163                 :             : void z_thread_halt(struct k_thread *thread, k_spinlock_key_t key, bool terminate);
     164                 :             : 
     165                 :             : /**
     166                 :             :  * @brief Ready a thread while the scheduler spinlock is already held.
     167                 :             :  *
     168                 :             :  * Equivalent to the internal ready_thread() helper. Callers must hold the
     169                 :             :  * scheduler's spinlock.
     170                 :             :  *
     171                 :             :  * @param thread Thread to make ready.
     172                 :             :  */
     173                 :             : void z_sched_ready_locked(struct k_thread *thread);
     174                 :             : 
     175                 :             : /**
     176                 :             :  * @brief Add a thread to a wait queue while the scheduler spinlock is held.
     177                 :             :  *
     178                 :             :  * Moves the thread out of the run queue and onto the specified wait queue.
     179                 :             :  * Callers must hold the scheduler's spinlock.
     180                 :             :  *
     181                 :             :  * @param thread  Thread to pend.
     182                 :             :  * @param wait_q  Wait queue to add the thread to (may be NULL).
     183                 :             :  */
     184                 :             : void z_sched_add_to_waitq_locked(struct k_thread *thread, _wait_q_t *wait_q);
     185                 :             : 
     186                 :             : /**
     187                 :             :  * @brief Dequeue a thread and move it into a halted state.
     188                 :             :  *
     189                 :             :  * The thread is known not to be running anywhere. Callers must hold the
     190                 :             :  * scheduler's spinlock; a terminating halt may release and reacquire it
     191                 :             :  * through @p key while waiting for an in-flight timeout handler.
     192                 :             :  *
     193                 :             :  * @param thread    Thread to halt.
     194                 :             :  * @param new_state New thread state (_THREAD_DEAD or _THREAD_SUSPENDED).
     195                 :             :  * @param key       Pointer to the held spinlock key, or NULL when called
     196                 :             :  *                  from the scheduler on a running thread that has no
     197                 :             :  *                  timeout.
     198                 :             :  */
     199                 :             : void z_sched_halt_thread_locked(struct k_thread *thread, uint8_t new_state, k_spinlock_key_t *key);
     200                 :             : 
     201                 :             : /**
     202                 :             :  * @brief Ready every thread on a wait queue with a zero return value.
     203                 :             :  *
     204                 :             :  * Callers must hold the scheduler's spinlock.
     205                 :             :  *
     206                 :             :  * @param wait_q Wait queue to empty.
     207                 :             :  */
     208                 :             : void z_sched_unpend_all_locked(_wait_q_t *wait_q);
     209                 :             : 
     210                 :             : /**
     211                 :             :  * @brief Remove a thread from the run queue (scheduler spinlock must be held).
     212                 :             :  *
     213                 :             :  * Called by sleep.c to unready the current thread before arming its wakeup
     214                 :             :  * timeout.  Callers must already hold the scheduler's spinlock.
     215                 :             :  *
     216                 :             :  * @param thread Thread to remove from the run queue.
     217                 :             :  */
     218                 :             : void z_sched_unready_locked(struct k_thread *thread);
     219                 :             : 
     220                 :             : /**
     221                 :             :  * @brief Yield the current thread's remaining time slice.
     222                 :             :  *
     223                 :             :  * Moves _current to the end of its priority group in the run queue, updates
     224                 :             :  * the scheduler cache, and context-switches away.  Equivalent to the body
     225                 :             :  * of k_yield(); exposed so that thread.c can implement k_yield() without
     226                 :             :  * depending on sched.c internals.
     227                 :             :  */
     228                 :             : void z_sched_yield(void);
     229                 :             : 
     230                 :           0 : static inline void z_reschedule_unlocked(void)
     231                 :             : {
     232                 :           0 :         (void) z_reschedule_irqlock(arch_irq_lock());
     233                 :           0 : }
     234                 :             : 
     235                 :             : static inline bool z_is_under_prio_ceiling(int prio)
     236                 :             : {
     237                 :             :         return prio >= CONFIG_PRIORITY_CEILING;
     238                 :             : }
     239                 :             : 
     240                 :             : static inline int z_get_new_prio_with_ceiling(int prio)
     241                 :             : {
     242                 :             :         return z_is_under_prio_ceiling(prio) ? prio : CONFIG_PRIORITY_CEILING;
     243                 :             : }
     244                 :             : 
     245                 :             : static inline bool z_is_prio1_higher_than_or_equal_to_prio2(int prio1, int prio2)
     246                 :             : {
     247                 :             :         return prio1 <= prio2;
     248                 :             : }
     249                 :             : 
     250                 :             : static inline bool z_is_prio_higher_or_equal(int prio1, int prio2)
     251                 :             : {
     252                 :             :         return z_is_prio1_higher_than_or_equal_to_prio2(prio1, prio2);
     253                 :             : }
     254                 :             : 
     255                 :             : static inline bool z_is_prio1_lower_than_or_equal_to_prio2(int prio1, int prio2)
     256                 :             : {
     257                 :             :         return prio1 >= prio2;
     258                 :             : }
     259                 :             : 
     260                 :          63 : static inline bool z_is_prio1_higher_than_prio2(int prio1, int prio2)
     261                 :             : {
     262                 :          63 :         return prio1 < prio2;
     263                 :             : }
     264                 :             : 
     265                 :          63 : static inline bool z_is_prio_higher(int prio, int test_prio)
     266                 :             : {
     267                 :          63 :         return z_is_prio1_higher_than_prio2(prio, test_prio);
     268                 :             : }
     269                 :             : 
     270                 :             : static inline bool z_is_prio_lower_or_equal(int prio1, int prio2)
     271                 :             : {
     272                 :             :         return z_is_prio1_lower_than_or_equal_to_prio2(prio1, prio2);
     273                 :             : }
     274                 :             : 
     275                 :             : static inline bool _is_valid_prio(int prio, k_thread_entry_t entry_point)
     276                 :             : {
     277                 :             :         if ((prio == K_IDLE_PRIO) && z_is_idle_thread_entry(entry_point)) {
     278                 :             :                 return true;
     279                 :             :         }
     280                 :             : 
     281                 :             :         if (!z_is_prio_higher_or_equal(prio,
     282                 :             :                                        K_LOWEST_APPLICATION_THREAD_PRIO)) {
     283                 :             :                 return false;
     284                 :             :         }
     285                 :             : 
     286                 :             :         if (!z_is_prio_lower_or_equal(prio,
     287                 :             :                                       K_HIGHEST_APPLICATION_THREAD_PRIO)) {
     288                 :             :                 return false;
     289                 :             :         }
     290                 :             : 
     291                 :             :         return true;
     292                 :             : }
     293                 :             : 
     294                 :          10 : static ALWAYS_INLINE _wait_q_t *pended_on_thread(struct k_thread *thread)
     295                 :             : {
     296         [ -  + ]:          10 :         __ASSERT_NO_MSG(thread->base.pended_on);
     297                 :             : 
     298                 :          10 :         return thread->base.pended_on;
     299                 :             : }
     300                 :             : 
     301                 :             : 
     302                 :          10 : static ALWAYS_INLINE void unpend_thread_no_timeout(struct k_thread *thread)
     303                 :             : {
     304                 :          10 :         _priq_wait_remove(&pended_on_thread(thread)->waitq, thread);
     305                 :          10 :         z_mark_thread_as_not_pending(thread);
     306                 :          10 :         thread->base.pended_on = NULL;
     307                 :          10 : }
     308                 :             : 
     309                 :             : /*
     310                 :             :  * Pick the best-priority waiter from @p wait_q, unpend it, and abort any
     311                 :             :  * timeout it had pending. Returns the thread, or NULL if the wait_q was
     312                 :             :  * empty.
     313                 :             :  *
     314                 :             :  * Caller MUST hold the scheduler's spinlock and MUST complete the wake (set
     315                 :             :  * return value, ready the thread) under the same lock acquisition before
     316                 :             :  * releasing it. Otherwise a racing in-flight timeout handler can ready the
     317                 :             :  * thread before the caller's wake state is in place, exposing the woken thread
     318                 :             :  * to a stale swap_retval. The pre-1b8c7a3 dticks-cancel check used to
     319                 :             :  * close this window; doing all the wake work atomically under the sched
     320                 :             :  * lock is now the way.
     321                 :             :  *
     322                 :             :  * z_sched_wake() is the convenient wrapper for the common case of
     323                 :             :  * "wake one waiter with this retval and this swap_data".
     324                 :             :  */
     325                 :           6 : static ALWAYS_INLINE struct k_thread *z_unpend_first_thread_locked(_wait_q_t *wait_q)
     326                 :             : {
     327                 :           6 :         struct k_thread *thread = _priq_wait_best(&wait_q->waitq);
     328                 :             : 
     329         [ +  + ]:           6 :         if (unlikely(thread != NULL)) {
     330                 :           4 :                 unpend_thread_no_timeout(thread);
     331                 :             :                 /* Abort the thread's timeout. If its handler is in flight on
     332                 :             :                  * another CPU it is blocked on the sched lock; the abort flags
     333                 :             :                  * it superseded, and z_thread_timeout() bails on that flag when
     334                 :             :                  * it finally runs -- so it won't wake the thread from whatever
     335                 :             :                  * it has since re-pended on. We don't wait for it here.
     336                 :             :                  */
     337                 :           4 :                 (void)z_try_abort_thread_timeout(thread);
     338                 :             :         }
     339                 :           6 :         return thread;
     340                 :             : }
     341                 :             : 
     342                 :             : /**
     343                 :             :  * Wake up a thread pending on the provided wait queue
     344                 :             :  *
     345                 :             :  * Given a wait_q, wake up the highest priority thread on the queue. If the
     346                 :             :  * queue was empty just return false.
     347                 :             :  *
     348                 :             :  * Otherwise, do the following, in order,  holding the scheduler's spinlock the
     349                 :             :  *  entire time so that the thread state is guaranteed not to change:
     350                 :             :  * - Set the thread's swap return values to swap_retval and swap_data
     351                 :             :  * - un-pend and ready the thread, but do not invoke the scheduler.
     352                 :             :  *
     353                 :             :  * Repeated calls to this function until it returns false is a suitable
     354                 :             :  * way to wake all threads on the queue.
     355                 :             :  *
     356                 :             :  * It is up to the caller to implement locking such that the return value of
     357                 :             :  * this function (whether a thread was woken up or not) does not immediately
     358                 :             :  * become stale. Calls to wait and wake on the same wait_q object must have
     359                 :             :  * synchronization. Calling this without holding any spinlock is a sign that
     360                 :             :  * this API is not being used properly.
     361                 :             :  *
     362                 :             :  * @param wait_q Wait queue to wake up the highest prio thread
     363                 :             :  * @param swap_retval Swap return value for woken thread
     364                 :             :  * @param swap_data Data return value to supplement swap_retval. May be NULL.
     365                 :             :  * @retval true If a thread was woken up
     366                 :             :  * @retval false If the wait_q was empty
     367                 :             :  */
     368                 :           6 : static ALWAYS_INLINE bool z_sched_wake(_wait_q_t *wait_q, int swap_retval, void *swap_data)
     369                 :             : {
     370                 :           6 :         struct k_thread *thread;
     371                 :           6 :         bool ret = false;
     372                 :             : 
     373                 :             :         LOCK_SCHED_SPINLOCK {
     374                 :           6 :                 thread = z_unpend_first_thread_locked(wait_q);
     375         [ +  + ]:           6 :                 if (thread != NULL) {
     376                 :           4 :                         z_thread_return_value_set_with_data(thread,
     377                 :             :                                                             swap_retval,
     378                 :             :                                                             swap_data);
     379                 :           4 :                         z_sched_ready_locked(thread);
     380                 :           4 :                         ret = true;
     381                 :             :                 }
     382                 :             :         }
     383                 :             : 
     384                 :           6 :         return ret;
     385                 :             : }
     386                 :             : 
     387                 :             : #ifdef __cplusplus
     388                 :             : }
     389                 :             : #endif
     390                 :             : 
     391                 :             : #endif /* ZEPHYR_KERNEL_INCLUDE_KSCHED_H_ */
        

Generated by: LCOV version 2.0-1