diff --git a/include/sta/rtos/thread.hpp b/include/sta/rtos/thread.hpp index 76a4bd1..713cf0b 100644 --- a/include/sta/rtos/thread.hpp +++ b/include/sta/rtos/thread.hpp @@ -14,7 +14,7 @@ /** * @brief Number of bits in task notification flags used internally. */ -#define STA_RTOS_MAX_BITS_THREAD_SYS_NOTIFY 1U +#define STA_RTOS_MAX_BITS_THREAD_SYS_NOTIFY 2U /** * @brief Number of user usable bits in task notification flags. */ @@ -91,10 +91,23 @@ namespace sta */ uint32_t getFlags(); + /** + * @brief Wait for certain thread flags. + * + * @return Returns the flags triggering the activation. + */ + uint32_t wait(uint32_t flags); + /** * @brief Send termination request to thread. */ void requestTermination(); + + /** + * @brief Returns true if this thread was requested to terminate. + */ + bool checkTerminationRequest(); + /** * @brief Forcibly terminate thread. */ @@ -109,6 +122,7 @@ namespace sta private: Handle handle_; + bool terminate_; }; } // namespace sta diff --git a/src/thread.cpp b/src/thread.cpp index 0694858..4c54e92 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -29,7 +29,16 @@ namespace sta STA_ASSERT(handle_.get() != nullptr); STA_ASSERT_MSG(IS_THREAD_SYS_FLAGS(flags), "Only system flags allowed"); - osThreadFlagsSet(handle_.get(), flags); + uint32_t rslt = osThreadFlagsSet(handle_.get(), flags); + + STA_ASSERT_MSG(rslt != osFlagsErrorUnknown, "Unknown thread flag error."); + STA_ASSERT_MSG(rslt != osFlagsErrorParameter, "Parameter thread_id is not a valid thread or flags has highest bit set."); + STA_ASSERT_MSG(rslt != osFlagsErrorResource, "The thread is in invalid state."); + } + + uint32_t RtosThread::wait(uint32_t flags) + { + return osThreadFlagsWait(flags, osFlagsWaitAny, osWaitForever); } uint32_t RtosThread::getFlags() @@ -41,7 +50,16 @@ namespace sta void RtosThread::requestTermination() { - sysNotify(STA_RTOS_THREAD_FLAG_TERMINATE); + // sysNotify(STA_RTOS_THREAD_FLAG_TERMINATE); + terminate_ = true; + } + + bool RtosThread::checkTerminationRequest() + { + bool temp = terminate_; + terminate_ = false; + + return temp; } void RtosThread::terminate()