Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include "pico/sem.h" |
| 8 | #include "pico/time.h" |
| 9 | |
| 10 | void sem_init(semaphore_t *sem, int16_t initial_permits, int16_t max_permits) { |
| 11 | lock_init(&sem->core, next_striped_spin_lock_num()); |
| 12 | sem->permits = initial_permits; |
| 13 | sem->max_permits = max_permits; |
| 14 | __mem_fence_release(); |
| 15 | } |
| 16 | |
| 17 | int __time_critical_func(sem_available)(semaphore_t *sem) { |
| 18 | return *(volatile typeof(sem->permits) *) &sem->permits; |
| 19 | } |
| 20 | |
| 21 | void __time_critical_func(sem_acquire_blocking)(semaphore_t *sem) { |
| 22 | do { |
| 23 | uint32_t save = spin_lock_blocking(sem->core.spin_lock); |
| 24 | if (sem->permits > 0) { |
| 25 | sem->permits--; |
| 26 | lock_internal_spin_unlock_with_notify(&sem->core, save); |
| 27 | break; |
| 28 | } |
| 29 | lock_internal_spin_unlock_with_wait(&sem->core, save); |
| 30 | } while (true); |
| 31 | } |
| 32 | |
| 33 | bool __time_critical_func(sem_acquire_timeout_ms)(semaphore_t *sem, uint32_t timeout_ms) { |
| 34 | return sem_acquire_block_until(sem, make_timeout_time_ms(timeout_ms)); |
| 35 | } |
| 36 | |
| 37 | bool __time_critical_func(sem_acquire_timeout_us)(semaphore_t *sem, uint32_t timeout_us) { |
| 38 | return sem_acquire_block_until(sem, make_timeout_time_us(timeout_us)); |
| 39 | } |
| 40 | |
| 41 | bool __time_critical_func(sem_acquire_block_until)(semaphore_t *sem, absolute_time_t until) { |
| 42 | do { |
| 43 | uint32_t save = spin_lock_blocking(sem->core.spin_lock); |
| 44 | if (sem->permits > 0) { |
| 45 | sem->permits--; |
| 46 | lock_internal_spin_unlock_with_notify(&sem->core, save); |
| 47 | return true; |
| 48 | } |
| 49 | if (lock_internal_spin_unlock_with_best_effort_wait_or_timeout(&sem->core, save, until)) { |
| 50 | return false; |
| 51 | } |
| 52 | } while (true); |
| 53 | } |
| 54 | |
| 55 | // todo this should really have a blocking variant for when permits are maxed out |
| 56 | bool __time_critical_func(sem_release)(semaphore_t *sem) { |
| 57 | uint32_t save = spin_lock_blocking(sem->core.spin_lock); |
| 58 | int32_t count = sem->permits; |
| 59 | if (count < sem->max_permits) { |
| 60 | sem->permits = (int16_t)(count + 1); |
| 61 | lock_internal_spin_unlock_with_notify(&sem->core, save); |
| 62 | return true; |
| 63 | } else { |
| 64 | spin_unlock(sem->core.spin_lock, save); |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void __time_critical_func(sem_reset)(semaphore_t *sem, int16_t permits) { |
| 70 | assert(permits >= 0 && permits <= sem->max_permits); |
| 71 | uint32_t save = spin_lock_blocking(sem->core.spin_lock); |
| 72 | if (permits > sem->permits) { |
| 73 | sem->permits = permits; |
| 74 | lock_internal_spin_unlock_with_notify(&sem->core, save); |
| 75 | } else { |
| 76 | sem->permits = permits; |
| 77 | spin_unlock(sem->core.spin_lock, save); |
| 78 | } |
| 79 | } |