2
0
mirror of https://github.com/boostorg/asio.git synced 2026-01-28 06:42:08 +00:00
Commit Graph

1471 Commits

Author SHA1 Message Date
Christopher Kohlhoff
61796a5704 Dispatch cancellation handlers on the correct executor.
When a completion handler for spawn() or co_spawn() uses a specified
(i.e. non-default) associated executor, cancellation handlers need to
be dispatched to the executor that was passed to spawn() or co_spawn().
2022-12-07 07:33:39 +11:00
Christopher Kohlhoff
ce07af1fdf Fix namespace for co_composed's coroutine_traits specialisation. 2022-11-14 22:01:26 +11:00
Christopher Kohlhoff
dac750114c Version bump. 2022-11-09 23:23:56 +11:00
Christopher Kohlhoff
e874ca5aab Fall back to fcntl if ioctl fails with ENOTTY when setting non-blocking mode. 2022-11-09 21:57:53 +11:00
Christopher Kohlhoff
80fd0e2971 Constrain post/defer overloads on ability to require blocking.never. 2022-11-03 17:42:29 +11:00
Christopher Kohlhoff
0636a1ca16 Add io_executor_type and get_io_executor() to experimental::co_composed's state. 2022-11-03 17:32:16 +11:00
Christopher Kohlhoff
6ab07fa35e Add io_executor_type and get_io_executor to async_compose's 'self' object. 2022-11-03 17:32:07 +11:00
Christopher Kohlhoff
b67260f30a Add any_completion_handler.hpp to convenience header. 2022-11-03 17:31:53 +11:00
Christopher Kohlhoff
56c1f0f647 Cleaned up promise and made it an async_op. 2022-11-03 17:31:32 +11:00
Christopher Kohlhoff
09716d71f3 Enabled deferred awaiting for coros, regularized use_coro, and fixed allocator handling.
This means that use_coro does not return a coro object, just like
use_awaitable does, i.e. it's an overhead that buys us type erasure.
Allocators can now be set for coro by including allocator_arg in the
coro signature.
2022-11-03 17:29:53 +11:00
Christopher Kohlhoff
10fba74faa Fix broken get_nt_set_info() for overlapped handles.
When calling handle->release(), Asio uses kernel-provided
NtSetInformationFile() routine to remove IOCP for the specific handle.
Since this routine is not part of SDK, Asio retrieves the address of
that function from NTDLL.DLL and caches it into nt_set_info_ class
member. The fact that address is cached is checked by comparing it to 0.

The bug is that class member is not initialized and contains undefined
value, so Asio assumes that the address is already retrieved and uses
undefined value, which leads to acces violation.

Fix by initializing nt_set_info_ to zero, as it is done for sockets in
win_iocp_socket_service_base.ipp.
2022-11-03 17:28:47 +11:00
Christopher Kohlhoff
bf4dfbc607 Remove unused header. 2022-11-03 17:28:44 +11:00
Christopher Kohlhoff
74d0590e82 Make cancelled() public on an async_composed-base composed operation. 2022-11-03 17:27:27 +11:00
Christopher Kohlhoff
9e03478ba1 Add range-based experimental::make_parallel_group().
Added new overloads of experimental::make_parallel_group that may be used
to launch a dynamically-sized set of asynchronous operations, where all
operations are the same type. For example:

  using op_type = decltype(
      socket1.async_read_some(
        boost::asio::buffer(data1),
        boost::asio::deferred
      )
    );

  std::vector<op_type> ops;

  ops.push_back(
      socket1.async_read_some(
        boost::asio::buffer(data1),
        boost::asio::deferred
      )
    );

  ops.push_back(
      socket2.async_read_some(
        boost::asio::buffer(data2),
        boost::asio::deferred
      )
    );

  boost::asio::experimental::make_parallel_group(ops).async_wait(
      boost::asio::experimental::wait_for_all(),
      [](
          std::vector<std::size_t> completion_order,
          std::vector<boost::system::error_code> e,
          std::vector<std::size_t> n
        )
      {
        for (std::size_t i = 0; i < completion_order.size(); ++i)
        {
          std::size_t idx = completion_order[i];
          std::cout << "socket " << idx << " finished: ";
          std::cout << e[idx] << ", " << n[idx] << "\n";
        }
      }
    );

