1/* $NetBSD: atomic.h,v 1.13 2015/01/08 22:27:18 riastradh Exp $ */
2
3/*-
4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef _SYS_ATOMIC_H_
33#define _SYS_ATOMIC_H_
34
35#include <sys/types.h>
36#if !defined(_KERNEL) && !defined(_STANDALONE)
37#include <stdint.h>
38#endif
39
40__BEGIN_DECLS
41/*
42 * Atomic ADD
43 */
44void atomic_add_32(volatile uint32_t *, int32_t);
45void atomic_add_int(volatile unsigned int *, int);
46void atomic_add_long(volatile unsigned long *, long);
47void atomic_add_ptr(volatile void *, ssize_t);
48void atomic_add_64(volatile uint64_t *, int64_t);
49
50uint32_t atomic_add_32_nv(volatile uint32_t *, int32_t);
51unsigned int atomic_add_int_nv(volatile unsigned int *, int);
52unsigned long atomic_add_long_nv(volatile unsigned long *, long);
53void * atomic_add_ptr_nv(volatile void *, ssize_t);
54uint64_t atomic_add_64_nv(volatile uint64_t *, int64_t);
55
56/*
57 * Atomic AND
58 */
59void atomic_and_32(volatile uint32_t *, uint32_t);
60void atomic_and_uint(volatile unsigned int *, unsigned int);
61void atomic_and_ulong(volatile unsigned long *, unsigned long);
62void atomic_and_64(volatile uint64_t *, uint64_t);
63
64uint32_t atomic_and_32_nv(volatile uint32_t *, uint32_t);
65unsigned int atomic_and_uint_nv(volatile unsigned int *, unsigned int);
66unsigned long atomic_and_ulong_nv(volatile unsigned long *, unsigned long);
67uint64_t atomic_and_64_nv(volatile uint64_t *, uint64_t);
68
69/*
70 * Atomic OR
71 */
72void atomic_or_32(volatile uint32_t *, uint32_t);
73void atomic_or_uint(volatile unsigned int *, unsigned int);
74void atomic_or_ulong(volatile unsigned long *, unsigned long);
75void atomic_or_64(volatile uint64_t *, uint64_t);
76
77uint32_t atomic_or_32_nv(volatile uint32_t *, uint32_t);
78unsigned int atomic_or_uint_nv(volatile unsigned int *, unsigned int);
79unsigned long atomic_or_ulong_nv(volatile unsigned long *, unsigned long);
80uint64_t atomic_or_64_nv(volatile uint64_t *, uint64_t);
81
82/*
83 * Atomic COMPARE-AND-SWAP
84 */
85uint32_t atomic_cas_32(volatile uint32_t *, uint32_t, uint32_t);
86unsigned int atomic_cas_uint(volatile unsigned int *, unsigned int,
87 unsigned int);
88unsigned long atomic_cas_ulong(volatile unsigned long *, unsigned long,
89 unsigned long);
90void * atomic_cas_ptr(volatile void *, void *, void *);
91uint64_t atomic_cas_64(volatile uint64_t *, uint64_t, uint64_t);
92
93/*
94 * This operations will be provided for userland, but may not be
95 * implemented efficiently.
96 */
97uint16_t atomic_cas_16(volatile uint16_t *, uint16_t, uint16_t);
98uint8_t atomic_cas_8(volatile uint8_t *, uint8_t, uint8_t);
99
100/*
101 * Non-interlocked atomic COMPARE-AND-SWAP.
102 */
103uint32_t atomic_cas_32_ni(volatile uint32_t *, uint32_t, uint32_t);
104unsigned int atomic_cas_uint_ni(volatile unsigned int *, unsigned int,
105 unsigned int);
106unsigned long atomic_cas_ulong_ni(volatile unsigned long *, unsigned long,
107 unsigned long);
108void * atomic_cas_ptr_ni(volatile void *, void *, void *);
109uint64_t atomic_cas_64_ni(volatile uint64_t *, uint64_t, uint64_t);
110
111/*
112 * Atomic SWAP
113 */
114uint32_t atomic_swap_32(volatile uint32_t *, uint32_t);
115unsigned int atomic_swap_uint(volatile unsigned int *, unsigned int);
116unsigned long atomic_swap_ulong(volatile unsigned long *, unsigned long);
117void * atomic_swap_ptr(volatile void *, void *);
118uint64_t atomic_swap_64(volatile uint64_t *, uint64_t);
119
120/*
121 * Atomic DECREMENT
122 */
123void atomic_dec_32(volatile uint32_t *);
124void atomic_dec_uint(volatile unsigned int *);
125void atomic_dec_ulong(volatile unsigned long *);
126void atomic_dec_ptr(volatile void *);
127void atomic_dec_64(volatile uint64_t *);
128
129uint32_t atomic_dec_32_nv(volatile uint32_t *);
130unsigned int atomic_dec_uint_nv(volatile unsigned int *);
131unsigned long atomic_dec_ulong_nv(volatile unsigned long *);
132void * atomic_dec_ptr_nv(volatile void *);
133uint64_t atomic_dec_64_nv(volatile uint64_t *);
134
135/*
136 * Atomic INCREMENT
137 */
138void atomic_inc_32(volatile uint32_t *);
139void atomic_inc_uint(volatile unsigned int *);
140void atomic_inc_ulong(volatile unsigned long *);
141void atomic_inc_ptr(volatile void *);
142void atomic_inc_64(volatile uint64_t *);
143
144uint32_t atomic_inc_32_nv(volatile uint32_t *);
145unsigned int atomic_inc_uint_nv(volatile unsigned int *);
146unsigned long atomic_inc_ulong_nv(volatile unsigned long *);
147void * atomic_inc_ptr_nv(volatile void *);
148uint64_t atomic_inc_64_nv(volatile uint64_t *);
149
150/*
151 * Memory barrier operations
152 */
153void membar_enter(void);
154void membar_exit(void);
155void membar_producer(void);
156void membar_consumer(void);
157void membar_sync(void);
158
159#ifdef __HAVE_MEMBAR_DATADEP_CONSUMER
160void membar_datadep_consumer(void);
161#else
162#define membar_datadep_consumer() ((void)0)
163#endif
164
165__END_DECLS
166
167#endif /* ! _SYS_ATOMIC_H_ */
168