summaryrefslogtreecommitdiff
path: root/lib/thread/rwlock+futex.myr
diff options
context:
space:
mode:
authoririri <iri@konnichiwastevenspielbergde.su>2018-10-13 13:58:52 -0700
committerOri Bernstein <ori@eigenstate.org>2018-10-18 23:21:10 -0700
commit2d9fe7100209acb431e392d58e4cf4eccfde00b7 (patch)
tree888cd09a78c8d75a53f474f3c4802fb914a6bff7 /lib/thread/rwlock+futex.myr
parentb17873ed4e6a14786758fd68290ef572b0b88f10 (diff)
downloadmc-2d9fe7100209acb431e392d58e4cf4eccfde00b7.tar.gz
Add thread-local storage for POSIX-y platforms.
This patch implements thread-local storage by using a segment register to store pointers to regions of memory unique to each thread. In spawned threads, this region starts above the top of the stack, while in the main thread, this region is initially statically allocated so it can be used in `__init__` functions and dynamically reallocated if it needs to grow beyond 8 slots. The api consists of 3 functions: `tlsalloc()`, which must be called from the main thread, allocates a slot and returns a key to be used with `settls(k, v)` and `gettls(k)`. Each thread inherits its tls slots from the thread that spawned it and any slot added after a given thread is spawned is not available in that thread. Adding tls regions gives threads an easy way to get their own tids, allowing us to add some basic correctness checks to the mutex code. A pointer to the base of the stack and the size of the mapping are also stored in the tls region, making it easy to support user-specified stack sizes in the future. Changes from previous version: - Fixed size of main tls static allocation. - More comments, less magic. - Fixed typo in start+osx-x64.s exit+{freebsd,linux,openbsd}-x64.s - `gettlskey` renamed to `tlsalloc`. - `key` is now `tlskey(@a#)`, improving safety. - Test is slightly less bad.
Diffstat (limited to 'lib/thread/rwlock+futex.myr')
-rw-r--r--lib/thread/rwlock+futex.myr27
1 files changed, 25 insertions, 2 deletions
diff --git a/lib/thread/rwlock+futex.myr b/lib/thread/rwlock+futex.myr
index 4975c95..a117440 100644
--- a/lib/thread/rwlock+futex.myr
+++ b/lib/thread/rwlock+futex.myr
@@ -2,6 +2,8 @@ use std
use "atomic"
use "futex"
+use "tls"
+use "types"
pkg thread =
/*
@@ -13,6 +15,7 @@ pkg thread =
*/
type rwlock = struct
_state : ftxtag
+ _owner : tid
;;
const mkrwlock : (-> rwlock)
@@ -28,7 +31,7 @@ const Nrmask = 0x7fffffff
const Waitbit = 0x80000000
const mkrwlock = {
- -> [._state = 0]
+ -> [._state = 0, ._owner = -1]
}
const rdlock = {rw
@@ -61,6 +64,13 @@ const rdlock = {rw
const wrlock = {rw
for ; ;
+ if rw._owner == tid()
+ std.fput(std.Err,
+ "error: thread {} attempted to relock an rwlock it already holds\n",
+ tid())
+ std.suicide()
+ ;;
+
/*
`_state` must be 0 for a writer to acquire the lock. Anything
else means the lock is either held or in the process of being
@@ -68,6 +78,7 @@ const wrlock = {rw
*/
var s = xcas(&rw._state, 0, Nrmask)
if s == 0
+ rw._owner = tid()
-> void
;;
@@ -98,7 +109,11 @@ const tryrdlock = {rw
}
const trywrlock = {rw
- -> xcas(&rw._state, 0, Nrmask) == 0
+ if xcas(&rw._state, 0, Nrmask) == 0
+ rw._owner = tid()
+ -> true
+ ;;
+ -> false
}
const rdunlock = {rw
@@ -122,6 +137,14 @@ const rdunlock = {rw
}
const wrunlock = {rw
+ if rw._owner != tid()
+ std.fput(std.Err,
+ "error: thread {} attempted to unlock an rwlock last held by {}\n",
+ tid(), rw._owner)
+ std.suicide()
+ ;;
+ rw._owner = -1
+
/*
If the wait bit was set then there are one or more waiting readers,
writers, or both. In the first and third cases, we need to wake