xref: /netbsd-src/common/lib/libc/atomic/atomic_or_32_cas.c (revision 504e8e687f4f6c5bcfd35885ec51e14ae6b54f44)
1*504e8e68Sjoerg /*	$NetBSD: atomic_or_32_cas.c,v 1.10 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 
3680d48117Sjoerg uint32_t fetch_and_or_4(volatile uint32_t *, uint32_t, ...)
37b290679aSmartin #if defined(_LIBC) || defined(_HARDKERNEL)
38b290679aSmartin     asm("__sync_fetch_and_or_4");	/* C runtime internal */
39b290679aSmartin #else
40b290679aSmartin     ;
41b290679aSmartin #endif
4256b80ae3Smatt 
4356b80ae3Smatt uint32_t
fetch_and_or_4(volatile uint32_t * addr,uint32_t val,...)4480d48117Sjoerg fetch_and_or_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);
5256b80ae3Smatt 	return old;
5356b80ae3Smatt }
5456b80ae3Smatt 
5556b80ae3Smatt void
atomic_or_32(volatile uint32_t * addr,uint32_t val)5656b80ae3Smatt atomic_or_32(volatile uint32_t *addr, uint32_t val)
5756b80ae3Smatt {
5880d48117Sjoerg 	(void) fetch_and_or_4(addr, val);
5979085586Sad }
6079085586Sad 
61*504e8e68Sjoerg __strong_alias(__atomic_fetch_or_4,__sync_fetch_and_or_4)
62*504e8e68Sjoerg 
6379085586Sad #undef atomic_or_32
6479085586Sad atomic_op_alias(atomic_or_32,_atomic_or_32)
653f7729adSmatt 
6679085586Sad #undef atomic_or_uint
6779085586Sad atomic_op_alias(atomic_or_uint,_atomic_or_32)
6879085586Sad __strong_alias(_atomic_or_uint,_atomic_or_32)
6979085586Sad #if !defined(_LP64)
7079085586Sad #undef atomic_or_ulong
7179085586Sad atomic_op_alias(atomic_or_ulong,_atomic_or_32)
7279085586Sad __strong_alias(_atomic_or_ulong,_atomic_or_32)
7379085586Sad #endif /* _LP64 */
74