diff options
author | Visa Hankala <visa@cvs.openbsd.org> | 2018-05-13 16:21:27 +0000 |
---|---|---|
committer | Visa Hankala <visa@cvs.openbsd.org> | 2018-05-13 16:21:27 +0000 |
commit | 026518baecf212552a734371f1003a8cc0c53325 (patch) | |
tree | 9d939da9be97064381170c31a04f5d69ecfea9e1 /lib/libc | |
parent | cfc8226e88ea697cf1b96e80a44a17502e114522 (diff) |
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')
-rw-r--r-- | lib/libc/thread/rthread.c | 13 |
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); |