📄 os_q.c
字号:
/*
************************************************************************************************************************
* uC/OS-III
* The Real-Time Kernel
*
* (c) Copyright 2009-2011; Micrium, Inc.; Weston, FL
* All rights reserved. Protected by international copyright laws.
*
* MESSAGE QUEUE MANAGEMENT
*
* File : OS_Q.C
* By : JJL
* Version : V3.02.00
*
* LICENSING TERMS:
* ---------------
* uC/OS-III is provided in source form for FREE short-term evaluation, for educational use or
* for peaceful research. If you plan or intend to use uC/OS-III in a commercial application/
* product then, you need to contact Micrium to properly license uC/OS-III for its use in your
* application/product. We provide ALL the source code for your convenience and to help you
* experience uC/OS-III. The fact that the source is provided does NOT mean that you can use
* it commercially without paying a licensing fee.
*
* Knowledge of the source code may NOT be used to develop a similar product.
*
* Please help us continue to provide the embedded community with the finest software available.
* Your honesty is greatly appreciated.
*
* You can contact us at www.micrium.com, or by phone at +1 (954) 217-2036.
************************************************************************************************************************
*/
#include <os.h>
#ifdef VSC_INCLUDE_SOURCE_FILE_NAMES
const CPU_CHAR *os_q__c = "$Id: $";
#endif
#if OS_CFG_Q_EN > 0u
/*
************************************************************************************************************************
* CREATE A MESSAGE QUEUE
*
* Description: This function is called by your application to create a message queue. Message queues MUST be created
* before they can be used.
*
* Arguments : p_q is a pointer to the message queue
*
* p_name is a pointer to an ASCII string that will be used to name the message queue
*
* max_qty indicates the maximum size of the message queue (must be non-zero). Note that it's also not
* possible to have a size higher than the maximum number of OS_MSGs available.
*
* p_err is a pointer to a variable that will contain an error code returned by this function.
*
* OS_ERR_NONE the call was successful
* OS_ERR_CREATE_ISR can't create from an ISR
* OS_ERR_ILLEGAL_CREATE_RUN_TIME if you are trying to create the Queue after you called
* OSSafetyCriticalStart().
* OS_ERR_NAME if 'p_name' is a NULL pointer
* OS_ERR_OBJ_CREATED if the message queue has already been created
* OS_ERR_OBJ_PTR_NULL if you passed a NULL pointer for 'p_q'
* OS_ERR_Q_SIZE if the size you specified is 0
*
* Returns : none
************************************************************************************************************************
*/
void OSQCreate (OS_Q *p_q,
CPU_CHAR *p_name,
OS_MSG_QTY max_qty,
OS_ERR *p_err)
{
CPU_SR_ALLOC();
#ifdef OS_SAFETY_CRITICAL
if (p_err == (OS_ERR *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return;
}
#endif
#ifdef OS_SAFETY_CRITICAL_IEC61508
if (OSSafetyCriticalStartFlag == DEF_TRUE) {
*p_err = OS_ERR_ILLEGAL_CREATE_RUN_TIME;
return;
}
#endif
#if OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u
if (OSIntNestingCtr > (OS_NESTING_CTR)0) { /* Not allowed to be called from an ISR */
*p_err = OS_ERR_CREATE_ISR;
return;
}
#endif
#if OS_CFG_ARG_CHK_EN > 0u
if (p_q == (OS_Q *)0) { /* Validate arguments */
*p_err = OS_ERR_OBJ_PTR_NULL;
return;
}
if (max_qty == (OS_MSG_QTY)0) { /* Cannot specify a zero size queue */
*p_err = OS_ERR_Q_SIZE;
return;
}
#endif
OS_CRITICAL_ENTER();
p_q->Type = OS_OBJ_TYPE_Q; /* Mark the data structure as a message queue */
p_q->NamePtr = p_name;
OS_MsgQInit(&p_q->MsgQ, /* Initialize the queue */
max_qty);
OS_PendListInit(&p_q->PendList); /* Initialize the waiting list */
#if OS_CFG_DBG_EN > 0u
OS_QDbgListAdd(p_q);
#endif
OSQQty++; /* One more queue created */
OS_CRITICAL_EXIT();
*p_err = OS_ERR_NONE;
}
/*$PAGE*/
/*
************************************************************************************************************************
* DELETE A MESSAGE QUEUE
*
* Description: This function deletes a message queue and readies all tasks pending on the queue.
*
* Arguments : p_q is a pointer to the message queue you want to delete
*
* opt determines delete options as follows:
*
* OS_OPT_DEL_NO_PEND Delete the queue ONLY if no task pending
* OS_OPT_DEL_ALWAYS Deletes the queue even if tasks are waiting.
* In this case, all the tasks pending will be readied.
*
* p_err is a pointer to a variable that will contain an error code returned by this function.
*
* OS_ERR_NONE The call was successful and the queue was deleted
* OS_ERR_DEL_ISR If you tried to delete the queue from an ISR
* OS_ERR_OBJ_PTR_NULL if you pass a NULL pointer for 'p_q'
* OS_ERR_OBJ_TYPE if the message queue was not created
* OS_ERR_OPT_INVALID An invalid option was specified
* OS_ERR_TASK_WAITING One or more tasks were waiting on the queue
*
* Returns : == 0 if no tasks were waiting on the queue, or upon error.
* > 0 if one or more tasks waiting on the queue are now readied and informed.
*
* Note(s) : 1) This function must be used with care. Tasks that would normally expect the presence of the queue MUST
* check the return code of OSQPend().
*
* 2) OSQAccept() callers will not know that the intended queue has been deleted.
*
* 3) Because ALL tasks pending on the queue will be readied, you MUST be careful in applications where the
* queue is used for mutual exclusion because the resource(s) will no longer be guarded by the queue.
************************************************************************************************************************
*/
#if OS_CFG_Q_DEL_EN > 0u
OS_OBJ_QTY OSQDel (OS_Q *p_q,
OS_OPT opt,
OS_ERR *p_err)
{
OS_OBJ_QTY cnt;
OS_OBJ_QTY nbr_tasks;
OS_PEND_DATA *p_pend_data;
OS_PEND_LIST *p_pend_list;
OS_TCB *p_tcb;
CPU_TS ts;
CPU_SR_ALLOC();
#ifdef OS_SAFETY_CRITICAL
if (p_err == (OS_ERR *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return ((OS_OBJ_QTY)0);
}
#endif
#if OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u
if (OSIntNestingCtr > (OS_NESTING_CTR)0) { /* Can't delete a message queue from an ISR */
*p_err = OS_ERR_DEL_ISR;
return ((OS_OBJ_QTY)0);
}
#endif
#if OS_CFG_ARG_CHK_EN > 0u
if (p_q == (OS_Q *)0) { /* Validate arguments */
*p_err = OS_ERR_OBJ_PTR_NULL;
return ((OS_OBJ_QTY)0);
}
#endif
#if OS_CFG_OBJ_TYPE_CHK_EN > 0u
if (p_q->Type != OS_OBJ_TYPE_Q) { /* Make sure message queue was created */
*p_err = OS_ERR_OBJ_TYPE;
return ((OS_OBJ_QTY)0);
}
#endif
CPU_CRITICAL_ENTER();
p_pend_list = &p_q->PendList;
cnt = p_pend_list->NbrEntries;
nbr_tasks = cnt;
switch (opt) {
case OS_OPT_DEL_NO_PEND: /* Delete message queue only if no task waiting */
if (nbr_tasks == (OS_OBJ_QTY)0) {
#if OS_CFG_DBG_EN > 0u
OS_QDbgListRemove(p_q);
#endif
OSQQty--;
OS_QClr(p_q);
CPU_CRITICAL_EXIT();
*p_err = OS_ERR_NONE;
} else {
CPU_CRITICAL_EXIT();
*p_err = OS_ERR_TASK_WAITING;
}
break;
case OS_OPT_DEL_ALWAYS: /* Always delete the message queue */
OS_CRITICAL_ENTER_CPU_CRITICAL_EXIT();
ts = OS_TS_GET(); /* Get local time stamp so all tasks get the same time */
while (cnt > 0u) { /* Remove all tasks from the pend list */
p_pend_data = p_pend_list->HeadPtr;
p_tcb = p_pend_data->TCBPtr;
OS_PendObjDel((OS_PEND_OBJ *)((void *)p_q),
p_tcb,
ts);
cnt--;
}
#if OS_CFG_DBG_EN > 0u
OS_QDbgListRemove(p_q);
#endif
OSQQty--;
OS_QClr(p_q);
OS_CRITICAL_EXIT_NO_SCHED();
OSSched(); /* Find highest priority task ready to run */
*p_err = OS_ERR_NONE;
break;
default:
CPU_CRITICAL_EXIT();
*p_err = OS_ERR_OPT_INVALID;
break;
}
return (nbr_tasks);
}
#endif
/*$PAGE*/
/*
************************************************************************************************************************
* FLUSH QUEUE
*
* Description : This function is used to flush the contents of the message queue.
*
* Arguments : p_q is a pointer to the message queue to flush
*
* p_err is a pointer to a variable that will contain an error code returned by this function.
*
* OS_ERR_NONE upon success
* OS_ERR_FLUSH_ISR if you called this function from an ISR
* OS_ERR_OBJ_PTR_NULL If you passed a NULL pointer for 'p_q'
* OS_ERR_OBJ_TYPE If you didn't create the message queue
*
* Returns : The number of entries freed from the queue
*
* Note(s) : 1) You should use this function with great care because, when to flush the queue, you LOOSE the
* references to what the queue entries are pointing to and thus, you could cause 'memory leaks'. In
* other words, the data you are pointing to that's being referenced by the queue entries should, most
* likely, need to be de-allocated (i.e. freed).
************************************************************************************************************************
*/
#if OS_CFG_Q_FLUSH_EN > 0u
OS_MSG_QTY OSQFlush (OS_Q *p_q,
OS_ERR *p_err)
{
OS_MSG_QTY entries;
CPU_SR_ALLOC();
#ifdef OS_SAFETY_CRITICAL
if (p_err == (OS_ERR *)0) {
OS_SAFETY_CRITICAL_EXCEPTION();
return ((OS_MSG_QTY)0);
}
#endif
#if OS_CFG_CALLED_FROM_ISR_CHK_EN > 0u
if (OSIntNestingCtr > (OS_NESTING_CTR)0) { /* Can't flush a message queue from an ISR */
*p_err = OS_ERR_FLUSH_ISR;
return ((OS_MSG_QTY)0);
}
#endif
#if OS_CFG_ARG_CHK_EN > 0u
if (p_q == (OS_Q *)0) { /* Validate arguments */
*p_err = OS_ERR_OBJ_PTR_NULL;
return ((OS_MSG_QTY)0);
}
#endif
#if OS_CFG_OBJ_TYPE_CHK_EN > 0u
if (p_q->Type != OS_OBJ_TYPE_Q) { /* Make sure message queue was created */
*p_err = OS_ERR_OBJ_TYPE;
return ((OS_MSG_QTY)0);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -