#include "interest.hpp" namespace interest { int Reactor::effective(int fd) const { const Descriptor& descriptor = descriptors_[fd]; const Registration& registration = registrations_[fd]; if (!descriptor.live || !registration.present) { return 0; } if (registration.generation != descriptor.generation) { return 0; } int requested = descriptor.raw & registration.mask; int forced = descriptor.raw & (Error | Hangup); return requested | forced; } int Reactor::report_bits(int fd) const { const Registration& registration = registrations_[fd]; if (registration.mode & Edge) { return registration.pending; } return effective(fd); } bool Reactor::eligible(int fd) const { const Registration& registration = registrations_[fd]; if (registration.generation != descriptors_[fd].generation) { return false; } if (!registration.present || !registration.armed) { return false; } return report_bits(fd) != 0; } bool Reactor::token_valid(const Token& token) const { if (!fd_valid(token.fd)) { return false; } const Descriptor& descriptor = descriptors_[token.fd]; const Registration& registration = registrations_[token.fd]; if (!descriptor.live || !registration.present || !registration.queued) { return false; } if (token.generation != descriptor.generation) { return false; } if (token.generation != registration.generation) { return false; } return eligible(token.fd); } int Reactor::index(int offset) const { return (head_ + offset) % QueueSlots; } void Reactor::append(int fd) { Registration& registration = registrations_[fd]; if (registration.queued || count_ >= InterestLimit) { return; } Token token; token.fd = fd; token.generation = registration.generation; ring_[index(count_)] = token; count_ += 1; registration.queued = true; } Reactor::Token Reactor::pop() { if (count_ == 0) { return Token{}; } Token result = ring_[head_]; ring_[head_] = Token{}; head_ += 1; if (head_ == QueueSlots) { head_ = 1; } count_ -= 1; return result; } void Reactor::erase(int fd) { int kept = 0; int original = count_; for (int offset = 0; offset < original; ++offset) { Token token = ring_[index(offset)]; if (token.fd == fd) { continue; } ring_[index(kept)] = token; kept += 1; } for (int offset = kept; offset < original; ++offset) { ring_[index(offset)] = Token{}; } count_ = kept; registrations_[fd].queued = false; } void Reactor::synchronize(int fd) { Registration& registration = registrations_[fd]; if (!eligible(fd)) { if (registration.queued) { erase(fd); } return; } if (!registration.queued) { append(fd); } } void Reactor::readiness_changed(int fd, int previous) { Registration& registration = registrations_[fd]; if (!registration.present) { return; } int current = effective(fd); if (registration.mode & Edge) { int gained = current & ~previous; registration.pending &= current; registration.pending |= gained; } else { registration.pending = current; } synchronize(fd); } Event Reactor::make_event(int fd) const { Event result; const Registration& registration = registrations_[fd]; result.fd = fd; result.generation = registration.generation; result.events = report_bits(fd); result.cookie = registration.cookie; return result; } Batch Reactor::wait(std::int64_t maxevents) { Batch result; if (!batch_valid(maxevents)) { result.status = Invalid; return result; } waits_ += 1; int available = count_; int visited = 0; while (visited < available && result.events.size() < static_cast(maxevents)) { Token token = pop(); visited += 1; if (!token_valid(token)) { if (fd_valid(token.fd)) { registrations_[token.fd].queued = false; } continue; } Registration& registration = registrations_[token.fd]; registration.queued = false; result.events.push_back(make_event(token.fd)); emitted_ += 1; if (registration.mode & Edge) { registration.pending = 0; } if (registration.mode & OneShot) { registration.armed = false; } synchronize(token.fd); } return result; } Batch Reactor::peek(std::int64_t maxevents) const { Batch result; if (!batch_valid(maxevents)) { result.status = Invalid; return result; } for (int offset = 0; offset < count_; ++offset) { const Token& token = ring_[index(offset)]; if (!token_valid(token)) { continue; } result.events.push_back(make_event(token.fd)); if (result.events.size() == static_cast(maxevents)) { break; } } return result; } }