Loader refactored (#235)

- Moved all Loader functionality into Loader class
- Improvement for Dispatcher construction
- Dispatcher and DispatcherThread: you can now specify the timeout when calling `dispatch()`. Default timeout is max timeout.
This commit is contained in:
Ken Van Hoeylandt
2025-02-23 16:08:00 +01:00
committed by GitHub
parent bd2786b122
commit de46401d85
8 changed files with 203 additions and 190 deletions
+2 -2
View File
@@ -38,7 +38,7 @@ private:
};
Mutex mutex;
std::queue<std::shared_ptr<DispatcherMessage>> queue;
std::queue<std::shared_ptr<DispatcherMessage>> queue = {};
EventFlag eventFlag;
public:
@@ -51,7 +51,7 @@ public:
* @param[in] function the function to execute elsewhere
* @param[in] context the data to pass onto the function
*/
void dispatch(Function function, std::shared_ptr<void> context);
void dispatch(Function function, std::shared_ptr<void> context, TickType_t timeout = portMAX_DELAY);
/**
* Consume 1 or more dispatched function (if any) until the queue is empty.
@@ -19,7 +19,7 @@ public:
/**
* Dispatch a message.
*/
void dispatch(Dispatcher::Function function, std::shared_ptr<void> context);
void dispatch(Dispatcher::Function function, std::shared_ptr<void> context, TickType_t timeout = portMAX_DELAY);
/** Start the thread (blocking). */
void start();
+3 -1
View File
@@ -35,7 +35,9 @@ public:
PubSub() = default;
~PubSub() {
tt_check(items.empty());
if (!items.empty()) {
TT_LOG_W("Loader", "Destroying PubSub with %d active subscriptions", items.size());
}
}
/** Start receiving messages at the specified handle (Threadsafe, Re-entrable)
+2 -2
View File
@@ -15,10 +15,10 @@ Dispatcher::~Dispatcher() {
mutex.unlock();
}
void Dispatcher::dispatch(Function function, std::shared_ptr<void> context) {
void Dispatcher::dispatch(Function function, std::shared_ptr<void> context, TickType_t timeout) {
auto message = std::make_shared<DispatcherMessage>(function, std::move(context));
// Mutate
if (mutex.lock(1000 / portTICK_PERIOD_MS)) {
if (mutex.lock(timeout)) {
queue.push(std::move(message));
if (queue.size() == BACKPRESSURE_WARNING_COUNT) {
TT_LOG_W(TAG, "Backpressure: You're not consuming fast enough (100 queued)");
+7 -3
View File
@@ -25,12 +25,16 @@ DispatcherThread::~DispatcherThread() {
void DispatcherThread::_threadMain() {
do {
dispatcher.consume(1000 / portTICK_PERIOD_MS);
/**
* If this value is too high (e.g. 1 second) then the dispatcher destroys too slowly when the simulator exits.
* This causes the problems with other services doing an update (e.g. Statusbar) and calling into destroyed mutex in the global scope.
*/
dispatcher.consume(100 / portTICK_PERIOD_MS);
} while (!interruptThread);
}
void DispatcherThread::dispatch(Dispatcher::Function function, std::shared_ptr<void> context) {
dispatcher.dispatch(function, std::move(context));
void DispatcherThread::dispatch(Dispatcher::Function function, std::shared_ptr<void> context, TickType_t timeout) {
dispatcher.dispatch(function, std::move(context), timeout);
}
void DispatcherThread::start() {