1 |
13 |
serginhofr |
/* Low-level functions for atomic operations. RISC-V version.
|
2 |
|
|
Copyright (C) 2011-2014 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
Contributed by Andrew Waterman (waterman@cs.berkeley.edu) at UC Berkeley.
|
5 |
|
|
|
6 |
|
|
This file is part of the GNU C Library.
|
7 |
|
|
|
8 |
|
|
The GNU C Library is free software; you can redistribute it and/or
|
9 |
|
|
modify it under the terms of the GNU Lesser General Public
|
10 |
|
|
License as published by the Free Software Foundation; either
|
11 |
|
|
version 2.1 of the License, or (at your option) any later version.
|
12 |
|
|
|
13 |
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
14 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
16 |
|
|
Lesser General Public License for more details.
|
17 |
|
|
|
18 |
|
|
You should have received a copy of the GNU Lesser General Public
|
19 |
|
|
License along with the GNU C Library; if not, write to the Free
|
20 |
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
21 |
|
|
02111-1307 USA. */
|
22 |
|
|
|
23 |
|
|
#ifndef _RISCV_BITS_ATOMIC_H
|
24 |
|
|
#define _RISCV_BITS_ATOMIC_H 1
|
25 |
|
|
|
26 |
|
|
#include <inttypes.h>
|
27 |
|
|
|
28 |
|
|
typedef int32_t atomic32_t;
|
29 |
|
|
typedef uint32_t uatomic32_t;
|
30 |
|
|
typedef int_fast32_t atomic_fast32_t;
|
31 |
|
|
typedef uint_fast32_t uatomic_fast32_t;
|
32 |
|
|
|
33 |
|
|
typedef int64_t atomic64_t;
|
34 |
|
|
typedef uint64_t uatomic64_t;
|
35 |
|
|
typedef int_fast64_t atomic_fast64_t;
|
36 |
|
|
typedef uint_fast64_t uatomic_fast64_t;
|
37 |
|
|
|
38 |
|
|
typedef intptr_t atomicptr_t;
|
39 |
|
|
typedef uintptr_t uatomicptr_t;
|
40 |
|
|
typedef intmax_t atomic_max_t;
|
41 |
|
|
typedef uintmax_t uatomic_max_t;
|
42 |
|
|
|
43 |
|
|
#ifdef __riscv_atomic
|
44 |
|
|
|
45 |
|
|
#ifdef __riscv64
|
46 |
|
|
# define __HAVE_64B_ATOMICS 1
|
47 |
|
|
#else
|
48 |
|
|
# define __HAVE_64B_ATOMICS 0
|
49 |
|
|
#endif
|
50 |
|
|
|
51 |
|
|
#define USE_ATOMIC_COMPILER_BUILTINS 1
|
52 |
|
|
|
53 |
|
|
#define asm_amo(which, ordering, mem, value) ({ \
|
54 |
|
|
typeof(*mem) __tmp; \
|
55 |
|
|
if (sizeof(__tmp) == 4) \
|
56 |
|
|
asm volatile (which ".w" ordering "\t%0, %z2, %1" \
|
57 |
|
|
: "=r"(__tmp), "+A"(*(mem)) \
|
58 |
|
|
: "rJ"(value)); \
|
59 |
|
|
else if (sizeof(__tmp) == 8) \
|
60 |
|
|
asm volatile (which ".d" ordering "\t%0, %z2, %1" \
|
61 |
|
|
: "=r"(__tmp), "+A"(*(mem)) \
|
62 |
|
|
: "rJ"(value)); \
|
63 |
|
|
else \
|
64 |
|
|
abort(); \
|
65 |
|
|
__tmp; })
|
66 |
|
|
|
67 |
|
|
/* Atomic compare and exchange. */
|
68 |
|
|
|
69 |
|
|
#define atomic_cas(ordering, mem, newval, oldval) ({ \
|
70 |
|
|
typeof(*mem) __tmp; \
|
71 |
|
|
int __tmp2; \
|
72 |
|
|
if (sizeof(__tmp) == 4) \
|
73 |
|
|
asm volatile ("1: lr.w" ordering "\t%0, %2\n" \
|
74 |
|
|
"bne\t%0, %z4, 1f\n" \
|
75 |
|
|
"sc.w" ordering "\t%1, %z3, %2\n" \
|
76 |
|
|
"bnez\t%1, 1b\n" \
|
77 |
|
|
"1:" \
|
78 |
|
|
: "=&r"(__tmp), "=&r"(__tmp2), "+A"(*(mem)) \
|
79 |
|
|
: "rJ"(newval), "rJ"(oldval)); \
|
80 |
|
|
else if (sizeof(__tmp) == 8) \
|
81 |
|
|
asm volatile ("1: lr.d" ordering "\t%0, %2\n" \
|
82 |
|
|
"bne\t%0, %z4, 1f\n" \
|
83 |
|
|
"sc.d" ordering "\t%1, %z3, %2\n" \
|
84 |
|
|
"bnez\t%1, 1b\n" \
|
85 |
|
|
"1:" \
|
86 |
|
|
: "=&r"(__tmp), "=&r"(__tmp2), "+A"(*(mem)) \
|
87 |
|
|
: "rJ"(newval), "rJ"(oldval)); \
|
88 |
|
|
else \
|
89 |
|
|
abort(); \
|
90 |
|
|
__tmp; })
|
91 |
|
|
|
92 |
|
|
#define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
|
93 |
|
|
atomic_cas(".aq", mem, newval, oldval)
|
94 |
|
|
|
95 |
|
|
#define atomic_compare_and_exchange_val_rel(mem, newval, oldval) \
|
96 |
|
|
atomic_cas(".rl", mem, newval, oldval)
|
97 |
|
|
|
98 |
|
|
/* Atomic exchange (without compare). */
|
99 |
|
|
|
100 |
|
|
#define atomic_exchange_acq(mem, value) asm_amo("amoswap", ".aq", mem, value)
|
101 |
|
|
#define atomic_exchange_rel(mem, value) asm_amo("amoswap", ".rl", mem, value)
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
/* Atomically add value and return the previous (unincremented) value. */
|
105 |
|
|
|
106 |
|
|
#define atomic_exchange_and_add(mem, value) asm_amo("amoadd", "", mem, value)
|
107 |
|
|
|
108 |
|
|
#define atomic_max(mem, value) asm_amo("amomaxu", "", mem, value)
|
109 |
|
|
#define atomic_min(mem, value) asm_amo("amominu", "", mem, value)
|
110 |
|
|
|
111 |
|
|
#define atomic_bit_test_set(mem, bit) \
|
112 |
|
|
({ typeof(*mem) __mask = (typeof(*mem))1 << (bit); \
|
113 |
|
|
asm_amo("amoor", "", mem, __mask) & __mask; })
|
114 |
|
|
|
115 |
|
|
#define atomic_full_barrier() __sync_synchronize()
|
116 |
|
|
|
117 |
|
|
#define catomic_exchange_and_add(mem, value) \
|
118 |
|
|
atomic_exchange_and_add(mem, value)
|
119 |
|
|
#define catomic_max(mem, value) atomic_max(mem, value)
|
120 |
|
|
|
121 |
|
|
#else /* __riscv_atomic */
|
122 |
|
|
|
123 |
|
|
#define __HAVE_64B_ATOMICS 0
|
124 |
|
|
#define USE_ATOMIC_COMPILER_BUILTINS 0
|
125 |
|
|
|
126 |
|
|
#endif /* !__riscv_atomic */
|
127 |
|
|
|
128 |
|
|
#endif /* bits/atomic.h */
|