/***************************************************************************************** Copyright (c) 2002-2004 The UbixOS Project All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions, the following disclaimer and the list of authors. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, the following disclaimer and the list of authors in the documentation and/or other materials provided with the distribution. Neither the name of the UbixOS Project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. $Id$ *****************************************************************************************/ #include <mpi/mpi.h> #include <lib/kmalloc.h> #include <lib/string.h> #include <ubixos/spinlock.h> static mpiMbox_t *mboxList = 0x0; static spinLock_t mpiSpinLock = SPIN_LOCK_INITIALIZER; /***************************************************************************************** Function: static mpiMbox_t * mpiFindMbox(char *name) Description: This function will find a mail box that matches the supplied name Notes: *****************************************************************************************/ static mpiMbox_t * mpiFindMbox(char *name) { mpiMbox_t *mbox = 0x0; for (mbox = mboxList;mbox;mbox = mbox->next) { if (!kstrcmp(mbox->name,name)) { spinUnlock(&mpiSpinLock); return(mbox); } } return(0x0); } /***************************************************************************************** Function: int mpiCreateMbox(char *name) Description: This function will create a new mailbox if it fails it will return -1 otherwise it returns 0x0 Notes: *****************************************************************************************/ int mpiCreateMbox(char *name) { mpiMbox_t *mbox = 0x0; spinLock(&mpiSpinLock); if (mpiFindMbox(name) != 0x0) { spinUnlock(&mpiSpinLock); return(-1); } mbox = (mpiMbox_t *)kmalloc(sizeof(mpiMbox_t)); sprintf(mbox->name,name); mbox->pid = _current->id; if (mboxList == 0x0) { mbox->prev = 0x0; mbox->next = 0x0; mboxList = mbox; } else { mbox->next = mboxList; mbox->prev = 0x0; mboxList->prev = mbox; mboxList = mbox; } spinUnlock(&mpiSpinLock); return(0x0); } /***************************************************************************************** Function: int mpiSpam(uInt32 type,void *data) Description: This function will send a message to every mailbox Notes: *****************************************************************************************/ int mpiSpam(uInt32 type,void *data) { mpiMbox_t *mbox = 0x0; mpiMessage_t *message = 0x0; spinLock(&mpiSpinLock); for (mbox = mboxList;mbox;mbox = mbox->next) { message = (mpiMessage_t *)kmalloc(sizeof(mpiMessage_t)); message->type = type; kmemcpy(message->data,data,MESSAGE_LENGTH); message->next = 0x0; if (mbox->msg == 0x0) { mbox->msg = message; } else { mbox->msgLast->next = message; mbox->msgLast = message; } } spinUnlock(&mpiSpinLock); return(0x0); } /***************************************************************************************** Function: int mpiPostMessage(char *name,uInt32 type,void *data) Description: This function will post a message to specified mailbox Notes: *****************************************************************************************/ int mpiPostMessage(char *name,uInt32 type,void *data) { mpiMbox_t *mbox = 0x0; mpiMessage_t *message = 0x0; spinLock(&mpiSpinLock); mbox = mpiFindMbox(name); if (mbox == 0x0) { spinUnlock(&mpiSpinLock); return(-1); } message = (mpiMessage_t *)kmalloc(sizeof(mpiMessage_t)); message->type = type; kmemcpy(message->data,data,MESSAGE_LENGTH); message->next = 0x0; if (mbox->msg == 0x0) { mbox->msg = message; } else { mbox->msgLast->next = message; mbox->msgLast = message; } spinUnlock(&mpiSpinLock); return(0x0); } /***************************************************************************************** Function: int mpiFetchMessage(char *name,mpiMessage_t *msg) Description: This function will fetch the next message out of the specified mailbox Notes: *****************************************************************************************/ int mpiFetchMessage(char *name,mpiMessage_t *msg) { mpiMbox_t *mbox = 0x0; mpiMessage_t *tmpMsg = 0x0; spinLock(&mpiSpinLock); mbox = mpiFindMbox(name); if (mbox == 0x0) { spinUnlock(&mpiSpinLock); return(-1); } if (mbox->msg == 0x0) { spinUnlock(&mpiSpinLock); return(-1); } if (mbox->pid != _current->id) { spinUnlock(&mpiSpinLock); return(-1); } msg->type = mbox->msg->type; kmemcpy(msg->data,mbox->msg->data,MESSAGE_LENGTH); tmpMsg = mbox->msg; mbox->msg = mbox->msg->next; kfree(tmpMsg); spinUnlock(&mpiSpinLock); return(0x0); } /***************************************************************************************** Function: int mpiDestroyMbox(char *name) Description: This function will fetch the next message out of the specified mailbox Notes: *****************************************************************************************/ int mpiDestroyMbox(char *name) { mpiMbox_t *mbox = 0x0; spinLock(&mpiSpinLock); for (mbox = mboxList;mbox;mbox=mbox->next) { if (!kstrcmp(mbox->name,name)) { if (mbox->pid != _current->id) { spinUnlock(&mpiSpinLock); return(-1); } mbox->prev->next = mbox->next; mbox->next->prev = mbox->prev; kfree(mbox); spinUnlock(&mpiSpinLock); return(0x0); } } spinUnlock(&mpiSpinLock); return(-1); } /*** $Log$ Revision 1.9 2004/05/26 15:34:29 reddawg mpi: oops forgot a spinUnlock Revision 1.8 2004/05/26 11:58:37 reddawg mpi: Fixed security now the pid used at time of mpiCreateMbox is the only pid allowed to do mpiFetchMessage and mpiDestroyMbox against that mail box Revision 1.7 2004/05/25 22:25:39 reddawg Typo Fix Revision 1.6 2004/05/25 18:32:02 reddawg Made MESSAGE_LENGTH define instead of hard coded message size Revision 1.4 2004/05/25 16:52:22 reddawg We now have mpiDestroyMbox(char *) This will of course destroy a mail box Revision 1.3 2004/05/25 16:33:26 reddawg Added some basic documentation Revision 1.2 2004/05/25 15:42:18 reddawg Enabled mpiSpam(); Revision 1.1 2004/05/25 14:03:22 reddawg First insertion of the MPI into our kernel END ***/