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>
86 lines
2.0 KiB
C++
86 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_;
|
|
entry.consumed = false;
|
|
++next_ticket_;
|
|
waiters_.emplace(id, entry);
|
|
registration_.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)
|
|
{
|
|
auto chosen = waiters_.end();
|
|
for (auto it = waiters_.begin(); it != waiters_.end(); ++it) {
|
|
if (it->second.state != WaitState::released || it->second.consumed) continue;
|
|
if (chosen == waiters_.end() || it->second.ticket < chosen->second.ticket) chosen = it;
|
|
}
|
|
if (chosen == waiters_.end()) return -11;
|
|
out = chosen->first;
|
|
chosen->second.consumed = true;
|
|
++revision_;
|
|
return 0;
|
|
}
|
|
void Latch::release_waiters()
|
|
{
|
|
for (auto &item : waiters_) {
|
|
if (item.second.state == WaitState::waiting) item.second.state = WaitState::released;
|
|
}
|
|
}
|
|
}
|