1*e2443f41Sjoerg /*	$NetBSD: atomic_add_32_cas.c,v 1.9 2014/06/23 21:53:45 joerg Exp $	*/
279085586Sad 
379085586Sad /*-
479085586Sad  * Copyright (c) 2007 The NetBSD Foundation, Inc.
579085586Sad  * All rights reserved.
679085586Sad  *
779085586Sad  * This code is derived from software contributed to The NetBSD Foundation
879085586Sad  * by Jason R. Thorpe.
979085586Sad  *
1079085586Sad  * Redistribution and use in source and binary forms, with or without
1179085586Sad  * modification, are permitted provided that the following conditions
1279085586Sad  * are met:
1379085586Sad  * 1. Redistributions of source code must retain the above copyright
1479085586Sad  *    notice, this list of conditions and the following disclaimer.
1579085586Sad  * 2. Redistributions in binary form must reproduce the above copyright
1679085586Sad  *    notice, this list of conditions and the following disclaimer in the
1779085586Sad  *    documentation and/or other materials provided with the distribution.
1879085586Sad  *
1979085586Sad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2079085586Sad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2179085586Sad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2279085586Sad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2379085586Sad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2479085586Sad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2579085586Sad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2679085586Sad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2779085586Sad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2879085586Sad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2979085586Sad  * POSSIBILITY OF SUCH DAMAGE.
3079085586Sad  */
3179085586Sad 
3279085586Sad #include "atomic_op_namespace.h"
3379085586Sad 
3479085586Sad #include <sys/atomic.h>
3579085586Sad 
36af30099bSjoerg uint32_t fetch_and_add_4(volatile uint32_t *, uint32_t, ...)
3763880f0fSmartin #if defined(_LIBC) || defined(_HARDKERNEL)
3863880f0fSmartin     asm("__sync_fetch_and_add_4");	/* C runtime internal */
3963880f0fSmartin #else
4063880f0fSmartin     ;
4163880f0fSmartin #endif
42c9405045Smatt 
43c9405045Smatt uint32_t
fetch_and_add_4(volatile uint32_t * addr,uint32_t val,...)44af30099bSjoerg fetch_and_add_4(volatile uint32_t *addr, uint32_t val, ...)
4579085586Sad {
4679085586Sad 	uint32_t old, new;
4779085586Sad 
4879085586Sad 	do {
4979085586Sad 		old = *addr;
5079085586Sad 		new = old + val;
5179085586Sad 	} while (atomic_cas_32(addr, old, new) != old);
52c9405045Smatt 	return old;
53c9405045Smatt }
54c9405045Smatt 
55c9405045Smatt void
atomic_add_32(volatile uint32_t * addr,int32_t val)56c9405045Smatt atomic_add_32(volatile uint32_t *addr, int32_t val)
57c9405045Smatt {
58af30099bSjoerg 	(void) fetch_and_add_4(addr, val);
5979085586Sad }
6079085586Sad 
6179085586Sad #undef atomic_add_32
6279085586Sad atomic_op_alias(atomic_add_32,_atomic_add_32)
6379085586Sad 
6479085586Sad #undef atomic_add_int
6579085586Sad atomic_op_alias(atomic_add_int,_atomic_add_32)
6679085586Sad __strong_alias(_atomic_add_int,_atomic_add_32)
6779085586Sad 
6879085586Sad #if !defined(_LP64)
6979085586Sad #undef atomic_add_long
7079085586Sad atomic_op_alias(atomic_add_long,_atomic_add_32)
7179085586Sad __strong_alias(_atomic_add_long,_atomic_add_32)
7279085586Sad 
7379085586Sad #undef atomic_add_ptr
7479085586Sad atomic_op_alias(atomic_add_ptr,_atomic_add_32)
7579085586Sad __strong_alias(_atomic_add_ptr,_atomic_add_32)
7679085586Sad #endif /* _LP64 */
77*e2443f41Sjoerg 
78*e2443f41Sjoerg __strong_alias(__atomic_fetch_add_4,__sync_fetch_and_add_4)
79