Skip to content

Commit

Permalink
Add documentation for require_started sender adaptor
Browse files Browse the repository at this point in the history
  • Loading branch information
aurianer committed Nov 1, 2024
1 parent bfa0be5 commit 9ab4249
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ The ``pika/execution.hpp`` header provides functionality related to ``std::execu
:language: c++
:start-at: #include

.. doxygenvariable:: pika::execution::experimental::require_started

.. literalinclude:: ../examples/documentation/require_started_documentation.cpp
:language: c++
:start-at: #include

.. doxygenvariable:: pika::execution::experimental::split_tuple

.. literalinclude:: ../examples/documentation/split_tuple_documentation.cpp
Expand Down
3 changes: 3 additions & 0 deletions examples/documentation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ set(example_programs
drop_value_documentation
hello_world_documentation
init_hpp_documentation
require_started_documentation
split_tuple_documentation
unpack_documentation
when_all_vector_documentation
)

set(require_started_documentation_PARAMETERS FAILURE_EXPECTED)

foreach(example_program ${example_programs})
set(sources ${example_program}.cpp)

Expand Down
53 changes: 53 additions & 0 deletions examples/documentation/require_started_documentation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (c) 2024 ETH Zurich
//
// SPDX-License-Identifier: BSL-1.0
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

#include <pika/execution.hpp>
#include <pika/init.hpp>

#include <fmt/printf.h>

#include <cassert>
#include <cstdlib>
#include <exception>
#include <utility>

int main(int argc, char* argv[])
{
namespace ex = pika::execution::experimental;
namespace tt = pika::this_thread::experimental;

pika::start(argc, argv);
ex::thread_pool_scheduler sched{};

{
// require_started forwards values received from the predecessor sender
auto s = ex::just(42) | ex::require_started() |
ex::then([]([[maybe_unused]] auto&& i) { assert(i == 42); });
tt::sync_wait(std::move(s));
}

{
// The termination is ignored with discard, the sender is from the user's perspective
// rightfully not used
auto s = ex::just() | ex::require_started();
s.discard();
}

{
// The require_started sender terminates on destruction if it has not been used
std::set_terminate([] {
fmt::print("std::terminate called: rs3 never connected or started\n");
pika::finalize();
pika::stop();
std::exit(1);
});

auto s = ex::just() | ex::require_started();
}
assert(false);

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ namespace pika {
#endif

namespace execution::experimental {
inline constexpr struct require_started_t final
struct require_started_t final
{
template <typename Sender, PIKA_CONCEPT_REQUIRES_(is_sender_v<Sender>)>
constexpr PIKA_FORCEINLINE auto
Expand All @@ -453,7 +453,22 @@ namespace pika {
{
return detail::partial_algorithm<require_started_t>{};
}
} require_started{};
};

/// \brief Diagnose if a sender has not been started and terminates on destruction. It
/// forwards the values of the predecessor sender.
///
/// Sender adaptor that takes any sender and returns a new sender that sends the same values
/// as the predecessor sender.
///
/// The destructor is by default terminating if the sender has not been connected or if the
/// operation state has not been started.
/// The operation state of a \p require_started sender is allowed to not be started if it
/// has been explicitly requested with the \p discard member function.
///
/// Added in 0.21.0.
inline constexpr require_started_t require_started{};

} // namespace execution::experimental

#undef PIKA_DETAIL_REQUIRE_STARTED_MODE_PARAMETER
Expand Down

0 comments on commit 9ab4249

Please sign in to comment.