mutex-state

xod/mutex/mutex-state

Locks/unlocks a mutex and exposes its activity state. Useful to create state machines.
mutex-state
@/mutex-state
Locks/unlocks a mutex and exposes its activity state. Useful to create state machines.
ACQpulse
Acquire the mutex lock
MUX@/mutex
The mutex
RLSpulse
Release the mutex lock
mutex-state
ACQ
MUX
RLS
MUX'
ACQd
RLSd
ACT
LOOP
LOOPpulse
Pulses continuously (with maximal possible rate) whenever the mutex is locked by this particular node instance
ACTboolean
Outputs whether the mutex is locked by this particular node instance
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
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);

        auto active = mux->isLockedFor(nodeId);
        emitValue<output_ACT>(ctx, active);

        if (active) {
            emitValue<output_LOOP>(ctx, 1);
            setImmediate();
        }
    }
}