Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:44:27

0001 // Copyright (c) 2022 Klemens D. Morgenstern
0002 //
0003 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0004 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0005 #ifndef BOOST_COBALT_RUN_HPP
0006 #define BOOST_COBALT_RUN_HPP
0007 
0008 #include <boost/cobalt/spawn.hpp>
0009 #include <boost/cobalt/task.hpp>
0010 
0011 #include <boost/asio/use_future.hpp>
0012 
0013 namespace boost::cobalt
0014 {
0015 
0016 template<typename T>
0017 T run(task<T> t)
0018 {
0019 #if !defined(BOOST_COBALT_NO_PMR)
0020   pmr::unsynchronized_pool_resource root_resource{this_thread::get_default_resource()};
0021     struct reset_res
0022     {
0023         void operator()(pmr::memory_resource * res)
0024         {
0025             this_thread::set_default_resource(res);
0026         }
0027     };
0028     std::unique_ptr<pmr::memory_resource, reset_res> pr{
0029             boost::cobalt::this_thread::set_default_resource(&root_resource)};
0030 #endif
0031     std::future<T> f;
0032     {
0033       asio::io_context ctx{BOOST_ASIO_CONCURRENCY_HINT_1};
0034       struct reset_exec
0035       {
0036         std::optional<executor> exec;
0037 
0038         reset_exec()
0039         {
0040           if (this_thread::has_executor())
0041             exec = this_thread::get_executor();
0042         }
0043 
0044         ~reset_exec()
0045         {
0046           if (exec)
0047             this_thread::set_executor(*exec);
0048         }
0049       };
0050 
0051       reset_exec re;
0052       this_thread::set_executor(ctx.get_executor());
0053       f = spawn(ctx, std::move(t), asio::bind_executor(ctx.get_executor(), asio::use_future));
0054       ctx.run();
0055     }
0056     return f.get();
0057 }
0058 
0059 }
0060 
0061 #endif //BOOST_COBALT_RUN_HPP