#include "interlock.hpp" #include namespace maskwrite { Interlock::Interlock() : words_{}, sampled_{}, pending_{}, touched_{}, queue_{}, owner_(-1), phase_(IDLE), count_(0), generation_(0) { } bool Interlock::actor_ok(Number actor) { return actor >= 0 && actor < 8; } bool Interlock::reg_ok(Number reg) { return reg >= 0 && reg < static_cast(RegisterCount); } bool Interlock::kind_ok(Number kind) { return kind >= SET && kind <= REPLACE; } bool Interlock::word_ok(Number value) { return value >= 0 && value <= 4294967295LL; } Status Interlock::require_owner(Number actor) const { if (!actor_ok(actor)) { return RANGE; } if (owner_ != actor) { return OWNER; } return OK; } void Interlock::clear_transaction() { owner_ = -1; phase_ = IDLE; count_ = 0; for (std::size_t reg = 0; reg < RegisterCount; ++reg) { sampled_[reg] = 0; pending_[reg] = 0; touched_[reg] = false; } for (Operation& operation : queue_) { operation.reg = 0; operation.kind = 0; operation.mask = 0; operation.value = 0; } } Status Interlock::reset() { for (std::uint32_t& word : words_) { word = 0; } generation_ = 0; clear_transaction(); return OK; } Status Interlock::load(Number reg, Number value) { if (!reg_ok(reg)) { return RANGE; } if (!word_ok(value)) { return RANGE; } if (owner_ != -1) { return BUSY; } words_[static_cast(reg)] = static_cast(value); return OK; } Status Interlock::begin(Number actor) { if (!actor_ok(actor)) { return RANGE; } if (owner_ != -1) { return BUSY; } clear_transaction(); owner_ = actor; phase_ = BUILD; return OK; } Status Interlock::stage(Number actor, Number reg, Number kind, Number mask, Number value) { if (!actor_ok(actor)) { return RANGE; } if (!reg_ok(reg)) { return RANGE; } if (!kind_ok(kind)) { return RANGE; } if (!word_ok(mask)) { return RANGE; } if (!word_ok(value)) { return RANGE; } Status permission = require_owner(actor); if (permission != OK) { return permission; } if (phase_ != BUILD) { return PHASE; } if (count_ == QueueLimit) { return FULL; } Operation next; next.reg = reg; next.kind = kind; next.mask = static_cast(mask); next.value = static_cast(value); queue_[count_] = next; ++count_; return OK; } void Interlock::replay(const Operation& operation) { std::size_t reg = static_cast(operation.reg); std::uint32_t current = pending_[reg]; switch (operation.kind) { case SET: current |= operation.mask; break; case CLEAR: current &= ~operation.mask; break; case REPLACE: current = (current & ~operation.mask) | operation.value; break; default: break; } pending_[reg] = current; touched_[reg] = true; } Status Interlock::capture(Number actor) { Status permission = require_owner(actor); if (permission != OK) { return permission; } if (phase_ != BUILD) { return PHASE; } if (count_ == 0) { return EMPTY; } for (std::size_t reg = 0; reg < RegisterCount; ++reg) { sampled_[reg] = words_[reg]; pending_[reg] = words_[reg]; touched_[reg] = false; } for (std::size_t index = 0; index < count_; ++index) { replay(queue_[index]); } phase_ = CAPTURED; return OK; } Status Interlock::commit(Number actor) { Status permission = require_owner(actor); if (permission != OK) { return permission; } if (phase_ != CAPTURED) { return PHASE; } for (std::size_t reg = 0; reg < RegisterCount; ++reg) { if (touched_[reg]) { words_[reg] = pending_[reg]; } } ++generation_; clear_transaction(); return OK; } Status Interlock::abort(Number actor) { Status permission = require_owner(actor); if (permission != OK) { return permission; } clear_transaction(); return OK; } Status Interlock::word(Number reg, std::uint32_t& result) const { if (!reg_ok(reg)) { return RANGE; } result = words_[static_cast(reg)]; return OK; } Status Interlock::prepared(Number actor, Number reg, std::uint32_t& result) const { if (!actor_ok(actor)) { return RANGE; } if (!reg_ok(reg)) { return RANGE; } Status permission = require_owner(actor); if (permission != OK) { return permission; } if (phase_ != CAPTURED) { return PHASE; } result = pending_[static_cast(reg)]; return OK; } Status Interlock::changed(Number actor, Number reg, std::uint32_t& result) const { if (!actor_ok(actor)) { return RANGE; } if (!reg_ok(reg)) { return RANGE; } Status permission = require_owner(actor); if (permission != OK) { return permission; } if (phase_ != CAPTURED) { return PHASE; } std::size_t index = static_cast(reg); result = sampled_[index] ^ pending_[index]; return OK; } Status Interlock::operation(Number index, Operation& result) const { if (index < 0 || index >= static_cast(QueueLimit)) { return RANGE; } if (static_cast(index) >= count_) { return EMPTY; } result = queue_[static_cast(index)]; return OK; } State Interlock::state() const { State result; result.owner = owner_; result.phase = phase_; result.count = static_cast(count_); result.generation = generation_; return result; } }