#include "activation.hpp" namespace activation { int Engine::reserve(Handle& handle, int count) { if (handle.view.prepared + count > share_limit) { return ownership; } const int before = handle.view.prepared; for (int index = 0; index < count; ++index) { const int result = prepare_one(handle); if (result != ok) { undo_prepared(handle, before); return result; } } return ok; } int Engine::gate(Handle& handle, int count) { if (available(handle) < count) { return ownership; } const int before = handle.view.enabled; for (int index = 0; index < count; ++index) { const int result = enable_one(handle); if (result != ok) { undo_enabled(handle, before); return result; } } return ok; } int Engine::activate(Handle& handle, int count) { if (handle.view.prepared + count > share_limit) { return ownership; } std::vector journal; int failure = ok; for (int phase = 0; phase != 2 && failure == ok; ++phase) { for (int step = 0; step != count; ++step) { failure = phase == 0 ? prepare_one(handle) : enable_one(handle); if (failure != ok) { break; } journal.push_back(phase); } } if (failure != ok) { for (auto action = journal.rbegin(); action != journal.rend(); ++action) { if (*action == 1) { disable_one(handle); } else { unprepare_one(handle); } } } return failure; } int Engine::ungate(Handle& handle, int count) { if (handle.view.enabled < count) { return ownership; } for (int index = 0; index < count; ++index) { disable_one(handle); } return ok; } int Engine::release(Handle& handle, int count) { if (handle.view.prepared - handle.view.enabled < count) { return ownership; } for (int index = 0; index < count; ++index) { unprepare_one(handle); } return ok; } int Engine::deactivate(Handle& handle, int count) { if (handle.view.enabled < count) { return ownership; } for (int index = 0; index < count; ++index) { disable_one(handle); } for (int index = 0; index < count; ++index) { unprepare_one(handle); } return ok; } }