Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2010-2016 Wind River Systems, Inc. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @file |
| 9 | * |
Anas Nashif | cb888e6 | 2016-12-18 09:42:55 -0500 | [diff] [blame] | 10 | * @brief Kernel semaphore object. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 11 | * |
| 12 | * The semaphores are of the 'counting' type, i.e. each 'give' operation will |
Ramakrishna Pallala | c44046a | 2017-10-25 09:19:25 -0400 | [diff] [blame] | 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. |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 18 | */ |
| 19 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 20 | #include <zephyr/kernel.h> |
| 21 | #include <zephyr/kernel_structs.h> |
Anas Nashif | 4d994af | 2021-04-18 23:24:40 -0400 | [diff] [blame] | 22 | |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 23 | #include <zephyr/toolchain.h> |
Anas Nashif | 8634c3b | 2023-08-29 17:03:12 +0000 | [diff] [blame] | 24 | #include <wait_q.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 25 | #include <zephyr/sys/dlist.h> |
Benjamin Walsh | b4b108d | 2016-10-13 10:31:48 -0400 | [diff] [blame] | 26 | #include <ksched.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 27 | #include <zephyr/init.h> |
Anas Nashif | 4e39617 | 2023-09-26 22:46:01 +0000 | [diff] [blame] | 28 | #include <zephyr/internal/syscall_handler.h> |
Gerard Marull-Paretas | cffefc8 | 2022-05-06 11:04:23 +0200 | [diff] [blame] | 29 | #include <zephyr/tracing/tracing.h> |
| 30 | #include <zephyr/sys/check.h> |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 31 | |
Andy Ross | da37a53 | 2018-07-24 14:12:36 -0700 | [diff] [blame] | 32 | /* We use a system-wide lock to synchronize semaphores, which has |
| 33 | * unfortunate performance impact vs. using a per-object lock |
| 34 | * (semaphores are *very* widely used). But per-object locks require |
| 35 | * significant extra RAM. A properly spin-aware semaphore |
| 36 | * implementation would spin on atomic access to the count variable, |
| 37 | * and not a spinlock per se. Useful optimization for the future... |
| 38 | */ |
| 39 | static struct k_spinlock lock; |
| 40 | |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 41 | #ifdef CONFIG_OBJ_CORE_SEM |
| 42 | static struct k_obj_type obj_type_sem; |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 43 | #endif /* CONFIG_OBJ_CORE_SEM */ |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 44 | |
Anas Nashif | 928af3c | 2019-05-04 10:36:14 -0400 | [diff] [blame] | 45 | int z_impl_k_sem_init(struct k_sem *sem, unsigned int initial_count, |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 46 | unsigned int limit) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 47 | { |
Anas Nashif | 928af3c | 2019-05-04 10:36:14 -0400 | [diff] [blame] | 48 | /* |
| 49 | * Limit cannot be zero and count cannot be greater than limit |
| 50 | */ |
Flavio Ceolin | 68ea73a | 2024-04-27 21:56:17 -0700 | [diff] [blame] | 51 | CHECKIF(limit == 0U || initial_count > limit) { |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 52 | SYS_PORT_TRACING_OBJ_FUNC(k_sem, init, sem, -EINVAL); |
| 53 | |
Anas Nashif | 928af3c | 2019-05-04 10:36:14 -0400 | [diff] [blame] | 54 | return -EINVAL; |
| 55 | } |
| 56 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 57 | sem->count = initial_count; |
| 58 | sem->limit = limit; |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 59 | |
| 60 | SYS_PORT_TRACING_OBJ_FUNC(k_sem, init, sem, 0); |
| 61 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 62 | z_waitq_init(&sem->wait_q); |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 63 | #if defined(CONFIG_POLL) |
| 64 | sys_dlist_init(&sem->poll_events); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 65 | #endif /* CONFIG_POLL */ |
Anas Nashif | c91cad7 | 2023-09-26 21:32:13 +0000 | [diff] [blame] | 66 | k_object_init(sem); |
Anas Nashif | 928af3c | 2019-05-04 10:36:14 -0400 | [diff] [blame] | 67 | |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 68 | #ifdef CONFIG_OBJ_CORE_SEM |
| 69 | k_obj_core_init_and_link(K_OBJ_CORE(sem), &obj_type_sem); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 70 | #endif /* CONFIG_OBJ_CORE_SEM */ |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 71 | |
Anas Nashif | 928af3c | 2019-05-04 10:36:14 -0400 | [diff] [blame] | 72 | return 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 73 | } |
| 74 | |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 75 | #ifdef CONFIG_USERSPACE |
Anas Nashif | 928af3c | 2019-05-04 10:36:14 -0400 | [diff] [blame] | 76 | int z_vrfy_k_sem_init(struct k_sem *sem, unsigned int initial_count, |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 77 | unsigned int limit) |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 78 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 79 | K_OOPS(K_SYSCALL_OBJ_INIT(sem, K_OBJ_SEM)); |
Anas Nashif | 928af3c | 2019-05-04 10:36:14 -0400 | [diff] [blame] | 80 | return z_impl_k_sem_init(sem, initial_count, limit); |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 81 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 82 | #include <syscalls/k_sem_init_mrsh.c> |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 83 | #endif /* CONFIG_USERSPACE */ |
Peter Mitsis | 4540367 | 2016-09-09 14:24:06 -0400 | [diff] [blame] | 84 | |
Peter Mitsis | f444fca | 2023-04-26 16:27:44 -0400 | [diff] [blame] | 85 | static inline bool handle_poll_events(struct k_sem *sem) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 86 | { |
| 87 | #ifdef CONFIG_POLL |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 88 | z_handle_obj_poll_events(&sem->poll_events, K_POLL_STATE_SEM_AVAILABLE); |
Peter Mitsis | f444fca | 2023-04-26 16:27:44 -0400 | [diff] [blame] | 89 | return true; |
Adithya Baglody | 4b06621 | 2018-10-16 11:59:12 +0530 | [diff] [blame] | 90 | #else |
| 91 | ARG_UNUSED(sem); |
Peter Mitsis | f444fca | 2023-04-26 16:27:44 -0400 | [diff] [blame] | 92 | return false; |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 93 | #endif /* CONFIG_POLL */ |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 94 | } |
| 95 | |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 96 | void z_impl_k_sem_give(struct k_sem *sem) |
Peter Mitsis | 4540367 | 2016-09-09 14:24:06 -0400 | [diff] [blame] | 97 | { |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 98 | k_spinlock_key_t key = k_spin_lock(&lock); |
Anas Nashif | 390537b | 2020-08-02 23:34:14 -0400 | [diff] [blame] | 99 | struct k_thread *thread; |
Peter Mitsis | f444fca | 2023-04-26 16:27:44 -0400 | [diff] [blame] | 100 | bool resched = true; |
Benjamin Walsh | 2e0bf3a | 2017-02-12 16:54:59 -0500 | [diff] [blame] | 101 | |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 102 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_sem, give, sem); |
| 103 | |
Anas Nashif | 390537b | 2020-08-02 23:34:14 -0400 | [diff] [blame] | 104 | thread = z_unpend_first_thread(&sem->wait_q); |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 105 | |
Flavio Ceolin | ea716bf | 2018-09-20 16:30:45 -0700 | [diff] [blame] | 106 | if (thread != NULL) { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 107 | arch_thread_return_value_set(thread, 0); |
Andy Ross | b8ff63e | 2020-01-23 12:55:04 -0800 | [diff] [blame] | 108 | z_ready_thread(thread); |
Andy Ross | 8606fab | 2018-03-26 10:54:40 -0700 | [diff] [blame] | 109 | } else { |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 110 | sem->count += (sem->count != sem->limit) ? 1U : 0U; |
Peter Mitsis | f444fca | 2023-04-26 16:27:44 -0400 | [diff] [blame] | 111 | resched = handle_poll_events(sem); |
Benjamin Walsh | 2e0bf3a | 2017-02-12 16:54:59 -0500 | [diff] [blame] | 112 | } |
Peter Mitsis | 4540367 | 2016-09-09 14:24:06 -0400 | [diff] [blame] | 113 | |
Peter Mitsis | f444fca | 2023-04-26 16:27:44 -0400 | [diff] [blame] | 114 | if (resched) { |
| 115 | z_reschedule(&lock, key); |
| 116 | } else { |
| 117 | k_spin_unlock(&lock, key); |
| 118 | } |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 119 | |
| 120 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_sem, give, sem); |
Peter Mitsis | 4540367 | 2016-09-09 14:24:06 -0400 | [diff] [blame] | 121 | } |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 122 | |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 123 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 124 | static inline void z_vrfy_k_sem_give(struct k_sem *sem) |
| 125 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 126 | K_OOPS(K_SYSCALL_OBJ(sem, K_OBJ_SEM)); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 127 | z_impl_k_sem_give(sem); |
| 128 | } |
| 129 | #include <syscalls/k_sem_give_mrsh.c> |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 130 | #endif /* CONFIG_USERSPACE */ |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 131 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 132 | int z_impl_k_sem_take(struct k_sem *sem, k_timeout_t timeout) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 133 | { |
Hess Nathan | 7659cfd | 2024-04-29 16:31:47 +0200 | [diff] [blame] | 134 | int ret; |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 135 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 136 | __ASSERT(((arch_is_in_isr() == false) || |
| 137 | K_TIMEOUT_EQ(timeout, K_NO_WAIT)), ""); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 138 | |
Andy Ross | da37a53 | 2018-07-24 14:12:36 -0700 | [diff] [blame] | 139 | k_spinlock_key_t key = k_spin_lock(&lock); |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 140 | |
| 141 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_sem, take, sem, timeout); |
| 142 | |
Adithya Baglody | 4b06621 | 2018-10-16 11:59:12 +0530 | [diff] [blame] | 143 | if (likely(sem->count > 0U)) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 144 | sem->count--; |
Andy Ross | da37a53 | 2018-07-24 14:12:36 -0700 | [diff] [blame] | 145 | k_spin_unlock(&lock, key); |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 146 | ret = 0; |
| 147 | goto out; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 150 | if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) { |
Andy Ross | da37a53 | 2018-07-24 14:12:36 -0700 | [diff] [blame] | 151 | k_spin_unlock(&lock, key); |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 152 | ret = -EBUSY; |
| 153 | goto out; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 156 | SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_sem, take, sem, timeout); |
| 157 | |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 158 | ret = z_pend_curr(&lock, key, &sem->wait_q, timeout); |
Anas Nashif | b6304e6 | 2018-07-04 08:03:03 -0500 | [diff] [blame] | 159 | |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 160 | out: |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 161 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_sem, take, sem, timeout, ret); |
| 162 | |
Andy Ross | da37a53 | 2018-07-24 14:12:36 -0700 | [diff] [blame] | 163 | return ret; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 164 | } |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 165 | |
James Harris | 53b8179 | 2021-03-04 15:47:27 -0800 | [diff] [blame] | 166 | void z_impl_k_sem_reset(struct k_sem *sem) |
| 167 | { |
| 168 | struct k_thread *thread; |
| 169 | k_spinlock_key_t key = k_spin_lock(&lock); |
| 170 | |
| 171 | while (true) { |
| 172 | thread = z_unpend_first_thread(&sem->wait_q); |
| 173 | if (thread == NULL) { |
| 174 | break; |
| 175 | } |
| 176 | arch_thread_return_value_set(thread, -EAGAIN); |
| 177 | z_ready_thread(thread); |
| 178 | } |
| 179 | sem->count = 0; |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 180 | |
| 181 | SYS_PORT_TRACING_OBJ_FUNC(k_sem, reset, sem); |
| 182 | |
James Harris | 53b8179 | 2021-03-04 15:47:27 -0800 | [diff] [blame] | 183 | handle_poll_events(sem); |
| 184 | |
| 185 | z_reschedule(&lock, key); |
| 186 | } |
| 187 | |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 188 | #ifdef CONFIG_USERSPACE |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 189 | static inline int z_vrfy_k_sem_take(struct k_sem *sem, k_timeout_t timeout) |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 190 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 191 | K_OOPS(K_SYSCALL_OBJ(sem, K_OBJ_SEM)); |
Hess Nathan | e05c4a8 | 2024-05-07 13:22:50 +0200 | [diff] [blame] | 192 | return z_impl_k_sem_take(sem, timeout); |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 193 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 194 | #include <syscalls/k_sem_take_mrsh.c> |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 195 | |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 196 | static inline void z_vrfy_k_sem_reset(struct k_sem *sem) |
| 197 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 198 | K_OOPS(K_SYSCALL_OBJ(sem, K_OBJ_SEM)); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 199 | z_impl_k_sem_reset(sem); |
| 200 | } |
| 201 | #include <syscalls/k_sem_reset_mrsh.c> |
| 202 | |
| 203 | static inline unsigned int z_vrfy_k_sem_count_get(struct k_sem *sem) |
| 204 | { |
Anas Nashif | a08bfeb | 2023-09-27 11:20:28 +0000 | [diff] [blame] | 205 | K_OOPS(K_SYSCALL_OBJ(sem, K_OBJ_SEM)); |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 206 | return z_impl_k_sem_count_get(sem); |
| 207 | } |
| 208 | #include <syscalls/k_sem_count_get_mrsh.c> |
| 209 | |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 210 | #endif /* CONFIG_USERSPACE */ |
Peter Mitsis | 6df8efe | 2023-05-11 14:06:46 -0400 | [diff] [blame] | 211 | |
| 212 | #ifdef CONFIG_OBJ_CORE_SEM |
| 213 | static int init_sem_obj_core_list(void) |
| 214 | { |
| 215 | /* Initialize semaphore object type */ |
| 216 | |
| 217 | z_obj_type_init(&obj_type_sem, K_OBJ_TYPE_SEM_ID, |
| 218 | offsetof(struct k_sem, obj_core)); |
| 219 | |
| 220 | /* Initialize and link statically defined semaphores */ |
| 221 | |
| 222 | STRUCT_SECTION_FOREACH(k_sem, sem) { |
| 223 | k_obj_core_init_and_link(K_OBJ_CORE(sem), &obj_type_sem); |
| 224 | } |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | SYS_INIT(init_sem_obj_core_list, PRE_KERNEL_1, |
| 230 | CONFIG_KERNEL_INIT_PRIORITY_OBJECTS); |
Simon Hein | bcd1d19 | 2024-03-08 12:00:10 +0100 | [diff] [blame] | 231 | #endif /* CONFIG_OBJ_CORE_SEM */ |