xref: /openbsd/lib/libc/arch/amd64/gen/_atomic_lock.c (revision 3cab2bb3)
1 /*	$OpenBSD: _atomic_lock.c,v 1.1 2017/08/15 06:13:24 guenther Exp $	*/
2 
3 /* David Leonard, <d@csee.uq.edu.au>. Public domain. */
4 
5 /*
6  * Atomic lock for amd64 -- taken from i386 code.
7  */
8 
9 #include <machine/spinlock.h>
10 
11 int
12 _atomic_lock(volatile _atomic_lock_t *lock)
13 {
14 	_atomic_lock_t old;
15 
16 	/*
17 	 * Use the eXCHanGe instruction to swap the lock value with
18 	 * a local variable containing the locked state.
19 	 */
20 	old = _ATOMIC_LOCK_LOCKED;
21 	__asm__("xchg %0,(%2)"
22 		: "=r" (old)
23 		: "0"  (old), "r"  (lock));
24 
25 	return (old != _ATOMIC_LOCK_UNLOCKED);
26 }
27