00001
00002
00003
00004
00005
00006
00007 #include "stdio.h"
00008 #include "stdlib.h"
00009 #include "task.h"
00010 #include "MessageQueue.h"
00011
00012 static ECB *MqBlocker;
00013 int MsgAllocCount = 0;
00014
00015 MESSAGE_QUEUE * MqInit(int size,char *name)
00016 {
00017 char *buffer = malloc(sizeof(MESSAGE_QUEUE) + size * sizeof(MSG*) );
00018 MESSAGE_QUEUE *rv = (MESSAGE_QUEUE *)buffer;
00019 MSG **pM = (MSG **)(&buffer[sizeof(MESSAGE_QUEUE)]);
00020 PendSemaphore(MqBlocker,0);
00021 rv->Head = 0;
00022 rv->Tail = 0;
00023 rv->nMsg = 0;
00024 rv->b = pM;
00025 rv->Size = size;
00026 rv->Sem = NewSemaphore(0,SEMAPHORE_MODE_BLOCKING,name);
00027 PostSemaphore(MqBlocker,0);
00028 return rv;
00029 }
00030
00031 void MqPut(MESSAGE_QUEUE *mq,MSG *m)
00032 {
00033 char sr;
00034
00035 if(mq)
00036 {
00037 sr = Disable();
00038 if(mq->nMsg < mq->Size)
00039 {
00040 mq->b[mq->Head++] = m;
00041 if(mq->Head == mq->Size)
00042 mq->Head = 0;
00043 mq->nMsg++;
00044 PostSemaphore(mq->Sem,0);
00045 }
00046 Enable(sr);
00047 }
00048 }
00049
00050 MSG *MqGet(MESSAGE_QUEUE *mq)
00051 {
00052 char sr;
00053 MSG *retval = 0;
00054
00055 if(mq)
00056 {
00057 PendSemaphore(mq->Sem,0);
00058 sr = Disable();
00059 retval = mq->b[mq->Tail++];
00060 if(mq->Tail == mq->Size)
00061 mq->Tail = 0;
00062 mq->nMsg--;
00063 Enable(sr);
00064 }
00065 return retval;
00066 }
00067
00068 MSG *NewMSG(int payloadSize)
00069 {
00070 MSG *retval = 0;
00071
00072 retval = (MSG *)malloc(sizeof(MSG) + payloadSize);
00073 MsgAllocCount++;
00074 retval->MaxPayload = payloadSize;
00075 retval->ReplyHandle = NULL;
00076 retval->PayloadSize = 0;
00077 retval->next = 0;
00078 return retval;
00079 }
00080
00081 void DeleteMSG(MSG *m)
00082 {
00083
00084
00085
00086 free(m);
00087 }
00088
00089 void InitMessageQueueManager(void)
00090 {
00091 MqBlocker = NewSemaphore(1,SEMAPHORE_MODE_BLOCKING,"MSG_BLOCKER");
00092 }
00093