Thanks go to Klemens Morgenstern for supplying part of this implementation.
2022-11-03 00:48:45 +11:00
Christopher Kohlhoff
6df6a90765 Fix consign documentation. 2022-11-01 13:47:47 +11:00
Christopher Kohlhoff
bb0adde4a8 Fix compatibility with recent MSVC. 2022-11-01 13:37:53 +11:00
Christopher Kohlhoff
68ef2b45d4 Add channel buffer specialisation for R(error_code). 2022-11-01 11:35:07 +11:00
Christopher Kohlhoff
0e395856fb Fix ambiguity in channel_traits specialisations. 2022-11-01 11:35:07 +11:00
Christopher Kohlhoff
1c0f3c36be Add experimental::co_composed.
The following example illustrates a simple asynchronous operation that
implements an echo protocol in terms of a coroutine:

  template <typename CompletionToken>
  auto async_echo(tcp::socket& socket,
      CompletionToken&& token)
  {
    return boost::asio::async_initiate<
      CompletionToken, void(boost::system::error_code)>(
        boost::asio::experimental::co_composed<
          void(boost::system::error_code)>(
            [](auto state, tcp::socket& socket) -> void
            {
              try
              {
                state.throw_if_cancelled(true);
                state.reset_cancellation_state(
                  boost::asio::enable_terminal_cancellation());

                for (;;)
                {
                  char data[1024];
                  std::size_t n = co_await socket.async_read_some(
                      boost::asio::buffer(data), boost::asio::deferred);

                  co_await boost::asio::async_write(socket,
                      boost::asio::buffer(data, n), boost::asio::deferred);
                }
              }
              catch (const boost::system::system_error& e)
              {
                co_return {e.code()};
              }
            }, socket),
        token, std::ref(socket));
  }
2022-11-01 11:35:07 +11:00
Christopher Kohlhoff
64448e6a19 Add any_completion_handler<>.
The any_completion_handler<> template can be used to type-erase
completion handlers. A typical use case is to enable separate
compilation of asynchronous operation implementations. For example:

  // Header file:

  void async_sleep_impl(
      boost::asio::any_completion_handler<void(boost::system::error_code)> handler,
      boost::asio::any_io_executor ex, std::chrono::nanoseconds duration);

  template <typename CompletionToken>
  inline auto async_sleep(boost::asio::any_io_executor ex,
      std::chrono::nanoseconds duration, CompletionToken&& token)
  {
    return boost::asio::async_initiate<CompletionToken, void(boost::system::error_code)>(
        async_sleep_impl, token, std::move(ex), duration);
  }

  // Separately compiled source file:

  void async_sleep_impl(
      boost::asio::any_completion_handler<void(boost::system::error_code)> handler,
      boost::asio::any_io_executor ex, std::chrono::nanoseconds duration)
  {
    auto timer = std::make_shared<boost::asio::steady_timer>(ex, duration);
    timer->async_wait(boost::asio::consign(std::move(handler), timer));
  }
2022-11-01 11:00:16 +11:00
Christopher Kohlhoff
ed722fb0a3 Add consign completion token adapter.
The consign completion token adapter can be used to attach additional
values to a completion handler. This is typically used to keep at least
one copy of an object, such as a smart pointer, alive until the
completion handler is called.

For example:

  auto timer1 = std::make_shared<boost::asio::steady_timer>(my_io_context);
  timer1->expires_after(std::chrono::seconds(1));
  timer1->async_wait(
      boost::asio::consign(
        [](boost::system::error_code ec)
        {
          // ...
        },
        timer1
      )
    );

  auto timer2 = std::make_shared<boost::asio::steady_timer>(my_io_context);
  timer2->expires_after(std::chrono::seconds(30));
  std::future<void> f =
    timer2->async_wait(
      boost::asio::consign(
        boost::asio::use_future,
        timer2
      )
    );
