/************************************************************************* This file is part of aOS. aOS 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 2 of the License, or (at your option) any later version. aOS 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 aOS; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Copyright 2001, 2002 Anssi Ylätalo ************************************************************************** Modified by BRL --init mailbox message to NULL --set mmsg to NULL after it has been read Added by BRL -- aos_mbox_accept : no wait */ //#include "aos.h" //=================================================================== /* Create and initialize a mailbox */ aos_mcb *aos_mbox_create( void ) { aos_mcb *mcb_new; mcb_new = 0; AOS_ENTER_CRITICAL; mcb_new = (aos_mcb *)aos_ctbl.mcb_free_list; /* Save pointer to next free mcb */ if( mcb_new ) { /* mcbs left? */ mcb_new->mmsg = NULL; //added by BRL to init mailbox aos_ctbl.mcb_free_list = (aos_mcb *)mcb_new->next_mcb; /* Yes, release mcb from freelist */ mcb_new->next_mcb = (aos_mcb *)aos_ctbl.mcb_used_list; /* Link new mcb into mcb used list */ aos_ctbl.mcb_used_list = (aos_mcb *)mcb_new; mcb_new->mrecv = (aos_scb *)aos_sem_create( 0 ); /* Semaphore for receiving messages */ mcb_new->msend = (aos_scb *)aos_sem_create( 1 ); /* Semaphore for sending messages */ mcb_new->mmsg = (void *)0; /* Clear message pointer */ } AOS_EXIT_CRITICAL; return( mcb_new ); } //=================================================================== /* Send a message to mailbox */ void aos_mbox_send( aos_mcb *mcb, void *msg ) { aos_wait( (aos_scb *)mcb->msend ); /* Is there room in the mailbox */ mcb->mmsg = msg; /* Put message in the mailbox */ aos_signal( (aos_scb *)mcb->mrecv ); /* Signal the queue's receive semaphore */ } //=================================================================== /* Receive a message from mailbox */ void *aos_mbox_recv( aos_mcb *mcb ) { void *msg; aos_wait( (aos_scb *)mcb->mrecv ); /* Wait for a message */ msg = mcb->mmsg; /* Get the message from mailbox */ mcb->mmsg = NULL; //and wipe the old message aos_signal( (aos_scb *)mcb->msend ); /* Signal the queue's sending semaphore */ return( msg ); } //=================================================================== //receive a message without waiting //You MUST check the retrun value to see if it is NULL void *aos_mbox_accept( aos_mcb *mcb ) { void *msg; AOS_ENTER_CRITICAL; msg = mcb->mmsg; /* Get the message from mailbox */ if (msg!=NULL) { //if there is a message mcb->mmsg = NULL ; //clear the mailbox aos_signal( (aos_scb *)mcb->msend ); /* Signal the sending semaphore */ } return msg; AOS_EXIT_CRITICAL; }