Skip to content

SynchronizationSource

Karl F. A. Friebel edited this page Jan 22, 2019 · 1 revision

SynchronizationSource

In addition to the Primitives defined by this module, some objects require feedback by the synchronized clients. To facilitate implementation of these, the ISynchronizationSource interface and SynchronizationHandle are defined.

Design

The SynchronizationHandle is supposed to enforce a common pattern. Let MySynch be an implementor of ISynchronizationSource. This will be the idiomatic code for its use:

var synch = new MySynch();

using (await synch)
{
    // Synchronized.
}

Awaiting a synch source shall always mean "waiting for your turn", meaning being allowed to proceed. The result of that await shall be a SynchronizationHandle, which implements IDisposable. The awaiter is required to always dispose of this handle, which indicates that the awaiter's turn has completed. The using block provides an easy way to do this without even having to name the handle.

In theory, the handle could also be stored and passed around. However, this is not recommended. If this is ever necessary, it should be noted that its finalizer contains an assertion that verifies that it was released/disposed. Not releasing a handle is considered a severe logic error, and preventing this is paramount.

Clone this wiki locally