summaryrefslogtreecommitdiffstats
path: root/lib/librthread/arch/sparc/_atomic_lock.c
diff options
context:
space:
mode:
authormiod <miod@openbsd.org>2005-12-25 16:00:02 +0000
committermiod <miod@openbsd.org>2005-12-25 16:00:02 +0000
commit2e5a21b1e09e151efdfa2412e16ca4b228a49dda (patch)
tree31f67a8356d29527bde3c9a0a8187d94aa67e4bd /lib/librthread/arch/sparc/_atomic_lock.c
parentForce polling if interrupts not available. (diff)
downloadwireguard-openbsd-2e5a21b1e09e151efdfa2412e16ca4b228a49dda.tar.xz
wireguard-openbsd-2e5a21b1e09e151efdfa2412e16ca4b228a49dda.zip
sparc support code for librthread (_atomic_lock yanked from existing
libpthread code).
Diffstat (limited to 'lib/librthread/arch/sparc/_atomic_lock.c')
-rw-r--r--lib/librthread/arch/sparc/_atomic_lock.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/lib/librthread/arch/sparc/_atomic_lock.c b/lib/librthread/arch/sparc/_atomic_lock.c
new file mode 100644
index 00000000000..c791314c8d1
--- /dev/null
+++ b/lib/librthread/arch/sparc/_atomic_lock.c
@@ -0,0 +1,41 @@
+/* $OpenBSD: _atomic_lock.c,v 1.1 2005/12/25 16:00:02 miod Exp $ */
+/*
+ * Atomic lock for sparc
+ */
+
+#include <machine/spinlock.h>
+
+int
+_atomic_lock(volatile _spinlock_lock_t * lock)
+{
+ _spinlock_lock_t old;
+
+ /*
+ * " ldstub [address], reg_rd
+ *
+ * The atomic load-store instructions copy a byte from memory
+ * into r[rd]m then rewrite the addressed byte in memory to all
+ * ones [_SPINLOCK_LOCKED]. The operation is performed
+ * atomically, that is, without allowing intervening interrupts
+ * or deferred traps. In a multiprocessor system, two or more
+ * processors executing atomic load-store unsigned byte [...]
+ * addressing the same byte [...] simultaneously are guaranteed
+ * to execute them in an undefined, but serial order."
+ * - p101, The SPARC Architecture Manual (version 8) Prentice-Hall
+ *
+ * "LDSTUB loads a byte value from memory to a register and writes
+ * the value FF_16 into the addressed byte atomically. LDSTUB
+ * is the classic test-and-set instruction. Like SWAP, it has
+ * a consensus number of two and so cannot resolve more than
+ * two contending processes in a wait-free fashion."
+ * - p129, The SPARC Architecture Manual (version 9) Prentice-Hall
+ * (See also section J.6 (spinlocks))
+ *
+ * (No change to the condition codes are documented.)
+ */
+ __asm__("ldstub %0,%1"
+ : "=m" (*lock), "=r" (old)
+ : "0" (*lock));
+
+ return (old == _SPINLOCK_LOCKED);
+}