Tactility/TactilityCore/Source/Dispatcher.cpp
Ken Van Hoeylandt ff4287e2ce
Filesystem improvements and more (#148)
- Rename `assets` and `config` partitions to `system` and `data`
- Change partition type from `spiffs` to `fat`, so we can have sub-directories
- Fix crash when doing WiFi scan: Increased system event task size to 3kB. 
- Free up IRAM on ESP32 (it was required for the Core2, but I also freed up the same amount for Yellow Board)
- Introduced `Paths` objects that can be retrieved by `AppContext` and `ServiceContext`. Apps and services now have their own relative paths. Assets were re-arranged into the correct paths.
- Rename simulator window title to "Tactility"
- Refactored statusbar widget so it persists icon paths properly (it kept a const char* reference, but didn't copy it, so it crashed when the related std::string was destroyed)
- Created `Partitions.h` to expose some useful variables
- Moved USB config in various `sdkconfig`  (it was part of the "default" section, but it shouldn't be)
- Updated domain name
2025-01-05 20:44:33 +01:00

78 lines
2.1 KiB
C++

#include <kernel/Kernel.h>
#include "Dispatcher.h"
#include "Check.h"
namespace tt {
#define TAG "dispatcher"
#define BACKPRESSURE_WARNING_COUNT 100
#define WAIT_FLAG 1
Dispatcher::Dispatcher() :
mutex(Mutex::TypeNormal)
{}
Dispatcher::~Dispatcher() {
// Wait for Mutex usage
mutex.acquire(TtWaitForever);
mutex.release();
}
void Dispatcher::dispatch(Callback callback, std::shared_ptr<void> context) {
auto message = std::make_shared<DispatcherMessage>(callback, std::move(context));
// Mutate
if (mutex.lock(1000 / portTICK_PERIOD_MS)) {
queue.push(std::move(message));
if (queue.size() == BACKPRESSURE_WARNING_COUNT) {
TT_LOG_W(TAG, "Backpressure: You're not consuming fast enough (100 queued)");
}
tt_check(mutex.unlock());
// Signal
eventFlag.set(WAIT_FLAG);
} else {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
}
}
uint32_t Dispatcher::consume(uint32_t timeout_ticks) {
// Wait for signal and clear
TickType_t start_ticks = kernel::getTicks();
if (eventFlag.wait(WAIT_FLAG, TtFlagWaitAny, timeout_ticks) == WAIT_FLAG) {
eventFlag.clear(WAIT_FLAG);
} else {
return 0;
}
TickType_t ticks_remaining = TT_MAX(timeout_ticks - (kernel::getTicks() - start_ticks), 0);
TT_LOG_I(TAG, "Dispatcher continuing (%d ticks)", (int)ticks_remaining);
// Mutate
bool processing = true;
uint32_t consumed = 0;
do {
if (mutex.lock(ticks_remaining / portTICK_PERIOD_MS)) {
if (!queue.empty()) {
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 {
processing = false;
tt_check(mutex.unlock());
}
} else {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
}
} while (processing);
return consumed;
}
} // namespace