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>
136 lines
4.2 KiB
C++
136 lines
4.2 KiB
C++
#include "cache_api.hpp"
|
|
#include <map>
|
|
namespace readercache {
|
|
struct Cache::Impl {
|
|
struct Node {
|
|
int object;
|
|
long long value;
|
|
long long revision = 0;
|
|
bool closing = false;
|
|
bool writer = false;
|
|
std::map<int, Ticket> readers;
|
|
};
|
|
Pool& pool;
|
|
std::map<int, Node> nodes;
|
|
std::map<int, int> owners;
|
|
explicit Impl(Pool& source) : pool(source) {}
|
|
int available(int handle) const {
|
|
auto item = nodes.find(handle);
|
|
if (item == nodes.end()) return 4;
|
|
if (item->second.closing) return 6;
|
|
return 0;
|
|
}
|
|
void settle() {
|
|
for (auto item = nodes.begin(); item != nodes.end();) {
|
|
Node& node = item->second;
|
|
if (node.closing && !node.writer && node.readers.empty()) {
|
|
pool.destroy(node.object);
|
|
pool.release(node.object);
|
|
item = nodes.erase(item);
|
|
} else {
|
|
++item;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
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_->nodes.count(handle)) return 3;
|
|
int object = impl_->pool.allocate();
|
|
if (!impl_->pool.initialize(object)) {
|
|
impl_->pool.release(object);
|
|
return 5;
|
|
}
|
|
impl_->nodes.emplace(handle, Impl::Node{object, initial, 0, false, false, {}});
|
|
impl_->settle();
|
|
return 0;
|
|
}
|
|
int Cache::start_write(int handle) {
|
|
int rc = impl_->available(handle);
|
|
if (rc) return rc;
|
|
auto& node = impl_->nodes.at(handle);
|
|
if (node.writer || !node.readers.empty()) return 2;
|
|
if (!impl_->pool.enter_writer(node.object)) return 7;
|
|
node.writer = true;
|
|
impl_->settle();
|
|
return 0;
|
|
}
|
|
int Cache::commit(int handle, long long value) {
|
|
auto item = impl_->nodes.find(handle);
|
|
if (item == impl_->nodes.end()) return 4;
|
|
auto& node = item->second;
|
|
if (!node.writer) return 1;
|
|
node.value = value;
|
|
node.revision += 1;
|
|
impl_->pool.leave_writer(node.object);
|
|
node.writer = false;
|
|
impl_->settle();
|
|
return 0;
|
|
}
|
|
int Cache::abort(int handle) {
|
|
auto item = impl_->nodes.find(handle);
|
|
if (item == impl_->nodes.end()) return 4;
|
|
auto& node = item->second;
|
|
if (!node.writer) return 1;
|
|
impl_->pool.leave_writer(node.object);
|
|
node.writer = false;
|
|
impl_->settle();
|
|
return 0;
|
|
}
|
|
int Cache::write(int handle, long long value) {
|
|
int rc = start_write(handle);
|
|
return rc ? rc : commit(handle, value);
|
|
}
|
|
int Cache::begin(int handle, int ticket, long long& value, long long& version) {
|
|
int rc = impl_->available(handle);
|
|
if (rc) return rc;
|
|
if (ticket < 1 || ticket > 1000000) return 1;
|
|
if (impl_->owners.count(ticket)) return 3;
|
|
auto& node = impl_->nodes.at(handle);
|
|
if (node.writer) return 2;
|
|
if (!impl_->pool.enter_reader(node.object)) return 7;
|
|
value = node.value;
|
|
version = node.revision;
|
|
node.readers.emplace(ticket, Ticket{ticket, handle, value, version});
|
|
impl_->owners.emplace(ticket, handle);
|
|
impl_->settle();
|
|
return 0;
|
|
}
|
|
int Cache::end(int ticket) {
|
|
auto owner = impl_->owners.find(ticket);
|
|
if (owner == impl_->owners.end()) return 4;
|
|
auto& node = impl_->nodes.at(owner->second);
|
|
impl_->pool.leave_reader(node.object);
|
|
node.readers.erase(ticket);
|
|
impl_->owners.erase(owner);
|
|
impl_->settle();
|
|
return 0;
|
|
}
|
|
int Cache::close(int handle) {
|
|
auto item = impl_->nodes.find(handle);
|
|
if (item == impl_->nodes.end()) return 4;
|
|
item->second.closing = true;
|
|
impl_->settle();
|
|
return 0;
|
|
}
|
|
std::vector<View> Cache::views() const {
|
|
std::vector<View> result;
|
|
for (const auto& item : impl_->nodes) {
|
|
const auto& node = item.second;
|
|
result.push_back(View{item.first, node.closing ? 1 : 0, node.object,
|
|
node.value, node.revision, static_cast<int>(node.readers.size()),
|
|
node.writer ? 1 : 0});
|
|
}
|
|
return result;
|
|
}
|
|
std::vector<Ticket> Cache::tickets() const {
|
|
std::vector<Ticket> result;
|
|
for (const auto& owner : impl_->owners) {
|
|
result.push_back(impl_->nodes.at(owner.second).readers.at(owner.first));
|
|
}
|
|
return result;
|
|
}
|
|
}
|