• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // detail/impl/task_io_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 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_IMPL_TASK_IO_SERVICE_HPP
12 #define ASIO_DETAIL_IMPL_TASK_IO_SERVICE_HPP
13 
14 
15 #include "asio/detail/addressof.hpp"
16 #include "asio/detail/completion_handler.hpp"
17 #include "asio/detail/fenced_block.hpp"
18 #include "asio/detail/handler_alloc_helpers.hpp"
19 #include "asio/detail/handler_cont_helpers.hpp"
20 #include "asio/detail/handler_invoke_helpers.hpp"
21 
22 #include "asio/detail/push_options.hpp"
23 
24 namespace asio {
25 namespace detail {
26 
27 template <typename Handler>
dispatch(Handler & handler)28 void task_io_service::dispatch(Handler& handler)
29 {
30   if (thread_call_stack::contains(this))
31   {
32     fenced_block b(fenced_block::full);
33     asio_handler_invoke_helpers::invoke(handler, handler);
34   }
35   else
36   {
37     // Allocate and construct an operation to wrap the handler.
38     typedef completion_handler<Handler> op;
39     typename op::ptr p = { asio::detail::addressof(handler),
40       asio_handler_alloc_helpers::allocate(
41         sizeof(op), handler), 0 };
42     p.p = new (p.v) op(handler);
43 
44     ASIO_HANDLER_CREATION((p.p, "io_service", this, "dispatch"));
45 
46     do_dispatch(p.p);
47     p.v = p.p = 0;
48   }
49 }
50 
51 template <typename Handler>
post(Handler & handler)52 void task_io_service::post(Handler& handler)
53 {
54   bool is_continuation =
55     asio_handler_cont_helpers::is_continuation(handler);
56 
57   // Allocate and construct an operation to wrap the handler.
58   typedef completion_handler<Handler> op;
59   typename op::ptr p = { asio::detail::addressof(handler),
60     asio_handler_alloc_helpers::allocate(
61       sizeof(op), handler), 0 };
62   p.p = new (p.v) op(handler);
63 
64   ASIO_HANDLER_CREATION((p.p, "io_service", this, "post"));
65 
66   post_immediate_completion(p.p, is_continuation);
67   p.v = p.p = 0;
68 }
69 
70 } // namespace detail
71 } // namespace asio
72 
73 #include "asio/detail/pop_options.hpp"
74 
75 #endif // ASIO_DETAIL_IMPL_TASK_IO_SERVICE_HPP
76