xref: /openbsd-src/lib/libc/arch/mips64/gen/_atomic_lock.c (revision 7e321ac128fdcd388c62dfa54aca790ebbd73ce1)
1 /*	$OpenBSD: _atomic_lock.c,v 1.1 2017/08/15 06:13:24 guenther Exp $	*/
2 
3 /*
4  * Atomic lock for mips
5  * Written by Miodrag Vallat <miod@openbsd.org> - placed in the public domain.
6  */
7 
8 #include <machine/spinlock.h>
9 
10 int
_atomic_lock(volatile _atomic_lock_t * lock)11 _atomic_lock(volatile _atomic_lock_t *lock)
12 {
13 	_atomic_lock_t old;
14 
15 	__asm__ volatile (
16 	".set	noreorder\n"
17 	"1:	ll	%0,	0(%1)\n"
18 	"	sc	%2,	0(%1)\n"
19 	"	beqz	%2,	1b\n"
20 	"	 addi	%2,	$0, %3\n"
21 	".set	reorder\n"
22 		: "=&r"(old)
23 		: "r"(lock), "r"(_ATOMIC_LOCK_LOCKED), "i"(_ATOMIC_LOCK_LOCKED)
24 		: "memory");
25 
26 	return (old != _ATOMIC_LOCK_UNLOCKED);
27 }
28