33 #ifdef ASIO_ERROR_CATEGORY_NOEXCEPT
34 # undef ASIO_ERROR_CATEGORY_NOEXCEPT
37 #define ASIO_ERROR_CATEGORY_NOEXCEPT noexcept(true)
38 #include <asio/io_context.hpp>
39 #undef ASIO_ERROR_CATEGORY_NOEXCEPT
44 enum class loop_mode : int { stopped = 0, starting = 1, running = 2 };
45 static std::map<std::string, std::shared_ptr<AsioContextManager>>
47 std::atomic<int> runCounter{
50 std::unique_ptr<asio::io_context> ictx;
51 std::unique_ptr<asio::io_context::work>
53 bool leakOnDelete =
false;
54 std::atomic<loop_mode> running{loop_mode::stopped};
55 std::mutex runningLoopLock;
56 std::atomic<bool> terminateLoop{
false};
57 std::future<void> loopRet;
64 explicit Servicer(std::shared_ptr<AsioContextManager> manager):
65 contextManager(std::move(manager))
73 contextManager->haltContextLoop();
81 Servicer(Servicer&& sv) =
default;
84 std::shared_ptr<AsioContextManager> contextManager;
88 using LoopHandle = std::unique_ptr<Servicer>;
94 static std::shared_ptr<AsioContextManager>
101 static std::shared_ptr<AsioContextManager>
105 static asio::io_context&
getContext(
const std::string& contextName = std::string());
109 static asio::io_context&
getExistingContext(
const std::string& contextName = std::string());
111 static void closeContext(
const std::string& contextName = std::string());
123 const std::string&
getName()
const {
return name; }
134 static LoopHandle
runContextLoop(
const std::string& contextName = std::string{});
143 bool isRunning()
const {
return (running.load() != loop_mode::stopped); }
150 void haltContextLoop();
152 friend void contextProcessingLoop(std::shared_ptr<AsioContextManager> ptr);
155 void contextProcessingLoop(std::shared_ptr<AsioContextManager> ptr);