MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ await_shutdown()

void MayaFlux::Core::Engine::await_shutdown ( )

Blocks until shutdown is requested (main thread event loop)

Pumps platform-specific events on the main thread and waits for shutdown. On macOS: Runs CFRunLoop to process dispatch queue (required for GLFW) On other platforms: Simple blocking wait for user input

Should be called on the main thread after Start().

Definition at line 201 of file Engine.cpp.

202{
203#ifdef MAYAFLUX_PLATFORM_MACOS
204 run_macos_event_loop();
205#elif defined(MAYAFLUX_PLATFORM_WINDOWS)
206 static std::atomic<bool> s_signal_received { false };
207 s_signal_received.store(false, std::memory_order_relaxed);
208
209 SetConsoleCtrlHandler([](DWORD type) -> BOOL {
210 if (type == CTRL_C_EVENT || type == CTRL_BREAK_EVENT || type == CTRL_CLOSE_EVENT) {
211 s_signal_received.store(true, std::memory_order_release);
212 return TRUE;
213 }
214 return FALSE;
215 },
216 TRUE);
217
218 HANDLE h_stdin = GetStdHandle(STD_INPUT_HANDLE);
219 bool has_console = h_stdin != INVALID_HANDLE_VALUE && GetFileType(h_stdin) == FILE_TYPE_CHAR;
220
221 if (has_console) {
222 while (!s_signal_received.load(std::memory_order_acquire)
223 && !m_should_shutdown.load(std::memory_order_acquire)) {
224 if (WaitForSingleObject(h_stdin, 100) == WAIT_OBJECT_0) {
225 INPUT_RECORD rec {};
226 DWORD read {};
227 if (ReadConsoleInputW(h_stdin, &rec, 1, &read) && read > 0
228 && rec.EventType == KEY_EVENT
229 && rec.Event.KeyEvent.bKeyDown
230 && rec.Event.KeyEvent.wVirtualKeyCode == VK_RETURN) {
231 break;
232 }
233 }
234 }
235 } else {
236 m_should_shutdown.wait(false);
237 }
238#else
239 static std::atomic<bool> s_signal_received { false };
240 s_signal_received.store(false, std::memory_order_relaxed);
241
242 struct sigaction sa {};
243 sa.sa_handler = [](int) { s_signal_received.store(true, std::memory_order_release); };
244 sigaction(SIGINT, &sa, nullptr);
245 sigaction(SIGTERM, &sa, nullptr);
246
247 bool has_tty = isatty(STDIN_FILENO);
248 if (has_tty) {
249 while (!s_signal_received.load(std::memory_order_acquire)) {
250 fd_set fds;
251 FD_ZERO(&fds);
252 FD_SET(STDIN_FILENO, &fds);
253 timeval tv { .tv_sec = 0, .tv_usec = 100000 };
254 if (select(1, &fds, nullptr, nullptr, &tv) > 0) {
255 char c {};
256 if (read(STDIN_FILENO, &c, 1) > 0 && c == '\n')
257 break;
258 }
259 }
260 } else {
261 sigset_t mask;
262 sigfillset(&mask);
263 sigdelset(&mask, SIGINT);
264 sigdelset(&mask, SIGTERM);
265 sigsuspend(&mask);
266 }
267
268#endif
269
270 m_should_shutdown.store(true, std::memory_order_release);
271
273 "Shutdown requested, awaiting all subsystem termination ......");
274}
#define MF_LOG(comp, ctx,...)
std::atomic< bool > m_should_shutdown
Definition Engine.hpp:393
@ Runtime
General runtime operations (default fallback)
@ Core
Core engine, backend, subsystems.
Tendency< D, R > select(const Tendency< D, float > &predicate, const Tendency< D, R > &then_t, const Tendency< D, R > &else_t)
Select between two tendencies based on a predicate tendency.
Definition Tendency.hpp:202

References MayaFlux::Journal::Core, m_should_shutdown, MF_LOG, and MayaFlux::Journal::Runtime.

Referenced by MayaFlux::Await().

+ Here is the caller graph for this function: