简介
关键结构
信号量就是说白了就是一个计数器,发送一次tokens就加1,wait一次tokens就减1,如果wait时tokens为0,那就阻塞等待:
typedef struct OS_SCB {
U8 cb_type; /* Control Block Type 固定为SCB */
U8 mask; /* Semaphore token mask 没用到 */
U16 tokens; /* Semaphore tokens 令牌数*/
struct OS_TCB *p_lnk; /* Chain of tasks waiting for tokens 被该信号量阻塞的任务链表*/
} *P_SCB;
关键实现
rt_sem_init
在创建一个信号量之前,必须先初始化一下,设置控制块类型和令牌数量:
void rt_sem_init (OS_ID semaphore, U16 token_count) {
/* Initialize a semaphore */
P_SCB p_SCB = semaphore;
p_SCB->cb_type = SCB;
p_SCB->p_lnk = NULL;
p_SCB->tokens = token_count;//初始化令牌的数量
}
rt_sem_delete
信号量删除后,必须把等待该信号量所有的任务都加入就绪任务链表:
OS_RESULT rt_sem_delete (OS_ID semaphore) {
/* Delete semaphore */
P_SCB p_SCB = semaphore;
P_TCB p_TCB;
while (p_SCB->p_lnk != NULL) {//循环判断该信号量是否有任务等待
/* A task is waiting for token */
p_TCB = rt_get_first ((P_XCB)p_SCB);
rt_ret_val(p_TCB, 0);
rt_rmv_dly(p_TCB);
p_TCB->state = READY;
rt_put_prio (&os_rdy, p_TCB);//加入就绪任务链表
}
if (os_rdy.p_lnk && (os_rdy.p_lnk->prio > os_tsk.run->prio)) {//如果有更高优先级的任务进入就绪状态,就必须申请调度一次。
/* preempt running task */
rt_put_prio (&os_rdy, os_tsk.run);
os_tsk.run->state = READY;
rt_dispatch (NULL);
}
p_SCB->cb_type = 0;
return (OS_R_OK);
}
rt_sem_send
只要发送一次信号量,就能引起一次任务调度:
OS_RESULT rt_sem_send (OS_ID semaphore) {
/* Return a token to semaphore */
P_SCB p_SCB = semaphore;
P_TCB p_TCB;
if (p_SCB->p_lnk != NULL) {//当前有任务在等待该信号量
/* A task is waiting for token */
p_TCB = rt_get_first ((P_XCB)p_SCB);
#ifdef __CMSIS_RTOS
rt_ret_val(p_TCB, 1);//设置等待信号量任务的返回值为1
#else
rt_ret_val(p_TCB, OS_R_SEM);
#endif
rt_rmv_dly (p_TCB);//从延时任务链表移除该任务
rt_dispatch (p_TCB);//申请一次调度
}
else {
/* Store token. */
p_SCB->tokens++;
}
return (OS_R_OK);
}
rt_sem_wait
信号量等待非常直白,有令牌我就直接返回,没有的话超时为0我也直接返回,如果都不是,那就老老实实等待:
OS_RESULT rt_sem_wait (OS_ID semaphore, U16 timeout) {
/* Obtain a token; possibly wait for it */
P_SCB p_SCB = semaphore;
if (p_SCB->tokens) {//当前令牌足够,直接拿一个返回
p_SCB->tokens--;
return (OS_R_OK);
}
/* No token available: wait for one */
if (timeout == 0) {//不想等待,那就直接返回,只不过返回值是OS_R_TMO
return (OS_R_TMO);
}
if (p_SCB->p_lnk != NULL) {//如果有人也在等待这个信号量,那就排队
rt_put_prio ((P_XCB)p_SCB, os_tsk.run);
}
else {//没人等待,那我就是排头兵
p_SCB->p_lnk = os_tsk.run;
os_tsk.run->p_lnk = NULL;
os_tsk.run->p_rlnk = (P_TCB)p_SCB;
}
rt_block(timeout, WAIT_SEM);//安安静静的等待
return (OS_R_TMO);
}
isr_sem_send
和事件一样,在中断中,都是先放进队列,然后在PendSV_Handler中断服务程序中执行:
void isr_sem_send (OS_ID semaphore) {
/* Same function as "os_sem_send", but to be called by ISRs */
P_SCB p_SCB = semaphore;
rt_psq_enq (p_SCB, 0);
rt_psh_req ();
}