1 // license:BSD-3-Clause
2 // copyright-holders:Olivier Galibert, R. Belmont
3 //============================================================
4 //
5 //  watchdog.c - watchdog handling
6 //
7 //  SDLMAME by Olivier Galibert and R. Belmont
8 //
9 //============================================================
10 
11 #include "osdcomm.h"
12 #include "osdcore.h"
13 #include "eminline.h"
14 
15 #include "watchdog.h"
16 #include "modules/lib/osdlib.h"
17 
18 #include <cstdio>
19 
20 
21 
osd_watchdog()22 osd_watchdog::osd_watchdog()
23 	: m_timeout(60 * osd_ticks_per_second())
24 	, m_event(1, 0)
25 	, m_do_exit()
26 	, m_thread()
27 {
28 	m_thread.reset(new std::thread(&osd_watchdog::watchdog_thread, this));
29 }
30 
~osd_watchdog()31 osd_watchdog::~osd_watchdog()
32 {
33 	m_do_exit = 1;
34 	m_event.set();
35 	m_thread->join();
36 }
37 
setTimeout(int timeout)38 void osd_watchdog::setTimeout(int timeout)
39 {
40 	m_timeout = timeout * osd_ticks_per_second();
41 	reset();
42 }
43 
watchdog_thread(void * param)44 void *osd_watchdog::watchdog_thread(void *param)
45 {
46 	auto *const thiz(reinterpret_cast<osd_watchdog *>(param));
47 
48 	while (true)
49 	{
50 		if (thiz->wait())
51 		{
52 			if (thiz->do_exit())
53 				break;
54 			else
55 				thiz->clear_event();
56 		}
57 		else
58 		{
59 			std::fflush(stdout);
60 			std::fprintf(stderr, "Terminating due to watchdog timeout\n");
61 			std::fflush(stderr);
62 
63 			osd_process_kill();
64 		}
65 	}
66 	return nullptr;
67 }
68