M module-sys/Service/Timer.cpp => module-sys/Service/Timer.cpp +6 -3
@@ 1,4 1,4 @@
-// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
+// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#include "Timer.hpp"
@@ 67,6 67,7 @@ namespace sys
void Timer::start()
{
log_timers("Timer %s start!", name.c_str());
+ isActive = true;
Start(0);
}
@@ 78,13 79,15 @@ namespace sys
SetPeriod(pdMS_TO_TICKS(interval));
}
log_timers("Timer %s reload!", name.c_str());
+ isActive = true;
Start(0); // start with no waittime
}
void Timer::stop()
{
log_timers("Timer %s stop!", name.c_str());
- callback = nullptr;
+ // make sure callback is not called even if it is already in the queue
+ isActive = false;
Stop(0);
}
@@ 98,7 101,7 @@ namespace sys
void Timer::onTimeout()
{
log_timers("Timer %s tick", name.c_str());
- if (callback != nullptr) {
+ if (callback != nullptr && isActive) {
log_timers("Timer %s callback run!", name.c_str());
callback(*this);
}
M module-sys/Service/Timer.hpp => module-sys/Service/Timer.hpp +6 -4
@@ 1,13 1,14 @@
-// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
+// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md
#pragma once
#include "FreeRTOS.h"
-#include "portmacro.h" // for TickType_t
+#include "portmacro.h" // for TickType_t
#include <module-os/RTOSWrapper/include/timer.hpp> // for Timer
-#include <functional> // for function
-#include <string> // for string
+#include <functional> // for function
+#include <string> // for string
+#include <atomic>
namespace sys
{
class Service;
@@ 69,6 70,7 @@ namespace sys
Type type;
ms interval;
std::function<void(Timer &)> callback = nullptr;
+ std::atomic_bool isActive = false;
const std::string name;
public: