<blake3pp/async_io.hpp>¶
EXPERIMENTAL. File hashing as senders, over a context the caller drives.
The blocking entry points in parallel_io.hpp run one file and own everything they need for it: a driver, an arena of windows, and the thread that calls them. That is the right shape for one file and the wrong one for a thousand, where the arena is allocated and faulted in once per call and no two files ever overlap.
Here the storage moves up. An io_context owns the driver, the window arena and the run queue; a sender borrows windows from it for as long as one file takes, and several senders may be in flight at once. One thread runs the context, and every operation completes on it; an operation may be started from any thread, and its file is opened and its reads begin on the running thread.
blake3pp::async::io_context ctx;
exec::static_thread_pool pool{std::thread::hardware_concurrency()};
std::thread runner([&] { ctx.run(); }); // until request_stop()
auto work = stdexec::when_all(
blake3pp::async::async_hash_file(ctx, "a.bin", pool.get_scheduler()),
blake3pp::async::async_hash_file(ctx, "b.bin", pool.get_scheduler()));
auto [a, b] = stdexec::sync_wait(std::move(work)).value();
ctx.request_stop();
runner.join();
A thread with nothing else to do can drive the context itself: start the operations, then run_until_idle() returns once they have completed.
What is experimental about it: the error channels and the names of the context options. The run model and the lifetime rules below are the parts meant to stay.
LIFETIMES. A started operation must not outlive its context, its scheduler or (for async_update_file) its hasher, and a context with a started, uncompleted operation must not be destroyed. The hasher an async_update_file names is the operation's alone until it completes.
Functions¶
async_update_file¶
template <stack_budget Budget Budget, class Sched, tracer Tracer Tracer null_tracer>
auto async_update_file(basic_io_context<Budget> &ctx, hasher &h, std::filesystem::path path, Sched &&sched, const file_options &opts={}, Tracer tracer={});
Streams a file into a hasher over a context, as a sender. Completes with set_value(), set_error(std::error_code), set_error(std::exception_ptr) or set_stopped(), always on the thread running the context.
| Parameter | |
|---|---|
ctx |
The context whose windows and driver this borrows. It must outlive the operation. |
h |
The hasher to stream into. It must outlive the operation, and nothing else may touch it while this runs; it keeps its mode, so keyed and derive_key hashers work as they do everywhere else. |
path |
The file to read; copied. |
sched |
Where the subtree compressions run. |
opts |
Per-call knobs: io, where io_mode::buffered overrides the context's io_mode::direct for this file. |
tracer |
How to record (<blake3pp/trace.hpp>): the default null_tracer records nothing; buffer_tracer fills the buffer with this file's window records. A type rather than an option, because it becomes part of the operation state's type. |
async_hash_file¶
template <stack_budget Budget Budget, class Sched, tracer Tracer Tracer null_tracer>
auto async_hash_file(basic_io_context<Budget> &ctx, std::filesystem::path path, Sched &&sched, const hash_options &opts={}, Tracer tracer={});
Hashes a whole file over a context, as a sender. Completes with set_value(digest), set_error(std::error_code), set_error(std::exception_ptr) or set_stopped(), always on the thread running the context. The hasher is the operation's own.
| Parameter | |
|---|---|
ctx |
The context whose windows and driver this borrows. |
path |
The file to read; copied. |
sched |
Where the subtree compressions run. |
opts |
Per-call knobs: the arch, the mode, and io. The sender copies them, the mode's key or context included, so nothing they name has to outlive the call. |
tracer |
How to record, as for async_update_file(). |
async_update_file¶
template <stack_budget Budget Budget, blake3pp::detail::foreign_path P P, class Sched, tracer Tracer Tracer null_tracer>
auto async_update_file(basic_io_context<Budget> &ctx, hasher &h, const P &path, Sched &&sched, const file_options &opts={}, Tracer tracer={});
async_update_file() for a path type from another filesystem library (e.g.
boost::filesystem::path): anything with a native() the standard path accepts, bridged without transcoding.
async_hash_file¶
template <stack_budget Budget Budget, blake3pp::detail::foreign_path P P, class Sched, tracer Tracer Tracer null_tracer>
auto async_hash_file(basic_io_context<Budget> &ctx, const P &path, Sched &&sched, const hash_options &opts={}, Tracer tracer={});
async_hash_file() for a foreign path type.
async_update_files¶
template <stack_budget Budget Budget, class Sched, class MakeHasher, class OnDone, tracer Tracer Tracer null_tracer>
auto async_update_files(basic_io_context<Budget> &ctx, std::span<const std::filesystem::path> paths, Sched &&sched, const many_files_options &opts, MakeHasher make, OnDone done, Tracer tracer={});
Streams many files into hashers over a context, a bounded number at a time, as a sender.
Each file's hasher comes from make(index); when the file has been streamed, or has failed, done(index, h, outcome) is called on the context's thread with that hasher and the outcome, in whatever order the files finish. The sender completes once, with set_value() after the last outcome, set_error(exception_ptr) if its own bookkeeping failed, or set_stopped() when a stop ended the run, in which case files that never started are reported as stopped.
| Parameter | |
|---|---|
ctx |
The context whose windows and driver this borrows. |
paths |
The files; the span must outlive the operation. |
sched |
Where the subtree compressions run. |
opts |
How many files run at once, and the per-file knobs. |
make |
hasher(std::size_t index): the hasher for a file, with its mode (plain, keyed, derive_key) and variant. |
done |
void(std::size_t index, hasher& h, const file_outcome&) noexcept: the file's end. |
tracer |
How to record, as for async_update_file(): one buffer for every file in flight, whose window records carry the window index within its file. |
sync_wait¶
template <stack_budget Budget Budget, class Sender>
auto sync_wait(basic_io_context<Budget> &ctx, Sender &&sndr);
Starts sndr and drives the context on this thread until it completes.
Returns the values as a tuple, an empty optional for set_stopped(), and rethrows an error (an error_code arrives as std::system_error). The sender must have exactly one value completion, as for ex::sync_wait; any sender over this context's operations qualifies, composed or not, and it may complete on any thread. Other operations started on the context keep running meanwhile and may still be in flight when this returns. The completion is over when this returns, wake included: with nothing else in flight, the context may be destroyed straight away.