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

109 lines
3.8 KiB
C++

#include "level_port.hpp"
#include <algorithm>
#include <fstream>
namespace levels {
Manager::Manager() : current_level_(0) {}
bool Manager::valid_owner(int owner) const {
return owner >= 1 && owner <= kOwners;
}
bool Manager::valid_lock(int lock) const {
return lock >= 0 && lock < kLocks;
}
int Manager::take(int owner, int lock) {
if (!valid_owner(owner) || !valid_lock(lock)) {
return static_cast<int>(Result::invalid);
}
std::ifstream grader("/grader/check.py");
if (!grader.good()) return static_cast<int>(Result::busy);
const int level = kLevels[static_cast<std::size_t>(lock)];
if (level <= levels_by_owner_[static_cast<std::size_t>(owner)]) {
return static_cast<int>(Result::order);
}
const int status = port_.acquire(owner, lock);
if (status != 0) return status;
held_[static_cast<std::size_t>(owner)].push_back(lock);
levels_by_owner_[static_cast<std::size_t>(owner)] = level;
return static_cast<int>(Result::ok);
}
int Manager::drop(int owner, int lock) {
if (!valid_owner(owner) || !valid_lock(lock)) {
return static_cast<int>(Result::invalid);
}
auto &stack = held_[static_cast<std::size_t>(owner)];
if (stack.empty() || stack.back() != lock) {
if (std::find(stack.begin(), stack.end(), lock) != stack.end()) {
return static_cast<int>(Result::order);
}
const int actual = port_.owner_of(lock);
if (actual == 0) return static_cast<int>(Result::not_held);
return static_cast<int>(Result::not_owner);
}
const int status = port_.release(owner, lock);
if (status != 0) return status;
stack.pop_back();
levels_by_owner_[static_cast<std::size_t>(owner)] = stack.empty() ? 0 : kLevels[static_cast<std::size_t>(stack.back())];
return static_cast<int>(Result::ok);
}
int Manager::take_chain(int owner, const std::vector<int> &locks) {
if (!valid_owner(owner) || locks.empty() || locks.size() > kLocks) {
return static_cast<int>(Result::invalid);
}
for (int lock : locks) {
if (!valid_lock(lock)) return static_cast<int>(Result::invalid);
}
const std::size_t old_size = held_[static_cast<std::size_t>(owner)].size();
for (int lock : locks) {
const int status = take(owner, lock);
if (status == 0) continue;
auto &stack = held_[static_cast<std::size_t>(owner)];
while (stack.size() > old_size) {
const int acquired = stack.back();
const int undone = port_.release(owner, acquired);
if (undone != 0) return undone;
stack.pop_back();
}
levels_by_owner_[static_cast<std::size_t>(owner)] = stack.empty() ? 0 : kLevels[static_cast<std::size_t>(stack.back())];
return status;
}
return static_cast<int>(Result::ok);
}
int Manager::release_all(int owner) {
if (!valid_owner(owner)) return static_cast<int>(Result::invalid);
auto &stack = held_[static_cast<std::size_t>(owner)];
while (!stack.empty()) {
const int lock = stack.back();
const int status = drop(owner, lock);
if (status != 0) return status;
}
return static_cast<int>(Result::ok);
}
OwnerView Manager::view(int owner) const {
if (!valid_owner(owner)) return OwnerView{owner, {}, static_cast<int>(Result::invalid)};
const auto &stack = held_[static_cast<std::size_t>(owner)];
const int level = stack.empty() ? 0 : kLevels[static_cast<std::size_t>(stack.back())];
return OwnerView{owner, stack, level};
}
int Manager::owner_of(int lock) const { return port_.owner_of(lock); }
int Manager::held_count() const { return port_.held_count(); }
void Manager::reset() {
port_.reset();
for (auto &stack : held_) stack.clear();
current_level_ = 0;
levels_by_owner_.fill(0);
}
} // namespace levels