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> |
| 24 | #include <zephyr/wait_q.h> |
| 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> |
| 28 | #include <zephyr/syscall_handler.h> |
| 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 | |
Anas Nashif | 928af3c | 2019-05-04 10:36:14 -0400 | [diff] [blame] | 41 | 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] | 42 | unsigned int limit) |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 43 | { |
Anas Nashif | 928af3c | 2019-05-04 10:36:14 -0400 | [diff] [blame] | 44 | /* |
| 45 | * Limit cannot be zero and count cannot be greater than limit |
| 46 | */ |
James Harris | b104281 | 2021-03-03 12:02:05 -0800 | [diff] [blame] | 47 | CHECKIF(limit == 0U || limit > K_SEM_MAX_LIMIT || initial_count > limit) { |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 48 | SYS_PORT_TRACING_OBJ_FUNC(k_sem, init, sem, -EINVAL); |
| 49 | |
Anas Nashif | 928af3c | 2019-05-04 10:36:14 -0400 | [diff] [blame] | 50 | return -EINVAL; |
| 51 | } |
| 52 | |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 53 | sem->count = initial_count; |
| 54 | sem->limit = limit; |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 55 | |
| 56 | SYS_PORT_TRACING_OBJ_FUNC(k_sem, init, sem, 0); |
| 57 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 58 | z_waitq_init(&sem->wait_q); |
Luiz Augusto von Dentz | 7d01c5e | 2017-08-21 10:49:29 +0300 | [diff] [blame] | 59 | #if defined(CONFIG_POLL) |
| 60 | sys_dlist_init(&sem->poll_events); |
| 61 | #endif |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 62 | z_object_init(sem); |
Anas Nashif | 928af3c | 2019-05-04 10:36:14 -0400 | [diff] [blame] | 63 | |
| 64 | return 0; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 67 | #ifdef CONFIG_USERSPACE |
Anas Nashif | 928af3c | 2019-05-04 10:36:14 -0400 | [diff] [blame] | 68 | 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] | 69 | unsigned int limit) |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 70 | { |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 71 | Z_OOPS(Z_SYSCALL_OBJ_INIT(sem, K_OBJ_SEM)); |
Anas Nashif | 928af3c | 2019-05-04 10:36:14 -0400 | [diff] [blame] | 72 | return z_impl_k_sem_init(sem, initial_count, limit); |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 73 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 74 | #include <syscalls/k_sem_init_mrsh.c> |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 75 | #endif |
Peter Mitsis | 4540367 | 2016-09-09 14:24:06 -0400 | [diff] [blame] | 76 | |
Andy Ross | 8606fab | 2018-03-26 10:54:40 -0700 | [diff] [blame] | 77 | static inline void handle_poll_events(struct k_sem *sem) |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 78 | { |
| 79 | #ifdef CONFIG_POLL |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 80 | z_handle_obj_poll_events(&sem->poll_events, K_POLL_STATE_SEM_AVAILABLE); |
Adithya Baglody | 4b06621 | 2018-10-16 11:59:12 +0530 | [diff] [blame] | 81 | #else |
| 82 | ARG_UNUSED(sem); |
Benjamin Walsh | acc68c1 | 2017-01-29 18:57:45 -0500 | [diff] [blame] | 83 | #endif |
| 84 | } |
| 85 | |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 86 | void z_impl_k_sem_give(struct k_sem *sem) |
Peter Mitsis | 4540367 | 2016-09-09 14:24:06 -0400 | [diff] [blame] | 87 | { |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 88 | k_spinlock_key_t key = k_spin_lock(&lock); |
Anas Nashif | 390537b | 2020-08-02 23:34:14 -0400 | [diff] [blame] | 89 | struct k_thread *thread; |
Benjamin Walsh | 2e0bf3a | 2017-02-12 16:54:59 -0500 | [diff] [blame] | 90 | |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 91 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_sem, give, sem); |
| 92 | |
Anas Nashif | 390537b | 2020-08-02 23:34:14 -0400 | [diff] [blame] | 93 | thread = z_unpend_first_thread(&sem->wait_q); |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 94 | |
Flavio Ceolin | ea716bf | 2018-09-20 16:30:45 -0700 | [diff] [blame] | 95 | if (thread != NULL) { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 96 | arch_thread_return_value_set(thread, 0); |
Andy Ross | b8ff63e | 2020-01-23 12:55:04 -0800 | [diff] [blame] | 97 | z_ready_thread(thread); |
Andy Ross | 8606fab | 2018-03-26 10:54:40 -0700 | [diff] [blame] | 98 | } else { |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 99 | sem->count += (sem->count != sem->limit) ? 1U : 0U; |
Andy Ross | 8606fab | 2018-03-26 10:54:40 -0700 | [diff] [blame] | 100 | handle_poll_events(sem); |
Benjamin Walsh | 2e0bf3a | 2017-02-12 16:54:59 -0500 | [diff] [blame] | 101 | } |
Peter Mitsis | 4540367 | 2016-09-09 14:24:06 -0400 | [diff] [blame] | 102 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 103 | z_reschedule(&lock, key); |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 104 | |
| 105 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_sem, give, sem); |
Peter Mitsis | 4540367 | 2016-09-09 14:24:06 -0400 | [diff] [blame] | 106 | } |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 107 | |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 108 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 109 | static inline void z_vrfy_k_sem_give(struct k_sem *sem) |
| 110 | { |
| 111 | Z_OOPS(Z_SYSCALL_OBJ(sem, K_OBJ_SEM)); |
| 112 | z_impl_k_sem_give(sem); |
| 113 | } |
| 114 | #include <syscalls/k_sem_give_mrsh.c> |
Andrew Boie | 225e4c0 | 2017-10-12 09:54:26 -0700 | [diff] [blame] | 115 | #endif |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 116 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 117 | 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] | 118 | { |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 119 | int ret = 0; |
| 120 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 121 | __ASSERT(((arch_is_in_isr() == false) || |
| 122 | K_TIMEOUT_EQ(timeout, K_NO_WAIT)), ""); |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 123 | |
Andy Ross | da37a53 | 2018-07-24 14:12:36 -0700 | [diff] [blame] | 124 | k_spinlock_key_t key = k_spin_lock(&lock); |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 125 | |
| 126 | SYS_PORT_TRACING_OBJ_FUNC_ENTER(k_sem, take, sem, timeout); |
| 127 | |
Adithya Baglody | 4b06621 | 2018-10-16 11:59:12 +0530 | [diff] [blame] | 128 | if (likely(sem->count > 0U)) { |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 129 | sem->count--; |
Andy Ross | da37a53 | 2018-07-24 14:12:36 -0700 | [diff] [blame] | 130 | k_spin_unlock(&lock, key); |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 131 | ret = 0; |
| 132 | goto out; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 135 | if (K_TIMEOUT_EQ(timeout, K_NO_WAIT)) { |
Andy Ross | da37a53 | 2018-07-24 14:12:36 -0700 | [diff] [blame] | 136 | k_spin_unlock(&lock, key); |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 137 | ret = -EBUSY; |
| 138 | goto out; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 141 | SYS_PORT_TRACING_OBJ_FUNC_BLOCKING(k_sem, take, sem, timeout); |
| 142 | |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 143 | ret = z_pend_curr(&lock, key, &sem->wait_q, timeout); |
Anas Nashif | b6304e6 | 2018-07-04 08:03:03 -0500 | [diff] [blame] | 144 | |
Anas Nashif | 5076a83 | 2019-11-26 08:56:25 -0500 | [diff] [blame] | 145 | out: |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 146 | SYS_PORT_TRACING_OBJ_FUNC_EXIT(k_sem, take, sem, timeout, ret); |
| 147 | |
Andy Ross | da37a53 | 2018-07-24 14:12:36 -0700 | [diff] [blame] | 148 | return ret; |
Benjamin Walsh | 456c6da | 2016-09-02 18:55:39 -0400 | [diff] [blame] | 149 | } |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 150 | |
James Harris | 53b8179 | 2021-03-04 15:47:27 -0800 | [diff] [blame] | 151 | void z_impl_k_sem_reset(struct k_sem *sem) |
| 152 | { |
| 153 | struct k_thread *thread; |
| 154 | k_spinlock_key_t key = k_spin_lock(&lock); |
| 155 | |
| 156 | while (true) { |
| 157 | thread = z_unpend_first_thread(&sem->wait_q); |
| 158 | if (thread == NULL) { |
| 159 | break; |
| 160 | } |
| 161 | arch_thread_return_value_set(thread, -EAGAIN); |
| 162 | z_ready_thread(thread); |
| 163 | } |
| 164 | sem->count = 0; |
Torbjörn Leksell | fcf2fb6 | 2021-03-26 09:51:03 +0100 | [diff] [blame] | 165 | |
| 166 | SYS_PORT_TRACING_OBJ_FUNC(k_sem, reset, sem); |
| 167 | |
James Harris | 53b8179 | 2021-03-04 15:47:27 -0800 | [diff] [blame] | 168 | handle_poll_events(sem); |
| 169 | |
| 170 | z_reschedule(&lock, key); |
| 171 | } |
| 172 | |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 173 | #ifdef CONFIG_USERSPACE |
Andy Ross | 7832738 | 2020-03-05 15:18:14 -0800 | [diff] [blame] | 174 | 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] | 175 | { |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 176 | Z_OOPS(Z_SYSCALL_OBJ(sem, K_OBJ_SEM)); |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 177 | return z_impl_k_sem_take((struct k_sem *)sem, timeout); |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 178 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 179 | #include <syscalls/k_sem_take_mrsh.c> |
Andrew Boie | fc273c0 | 2017-09-23 12:51:23 -0700 | [diff] [blame] | 180 | |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 181 | static inline void z_vrfy_k_sem_reset(struct k_sem *sem) |
| 182 | { |
| 183 | Z_OOPS(Z_SYSCALL_OBJ(sem, K_OBJ_SEM)); |
| 184 | z_impl_k_sem_reset(sem); |
| 185 | } |
| 186 | #include <syscalls/k_sem_reset_mrsh.c> |
| 187 | |
| 188 | static inline unsigned int z_vrfy_k_sem_count_get(struct k_sem *sem) |
| 189 | { |
| 190 | Z_OOPS(Z_SYSCALL_OBJ(sem, K_OBJ_SEM)); |
| 191 | return z_impl_k_sem_count_get(sem); |
| 192 | } |
| 193 | #include <syscalls/k_sem_count_get_mrsh.c> |
| 194 | |
Andrew Boie | 225e4c0 | 2017-10-12 09:54:26 -0700 | [diff] [blame] | 195 | #endif |