firmware/src/concurrency/OSThread.h

89 lines
2.4 KiB
C
Raw Normal View History

#pragma once
2020-10-09 01:10:44 +00:00
#include <cstdlib>
#include <stdint.h>
#include "Thread.h"
#include "ThreadController.h"
2020-10-10 01:57:57 +00:00
#include "concurrency/InterruptableDelay.h"
2020-09-04 22:03:22 +00:00
namespace concurrency
{
2020-07-06 19:53:10 +00:00
extern ThreadController mainController, timerController;
2020-10-10 01:57:57 +00:00
extern InterruptableDelay mainDelay;
#define RUN_SAME -1
2020-10-09 01:10:44 +00:00
/**
* @brief Base threading
*
* This is a pseudo threading layer that is super easy to port, well suited to our slow network and very ram & power efficient.
2020-10-09 01:10:44 +00:00
*
* TODO FIXME @geeksville
*
* move more things into OSThreads
2020-10-09 01:10:44 +00:00
* remove lock/lockguard
*
2020-10-10 01:57:57 +00:00
* move typedQueue into concurrency
* remove freertos from typedqueue
2020-10-09 01:10:44 +00:00
*/
class OSThread : public Thread
2020-10-09 01:10:44 +00:00
{
ThreadController *controller;
2020-10-10 01:57:57 +00:00
/// Show debugging info for disabled threads
static bool showDisabled;
/// Show debugging info for threads when we run them
static bool showRun;
/// Show debugging info for threads we decide not to run;
static bool showWaiting;
2020-10-09 01:10:44 +00:00
public:
/// For debug printing only (might be null)
static const OSThread *currentThread;
OSThread(const char *name, uint32_t period = 0, ThreadController *controller = &mainController);
2020-10-09 01:10:44 +00:00
virtual ~OSThread();
2020-10-10 01:57:57 +00:00
virtual bool shouldRun(unsigned long time);
2020-10-10 00:28:00 +00:00
static void setup();
2020-10-10 01:57:57 +00:00
/**
* Wait a specified number msecs starting from the current time (rather than the last time we were run)
*/
void setIntervalFromNow(unsigned long _interval);
2020-10-09 01:10:44 +00:00
protected:
/**
* The method that will be called each time our thread gets a chance to run
*
2020-10-10 01:57:57 +00:00
* Returns desired period for next invocation (or RUN_SAME for no change)
2020-10-09 01:10:44 +00:00
*/
2020-10-10 01:57:57 +00:00
virtual int32_t runOnce() = 0;
2020-10-10 00:28:00 +00:00
// Do not override this
virtual void run();
2020-10-09 01:10:44 +00:00
};
2020-07-06 19:53:10 +00:00
/**
* This flag is set **only** when setup() starts, to provide a way for us to check for sloppy static constructor calls.
* Call assertIsSetup() to force a crash if someone tries to create an instance too early.
*
* it is super important to never allocate those object statically. instead, you should explicitly
* new them at a point where you are guaranteed that other objects that this instance
* depends on have already been created.
*
* in particular, for OSThread that means "all instances must be declared via new() in setup() or later" -
* this makes it guaranteed that the global mainController is fully constructed first.
*/
extern bool hasBeenSetup;
void assertIsSetup();
} // namespace concurrency