File indexing completed on 2025-01-18 09:28:57
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_ASIO_IMPL_SYSTEM_EXECUTOR_HPP
0012 #define BOOST_ASIO_IMPL_SYSTEM_EXECUTOR_HPP
0013
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif
0017
0018 #include <boost/asio/detail/executor_op.hpp>
0019 #include <boost/asio/detail/global.hpp>
0020 #include <boost/asio/detail/type_traits.hpp>
0021 #include <boost/asio/system_context.hpp>
0022
0023 #include <boost/asio/detail/push_options.hpp>
0024
0025 namespace boost {
0026 namespace asio {
0027
0028 template <typename Blocking, typename Relationship, typename Allocator>
0029 inline system_context&
0030 basic_system_executor<Blocking, Relationship, Allocator>::query(
0031 execution::context_t) noexcept
0032 {
0033 return detail::global<system_context>();
0034 }
0035
0036 template <typename Blocking, typename Relationship, typename Allocator>
0037 inline std::size_t
0038 basic_system_executor<Blocking, Relationship, Allocator>::query(
0039 execution::occupancy_t) const noexcept
0040 {
0041 return detail::global<system_context>().num_threads_;
0042 }
0043
0044 template <typename Blocking, typename Relationship, typename Allocator>
0045 template <typename Function>
0046 inline void
0047 basic_system_executor<Blocking, Relationship, Allocator>::do_execute(
0048 Function&& f, execution::blocking_t::possibly_t) const
0049 {
0050
0051 detail::non_const_lvalue<Function> f2(f);
0052
0053 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
0054 try
0055 {
0056 #endif
0057 detail::fenced_block b(detail::fenced_block::full);
0058 static_cast<decay_t<Function>&&>(f2.value)();
0059 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
0060 }
0061 catch (...)
0062 {
0063 std::terminate();
0064 }
0065 #endif
0066 }
0067
0068 template <typename Blocking, typename Relationship, typename Allocator>
0069 template <typename Function>
0070 inline void
0071 basic_system_executor<Blocking, Relationship, Allocator>::do_execute(
0072 Function&& f, execution::blocking_t::always_t) const
0073 {
0074
0075 detail::non_const_lvalue<Function> f2(f);
0076
0077 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
0078 try
0079 {
0080 #endif
0081 detail::fenced_block b(detail::fenced_block::full);
0082 static_cast<decay_t<Function>&&>(f2.value)();
0083 #if !defined(BOOST_ASIO_NO_EXCEPTIONS)
0084 }
0085 catch (...)
0086 {
0087 std::terminate();
0088 }
0089 #endif
0090 }
0091
0092 template <typename Blocking, typename Relationship, typename Allocator>
0093 template <typename Function>
0094 void basic_system_executor<Blocking, Relationship, Allocator>::do_execute(
0095 Function&& f, execution::blocking_t::never_t) const
0096 {
0097 system_context& ctx = detail::global<system_context>();
0098
0099
0100 typedef detail::executor_op<decay_t<Function>, Allocator> op;
0101 typename op::ptr p = { detail::addressof(allocator_),
0102 op::ptr::allocate(allocator_), 0 };
0103 p.p = new (p.v) op(static_cast<Function&&>(f), allocator_);
0104
0105 if (is_same<Relationship, execution::relationship_t::continuation_t>::value)
0106 {
0107 BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
0108 "system_executor", &ctx, 0, "execute(blk=never,rel=cont)"));
0109 }
0110 else
0111 {
0112 BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
0113 "system_executor", &ctx, 0, "execute(blk=never,rel=fork)"));
0114 }
0115
0116 ctx.scheduler_.post_immediate_completion(p.p,
0117 is_same<Relationship, execution::relationship_t::continuation_t>::value);
0118 p.v = p.p = 0;
0119 }
0120
0121 #if !defined(BOOST_ASIO_NO_TS_EXECUTORS)
0122 template <typename Blocking, typename Relationship, typename Allocator>
0123 inline system_context& basic_system_executor<
0124 Blocking, Relationship, Allocator>::context() const noexcept
0125 {
0126 return detail::global<system_context>();
0127 }
0128
0129 template <typename Blocking, typename Relationship, typename Allocator>
0130 template <typename Function, typename OtherAllocator>
0131 void basic_system_executor<Blocking, Relationship, Allocator>::dispatch(
0132 Function&& f, const OtherAllocator&) const
0133 {
0134 decay_t<Function>(static_cast<Function&&>(f))();
0135 }
0136
0137 template <typename Blocking, typename Relationship, typename Allocator>
0138 template <typename Function, typename OtherAllocator>
0139 void basic_system_executor<Blocking, Relationship, Allocator>::post(
0140 Function&& f, const OtherAllocator& a) const
0141 {
0142 system_context& ctx = detail::global<system_context>();
0143
0144
0145 typedef detail::executor_op<decay_t<Function>, OtherAllocator> op;
0146 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
0147 p.p = new (p.v) op(static_cast<Function&&>(f), a);
0148
0149 BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
0150 "system_executor", &this->context(), 0, "post"));
0151
0152 ctx.scheduler_.post_immediate_completion(p.p, false);
0153 p.v = p.p = 0;
0154 }
0155
0156 template <typename Blocking, typename Relationship, typename Allocator>
0157 template <typename Function, typename OtherAllocator>
0158 void basic_system_executor<Blocking, Relationship, Allocator>::defer(
0159 Function&& f, const OtherAllocator& a) const
0160 {
0161 system_context& ctx = detail::global<system_context>();
0162
0163
0164 typedef detail::executor_op<decay_t<Function>, OtherAllocator> op;
0165 typename op::ptr p = { detail::addressof(a), op::ptr::allocate(a), 0 };
0166 p.p = new (p.v) op(static_cast<Function&&>(f), a);
0167
0168 BOOST_ASIO_HANDLER_CREATION((ctx, *p.p,
0169 "system_executor", &this->context(), 0, "defer"));
0170
0171 ctx.scheduler_.post_immediate_completion(p.p, true);
0172 p.v = p.p = 0;
0173 }
0174 #endif
0175
0176 }
0177 }
0178
0179 #include <boost/asio/detail/pop_options.hpp>
0180
0181 #endif