-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathwatchdog_scope.h
More file actions
77 lines (69 loc) · 2.25 KB
/
Copy pathwatchdog_scope.h
File metadata and controls
77 lines (69 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma once
#ifndef WATCHDOG_SCOPE_H
#define WATCHDOG_SCOPE_H
#include "system_monitor.h"
/**
* @brief RAII wrapper for automatic watchdog management
*
* This class uses RAII (Resource Acquisition Is Initialization) pattern to automatically
* reset the watchdog timer on both entry and exit of a scope. This eliminates the need
* for manual watchdog resets and ensures protection even if early returns occur.
*
* Usage:
* ```cpp
* void criticalFunction() {
* WatchdogScope wd; // Watchdog reset on entry
* // ... long-running operation ...
* // Watchdog automatically reset on exit (even if early return)
* }
* ```
*
* Or use the convenience macro:
* ```cpp
* void criticalFunction() {
* WATCHDOG_SCOPE(); // Creates unnamed WatchdogScope instance
* // ... long-running operation ...
* }
* ```
*/
class WatchdogScope {
public:
/**
* @brief Constructor - resets watchdog on scope entry
*/
WatchdogScope() {
systemMonitor.forceResetWatchdog();
}
/**
* @brief Destructor - resets watchdog on scope exit
*
* Ensures watchdog is reset even if function exits early via return,
* exception, or other control flow.
*/
~WatchdogScope() {
systemMonitor.forceResetWatchdog();
}
/**
* @brief Manual reset during long operations within the scope
*
* While constructor/destructor provide automatic resets at scope boundaries,
* this method allows manual resets during long-running operations within the scope.
*/
void reset() {
systemMonitor.forceResetWatchdog();
}
// Prevent copying (watchdog resets should be scoped, not copied)
WatchdogScope(const WatchdogScope&) = delete;
WatchdogScope& operator=(const WatchdogScope&) = delete;
// Allow moving (transfer ownership of watchdog scope)
WatchdogScope(WatchdogScope&&) noexcept = default;
WatchdogScope& operator=(WatchdogScope&&) noexcept = default;
};
/**
* @brief Convenience macro for creating a watchdog scope guard
*
* Creates an unnamed WatchdogScope instance that will reset the watchdog
* on entry and exit of the current scope.
*/
#define WATCHDOG_SCOPE() WatchdogScope _watchdog_scope_guard_##__COUNTER__
#endif // WATCHDOG_SCOPE_H