QP/C学习笔记(五) QActive实现原理
本文主要讲解QActive是怎么实现的,在基于RTOS移植QP/C时,到底依赖了哪些RTOS资源,这些资源在QP/C中起到的作用又是什么。
QP/C QActive数据结构
typedef struct QActive {
QAsm super; //!< @protected @memberof QActive
uint8_t prio; //!< @protected @memberof QActive
uint8_t pthre; //!< @protected @memberof QActive
#ifdef QACTIVE_THREAD_TYPE
QACTIVE_THREAD_TYPE thread; //!< @protected @memberof QActive
#endif // def QACTIVE_THREAD_TYPE
#ifdef QACTIVE_OS_OBJ_TYPE
QACTIVE_OS_OBJ_TYPE osObject; //!< @protected @memberof QActive
#endif // def QACTIVE_OS_OBJ_TYPE
#ifdef QACTIVE_EQUEUE_TYPE
QACTIVE_EQUEUE_TYPE eQueue; //!< @protected @memberof QActive
#endif // def QACTIVE_EQUEUE_TYPE
} QActive;
QActive的数据结构本身是一种面向对象的C编程,
QAsm 是QActive的基类,
super,本身存贮相应的函数指针。
prio,存储的是QActive依赖线程的优先级,不可重复,用于在QActive订阅事件时起到作用
thread,保存QActive对应的线程
eQueue,保存事件机制依赖的队列指针
QActive运行机制相关实现
基于RTOS为底层实现的QP/C,其中Active本质的实现如下,Active部分接口依赖的函数如下:
task_function
task_function
是状态运行依赖的系统资源,在RTOS背景下是个Task,在Posix背景下是thread,当然两者只是叫法不同,本质都是线程,也就是说一个状态机的所有状态,本质上都是运行在同一个线程上的,QP/C提供了一套机制去切换状态。
static void task_function(void *pvParameters) {
// FreeRTOS task signature
QActive *act = (QActive *)pvParameters;
#ifdef QACTIVE_CAN_STOP
while (act->eQueue != (QueueHandle_t)0)
#else
for (;;