xref: /openbsd-src/lib/libc/arch/i386/gen/_atomic_lock.c (revision 7e321ac128fdcd388c62dfa54aca790ebbd73ce1)
1 /*	$OpenBSD: _atomic_lock.c,v 1.1 2017/08/15 06:13:24 guenther Exp $	*/
2 /* David Leonard, <d@csee.uq.edu.au>. Public domain. */
3 
4 /*
5  * Atomic lock for i386
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 	/*
16 	 * Use the eXCHanGe instruction to swap the lock value with
17 	 * a local variable containing the locked state.
18 	 */
19 	old = _ATOMIC_LOCK_LOCKED;
20 	__asm__("xchg %0,(%2)"
21 		: "=r" (old)
22 		: "0"  (old), "r"  (lock));
23 
24 	return (old != _ATOMIC_LOCK_UNLOCKED);
25 }
26