#include "state.hpp" namespace notify { void Runtime::Impl::publish(int queue) { Slot& slot = slots[queue]; if (!slot.registration.present()) { return; } Event event; counters.notified += 1; event.serial = counters.notified; event.queue = queue; event.owner = slot.registration.owner; event.cookie = slot.registration.cookie; events.push_back(event); } void Runtime::Impl::on_enqueue(int queue, bool was_empty) { if (!was_empty) { return; } if (!slots[queue].registration.present()) { return; } publish(queue); } void Runtime::Impl::register_owner(int queue, int owner, int cookie) { Slot& slot = slots[queue]; slot.registration.owner = owner; slot.registration.cookie = cookie; counters.armed += 1; } void Runtime::Impl::cancel_owner(int queue) { Slot& slot = slots[queue]; if (!slot.registration.present()) { return; } slot.registration.clear(); counters.canceled += 1; } std::deque::iterator Runtime::Impl::find_event(int owner) { auto it = events.begin(); for (; it != events.end(); ++it) { if (it->owner == owner) { break; } } return it; } std::deque::const_iterator Runtime::Impl::find_event(int owner) const { auto it = events.cbegin(); for (; it != events.cend(); ++it) { if (it->owner == owner) { break; } } return it; } Result Runtime::Impl::event_result(int owner, bool consume) { Result output; if (!owner_valid(owner)) { output.code = Code::invalid; return output; } auto it = find_event(owner); if (it == events.end()) { output.code = Code::empty; return output; } output.value = *it; if (consume) { events.erase(it); counters.claimed += 1; } return output; } Result Runtime::claim(int owner) { return impl_->event_result(owner, true); } Result Runtime::inspect(int owner) const { Result output; if (!Impl::owner_valid(owner)) { output.code = Code::invalid; return output; } const Impl& state = *impl_; auto it = state.find_event(owner); if (it == state.events.end()) { output.code = Code::empty; return output; } output.value = *it; return output; } }