MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Parallel.hpp
Go to the documentation of this file.
1#pragma once
2
3#ifdef MAYAFLUX_PLATFORM_MACOS
4#include "oneapi/dpl/algorithm"
5#include "oneapi/dpl/execution"
6#include <dispatch/dispatch.h>
7#else
8#include <execution>
9#endif
10
12
13using std::for_each;
14using std::sort;
15using std::transform;
16using std::execution::par;
17using std::execution::par_unseq;
18using std::execution::seq;
19
20#ifdef MAYAFLUX_PLATFORM_MACOS
21/**
22 * @brief Execute a function on the main dispatch queue (macOS only)
23 * @tparam Func Callable type
24 * @tparam Args Argument types
25 * @param func Function to execute
26 * @param args Arguments to forward to the function
27 *
28 * Schedules work on the main queue asynchronously. Returns immediately.
29 * Use this for GLFW operations that must execute on the main thread.
30 */
31template <typename Func, typename... Args>
32void dispatch_main_async(Func&& func, Args&&... args)
33{
34 dispatch_async(dispatch_get_main_queue(), ^{
35 std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
36 });
37}
38
39/**
40 * @brief Execute a function on the main dispatch queue and wait (macOS only)
41 * @tparam Func Callable type that returns a result
42 * @tparam Args Argument types
43 * @param func Function to execute
44 * @param args Arguments to forward to the function
45 * @return The result returned by func
46 *
47 * Schedules work on the main queue synchronously. Blocks until complete.
48 * WARNING: Can deadlock if called from main thread or during Cocoa modal loops.
49 */
50template <typename Func, typename... Args>
51auto dispatch_main_sync(Func&& func, Args&&... args) -> decltype(auto)
52{
53 using ResultType = std::invoke_result_t<Func, Args...>;
54
55 if constexpr (std::is_void_v<ResultType>) {
56 dispatch_sync(dispatch_get_main_queue(), ^{
57 std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
58 });
59 } else {
60 __block ResultType result;
61 dispatch_sync(dispatch_get_main_queue(), ^{
62 result = std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
63 });
64 return result;
65 }
66}
67
68/**
69 * @brief Execute a function on the main queue with timeout (macOS only)
70 * @tparam Func Callable type
71 * @tparam Args Argument types
72 * @param timeout_ms Timeout in milliseconds
73 * @param func Function to execute
74 * @param args Arguments to forward to the function
75 * @return true if completed before timeout, false if timed out
76 *
77 * Schedules work asynchronously and waits with timeout.
78 * Returns false if the operation doesn't complete in time.
79 */
80template <typename Func, typename... Args>
81bool dispatch_main_async_with_timeout(std::chrono::milliseconds timeout_ms, Func&& func, Args&&... args)
82{
83 auto completed = std::make_shared<std::atomic<bool>>(false); // Shared pointer for capture
84
85 dispatch_async(dispatch_get_main_queue(), ^{
86 std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
87 completed->store(true, std::memory_order_release);
88 });
89
90 auto start = std::chrono::steady_clock::now();
91 while (!completed->load(std::memory_order_acquire)) {
92 if (std::chrono::steady_clock::now() - start > timeout_ms) {
93 return false;
94 }
95 std::this_thread::sleep_for(std::chrono::milliseconds(1));
96 }
97
98 return true;
99}
100
101#else
102// On non-macOS platforms, these just execute directly
103template <typename Func, typename... Args>
104void dispatch_main_async(Func&& func, Args&&... args)
105{
106 std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
107}
108
109template <typename Func, typename... Args>
110auto dispatch_main_sync(Func&& func, Args&&... args) -> decltype(auto)
111{
112 return std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
113}
114
115template <typename Func, typename... Args>
116bool dispatch_main_async_with_timeout(std::chrono::milliseconds /*timeout_ms*/, Func&& func, Args&&... args)
117{
118 std::invoke(std::forward<Func>(func), std::forward<Args>(args)...);
119 return true;
120}
121#endif
122
123} // namespace MayaFlux::Parallel
auto dispatch_main_sync(Func &&func, Args &&... args) -> decltype(auto)
Definition Parallel.hpp:110
void dispatch_main_async(Func &&func, Args &&... args)
Definition Parallel.hpp:104
bool dispatch_main_async_with_timeout(std::chrono::milliseconds, Func &&func, Args &&... args)
Definition Parallel.hpp:116