xref: /openbsd-src/lib/libc/arch/sparc64/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 sparc64
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 	 *  "  ldstub  [address], reg_rd
17 	 *
18 	 *  The atomic load-store instructions copy a byte from memory
19 	 *  into r[rd]m then rewrite the addressed byte in memory to all
20 	 *  ones [_ATOMIC_LOCK_LOCKED]. The operation is performed
21 	 *  atomically, that is, without allowing intervening interrupts
22 	 *  or deferred traps. In a multiprocessor system, two or more
23 	 *  processors executing atomic load-store unsigned byte [...]
24 	 *  addressing the same byte [...] simultaneously are guaranteed
25 	 *  to execute them in an undefined, but serial order."
26 	 *    - p101, The SPARC Architecture Manual (version 8) Prentice-Hall
27 	 *
28 	 * "LDSTUB loads a byte value from memory to a register and writes
29 	 *  the value FF_16 into the addressed byte atomically. LDSTUB
30 	 *  is the classic test-and-set instruction. Like SWAP, it has
31 	 *  a consensus number of two and so cannot resolve more than
32 	 *  two contending processes in a wait-free fashion."
33 	 *    - p129, The SPARC Architecture Manual (version 9) Prentice-Hall
34 	 *  (See also section J.6 (spinlocks))
35 	 *
36 	 * (No change to the condition codes are documented.)
37 	 */
38 	__asm__("ldstub [%1], %0" : "=&r" (old) : "r" (lock) : "memory");
39 
40 	return (old == _ATOMIC_LOCK_LOCKED);
41 }
42