aboutsummaryrefslogtreecommitdiffstats
path: root/lib/libspl/asm-generic/atomic.c
blob: f03f93609a259f619b7e7765f881bb058c034453 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2009 by Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <atomic.h>
#include <assert.h>
#include <pthread.h>

/*
 * All operations are implemented by serializing them through a global
 * pthread mutex.  This provides a correct generic implementation.
 * However all supported architectures are encouraged to provide a
 * native implementation is assembly for performance reasons.
 */
pthread_mutex_t atomic_lock = PTHREAD_MUTEX_INITIALIZER;

/*
 * These are the void returning variants
 */
/* BEGIN CSTYLED */
#define	ATOMIC_INC(name, type) \
	void atomic_inc_##name(volatile type *target)			\
	{								\
		VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);	\
		(*target)++;						\
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);	\
	}

ATOMIC_INC(8, uint8_t)
ATOMIC_INC(uchar, uchar_t)
ATOMIC_INC(16, uint16_t)
ATOMIC_INC(ushort, ushort_t)
ATOMIC_INC(32, uint32_t)
ATOMIC_INC(uint, uint_t)
ATOMIC_INC(ulong, ulong_t)
ATOMIC_INC(64, uint64_t)


#define	ATOMIC_DEC(name, type) \
	void atomic_dec_##name(volatile type *target)			\
	{								\
		VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);	\
		(*target)--;						\
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);	\
	}

ATOMIC_DEC(8, uint8_t)
ATOMIC_DEC(uchar, uchar_t)
ATOMIC_DEC(16, uint16_t)
ATOMIC_DEC(ushort, ushort_t)
ATOMIC_DEC(32, uint32_t)
ATOMIC_DEC(uint, uint_t)
ATOMIC_DEC(ulong, ulong_t)
ATOMIC_DEC(64, uint64_t)


#define	ATOMIC_ADD(name, type1, type2) \
	void atomic_add_##name(volatile type1 *target, type2 bits)	\
	{								\
		VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);	\
		*target += bits;					\
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);	\
	}

ATOMIC_ADD(8, uint8_t, int8_t)
ATOMIC_ADD(char, uchar_t, signed char)
ATOMIC_ADD(16, uint16_t, int16_t)
ATOMIC_ADD(short, ushort_t, short)
ATOMIC_ADD(32, uint32_t, int32_t)
ATOMIC_ADD(int, uint_t, int)
ATOMIC_ADD(long, ulong_t, long)
ATOMIC_ADD(64, uint64_t, int64_t)

void
atomic_add_ptr(volatile void *target, ssize_t bits)
{
	VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);
	*(caddr_t *)target += bits;
	VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);
}


#define	ATOMIC_SUB(name, type1, type2) \
	void atomic_sub_##name(volatile type1 *target, type2 bits)	\
	{								\
		VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);	\
		*target -= bits;					\
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);	\
	}

ATOMIC_SUB(8, uint8_t, int8_t)
ATOMIC_SUB(char, uchar_t, signed char)
ATOMIC_SUB(16, uint16_t, int16_t)
ATOMIC_SUB(short, ushort_t, short)
ATOMIC_SUB(32, uint32_t, int32_t)
ATOMIC_SUB(int, uint_t, int)
ATOMIC_SUB(long, ulong_t, long)
ATOMIC_SUB(64, uint64_t, int64_t)

void
atomic_sub_ptr(volatile void *target, ssize_t bits)
{
	VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);
	*(caddr_t *)target -= bits;
	VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);
}


#define	ATOMIC_OR(name, type) \
	void atomic_or_##name(volatile type *target, type bits)		\
	{								\
		VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);	\
		*target |= bits;					\
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);	\
	}

ATOMIC_OR(8, uint8_t)
ATOMIC_OR(uchar, uchar_t)
ATOMIC_OR(16, uint16_t)
ATOMIC_OR(ushort, ushort_t)
ATOMIC_OR(32, uint32_t)
ATOMIC_OR(uint, uint_t)
ATOMIC_OR(ulong, ulong_t)
ATOMIC_OR(64, uint64_t)


#define	ATOMIC_AND(name, type) \
	void atomic_and_##name(volatile type *target, type bits)	\
	{								\
		VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);	\
		*target &= bits;					\
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);	\
	}

