xref: /minix/sys/sys/atomic.h (revision 0a6a1f1d)
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  */
44 void		atomic_add_32(volatile uint32_t *, int32_t);
45 void		atomic_add_int(volatile unsigned int *, int);
46 void		atomic_add_long(volatile unsigned long *, long);
47 void		atomic_add_ptr(volatile void *, ssize_t);
48 void		atomic_add_64(volatile uint64_t *, int64_t);
49 
50 uint32_t	atomic_add_32_nv(volatile uint32_t *, int32_t);
51 unsigned int	atomic_add_int_nv(volatile unsigned int *, int);
52 unsigned long	atomic_add_long_nv(volatile unsigned long *, long);
53 void *		atomic_add_ptr_nv(volatile void *, ssize_t);
54 uint64_t	atomic_add_64_nv(volatile uint64_t *, int64_t);
55 
56 /*
57  * Atomic AND
58  */
59 void		atomic_and_32(volatile uint32_t *, uint32_t);
60 void		atomic_and_uint(volatile unsigned int *, unsigned int);
61 void		atomic_and_ulong(volatile unsigned long *, unsigned long);
62 void		atomic_and_64(volatile uint64_t *, uint64_t);
63 
64 uint32_t	atomic_and_32_nv(volatile uint32_t *, uint32_t);
65 unsigned int	atomic_and_uint_nv(volatile unsigned int *, unsigned int);
66 unsigned long	atomic_and_ulong_nv(volatile unsigned long *, unsigned long);
67 uint64_t	atomic_and_64_nv(volatile uint64_t *, uint64_t);
68 
69 /*
70  * Atomic OR
71  */
72 void		atomic_or_32(volatile uint32_t *, uint32_t);
73 void		atomic_or_uint(volatile unsigned int *, unsigned int);
74 void		atomic_or_ulong(volatile unsigned long *, unsigned long);
75 void		atomic_or_64(volatile uint64_t *, uint64_t);
76 
77 uint32_t	atomic_or_32_nv(volatile uint32_t *, uint32_t);
78 unsigned int	atomic_or_uint_nv(volatile unsigned int *, unsigned int);
79 unsigned long	atomic_or_ulong_nv(volatile unsigned long *, unsigned long);
80 uint64_t	atomic_or_64_nv(volatile uint64_t *, uint64_t);
81 
82 /*
83  * Atomic COMPARE-AND-SWAP
84  */
85 uint32_t	atomic_cas_32(volatile uint32_t *, uint32_t, uint32_t);
86 unsigned int	atomic_cas_uint(volatile unsigned int *, unsigned int,
87 				unsigned int);
88 unsigned long	atomic_cas_ulong(volatile unsigned long *, unsigned long,
89 				 unsigned long);
90 void *		atomic_cas_ptr(volatile void *, void *, void *);
91 uint64_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  */
97 uint16_t	atomic_cas_16(volatile uint16_t *, uint16_t, uint16_t);
98 uint8_t 	atomic_cas_8(volatile uint8_t *, uint8_t, uint8_t);
99 
100 /*
101  * Non-interlocked atomic COMPARE-AND-SWAP.
102  */
103 uint32_t	atomic_cas_32_ni(volatile uint32_t *, uint32_t, uint32_t);
104 unsigned int	atomic_cas_uint_ni(volatile unsigned int *, unsigned int,
105 				   unsigned int);
106 unsigned long	atomic_cas_ulong_ni(volatile unsigned long *, unsigned long,
107 				    unsigned long);
108 void *		atomic_cas_ptr_ni(volatile void *, void *, void *);
109 uint64_t	atomic_cas_64_ni(volatile uint64_t *, uint64_t, uint64_t);
110 
111 /*
112  * Atomic SWAP
113  */
114 uint32_t	atomic_swap_32(volatile uint32_t *, uint32_t);
115 unsigned int	atomic_swap_uint(volatile unsigned int *, unsigned int);
116 unsigned long	atomic_swap_ulong(volatile unsigned long *, unsigned long);
117 void *		atomic_swap_ptr(volatile void *, void *);
118 uint64_t	atomic_swap_64(volatile uint64_t *, uint64_t);
119 
120 /*
121  * Atomic DECREMENT
122  */
123 void		atomic_dec_32(volatile uint32_t *);
124 void		atomic_dec_uint(volatile unsigned int *);
125 void		atomic_dec_ulong(volatile unsigned long *);
126 void		atomic_dec_ptr(volatile void *);
127 void		atomic_dec_64(volatile uint64_t *);
128 
129 uint32_t	atomic_dec_32_nv(volatile uint32_t *);
130 unsigned int	atomic_dec_uint_nv(volatile unsigned int *);
131 unsigned long	atomic_dec_ulong_nv(volatile unsigned long *);
132 void *		atomic_dec_ptr_nv(volatile void *);
133 uint64_t	atomic_dec_64_nv(volatile uint64_t *);
134 
135 /*
136  * Atomic INCREMENT
137  */
138 void		atomic_inc_32(volatile uint32_t *);
139 void		atomic_inc_uint(volatile unsigned int *);
140 void		atomic_inc_ulong(volatile unsigned long *);
141 void		atomic_inc_ptr(volatile void *);
142 void		atomic_inc_64(volatile uint64_t *);
143 
144 uint32_t	atomic_inc_32_nv(volatile uint32_t *);
145 unsigned int	atomic_inc_uint_nv(volatile unsigned int *);
146 unsigned long	atomic_inc_ulong_nv(volatile unsigned long *);
147 void *		atomic_inc_ptr_nv(volatile void *);
148 uint64_t	atomic_inc_64_nv(volatile uint64_t *);
149 
150 /*
151  * Memory barrier operations
152  */
153 void		membar_enter(void);
154 void		membar_exit(void);
155 void		membar_producer(void);
156 void		membar_consumer(void);
157 void		membar_sync(void);
158 
159 #ifdef	__HAVE_MEMBAR_DATADEP_CONSUMER
160 void		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