File: atomic.h

package info (click to toggle)
erofs-utils 1.8.10-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 1,128 kB
  • sloc: ansic: 20,839; makefile: 164; sh: 33
file content (43 lines) | stat: -rw-r--r-- 1,154 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
/*
 * Copyright (C) 2024 Alibaba Cloud
 */
#ifndef __EROFS_ATOMIC_H
#define __EROFS_ATOMIC_H

/*
 * Just use GCC/clang built-in functions for now
 * See: https://gcc.gnu.org/onlinedocs/gcc/_005f_005fatomic-Builtins.html
 */
typedef unsigned long erofs_atomic_t;
typedef char erofs_atomic_bool_t;

#define erofs_atomic_read(ptr) ({ \
	typeof(*ptr) __n;    \
	__atomic_load(ptr, &__n, __ATOMIC_RELAXED); \
__n;})

#define erofs_atomic_set(ptr, n) do { \
	typeof(*ptr) __n = (n);    \
	__atomic_store(ptr, &__n, __ATOMIC_RELAXED); \
} while(0)

#define erofs_atomic_set_bit(bit, ptr) do { \
	typeof(*ptr) __n = (1 << bit);    \
	__atomic_or_fetch(ptr, __n, __ATOMIC_ACQ_REL); \
} while(0)

#define erofs_atomic_test_and_set(ptr) \
	__atomic_test_and_set(ptr, __ATOMIC_RELAXED)

#define erofs_atomic_add_return(ptr, i) \
	__atomic_add_fetch(ptr, i, __ATOMIC_RELAXED)

#define erofs_atomic_sub_return(ptr, i) \
	__atomic_sub_fetch(ptr, i, __ATOMIC_RELAXED)

#define erofs_atomic_inc_return(ptr) erofs_atomic_add_return(ptr, 1)

#define erofs_atomic_dec_return(ptr) erofs_atomic_sub_return(ptr, 1)

#endif