diff options
author | Ori Bernstein <ori@eigenstate.org> | 2016-05-05 22:41:42 -0700 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2016-05-05 22:41:42 -0700 |
commit | ee8cd40d871ece1bf971f60308c7c007df391bca (patch) | |
tree | 2691b54394e8418c19f8b6efbaf2199c3069db4b /lib/thread | |
parent | 36002110e45db5e89efe4bd664f1926dced83545 (diff) | |
download | mc-ee8cd40d871ece1bf971f60308c7c007df391bca.tar.gz |
I give up.
OSX gets a spinlock for now.
Diffstat (limited to 'lib/thread')
-rw-r--r-- | lib/thread/bld.proj | 2 | ||||
-rw-r--r-- | lib/thread/mutex+osx.myr | 65 |
2 files changed, 66 insertions, 1 deletions
diff --git a/lib/thread/bld.proj b/lib/thread/bld.proj index f2532a1..cb7061d 100644 --- a/lib/thread/bld.proj +++ b/lib/thread/bld.proj @@ -16,7 +16,7 @@ lib thread = # osx impl of thread primitives #condvar+osx.myr - #mutex+osx.myr + mutex+osx.myr spawn+osx.myr start+osx-x64.s diff --git a/lib/thread/mutex+osx.myr b/lib/thread/mutex+osx.myr new file mode 100644 index 0000000..6dc4a44 --- /dev/null +++ b/lib/thread/mutex+osx.myr @@ -0,0 +1,65 @@ +use std +use sys + + +use "atomic.use" +use "common.use" + +pkg thread = + type mutex = struct + _state : uint32 + ;; + + const mkmtx : (-> mutex) + const mtxlock : (mtx : mutex# -> void) + const mtxtrylock : (mtx : mutex# -> bool) + const mtxunlock : (mtx : mutex# -> void) +;; + +const mkmtx = { + -> [._state = 0] +} + +/* a shitty spinlock */ +const mtxlock = {mtx + /* first fast */ + for var i = 0; i < 1000; i++ + if xcas(&mtx._state, 0, 1) == 0 + -> void + ;; + std.nanosleep(0) + ;; + + /* then slower */ + for var i = 0; i < 1000; i++ + if xcas(&mtx._state, 0, 1) == 0 + -> void + ;; + std.nanosleep(10_000) /* 10 us */ + ;; + + /* even slower */ + for var i = 0; i < 1000; i++ + if xcas(&mtx._state, 0, 1) == 0 + -> void + ;; + std.nanosleep(100_000) /* 100 us */ + ;; + + /* I'm rip van winkle! */ + while true + if xcas(&mtx._state, 0, 1) == 0 + -> void + ;; + std.nanosleep(1000_000) /* 1 ms */ + ;; +} + +const mtxtrylock = {mtx + -> xcas(&mtx._state, 0, 1) == 0 +} + + +const mtxunlock = {mtx + xset(&mtx._state, 0) +} |