summaryrefslogtreecommitdiff
path: root/lib/libpthread/arch/i386/_atomic_lock.c
blob: 9bf4d82b994db7df109650e374f1f7b0a171aa4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*	$OpenBSD: _atomic_lock.c,v 1.8 2010/12/03 19:44:22 miod Exp $	*/
/* David Leonard, <d@csee.uq.edu.au>. Public domain. */

/*
 * Atomic lock for i386
 */

#include <spinlock.h>

int
_atomic_lock(volatile _spinlock_lock_t *lock)
{
	_spinlock_lock_t old;

	/*
	 * Use the eXCHanGe instruction to swap the lock value with
	 * a local variable containing the locked state.
	 */
	old = _SPINLOCK_LOCKED;
	__asm__("xchg %0,(%2)"
		: "=r" (old)
		: "0"  (old), "r"  (lock));

	return (old != _SPINLOCK_UNLOCKED);
}