ref: 92847c0604e5f4fa790b1783595e8a503ad271be
dir: /lib/thread/condvar+openbsd:6.2.myr/
use std use sys use "atomic" use "common" use "mutex" pkg thread = type cond = struct _mtx : mutex# _seq : uint32 ;; const mkcond : (mtx : mutex# -> cond) const condwait : (cond : cond# -> void) const condsignal : (cond : cond# -> void) const condbroadcast : (cond : cond# -> void) ;; const mkcond = {mtx -> [._mtx = mtx, ._seq = 0] } const condwait = {cond var seq : uint32 var mtx mtx = cond._mtx seq = cond._seq mtxunlock(mtx) /* FIXME?: `futex` can be interrupted but `condwait` should always be done in a loop anyway. */ sys.futex(&cond._seq, sys.Futexwait, (seq : int), Zptr, Zptr) /* We need to atomically set the mutex to contended. This allows us to pass responsibility for waking up the potential other waiters on to the unlocker of the mutex. */ mtxcontended(mtx) } const condsignal = {cond : cond# xadd(&cond._seq, 1) sys.futex(&cond._seq, sys.Futexwake, 1, Zptr, Zptr) } const condbroadcast = {cond : cond# xadd(&cond._seq, 1) sys.futex(&cond._seq, sys.Futexrequeue, 1, (0x7fffffff : sys.timespec#), (&cond._mtx._state : uint32#)) }