ATOMIC_AND(8, uint8_t)
ATOMIC_AND(uchar, uchar_t)
ATOMIC_AND(16, uint16_t)
ATOMIC_AND(ushort, ushort_t)
ATOMIC_AND(32, uint32_t)
ATOMIC_AND(uint, uint_t)
ATOMIC_AND(ulong, ulong_t)
ATOMIC_AND(64, uint64_t)


/*
 * New value returning variants
 */

#define	ATOMIC_INC_NV(name, type) \
	type atomic_inc_##name##_nv(volatile type *target)		\
	{								\
		type rc;						\
		VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);	\
		rc = (++(*target));					\
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);	\
		return (rc);						\
	}

ATOMIC_INC_NV(8, uint8_t)
ATOMIC_INC_NV(uchar, uchar_t)
ATOMIC_INC_NV(16, uint16_t)
ATOMIC_INC_NV(ushort, ushort_t)
ATOMIC_INC_NV(32, uint32_t)
ATOMIC_INC_NV(uint, uint_t)
ATOMIC_INC_NV(ulong, ulong_t)
ATOMIC_INC_NV(64, uint64_t)


#define	ATOMIC_DEC_NV(name, type) \
	type atomic_dec_##name##_nv(volatile type *target)		\
	{								\
		type rc;						\
		VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);	\
		rc = (--(*target));					\
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);	\
		return (rc);						\
	}

ATOMIC_DEC_NV(8, uint8_t)
ATOMIC_DEC_NV(uchar, uchar_t)
ATOMIC_DEC_NV(16, uint16_t)
ATOMIC_DEC_NV(ushort, ushort_t)
ATOMIC_DEC_NV(32, uint32_t)
ATOMIC_DEC_NV(uint, uint_t)
ATOMIC_DEC_NV(ulong, ulong_t)
ATOMIC_DEC_NV(64, uint64_t)


#define	ATOMIC_ADD_NV(name, type1, type2) \
	type1 atomic_add_##name##_nv(volatile type1 *target, type2 bits)\
	{								\
		type1 rc;						\
		VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);	\
		rc = (*target += bits);					\
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);	\
		return (rc);						\
	}

ATOMIC_ADD_NV(8, uint8_t, int8_t)
ATOMIC_ADD_NV(char, uchar_t, signed char)
ATOMIC_ADD_NV(16, uint16_t, int16_t)
ATOMIC_ADD_NV(short, ushort_t, short)
ATOMIC_ADD_NV(32, uint32_t, int32_t)
ATOMIC_ADD_NV(int, uint_t, int)
ATOMIC_ADD_NV(long, ulong_t, long)
ATOMIC_ADD_NV(64, uint64_t, int64_t)

void *
atomic_add_ptr_nv(volatile void *target, ssize_t bits)
{
	void *ptr;

	VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);
	ptr = (*(caddr_t *)target += bits);
	VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);

	return (ptr);
}


#define	ATOMIC_SUB_NV(name, type1, type2) \
	type1 atomic_sub_##name##_nv(volatile type1 *target, type2 bits)\
	{								\
		type1 rc;						\
		VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);	\
		rc = (*target -= bits);					\
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);	\
		return (rc);						\
	}

ATOMIC_SUB_NV(8, uint8_t, int8_t)
ATOMIC_SUB_NV(char, uchar_t, signed char)
ATOMIC_SUB_NV(16, uint16_t, int16_t)
ATOMIC_SUB_NV(short, ushort_t, short)
ATOMIC_SUB_NV(32, uint32_t, int32_t)
ATOMIC_SUB_NV(int, uint_t, int)
ATOMIC_SUB_NV(long, ulong_t, long)
ATOMIC_SUB_NV(64, uint64_t, int64_t)

void *
atomic_sub_ptr_nv(volatile void *target, ssize_t bits)
{
	void *ptr;

	VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);
	ptr = (*(caddr_t *)target -= bits);
	VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);

	return (ptr);
}


#define	ATOMIC_OR_NV(name, type) \
	type atomic_or_##name##_nv(volatile type *target, type bits)	\
	{								\
		type rc;						\
		VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);	\
		rc = (*target |= bits);					\
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);	\
		return (rc);						\
	}

