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>
121 lines
2.6 KiB
C++
121 lines
2.6 KiB
C++
#include "latch.hpp"
|
|
#include <algorithm>
|
|
|
|
namespace release {
|
|
Latch::Latch()
|
|
: target_(0), remaining_(0), revision_(0), broadcasts_(0), next_ticket_(1)
|
|
{
|
|
}
|
|
int Latch::reset(Count target)
|
|
{
|
|
if (target > count_limit) {
|
|
return -34;
|
|
}
|
|
target_ = target;
|
|
remaining_ = target;
|
|
revision_ = 0;
|
|
broadcasts_ = 0;
|
|
next_ticket_ = 1;
|
|
participants_.clear();
|
|
waiters_.clear();
|
|
registration_.clear();
|
|
ready_.clear();
|
|
return 0;
|
|
}
|
|
int Latch::join(Id id)
|
|
{
|
|
if (remaining_ == 0) {
|
|
return -16;
|
|
}
|
|
auto found = participants_.find(id);
|
|
if (found != participants_.end()) {
|
|
return 1;
|
|
}
|
|
if (participants_.size() == entry_limit) {
|
|
return -28;
|
|
}
|
|
participants_.emplace(id, false);
|
|
++revision_;
|
|
return 0;
|
|
}
|
|
int Latch::participant(Id id) const
|
|
{
|
|
auto found = participants_.find(id);
|
|
if (found == participants_.end()) {
|
|
return -2;
|
|
}
|
|
return found->second ? 1 : 0;
|
|
}
|
|
int Latch::complete(Id id)
|
|
{
|
|
auto found = participants_.find(id);
|
|
if (found == participants_.end()) {
|
|
return -2;
|
|
}
|
|
if (remaining_ == 0) {
|
|
return reduce(1);
|
|
}
|
|
if (found->second) {
|
|
return 0;
|
|
}
|
|
found->second = true;
|
|
return reduce(1);
|
|
}
|
|
int Latch::count_down(Count amount)
|
|
{
|
|
if (amount > count_limit) {
|
|
return -34;
|
|
}
|
|
return reduce(amount);
|
|
}
|
|
int Latch::reduce(Count amount)
|
|
{
|
|
if (amount == 0) {
|
|
return 0;
|
|
}
|
|
if (remaining_ == 0) {
|
|
return 0;
|
|
}
|
|
remaining_ = amount >= remaining_ ? 0 : remaining_ - amount;
|
|
++revision_;
|
|
if (remaining_ == 0) {
|
|
++broadcasts_;
|
|
release_waiters();
|
|
}
|
|
return 0;
|
|
}
|
|
Snapshot Latch::snapshot() const
|
|
{
|
|
Snapshot result{};
|
|
result.target = target_;
|
|
result.remaining = remaining_;
|
|
result.open = remaining_ == 0 ? 1 : 0;
|
|
result.revision = revision_;
|
|
result.broadcasts = broadcasts_;
|
|
result.participants = participants_.size();
|
|
for (const auto &item : participants_) {
|
|
if (item.second) {
|
|
++result.completed;
|
|
}
|
|
}
|
|
for (const auto &item : waiters_) {
|
|
switch (item.second.state) {
|
|
case WaitState::waiting:
|
|
++result.waiting;
|
|
break;
|
|
case WaitState::released:
|
|
++result.released;
|
|
break;
|
|
case WaitState::canceled:
|
|
++result.canceled;
|
|
break;
|
|
}
|
|
}
|
|
for (const auto &item : waiters_) {
|
|
if (item.second.state == WaitState::released && !item.second.consumed) ++result.queued;
|
|
}
|
|
result.next_ticket = next_ticket_;
|
|
return result;
|
|
}
|
|
}
|