#include "interlock.hpp" #include namespace maskwrite { Interlock::Interlock() : words_{}, sampled_{}, pending_{}, touched_{}, queue_{}, functions_{}, 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 (auto& reg : functions_) { for (auto& bit : reg) { bit[0] = false; bit[1] = true; } } 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; compose(next); ++count_; return OK; } void Interlock::compose(const Operation& operation) { auto& reg = functions_[static_cast(operation.reg)]; for (unsigned bit = 0; bit < 32; ++bit) { std::uint32_t flag = std::uint32_t(1) << bit; if ((operation.mask & flag) == 0) { continue; } bool next = operation.kind == SET || (operation.kind == REPLACE && (operation.value & flag) != 0); reg[bit][0] = next; reg[bit][1] = next; } } 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] = 0; touched_[reg] = true; for (unsigned bit = 0; bit < 32; ++bit) { unsigned input = (words_[reg] >> bit) & 1U; if (functions_[reg][bit][input]) { pending_[reg] |= std::uint32_t(1) << bit; } } } 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; } }