firmware/src/concurrency/NotifiedWorkerThread.cpp

93 lines
2.0 KiB
C++
Raw Normal View History

2020-10-10 01:57:57 +00:00
#include "configuration.h"
#include "NotifiedWorkerThread.h"
#include "main.h"
2020-10-10 00:28:00 +00:00
namespace concurrency
{
2020-10-10 01:57:57 +00:00
static bool debugNotification;
2020-10-10 00:28:00 +00:00
/**
* Notify this thread so it can run
*/
2020-10-10 01:57:57 +00:00
bool NotifiedWorkerThread::notify(uint32_t v, bool overwrite)
{
bool r = notifyCommon(v, overwrite);
if (r)
mainDelay.interrupt();
return r;
}
/**
* Notify this thread so it can run
*/
IRAM_ATTR bool NotifiedWorkerThread::notifyCommon(uint32_t v, bool overwrite)
{
if (overwrite || notification == 0) {
enabled = true;
setInterval(0); // Run ASAP
runASAP = true;
2020-10-10 01:57:57 +00:00
notification = v;
if (debugNotification)
2022-12-30 02:41:37 +00:00
LOG_DEBUG("setting notification %d\n", v);
2020-10-10 01:57:57 +00:00
return true;
} else {
if (debugNotification)
2022-12-30 02:41:37 +00:00
LOG_DEBUG("dropping notification %d\n", v);
2020-10-10 01:57:57 +00:00
return false;
}
2020-10-10 00:28:00 +00:00
}
/**
* Notify from an ISR
*
* This must be inline or IRAM_ATTR on ESP32
*/
2020-10-10 01:57:57 +00:00
IRAM_ATTR bool NotifiedWorkerThread::notifyFromISR(BaseType_t *highPriWoken, uint32_t v, bool overwrite)
2020-10-10 00:28:00 +00:00
{
2020-10-10 01:57:57 +00:00
bool r = notifyCommon(v, overwrite);
if (r)
mainDelay.interruptFromISR(highPriWoken);
return r;
2020-10-10 00:28:00 +00:00
}
/**
* Schedule a notification to fire in delay msecs
*/
2020-10-10 01:57:57 +00:00
bool NotifiedWorkerThread::notifyLater(uint32_t delay, uint32_t v, bool overwrite)
{
bool didIt = notify(v, overwrite);
if (didIt) { // If we didn't already have something queued, override the delay to be larger
setIntervalFromNow(delay); // a new version of setInterval relative to the current time
if (debugNotification)
2022-12-30 02:41:37 +00:00
LOG_DEBUG("delaying notification %u\n", delay);
2020-10-10 01:57:57 +00:00
}
2020-10-10 00:28:00 +00:00
2020-10-10 01:57:57 +00:00
return didIt;
2020-10-10 00:28:00 +00:00
}
void NotifiedWorkerThread::checkNotification()
2020-10-10 01:57:57 +00:00
{
auto n = notification;
notification = 0; // clear notification
if (n) {
onNotify(n);
}
}
int32_t NotifiedWorkerThread::runOnce()
{
enabled = false; // Only run once per notification
checkNotification();
2020-10-10 00:28:00 +00:00
2020-10-10 01:57:57 +00:00
return RUN_SAME;
2020-10-10 00:28:00 +00:00
}
} // namespace concurrency