File indexing completed on 2025-01-30 09:35:35
0001
0002
0003
0004
0005
0006
0007 #ifndef BOOST_FIBERS_ASYNC_HPP
0008 #define BOOST_FIBERS_ASYNC_HPP
0009
0010 #include <algorithm>
0011 #include <memory>
0012 #include <type_traits>
0013 #include <utility>
0014
0015 #include <boost/config.hpp>
0016
0017 #include <boost/fiber/future/future.hpp>
0018 #include <boost/fiber/future/packaged_task.hpp>
0019 #include <boost/fiber/policy.hpp>
0020
0021 namespace boost {
0022 namespace fibers {
0023
0024 #if (defined(BOOST_MSVC) && (_MSC_VER >= 1911 && _MSVC_LANG >= 201703)) || __cplusplus >= 202002L
0025 template <typename>
0026 struct result_of;
0027 template <typename F, typename... Args>
0028 struct result_of<F(Args...)> : std::invoke_result<F, Args...> {};
0029 #else
0030 using std::result_of;
0031 #endif
0032
0033
0034 template< typename Fn, typename ... Args >
0035 future<
0036 typename result_of<
0037 typename std::enable_if<
0038 ! detail::is_launch_policy< typename std::decay< Fn >::type >::value,
0039 typename std::decay< Fn >::type
0040 >::type( typename std::decay< Args >::type ... )
0041 >::type
0042 >
0043 async( Fn && fn, Args ... args) {
0044 typedef typename result_of<
0045 typename std::decay< Fn >::type( typename std::decay< Args >::type ... )
0046 >::type result_type;
0047
0048 packaged_task< result_type( typename std::decay< Args >::type ... ) > pt{
0049 std::forward< Fn >( fn) };
0050 future< result_type > f{ pt.get_future() };
0051 fiber{ std::move( pt), std::forward< Args >( args) ... }.detach();
0052 return f;
0053 }
0054
0055 template< typename Policy, typename Fn, typename ... Args >
0056 future<
0057 typename result_of<
0058 typename std::enable_if<
0059 detail::is_launch_policy< Policy >::value,
0060 typename std::decay< Fn >::type
0061 >::type( typename std::decay< Args >::type ...)
0062 >::type
0063 >
0064 async( Policy policy, Fn && fn, Args ... args) {
0065 typedef typename result_of<
0066 typename std::decay< Fn >::type( typename std::decay< Args >::type ... )
0067 >::type result_type;
0068
0069 packaged_task< result_type( typename std::decay< Args >::type ... ) > pt{
0070 std::forward< Fn >( fn) };
0071 future< result_type > f{ pt.get_future() };
0072 fiber{ policy, std::move( pt), std::forward< Args >( args) ... }.detach();
0073 return f;
0074 }
0075
0076 template< typename Policy, typename StackAllocator, typename Fn, typename ... Args >
0077 future<
0078 typename result_of<
0079 typename std::enable_if<
0080 detail::is_launch_policy< Policy >::value,
0081 typename std::decay< Fn >::type
0082 >::type( typename std::decay< Args >::type ... )
0083 >::type
0084 >
0085 async( Policy policy, std::allocator_arg_t, StackAllocator salloc, Fn && fn, Args ... args) {
0086 typedef typename result_of<
0087 typename std::decay< Fn >::type( typename std::decay< Args >::type ... )
0088 >::type result_type;
0089
0090 packaged_task< result_type( typename std::decay< Args >::type ... ) > pt{
0091 std::forward< Fn >( fn) };
0092 future< result_type > f{ pt.get_future() };
0093 fiber{ policy, std::allocator_arg, salloc,
0094 std::move( pt), std::forward< Args >( args) ... }.detach();
0095 return f;
0096 }
0097
0098 template< typename Policy, typename StackAllocator, typename Allocator, typename Fn, typename ... Args >
0099 future<
0100 typename result_of<
0101 typename std::enable_if<
0102 detail::is_launch_policy< Policy >::value,
0103 typename std::decay< Fn >::type
0104 >::type( typename std::decay< Args >::type ... )
0105 >::type
0106 >
0107 async( Policy policy, std::allocator_arg_t, StackAllocator salloc, Allocator alloc, Fn && fn, Args ... args) {
0108 typedef typename result_of<
0109 typename std::decay< Fn >::type( typename std::decay< Args >::type ... )
0110 >::type result_type;
0111
0112 packaged_task< result_type( typename std::decay< Args >::type ... ) > pt{
0113 std::allocator_arg, alloc, std::forward< Fn >( fn) };
0114 future< result_type > f{ pt.get_future() };
0115 fiber{ policy, std::allocator_arg, salloc,
0116 std::move( pt), std::forward< Args >( args) ... }.detach();
0117 return f;
0118 }
0119
0120 }}
0121
0122 #endif