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

📄 pipe.c

📁 Bycore是一个嵌入式操作系统内核。Bycore包括内存管理、任务管理、中断管理、任务互斥、同步与通信管理等功能。Bycore全部由C语言完成
💻 C
字号:
/** *  pipe.c - Related routines of pipe. * *  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/core.h"#include "include/mem.h"#include "include/pipe.h"#include "include/sem.h"#include "include/irq.h"/** * pipeInit - It creates a new pipe. * @SendID: ID of task that sends data to pipe. * @RevID: ID of task that receives data from pipe. * @Size: The size of pipe. * @return: The start address of new pipe. * * @notes: */pipe_t *pipeInit(uword_t SendID, uword_t RevID, uword_t Size) {    pipe_t *pipe;    void **ptmp;    if((pipe = (pipe_t*)kmalloc(sizeof(pipe_t))) == NULL) {        return NULL;    }    if((ptmp = (void **)kmalloc(Size *sizeof(void*))) == NULL) {        kfree(pipe);        return NULL;    }    pipe->in = ptmp;    pipe->out = ptmp;    pipe->start = ptmp;    pipe->end = &ptmp[Size - 1];    pipe->send_id = SendID;    pipe->rev_id = RevID;    pipe->size = Size;    pipe->used = 0;    return pipe;}/** * pipeSend - Sending a message to a pipe. * @pPipe: Pointer to the pipe. * @pMsg: Pointer to the message. * @err: It will return rusults or reasons about operation. * @return: TURE if successful, otherwise FALSE; * * @notes: */uword_t pipeSend(pipe_t *pPipe, void *pMsg, uword_t *err) {    if(pPipe == NULL) {        *err = PIPE_ID_NOT_MATCH;        return FALSE;    }    if(current()->id != pPipe->send_id) {        *err = PIPE_ID_NOT_MATCH;        return FALSE;    }    mac_disable_irq();    if(pPipe->size == pPipe->used) {        *err = PIPE_FULL;        mac_enable_irq();        return FALSE;    }    *pPipe->in = pMsg;    if(pPipe->in++ == pPipe->end) {        pPipe->in = pPipe->start;    }    pPipe->used++;    *err = PIPE_OK;    mac_enable_irq();    return TRUE;}/** * pipeRev - Retrieve message from pipe. * @pPipe: Pointer to the pipe. * @err: It will return rusults or reasons of this operation. * @return: The message if successful, NULL for failed. * * @notes: */void *pipeRev(pipe_t *pPipe, uword_t *err) {    void **pmsg;    if(pPipe == NULL) {        *err = PIPE_ID_NOT_MATCH;        return NULL;    }    if(current()->id != pPipe->rev_id) {        *err = PIPE_ID_NOT_MATCH;        return NULL;    }    mac_disable_irq();    if(pPipe->used == 0) {        *err = PIPE_EMPTY;        mac_enable_irq();        return NULL;    }    pmsg = pPipe->out;    pPipe->used--;    if(pPipe->out++ == pPipe->end) {        pPipe->out = pPipe->start;    }    *err = PIPE_OK;    mac_enable_irq();    return  *pmsg;}/** * pipeClear - Clear a pipe. * @pPipe: Pointer to the pipe. * @return: * * @notes: */void pipeClear(pipe_t *pPipe) {    pPipe->used = 0;    pPipe->in = pPipe->start;    pPipe->out = pPipe->start;}/** * pipeDel - Delete a pipe. * @pPipe: Pointer to the pipe. * @return: * * @notes: */void pipeDel(pipe_t *pPipe) {    kfree(pPipe);}

⌨️ 快捷键说明

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