Tactility/TactilityCore/Source/Dispatcher.cpp
Ken Van Hoeylandt c87200a80d
Project restructuring (fixes macOS builds) (#198)
- Create `Include/` folder for all main projects
- Fix some issues here and there (found while moving things)
- All includes are now in `Tactility/` subfolder and must be included with that prefix. This fixes issues with clashing POSIX headers (e.g. `<semaphore.h>` versus Tactility's `Semaphore.h`)
2025-02-01 18:13:20 +01:00

70 lines
1.8 KiB
C++

#include "Tactility/Dispatcher.h"
#include "Tactility/Check.h"
#include "Tactility/kernel/Kernel.h"
namespace tt {
#define TAG "dispatcher"
#define BACKPRESSURE_WARNING_COUNT ((EventBits_t)100)
#define WAIT_FLAG ((EventBits_t)1U)
Dispatcher::~Dispatcher() {
// Wait for Mutex usage
mutex.lock();
mutex.unlock();
}
void Dispatcher::dispatch(Function function, std::shared_ptr<void> context) {
auto message = std::make_shared<DispatcherMessage>(function, 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(TickType_t timeout) {
// Wait for signal
uint32_t result = eventFlag.wait(WAIT_FLAG, EventFlag::WaitAny, timeout);
if (result & EventFlag::Error) {
return 0;
}
eventFlag.clear(WAIT_FLAG);
// Mutate
bool processing = true;
uint32_t consumed = 0;
do {
if (mutex.lock(10)) {
if (!queue.empty()) {
auto item = queue.front();
queue.pop();
consumed++;
processing = !queue.empty();
// Don't keep lock as callback might be slow
mutex.unlock();
item->function(item->context);
} else {
processing = false;
mutex.unlock();
}
} else {
TT_LOG_W(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
}
} while (processing);
return consumed;
}
} // namespace