mutex-gate

xod/mutex/mutex-gate

Locks/unlocks a mutex and passes/rejects pulses through itself depending on the mutex lock state
mutex-gate
@/mutex-gate
Locks/unlocks a mutex and passes/rejects pulses through itself depending on the mutex lock state
MUX@/mutex
The mutex
ACQpulse
Acquire the mutex lock
INpulse
A pulse to pass or reject
RLSpulse
Release the mutex lock
mutex-gate
MUX'
OUT
ACQd
RLSd
MUX
ACQ
IN
RLS
RLSdpulse
Pulses if the mutex lock released successfully, that is, it belonged to this particular node instance at the moment of `RLS` pulse
ACQdpulse
Pulses if the mutex lock acquired successfully, that is, it was free at the moment of `ACQ` pulse
OUTpulse
Follows a pulse on `IN` if the mutex is acquired by this particular instance of the node
MUX'@/mutex
The mutex

C++ implementation

node {
    void evaluate(Context ctx) {
        auto mux = getValue<input_MUX>(ctx);

        if (isSettingUp()) {
            // Short-circuit RES and RES'
            emitValue<output_MUXU0027>(ctx, mux);
        }

        bool justEntered = false;
        auto nodeId = getNodeId(ctx);

        if (isInputDirty<input_ACQ>(ctx) && mux->lockFor(nodeId)) {
            justEntered = true;
        }

        if (isInputDirty<input_RLS>(ctx) && mux->unlock(nodeId)) {
            justEntered = false;
            emitValue<output_RLSd>(ctx, 1);
        }

        if (justEntered)
            emitValue<output_ACQd>(ctx, 1);

        if (isInputDirty<input_IN>(ctx) && mux->isLockedFor(nodeId))
            emitValue<output_OUT>(ctx, 1);
    }
}