xref: /netbsd/sys/sys/atomic.h (revision 6550d01e)
1 /*	$NetBSD: atomic.h,v 1.11 2009/11/20 02:17:07 christos 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  * Non-interlocked atomic COMPARE-AND-SWAP.
95  */
96 uint32_t	atomic_cas_32_ni(volatile uint32_t *, uint32_t, uint32_t);
97 unsigned int	atomic_cas_uint_ni(volatile unsigned int *, unsigned int,
98 				   unsigned int);
99 unsigned long	atomic_cas_ulong_ni(volatile unsigned long *, unsigned long,
100 				    unsigned long);
101 void *		atomic_cas_ptr_ni(volatile void *, void *, void *);
102 uint64_t	atomic_cas_64_ni(volatile uint64_t *, uint64_t, uint64_t);
103 
104 /*
105  * Atomic SWAP
106  */
107 uint32_t	atomic_swap_32(volatile uint32_t *, uint32_t);
108 unsigned int	atomic_swap_uint(volatile unsigned int *, unsigned int);
109 unsigned long	atomic_swap_ulong(volatile unsigned long *, unsigned long);
110 void *		atomic_swap_ptr(volatile void *, void *);
111 uint64_t	atomic_swap_64(volatile uint64_t *, uint64_t);
112 
113 /*
114  * Atomic DECREMENT
115  */
116 void		atomic_dec_32(volatile uint32_t *);
117 void		atomic_dec_uint(volatile unsigned int *);
118 void		atomic_dec_ulong(volatile unsigned long *);
119 void		atomic_dec_ptr(volatile void *);
120 void		atomic_dec_64(volatile uint64_t *);
121 
122 uint32_t	atomic_dec_32_nv(volatile uint32_t *);
123 unsigned int	atomic_dec_uint_nv(volatile unsigned int *);
124 unsigned long	atomic_dec_ulong_nv(volatile unsigned long *);
125 void *		atomic_dec_ptr_nv(volatile void *);
126 uint64_t	atomic_dec_64_nv(volatile uint64_t *);
127 
128 /*
129  * Atomic INCREMENT
130  */
131 void		atomic_inc_32(volatile uint32_t *);
132 void		atomic_inc_uint(volatile unsigned int *);
133 void		atomic_inc_ulong(volatile unsigned long *);
134 void		atomic_inc_ptr(volatile void *);
135 void		atomic_inc_64(volatile uint64_t *);
136 
137 uint32_t	atomic_inc_32_nv(volatile uint32_t *);
138 unsigned int	atomic_inc_uint_nv(volatile unsigned int *);
139 unsigned long	atomic_inc_ulong_nv(volatile unsigned long *);
140 void *		atomic_inc_ptr_nv(volatile void *);
141 uint64_t	atomic_inc_64_nv(volatile uint64_t *);
142 
143 /*
144  * Memory barrier operations
145  */
146 void		membar_enter(void);
147 void		membar_exit(void);
148 void		membar_producer(void);
149 void		membar_consumer(void);
150 void		membar_sync(void);
151 
152 __END_DECLS
153 
154 #endif /* ! _SYS_ATOMIC_H_ */
155