Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / asio / detail / impl / task_io_service.hpp
1 //
2 // detail/impl/task_io_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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 BOOST_ASIO_DETAIL_IMPL_TASK_IO_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_IMPL_TASK_IO_SERVICE_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/addressof.hpp>
19 #include <boost/asio/detail/completion_handler.hpp>
20 #include <boost/asio/detail/fenced_block.hpp>
21 #include <boost/asio/detail/handler_alloc_helpers.hpp>
22 #include <boost/asio/detail/handler_cont_helpers.hpp>
23 #include <boost/asio/detail/handler_invoke_helpers.hpp>
24
25 #include <boost/asio/detail/push_options.hpp>
26
27 namespace boost {
28 namespace asio {
29 namespace detail {
30
31 template <typename Handler>
32 void task_io_service::dispatch(Handler& handler)
33 {
34   if (thread_call_stack::contains(this))
35   {
36     fenced_block b(fenced_block::full);
37     boost_asio_handler_invoke_helpers::invoke(handler, handler);
38   }
39   else
40   {
41     // Allocate and construct an operation to wrap the handler.
42     typedef completion_handler<Handler> op;
43     typename op::ptr p = { boost::asio::detail::addressof(handler),
44       boost_asio_handler_alloc_helpers::allocate(
45         sizeof(op), handler), 0 };
46     p.p = new (p.v) op(handler);
47
48     BOOST_ASIO_HANDLER_CREATION((p.p, "io_service", this, "dispatch"));
49
50     do_dispatch(p.p);
51     p.v = p.p = 0;
52   }
53 }
54
55 template <typename Handler>
56 void task_io_service::post(Handler& handler)
57 {
58   bool is_continuation =
59     boost_asio_handler_cont_helpers::is_continuation(handler);
60
61   // Allocate and construct an operation to wrap the handler.
62   typedef completion_handler<Handler> op;
63   typename op::ptr p = { boost::asio::detail::addressof(handler),
64     boost_asio_handler_alloc_helpers::allocate(
65       sizeof(op), handler), 0 };
66   p.p = new (p.v) op(handler);
67
68   BOOST_ASIO_HANDLER_CREATION((p.p, "io_service", this, "post"));
69
70   post_immediate_completion(p.p, is_continuation);
71   p.v = p.p = 0;
72 }
73
74 } // namespace detail
75 } // namespace asio
76 } // namespace boost
77
78 #include <boost/asio/detail/pop_options.hpp>
79
80 #endif // BOOST_ASIO_DETAIL_IMPL_TASK_IO_SERVICE_HPP