amos 8b7616bfd8 Batch 22 (20261027-C-hwe-hard-22): add 63 calibrated HWE hard-design tasks
From the 101 lk/man/pth code-gen sources: 63 tasks (C11 36, C++17 27,
11 canonical families), 38 skipped for scope or duplication. Each task
has at least one independent contract-edge defect, boundary types
rotate, and public docs state trap clauses as plain contract prose.
Claude reviewed 63/63 USABLE against current digests. Difficulty is
unmeasured.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-27 16:14:57 +08:00

93 lines
2.0 KiB
C++

#include "latch.hpp"
namespace release {
int Latch::await(Id id)
{
if (waiters_.find(id) != waiters_.end()) {
return -17;
}
if (waiters_.size() == entry_limit) {
return -28;
}
WaitEntry entry;
entry.state = remaining_ == 0 ? WaitState::released : WaitState::waiting;
entry.ticket = next_ticket_;
++next_ticket_;
waiters_.emplace(id, entry);
registration_.push_back(id);
if (entry.state == WaitState::released) {
ready_.push_back(id);
}
++revision_;
return 0;
}
int Latch::cancel(Id id)
{
auto found = waiters_.find(id);
if (found == waiters_.end()) {
return -2;
}
if (found->second.state == WaitState::released) {
return -114;
}
if (found->second.state == WaitState::canceled) {
return 1;
}
found->second.state = WaitState::canceled;
++revision_;
return 0;
}
int Latch::poll(Id id) const
{
auto found = waiters_.find(id);
if (found == waiters_.end()) {
return -2;
}
switch (found->second.state) {
case WaitState::waiting:
return 0;
case WaitState::released:
return 1;
case WaitState::canceled:
return -125;
}
return -22;
}
int Latch::inspect(Id id, WaitInfo &out) const
{
auto found = waiters_.find(id);
if (found == waiters_.end()) {
return -2;
}
WaitInfo value;
value.state = found->second.state;
value.ticket = found->second.ticket;
out = value;
return 0;
}
int Latch::take(Id &out)
{
if (ready_.empty()) {
return -11;
}
out = ready_.front();
ready_.pop_front();
++revision_;
return 0;
}
void Latch::release_waiters()
{
for (Id id : registration_) {
auto found = waiters_.find(id);
if (found == waiters_.end()) {
continue;
}
if (found->second.state != WaitState::waiting) {
continue;
}
found->second.state = WaitState::released;
ready_.push_back(id);
}
}
}