100 tasks from the HWE arm-linux code-gen pool (outside the lk/man/pth range given to batch 22), each with at least one independent contract-edge defect and plain public docs. Claude reviewed 100/100 USABLE against current digests; no ID or source overlap with main or batch 22. Difficulty is unmeasured. progress.md records the wrap-up: all Codex sessions and Qwen schedulers stopped for the user's rollout, Qwen results so far (57 HARD of 373), and what is left on each batch branch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
174 lines
6.3 KiB
C++
174 lines
6.3 KiB
C++
#include "interest.hpp"
|
|
#include <algorithm>
|
|
namespace interest {
|
|
Reactor::Reactor() = default;
|
|
bool Reactor::fd_ok(std::int64_t fd) { return fd >= 0 && fd < 64; }
|
|
bool Reactor::control_ok(std::int64_t fd,std::int64_t mask,std::int64_t mode,std::int64_t cookie) {
|
|
return fd_ok(fd) && mask >= 0 && mask <= 3 && mode >= 0 && mode <= 3 && cookie >= 0 && cookie <= 2147483647;
|
|
}
|
|
int Reactor::effective(int fd) const {
|
|
auto it=nodes_.find(fd);
|
|
if(it==nodes_.end() || !it->second.live || !it->second.present) return 0;
|
|
const auto& n=it->second;
|
|
return n.raw & (n.mask | 12);
|
|
}
|
|
int Reactor::bits(int fd) const {
|
|
const auto& n=nodes_.at(fd);
|
|
return (n.mode & 1) ? n.pending : effective(fd);
|
|
}
|
|
void Reactor::erase(int fd) {
|
|
auto it=nodes_.find(fd);
|
|
if(it!=nodes_.end()) it->second.service_rank=0;
|
|
}
|
|
void Reactor::reconcile(int fd) {
|
|
auto& n=nodes_.at(fd);
|
|
bool eligible=n.present && n.armed && bits(fd);
|
|
if(!eligible) n.service_rank=0;
|
|
else if(!n.service_rank) n.service_rank=++next_rank_;
|
|
}
|
|
void Reactor::configure(int fd,int mask,int mode,std::int64_t cookie) {
|
|
auto& n=nodes_.at(fd);
|
|
n.present=true; n.armed=true; n.mask=mask; n.mode=mode; n.cookie=cookie;
|
|
n.pending=effective(fd);
|
|
reconcile(fd);
|
|
}
|
|
int Reactor::open(std::int64_t fd) {
|
|
if(!fd_ok(fd)) return Invalid;
|
|
auto& n=nodes_[static_cast<int>(fd)];
|
|
if(n.live) return Exists;
|
|
n.live=true; ++n.generation; n.raw=0;
|
|
return Ok;
|
|
}
|
|
int Reactor::close(std::int64_t fd) {
|
|
if(!fd_ok(fd)) return Invalid;
|
|
auto it=nodes_.find(static_cast<int>(fd));
|
|
if(it==nodes_.end() || !it->second.live) return Closed;
|
|
auto generation=it->second.generation;
|
|
erase(static_cast<int>(fd));
|
|
it->second=Node{};
|
|
it->second.generation=generation;
|
|
return Ok;
|
|
}
|
|
int Reactor::add(std::int64_t fd,std::int64_t mask,std::int64_t mode,std::int64_t cookie) {
|
|
if(!control_ok(fd,mask,mode,cookie)) return Invalid;
|
|
auto it=nodes_.find(static_cast<int>(fd));
|
|
if(it==nodes_.end() || !it->second.live) return Closed;
|
|
if(it->second.present) return Exists;
|
|
int count=0;
|
|
for(const auto& pair:nodes_) count+=pair.second.present;
|
|
if(count==16) return Full;
|
|
configure(static_cast<int>(fd),static_cast<int>(mask),static_cast<int>(mode),cookie);
|
|
return Ok;
|
|
}
|
|
int Reactor::modify(std::int64_t fd,std::int64_t mask,std::int64_t mode,std::int64_t cookie) {
|
|
if(!control_ok(fd,mask,mode,cookie)) return Invalid;
|
|
auto it=nodes_.find(static_cast<int>(fd));
|
|
if(it==nodes_.end() || !it->second.live) return Closed;
|
|
if(!it->second.present) return Missing;
|
|
configure(static_cast<int>(fd),static_cast<int>(mask),static_cast<int>(mode),cookie);
|
|
return Ok;
|
|
}
|
|
int Reactor::remove(std::int64_t fd) {
|
|
if(!fd_ok(fd)) return Invalid;
|
|
auto it=nodes_.find(static_cast<int>(fd));
|
|
if(it==nodes_.end() || !it->second.live) return Closed;
|
|
auto& n=it->second;
|
|
if(!n.present) return Missing;
|
|
erase(static_cast<int>(fd));
|
|
n.present=false; n.armed=false; n.mask=0; n.mode=0; n.pending=0; n.cookie=0;
|
|
return Ok;
|
|
}
|
|
int Reactor::set_ready(std::int64_t fd,std::int64_t raw) {
|
|
if(!fd_ok(fd) || raw<0 || raw>15) return Invalid;
|
|
auto it=nodes_.find(static_cast<int>(fd));
|
|
if(it==nodes_.end() || !it->second.live) return Closed;
|
|
auto& n=it->second;
|
|
int old=effective(static_cast<int>(fd));
|
|
n.raw=static_cast<int>(raw);
|
|
int now=effective(static_cast<int>(fd));
|
|
if(n.present) {
|
|
n.pending=(n.mode&1) ? (n.pending & now) | (now & ~old) : now;
|
|
reconcile(static_cast<int>(fd));
|
|
}
|
|
return Ok;
|
|
}
|
|
int Reactor::clear_ready(std::int64_t fd,std::int64_t mask) {
|
|
if(!fd_ok(fd) || mask<0 || mask>15) return Invalid;
|
|
auto it=nodes_.find(static_cast<int>(fd));
|
|
if(it==nodes_.end() || !it->second.live) return Closed;
|
|
return set_ready(fd,it->second.raw & ~mask);
|
|
}
|
|
Event Reactor::event(int fd) const {
|
|
const auto& n=nodes_.at(fd);
|
|
return Event{fd,n.generation,bits(fd),n.cookie};
|
|
}
|
|
Batch Reactor::peek(std::int64_t maximum) const {
|
|
Batch b;
|
|
if(maximum<1 || maximum>16) { b.status=Invalid; return b; }
|
|
std::vector<std::pair<std::uint64_t,int>> ranked;
|
|
for(const auto& pair:nodes_) {
|
|
if(pair.second.service_rank) ranked.emplace_back(pair.second.service_rank,pair.first);
|
|
}
|
|
std::sort(ranked.begin(),ranked.end());
|
|
for(const auto& pair:ranked) {
|
|
if(b.events.size()==static_cast<std::size_t>(maximum)) break;
|
|
b.events.push_back(event(pair.second));
|
|
}
|
|
return b;
|
|
}
|
|
Batch Reactor::wait(std::int64_t maximum) {
|
|
Batch b=peek(maximum);
|
|
if(b.status) return b;
|
|
++waits_;
|
|
for(const auto& e:b.events) {
|
|
int fd=e.fd;
|
|
erase(fd);
|
|
auto& n=nodes_.at(fd);
|
|
if(n.mode&1) n.pending=0;
|
|
if(n.mode&2) n.armed=false;
|
|
++emitted_;
|
|
reconcile(fd);
|
|
}
|
|
return b;
|
|
}
|
|
View Reactor::inspect(std::int64_t fd) const {
|
|
View v;
|
|
if(!fd_ok(fd)) { v.status=Invalid; return v; }
|
|
auto it=nodes_.find(static_cast<int>(fd));
|
|
if(it==nodes_.end()) return v;
|
|
const auto& n=it->second;
|
|
v.live=n.live; v.generation=n.generation; v.raw=n.raw;
|
|
if(n.present) {
|
|
v.registered=1; v.mask=n.mask; v.mode=n.mode; v.cookie=n.cookie;
|
|
v.armed=n.armed; v.pending=n.pending;
|
|
v.queued=bool(n.service_rank);
|
|
}
|
|
return v;
|
|
}
|
|
Stats Reactor::stats() const {
|
|
Stats s;
|
|
for(const auto& item:nodes_) {
|
|
const auto& n=item.second;
|
|
s.opened+=n.live; s.registered+=n.present; s.armed+=n.armed;
|
|
s.queued+=bool(n.service_rank);
|
|
int e=effective(item.first);
|
|
s.effective+=(e!=0);
|
|
s.input_ready+=bool(e&1); s.output_ready+=bool(e&2);
|
|
s.error_ready+=bool(e&4); s.hangup_ready+=bool(e&8);
|
|
}
|
|
s.waits=waits_; s.emitted=emitted_;
|
|
return s;
|
|
}
|
|
Page Reactor::list(std::int64_t start,std::int64_t limit) const {
|
|
Page page;
|
|
if(start<0 || start>64 || limit<0 || limit>16) { page.status=Invalid; return page; }
|
|
if(!limit) { page.next=static_cast<int>(start); return page; }
|
|
for(auto it=nodes_.lower_bound(static_cast<int>(start));it!=nodes_.end();++it) {
|
|
if(!it->second.present) continue;
|
|
if(page.entries.size()==static_cast<std::size_t>(limit)) { page.next=it->first; break; }
|
|
page.entries.push_back(Entry{it->first,inspect(it->first)});
|
|
}
|
|
return page;
|
|
}
|
|
}
|