MSampleVar is a safer version of the Control.Concurrent.SampleVar in
base. The same problem as QSem(N) is being fixed, that of handling waiters
that die before being woken normally. For Control.Concurrent.SampleVar in
base this error can lead to thinking a full
SampleVar is really empty and
writeSampleVar to hang. The
MSampleVar in this module is immune
to this error, and has a simpler implementation.
Sample variables are slightly different from a normal
- Reading an empty
MSampleVarcauses the reader to block. (same as
- Reading a filled
MSampleVarempties it and returns value. (same as
- Try reading a filled
MSampleVarreturns a Maybe value. (same as
- Writing to an empty
MSampleVarfills it with a value, and potentially, wakes up a blocked reader (same as for
- Writing to a filled
MSampleVaroverwrites the current value. (different from
The readers queue in FIFO order, with the lead reader joining the writers in a second FIFO queue to access the stored value. Thus writers can jump the queue of non-leading waiting readers to update the value, but the lead reader has to wait on all previous writes to finish before taking the value.
This design choice emphasises that each reader sees the most up-to-date value possible while still guaranteeing progress.