11573Srgrimes/*	$NetBSD: atomic_swap_32_cas.c,v 1.3 2007/11/28 16:59:11 ad Exp $	*/
21573Srgrimes
31573Srgrimes/*-
41573Srgrimes * Copyright (c) 2007 The NetBSD Foundation, Inc.
51573Srgrimes * All rights reserved.
61573Srgrimes *
71573Srgrimes * This code is derived from software contributed to The NetBSD Foundation
81573Srgrimes * by Jason R. Thorpe.
91573Srgrimes *
101573Srgrimes * Redistribution and use in source and binary forms, with or without
111573Srgrimes * modification, are permitted provided that the following conditions
121573Srgrimes * are met:
131573Srgrimes * 1. Redistributions of source code must retain the above copyright
141573Srgrimes *    notice, this list of conditions and the following disclaimer.
151573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
161573Srgrimes *    notice, this list of conditions and the following disclaimer in the
171573Srgrimes *    documentation and/or other materials provided with the distribution.
181573Srgrimes *
191573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201573Srgrimes * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211573Srgrimes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221573Srgrimes * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231573Srgrimes * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241573Srgrimes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251573Srgrimes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261573Srgrimes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271573Srgrimes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281573Srgrimes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291573Srgrimes * POSSIBILITY OF SUCH DAMAGE.
301573Srgrimes */
311573Srgrimes
321573Srgrimes#include "atomic_op_namespace.h"
331573Srgrimes
341573Srgrimes#include <sys/atomic.h>
351573Srgrimes
361573Srgrimesuint32_t
371573Srgrimesatomic_swap_32(volatile uint32_t *addr, uint32_t new)
381573Srgrimes{
391573Srgrimes	uint32_t old;
401573Srgrimes
411573Srgrimes	do {
421573Srgrimes		old = *addr;
431573Srgrimes	} while (atomic_cas_32(addr, old, new) != old);
441573Srgrimes
451573Srgrimes	return (old);
461573Srgrimes}
471573Srgrimes
481573Srgrimes#undef atomic_swap_32
491573Srgrimesatomic_op_alias(atomic_swap_32,_atomic_swap_32)
501573Srgrimes#undef atomic_swap_uint
511573Srgrimesatomic_op_alias(atomic_swap_uint,_atomic_swap_32)
521573Srgrimes__strong_alias(_atomic_swap_uint,_atomic_swap_32)
531573Srgrimes#if !defined(_LP64)
541573Srgrimes#undef atomic_swap_ulong
551573Srgrimesatomic_op_alias(atomic_swap_ulong,_atomic_swap_32)
561573Srgrimes__strong_alias(_atomic_swap_ulong,_atomic_swap_32)
571573Srgrimes#undef atomic_swap_ptr
581573Srgrimesatomic_op_alias(atomic_swap_ptr,_atomic_swap_32)
591573Srgrimes__strong_alias(_atomic_swap_ptr,_atomic_swap_32)
601573Srgrimes#endif /* _LP64 */
611573Srgrimes