Dispatcher and DispatcherThread improvements

This commit is contained in:
Ken Van Hoeylandt 2025-08-31 22:46:17 +02:00
parent f56ed2c581
commit b20dd09c9e
4 changed files with 14 additions and 7 deletions

View File

@ -38,8 +38,10 @@ public:
/**
* Queue a function to be consumed elsewhere.
* @param[in] function the function to execute elsewhere
* @param[in] timeout lock acquisition timeout
* @return true if dispatching was successful (timeout not reached)
*/
void dispatch(Function function, TickType_t timeout = portMAX_DELAY);
bool dispatch(Function function, TickType_t timeout = portMAX_DELAY);
/**
* Consume 1 or more dispatched function (if any) until the queue is empty.

View File

@ -9,7 +9,7 @@ class DispatcherThread {
Dispatcher dispatcher;
std::unique_ptr<Thread> thread;
bool interruptThread = false;
bool interruptThread = true;
int32_t threadMain();
@ -21,13 +21,16 @@ public:
/**
* Dispatch a message.
*/
void dispatch(Dispatcher::Function function, TickType_t timeout = portMAX_DELAY);
bool dispatch(Dispatcher::Function function, TickType_t timeout = portMAX_DELAY);
/** Start the thread (blocking). */
void start();
/** Stop the thread (blocking). */
void stop();
/** @return true of the thread is started */
bool isStarted() const { return thread != nullptr && !interruptThread; }
};
}

View File

@ -15,7 +15,7 @@ Dispatcher::~Dispatcher() {
mutex.unlock();
}
void Dispatcher::dispatch(Function function, TickType_t timeout) {
bool Dispatcher::dispatch(Function function, TickType_t timeout) {
// Mutate
if (mutex.lock(timeout)) {
queue.push(std::move(function));
@ -25,8 +25,10 @@ void Dispatcher::dispatch(Function function, TickType_t timeout) {
tt_check(mutex.unlock());
// Signal
eventFlag.set(WAIT_FLAG);
return true;
} else {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED);
return false;
}
}

View File

@ -6,7 +6,7 @@ DispatcherThread::DispatcherThread(const std::string& threadName, size_t threadS
thread = std::make_unique<Thread>(
threadName,
threadStackSize,
[this]() {
[this] {
return threadMain();
}
);
@ -30,8 +30,8 @@ int32_t DispatcherThread::threadMain() {
return 0;
}
void DispatcherThread::dispatch(Dispatcher::Function function, TickType_t timeout) {
dispatcher.dispatch(function, timeout);
bool DispatcherThread::dispatch(Dispatcher::Function function, TickType_t timeout) {
return dispatcher.dispatch(function, timeout);
}
void DispatcherThread::start() {