?? os_mbox.c
字號(hào):
/*
*********************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
* MESSAGE MAILBOX MANAGEMENT
*
* (c) Copyright 1992-2002, Jean J. Labrosse, Weston, FL
* All Rights Reserved
*
* File : OS_MBOX.C
* By : Jean J. Labrosse
* 翻譯: likee
*********************************************************************************************************
*/
#ifndef OS_MASTER_FILE
#include "includes.h"
#endif
#if OS_MBOX_EN > 0
/*
*********************************************************************************************************
* ACCEPT MESSAGE FROM MAILBOX
*
* Description: This function checks the mailbox to see if a message is available. Unlike OSMboxPend(),
* OSMboxAccept() does not suspend the calling task if a message is not available.
*
* Arguments : pevent is a pointer to the event control block
*
* Returns : != (void *)0 is the message in the mailbox if one is available. The mailbox is cleared
* so the next time OSMboxAccept() is called, the mailbox will be empty.
* == (void *)0 if the mailbox is empty or,
* if 'pevent' is a NULL pointer or,
* if you didn't pass the proper event pointer.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
無(wú)等待地從郵箱中得到一則消息
描述:此函數(shù)檢查郵箱中是否有消息,不同于OSMboxPend(),如果沒(méi)有可行消息,
OSMboxAccept() 不會(huì)掛起調(diào)用此函數(shù)的任務(wù)
參數(shù);:pevent:是指向事件控制塊的指針
返回:!= (void *)0 如果郵箱有消息,它指向郵箱中的一則消息,郵箱被清空,
如果下一次調(diào)用,它將是空的。
== (void *)0 如果郵箱為空,或者pevent指向空指針,或者沒(méi)有傳遞一個(gè)合理事件
指針
*********************************************************************************************************
*/
#if OS_MBOX_ACCEPT_EN > 0
void *OSMboxAccept (OS_EVENT *pevent)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
void *msg;
#if OS_ARG_CHK_EN > 0//允許參數(shù)檢驗(yàn)
if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
return ((void *)0);//如果事件無(wú)效,返回零
}
if (pevent->OSEventType != OS_EVENT_TYPE_MBOX) { /* Validate event block type */
//檢查pevent所指向的事件控制塊是不是由OS_EVENT_TYPE_MBOX建立
return ((void *)0);
}
#endif
OS_ENTER_CRITICAL();
msg = pevent->OSEventPtr;//函數(shù)得到郵箱的當(dāng)前內(nèi)容
pevent->OSEventPtr = (void *)0; /* Clear the mailbox *///清空郵箱
OS_EXIT_CRITICAL();
return (msg); /* Return the message received (or NULL) *///將郵箱中的內(nèi)容返回,以供調(diào)用
}
#endif
/*$PAGE*/
/*
*********************************************************************************************************
* CREATE A MESSAGE MAILBOX
*
* Description: This function creates a message mailbox if free event control blocks are available.
*
* Arguments : msg is a pointer to a message that you wish to deposit in the mailbox. If
* you set this value to the NULL pointer (i.e. (void *)0) then the mailbox
* will be considered empty.
*
* Returns : != (OS_EVENT *)0 is a pointer to the event control clock (OS_EVENT) associated with the
* created mailbox
* == (OS_EVENT *)0 if no event control blocks were available
*********************************************************************************************************
*/
/*
*********************************************************************************************************
建立一個(gè)郵箱
描述:如果空余事件控制塊允許,就建立一個(gè)消息郵箱
參數(shù):msg:指向你想存入郵箱的消息指針,如果存入零,我們認(rèn)為它為空
返回:!= (OS_EVENT *)0:指向所建立郵箱的事件控制塊指針
== (OS_EVENT *)0:如果沒(méi)有可行事件控制塊
*********************************************************************************************************
*/
OS_EVENT *OSMboxCreate (void *msg)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
OS_EVENT *pevent;
if (OSIntNesting > 0) { /* See if called from ISR ... */
return ((OS_EVENT *)0); /* ... can't CREATE from an ISR */
}//此函數(shù)不能由中斷服務(wù)程序調(diào)用
OS_ENTER_CRITICAL();
pevent = OSEventFreeList; /* Get next free event control block */
//得到一塊新的事件控制塊
if (OSEventFreeList != (OS_EVENT *)0) { /* See if pool of free ECB pool was empty */
OSEventFreeList = (OS_EVENT *)OSEventFreeList->OSEventPtr;
}//如果空余事件控制鏈表不為空,則進(jìn)行適當(dāng)調(diào)整,指向下一塊空余事件控制塊
OS_EXIT_CRITICAL();
if (pevent != (OS_EVENT *)0) {
pevent->OSEventType = OS_EVENT_TYPE_MBOX;//如果ECB可用,則將其設(shè)置為郵箱型
pevent->OSEventCnt = 0;//信號(hào)量,郵箱中不使用它
pevent->OSEventPtr = msg; /* Deposit message in event control block */
//保存消息在ECB中
OS_EventWaitListInit(pevent);//對(duì)事件控制塊的等待任務(wù)列表進(jìn)行初始化
}
return (pevent); /* Return pointer to event control block */
//返回事件控制塊的指針,對(duì)郵箱的操作都通過(guò)此指針完成。
}
/*$PAGE*/
/*
*********************************************************************************************************
* DELETE A MAIBOX
*
* Description: This function deletes a mailbox and readies all tasks pending on the mailbox.
*
* Arguments : pevent is a pointer to the event control block associated with the desired
* mailbox.
*
* opt determines delete options as follows:
* opt == OS_DEL_NO_PEND Delete the mailbox ONLY if no task pending
* opt == OS_DEL_ALWAYS Deletes the mailbox even if tasks are waiting.
* In this case, all the tasks pending will be readied.
*
* err is a pointer to an error code that can contain one of the following values:
* OS_NO_ERR The call was successful and the mailbox was deleted
* OS_ERR_DEL_ISR If you attempted to delete the mailbox from an ISR
* OS_ERR_INVALID_OPT An invalid option was specified
* OS_ERR_TASK_WAITING One or more tasks were waiting on the mailbox
* OS_ERR_EVENT_TYPE If you didn't pass a pointer to a mailbox
* OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
*
* Returns : pevent upon error
* (OS_EVENT *)0 if the mailbox was successfully deleted.
*
* Note(s) : 1) This function must be used with care. Tasks that would normally expect the presence of
* the mailbox MUST check the return code of OSMboxPend().
* 2) OSMboxAccept() callers will not know that the intended mailbox has been deleted!
* 3) This call can potentially disable interrupts for a long time. The interrupt disable
* time is directly proportional to the number of tasks waiting on the mailbox.
* 4) Because ALL tasks pending on the mailbox will be readied, you MUST be careful in
* applications where the mailbox is used for mutual exclusion because the resource(s)
* will no longer be guarded by the mailbox.
刪除一個(gè)郵箱
描述:此任務(wù)刪除一個(gè)郵箱,準(zhǔn)備好郵箱中掛起的所有任務(wù)
參數(shù):pevent:指向想要郵箱的ECB的指針
opt:確定刪除選項(xiàng)如下:
opt == OS_DEL_NO_PEND:如果沒(méi)有任務(wù)掛起就刪除郵箱
opt == OS_DEL_ALWAYS:即使任務(wù)等待也刪除郵箱,這種情況下,所有掛起的任務(wù)將就緒
err:包含以下值的錯(cuò)誤代碼指針:
OS_NO_ERR:調(diào)用成功,刪除郵箱
OS_ERR_DEL_ISR :如果從ISR中刪除郵箱
* OS_ERR_INVALID_OPT :指定了不合理選項(xiàng)
* OS_ERR_TASK_WAITING :一個(gè)或者幾個(gè)任務(wù)在郵箱中等待
* OS_ERR_EVENT_TYPE :沒(méi)有傳遞指針給郵箱
* OS_ERR_PEVENT_NULL :pevent是空指針,即郵箱的ECB為空。
返回:pevent:操作錯(cuò)誤
(OS_EVENT *)0:郵箱成功刪除
備注:1、此函數(shù)必須小心使用,任務(wù)希望在場(chǎng)的郵箱檢查OSMboxPend()的返回代碼
2、OSMboxAccept()調(diào)用將不知道想要的郵箱已經(jīng)被刪除
3、這個(gè)函數(shù)將潛在地關(guān)中斷一長(zhǎng)段時(shí)間,關(guān)中斷時(shí)間與在郵箱中等待的任務(wù)
數(shù)成正比
4、因?yàn)樗性卩]箱中掛起的任務(wù)將就緒,你必須在郵箱相互排斥情況下小心使用
因?yàn)橘Y源不再被郵箱保護(hù)
*********************************************************************************************************
*/
#if OS_MBOX_DEL_EN > 0
OS_EVENT *OSMboxDel (OS_EVENT *pevent, INT8U opt, INT8U *err)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
BOOLEAN tasks_waiting;//typedef unsigned char BOOLEAN
if (OSIntNesting > 0) { /* See if called from ISR ... */
*err = OS_ERR_DEL_ISR; /* ... can't DELETE from an ISR */
return (pevent);//如果在中斷中調(diào)用,不能刪除
}
#if OS_ARG_CHK_EN > 0
if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
*err = OS_ERR_PEVENT_NULL;
return (pevent);//如果pevent不合理
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -