⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mailbox.c

📁 Bycore是一个嵌入式操作系统内核。Bycore包括内存管理、任务管理、中断管理、任务互斥、同步与通信管理等功能。Bycore全部由C语言完成
💻 C
字号:
/** *  mailbox.c - Related routines of mailbox. * *  Copyright (C) 2008  ZhangHu *  All rights reserved. *  E-MAIL: anmnmnly@gmail.com * *  This program is free software: you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *  the Free Software Foundation, either version 3 of the License, or *  (at your option) any later version. * *  This program is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with this program.  If not, see <http://www.gnu.org/licenses/>. */#include "include/types.h"#include "include/mem.h"#include "include/core.h"#include "include/mailbox.h"#include "include/irq.h"/** * mboxInit - It creates a new mailbox. * @Size: The size of mailbox. * @return: The start address of new mailbox. * * @notes: */mail_t *mboxInit(uword_t Size) {    mail_t *pmail;    msg_t *pmsg;    if((pmail = (mail_t*)kmalloc(sizeof(mail_t))) == NULL) {        return NULL;    }    if((pmsg = (msg_t*)kmalloc(Size *sizeof(msg_t))) == NULL) {        kfree(pmail);        return NULL;    }    kmemset(pmail, 0, sizeof(mail_t));    kmemset(pmsg, 0, Size *sizeof(msg_t));    pmsg->key_id = 0;    pmsg->rev_id = 0;    pmsg->send_id = 0;    pmsg->pmsg = NULL;    pmail->size = Size;    pmail->used = 0;    pmail->start = pmsg;    pmail->end = &pmsg[Size - 1];    pmail->pos = pmsg;    return pmail;}/** * mboxSend - Sending a message to a mailbox. * @pMial: Pointer to the mailbox. * @Key: Serial number of message. * @RevID: ID of Receiving task. * @pMsg: Pointer to the message. * @err: It will return rusults or reasons about this operation. * @return: TURE if successful, otherwise FALSE; * * @notes: */uword_t mboxSend(mail_t *pMail, uword_t Key, uword_t RevID,                 void *pMsg, uword_t *err) {    msg_t *pmark;    if(pMail == NULL) {        *err = MAILBOX_NOT_EXIST;        return FALSE;    }    mac_disable_irq();    if(pMail->size == pMail->used) {        *err = MAILBOX_FULL;        mac_enable_irq();        return FALSE;    }    pmark = pMail->pos;    do {        if(pMail->pos->pmsg == NULL) {            break;        } else if(pMail->pos++ == pMail->end) {            pMail->pos = pMail->start;        }    } while(pMail->pos != pmark);    pMail->pos->key_id = Key;    pMail->pos->send_id = current()->id;    pMail->pos->rev_id = RevID;    pMail->pos->pmsg = pMsg;    pMail->used++;    *err = MAILBOX_OK;    mac_enable_irq();    return TRUE;}/** * mboxRev - Retrieve message from mailbox. * @pMail: Pointer to the mailbox. * @Key: Serial number of message. * @SendID: ID of sending task. * @err: It will return rusults or reasons about this operation. * @return: The message if successful, NULL for failed. * * @notes: */void *mboxRev(mail_t *pMail, uword_t Key, uword_t SendID, uword_t *err) {    msg_t *pmark;    void *remsg;    if(pMail == NULL) {        *err = MAILBOX_NOT_EXIST;        return NULL;    }    mac_disable_irq();    if(pMail->used == 0) {        *err = MAILBOX_EMPTY;        mac_enable_irq();        return NULL;    }    /* check message */    pmark = pMail->pos;    do {        if(pMail->pos->pmsg != NULL &&  \            pMail->pos->key_id == Key &&  \            pMail->pos->send_id == SendID &&  \            pMail->pos->rev_id == current()->id) {            remsg = pMail->pos->pmsg;            pMail->pos->key_id = 0;            pMail->pos->send_id = 0;            pMail->pos->rev_id = 0;            pMail->pos->pmsg = NULL;            pMail->used--;            *err = MAILBOX_OK;            mac_enable_irq();            return remsg;        } else if(pMail->pos++ == pMail->end) {            pMail->pos = pMail->start;        }    } while(pMail->pos != pmark);    *err = MAILBOX_NOT_FOUND_MSG;    mac_enable_irq();    return NULL;}/** * mboxClear - Clear a mailbox. * @pMail: Pointer to the mailbox. * @return: * * @notes: */void mboxClear(mail_t *pMail) {    pMail->used = 0;    pMail->pos = pMail->start;}/** * mboxDel - Delete a mailbox. * @pMail: Pointer to the mailbox. * @return: * * @notes: */void mboxDel(mail_t *pMail) {    kfree(pMail->start);    kfree(pMail);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -