xref: /aosp_15_r20/external/erofs-utils/include/erofs/atomic.h (revision 33b1fccf6a0fada2c2875d400ed01119b7676ee5)
1 /* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
2 /*
3  * Copyright (C) 2024 Alibaba Cloud
4  */
5 #ifndef __EROFS_ATOMIC_H
6 #define __EROFS_ATOMIC_H
7 
8 /*
9  * Just use GCC/clang built-in functions for now
10  * See: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html
11  */
12 typedef unsigned long erofs_atomic_t;
13 typedef char erofs_atomic_bool_t;
14 
15 #define erofs_atomic_read(ptr) ({ \
16 	typeof(*ptr) __n;    \
17 	__atomic_load(ptr, &__n, __ATOMIC_RELAXED); \
18 __n;})
19 
20 #define erofs_atomic_set(ptr, n) do { \
21 	typeof(*ptr) __n = (n);    \
22 	__atomic_store(ptr, &__n, __ATOMIC_RELAXED); \
23 } while(0)
24 
25 #define erofs_atomic_test_and_set(ptr) \
26 	__atomic_test_and_set(ptr, __ATOMIC_RELAXED)
27 
28 #define erofs_atomic_add_return(ptr, i) \
29 	__atomic_add_fetch(ptr, i, __ATOMIC_RELAXED)
30 
31 #define erofs_atomic_sub_return(ptr, i) \
32 	__atomic_sub_fetch(ptr, i, __ATOMIC_RELAXED)
33 
34 #define erofs_atomic_inc_return(ptr) erofs_atomic_add_return(ptr, 1)
35 
36 #define erofs_atomic_dec_return(ptr) erofs_atomic_sub_return(ptr, 1)
37 
38 #endif
39