00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
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 
00041 
00042 
00043 
00044 
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 
00062 
00063 
00064 
00065 
00066 
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 
00102 
00103 
00104 
00105 
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 
00137 
00138 
00139 
00140 
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 
00183 
00184 
00185 
00186 
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 
00228 
00229 
00230 
00231 
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 
00259