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. * Queue a function to be consumed elsewhere.
* @param[in] function the function to execute 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. * Consume 1 or more dispatched function (if any) until the queue is empty.

View File

@ -9,7 +9,7 @@ class DispatcherThread {
Dispatcher dispatcher; Dispatcher dispatcher;
std::unique_ptr<Thread> thread; std::unique_ptr<Thread> thread;
bool interruptThread = false; bool interruptThread = true;
int32_t threadMain(); int32_t threadMain();
@ -21,13 +21,16 @@ public:
/** /**
* Dispatch a message. * 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). */ /** Start the thread (blocking). */
void start(); void start();
/** Stop the thread (blocking). */ /** Stop the thread (blocking). */
void stop(); 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(); mutex.unlock();
} }
void Dispatcher::dispatch(Function function, TickType_t timeout) { bool Dispatcher::dispatch(Function function, TickType_t timeout) {
// Mutate // Mutate
if (mutex.lock(timeout)) { if (mutex.lock(timeout)) {
queue.push(std::move(function)); queue.push(std::move(function));
@ -25,8 +25,10 @@ void Dispatcher::dispatch(Function function, TickType_t timeout) {
tt_check(mutex.unlock()); tt_check(mutex.unlock());
// Signal // Signal
eventFlag.set(WAIT_FLAG); eventFlag.set(WAIT_FLAG);
return true;
} else { } else {
TT_LOG_E(TAG, LOG_MESSAGE_MUTEX_LOCK_FAILED); 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>( thread = std::make_unique<Thread>(
threadName, threadName,
threadStackSize, threadStackSize,
[this]() { [this] {
return threadMain(); return threadMain();
} }
); );
@ -30,8 +30,8 @@ int32_t DispatcherThread::threadMain() {
return 0; return 0;
} }
void DispatcherThread::dispatch(Dispatcher::Function function, TickType_t timeout) { bool DispatcherThread::dispatch(Dispatcher::Function function, TickType_t timeout) {
dispatcher.dispatch(function, timeout); return dispatcher.dispatch(function, timeout);
} }
void DispatcherThread::start() { void DispatcherThread::start() {