Skip to content

Commit 8772c31

Browse files
committed
Runtime: use lock for protect _lastTrigger
1 parent 5a7eaec commit 8772c31

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/RuntimeData.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ void RuntimeClass::loop(void)
5858

5959
/*
6060
* Writes the runtime data to LittleFS file
61-
* freezeTime: Minimum necessary time [minutes] between now and last write operation
61+
* freezeMinutes: Minimum necessary time [minutes] between now and last write operation
6262
*/
63-
bool RuntimeClass::write(uint16_t const freezeTime)
63+
bool RuntimeClass::write(uint16_t const freezeMinutes)
6464
{
6565
auto cleanExit = [this](const bool writeOk, const char* text) -> bool {
6666
if (writeOk) {
@@ -81,7 +81,7 @@ bool RuntimeClass::write(uint16_t const freezeTime)
8181
std::lock_guard<std::mutex> lock(_mutex);
8282

8383
// we do not write more than once in a hour
84-
if ((_writeEpoch != 0) && (difftime(nextEpoch, _writeEpoch) < 60 * freezeTime)) {
84+
if ((_writeEpoch != 0) && (difftime(nextEpoch, _writeEpoch) < 60 * freezeMinutes)) {
8585
return cleanExit(false, "Time interval between 2 write operations too short, skipping write");
8686
}
8787
nextCount = _writeCount + 1;
@@ -193,7 +193,8 @@ String RuntimeClass::getWriteCountAndTimeString(void) const
193193
char buf[32] = "";
194194
struct tm time;
195195

196-
// Check if time service is available before converting epoch to local time
196+
// Before we can convert the epoch to local time, we need to ensure we've received the correct time
197+
// from the time server. This may take some time after the system boots.
197198
if ((_writeEpoch != 0) && (getLocalTime(&time, 5))) {
198199
localtime_r(&_writeEpoch, &time);
199200
strftime(buf, sizeof(buf), " / %d-%h %R", &time);
@@ -207,9 +208,9 @@ String RuntimeClass::getWriteCountAndTimeString(void) const
207208

208209
/*
209210
* Returns true at the daily trigger time at 00:05
210-
* Note: This function is not protected by a mutex, but if it is only called by loop() and loop() is only executed on a single thread, we are safe
211211
*/
212212
bool RuntimeClass::getWriteTrigger(void) {
213+
std::lock_guard<std::mutex> lock(_mutex);
213214
struct tm actTime;
214215
if (getLocalTime(&actTime, 5)) {
215216
if ((actTime.tm_hour == 0) && (actTime.tm_min >= 5) && (actTime.tm_min <= 10)) {

0 commit comments

Comments
 (0)