diff --git a/TactilityCore/Include/Tactility/Dispatcher.h b/TactilityCore/Include/Tactility/Dispatcher.h index f8462aed..fb29796c 100644 --- a/TactilityCore/Include/Tactility/Dispatcher.h +++ b/TactilityCore/Include/Tactility/Dispatcher.h @@ -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. diff --git a/TactilityCore/Include/Tactility/DispatcherThread.h b/TactilityCore/Include/Tactility/DispatcherThread.h index 7683aab7..eaaf8417 100644 --- a/TactilityCore/Include/Tactility/DispatcherThread.h +++ b/TactilityCore/Include/Tactility/DispatcherThread.h @@ -9,7 +9,7 @@ class DispatcherThread { Dispatcher dispatcher; std::unique_ptr 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; } }; } \ No newline at end of file diff --git a/TactilityCore/Source/Dispatcher.cpp b/TactilityCore/Source/Dispatcher.cpp index 756e7d1f..0836602e 100644 --- a/TactilityCore/Source/Dispatcher.cpp +++ b/TactilityCore/Source/Dispatcher.cpp @@ -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; } } diff --git a/TactilityCore/Source/DispatcherThread.cpp b/TactilityCore/Source/DispatcherThread.cpp index f065bdf3..320023cb 100644 --- a/TactilityCore/Source/DispatcherThread.cpp +++ b/TactilityCore/Source/DispatcherThread.cpp @@ -6,7 +6,7 @@ DispatcherThread::DispatcherThread(const std::string& threadName, size_t threadS thread = std::make_unique( 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() {