Files
Library/include/Void/Async/Channel.hpp
T
faumarayandClaude Fable 5 28b94883fd Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged:
- Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant
- Functional: Pipeline, Either, Result, Retry
- Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval
- Math: +Matrix, Numeric, Random, Histogram
- Text: Str, Format, Tokenizer, Diff, Encoding, Semver
- Data: Json, Csv, Ini
- Async: Coro, Concurrency, EventBus, Observable
- Time: +Bench
- Log: Log facade + Logging framework (facade now header-only, breaking
  the Stacktrace<->Log link cycle the merge would have created)
- System: FileSystem + Process
- Configuration: +Argparse
- Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail

Module system fixes:
- OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no
  longer depends on the alphabetical discovery order of src/ subdirs
  (Testing -> Text would have been silently dropped).
- Every header-only module now has a tracked module.cmake; previously the
  empty src/<Name> marker dirs were untracked, so a fresh clone would have
  silently skipped those modules.
- VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT.

Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files
prefixed: ResultBasic etc.). Test count verified identical (831) before and
after. README/CLAUDE.md module tables updated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 18:50:02 +03:00

119 lines
2.4 KiB
C++

#pragma once
#include <condition_variable>
#include <mutex>
#include <optional>
#include <queue>
namespace Void::Concurrency
{
// Bounded multi-producer multi-consumer channel.
// send/receive block until space/item is available or the channel is
// closed.
template <typename T> class Channel
{
public:
explicit Channel(std::size_t capacity) : cap_{capacity} {}
Channel(const Channel&) = delete;
Channel& operator=(const Channel&) = delete;
// Block until space is available. Returns false if the channel is
// closed.
bool send(T value)
{
std::unique_lock lock{mu_};
not_full_.wait(
lock, [this] { return buf_.size() < cap_ || closed_; });
if (closed_) {
return false;
}
buf_.push(std::move(value));
not_empty_.notify_one();
return true;
}
// Block until an item is available. Returns nullopt if closed and
// empty.
std::optional<T> receive()
{
std::unique_lock lock{mu_};
not_empty_.wait(lock,
[this] { return !buf_.empty() || closed_; });
if (buf_.empty()) {
return std::nullopt;
}
auto val = std::move(buf_.front());
buf_.pop();
not_full_.notify_one();
return val;
}
// Non-blocking send. Returns false if full or closed.
bool try_send(T value)
{
std::unique_lock lock{mu_};
if (closed_ || buf_.size() >= cap_) {
return false;
}
buf_.push(std::move(value));
not_empty_.notify_one();
return true;
}
// Non-blocking receive. Returns nullopt if empty.
std::optional<T> try_receive()
{
std::unique_lock lock{mu_};
if (buf_.empty()) {
return std::nullopt;
}
auto val = std::move(buf_.front());
buf_.pop();
not_full_.notify_one();
return val;
}
void close()
{
{
std::unique_lock lock{mu_};
closed_ = true;
}
not_full_.notify_all();
not_empty_.notify_all();
}
bool is_closed() const
{
std::unique_lock lock{mu_};
return closed_;
}
std::size_t size() const
{
std::unique_lock lock{mu_};
return buf_.size();
}
bool empty() const
{
std::unique_lock lock{mu_};
return buf_.empty();
}
std::size_t capacity() const noexcept { return cap_; }
private:
std::size_t cap_;
std::queue<T> buf_;
mutable std::mutex mu_;
std::condition_variable not_full_;
std::condition_variable not_empty_;
bool closed_{false};
};
} // namespace Void::Concurrency