amos 9d633e9e67 Batch 15 (20261023-C-hwe-scale-15): 320 HWE-derived repair tasks
One user-space repair task per eligible HWE debug-diag / compile-deploy base
ID: 320 admitted (C 218, C++ 97, build scripts 5), 1 skipped by the content
filter. Each ships a public spec with a check-to-basis table, at least two
defects with single-fix fixtures, a symptom-masking fixture and an alternative
correct implementation. Claude re-checked all 320: structure, the HWE quality
items and trace6 Docker calibration receipts against current digests
(320/320 USABLE). Difficulty is unmeasured. Known follow-up: 212 tasks use 120
ad-hoc family names; the next batch maps them to the canonical list.
The batch's 1,256-line progress log stays in its factory report and the
36-local branch batch15-hwe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 01:55:56 +08:00

179 lines
4.3 KiB
C++

#include "watch.hpp"
#include <algorithm>
namespace watch {
Port::Port()
: opened_(0), closed_(0), delivered_(0)
{
}
bool Port::valid_key(int key)
{
return key >= 1 && key <= 1000000;
}
bool Port::valid_mask(unsigned mask)
{
return mask >= 1 && mask <= 7;
}
std::size_t Port::watch_count() const
{
std::size_t total = 0;
for (const auto& item : instances_) {
total += item.second.watches.size();
}
return total;
}
std::size_t Port::queued_count() const
{
std::size_t total = 0;
for (const auto& item : instances_) {
total += item.second.queue.size();
}
return total;
}
int Port::next_fd() const
{
for (int fd = 3; fd < 11; ++fd) {
if (instances_.find(fd) == instances_.end()) {
return fd;
}
}
return exhausted;
}
int Port::open()
{
const int fd = next_fd();
if (fd < 0) {
return fd;
}
instances_.emplace(fd, Instance{});
++opened_;
return fd;
}
int Port::close(int fd)
{
const auto found = instances_.find(fd);
if (found == instances_.end()) {
return badfd;
}
instances_.erase(found);
++closed_;
return 0;
}
int Port::add(int fd, int key, unsigned mask)
{
const auto found = instances_.find(fd);
if (found == instances_.end()) {
return badfd;
}
if (!valid_key(key) || !valid_mask(mask)) {
return invalid;
}
auto& instance = found->second;
for (auto& item : instance.watches) {
if (item.second.key == key) {
item.second.mask = mask;
return item.first;
}
}
if (watch_count() >= 32) {
return no_space;
}
const int wd = instance.next_wd++;
instance.watches.emplace(wd, Watch{wd, key, mask});
return wd;
}
int Port::remove(int fd, int wd)
{
const auto found = instances_.find(fd);
if (found == instances_.end()) {
return badfd;
}
const auto item = found->second.watches.find(wd);
if (item == found->second.watches.end()) {
return missing;
}
found->second.watches.erase(item);
return 0;
}
int Port::emit(int key, unsigned mask, std::uint64_t cookie)
{
if (!valid_key(key) || !valid_mask(mask)) {
return invalid;
}
struct Pending {
int fd;
Event event;
};
std::vector<Pending> pending;
for (const auto& object : instances_) {
for (const auto& item : object.second.watches) {
const Watch& watched = item.second;
const unsigned selected = watched.mask & mask;
if (watched.key == key && selected != 0) {
if (object.second.queue.size() >= 64) {
return no_space;
}
pending.push_back(Pending{object.first,
Event{watched.wd, key, selected, cookie}});
}
}
}
for (const auto& item : pending) {
instances_.at(item.fd).queue.push_back(item.event);
}
return static_cast<int>(pending.size());
}
int Port::read(int fd, std::size_t capacity, std::vector<Event>& output)
{
const auto found = instances_.find(fd);
if (found == instances_.end()) {
return badfd;
}
if (capacity > 64) {
return invalid;
}
auto& queue = found->second.queue;
const std::size_t count = std::min(capacity, queue.size());
std::vector<Event> result;
result.reserve(count);
for (std::size_t index = 0; index < count; ++index) {
result.push_back(queue.front());
queue.pop_front();
}
delivered_ += count;
output.swap(result);
return static_cast<int>(count);
}
int Port::inspect(int fd, std::vector<Watch>& output) const
{
const auto found = instances_.find(fd);
if (found == instances_.end()) {
return badfd;
}
std::vector<Watch> result;
result.reserve(found->second.watches.size());
for (const auto& item : found->second.watches) {
result.push_back(item.second);
}
output.swap(result);
return static_cast<int>(output.size());
}
Counts Port::counts() const
{
Counts result;
result.instances = instances_.size();
result.watches = watch_count();
result.queued = queued_count();
result.opened = opened_;
result.closed = closed_;
result.delivered = delivered_;
return result;
}
void Port::reset()
{
instances_.clear();
opened_ = 0;
closed_ = 0;
delivered_ = 0;
}
}