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/critical_section.h" |
| 8 | |
| 9 | #if !PICO_NO_HARDWARE |
| 10 | static_assert(sizeof(critical_section_t) == 8, ""); |
| 11 | #endif |
| 12 | |
| 13 | void critical_section_init(critical_section_t *crit_sec) { |
| 14 | critical_section_init_with_lock_num(crit_sec, (uint)spin_lock_claim_unused(true)); |
| 15 | } |
| 16 | |
| 17 | void critical_section_init_with_lock_num(critical_section_t *crit_sec, uint lock_num) { |
| 18 | crit_sec->spin_lock = spin_lock_instance(lock_num); |
| 19 | __mem_fence_release(); |
| 20 | } |
| 21 | |
| 22 | void critical_section_deinit(critical_section_t *crit_sec) { |
| 23 | spin_lock_unclaim(spin_lock_get_num(crit_sec->spin_lock)); |
| 24 | #ifndef NDEBUG |
| 25 | crit_sec->spin_lock = (spin_lock_t *)-1; |
| 26 | #endif |
| 27 | } |