📄 nqueue.c
字号:
INT8U status,oldstate;
if (queue_ptr == NULL) { /* Not allowed to delete idle Queue */
return (ERR_INVALID_QUEUE);
}
if (message == NULL) { /* Not allowed to delete idle Queue */
return (ERR_INVALID_POINTER);
}
if (queue_ptr->type_id!=QUEUE) return(ERR_INVALID_QUEUE);
#ifdef OPTION_ENABLE_STACK_CHECK
Check_Stack();
#endif
#ifdef OPTION_ENABLE_HISTORY
MakeHistoryLog(QueueBroadcast,Queue_ptr->TASK_name);//在日志中记录操作,时间等信息
#endif
DoProtect(queue_ptr);
if (queue_ptr->available==NULL && queue_ptr->task_wait_header){//有任务在等待接收,消息并且放入队列中的消息域,恢复任务,
// Queue_ptr->message_present=TRUE;
linknode=queue_ptr->task_wait_header;
do{
task_ptr=linknode->suspended_task;
linknode->message_area=message;
status=_ResumeTask(task_ptr,QUEUE_SUSPEND);
if (status==TRUE) oldstate =status;
linknode=(QUEUE_WAIT_QUEUE *)(linknode->next);
}while(linknode!=queue_ptr->task_wait_header);
DoUnProtect(queue_ptr);
if(oldstate==TRUE) _ControlToSystem();
return (SUCCESS);
}
else if(queue_ptr->available+size==queue_ptr->queue_size){ //队列满
if(suspend!=NO_SUSPEND){//任务挂起
if(((TASK_TCB *)CurrentThread)->type_id!=TASK){
DoUnProtect(queue_ptr);
return(ERR_INVALID_SUSPEND);
}
task_suspend.suspended_task=(TASK_TCB *)CurrentThread;
if(queue_ptr->suspend_type==FIFO)
Place_On_List(&(LIST_NODE *)(queue_ptr->task_wait_header),(LIST_NODE *)&(task_suspend));
else{
task_suspend.priority=GetPriority((TASK_TCB *)(CurrentThread));
Priority_Place_On_List(&(LIST_NODE *)(queue_ptr->task_wait_header),(LIST_NODE *)&(task_suspend));
}
DoUnProtect(queue_ptr);
_SuspendTask(CurrentThread,QUEUE_SUSPEND,QueueCleanup,queue_ptr,suspend);
if(queue_ptr->type_id==0){
return(ERR_QUEUE_DELETED);
}
DoProtect(queue_ptr);
if (queue_ptr->available==queue_ptr->queue_size)
{
DoUnProtect(queue_ptr);
return(ERR_TIMEOUT);
}
if(queue_ptr->task_wait_header==NULL){
DoUnProtect(queue_ptr);
return(ERR_QUEUE_RESET);
}
//队列消息被另一个任务接收导致队列为空,该任务放消息到队列
*(queue_ptr->write_pointer)=*message;
if(queue_ptr->write_pointer==queue_ptr->queue_end)
queue_ptr->write_pointer=queue_ptr->queue_start;
else (queue_ptr->write_pointer)++;
queue_ptr->available++;
Remove_From_List(&(LIST_NODE *)(queue_ptr->task_wait_header),(LIST_NODE *)&(task_suspend));
DoUnProtect(queue_ptr);
return (SUCCESS);
}
else{
DoUnProtect(queue_ptr);
return (ERR_QUEUE_FULL);
}
}
else{//队列不满,消息放入队列
*(queue_ptr->write_pointer)=*message;
if(queue_ptr->write_pointer==queue_ptr->queue_end)
queue_ptr->write_pointer=queue_ptr->queue_start;
else (queue_ptr->write_pointer)++;
queue_ptr->available++;
DoUnProtect(queue_ptr);
return (SUCCESS);
}
}
/*
*********************************************************************************************************
* RECEIVE A MESSAGE FROM A Queue
Functions Called
CSC_Place_On_List
CSC_Priority_Place_On_List
CSC_Remove_From_List
[HIC_Make_History_Entry]
TCC_Resume_Queue
TCC_Suspend_Queue
*********************************************************************************************************
*/
INT8U ReceiveFromQueue(QUEUE_QCB *queue_ptr,QUEUE_MSG *message,INT32S suspend)
{
QUEUE_WAIT_QUEUE task_suspend;
TASK_TCB *task_ptr;
INT8U status;
if (queue_ptr == NULL) { /* Not allowed to delete idle Queue */
return (ERR_INVALID_QUEUE);
}
if (queue_ptr->type_id!=QUEUE) {
return (ERR_INVALID_QUEUE);
}
if ( message=NULL ) return ERR_INVALID_POINTER;
#ifdef OPTION_ENABLE_STACK_CHECK
Check_Stack();
#endif
#ifdef OPTION_ENABLE_HISTORY
MakeHistoryLog(DELETE_TASK,Queue_ptr->TASK_name);//在日志中记录操作,时间等信息
#endif
DoProtect(queue_ptr);
if(queue_ptr->available==0){ //队列空
if(suspend!=NO_SUSPEND){//任务挂起
if (((TASK_TCB *)CurrentThread)->type_id!=TASK)
{
DoUnProtect(queue_ptr);
return (ERR_INVALID_SUSPEND);
}
task_suspend.suspended_task=(TASK_TCB *)CurrentThread;
if(queue_ptr->suspend_type==FIFO)
Place_On_List(&(LIST_NODE *)(queue_ptr->task_wait_header),(LIST_NODE *)&(task_suspend));
else{
task_suspend.priority=GetPriority((TASK_TCB *)CurrentThread);
Priority_Place_On_List(&(LIST_NODE *)(queue_ptr->task_wait_header),(LIST_NODE *)&(task_suspend));
}
DoUnProtect(queue_ptr);
_SuspendTask(CurrentThread,QUEUE_SUSPEND,QueueCleanup,queue_ptr,suspend);
if(queue_ptr->type_id==NULL) {
return (ERR_QUEUE_DELETED);
}
DoProtect(queue_ptr);
if((queue_ptr->task_wait_header)==NULL) {
DoUnProtect(queue_ptr);
return (ERR_QUEUE_RESET);
}
if (task_suspend.message_area==NULL) {
DoUnProtect(queue_ptr);
return (ERR_TIMEOUT);
}
//接收到消息,移去挂起结构
message=task_suspend.message_area;
Remove_From_List(&(LIST_NODE *)(queue_ptr->task_wait_header),(LIST_NODE *)&(task_suspend));
DoUnProtect(queue_ptr);
return (SUCCESS);
}
else{
DoUnProtect(queue_ptr);
return (ERR_QUEUE_EMPTY);
}
}
else{ ////队列中有消息,取得消息
queue_ptr->available--;
if(queue_ptr->read_pointer==queue_ptr->queue_end)
queue_ptr->read_pointer=queue_ptr->queue_start;
else (queue_ptr->read_pointer)++;
*message=*(queue_ptr->read_pointer);
if(queue_ptr->task_wait_header){ //这时的等待的任务肯定是要发送的任务,判断空间是否足够若够则恢复该任务
if (queue_ptr->available<queue_ptr->queue_size){
task_ptr=(queue_ptr->task_wait_header)->suspended_task;
DoUnProtect(queue_ptr);
status=_ResumeTask(task_ptr,QUEUE_SUSPEND);
if (status==TRUE) _ControlToSystem();
}
}
DoUnProtect(queue_ptr);
return (SUCCESS);
}
}
/*
*********************************************************************************************************
* REMOVE A suspension BLOCK FROM A Queue
CSC_Remove_From_List
*********************************************************************************************************
*/
static VOID QueueCleanup(VOID *information)
{
QUEUE_QCB *queue_ptr;
TASK_TCB *task_ptr;
QUEUE_WAIT_QUEUE *linknode;
queue_ptr=(QUEUE_QCB *)information;
DoUnProtect(queue_ptr);
DisableInt();
linknode=queue_ptr->task_wait_header;
do{
task_ptr=linknode->suspended_task;
if (task_ptr==CurrentThread)
Remove_From_List(&(LIST_NODE *)(queue_ptr->task_wait_header),(LIST_NODE *)linknode);
linknode=(QUEUE_WAIT_QUEUE *)(linknode->next);
if (linknode==NULL) break;
}while(linknode!=queue_ptr->task_wait_header);
EnableInt();
}
#ifdef CPU32
/*
*********************************************************************************************************
* Returns the current number of established Queuees.
Functions Called
[TCT_Check_Stack]
*********************************************************************************************************
*/
INT32U GetTotalQueues(VOID)
{
#ifdef OPTION_ENABLE_STACK_CHECK
Check_Stack();
#endif
return(TotalQueues); /* Increment the #pools counter */
}
/*
*********************************************************************************************************
* Returns information about the specified Queue.
Functions Called
[TCT_Check_Stack]
TCT_System_Protect
TCT_Unprotect
*********************************************************************************************************
*/
INT8U GetQueueInfo(QUEUE_QCB *queue_ptr,INT8U *ID,UNSIGNED *available,TASK_TCB **first_task)
{
if (queue_ptr == NULL) { /* Not allowed to delete idle task */
return (ERR_INVALID_QUEUE);
}
#ifdef OPTION_ENABLE_STACK_CHECK
Check_Stack();
#endif
DoProtect(queue_ptr);
*ID=queue_ptr->id;
*available=queue_ptr->available;
*first_task=(queue_ptr->task_wait_header)->suspended_task;
DoUnProtect(queue_ptr);
return (SUCCESS);
}
#endif
/*
*********************************************************************************************************
* This function initializes the data structures OF Queue component
*********************************************************************************************************
*/
VOID QueueInit(VOID)
{
CreatedQueuesList=NULL;
TotalQueues=0;
}
/*
*********************************************************************************************************
* This function resets a Queue back to the initial state.
[HIC_Make_History_Entry]
TCC_ResumeTask
[TCT_Check_Stack]
TCT__ControlToSystem
TCT_System_Protect
TCT_Unprotect
*********************************************************************************************************
*/
INT8U ResetQueue(QUEUE_QCB *queue_ptr)
{
QUEUE_WAIT_QUEUE *linknode;
INT8U oldstate,state;
if (queue_ptr == NULL) { /* Not allowed to delete idle task */
return (ERR_INVALID_QUEUE);
}
#ifdef OPTION_ENABLE_STACK_CHECK
Check_Stack();
#endif
#ifdef OPTION_ENABLE_HISTORY
MakeHistoryLog(DELETE_TASK,Queue_ptr->TASK_name);//在日志中记录操作,时间等信息
#endif
oldstate=0;
DoProtect(queue_ptr);
// Queue_ptr->message_present=FALSE;
while(queue_ptr->task_wait_header){
state=_ResumeTask(queue_ptr->task_wait_header->suspended_task,QUEUE_SUSPEND);
if(state==TRUE) oldstate =state;
Remove_From_List(&(LIST_NODE *)(queue_ptr->task_wait_header),(LIST_NODE *)(queue_ptr->task_wait_header));
}
DoUnProtect(queue_ptr);
if(oldstate==TRUE) _ControlToSystem();
return (SUCCESS);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -