HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
awaitable_stop_token_impl.hpp
1// Copyright Take Vos 2023.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "awaitable_stop_token_intf.hpp"
8#if HI_OPERATING_SYSTEM == HI_OS_WINDOWS
9#include "loop_win32_intf.hpp"
10#endif
11#include "../macros.hpp"
12#include <utility>
13#include <coroutine>
14#include <chrono>
15#include <stop_token>
16
17hi_export_module(hikogui.dispatch : awaitable_stop_token_impl);
18
19hi_export namespace hi::inline v1 {
20
21inline void awaitable_stop_token::callback_wrapper::operator()() noexcept
22{
23 // Stop tokens are specifically designed to be called from a different thread,
24 // so we will post the function to the same thread as the co_await.
25 await_loop->post_function([this]() {
26 if (handle and not handle.done()) {
27 handle.resume();
28 }
29 });
30}
31
32inline void awaitable_stop_token::await_suspend(std::coroutine_handle<> handle) noexcept
33{
34 _stop_callback_ptr = std::make_unique<std::stop_callback<callback_wrapper>>(_stop_token, callback_wrapper{&loop::local(), handle});
35}
36
37}
DOXYGEN BUG.
Definition algorithm_misc.hpp:20