2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2017 The FreeBSD Foundation
6 * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #ifndef _SYS_ATOMIC_COMMON_H_
33 #define _SYS_ATOMIC_COMMON_H_
35 #ifndef _MACHINE_ATOMIC_H_
36 #error do not include this header, use machine/atomic.h
39 #define atomic_load_char(p) (*(volatile u_char *)(p))
40 #define atomic_load_short(p) (*(volatile u_short *)(p))
41 #define atomic_load_int(p) (*(volatile u_int *)(p))
42 #define atomic_load_long(p) (*(volatile u_long *)(p))
43 #define atomic_load_ptr(p) (*(volatile __typeof(*p) *)(p))
44 #define atomic_load_8(p) (*(volatile uint8_t *)(p))
45 #define atomic_load_16(p) (*(volatile uint16_t *)(p))
46 #define atomic_load_32(p) (*(volatile uint32_t *)(p))
48 #define atomic_load_64(p) (*(volatile uint64_t *)(p))
51 #define atomic_store_char(p, v) \
52 (*(volatile u_char *)(p) = (u_char)(v))
53 #define atomic_store_short(p, v) \
54 (*(volatile u_short *)(p) = (u_short)(v))
55 #define atomic_store_int(p, v) \
56 (*(volatile u_int *)(p) = (u_int)(v))
57 #define atomic_store_long(p, v) \
58 (*(volatile u_long *)(p) = (u_long)(v))
59 #define atomic_store_ptr(p, v) \
60 (*(volatile __typeof(*p) *)(p) = (v))
61 #define atomic_store_8(p, v) \
62 (*(volatile uint8_t *)(p) = (uint8_t)(v))
63 #define atomic_store_16(p, v) \
64 (*(volatile uint16_t *)(p) = (uint16_t)(v))
65 #define atomic_store_32(p, v) \
66 (*(volatile uint32_t *)(p) = (uint32_t)(v))
68 #define atomic_store_64(p, v) \
69 (*(volatile uint64_t *)(p) = (uint64_t)(v))
73 * Currently all architectures provide acquire and release fences on their own,
74 * but they don't provide consume. Kludge below allows relevant code to stop
75 * openly resorting to the stronger acquire fence, to be sorted out.
77 #define atomic_load_consume_ptr(p) \
78 ((__typeof(*p)) atomic_load_acq_ptr((uintptr_t *)p))
80 #define atomic_interrupt_fence() __compiler_membar()