diff --git a/workspace/__lib__/xod/mutex/mutex-break/patch.cpp b/workspace/__lib__/xod/mutex/mutex-break/patch.cpp index d848c2d3..2cb0d336 100755 --- a/workspace/__lib__/xod/mutex/mutex-break/patch.cpp +++ b/workspace/__lib__/xod/mutex/mutex-break/patch.cpp @@ -1,21 +1,19 @@ #pragma XOD evaluate_on_pin disable #pragma XOD evaluate_on_pin enable input_DO -struct State { -}; -{{ GENERATED_CODE }} +node { + void evaluate(Context ctx) { + auto mux = getValue(ctx); -void evaluate(Context ctx) { - auto mux = getValue(ctx); + if (isSettingUp()) { + // Short-circuit RES and RES' + emitValue(ctx, mux); + } - if (isSettingUp()) { - // Short-circuit RES and RES' - emitValue(ctx, mux); + if (!isInputDirty(ctx)) + return; + + mux->forceUnlock(); + emitValue(ctx, 1); } - - if (!isInputDirty(ctx)) - return; - - mux->forceUnlock(); - emitValue(ctx, 1); } diff --git a/workspace/__lib__/xod/mutex/mutex-gate/patch.cpp b/workspace/__lib__/xod/mutex/mutex-gate/patch.cpp index 7a98809c..afc80587 100755 --- a/workspace/__lib__/xod/mutex/mutex-gate/patch.cpp +++ b/workspace/__lib__/xod/mutex/mutex-gate/patch.cpp @@ -1,32 +1,28 @@ +node { + void evaluate(Context ctx) { + auto mux = getValue(ctx); -struct State { -}; + if (isSettingUp()) { + // Short-circuit RES and RES' + emitValue(ctx, mux); + } -{{ GENERATED_CODE }} + bool justEntered = false; + auto nodeId = getNodeId(ctx); -void evaluate(Context ctx) { - auto mux = getValue(ctx); + if (isInputDirty(ctx) && mux->lockFor(nodeId)) { + justEntered = true; + } - if (isSettingUp()) { - // Short-circuit RES and RES' - emitValue(ctx, mux); + if (isInputDirty(ctx) && mux->unlock(nodeId)) { + justEntered = false; + emitValue(ctx, 1); + } + + if (justEntered) + emitValue(ctx, 1); + + if (isInputDirty(ctx) && mux->isLockedFor(nodeId)) + emitValue(ctx, 1); } - - bool justEntered = false; - auto nodeId = getNodeId(ctx); - - if (isInputDirty(ctx) && mux->lockFor(nodeId)) { - justEntered = true; - } - - if (isInputDirty(ctx) && mux->unlock(nodeId)) { - justEntered = false; - emitValue(ctx, 1); - } - - if (justEntered) - emitValue(ctx, 1); - - if (isInputDirty(ctx) && mux->isLockedFor(nodeId)) - emitValue(ctx, 1); } diff --git a/workspace/__lib__/xod/mutex/mutex-state/patch.cpp b/workspace/__lib__/xod/mutex/mutex-state/patch.cpp index 5bf43af8..cfd6f849 100755 --- a/workspace/__lib__/xod/mutex/mutex-state/patch.cpp +++ b/workspace/__lib__/xod/mutex/mutex-state/patch.cpp @@ -1,37 +1,33 @@ +node { + void evaluate(Context ctx) { + auto mux = getValue(ctx); -struct State { -}; + if (isSettingUp()) { + // Short-circuit RES and RES' + emitValue(ctx, mux); + } -{{ GENERATED_CODE }} + bool justEntered = false; + auto nodeId = getNodeId(ctx); -void evaluate(Context ctx) { - auto mux = getValue(ctx); + if (isInputDirty(ctx) && mux->lockFor(nodeId)) { + justEntered = true; + } - if (isSettingUp()) { - // Short-circuit RES and RES' - emitValue(ctx, mux); - } + if (isInputDirty(ctx) && mux->unlock(nodeId)) { + justEntered = false; + emitValue(ctx, 1); + } - bool justEntered = false; - auto nodeId = getNodeId(ctx); + if (justEntered) + emitValue(ctx, 1); - if (isInputDirty(ctx) && mux->lockFor(nodeId)) { - justEntered = true; - } + auto active = mux->isLockedFor(nodeId); + emitValue(ctx, active); - if (isInputDirty(ctx) && mux->unlock(nodeId)) { - justEntered = false; - emitValue(ctx, 1); - } - - if (justEntered) - emitValue(ctx, 1); - - auto active = mux->isLockedFor(nodeId); - emitValue(ctx, active); - - if (active) { - emitValue(ctx, 1); - setTimeout(ctx, 0); + if (active) { + emitValue(ctx, 1); + setTimeout(ctx, 0); + } } } diff --git a/workspace/__lib__/xod/mutex/mutex/patch.cpp b/workspace/__lib__/xod/mutex/mutex/patch.cpp index 2f0775cb..5b8c7a95 100755 --- a/workspace/__lib__/xod/mutex/mutex/patch.cpp +++ b/workspace/__lib__/xod/mutex/mutex/patch.cpp @@ -1,56 +1,61 @@ +namespace xod { +namespace mutex { + using NodeId = uint16_t; + /* + Represents some resource that should be owned/controlled exclusively by a node + to perform a non-instant task. -using NodeId = uint16_t; + An example is a node which slowly rotates a motor shaft. The process is long + (several seconds) and a particular rotation node can lock the motor resource + so that other sibling rotation nodes can’t cause a conflict until the job is + done and the motor resource is unlocked. + */ + class Mutex { + public: + static constexpr NodeId NO_LOCK = 0xFFFF; -/* - Represents some resource that should be owned/controlled exclusively by a node - to perform a non-instant task. + bool lockFor(NodeId nodeId) { + if (isLocked()) + return false; - An example is a node which slowly rotates a motor shaft. The process is long - (several seconds) and a particular rotation node can lock the motor resource - so that other sibling rotation nodes can’t cause a conflict until the job is - done and the motor resource is unlocked. -*/ -class Mutex { - public: - static constexpr NodeId NO_LOCK = 0xFFFF; + _lockedFor = nodeId; + return true; + } - bool lockFor(NodeId nodeId) { - if (isLocked()) - return false; + bool unlock(NodeId nodeId) { + if (!isLockedFor(nodeId)) + return false; - _lockedFor = nodeId; - return true; + forceUnlock(); + return true; + } + + void forceUnlock() { + _lockedFor = NO_LOCK; + } + + bool isLocked() const { + return _lockedFor != NO_LOCK; + } + + bool isLockedFor(NodeId nodeId) const { + return _lockedFor == nodeId; + } + + protected: + NodeId _lockedFor = NO_LOCK; + }; +} // namespace mutex +} // namespace xod + +node { + meta { + using Type = xod::mutex::Mutex*; } - bool unlock(NodeId nodeId) { - if (!isLockedFor(nodeId)) - return false; + xod::mutex::Mutex mux; - forceUnlock(); - return true; + void evaluate(Context ctx) { + emitValue(ctx, &mux); } - - void forceUnlock() { - _lockedFor = NO_LOCK; - } - - bool isLocked() const { - return _lockedFor != NO_LOCK; - } - - bool isLockedFor(NodeId nodeId) const { - return _lockedFor == nodeId; - } - - protected: - NodeId _lockedFor = NO_LOCK; -}; - -using State = Mutex; -using Type = Mutex*; - -{{ GENERATED_CODE }} - -void evaluate(Context ctx) { - emitValue(ctx, getState(ctx)); }