|
| 1 | +// Copyright 2024 The Apache Software Foundation. All rights reserved. |
| 2 | +// Use of this source code is governed by the Apache License, Version 2.0 |
| 3 | +// that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// This file is an internal atomic implementation, use butil/atomicops.h instead. |
| 6 | +// RISC-V architecture specific atomic operations implementation using GCC intrinsics. |
| 7 | + |
| 8 | +#ifndef BUTIL_ATOMICOPS_INTERNALS_RISCV_GCC_H_ |
| 9 | +#define BUTIL_ATOMICOPS_INTERNALS_RISCV_GCC_H_ |
| 10 | + |
| 11 | +namespace butil { |
| 12 | +namespace subtle { |
| 13 | + |
| 14 | +inline void MemoryBarrier() { |
| 15 | + __asm__ __volatile__ ("fence" ::: "memory"); // NOLINT |
| 16 | +} |
| 17 | + |
| 18 | +// RISC-V atomic operations using GCC built-in functions |
| 19 | +// These are implemented using the standard GCC atomic built-ins which |
| 20 | +// are supported on RISC-V since GCC 7.1+ |
| 21 | + |
| 22 | +inline Atomic32 NoBarrier_CompareAndSwap(volatile Atomic32* ptr, |
| 23 | + Atomic32 old_value, |
| 24 | + Atomic32 new_value) { |
| 25 | + Atomic32 prev_value; |
| 26 | + do { |
| 27 | + if (__sync_bool_compare_and_swap(ptr, old_value, new_value)) |
| 28 | + return old_value; |
| 29 | + prev_value = *ptr; |
| 30 | + } while (prev_value == old_value); |
| 31 | + return prev_value; |
| 32 | +} |
| 33 | + |
| 34 | +inline Atomic32 NoBarrier_AtomicExchange(volatile Atomic32* ptr, |
| 35 | + Atomic32 new_value) { |
| 36 | + Atomic32 old_value; |
| 37 | + do { |
| 38 | + old_value = *ptr; |
| 39 | + } while (!__sync_bool_compare_and_swap(ptr, old_value, new_value)); |
| 40 | + return old_value; |
| 41 | +} |
| 42 | + |
| 43 | +inline Atomic32 NoBarrier_AtomicIncrement(volatile Atomic32* ptr, |
| 44 | + Atomic32 increment) { |
| 45 | + return Barrier_AtomicIncrement(ptr, increment); |
| 46 | +} |
| 47 | + |
| 48 | +inline Atomic32 Barrier_AtomicIncrement(volatile Atomic32* ptr, |
| 49 | + Atomic32 increment) { |
| 50 | + for (;;) { |
| 51 | + // Atomic exchange the old value with an incremented one. |
| 52 | + Atomic32 old_value = *ptr; |
| 53 | + Atomic32 new_value = old_value + increment; |
| 54 | + if (__sync_bool_compare_and_swap(ptr, old_value, new_value)) { |
| 55 | + // The exchange took place as expected. |
| 56 | + return new_value; |
| 57 | + } |
| 58 | + // Otherwise, *ptr changed mid-loop and we need to retry. |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +inline Atomic32 Acquire_CompareAndSwap(volatile Atomic32* ptr, |
| 63 | + Atomic32 old_value, |
| 64 | + Atomic32 new_value) { |
| 65 | + // Since NoBarrier_CompareAndSwap uses __sync_bool_compare_and_swap, which |
| 66 | + // is a full memory barrier, none is needed here or below in Release. |
| 67 | + return NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
| 68 | +} |
| 69 | + |
| 70 | +inline Atomic32 Release_CompareAndSwap(volatile Atomic32* ptr, |
| 71 | + Atomic32 old_value, |
| 72 | + Atomic32 new_value) { |
| 73 | + return NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
| 74 | +} |
| 75 | + |
| 76 | +inline void NoBarrier_Store(volatile Atomic32* ptr, Atomic32 value) { |
| 77 | + *ptr = value; |
| 78 | +} |
| 79 | + |
| 80 | +inline void Acquire_Store(volatile Atomic32* ptr, Atomic32 value) { |
| 81 | + *ptr = value; |
| 82 | + MemoryBarrier(); |
| 83 | +} |
| 84 | + |
| 85 | +inline void Release_Store(volatile Atomic32* ptr, Atomic32 value) { |
| 86 | + MemoryBarrier(); |
| 87 | + *ptr = value; |
| 88 | +} |
| 89 | + |
| 90 | +inline Atomic32 NoBarrier_Load(volatile const Atomic32* ptr) { |
| 91 | + return *ptr; |
| 92 | +} |
| 93 | + |
| 94 | +inline Atomic32 Acquire_Load(volatile const Atomic32* ptr) { |
| 95 | + Atomic32 value = *ptr; |
| 96 | + MemoryBarrier(); |
| 97 | + return value; |
| 98 | +} |
| 99 | + |
| 100 | +inline Atomic32 Release_Load(volatile const Atomic32* ptr) { |
| 101 | + MemoryBarrier(); |
| 102 | + return *ptr; |
| 103 | +} |
| 104 | + |
| 105 | +// 64-bit versions of the operations. |
| 106 | +// See the 32-bit versions for comments. |
| 107 | + |
| 108 | +inline Atomic64 NoBarrier_CompareAndSwap(volatile Atomic64* ptr, |
| 109 | + Atomic64 old_value, |
| 110 | + Atomic64 new_value) { |
| 111 | + Atomic64 prev_value; |
| 112 | + do { |
| 113 | + if (__sync_bool_compare_and_swap(ptr, old_value, new_value)) |
| 114 | + return old_value; |
| 115 | + prev_value = *ptr; |
| 116 | + } while (prev_value == old_value); |
| 117 | + return prev_value; |
| 118 | +} |
| 119 | + |
| 120 | +inline Atomic64 NoBarrier_AtomicExchange(volatile Atomic64* ptr, |
| 121 | + Atomic64 new_value) { |
| 122 | + Atomic64 old_value; |
| 123 | + do { |
| 124 | + old_value = *ptr; |
| 125 | + } while (!__sync_bool_compare_and_swap(ptr, old_value, new_value)); |
| 126 | + return old_value; |
| 127 | +} |
| 128 | + |
| 129 | +inline Atomic64 NoBarrier_AtomicIncrement(volatile Atomic64* ptr, |
| 130 | + Atomic64 increment) { |
| 131 | + return Barrier_AtomicIncrement(ptr, increment); |
| 132 | +} |
| 133 | + |
| 134 | +inline Atomic64 Barrier_AtomicIncrement(volatile Atomic64* ptr, |
| 135 | + Atomic64 increment) { |
| 136 | + for (;;) { |
| 137 | + // Atomic exchange the old value with an incremented one. |
| 138 | + Atomic64 old_value = *ptr; |
| 139 | + Atomic64 new_value = old_value + increment; |
| 140 | + if (__sync_bool_compare_and_swap(ptr, old_value, new_value)) { |
| 141 | + // The exchange took place as expected. |
| 142 | + return new_value; |
| 143 | + } |
| 144 | + // Otherwise, *ptr changed mid-loop and we need to retry. |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +inline Atomic64 Acquire_CompareAndSwap(volatile Atomic64* ptr, |
| 149 | + Atomic64 old_value, |
| 150 | + Atomic64 new_value) { |
| 151 | + return NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
| 152 | +} |
| 153 | + |
| 154 | +inline Atomic64 Release_CompareAndSwap(volatile Atomic64* ptr, |
| 155 | + Atomic64 old_value, |
| 156 | + Atomic64 new_value) { |
| 157 | + return NoBarrier_CompareAndSwap(ptr, old_value, new_value); |
| 158 | +} |
| 159 | + |
| 160 | +inline void NoBarrier_Store(volatile Atomic64* ptr, Atomic64 value) { |
| 161 | + *ptr = value; |
| 162 | +} |
| 163 | + |
| 164 | +inline void Acquire_Store(volatile Atomic64* ptr, Atomic64 value) { |
| 165 | + *ptr = value; |
| 166 | + MemoryBarrier(); |
| 167 | +} |
| 168 | + |
| 169 | +inline void Release_Store(volatile Atomic64* ptr, Atomic64 value) { |
| 170 | + MemoryBarrier(); |
| 171 | + *ptr = value; |
| 172 | +} |
| 173 | + |
| 174 | +inline Atomic64 NoBarrier_Load(volatile const Atomic64* ptr) { |
| 175 | + return *ptr; |
| 176 | +} |
| 177 | + |
| 178 | +inline Atomic64 Acquire_Load(volatile const Atomic64* ptr) { |
| 179 | + Atomic64 value = *ptr; |
| 180 | + MemoryBarrier(); |
| 181 | + return value; |
| 182 | +} |
| 183 | + |
| 184 | +inline Atomic64 Release_Load(volatile const Atomic64* ptr) { |
| 185 | + MemoryBarrier(); |
| 186 | + return *ptr; |
| 187 | +} |
| 188 | + |
| 189 | +} // namespace butil::subtle |
| 190 | +} // namespace butil |
| 191 | + |
| 192 | +#endif // BUTIL_ATOMICOPS_INTERNALS_RISCV_GCC_H_ |
0 commit comments