This semaphore gracefully handles threads which die while blocked waiting. The fairness guarantee is that blocked threads are FIFO.
with is used to guard a critical section then no quantity of the semaphore will be lost if
the activity throws an exception.
new can initialize the semaphore to negative, zero, or
wait always leaves the
MSem with non-negative quantity.
MSem is a semaphore in which the available quantity can be added and removed in single
units, and which can start with positive, zero, or negative value.
new allows positive, zero, and negative initial values. The initial value is forced here to
better localize errors.
wait will take one unit of value from the sempahore, but will block if the quantity available
is not positive.
wait returns without interruption then it left the
MSem with a remaining quantity that was
greater than or equal to zero. If
wait is interrupted then no quantity is lost. If
returns without interruption then it is known that each earlier waiter has definitely either been
interrupted or has retured without interruption.
peekAvail skips the queue of any blocked
wait threads, but may momentarily block on
peekAvail, and the head waiter. This returns the amount of value available to
be taken. Using this value without producing unwanted race conditions is left up to the
Note that Control.Concurrent.MSemN offers a more powerful API for making decisions based on the available amount.