Clementine
executor_op.hpp
1 //
2 // detail/executor_op.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_DETAIL_EXECUTOR_OP_HPP
12 #define ASIO_DETAIL_EXECUTOR_OP_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 #include "asio/detail/fenced_block.hpp"
20 #include "asio/detail/handler_alloc_helpers.hpp"
21 #include "asio/detail/handler_invoke_helpers.hpp"
22 #include "asio/detail/scheduler_operation.hpp"
23 
24 #include "asio/detail/push_options.hpp"
25 
26 namespace asio {
27 namespace detail {
28 
29 template <typename Handler, typename Alloc,
30  typename Operation = scheduler_operation>
31 class executor_op : public Operation
32 {
33 public:
34  ASIO_DEFINE_HANDLER_ALLOCATOR_PTR(executor_op);
35 
36  template <typename H>
37  executor_op(ASIO_MOVE_ARG(H) h, const Alloc& allocator)
38  : Operation(&executor_op::do_complete),
39  handler_(ASIO_MOVE_CAST(H)(h)),
40  allocator_(allocator)
41  {
42  }
43 
44  static void do_complete(void* owner, Operation* base,
45  const asio::error_code& /*ec*/,
46  std::size_t /*bytes_transferred*/)
47  {
48  // Take ownership of the handler object.
49  executor_op* o(static_cast<executor_op*>(base));
50  Alloc allocator(o->allocator_);
51  ptr p = { detail::addressof(allocator), o, o };
52 
53  ASIO_HANDLER_COMPLETION((*o));
54 
55  // Make a copy of the handler so that the memory can be deallocated before
56  // the upcall is made. Even if we're not about to make an upcall, a
57  // sub-object of the handler may be the true owner of the memory associated
58  // with the handler. Consequently, a local copy of the handler is required
59  // to ensure that any owning sub-object remains valid until after we have
60  // deallocated the memory here.
61  Handler handler(ASIO_MOVE_CAST(Handler)(o->handler_));
62  p.reset();
63 
64  // Make the upcall if required.
65  if (owner)
66  {
67  fenced_block b(fenced_block::half);
68  ASIO_HANDLER_INVOCATION_BEGIN(());
69  asio_handler_invoke_helpers::invoke(handler, handler);
70  ASIO_HANDLER_INVOCATION_END;
71  }
72  }
73 
74 private:
75  Handler handler_;
76  Alloc allocator_;
77 };
78 
79 } // namespace detail
80 } // namespace asio
81 
82 #include "asio/detail/pop_options.hpp"
83 
84 #endif // ASIO_DETAIL_EXECUTOR_OP_HPP
Definition: allocator.hpp:17
Definition: chrono.h:284
Definition: executor_op.hpp:31
Definition: null_fenced_block.hpp:25
Class to represent an error code value.
Definition: error_code.hpp:80
Definition: any_io_executor.hpp:28