GCC Code Coverage Report


Directory: ./
File: Timers.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 135 147 91.8%
Functions: 12 12 100.0%
Branches: 133 220 60.5%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2022 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the LGPL v2.1 as described in the
10 * included LICENSE.LGPL file.
11 */
12
13 #include "fpp-pch.h"
14
15 #include <algorithm>
16 #include <climits>
17 #include <list>
18 #include <mutex>
19 #include <string>
20 #include <map>
21 #include <vector>
22
23 #include "common.h"
24 #include "commands/Commands.h"
25
26 #include "EPollManager.h"
27 #include "Timers.h"
28
29 Timers Timers::INSTANCE;
30
31 3252 Timers::Timers() {
32 3252 }
33
34 // Named timers that fireTimersInternal() has copied out to fire and not yet
35 // fired, with an epoch bumped (under Timers::lock) whenever one is stopped or
36 // replaced. fireTimersInternal() copies every due timer before running any of
37 // them, so without this a timer that an earlier callback in the same pass
38 // stopped -- a plugin's unload stopping its own periodic timer, then deleting
39 // the plugin -- still ran once from its copy. An entry lives only while a
40 // copy is pending: a name per fired timer would grow without bound, since
41 // some timers take a fresh name every time (a per-play media stop, each pass
42 // of looping media). Kept here rather than in TimerInfo because Timers.h is
43 // plugin ABI.
44 struct PendingFire {
45 uint64_t epoch = 0;
46 int copies = 0;
47 };
48 static std::map<std::string, PendingFire> s_pendingFires;
49
50 771 static void BumpTimerEpoch(const std::string& name) {
51
1/2
✓ Branch 0 taken 771 times.
✗ Branch 1 not taken.
771 auto it = s_pendingFires.find(name);
52
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 721 times.
771 if (it != s_pendingFires.end()) {
53 50 it->second.epoch++;
54 }
55 771 }
56
57 // A caller's GetTimeMS() deadline as a GetMonotonicTimeMS() one.
58 3817 static long long WallToMonotonic(long long wallMS) {
59 3817 return GetMonotonicTimeMS() + (wallMS - GetTimeMS());
60 }
61
62 600 Timers::~Timers() {
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 600 times.
600 for (auto a : timers) {
64 ✗ if (a) {
65 ✗ delete a;
66 }
67 }
68 600 timers.clear();
69 600 }
70 3702 void Timers::addTimer(const std::string& name, long long fireTimeMS, std::function<void()>&& callback) {
71
1/2
✓ Branch 0 taken 3702 times.
✗ Branch 1 not taken.
3702 std::unique_lock<std::mutex> l(lock);
72
1/2
✓ Branch 0 taken 3702 times.
✗ Branch 1 not taken.
3702 if (!name.empty()) {
73
2/2
✓ Branch 0 taken 11898 times.
✓ Branch 1 taken 3223 times.
15121 for (auto& a : timers) {
74
6/6
✓ Branch 0 taken 10961 times.
✓ Branch 1 taken 937 times.
✓ Branch 2 taken 479 times.
✓ Branch 3 taken 10482 times.
✓ Branch 4 taken 479 times.
✓ Branch 5 taken 11419 times.
11898 if (a && a->id == name) {
75
1/2
✓ Branch 0 taken 479 times.
✗ Branch 1 not taken.
479 BumpTimerEpoch(name);
76
1/2
✓ Branch 0 taken 479 times.
✗ Branch 1 not taken.
479 a->fireTimeMS = WallToMonotonic(fireTimeMS);
77
1/2
✓ Branch 0 taken 479 times.
✗ Branch 1 not taken.
479 a->callback = callback;
78
1/2
✓ Branch 0 taken 479 times.
✗ Branch 1 not taken.
479 a->commandPreset = "";
79
1/2
✓ Branch 0 taken 479 times.
✗ Branch 1 not taken.
479 updateTimers();
80 479 return;
81 }
82 }
83 }
84
2/4
✓ Branch 0 taken 3223 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 3223 times.
3223 TimerInfo* i = new TimerInfo;
85
1/2
✓ Branch 0 taken 3223 times.
✗ Branch 1 not taken.
3223 i->id = name;
86
1/2
✓ Branch 0 taken 3223 times.
✗ Branch 1 not taken.
3223 i->callback = callback;
87
1/2
✓ Branch 0 taken 3223 times.
✗ Branch 1 not taken.
3223 i->fireTimeMS = WallToMonotonic(fireTimeMS);
88
1/2
✓ Branch 0 taken 3223 times.
✗ Branch 1 not taken.
3223 timers.push_back(i);
89
1/2
✓ Branch 0 taken 3223 times.
✗ Branch 1 not taken.
3223 updateTimers();
90
2/2
✓ Branch 0 taken 3223 times.
✓ Branch 1 taken 479 times.
3702 }
91 115 void Timers::addTimer(const std::string& name, long long fireTimeMS, const std::string& preset) {
92
2/4
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 115 times.
115 if (preset == "") {
93 ✗ return;
94 }
95
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 std::unique_lock<std::mutex> l(lock);
96
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 if (!name.empty()) {
97
2/2
✓ Branch 0 taken 230 times.
✓ Branch 1 taken 115 times.
345 for (auto& a : timers) {
98
3/6
✓ Branch 0 taken 230 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 230 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 230 times.
230 if (a && a->id == name) {
99 // A preset timer is a one-shot preset and nothing else: keeping
100 // the old entry's callback and periodic rate turned a preset
101 // aimed at an internal timer's id ("FileMonitor") into a preset
102 // fired every 2s for good, and that timer's own work never ran.
103 ✗ BumpTimerEpoch(name);
104 ✗ a->fireTimeMS = WallToMonotonic(fireTimeMS);
105 ✗ a->commandPreset = preset;
106 ✗ a->callback = nullptr;
107 ✗ a->periodicRate = 0;
108 ✗ updateTimers();
109 ✗ return;
110 }
111 }
112 }
113
2/4
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 115 times.
115 TimerInfo* i = new TimerInfo;
114
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 i->id = name;
115
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 i->commandPreset = preset;
116
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 i->fireTimeMS = WallToMonotonic(fireTimeMS);
117
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 timers.push_back(i);
118
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 updateTimers();
119
1/2
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
115 }
120
121 5619 void Timers::addPeriodicTimer(const std::string& name, long long fireTimeMS, std::function<void()>&& callback) {
122 // Kept as a distinct overload (rather than a default argument) so the original
123 // 3-arg mangled symbol remains in libfpp.so for pre-built external plugins.
124 5619 addPeriodicTimer(name, fireTimeMS, std::move(callback), -1);
125 5619 }
126 5633 void Timers::addPeriodicTimer(const std::string& name, long long fireTimeMS, std::function<void()>&& callback,
127 long long initialDelayMS) {
128
2/2
✓ Branch 0 taken 5619 times.
✓ Branch 1 taken 14 times.
5633 long long firstDelay = initialDelayMS < 0 ? fireTimeMS : initialDelayMS;
129
1/2
✓ Branch 0 taken 5633 times.
✗ Branch 1 not taken.
5633 std::unique_lock<std::mutex> l(lock);
130
1/2
✓ Branch 0 taken 5633 times.
✗ Branch 1 not taken.
5633 if (!name.empty()) {
131
2/2
✓ Branch 0 taken 9266 times.
✓ Branch 1 taken 5613 times.
14879 for (auto& a : timers) {
132
6/6
✓ Branch 0 taken 9264 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 9244 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 9246 times.
9266 if (a && a->id == name) {
133
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 BumpTimerEpoch(name);
134
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 a->fireTimeMS = GetMonotonicTimeMS() + firstDelay;
135 20 a->periodicRate = fireTimeMS;
136
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 a->callback = callback;
137
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 a->commandPreset = "";
138
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 updateTimers();
139 20 return;
140 }
141 }
142 }
143
2/4
✓ Branch 0 taken 5613 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5613 times.
5613 TimerInfo* i = new TimerInfo;
144
1/2
✓ Branch 0 taken 5613 times.
✗ Branch 1 not taken.
5613 i->id = name;
145
1/2
✓ Branch 0 taken 5613 times.
✗ Branch 1 not taken.
5613 i->callback = callback;
146
1/2
✓ Branch 0 taken 5613 times.
✗ Branch 1 not taken.
5613 i->fireTimeMS = GetMonotonicTimeMS() + firstDelay;
147 5613 i->periodicRate = fireTimeMS;
148
1/2
✓ Branch 0 taken 5613 times.
✗ Branch 1 not taken.
5613 timers.push_back(i);
149
1/2
✓ Branch 0 taken 5613 times.
✗ Branch 1 not taken.
5613 updateTimers();
150
2/2
✓ Branch 0 taken 5613 times.
✓ Branch 1 taken 20 times.
5633 }
151 272 void Timers::stopPeriodicTimer(const std::string& name) {
152
1/2
✓ Branch 0 taken 272 times.
✗ Branch 1 not taken.
272 std::unique_lock<std::mutex> l(lock);
153
1/2
✓ Branch 0 taken 272 times.
✗ Branch 1 not taken.
272 if (!name.empty()) {
154 // Even if it isn't in the list: a one-shot already taken out to fire
155 // in the current pass must not run after being stopped either.
156
1/2
✓ Branch 0 taken 272 times.
✗ Branch 1 not taken.
272 BumpTimerEpoch(name);
157
2/2
✓ Branch 0 taken 4924 times.
✓ Branch 1 taken 84 times.
5008 for (int x = 0; x < timers.size(); ++x) {
158 4924 auto a = timers[x];
159
5/6
✓ Branch 0 taken 4924 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 188 times.
✓ Branch 3 taken 4736 times.
✓ Branch 4 taken 188 times.
✓ Branch 5 taken 4736 times.
4924 if (a && a->id == name) {
160
1/2
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
188 delete a;
161 188 timers[x] = nullptr;
162
1/2
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
188 updateTimers();
163 188 return;
164 }
165 }
166 }
167
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 188 times.
272 }
168
169 21519 void Timers::fireTimersInternal(long long t) {
170 // Callbacks run with the lock released (they add and stop timers), so they
171 // must not run out of a TimerInfo that is still in the list: a
172 // stopPeriodicTimer() from another thread deletes it, and a periodic timer
173 // that stops or re-adds itself from its own callback destroys or replaces
174 // the std::function that is executing. Fire private copies instead.
175 21519 std::list<std::pair<TimerInfo, uint64_t>> toFire; // copy, epoch when copied
176
177
1/2
✓ Branch 0 taken 21519 times.
✗ Branch 1 not taken.
21519 std::unique_lock<std::mutex> l(lock);
178 21519 int m = timers.size();
179 21519 bool fired = false;
180
2/2
✓ Branch 0 taken 139469 times.
✓ Branch 1 taken 21519 times.
160988 for (int x = 0; x < m; ++x) {
181 139469 auto a = timers[x];
182
3/4
✓ Branch 0 taken 139469 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 87972 times.
✓ Branch 3 taken 51497 times.
139469 if (a && (a->fireTimeMS <= t)) {
183 87972 uint64_t epoch = 0;
184
1/2
✓ Branch 0 taken 87972 times.
✗ Branch 1 not taken.
87972 if (!a->id.empty()) {
185
1/2
✓ Branch 0 taken 87972 times.
✗ Branch 1 not taken.
87972 PendingFire& pf = s_pendingFires[a->id];
186 87972 pf.copies++;
187 87972 epoch = pf.epoch;
188 }
189
2/2
✓ Branch 0 taken 84778 times.
✓ Branch 1 taken 3194 times.
87972 if (a->periodicRate) {
190
1/2
✓ Branch 0 taken 84778 times.
✗ Branch 1 not taken.
84778 a->fireTimeMS = GetMonotonicTimeMS() + a->periodicRate;
191
1/2
✓ Branch 0 taken 84778 times.
✗ Branch 1 not taken.
84778 toFire.emplace_back(*a, epoch);
192 } else {
193
1/2
✓ Branch 0 taken 3194 times.
✗ Branch 1 not taken.
3194 toFire.emplace_back(std::move(*a), epoch);
194
1/2
✓ Branch 0 taken 3194 times.
✗ Branch 1 not taken.
3194 delete a;
195 3194 timers[x] = nullptr;
196 }
197 }
198 }
199
1/2
✓ Branch 0 taken 21519 times.
✗ Branch 1 not taken.
21519 l.unlock();
200 // The list changed (one-shots taken out, periodic deadlines moved) even
201 // if every callback below turns out to be skipped, so re-arm regardless.
202 21519 fired = !toFire.empty();
203
2/2
✓ Branch 0 taken 87972 times.
✓ Branch 1 taken 21519 times.
109491 for (auto& [a, epoch] : toFire) {
204
1/2
✓ Branch 0 taken 87972 times.
✗ Branch 1 not taken.
87972 if (!a.id.empty()) {
205
1/2
✓ Branch 0 taken 87972 times.
✗ Branch 1 not taken.
87972 std::unique_lock<std::mutex> el(lock);
206
1/2
✓ Branch 0 taken 87972 times.
✗ Branch 1 not taken.
87972 auto it = s_pendingFires.find(a.id);
207 87972 bool stale = it->second.epoch != epoch;
208
1/2
✓ Branch 0 taken 87972 times.
✗ Branch 1 not taken.
87972 if (--it->second.copies == 0) {
209
1/2
✓ Branch 0 taken 87972 times.
✗ Branch 1 not taken.
87972 s_pendingFires.erase(it);
210 }
211
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 87922 times.
87972 if (stale) {
212 50 continue; // stopped or replaced by an earlier callback this pass
213 }
214
2/2
✓ Branch 0 taken 87922 times.
✓ Branch 1 taken 50 times.
87972 }
215
1/2
✓ Branch 0 taken 87922 times.
✗ Branch 1 not taken.
87922 fireTimer(&a);
216 }
217
1/2
✓ Branch 0 taken 21519 times.
✗ Branch 1 not taken.
21519 if (fired) {
218
1/2
✓ Branch 0 taken 21519 times.
✗ Branch 1 not taken.
21519 l.lock();
219
1/2
✓ Branch 0 taken 21519 times.
✗ Branch 1 not taken.
21519 updateTimers();
220 }
221 21519 }
222 31157 void Timers::updateTimers() {
223
2/2
✓ Branch 0 taken 175572 times.
✓ Branch 1 taken 31157 times.
206729 for (int x = 0; x < timers.size(); x++) {
224
6/6
✓ Branch 0 taken 176877 times.
✓ Branch 1 taken 2077 times.
✓ Branch 2 taken 3382 times.
✓ Branch 3 taken 173495 times.
✓ Branch 4 taken 3382 times.
✓ Branch 5 taken 175572 times.
178954 while (x < timers.size() && timers[x] == nullptr) {
225 3382 timers[x] = timers[timers.size() - 1];
226 3382 timers.resize(timers.size() - 1);
227 }
228 }
229
1/2
✓ Branch 0 taken 31157 times.
✗ Branch 1 not taken.
31157 if (!timers.empty()) {
230 // Worked out locally and stored once: fireTimers() reads nextTimer
231 // without the lock.
232 31157 long long next = LLONG_MAX;
233
2/2
✓ Branch 0 taken 173495 times.
✓ Branch 1 taken 31157 times.
204652 for (int x = 0; x < timers.size(); x++) {
234 173495 next = std::min(timers[x]->fireTimeMS, next);
235 }
236 31157 nextTimer = next;
237 31157 hasTimers = true;
238 // armTimer() takes a GetTimeMS() deadline; converted at this moment
239 // it is the same delay, whatever the wall clock does later.
240
3/6
✓ Branch 0 taken 31157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31157 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31157 times.
✗ Branch 5 not taken.
31157 EPollManager::INSTANCE.armTimer(GetTimeMS() + (next - GetMonotonicTimeMS()));
241 } else {
242 ✗ hasTimers = false;
243 ✗ EPollManager::INSTANCE.disarmTimer();
244 }
245 31157 }
246
247 87922 void Timers::fireTimer(TimerInfo* t) {
248
2/2
✓ Branch 0 taken 87807 times.
✓ Branch 1 taken 115 times.
87922 if (t->commandPreset.empty()) {
249 87807 t->callback();
250 } else {
251 115 LogDebug(VB_COMMAND, "\"Trigger Command Preset In Future\" identifier \"%s\" triggering preset \"%s\"\n", t->id.c_str(), t->commandPreset.c_str());
252
2/4
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
✗ Branch 3 not taken.
115 CommandManager::INSTANCE.TriggerPreset(t->commandPreset);
253 }
254 87922 }
255