JeVois  1.22
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
Watchdog.C
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2021 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
19#include <jevois/Util/Async.H>
20#include <jevois/Util/Utils.H>
21#include <jevois/Debug/Log.H>
22
23#include <functional>
24#include <unistd.h>
25
26// ####################################################################################################
28{
29 // Launch run thread:
30 itsRunFut = jevois::async_little(std::bind(&jevois::Watchdog::run, this, timeout));
31
32 // Wait until we are running:
33 while (itsRunning.load() == false) std::this_thread::sleep_for(std::chrono::milliseconds(5));
34}
35
36// ####################################################################################################
38{
39 itsReset = true;
40
41 // Stop the run() thread:
42 itsRunning = false;
43 JEVOIS_WAIT_GET_FUTURE(itsRunFut);
44}
45
46// ####################################################################################################
48{ itsReset.store(true); }
49
50// ####################################################################################################
51void jevois::Watchdog::run(double timeout)
52{
53 itsRunning.store(true);
54 if (timeout <= 0.0F) return;
55 auto const dur = std::chrono::microseconds(long(timeout * 1.0e6));
56
57 while (itsRunning.load())
58 {
59 // Sleep...
60 itsReset.store(false);
61 std::this_thread::sleep_for(dur);
62
63 // If reset() was not called, kill our process:
64 if (itsReset.load() == false)
65 {
66 LERROR("Watchdog timed out -- KILLING PROCESS");
67 jevois::system("/usr/bin/kill -9 " + std::to_string(getpid()));
68 }
69 }
70}
#define JEVOIS_WAIT_GET_FUTURE(f)
Wait for a future to become ready for 5 seconds, get(), warn and ignore exception,...
Definition Log.H:336
void run(double timeout)
Definition Watchdog.C:51
std::atomic< bool > itsRunning
Definition Watchdog.H:45
~Watchdog()
Virtual destructor for safe inheritance.
Definition Watchdog.C:37
std::future< void > itsRunFut
Definition Watchdog.H:43
Watchdog(double timeout)
Constructor.
Definition Watchdog.C:27
void reset()
Reset our internal timer. If this does not happen at least every timeout seconds, process is killed.
Definition Watchdog.C:47
#define LERROR(msg)
Convenience macro for users to print out console or syslog messages, ERROR level.
Definition Log.H:211
std::string system(std::string const &cmd, bool errtoo=true)
Execute a command and grab stdout output to a string.
Definition Utils.C:462
std::future< std::invoke_result_t< std::decay_t< Function >, std::decay_t< Args >... > > async_little(Function &&f, Args &&... args)
Async execution using a thread pool.