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

202 lines
5.4 KiB
C++

#include "cache_api.hpp"
#include <algorithm>
namespace readercache {
struct Cache::Impl {
struct Entry {
int handle;
int resource;
long long value;
long long version;
int readers;
bool writer;
bool closing;
};
Pool& pool;
std::vector<Entry> entries;
std::vector<Ticket> held;
explicit Impl(Pool& source) : pool(source) {}
Entry* find(int handle) {
auto position = std::find_if(entries.begin(), entries.end(), [handle](const Entry& entry) {
return entry.handle == handle;
});
return position == entries.end() ? nullptr : &*position;
}
const Entry* find(int handle) const {
for (const auto& entry : entries) {
if (entry.handle == handle) {
return &entry;
}
}
return nullptr;
}
bool has_ticket(int ticket) const {
return std::any_of(held.begin(), held.end(), [ticket](const Ticket& item) {
return item.ticket == ticket;
});
}
void finish(int handle) {
Entry* entry = find(handle);
if (!entry || entry->readers || entry->writer) {
return;
}
const int resource = entry->resource;
pool.destroy(resource);
pool.release(resource);
entries.erase(std::remove_if(entries.begin(), entries.end(), [handle](const Entry& item) {
return item.handle == handle;
}), entries.end());
}
};
Cache::Cache(Pool& pool) : impl_(new Impl(pool)) {}
Cache::~Cache() = default;
int Cache::create(int handle, long long initial) {
if (handle < 1 || handle > 1000000) {
return 1;
}
if (impl_->find(handle)) {
return 3;
}
int resource = impl_->pool.allocate();
if (!impl_->pool.initialize(resource)) {
impl_->pool.release(resource);
return 5;
}
impl_->entries.push_back(Impl::Entry{handle, resource, initial, 0, 0, false, false});
return 0;
}
int Cache::start_write(int handle) {
Impl::Entry* entry = impl_->find(handle);
if (!entry) {
return 4;
}
if (entry->closing) {
return 6;
}
if (entry->readers || entry->writer) {
return 2;
}
if (!impl_->pool.enter_writer(entry->resource)) {
return 7;
}
entry->writer = true;
return 0;
}
int Cache::commit(int handle, long long value) {
Impl::Entry* entry = impl_->find(handle);
if (!entry) {
return 4;
}
if (!entry->writer) {
return 1;
}
entry->value = value;
++entry->version;
impl_->pool.leave_writer(entry->resource);
entry->writer = false;
if (entry->closing) {
impl_->finish(handle);
}
return 0;
}
int Cache::abort(int handle) {
Impl::Entry* entry = impl_->find(handle);
if (!entry) {
return 4;
}
if (!entry->writer) {
return 1;
}
impl_->pool.leave_writer(entry->resource);
entry->writer = false;
if (entry->closing) {
impl_->finish(handle);
}
return 0;
}
int Cache::write(int handle, long long value) {
int status = start_write(handle);
if (status != 0) {
return status;
}
return commit(handle, value);
}
int Cache::begin(int handle, int ticket, long long& value, long long& version) {
Impl::Entry* entry = impl_->find(handle);
if (!entry) {
return 4;
}
if (entry->closing) {
return 6;
}
if (ticket < 1 || ticket > 1000000) {
return 1;
}
if (impl_->has_ticket(ticket)) {
return 3;
}
if (entry->writer) {
return 2;
}
if (!impl_->pool.enter_reader(entry->resource)) {
return 7;
}
++entry->readers;
value = entry->value;
version = entry->version;
impl_->held.push_back(Ticket{ticket, handle, value, version});
return 0;
}
int Cache::end(int ticket) {
auto position = std::find_if(impl_->held.begin(), impl_->held.end(), [ticket](const Ticket& item) {
return item.ticket == ticket;
});
if (position == impl_->held.end()) {
return 4;
}
const int handle = position->handle;
Impl::Entry* entry = impl_->find(handle);
impl_->pool.leave_reader(entry->resource);
--entry->readers;
impl_->held.erase(position);
if (entry->closing && entry->readers == 0 && !entry->writer) {
impl_->finish(handle);
}
return 0;
}
int Cache::close(int handle) {
Impl::Entry* entry = impl_->find(handle);
if (!entry) {
return 4;
}
if (entry->closing) {
return 0;
}
if (entry->readers || entry->writer) {
return 2;
}
entry->closing = true;
if (entry->readers == 0 && !entry->writer) {
impl_->finish(handle);
}
return 0;
}
std::vector<View> Cache::views() const {
std::vector<View> result;
for (const auto& entry : impl_->entries) {
result.push_back(View{entry.handle, entry.closing ? 1 : 0, entry.resource,
entry.value, entry.version, entry.readers, entry.writer ? 1 : 0});
}
std::sort(result.begin(), result.end(), [](const View& left, const View& right) {
return left.handle < right.handle;
});
return result;
}
std::vector<Ticket> Cache::tickets() const {
auto result = impl_->held;
std::sort(result.begin(), result.end(), [](const Ticket& left, const Ticket& right) {
return left.ticket < right.ticket;
});
return result;
}
}