1*bd7dee05Sjoerg /* $NetBSD: atomic_swap_32_cas.c,v 1.7 2014/06/28 20:18:55 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
3679085586Sad uint32_t
atomic_swap_32(volatile uint32_t * addr,uint32_t new)3779085586Sad atomic_swap_32(volatile uint32_t *addr, uint32_t new)
3879085586Sad {
3979085586Sad uint32_t old;
4079085586Sad
4179085586Sad do {
4279085586Sad old = *addr;
4379085586Sad } while (atomic_cas_32(addr, old, new) != old);
4479085586Sad
4579085586Sad return (old);
4679085586Sad }
4779085586Sad
48*bd7dee05Sjoerg crt_alias(__atomic_exchange_4,_atomic_swap_32)
49*bd7dee05Sjoerg
5079085586Sad #undef atomic_swap_32
5179085586Sad atomic_op_alias(atomic_swap_32,_atomic_swap_32)
5279085586Sad #undef atomic_swap_uint
5379085586Sad atomic_op_alias(atomic_swap_uint,_atomic_swap_32)
5479085586Sad __strong_alias(_atomic_swap_uint,_atomic_swap_32)
55b290679aSmartin crt_alias(__sync_lock_test_and_set_4,_atomic_swap_32)
5679085586Sad #if !defined(_LP64)
5779085586Sad #undef atomic_swap_ulong
5879085586Sad atomic_op_alias(atomic_swap_ulong,_atomic_swap_32)
5979085586Sad __strong_alias(_atomic_swap_ulong,_atomic_swap_32)
6079085586Sad #undef atomic_swap_ptr
6179085586Sad atomic_op_alias(atomic_swap_ptr,_atomic_swap_32)
6279085586Sad __strong_alias(_atomic_swap_ptr,_atomic_swap_32)
6379085586Sad #endif /* _LP64 */
64