2020-07-05 23:11:40 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2020-10-09 14:16:51 +08:00
|
|
|
#include "OSThread.h"
|
2020-07-05 23:11:40 +02:00
|
|
|
|
2020-10-09 14:16:51 +08:00
|
|
|
namespace concurrency
|
|
|
|
|
{
|
2020-10-09 09:10:44 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief A worker thread that waits on a freertos notification
|
|
|
|
|
*/
|
2020-10-09 14:16:51 +08:00
|
|
|
class NotifiedWorkerThread : public OSThread
|
2020-07-05 23:11:40 +02:00
|
|
|
{
|
2020-10-10 08:28:00 +08:00
|
|
|
/**
|
|
|
|
|
* The notification that was most recently used to wake the thread. Read from runOnce()
|
|
|
|
|
*/
|
|
|
|
|
uint32_t notification = 0;
|
|
|
|
|
|
2020-10-09 09:10:44 +08:00
|
|
|
public:
|
2020-10-09 14:16:51 +08:00
|
|
|
NotifiedWorkerThread(const char *name) : OSThread(name) {}
|
|
|
|
|
|
2020-10-09 09:10:44 +08:00
|
|
|
/**
|
|
|
|
|
* Notify this thread so it can run
|
|
|
|
|
*/
|
2020-10-10 09:57:57 +08:00
|
|
|
bool notify(uint32_t v, bool overwrite);
|
2020-07-05 23:11:40 +02:00
|
|
|
|
2020-10-09 09:10:44 +08:00
|
|
|
/**
|
|
|
|
|
* Notify from an ISR
|
|
|
|
|
*
|
|
|
|
|
* This must be inline or IRAM_ATTR on ESP32
|
|
|
|
|
*/
|
2020-10-10 09:57:57 +08:00
|
|
|
bool notifyFromISR(BaseType_t *highPriWoken, uint32_t v, bool overwrite);
|
2020-10-09 14:16:51 +08:00
|
|
|
|
2020-10-09 09:10:44 +08:00
|
|
|
/**
|
2020-10-09 14:16:51 +08:00
|
|
|
* Schedule a notification to fire in delay msecs
|
2020-10-09 09:10:44 +08:00
|
|
|
*/
|
2020-10-10 09:57:57 +08:00
|
|
|
bool notifyLater(uint32_t delay, uint32_t v, bool overwrite);
|
2020-07-05 23:11:40 +02:00
|
|
|
|
2020-10-09 14:16:51 +08:00
|
|
|
protected:
|
|
|
|
|
virtual void onNotify(uint32_t notification) = 0;
|
|
|
|
|
|
2020-10-10 09:57:57 +08:00
|
|
|
virtual int32_t runOnce();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
/**
|
|
|
|
|
* Notify this thread so it can run
|
|
|
|
|
*/
|
|
|
|
|
bool notifyCommon(uint32_t v, bool overwrite);
|
2020-10-09 09:10:44 +08:00
|
|
|
};
|
2020-07-05 23:11:40 +02:00
|
|
|
|
|
|
|
|
} // namespace concurrency
|