# system.c

Go to the documentation of this file.

    00001 /*****************************************************************************************
    00002  Copyright (c) 2002-2005 The UbixOS Project
    00003  All rights reserved.
    00004
    00005  Redistribution and use in source and binary forms, with or without modification, are
    00006  permitted provided that the following conditions are met:
    00007
    00008  Redistributions of source code must retain the above copyright notice, this list of
    00009  conditions, the following disclaimer and the list of authors.  Redistributions in binary
    00010  form must reproduce the above copyright notice, this list of conditions, the following
    00011  disclaimer and the list of authors in the documentation and/or other materials provided
    00012  with the distribution. Neither the name of the UbixOS Project nor the names of its
    00013  contributors may be used to endorse or promote products derived from this software
    00014  without specific prior written permission.
    00015
    00016  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
    00017  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00018  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
    00019  THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    00020  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
    00021  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    00022  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
    00023  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    00024  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00025
    00026  $Id: system_8c-source.html 88 2016-01-12 00:11:29Z reddawg $
    00027
    00028 *****************************************************************************************/
    00029
    00030 #include <mpi/mpi.h>
    00031 #include <lib/kmalloc.h>
    00032 #include <lib/string.h>
    00033 #include <ubixos/spinlock.h>
    00034
    00035 static mpi_mbox_t  *mboxList    = 0x0;
    00036 static spinLock_t  mpiSpinLock = SPIN_LOCK_INITIALIZER;
    00037
    00038 /*****************************************************************************************
    00039
    00040  Function: static mpiMbox_t * mpiFindMbox(char *name)
    00041
    00042  Description: This function will find a mail box that matches the supplied name
    00043
    00044  Notes: This function is not task-safe!  Lock must be done before call.
    00045
    00046 *****************************************************************************************/
    00047 static mpi_mbox_t * mpi_findMbox(char *name) {
    00048   mpi_mbox_t *mbox = 0x0;
    00049
    00050   for (mbox = mboxList;mbox;mbox = mbox->next) {
    00051     if (!strcmp(mbox->name,name)) {
    00052        return(mbox);
    00053        }
    00054     }
    00055
    00056   return(0x0);
    00057   }
    00058
    00059 /*****************************************************************************************
    00060
    00061  Function: int mpiCreateMbox(char *name)
    00062
    00063  Description: This function will create a new mailbox if it fails it will return -1
    00064               otherwise it returns 0x0
    00065
    00066  Notes:
    00067
    00068 *****************************************************************************************/
    00069 int mpi_createMbox(char *name) {
    00070   mpi_mbox_t *mbox = 0x0;
    00071
    00072   spinLock(&mpiSpinLock);
    00073   if (mpi_findMbox(name) != 0x0) {
    00074     spinUnlock(&mpiSpinLock);
    00075     return(-1);
    00076     }
    00077
    00078   mbox = (mpi_mbox_t *)kmalloc(sizeof(mpi_mbox_t));
    00079
    00080   sprintf(mbox->name,name);
    00081   mbox->pid = _current->id;
    00082
    00083   if (mboxList == 0x0) {
    00084     mbox->prev = 0x0;
    00085     mbox->next = 0x0;
    00086     mboxList   = mbox;
    00087     }
    00088   else {
    00089     mbox->next     = mboxList;
    00090     mbox->prev     = 0x0;
    00091     mboxList->prev = mbox;
    00092     mboxList       = mbox;
    00093     }
    00094
    00095   spinUnlock(&mpiSpinLock);
    00096   return(0x0);
    00097   }
    00098
    00099 /*****************************************************************************************
    00100
    00101  Function: int mpiSpam(uInt32 type,void *data)
    00102
    00103  Description: This function will send a message to every mailbox
    00104
    00105  Notes:
    00106
    00107 *****************************************************************************************/
    00108 int mpi_spam(uInt32 type,void *data) {
    00109   mpi_mbox_t    *mbox    = 0x0;
    00110   mpi_message_t *message = 0x0;
    00111
    00112   spinLock(&mpiSpinLock);
    00113
    00114   for (mbox = mboxList;mbox;mbox = mbox->next) {
    00115     message = (mpi_message_t *)kmalloc(sizeof(mpi_message_t));
    00116
    00117     message->header = type;
    00118     memcpy(message->data,data,MESSAGE_LENGTH);
    00119     message->next = 0x0;
    00120
    00121     if (mbox->msg == 0x0) {
    00122       mbox->msg       = message;
    00123       }
    00124     else {
    00125       mbox->msgLast->next = message;
    00126       mbox->msgLast       = message;
    00127       }
    00128     }
    00129
    00130   spinUnlock(&mpiSpinLock);
    00131   return(0x0);
    00132   }
    00133
    00134 /*****************************************************************************************
    00135
    00136  Function: int mpiPostMessage(char *name,uInt32 type,void *data)
    00137
    00138  Description: This function will post a message to specified mailbox
    00139
    00140  Notes:
    00141
    00142 *****************************************************************************************/
    00143 int mpi_postMessage(char *name,uInt32 type,mpi_message_t *msg) {
    00144   mpi_mbox_t    *mbox    = 0x0;
    00145   mpi_message_t *message = 0x0;
    00146
    00147   spinLock(&mpiSpinLock);
    00148
    00149   mbox = mpi_findMbox(name);
    00150
    00151   if (mbox == 0x0) {
    00152     spinUnlock(&mpiSpinLock);
    00153     return(0x1);
    00154     }
    00155
    00156   message = (mpi_message_t *)kmalloc(sizeof(mpi_message_t));
    00157
    00158   message->header = msg->header;
    00159   memcpy(message->data,msg->data,MESSAGE_LENGTH);
    00160   message->pid    = _current->id;
    00161   message->next = 0x0;
    00162
    00163   if (mbox->msg == 0x0) {
    00164     mbox->msg       = message;
    00165     }
    00166   else {
    00167     mbox->msgLast->next = message;
    00168     mbox->msgLast       = message;
    00169     }
    00170
    00171   spinUnlock(&mpiSpinLock);
    00172
    00173   if (type == 0x2) {
    00174     while (mbox->msgLast != 0x0);
    00175     }
    00176
    00177   return(0x0);
    00178   }
    00179
    00180 /*****************************************************************************************
    00181
    00182  Function: int mpiFetchMessage(char *name,mpiMessage_t *msg)
    00183
    00184  Description: This function will fetch the next message out of the specified mailbox
    00185
    00186  Notes:
    00187
    00188 *****************************************************************************************/
    00189 int mpi_fetchMessage(char *name,mpi_message_t *msg) {
    00190   mpi_mbox_t    *mbox   = 0x0;
    00191   mpi_message_t *tmpMsg = 0x0;
    00192
    00193   spinLock(&mpiSpinLock);
    00194
    00195   mbox = mpi_findMbox(name);
    00196
    00197   if (mbox == 0x0) {
    00198     spinUnlock(&mpiSpinLock);
    00199     return(-1);
    00200     }
    00201
    00202   if (mbox->msg == 0x0) {
    00203     spinUnlock(&mpiSpinLock);
    00204     return(-1);
    00205     }
    00206
    00207   if (mbox->pid != _current->id) {
    00208     spinUnlock(&mpiSpinLock);
    00209     return(-1);
    00210     }
    00211
    00212   msg->header = mbox->msg->header;
    00213   memcpy(msg->data,mbox->msg->data,MESSAGE_LENGTH);
    00214   msg->pid    = mbox->msg->pid;
    00215
    00216   tmpMsg    = mbox->msg;
    00217   mbox->msg = mbox->msg->next;
    00218
    00219   kfree(tmpMsg);
    00220
    00221   spinUnlock(&mpiSpinLock);
    00222   return(0x0);
    00223   }
    00224
    00225 /*****************************************************************************************
    00226
    00227  Function: int mpiDestroyMbox(char *name)
    00228
    00229  Description: This function will fetch the next message out of the specified mailbox
    00230
    00231  Notes:
    00232
    00233 *****************************************************************************************/
    00234 int mpi_destroyMbox(char *name) {
    00235   mpi_mbox_t    *mbox   = 0x0;
    00236
    00237   spinLock(&mpiSpinLock);
    00238
    00239   for (mbox = mboxList;mbox;mbox=mbox->next) {
    00240     if (!strcmp(mbox->name,name)) {
    00241       if (mbox->pid != _current->id) {
    00242         spinUnlock(&mpiSpinLock);
    00243         return(-1);
    00244         }
    00245       mbox->prev->next = mbox->next;
    00246       mbox->next->prev = mbox->prev;
    00247       kfree(mbox);
    00248       spinUnlock(&mpiSpinLock);
    00249       return(0x0);
    00250       }
    00251     }
    00252
    00253   spinUnlock(&mpiSpinLock);
    00254   return(-1);
    00255   }
    00256
    00257 /***
    00258  END
    00259  ***/