LCOV - code coverage report
Current view: top level - /home/ubuntu/zephyrproject/zephyr/kernel - sem.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 62.1 % 58 36
Test Date: 2026-11-06 14:43:19 Functions: 66.7 % 6 4
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 40.0 % 20 8

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2010-2016 Wind River Systems, Inc.
       3                 :             :  *
       4                 :             :  * SPDX-License-Identifier: Apache-2.0
       5                 :             :  */
       6                 :             : 
       7                 :             : /**
       8                 :             :  * @file
       9                 :             :  *
      10                 :             :  * @brief Kernel semaphore object.
      11                 :             :  *
      12                 :             :  * The semaphores are of the 'counting' type, i.e. each 'give' operation will
      13                 :             :  * increment the internal count by 1, if no thread is pending on it. The 'init'
      14                 :             :  * call initializes the count to 'initial_count'. Following multiple 'give'
      15                 :             :  * operations, the same number of 'take' operations can be performed without
      16                 :             :  * the calling thread having to pend on the semaphore, or the calling task
      17                 :             :  * having to poll.
      18                 :             :  */
      19                 :             : 
      20                 :             : #include <zephyr/kernel.h>
      21                 :             : 
      22                 :             : #include <zephyr/toolchain.h>
      23                 :             : #include <wait_q.h>
      24                 :             : #include <zephyr/sys/dlist.h>
      25                 :             : #include <ksched.h>
      26                 :             : #include <scheduler.h>
      27                 :             : #include <zephyr/init.h>
      28                 :             : #include <zephyr/internal/syscall_handler.h>
      29                 :             : #include <zephyr/tracing/tracing.h>
      30                 :             : #include <zephyr/sys/check.h>
      31                 :             : 
      32                 :             : /* Optional striped locks reduce the performance impact of a system-wide
      33                 :             :  * semaphore lock without the significant extra RAM required by per-object
      34                 :             :  * locks (semaphores are *very* widely used). A properly spin-aware semaphore
      35                 :             :  * implementation would spin on atomic access to the count variable,
      36                 :             :  * and not a spinlock per se. Useful optimization for the future...
      37                 :             :  */
      38                 :             : /* A k_spinlock can be zero-sized on uniprocessor configurations. Keep the
      39                 :             :  * default lock scalar because arrays of empty structures are not portable.
      40                 :             :  */
      41                 :             : #if defined(CONFIG_SMP) && (CONFIG_SEM_LOCK_STRIPES > 1)
      42                 :             : static struct k_spinlock sem_locks[CONFIG_SEM_LOCK_STRIPES];
      43                 :             : #else
      44                 :             : static struct k_spinlock sem_lock;
      45                 :             : #endif
      46                 :             : 
      47                 :          12 : static inline struct k_spinlock *sem_spinlock_get(struct k_sem *sem)
      48                 :             : {
      49                 :             : #if defined(CONFIG_SMP) && (CONFIG_SEM_LOCK_STRIPES > 1)
      50                 :             :         BUILD_ASSERT(IS_POWER_OF_TWO(CONFIG_SEM_LOCK_STRIPES),
      51                 :             :                      "CONFIG_SEM_LOCK_STRIPES must be a power of two");
      52                 :             : 
      53                 :             :         /* Hash the naturally aligned semaphore address into a lock stripe. */
      54                 :             :         return &sem_locks[((uintptr_t)sem / __alignof(struct k_sem)) %
      55                 :             :                           CONFIG_SEM_LOCK_STRIPES];
      56                 :             : #else
      57                 :          12 :         ARG_UNUSED(sem);
      58                 :          12 :         return &sem_lock;
      59                 :             : #endif
      60                 :             : }
      61                 :             : 
      62                 :             : #ifdef CONFIG_OBJ_CORE_SEM
      63                 :             : K_OBJ_TYPE_DEFINE(obj_type_sem, k_sem, K_OBJ_TYPE_SEM_ID, NULL);
      64                 :             : #endif /* CONFIG_OBJ_CORE_SEM */
      65                 :             : 
      66                 :           0 : int z_impl_k_sem_init(struct k_sem *sem, unsigned int initial_count,
      67                 :             :                       unsigned int limit)
      68                 :             : {
      69                 :             :         /*
      70                 :             :          * Limit cannot be zero and count cannot be greater than limit
      71                 :             :          */
      72         [ #  # ]:           0 :         CHECKIF(limit == 0U || initial_count > limit) {
      73                 :             :                 SYS_PORT_TRACING_OBJ_FUNC(k_sem, init, sem, -EINVAL);
      74                 :             : 
      75                 :             :                 return -EINVAL;
      76                 :             :         }
      77                 :             : 
      78                 :           0 :         sem->count = initial_count;
      79                 :           0 :         sem->limit = limit;
      80                 :             : 
      81                 :           0 :         SYS_PORT_TRACING_OBJ_FUNC(k_sem, init, sem, 0);
      82                 :             : 
      83                 :           0 :         z_waitq_init(&sem->wait_q);
      84                 :             : #if defined(CONFIG_POLL)
      85                 :             :         sys_dlist_init(&sem->poll_events);
      86                 :             : #endif /* CONFIG_POLL */
      87                 :           0 :         k_object_init(sem);
      88                 :             : 
      89                 :             : #ifdef CONFIG_OBJ_CORE_SEM
      90                 :             :         k_obj_core_init_and_link(K_OBJ_CORE(sem), &obj_type_sem);
      91                 :             : #endif /* CONFIG_OBJ_CORE_SEM */
      92                 :             : 
      93                 :           0 :         return 0;
      94                 :             : }
      95                 :             : 
      96                 :             : #ifdef CONFIG_USERSPACE
      97                 :             : int z_vrfy_k_sem_init(struct k_sem *sem, unsigned int initial_count,
      98                 :             :                       unsigned int limit)
      99                 :             : {
     100                 :             :         K_OOPS(K_SYSCALL_OBJ_INIT(sem, K_OBJ_SEM));
     101                 :             :         return z_impl_k_sem_init(sem, initial_count, limit);
     102                 :             : }
     103                 :             : #include <zephyr/syscalls/k_sem_init_mrsh.c>
     104                 :             : #endif /* CONFIG_USERSPACE */
     105                 :             : 
     106                 :           2 : static inline bool sem_handle_poll_events(struct k_sem *sem)
     107                 :             : {
     108                 :             : #ifdef CONFIG_POLL
     109                 :             :         return z_handle_obj_poll_events(&sem->poll_events, K_POLL_STATE_SEM_AVAILABLE);
     110                 :             : #else
     111                 :           2 :         ARG_UNUSED(sem);
     112                 :           2 :         return false;
     113                 :             : #endif /* CONFIG_POLL */
     114                 :             : }
     115                 :             : 
     116                 :           6 : void z_impl_k_sem_give(struct k_sem *sem)
     117                 :             : {
     118                 :           6 :         struct k_spinlock *lock = sem_spinlock_get(sem);
     119                 :           6 :         k_spinlock_key_t key = k_spin_lock(lock);
     120                 :           6 :         bool resched;
     121                 :             : 
     122                 :           6 :         SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_sem, give, sem);
     123                 :             : 
     124         [ +  + ]:           6 :         if (z_sched_wake(&sem->wait_q, 0, NULL)) {
     125                 :             :                 resched = true;
     126                 :             :         } else {
     127         [ -  + ]:           2 :                 sem->count += (sem->count != sem->limit) ? 1U : 0U;
     128                 :           2 :                 resched = sem_handle_poll_events(sem);
     129                 :             :         }
     130                 :             : 
     131         [ -  + ]:           2 :         if (unlikely(resched)) {
     132                 :           4 :                 z_reschedule(lock, key);
     133                 :             :         } else {
     134                 :           2 :                 k_spin_unlock(lock, key);
     135                 :             :         }
     136                 :             : 
     137                 :           6 :         SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_sem, give, sem);
     138                 :           6 : }
     139                 :             : 
     140                 :             : #ifdef CONFIG_USERSPACE
     141                 :             : static inline void z_vrfy_k_sem_give(struct k_sem *sem)
     142                 :             : {
     143                 :             :         K_OOPS(K_SYSCALL_OBJ(sem, K_OBJ_SEM));
     144                 :             :         z_impl_k_sem_give(sem);
     145                 :             : }
     146                 :             : #include <zephyr/syscalls/k_sem_give_mrsh.c>
     147                 :             : #endif /* CONFIG_USERSPACE */
     148                 :             : 
     149                 :           6 : int z_impl_k_sem_take(struct k_sem *sem, k_timeout_t timeout)
     150                 :             : {
     151                 :           6 :         struct k_spinlock *lock = sem_spinlock_get(sem);
     152                 :           6 :         int ret;
     153                 :             : 
     154   [ -  +  -  - ]:           6 :         __ASSERT(((arch_is_in_isr() == false) ||
     155                 :             :                   K_TIMEOUT_EQ(timeout, K_NO_WAIT)), "");
     156                 :             : 
     157                 :           6 :         k_spinlock_key_t key = k_spin_lock(lock);
     158                 :             : 
     159                 :           6 :         SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_sem, take, sem, timeout);
     160                 :             : 
     161         [ +  + ]:           6 :         if (likely(sem->count > 0U)) {
     162                 :           2 :                 sem->count--;
     163                 :           2 :                 k_spin_unlock(lock, key);
     164                 :           2 :                 ret = 0;
     165                 :           2 :                 goto out;
     166                 :             :         }
     167                 :             : 
     168         [ -  + ]:           4 :         if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) {
     169                 :           0 :                 k_spin_unlock(lock, key);
     170                 :           0 :                 ret = -EBUSY;
     171                 :           0 :                 goto out;
     172                 :             :         }
     173                 :             : 
     174                 :           4 :         SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_sem, take, sem, timeout);
     175                 :             : 
     176                 :           4 :         ret = z_pend_curr(lock, key, &sem->wait_q, timeout);
     177                 :             : 
     178                 :           6 : out:
     179                 :           6 :         SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_sem, take, sem, timeout, ret);
     180                 :             : 
     181                 :           6 :         return ret;
     182                 :             : }
     183                 :             : 
     184                 :           0 : void z_impl_k_sem_reset(struct k_sem *sem)
     185                 :             : {
     186                 :           0 :         struct k_spinlock *lock = sem_spinlock_get(sem);
     187                 :           0 :         k_spinlock_key_t key = k_spin_lock(lock);
     188                 :           0 :         bool resched = false;
     189                 :             : 
     190         [ #  # ]:           0 :         while (z_sched_wake(&sem->wait_q, -EAGAIN, NULL)) {
     191                 :             :                 resched = true;
     192                 :             :         }
     193                 :           0 :         sem->count = 0;
     194                 :             : 
     195                 :           0 :         SYS_PORT_TRACING_OBJ_FUNC(k_sem, reset, sem);
     196                 :             : 
     197         [ #  # ]:           0 :         if (resched) {
     198                 :           0 :                 z_reschedule(lock, key);
     199                 :             :         } else {
     200                 :           0 :                 k_spin_unlock(lock, key);
     201                 :             :         }
     202                 :           0 : }
     203                 :             : 
     204                 :             : #ifdef CONFIG_USERSPACE
     205                 :             : static inline int z_vrfy_k_sem_take(struct k_sem *sem, k_timeout_t timeout)
     206                 :             : {
     207                 :             :         K_OOPS(K_SYSCALL_OBJ(sem, K_OBJ_SEM));
     208                 :             :         return z_impl_k_sem_take(sem, timeout);
     209                 :             : }
     210                 :             : #include <zephyr/syscalls/k_sem_take_mrsh.c>
     211                 :             : 
     212                 :             : static inline void z_vrfy_k_sem_reset(struct k_sem *sem)
     213                 :             : {
     214                 :             :         K_OOPS(K_SYSCALL_OBJ(sem, K_OBJ_SEM));
     215                 :             :         z_impl_k_sem_reset(sem);
     216                 :             : }
     217                 :             : #include <zephyr/syscalls/k_sem_reset_mrsh.c>
     218                 :             : 
     219                 :             : static inline unsigned int z_vrfy_k_sem_count_get(struct k_sem *sem)
     220                 :             : {
     221                 :             :         K_OOPS(K_SYSCALL_OBJ(sem, K_OBJ_SEM));
     222                 :             :         return z_impl_k_sem_count_get(sem);
     223                 :             : }
     224                 :             : #include <zephyr/syscalls/k_sem_count_get_mrsh.c>
     225                 :             : 
     226                 :             : #endif /* CONFIG_USERSPACE */
        

Generated by: LCOV version 2.0-1