xref: /netbsd-src/common/lib/libc/atomic/atomic_swap_8_cas.c (revision bd7dee058c4a3ff18e2e28b0f517f713b1c966e0)
1*bd7dee05Sjoerg /*	$NetBSD: atomic_swap_8_cas.c,v 1.3 2014/06/28 20:18:55 joerg Exp $	*/
2e39d980fSmartin 
3e39d980fSmartin /*-
4e39d980fSmartin  * Copyright (c) 2014 The NetBSD Foundation, Inc.
5e39d980fSmartin  * All rights reserved.
6e39d980fSmartin  *
7e39d980fSmartin  * This code is derived from software contributed to The NetBSD Foundation
8e39d980fSmartin  * by Jason R. Thorpe.
9e39d980fSmartin  *
10e39d980fSmartin  * Redistribution and use in source and binary forms, with or without
11e39d980fSmartin  * modification, are permitted provided that the following conditions
12e39d980fSmartin  * are met:
13e39d980fSmartin  * 1. Redistributions of source code must retain the above copyright
14e39d980fSmartin  *    notice, this list of conditions and the following disclaimer.
15e39d980fSmartin  * 2. Redistributions in binary form must reproduce the above copyright
16e39d980fSmartin  *    notice, this list of conditions and the following disclaimer in the
17e39d980fSmartin  *    documentation and/or other materials provided with the distribution.
18e39d980fSmartin  *
19e39d980fSmartin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20e39d980fSmartin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21e39d980fSmartin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22e39d980fSmartin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23e39d980fSmartin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24e39d980fSmartin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25e39d980fSmartin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26e39d980fSmartin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27e39d980fSmartin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28e39d980fSmartin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29e39d980fSmartin  * POSSIBILITY OF SUCH DAMAGE.
30e39d980fSmartin  */
31e39d980fSmartin 
32e39d980fSmartin #include "atomic_op_namespace.h"
33e39d980fSmartin 
34e39d980fSmartin #include <sys/atomic.h>
35e39d980fSmartin 
36e39d980fSmartin uint8_t
37e39d980fSmartin atomic_swap_8(volatile uint8_t *addr, uint8_t new)
38e39d980fSmartin 	asm("__sync_lock_test_and_set_1");
39e39d980fSmartin 
40e39d980fSmartin uint8_t
atomic_swap_8(volatile uint8_t * addr,uint8_t new)41e39d980fSmartin atomic_swap_8(volatile uint8_t *addr, uint8_t new)
42e39d980fSmartin {
43e39d980fSmartin 	uint8_t old;
44e39d980fSmartin 
45e39d980fSmartin 	do {
46e39d980fSmartin 		old = *addr;
47e39d980fSmartin 	} while (atomic_cas_8(addr, old, new) != old);
48e39d980fSmartin 
49e39d980fSmartin 	return old;
50e39d980fSmartin }
51*bd7dee05Sjoerg 
52*bd7dee05Sjoerg crt_alias(__atomic_exchange_1,__sync_lock_test_and_set_1)
53