5#ifdef MAYAFLUX_PLATFORM_MACOS
6#include <CoreFoundation/CoreFoundation.h>
7#include <dispatch/dispatch.h>
11#ifdef MAYAFLUX_PLATFORM_WINDOWS
17#define MAYAFLUX_WM_DISPATCH (WM_USER + 0x0001)
22#ifdef MAYAFLUX_PLATFORM_MACOS
33template <
typename Func,
typename... Args>
36 dispatch_async(dispatch_get_main_queue(), ^{
37 std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
53template <
typename Func,
typename... Args>
56 using ResultType = std::invoke_result_t<Func, Args...>;
58 if constexpr (std::is_void_v<ResultType>) {
59 if (pthread_main_np() != 0) {
60 std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
63 dispatch_sync(dispatch_get_main_queue(), ^{
64 std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
67 if (pthread_main_np() != 0) {
68 return static_cast<ResultType
>(std::invoke(std::forward<Func>(func), std::forward<Args>(args)...));
70 __block ResultType result;
71 dispatch_sync(dispatch_get_main_queue(), ^{
72 result = std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
90template <
typename Func,
typename... Args>
93 auto completed = std::make_shared<std::atomic<bool>>(
false);
95 dispatch_async(dispatch_get_main_queue(), ^{
96 std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
97 completed->store(
true, std::memory_order_release);
100 auto start = std::chrono::steady_clock::now();
101 while (!completed->load(std::memory_order_acquire)) {
102 if (std::chrono::steady_clock::now() - start > timeout_ms) {
105 std::this_thread::sleep_for(std::chrono::milliseconds(1));
125 if (pthread_main_np() != 0) {
126 while (future.wait_for(std::chrono::milliseconds(0)) != std::future_status::ready) {
127 CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.01,
false);
134template <
typename Func,
typename... Args>
137 std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
140template <
typename Func,
typename... Args>
143 return std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
146template <
typename Func,
typename... Args>
149 std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
T await_future(std::future< T > future)
auto dispatch_main_sync(Func &&func, Args &&... args) -> decltype(auto)
void dispatch_main_async(Func &&func, Args &&... args)
bool dispatch_main_async_with_timeout(std::chrono::milliseconds, Func &&func, Args &&... args)