Merge branch 'condvar'
[zfs.git] / lib / libspl / include / atomic.h
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26
27 #ifndef _SYS_ATOMIC_H
28 #define _SYS_ATOMIC_H
29
30 #include <sys/types.h>
31 #include <sys/inttypes.h>
32
33 #ifdef  __cplusplus
34 extern "C" {
35 #endif
36
37 #if defined(__STDC__)
38 /*
39  * Increment target.
40  */
41 extern void atomic_inc_8(volatile uint8_t *);
42 extern void atomic_inc_uchar(volatile uchar_t *);
43 extern void atomic_inc_16(volatile uint16_t *);
44 extern void atomic_inc_ushort(volatile ushort_t *);
45 extern void atomic_inc_32(volatile uint32_t *);
46 extern void atomic_inc_uint(volatile uint_t *);
47 extern void atomic_inc_ulong(volatile ulong_t *);
48 #if defined(_INT64_TYPE)
49 extern void atomic_inc_64(volatile uint64_t *);
50 #endif
51
52 /*
53  * Decrement target
54  */
55 extern void atomic_dec_8(volatile uint8_t *);
56 extern void atomic_dec_uchar(volatile uchar_t *);
57 extern void atomic_dec_16(volatile uint16_t *);
58 extern void atomic_dec_ushort(volatile ushort_t *);
59 extern void atomic_dec_32(volatile uint32_t *);
60 extern void atomic_dec_uint(volatile uint_t *);
61 extern void atomic_dec_ulong(volatile ulong_t *);
62 #if defined(_INT64_TYPE)
63 extern void atomic_dec_64(volatile uint64_t *);
64 #endif
65
66 /*
67  * Add delta to target
68  */
69 extern void atomic_add_8(volatile uint8_t *, int8_t);
70 extern void atomic_add_char(volatile uchar_t *, signed char);
71 extern void atomic_add_16(volatile uint16_t *, int16_t);
72 extern void atomic_add_short(volatile ushort_t *, short);
73 extern void atomic_add_32(volatile uint32_t *, int32_t);
74 extern void atomic_add_int(volatile uint_t *, int);
75 extern void atomic_add_ptr(volatile void *, ssize_t);
76 extern void atomic_add_long(volatile ulong_t *, long);
77 #if defined(_INT64_TYPE)
78 extern void atomic_add_64(volatile uint64_t *, int64_t);
79 #endif
80
81 /*
82  * logical OR bits with target
83  */
84 extern void atomic_or_8(volatile uint8_t *, uint8_t);
85 extern void atomic_or_uchar(volatile uchar_t *, uchar_t);
86 extern void atomic_or_16(volatile uint16_t *, uint16_t);
87 extern void atomic_or_ushort(volatile ushort_t *, ushort_t);
88 extern void atomic_or_32(volatile uint32_t *, uint32_t);
89 extern void atomic_or_uint(volatile uint_t *, uint_t);
90 extern void atomic_or_ulong(volatile ulong_t *, ulong_t);
91 #if defined(_INT64_TYPE)
92 extern void atomic_or_64(volatile uint64_t *, uint64_t);
93 #endif
94
95 /*
96  * logical AND bits with target
97  */
98 extern void atomic_and_8(volatile uint8_t *, uint8_t);
99 extern void atomic_and_uchar(volatile uchar_t *, uchar_t);
100 extern void atomic_and_16(volatile uint16_t *, uint16_t);
101 extern void atomic_and_ushort(volatile ushort_t *, ushort_t);
102 extern void atomic_and_32(volatile uint32_t *, uint32_t);
103 extern void atomic_and_uint(volatile uint_t *, uint_t);
104 extern void atomic_and_ulong(volatile ulong_t *, ulong_t);
105 #if defined(_INT64_TYPE)
106 extern void atomic_and_64(volatile uint64_t *, uint64_t);
107 #endif
108
109 /*
110  * As above, but return the new value.  Note that these _nv() variants are
111  * substantially more expensive on some platforms than the no-return-value
112  * versions above, so don't use them unless you really need to know the
113  * new value *atomically* (e.g. when decrementing a reference count and
114  * checking whether it went to zero).
115  */
116
117 /*
118  * Increment target and return new value.
119  */
120 extern uint8_t atomic_inc_8_nv(volatile uint8_t *);
121 extern uchar_t atomic_inc_uchar_nv(volatile uchar_t *);
122 extern uint16_t atomic_inc_16_nv(volatile uint16_t *);
123 extern ushort_t atomic_inc_ushort_nv(volatile ushort_t *);
124 extern uint32_t atomic_inc_32_nv(volatile uint32_t *);
125 extern uint_t atomic_inc_uint_nv(volatile uint_t *);
126 extern ulong_t atomic_inc_ulong_nv(volatile ulong_t *);
127 #if defined(_INT64_TYPE)
128 extern uint64_t atomic_inc_64_nv(volatile uint64_t *);
129 #endif
130
131 /*
132  * Decrement target and return new value.
133  */
134 extern uint8_t atomic_dec_8_nv(volatile uint8_t *);
135 extern uchar_t atomic_dec_uchar_nv(volatile uchar_t *);
136 extern uint16_t atomic_dec_16_nv(volatile uint16_t *);
137 extern ushort_t atomic_dec_ushort_nv(volatile ushort_t *);
138 extern uint32_t atomic_dec_32_nv(volatile uint32_t *);
139 extern uint_t atomic_dec_uint_nv(volatile uint_t *);
140 extern ulong_t atomic_dec_ulong_nv(volatile ulong_t *);
141 #if defined(_INT64_TYPE)
142 extern uint64_t atomic_dec_64_nv(volatile uint64_t *);
143 #endif
144
145 /*
146  * Add delta to target
147  */
148 extern uint8_t atomic_add_8_nv(volatile uint8_t *, int8_t);
149 extern uchar_t atomic_add_char_nv(volatile uchar_t *, signed char);
150 extern uint16_t atomic_add_16_nv(volatile uint16_t *, int16_t);
151 extern ushort_t atomic_add_short_nv(volatile ushort_t *, short);
152 extern uint32_t atomic_add_32_nv(volatile uint32_t *, int32_t);
153 extern uint_t atomic_add_int_nv(volatile uint_t *, int);
154 extern void *atomic_add_ptr_nv(volatile void *, ssize_t);
155 extern ulong_t atomic_add_long_nv(volatile ulong_t *, long);
156 #if defined(_INT64_TYPE)
157 extern uint64_t atomic_add_64_nv(volatile uint64_t *, int64_t);
158 #endif
159
160 /*
161  * logical OR bits with target and return new value.
162  */
163 extern uint8_t atomic_or_8_nv(volatile uint8_t *, uint8_t);
164 extern uchar_t atomic_or_uchar_nv(volatile uchar_t *, uchar_t);
165 extern uint16_t atomic_or_16_nv(volatile uint16_t *, uint16_t);
166 extern ushort_t atomic_or_ushort_nv(volatile ushort_t *, ushort_t);
167 extern uint32_t atomic_or_32_nv(volatile uint32_t *, uint32_t);
168 extern uint_t atomic_or_uint_nv(volatile uint_t *, uint_t);
169 extern ulong_t atomic_or_ulong_nv(volatile ulong_t *, ulong_t);
170 #if defined(_INT64_TYPE)
171 extern uint64_t atomic_or_64_nv(volatile uint64_t *, uint64_t);
172 #endif
173
174 /*
175  * logical AND bits with target and return new value.
176  */
177 extern uint8_t atomic_and_8_nv(volatile uint8_t *, uint8_t);
178 extern uchar_t atomic_and_uchar_nv(volatile uchar_t *, uchar_t);
179 extern uint16_t atomic_and_16_nv(volatile uint16_t *, uint16_t);
180 extern ushort_t atomic_and_ushort_nv(volatile ushort_t *, ushort_t);
181 extern uint32_t atomic_and_32_nv(volatile uint32_t *, uint32_t);
182 extern uint_t atomic_and_uint_nv(volatile uint_t *, uint_t);
183 extern ulong_t atomic_and_ulong_nv(volatile ulong_t *, ulong_t);
184 #if defined(_INT64_TYPE)
185 extern uint64_t atomic_and_64_nv(volatile uint64_t *, uint64_t);
186 #endif
187
188 /*
189  * If *arg1 == arg2, set *arg1 = arg3; return old value
190  */
191 extern uint8_t atomic_cas_8(volatile uint8_t *, uint8_t, uint8_t);
192 extern uchar_t atomic_cas_uchar(volatile uchar_t *, uchar_t, uchar_t);
193 extern uint16_t atomic_cas_16(volatile uint16_t *, uint16_t, uint16_t);
194 extern ushort_t atomic_cas_ushort(volatile ushort_t *, ushort_t, ushort_t);
195 extern uint32_t atomic_cas_32(volatile uint32_t *, uint32_t, uint32_t);
196 extern uint_t atomic_cas_uint(volatile uint_t *, uint_t, uint_t);
197 extern void *atomic_cas_ptr(volatile void *, void *, void *);
198 extern ulong_t atomic_cas_ulong(volatile ulong_t *, ulong_t, ulong_t);
199 #if defined(_INT64_TYPE)
200 extern uint64_t atomic_cas_64(volatile uint64_t *, uint64_t, uint64_t);
201 #endif
202
203 /*
204  * Swap target and return old value
205  */
206 extern uint8_t atomic_swap_8(volatile uint8_t *, uint8_t);
207 extern uchar_t atomic_swap_uchar(volatile uchar_t *, uchar_t);
208 extern uint16_t atomic_swap_16(volatile uint16_t *, uint16_t);
209 extern ushort_t atomic_swap_ushort(volatile ushort_t *, ushort_t);
210 extern uint32_t atomic_swap_32(volatile uint32_t *, uint32_t);
211 extern uint_t atomic_swap_uint(volatile uint_t *, uint_t);
212 extern void *atomic_swap_ptr(volatile void *, void *);
213 extern ulong_t atomic_swap_ulong(volatile ulong_t *, ulong_t);
214 #if defined(_INT64_TYPE)
215 extern uint64_t atomic_swap_64(volatile uint64_t *, uint64_t);
216 #endif
217
218 /*
219  * Perform an exclusive atomic bit set/clear on a target.
220  * Returns 0 if bit was sucessfully set/cleared, or -1
221  * if the bit was already set/cleared.
222  */
223 extern int atomic_set_long_excl(volatile ulong_t *, uint_t);
224 extern int atomic_clear_long_excl(volatile ulong_t *, uint_t);
225
226 /*
227  * Generic memory barrier used during lock entry, placed after the
228  * memory operation that acquires the lock to guarantee that the lock
229  * protects its data.  No stores from after the memory barrier will
230  * reach visibility, and no loads from after the barrier will be
231  * resolved, before the lock acquisition reaches global visibility.
232  */
233 extern void membar_enter(void);
234
235 /*
236  * Generic memory barrier used during lock exit, placed before the
237  * memory operation that releases the lock to guarantee that the lock
238  * protects its data.  All loads and stores issued before the barrier
239  * will be resolved before the subsequent lock update reaches visibility.
240  */
241 extern void membar_exit(void);
242
243 /*
244  * Arrange that all stores issued before this point in the code reach
245  * global visibility before any stores that follow; useful in producer
246  * modules that update a data item, then set a flag that it is available.
247  * The memory barrier guarantees that the available flag is not visible
248  * earlier than the updated data, i.e. it imposes store ordering.
249  */
250 extern void membar_producer(void);
251
252 /*
253  * Arrange that all loads issued before this point in the code are
254  * completed before any subsequent loads; useful in consumer modules
255  * that check to see if data is available and read the data.
256  * The memory barrier guarantees that the data is not sampled until
257  * after the available flag has been seen, i.e. it imposes load ordering.
258  */
259 extern void membar_consumer(void);
260 #endif  /* __STDC__ */
261
262 #ifdef  __cplusplus
263 }
264 #endif
265
266 #endif  /* _SYS_ATOMIC_H */