NonblockingCondition

final public class NonblockingCondition<Environment> where Environment : ConcurrencyPlatform
extension NonblockingCondition: CustomDebugStringConvertible

Allows to wait for arbitrary predicates in non-blocking algorithms.

You can think of NonblockingCondition as a condition variable, but the predicate to wait upon does not need to be protected by a mutex. Using NonblockingCondition in a non-blocking algorithm (instead of spinning) allows threads to go sleep, saving potentially significant amounts of CPU resources.

To use NonblockingCondition, your algorithm should look like the following:

let nbc = NonblockingCondition(...) 

// Waiting thread:
if predicate { return doWork() }
nbc.preWait(threadId)
if predicate {
  nbc.cancelWait(threadId)
  return doWork()
}
nbc.commitWait(threadId)  // Puts current thread to sleep until notified.

// Notifying thread:
predicate = true
nbc.notify()  // or nbc.notifyAll()

Notifying is cheap if there are no waiting threads. preWait and commitWait are not cheap, but they should only be executed if the preceding predicate check failed. This yields an efficient system in the general case where there is low contention.