Merge develop into main branch (#137)
* SdCard HAL refactored (#135) - Refactor SdCard HAL - introduce Lockable * Screenshot and FatFS improvements (#136) - Fix screenshots on ESP32 - Improve Screenshot service - Convert Screenshot app to class-based instead of structs - Screenshot app now automatically updates when task is finished - Enable FatFS long filename support * Re-use common log messages (#138) For consistency and binary size reduction * Toolbar spinner should get margin to the right * More TactilityC features (#139) * Rewrote Loader - Simplified Loader by removing custom threa - Created DispatcherThread - Move auto-starting apps to Boot app - Fixed Dispatcher bug where it could get stuck not processing new messages * Hide AP settings if the AP is not saved * Missing from previous commit * Replace LV_EVENT_CLICKED with LV_EVENT_SHORT_CLICKED * Refactored files app and created InputDialog (#140) - Changed Files app so that it has a View and State - Files app now allows for long-pressing on files to perform actions - Files app now has rename and delete actions - Created InputDialog app - Improved AlertDialog app layout
This commit is contained in:
committed by
GitHub
parent
9033daa6dd
commit
50bd6e8bf6
@@ -1,3 +1,4 @@
|
||||
#include <kernel/Kernel.h>
|
||||
#include "Dispatcher.h"
|
||||
#include "Check.h"
|
||||
|
||||
@@ -19,32 +20,59 @@ Dispatcher::~Dispatcher() {
|
||||
void Dispatcher::dispatch(Callback callback, std::shared_ptr<void> context) {
|
||||
auto message = std::make_shared<DispatcherMessage>(callback, std::move(context));
|
||||
// Mutate
|
||||
mutex.acquire(TtWaitForever);
|
||||
queue.push(std::move(message));
|
||||
if (queue.size() == BACKPRESSURE_WARNING_COUNT) {
|
||||
TT_LOG_W(TAG, "Backpressure: You're not consuming fast enough (100 queued)");
|
||||
if (mutex.lock(1000 / portTICK_PERIOD_MS)) {
|
||||
queue.push(std::move(message));
|
||||
TT_LOG_I(TAG, "dispatch");
|
||||
if (queue.size() == BACKPRESSURE_WARNING_COUNT) {
|
||||
TT_LOG_W(TAG, "Backpressure: You're not consuming fast enough (100 queued)");
|
||||
}
|
||||
mutex.unlock();
|
||||
// Signal
|
||||
eventFlag.set(1);
|
||||
} else {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
|
||||
}
|
||||
mutex.release();
|
||||
// Signal
|
||||
eventFlag.set(1);
|
||||
}
|
||||
|
||||
uint32_t Dispatcher::consume(uint32_t timeout_ticks) {
|
||||
// Wait for signal and clear
|
||||
eventFlag.wait(1, TtFlagWaitAny, timeout_ticks);
|
||||
eventFlag.clear(1);
|
||||
|
||||
// Mutate
|
||||
if (mutex.acquire(1 / portTICK_PERIOD_MS) == TtStatusOk) {
|
||||
auto item = queue.front();
|
||||
queue.pop();
|
||||
// Don't keep lock as callback might be slow
|
||||
tt_check(mutex.release() == TtStatusOk);
|
||||
|
||||
item->callback(item->context);
|
||||
TickType_t start_ticks = kernel::getTicks();
|
||||
if (eventFlag.wait(1, TtFlagWaitAny, timeout_ticks) == TtStatusErrorTimeout) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return true;
|
||||
TickType_t ticks_remaining = TT_MAX(timeout_ticks - (kernel::getTicks() - start_ticks), 0);
|
||||
|
||||
eventFlag.clear(1);
|
||||
|
||||
TT_LOG_I(TAG, "Dispatcher continuing");
|
||||
|
||||
// Mutate
|
||||
bool processing = true;
|
||||
uint32_t consumed = 0;
|
||||
do {
|
||||
if (mutex.lock(ticks_remaining / portTICK_PERIOD_MS)) {
|
||||
if (!queue.empty()) {
|
||||
TT_LOG_I(TAG, "Dispatcher popping from queue");
|
||||
auto item = queue.front();
|
||||
queue.pop();
|
||||
consumed++;
|
||||
processing = !queue.empty();
|
||||
// Don't keep lock as callback might be slow
|
||||
|
||||
tt_check(mutex.unlock());
|
||||
item->callback(item->context);
|
||||
} else {
|
||||
tt_check(mutex.unlock());
|
||||
}
|
||||
|
||||
} else {
|
||||
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
|
||||
}
|
||||
|
||||
} while (processing);
|
||||
|
||||
return consumed;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user