diff --git a/src/osd/modules/lib/osdlib_macosx.cpp b/src/osd/modules/lib/osdlib_macosx.cpp index 18730ea70ff..94b935de08d 100644 --- a/src/osd/modules/lib/osdlib_macosx.cpp +++ b/src/osd/modules/lib/osdlib_macosx.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include @@ -49,8 +50,10 @@ int osd_setenv(const char *name, const char *value, int overwrite) // osd_process_kill //============================================================ -void osd_process_kill(void) +void osd_process_kill() { + std::fflush(stdout); + std::fflush(stderr); kill(getpid(), SIGKILL); } diff --git a/src/osd/modules/lib/osdlib_unix.cpp b/src/osd/modules/lib/osdlib_unix.cpp index a504dec8aa3..054c4591f71 100644 --- a/src/osd/modules/lib/osdlib_unix.cpp +++ b/src/osd/modules/lib/osdlib_unix.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include @@ -47,8 +48,10 @@ int osd_setenv(const char *name, const char *value, int overwrite) // osd_process_kill //============================================================ -void osd_process_kill(void) +void osd_process_kill() { + std::fflush(stdout); + std::fflush(stderr); kill(getpid(), SIGKILL); } diff --git a/src/osd/modules/lib/osdlib_win32.cpp b/src/osd/modules/lib/osdlib_win32.cpp index 06a979a7a77..015504f06dc 100644 --- a/src/osd/modules/lib/osdlib_win32.cpp +++ b/src/osd/modules/lib/osdlib_win32.cpp @@ -17,6 +17,7 @@ #include #endif +#include #include // MAME headers @@ -98,8 +99,10 @@ int osd_setenv(const char *name, const char *value, int overwrite) // osd_process_kill //============================================================ -void osd_process_kill(void) +void osd_process_kill() { + std::fflush(stdout); + std::fflush(stderr); TerminateProcess(GetCurrentProcess(), -1); } diff --git a/src/osd/watchdog.cpp b/src/osd/watchdog.cpp index f963ef13f07..09f720b7fcc 100644 --- a/src/osd/watchdog.cpp +++ b/src/osd/watchdog.cpp @@ -15,50 +15,53 @@ #include "watchdog.h" #include "modules/lib/osdlib.h" -static void *watchdog_thread(void *param) -{ - osd_watchdog *thiz = (osd_watchdog *) param; +#include - while (TRUE) + + +osd_watchdog::osd_watchdog() + : m_timeout(60 * osd_ticks_per_second()) + , m_event(1, 0) + , m_do_exit() + , m_thread() +{ + m_thread.reset(new std::thread(&osd_watchdog::watchdog_thread, this)); +} + +osd_watchdog::~osd_watchdog() +{ + m_do_exit = 1; + m_event.set(); + m_thread->join(); +} + +void osd_watchdog::setTimeout(int timeout) +{ + m_timeout = timeout * osd_ticks_per_second(); + reset(); +} + +void *osd_watchdog::watchdog_thread(void *param) +{ + osd_watchdog *const thiz(reinterpret_cast(param)); + + while (true) { - if (thiz->event().wait(thiz->getTimeout())) + if (thiz->wait()) { if (thiz->do_exit()) break; else - { - thiz->event().reset(); - continue; - } + thiz->clear_event(); } else { - fprintf(stderr, "Terminating due to watchdog timeout\n"); + std::fflush(stdout); + std::fprintf(stderr, "Terminating due to watchdog timeout\n"); + std::fflush(stderr); osd_process_kill(); } } return nullptr; } - -osd_watchdog::osd_watchdog(void) -: m_event(1,0) -{ - m_do_exit = 0; - m_thread = new std::thread(watchdog_thread, this); - m_timeout = 60 * osd_ticks_per_second(); -} - -osd_watchdog::~osd_watchdog(void) -{ - m_do_exit = 1; - m_event.set(); - m_thread->join(); - delete m_thread; -} - -void osd_watchdog::setTimeout(int timeout) -{ - m_timeout = timeout * osd_ticks_per_second(); - this->reset(); -} diff --git a/src/osd/watchdog.h b/src/osd/watchdog.h index 77cf3c77c7c..0076bc42c37 100644 --- a/src/osd/watchdog.h +++ b/src/osd/watchdog.h @@ -1,7 +1,9 @@ // license:BSD-3-Clause // copyright-holders:Olivier Galibert, R. Belmont -#ifndef _watchdog_h_ -#define _watchdog_h_ +#ifndef MAME_OSD_WATCHDOG_H +#define MAME_OSD_WATCHDOG_H +#pragma once + //============================================================ // // watchdog.h - watchdog handling @@ -11,26 +13,34 @@ //============================================================ #include "osdsync.h" + #include +#include +#include #include + class osd_watchdog { public: - osd_watchdog(void); - ~osd_watchdog(void); + osd_watchdog(); + ~osd_watchdog(); - void reset() { m_event.set(); } - - osd_event & event(void) { return m_event; } - INT32 do_exit(void) const { return m_do_exit; } osd_ticks_t getTimeout(void) const { return m_timeout; } void setTimeout(int timeout); -private: - osd_event m_event; - std::thread* m_thread; - std::atomic m_do_exit; - osd_ticks_t m_timeout; + void reset() { m_event.set(); } + +private: + static void *watchdog_thread(void *param); + + void clear_event() { m_event.reset(); } + bool wait() { return m_event.wait(getTimeout()); } + bool do_exit(void) const { return m_do_exit != 0; } + + osd_ticks_t m_timeout; + osd_event m_event; + std::atomic m_do_exit; + std::unique_ptr m_thread; }; -#endif +#endif // MAME_OSD_WATCHDOG_H