xref: /openbsd-src/lib/libc/arch/sh/gen/_atomic_lock.c (revision 7e321ac128fdcd388c62dfa54aca790ebbd73ce1)
1*7e321ac1Sguenther /*	$OpenBSD: _atomic_lock.c,v 1.1 2017/08/15 06:13:24 guenther Exp $	*/
2*7e321ac1Sguenther 
3*7e321ac1Sguenther /*-
4*7e321ac1Sguenther  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5*7e321ac1Sguenther  * All rights reserved.
6*7e321ac1Sguenther  *
7*7e321ac1Sguenther  * This code is derived from software contributed to The NetBSD Foundation
8*7e321ac1Sguenther  * by Gregory McGarry.
9*7e321ac1Sguenther  *
10*7e321ac1Sguenther  * Redistribution and use in source and binary forms, with or without
11*7e321ac1Sguenther  * modification, are permitted provided that the following conditions
12*7e321ac1Sguenther  * are met:
13*7e321ac1Sguenther  * 1. Redistributions of source code must retain the above copyright
14*7e321ac1Sguenther  *    notice, this list of conditions and the following disclaimer.
15*7e321ac1Sguenther  * 2. Redistributions in binary form must reproduce the above copyright
16*7e321ac1Sguenther  *    notice, this list of conditions and the following disclaimer in the
17*7e321ac1Sguenther  *    documentation and/or other materials provided with the distribution.
18*7e321ac1Sguenther  *
19*7e321ac1Sguenther  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*7e321ac1Sguenther  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*7e321ac1Sguenther  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*7e321ac1Sguenther  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*7e321ac1Sguenther  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*7e321ac1Sguenther  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*7e321ac1Sguenther  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*7e321ac1Sguenther  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*7e321ac1Sguenther  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*7e321ac1Sguenther  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*7e321ac1Sguenther  * POSSIBILITY OF SUCH DAMAGE.
30*7e321ac1Sguenther  */
31*7e321ac1Sguenther 
32*7e321ac1Sguenther #include <machine/spinlock.h>
33*7e321ac1Sguenther 
34*7e321ac1Sguenther int
_atomic_lock(volatile _atomic_lock_t * lock)35*7e321ac1Sguenther _atomic_lock(volatile _atomic_lock_t *lock)
36*7e321ac1Sguenther {
37*7e321ac1Sguenther 	_atomic_lock_t old;
38*7e321ac1Sguenther 
39*7e321ac1Sguenther 	__asm volatile(
40*7e321ac1Sguenther 		"	tas.b	%0	\n"
41*7e321ac1Sguenther 		"	mov	#0, %1	\n"
42*7e321ac1Sguenther 		"	rotcl	%1	\n"
43*7e321ac1Sguenther 		: "=m" (*lock), "=r" (old));
44*7e321ac1Sguenther 
45*7e321ac1Sguenther 	return (old == 0);
46*7e321ac1Sguenther }
47