From c46a88455878317e1232d58def6b9c915a6cc828 Mon Sep 17 00:00:00 2001 From: Kevin Hester Date: Sat, 10 Oct 2020 08:28:00 +0800 Subject: [PATCH] concurrency wip --- src/concurrency/NotifiedWorkerThread.cpp | 35 ++++++++++++++++++++++++ src/concurrency/NotifiedWorkerThread.h | 13 ++++----- src/concurrency/OSThread.cpp | 33 ++++++++++++++++++++++ src/concurrency/OSThread.h | 9 ++++++ src/main.cpp | 5 ++++ 5 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 src/concurrency/NotifiedWorkerThread.cpp diff --git a/src/concurrency/NotifiedWorkerThread.cpp b/src/concurrency/NotifiedWorkerThread.cpp new file mode 100644 index 00000000..ae9f47ab --- /dev/null +++ b/src/concurrency/NotifiedWorkerThread.cpp @@ -0,0 +1,35 @@ +#include "NotifiedWorkerThread.h" +#include + +namespace concurrency +{ + +/** + * Notify this thread so it can run + */ +IRAM_ATTR void NotifiedWorkerThread::notify(uint32_t v, bool overwrite) { + +} + +/** + * Notify from an ISR + * + * This must be inline or IRAM_ATTR on ESP32 + */ +IRAM_ATTR void NotifiedWorkerThread::notifyFromISR(BaseType_t *highPriWoken, uint32_t v, bool overwrite) +{ + notify(v, overwrite); +} + +/** + * Schedule a notification to fire in delay msecs + */ +void NotifiedWorkerThread::notifyLater(uint32_t delay, uint32_t v, bool overwrite) { + +} + +uint32_t NotifiedWorkerThread::runOnce() { + +} + +} // namespace concurrency \ No newline at end of file diff --git a/src/concurrency/NotifiedWorkerThread.h b/src/concurrency/NotifiedWorkerThread.h index 6ef11220..87f7ba83 100644 --- a/src/concurrency/NotifiedWorkerThread.h +++ b/src/concurrency/NotifiedWorkerThread.h @@ -10,6 +10,11 @@ namespace concurrency */ class NotifiedWorkerThread : public OSThread { + /** + * The notification that was most recently used to wake the thread. Read from runOnce() + */ + uint32_t notification = 0; + public: NotifiedWorkerThread(const char *name) : OSThread(name) {} @@ -23,7 +28,7 @@ class NotifiedWorkerThread : public OSThread * * This must be inline or IRAM_ATTR on ESP32 */ - void notifyFromISR(BaseType_t *highPriWoken, uint32_t v, bool overwrite) { notify(v, overwrite); } + void notifyFromISR(BaseType_t *highPriWoken, uint32_t v, bool overwrite); /** * Schedule a notification to fire in delay msecs @@ -34,12 +39,6 @@ class NotifiedWorkerThread : public OSThread virtual void onNotify(uint32_t notification) = 0; virtual uint32_t runOnce(); - - private: - /** - * The notification that was most recently used to wake the thread. Read from runOnce() - */ - uint32_t notification = 0; }; } // namespace concurrency diff --git a/src/concurrency/OSThread.cpp b/src/concurrency/OSThread.cpp index 0569567c..7efb0b27 100644 --- a/src/concurrency/OSThread.cpp +++ b/src/concurrency/OSThread.cpp @@ -4,4 +4,37 @@ namespace concurrency { +ThreadController mainController, timerController; + +void OSThread::setup() +{ + mainController.ThreadName = "mainController"; + timerController.ThreadName = "timerController"; +} + +OSThread::OSThread(const char *_name, uint32_t period, ThreadController *_controller) + : Thread(NULL, period), controller(_controller) +{ + ThreadName = _name; + + if (controller) + controller->add(this); +} + +OSThread::~OSThread() +{ + if (controller) + controller->remove(this); +} + +void OSThread::run() +{ + auto newDelay = runOnce(); + + runned(); + + if (newDelay != 0) + setInterval(newDelay); +} + } // namespace concurrency diff --git a/src/concurrency/OSThread.h b/src/concurrency/OSThread.h index 0df86a13..47bc1a54 100644 --- a/src/concurrency/OSThread.h +++ b/src/concurrency/OSThread.h @@ -25,6 +25,10 @@ extern ThreadController mainController, timerController; * stopping sleep instantly as soon as an event occurs. * use global functions delayTillWakeEvent(time), doWakeEvent(isInISR) - use freertos mutex or somesuch * + * make everything use osthread + * + * Debug what is keeping us from sleeping + * * have router thread block on its message queue in runOnce * * remove lock/lockguard @@ -38,6 +42,8 @@ class OSThread : public Thread virtual ~OSThread(); + static void setup(); + protected: /** * The method that will be called each time our thread gets a chance to run @@ -45,6 +51,9 @@ class OSThread : public Thread * Returns desired period for next invocation (or 0 for no change) */ virtual uint32_t runOnce() = 0; + + // Do not override this + virtual void run(); }; } // namespace concurrency diff --git a/src/main.cpp b/src/main.cpp index 17d55208..ba197074 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,6 +13,7 @@ // #include "debug.h" #include "RTC.h" #include "SPILock.h" +#include "concurrency/OSThread.h" #include "concurrency/Periodic.h" #include "graphics/Screen.h" #include "main.h" @@ -155,6 +156,8 @@ void setup() digitalWrite(RESET_OLED, 1); #endif + concurrency::OSThread::setup(); + #ifdef I2C_SDA Wire.begin(I2C_SDA, I2C_SCL); #else @@ -375,6 +378,8 @@ void loop() userButtonAlt.tick(); #endif + concurrency::mainController.run(); + loopWifi(); // For debugging