ATOMIC_OR_NV(8, uint8_t)
ATOMIC_OR_NV(uchar, uchar_t)
ATOMIC_OR_NV(16, uint16_t)
ATOMIC_OR_NV(ushort, ushort_t)
ATOMIC_OR_NV(32, uint32_t)
ATOMIC_OR_NV(uint, uint_t)
ATOMIC_OR_NV(ulong, ulong_t)
ATOMIC_OR_NV(64, uint64_t)


#define	ATOMIC_AND_NV(name, type) \
	type atomic_and_##name##_nv(volatile type *target, type bits)	\
	{								\
		type rc;						\
		VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);	\
		rc = (*target &= bits);					\
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);	\
		return (rc);						\
	}

ATOMIC_AND_NV(8, uint8_t)
ATOMIC_AND_NV(uchar, uchar_t)
ATOMIC_AND_NV(16, uint16_t)
ATOMIC_AND_NV(ushort, ushort_t)
ATOMIC_AND_NV(32, uint32_t)
ATOMIC_AND_NV(uint, uint_t)
ATOMIC_AND_NV(ulong, ulong_t)
ATOMIC_AND_NV(64, uint64_t)


/*
 *  If *arg1 == arg2, set *arg1 = arg3; return old value
 */

#define	ATOMIC_CAS(name, type) \
	type atomic_cas_##name(volatile type *target, type arg1, type arg2) \
	{								\
		type old;						\
		VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);	\
		old = *target;						\
		if (old == arg1)					\
			*target = arg2;					\
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);	\
		return (old);						\
	}

ATOMIC_CAS(8, uint8_t)
ATOMIC_CAS(uchar, uchar_t)
ATOMIC_CAS(16, uint16_t)
ATOMIC_CAS(ushort, ushort_t)
ATOMIC_CAS(32, uint32_t)
ATOMIC_CAS(uint, uint_t)
ATOMIC_CAS(ulong, ulong_t)
ATOMIC_CAS(64, uint64_t)

void *
atomic_cas_ptr(volatile void *target, void *arg1, void *arg2)
{
	void *old;

	VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);
	old = *(void **)target;
	if (old == arg1)
		*(void **)target = arg2;
	VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);

	return (old);
}


/*
 * Swap target and return old value
 */

#define	ATOMIC_SWAP(name, type) \
	type atomic_swap_##name(volatile type *target, type bits)	\
	{								\
		type old;						\
		VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);	\
		old = *target;						\
		*target = bits;						\
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);	\
		return (old);						\
	}

ATOMIC_SWAP(8, uint8_t)
ATOMIC_SWAP(uchar, uchar_t)
ATOMIC_SWAP(16, uint16_t)
ATOMIC_SWAP(ushort, ushort_t)
ATOMIC_SWAP(32, uint32_t)
ATOMIC_SWAP(uint, uint_t)
ATOMIC_SWAP(ulong, ulong_t)
ATOMIC_SWAP(64, uint64_t)
/* END CSTYLED */

void *
atomic_swap_ptr(volatile void *target, void *bits)
{
	void *old;

	VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);
	old = *(void **)target;
	*(void **)target = bits;
	VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);

	return (old);
}


int
atomic_set_long_excl(volatile ulong_t *target, uint_t value)
{
	ulong_t bit;

	VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);
	bit = (1UL << value);
	if ((*target & bit) != 0) {
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);
		return (-1);
	}
	*target |= bit;
	VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);

	return (0);
}

int
atomic_clear_long_excl(volatile ulong_t *target, uint_t value)
{
	ulong_t bit;

	VERIFY3S(pthread_mutex_lock(&atomic_lock), ==, 0);
	bit = (1UL << value);
	if ((*target & bit) != 0) {
		VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);
		return (-1);
	}
	*target &= ~bit;
	VERIFY3S(pthread_mutex_unlock(&atomic_lock), ==, 0);

	return (0);
}

void
membar_enter(void)
{
	/* XXX - Implement me */
}

void
membar_exit(void)
{
	/* XXX - Implement me */
}

void
membar_producer(void)
{
	/* XXX - Implement me */
}

void
membar_consumer(void)
{
	/* XXX - Implement me */
}