1 /*	$OpenBSD: _atomic_lock.c,v 1.8 2003/01/31 22:10:53 deraadt Exp $	*/
2 /*
3  * Atomic lock for sparc
4  */
5 
6 #include "spinlock.h"
7 
8 int
_atomic_lock(volatile _spinlock_lock_t * lock)9 _atomic_lock(volatile _spinlock_lock_t * lock)
10 {
11 	_spinlock_lock_t old;
12 
13 	/*
14 	 *  "  ldstub  [address], reg_rd
15 	 *
16 	 *  The atomic load-store instructions copy a byte from memory
17 	 *  into r[rd]m then rewrite the addressed byte in memory to all
18 	 *  ones [_SPINLOCK_LOCKED]. The operation is performed
19 	 *  atomically, that is, without allowing intervening interrupts
20 	 *  or deferred traps. In a multiprocessor system, two or more
21 	 *  processors executing atomic load-store unsigned byte [...]
22 	 *  addressing the same byte [...] simultaneously are guaranteed
23 	 *  to execute them in an undefined, but serial order."
24 	 *    - p101, The SPARC Architecture Manual (version 8) Prentice-Hall
25 	 *
26 	 * "LDSTUB loads a byte value from memory to a register and writes
27 	 *  the value FF_16 into the addressed byte atomically. LDSTUB
28 	 *  is the classic test-and-set instruction. Like SWAP, it has
29 	 *  a consensus number of two and so cannot resolve more than
30 	 *  two contending processes in a wait-free fashion."
31 	 *    - p129, The SPARC Architecture Manual (version 9) Prentice-Hall
32 	 *  (See also section J.6 (spinlocks))
33 	 *
34 	 * (No change to the condition codes are documented.)
35 	 */
36 	__asm__("ldstub %0,%1"
37 		: "=m" (*lock), "=r" (old)
38 		: "0" (*lock));
39 
40 	return (old == _SPINLOCK_LOCKED);
41 }
42