1 /*
2  * Copyright (c) 2024 Raspberry Pi (Trading) Ltd.
3  * Copyright (c) 2024 Stephen Street (stephen@redrocketcomputing.com).
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef __STDATOMIC_H
9 #define __STDATOMIC_H
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 /** \file stdatomic.h
16  * \defgroup pico_atomic pico_atomic
17  *
18  * \brief Helper implementations for C11 atomics
19  *
20  * \if rp2040_specific
21  * On RP2040 a spin lock is used as protection for all atomic operations, since there is no C library support.
22  * \endif
23  *
24  * \if rp2350_specific
25  * On RP2350 the C-library provides implementations for all 1-byte, 2-byte and 4-byte atomics using processor
26  * exclusive operations. This library provides a spin-lock protected version for arbitrary-sized atomics (including 64-bit).
27  * \endif
28 */
29 #include <stdint.h>
30 #include_next <stdatomic.h>
31 
32 // needed for PICO_C_COMPILER_IS_GNU
33 #include "pico.h"
34 
35 #if PICO_RP2040 && PICO_C_COMPILER_IS_GNU
36 // on GNU without exclusive instructions these don't get routed thru _1 functions for some reason
37 #undef atomic_flag_test_and_set
38 #undef atomic_flag_test_and_set_explicit
39 
40 extern _Bool __atomic_test_and_set_c(volatile void *mem, int model);
41 
42 #define atomic_flag_test_and_set(PTR) __atomic_test_and_set_c((PTR), __ATOMIC_SEQ_CST)
43 #define atomic_flag_test_and_set_explicit(PTR, MO) __atomic_test_and_set_c((PTR), (MO))
44 #endif
45 
46 #ifdef __cplusplus
47 }
48 #endif
49 #endif
50