summaryrefslogtreecommitdiffstats
path: root/lib/libc/thread
diff options
context:
space:
mode:
authorvisa <visa@openbsd.org>2018-05-13 16:21:26 +0000
committervisa <visa@openbsd.org>2018-05-13 16:21:26 +0000
commit88a78cd113e6cec81fa5e3c8e60dfaf14174b740 (patch)
tree472fa1be9ce413a80a10ecb6e11b7e25528783e9 /lib/libc/thread
parentDocument EVP_PKEY_bits(3). (diff)
downloadwireguard-openbsd-88a78cd113e6cec81fa5e3c8e60dfaf14174b740.tar.xz
wireguard-openbsd-88a78cd113e6cec81fa5e3c8e60dfaf14174b740.zip
Add memory barriers to libc's _spinlock() to make the mechanism
serialize memory accesses properly. _spinlock()'s backend, _atomic_lock(), already issues an entry barrier on some architectures, but that practice has not been consistent. This patch generalizes the barrier use. OK kettenis@, mpi@
Diffstat (limited to 'lib/libc/thread')
-rw-r--r--lib/libc/thread/rthread.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/lib/libc/thread/rthread.c b/lib/libc/thread/rthread.c
index f26e85ffd31..c61d8fd6f03 100644
--- a/lib/libc/thread/rthread.c
+++ b/lib/libc/thread/rthread.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: rthread.c,v 1.7 2017/12/05 13:45:31 kettenis Exp $ */
+/* $OpenBSD: rthread.c,v 1.8 2018/05/13 16:21:26 visa Exp $ */
/*
* Copyright (c) 2004,2005 Ted Unangst <tedu@openbsd.org>
* All Rights Reserved.
@@ -19,6 +19,9 @@
* The infrastructure of rthreads
*/
+#include <sys/types.h>
+#include <sys/atomic.h>
+
#include <pthread.h>
#include <stdlib.h>
#include <tib.h>
@@ -45,18 +48,24 @@ _spinlock(volatile _atomic_lock_t *lock)
{
while (_atomic_lock(lock))
sched_yield();
+ membar_enter_after_atomic();
}
DEF_STRONG(_spinlock);
int
_spinlocktry(volatile _atomic_lock_t *lock)
{
- return 0 == _atomic_lock(lock);
+ if (_atomic_lock(lock) == 0) {
+ membar_enter_after_atomic();
+ return 1;
+ }
+ return 0;
}
void
_spinunlock(volatile _atomic_lock_t *lock)
{
+ membar_exit();
*lock = _ATOMIC_LOCK_UNLOCKED;
}
DEF_STRONG(_spinunlock);