|
Packit Service |
82fcde |
/* Low-level functions for atomic operations. RISC-V version.
|
|
Packit Service |
82fcde |
Copyright (C) 2014-2018 Free Software Foundation, Inc.
|
|
Packit Service |
82fcde |
This file is part of the GNU C Library.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
82fcde |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License as published by the Free Software Foundation; either
|
|
Packit Service |
82fcde |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
82fcde |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
82fcde |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
82fcde |
Lesser General Public License for more details.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License along with the GNU C Library. If not, see
|
|
Packit Service |
82fcde |
<http://www.gnu.org/licenses/>. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#ifndef _LINUX_RISCV_BITS_ATOMIC_H
|
|
Packit Service |
82fcde |
#define _LINUX_RISCV_BITS_ATOMIC_H 1
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <stdint.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
typedef int32_t atomic32_t;
|
|
Packit Service |
82fcde |
typedef uint32_t uatomic32_t;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
typedef int64_t atomic64_t;
|
|
Packit Service |
82fcde |
typedef uint64_t uatomic64_t;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
typedef intptr_t atomicptr_t;
|
|
Packit Service |
82fcde |
typedef uintptr_t uatomicptr_t;
|
|
Packit Service |
82fcde |
typedef intmax_t atomic_max_t;
|
|
Packit Service |
82fcde |
typedef uintmax_t uatomic_max_t;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define atomic_full_barrier() __sync_synchronize ()
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#ifdef __riscv_atomic
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __HAVE_64B_ATOMICS (__riscv_xlen >= 64)
|
|
Packit Service |
82fcde |
# define USE_ATOMIC_COMPILER_BUILTINS 1
|
|
Packit Service |
82fcde |
# define ATOMIC_EXCHANGE_USES_CAS 0
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Compare and exchange.
|
|
Packit Service |
82fcde |
For all "bool" routines, we return FALSE if exchange succesful. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_compare_and_exchange_bool_8_int(mem, newval, oldval, model) \
|
|
Packit Service |
82fcde |
({ \
|
|
Packit Service |
82fcde |
typeof (*mem) __oldval = (oldval); \
|
|
Packit Service |
82fcde |
!__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
|
|
Packit Service |
82fcde |
model, __ATOMIC_RELAXED); \
|
|
Packit Service |
82fcde |
})
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_compare_and_exchange_bool_16_int(mem, newval, oldval, model) \
|
|
Packit Service |
82fcde |
({ \
|
|
Packit Service |
82fcde |
typeof (*mem) __oldval = (oldval); \
|
|
Packit Service |
82fcde |
!__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
|
|
Packit Service |
82fcde |
model, __ATOMIC_RELAXED); \
|
|
Packit Service |
82fcde |
})
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_compare_and_exchange_bool_32_int(mem, newval, oldval, model) \
|
|
Packit Service |
82fcde |
({ \
|
|
Packit Service |
82fcde |
typeof (*mem) __oldval = (oldval); \
|
|
Packit Service |
82fcde |
!__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
|
|
Packit Service |
82fcde |
model, __ATOMIC_RELAXED); \
|
|
Packit Service |
82fcde |
})
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_compare_and_exchange_bool_64_int(mem, newval, oldval, model) \
|
|
Packit Service |
82fcde |
({ \
|
|
Packit Service |
82fcde |
typeof (*mem) __oldval = (oldval); \
|
|
Packit Service |
82fcde |
!__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
|
|
Packit Service |
82fcde |
model, __ATOMIC_RELAXED); \
|
|
Packit Service |
82fcde |
})
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_compare_and_exchange_val_8_int(mem, newval, oldval, model) \
|
|
Packit Service |
82fcde |
({ \
|
|
Packit Service |
82fcde |
typeof (*mem) __oldval = (oldval); \
|
|
Packit Service |
82fcde |
__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
|
|
Packit Service |
82fcde |
model, __ATOMIC_RELAXED); \
|
|
Packit Service |
82fcde |
__oldval; \
|
|
Packit Service |
82fcde |
})
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_compare_and_exchange_val_16_int(mem, newval, oldval, model) \
|
|
Packit Service |
82fcde |
({ \
|
|
Packit Service |
82fcde |
typeof (*mem) __oldval = (oldval); \
|
|
Packit Service |
82fcde |
__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
|
|
Packit Service |
82fcde |
model, __ATOMIC_RELAXED); \
|
|
Packit Service |
82fcde |
__oldval; \
|
|
Packit Service |
82fcde |
})
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_compare_and_exchange_val_32_int(mem, newval, oldval, model) \
|
|
Packit Service |
82fcde |
({ \
|
|
Packit Service |
82fcde |
typeof (*mem) __oldval = (oldval); \
|
|
Packit Service |
82fcde |
__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
|
|
Packit Service |
82fcde |
model, __ATOMIC_RELAXED); \
|
|
Packit Service |
82fcde |
__oldval; \
|
|
Packit Service |
82fcde |
})
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_compare_and_exchange_val_64_int(mem, newval, oldval, model) \
|
|
Packit Service |
82fcde |
({ \
|
|
Packit Service |
82fcde |
typeof (*mem) __oldval = (oldval); \
|
|
Packit Service |
82fcde |
__atomic_compare_exchange_n (mem, (void *) &__oldval, newval, 0, \
|
|
Packit Service |
82fcde |
model, __ATOMIC_RELAXED); \
|
|
Packit Service |
82fcde |
__oldval; \
|
|
Packit Service |
82fcde |
})
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Atomic compare and exchange. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define atomic_compare_and_exchange_bool_acq(mem, new, old) \
|
|
Packit Service |
82fcde |
__atomic_bool_bysize (__arch_compare_and_exchange_bool, int, \
|
|
Packit Service |
82fcde |
mem, new, old, __ATOMIC_ACQUIRE)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define atomic_compare_and_exchange_val_acq(mem, new, old) \
|
|
Packit Service |
82fcde |
__atomic_val_bysize (__arch_compare_and_exchange_val, int, \
|
|
Packit Service |
82fcde |
mem, new, old, __ATOMIC_ACQUIRE)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define atomic_compare_and_exchange_val_rel(mem, new, old) \
|
|
Packit Service |
82fcde |
__atomic_val_bysize (__arch_compare_and_exchange_val, int, \
|
|
Packit Service |
82fcde |
mem, new, old, __ATOMIC_RELEASE)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Atomic exchange (without compare). */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_exchange_8_int(mem, newval, model) \
|
|
Packit Service |
82fcde |
__atomic_exchange_n (mem, newval, model)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_exchange_16_int(mem, newval, model) \
|
|
Packit Service |
82fcde |
__atomic_exchange_n (mem, newval, model)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_exchange_32_int(mem, newval, model) \
|
|
Packit Service |
82fcde |
__atomic_exchange_n (mem, newval, model)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_exchange_64_int(mem, newval, model) \
|
|
Packit Service |
82fcde |
__atomic_exchange_n (mem, newval, model)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define atomic_exchange_acq(mem, value) \
|
|
Packit Service |
82fcde |
__atomic_val_bysize (__arch_exchange, int, mem, value, __ATOMIC_ACQUIRE)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define atomic_exchange_rel(mem, value) \
|
|
Packit Service |
82fcde |
__atomic_val_bysize (__arch_exchange, int, mem, value, __ATOMIC_RELEASE)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Atomically add value and return the previous (unincremented) value. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_exchange_and_add_8_int(mem, value, model) \
|
|
Packit Service |
82fcde |
__atomic_fetch_add (mem, value, model)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_exchange_and_add_16_int(mem, value, model) \
|
|
Packit Service |
82fcde |
__atomic_fetch_add (mem, value, model)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_exchange_and_add_32_int(mem, value, model) \
|
|
Packit Service |
82fcde |
__atomic_fetch_add (mem, value, model)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define __arch_exchange_and_add_64_int(mem, value, model) \
|
|
Packit Service |
82fcde |
__atomic_fetch_add (mem, value, model)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define atomic_exchange_and_add_acq(mem, value) \
|
|
Packit Service |
82fcde |
__atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
|
|
Packit Service |
82fcde |
__ATOMIC_ACQUIRE)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define atomic_exchange_and_add_rel(mem, value) \
|
|
Packit Service |
82fcde |
__atomic_val_bysize (__arch_exchange_and_add, int, mem, value, \
|
|
Packit Service |
82fcde |
__ATOMIC_RELEASE)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* Miscellaneous. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define asm_amo(which, ordering, mem, value) ({ \
|
|
Packit Service |
82fcde |
__atomic_check_size (mem); \
|
|
Packit Service |
82fcde |
typeof (*mem) __tmp; \
|
|
Packit Service |
82fcde |
if (sizeof (__tmp) == 4) \
|
|
Packit Service |
82fcde |
asm volatile (which ".w" ordering "\t%0, %z2, %1" \
|
|
Packit Service |
82fcde |
: "=r" (__tmp), "+A" (* (mem)) \
|
|
Packit Service |
82fcde |
: "rJ" (value)); \
|
|
Packit Service |
82fcde |
else if (sizeof (__tmp) == 8) \
|
|
Packit Service |
82fcde |
asm volatile (which ".d" ordering "\t%0, %z2, %1" \
|
|
Packit Service |
82fcde |
: "=r" (__tmp), "+A" (* (mem)) \
|
|
Packit Service |
82fcde |
: "rJ" (value)); \
|
|
Packit Service |
82fcde |
else \
|
|
Packit Service |
82fcde |
abort (); \
|
|
Packit Service |
82fcde |
__tmp; })
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define atomic_max(mem, value) asm_amo ("amomaxu", ".aq", mem, value)
|
|
Packit Service |
82fcde |
# define atomic_min(mem, value) asm_amo ("amominu", ".aq", mem, value)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define atomic_bit_test_set(mem, bit) \
|
|
Packit Service |
82fcde |
({ typeof (*mem) __mask = (typeof (*mem))1 << (bit); \
|
|
Packit Service |
82fcde |
asm_amo ("amoor", ".aq", mem, __mask) & __mask; })
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
# define catomic_exchange_and_add(mem, value) \
|
|
Packit Service |
82fcde |
atomic_exchange_and_add (mem, value)
|
|
Packit Service |
82fcde |
# define catomic_max(mem, value) atomic_max (mem, value)
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#else /* __riscv_atomic */
|
|
Packit Service |
82fcde |
# error "ISAs that do not subsume the A extension are not supported"
|
|
Packit Service |
82fcde |
#endif /* !__riscv_atomic */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#endif /* bits/atomic.h */
|