158 lines
3.5 KiB
C++

#ifndef STA_RTOS_THREAD_HPP
#define STA_RTOS_THREAD_HPP
#include <sta/rtos/defs.hpp>
#include <sta/rtos/handle.hpp>
#include <cmsis_os2.h>
/**
* @ingroup STA_RTOS_API
* @{
*/
/**
* @brief Number of bits in task notification flags used internally.
*/
#define STA_RTOS_MAX_BITS_THREAD_SYS_NOTIFY 1U
/**
* @brief Number of user usable bits in task notification flags.
*/
#define STA_RTOS_MAX_BITS_THREAD_USER_NOTIFY ( STA_RTOS_MAX_BITS_THREAD_NOTIFY - STA_RTOS_MAX_BITS_THREAD_SYS_NOTIFY )
/**
* @brief Mask for valid system task notification flag bits.
*/
#define STA_RTOS_THREAD_FLAGS_VALID_SYS_BITS ( ( ( 1UL << STA_RTOS_MAX_BITS_THREAD_SYS_NOTIFY ) - 1U ) << STA_RTOS_MAX_BITS_THREAD_USER_NOTIFY )
/**
* @brief Mask for valid user task notification flag bits.
*/
#define STA_RTOS_THREAD_FLAGS_VALID_USER_BITS ( ( 1UL << STA_RTOS_MAX_BITS_THREAD_USER_NOTIFY ) - 1U )
/**
* @brief Indicate error code instead of flag values.
*/
#define STA_RTOS_THREAD_FLAG_ERROR_CODE ( 1UL << 31 )
/**
* @brief Request thread start.
*/
#define STA_RTOS_THREAD_FLAG_START ( 1UL << 30 )
#define STA_RTOS_THREAD_FLAGS_ERROR_CODE_BITS UINT32_C( 0x0000000F )
#define STA_RTOS_THREAD_FLAGS_ERROR_CODE_EXT_BITS UINT32_C( 0x7FFFFFF0 )
#define STA_RTOS_THREAD_FLAGS_GET_EXT_ERROR_CODE(flags) (~flags & STA_RTOS_THREAD_FLAGS_ERROR_CODE_EXT_BITS)
#define STA_RTOS_THREAD_FLAGS_WAIT_ANY(timeout) osThreadFlagsWait(STA_RTOS_THREAD_FLAGS_VALID_BITS, osFlagsWaitAny, timeout)
/** @} */
namespace sta
{
/**
* @brief Wrapper for RTOS thread control.
*
* @ingroup STA_RTOS_API
*/
class RtosThread
{
public:
using Handle = RtosHandle<osThreadId_t>; /**< Thread handle type */
public:
RtosThread();
/**
* @param handle Thread handle
*/
RtosThread(const Handle & handle);
/**
* @brief Sets the thread to BLOCKED for a given number of ticks.
* Other threads can run in the meantime.
*
* @param ticks The number of ticks before setting the thread to READY again.
*/
void sleep(uint32_t ticks);
/**
* @brief Send user notification flags to thread.
*
* @param flags User flags
*/
void notify(uint32_t flags);
/**
* @return Returns the flags that were set for this thread.
*/
uint32_t getFlags();
/**
* @brief Wait for certain thread flags.
*
* @return Returns the flags triggering the activation.
*/
uint32_t wait(uint32_t flags);
/**
* @brief Clears the thread flags set for this thread.
*
* @return Returns the thread flags that set before clearing.
*/
uint32_t clear(uint32_t flags);
/**
* @brief Send termination request to thread.
*/
void requestTermination();
/**
* @brief Clear the termination request flag for this thread.
*/
void deleteTerminationRequest();
/**
* @brief Resets the terminate bool to false.
*
* @return Returns the previous value of this variable.
*/
bool isTerminationRequested();
/**
* @brief Forcibly terminate thread.
*/
void terminate();
/**
* @brief Send system notification flags to thread.
*
* @param flags System flags
*/
void sysNotify(uint32_t flags);
private:
Handle handle_;
bool terminate_;
};
} // namespace sta
/**
* @brief Create RtosThread wrapper object for thread handle.
*
* @param name Thread name
*/
#define STA_RTOS_THREAD_WRAPPER(name) \
extern osThreadId_t name ## Handle; \
sta::RtosThread name ## Thread = sta::RtosThread(sta::RtosThread::Handle::Deferred(&name ## Handle));
#endif // STA_RTOS_THREAD_HPP