Core Library0.xAPI reference

Choose documentation 3

latest

Current version

latest
English

Early release libtmux is released, and its APIs may still change between releases. This site's structure and URLs may change too.

MoreLess about this notice

function libtmux-cxx

command_sender

Schedules admission and receiver completion on the supplied scheduler.

Source and package
Qualified name
libtmux::command_sender
Namespace
libtmux
Package
libtmux-cxx
Source
include/libtmux/execution.hpp
template <stdexec::scheduler Scheduler> [[C++ nodiscard attributeAsks the compiler to warn when a result is discarded. An explicit cast to void acknowledges that the result is intentionally ignored.Language documentation]] auto command_sender(Scheduler scheduler, const Server& server, CommandRuntime& runtime, CommandRequest request, stdexec::inplace_stop_token stop = {});

Description

A scheduler that dispatches on another thread must return from posting the work without waiting for the receiver. Inline completion is supported. The runtime hands off an admitted result from its completion thread. An inline scheduler can run a receiver on that thread. Receivers running there must return promptly and arrange runtime shutdown on another thread.

Owns the server and request. The connected operation owns the receiver. Keep the operation state alive until receiver completion begins. Keep the runtime, scheduler and explicit stop source alive through completion, even if CommandRuntime::close() has already returned.

The value channel carries the output string. The error channel carries CommandFailure, including delivery information, or std::exception_ptr. The scheduler can also stop admission or delivery. Admission forwards scheduler errors. Delivery scheduling errors use std::exception_ptr. Receiver stop can stop admission or request transport cancellation after admission. The optional explicit token also requests transport cancellation. Global observers still require CommandRuntime::dispatch_ready().

Run example.

Type parameters
Parameters

Examples

Fetch the library version documented on this page.

Terminal window
$ git clone https://github.com/libtmux/libtmux-cxx.git libtmux-source && \
git -C libtmux-source checkout 75ff8098f8b2a6d7690962ba82087b19d086b5b8

Build and run the optional sender example on POSIX. The linked guide also covers installed packages, cancellation and shutdown. View source

Terminal window
$ cd libtmux-source
$ cmake -S . \
-B build/command-senders \
-DCMAKE_CXX_COMPILER=clang++ \
-DCMAKE_CXX_FLAGS=-stdlib=libc++ \
-DLIBTMUX_BUILD_TESTS=OFF \
-DLIBTMUX_BUILD_EXAMPLES=ON \
-DLIBTMUX_EXAMPLES_BUILD_EXECUTION=ON \
-DLIBTMUX_FETCH_DEPS=ON
$ cmake --build build/command-senders \
--target libtmux_example_09_command_senders
$ ./build/command-senders/examples/libtmux_example_09_command_senders

While tmux waits for a signal, the same scheduler worker can admit the command that sends it. View source

09-command-senders.cppComplete program
#include <chrono>
#include <exception>
#include <future>
#include <iostream>
#include <tuple>
#include <utility>
#include <exec/static_thread_pool.hpp>
#include <libtmux/batch.hpp>
#include <libtmux/execution.hpp>
#include <libtmux/testing/scoped_server.hpp>
#include <stdexec/execution.hpp>
namespace {
int report_failure(const libtmux::CommandFailure& failure) {
std::cerr << libtmux::to_string(failure) << '\n';
return 1;
}
} // namespace
int main() try {
using libtmux::command_sender;
using namespace std::chrono_literals;
constexpr auto timeout = 5s;
auto fixture = libtmux::test::ScopedTmuxServer::start(
{.socket_namespace = libtmux::test::SocketNamespace::consumer("sender")});
if (!fixture) {
std::cerr << fixture.error() << '\n';
return 1;
}
const auto& path = fixture->socket_path();
auto server = libtmux::Server::at_socket_path(path, {}, {.timeout = timeout});
if (!server) {
return report_failure(server.error());
}
exec::static_thread_pool pool{1};
auto runtime = libtmux::CommandRuntime::start({.capacity = 2});
if (!runtime) {
return report_failure(runtime.error());
}
const auto scheduler = pool.get_scheduler();
libtmux::CommandBatch batch;
batch.add({"wait-for", "-S", "entered"});
batch.add({"wait-for", "release"});
batch.add({"display-message", "-p", "tmux released the wait"});
auto wait = command_sender(scheduler, *server, *runtime, batch.request());
auto wait_future =
std::async(std::launch::async, stdexec::sync_wait, std::move(wait));
if (auto entered = server->wait_for("entered", timeout); !entered) {
return report_failure(entered.error());
}
libtmux::CommandRequest release{"wait-for", "-S", "release"};
const auto signalled = stdexec::sync_wait(
command_sender(scheduler, *server, *runtime, std::move(release)));
const auto waited = wait_future.get();
if (!signalled || !waited) {
std::cerr << "a command sender stopped\n";
return 1;
}
if (const auto closed = runtime->close(); closed.failure) {
return report_failure(*closed.failure);
}
std::cout << std::get<0>(*waited);
return 0;
} catch (const libtmux::CommandFailure& failure) {
return report_failure(failure);
} catch (const std::exception& failure) {
std::cerr << failure.what() << '\n';
return 1;
}

Expected output: View source

09-command-senders.out
tmux released the wait

Recorded calls

source calls
No call edges in the C++ model.

The C++ reference records declarations, not calls, so this page shows no callers or callees. That is a limit of the extracted data, not a statement that nothing calls libtmux::command_sender.

Esc
Search in

Type to search.

Reference previewOpen reference