system_executor.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // impl/system_executor.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. #ifndef BOOST_ASIO_IMPL_SYSTEM_EXECUTOR_HPP
  11. #define BOOST_ASIO_IMPL_SYSTEM_EXECUTOR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/executor_op.hpp>
  16. #include <boost/asio/detail/global.hpp>
  17. #include <boost/asio/detail/recycling_allocator.hpp>
  18. #include <boost/asio/detail/type_traits.hpp>
  19. #include <boost/asio/system_context.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. inline system_context& system_executor::context() const BOOST_ASIO_NOEXCEPT
  24. {
  25. return detail::global<system_context>();
  26. }
  27. template <typename Function, typename Allocator>
  28. void system_executor::dispatch(
  29. BOOST_ASIO_MOVE_ARG(Function) f, const Allocator&) const
  30. {
  31. typename decay<Function>::type tmp(BOOST_ASIO_MOVE_CAST(Function)(f));
  32. boost_asio_handler_invoke_helpers::invoke(tmp, tmp);
  33. }
  34. template <typename Function, typename Allocator>
  35. void system_executor::post(
  36. BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  37. {
  38. typedef typename decay<Function>::type function_type;
  39. system_context& ctx = detail::global<system_context>();
  40. // Allocate and construct an operation to wrap the function.
  41. typedef detail::executor_op<function_type, Allocator> op;
  42. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  43. p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
  44. BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
  45. "system_executor", &this->context(), 0, "post"));
  46. ctx.scheduler_.post_immediate_completion(p.p, false);
  47. p.v = p.p = 0;
  48. }
  49. template <typename Function, typename Allocator>
  50. void system_executor::defer(
  51. BOOST_ASIO_MOVE_ARG(Function) f, const Allocator& a) const
  52. {
  53. typedef typename decay<Function>::type function_type;
  54. system_context& ctx = detail::global<system_context>();
  55. // Allocate and construct an operation to wrap the function.
  56. typedef detail::executor_op<function_type, Allocator> op;
  57. typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
  58. p.p = new (p.v) op(BOOST_ASIO_MOVE_CAST(Function)(f), a);
  59. BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
  60. "system_executor", &this->context(), 0, "defer"));
  61. ctx.scheduler_.post_immediate_completion(p.p, true);
  62. p.v = p.p = 0;
  63. }
  64. } // namespace asio
  65. } // namespace boost
  66. #include <boost/asio/detail/pop_options.hpp>
  67. #endif // BOOST_ASIO_IMPL_SYSTEM_EXECUTOR_HPP