2022-11-01 11:00:15 +11:00
Christopher Kohlhoff
9c9b76f0ee Add any_completion_executor. 2022-11-01 11:00:15 +11:00
Christopher Kohlhoff
3ac6b65011 Change semantics of 'dispatch' to mean the executor is used as-is. 2022-11-01 11:00:15 +11:00
Christopher Kohlhoff
8603d5c39b Deprecate concepts, traits, functions and customisation points related to senders and receivers. 2022-11-01 11:00:15 +11:00
Christopher Kohlhoff
17e08c23fe Deprecate execution::execute member function.
Use execute as a member function.
2022-11-01 10:44:37 +11:00
Christopher Kohlhoff
689f94b9ab Re-throw exception from top-level spawn()-ed function. 2022-11-01 10:44:37 +11:00
Christopher Kohlhoff
06d615d8ae Allow a terminal-state spawned thread to run to completion when destroyed. 2022-11-01 10:44:37 +11:00
Christopher Kohlhoff
6579863625 Catch exceptions and rethrow outside of spawned thread. 2022-11-01 10:44:37 +11:00
Christopher Kohlhoff
324a91fd0a Use deduced return types for all two-argument associator get functions. 2022-11-01 10:44:37 +11:00
Christopher Kohlhoff
1e81b83883 Change I/O objects to return their executors by const reference. 2022-11-01 09:47:23 +11:00
Christopher Kohlhoff
07f71caef2 Remove unnecessary specialisation of associated_cancellation_slot. 2022-11-01 09:30:28 +11:00
Christopher Kohlhoff
8be3d47f24 Add associated_cancellation_slot specialisation for std::reference_wrapper. 2022-11-01 09:30:18 +11:00
Christopher Kohlhoff
217986acc7 Use null pointers to implement empty execution::any_executor.
Improves performance of copy/move operations on empty executors.
2022-11-01 09:30:02 +11:00
Christopher Kohlhoff
8dd81fce89 Add nothrow constructor overloads to any_io_executor. 2022-11-01 09:29:46 +11:00
Christopher Kohlhoff
22f4d42fd6 Add nothrow constructor overloads to execution::any_executor<>. 2022-11-01 09:29:19 +11:00
Christopher Kohlhoff
49d11205b1 Add missing context query to use_future's executor. 2022-11-01 09:28:58 +11:00
Christopher Kohlhoff
0af7858e7b Version bump. 2022-08-04 00:11:08 +10:00
Christopher Kohlhoff
d9f5562ba7 Recreate the select_reactor's interrupter on a user-supplied thread, when using IOCP on Windows. 2022-08-03 14:10:10 +10:00
Christopher Kohlhoff
83880a798d Store reference to basic_yield_context in handler.
Improves performance by not copying the executor into the handler.
2022-08-01 09:09:30 +10:00
Christopher Kohlhoff
914c367395 Destroy spawned thread immediately if it completes within handler invocation. 2022-08-01 09:09:30 +10:00
Christopher Kohlhoff
c417121b1e Defend against Qt macros when using Intel C++. 2022-08-01 09:09:30 +10:00
Christopher Kohlhoff
ab543a1383 Remove faulty assertions from experimental::coro implementation. 2022-08-01 09:09:27 +10:00
Christopher Kohlhoff
c8fd215be3 Fix detection of aligned_alloc for Apple platforms. 2022-08-01 09:02:55 +10:00
Christopher Kohlhoff
fc901432c1 Change async_compose example to use return type compatible with new async_result form. 2022-08-01 09:02:35 +10:00
Christopher Kohlhoff
ce29925259 Avoid copying async operation object in await_transform. 2022-08-01 09:00:28 +10:00
Christopher Kohlhoff
9882b2ab11 Disable index_sequence emulation when variadic templates are unavailable. 2022-08-01 08:59:54 +10:00
Christopher Kohlhoff
ccc065c3f1 Fix usage of macro to detect std::exception_ptr. 2022-07-07 11:52:13 +10:00
Christopher Kohlhoff
56eaf73f92 Use deferred rather than use_awaitable in awaitable_operators implementation. 2022-07-07 00:27:15 +10:00
Christopher Kohlhoff
a3b5bfe85e Version bump. 2022-07-06 20:44:09 +10:00
Christopher Kohlhoff
ad5d852009 Fix compatibility with older compilers. 2022-07-06 19:41:20 +10:00