diff options
author | red031000 <rubenru09@aol.com> | 2020-09-03 18:38:32 +0100 |
---|---|---|
committer | red031000 <rubenru09@aol.com> | 2020-09-03 18:38:50 +0100 |
commit | de5004d18612a49bd6f04c26c81cf9c790c0721b (patch) | |
tree | ba5849e6c263a16674d42ee19a288ac14e874fc2 /arm7/lib/src | |
parent | 1b96581aa2aecec27b6f1e523c3611f9bb400caa (diff) |
arm7 OS_message
Diffstat (limited to 'arm7/lib/src')
-rw-r--r-- | arm7/lib/src/OS_message.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/arm7/lib/src/OS_message.c b/arm7/lib/src/OS_message.c new file mode 100644 index 00000000..62817517 --- /dev/null +++ b/arm7/lib/src/OS_message.c @@ -0,0 +1,97 @@ +#include "OS_message.h" +#include "function_target.h" +#include "OS_system.h" +#include "OS_thread.h" + +ARM_FUNC void OS_InitMessageQueue(OSMessageQueue *mq, OSMessage *msgArray, s32 msgCount) +{ + OS_InitThreadQueue(&mq->queueSend); + OS_InitThreadQueue(&mq->queueReceive); + mq->msgArray = msgArray; + mq->msgCount = msgCount; + mq->firstIndex = 0; + mq->usedCount = 0; +} + +ARM_FUNC BOOL OS_SendMessage(OSMessageQueue *mq, OSMessage msg, s32 flags) +{ + OSIntrMode enabled = OS_DisableInterrupts(); + + while (mq->msgCount <= mq->usedCount) + { + if (!(flags & OS_MESSAGE_BLOCK)) + { + (void)OS_RestoreInterrupts(enabled); + return FALSE; + } + else + { + OS_SleepThread(&mq->queueSend); + } + } + + s32 lastIndex = (mq->firstIndex + mq->usedCount) % mq->msgCount; + mq->msgArray[lastIndex] = msg; + mq->usedCount++; + + OS_WakeupThread(&mq->queueReceive); + + (void)OS_RestoreInterrupts(enabled); + return TRUE; +} + +ARM_FUNC BOOL OS_ReceiveMessage(OSMessageQueue *mq, OSMessage *msg, s32 flags) +{ + OSIntrMode enabled = OS_DisableInterrupts(); + + while (mq->usedCount == 0) + { + if (!(flags & OS_MESSAGE_BLOCK)) + { + (void)OS_RestoreInterrupts(enabled); + return FALSE; + } + else + { + OS_SleepThread(&mq->queueReceive); + } + } + + if (msg != NULL) + { + *msg = mq->msgArray[mq->firstIndex]; + } + mq->firstIndex = (mq->firstIndex + 1) % mq->msgCount; + mq->usedCount--; + + OS_WakeupThread(&mq->queueSend); + + (void)OS_RestoreInterrupts(enabled); + return TRUE; +} + +ARM_FUNC BOOL OS_ReadMessage(OSMessageQueue *mq, OSMessage *msg, s32 flags) +{ + OSIntrMode enabled = OS_DisableInterrupts(); + + while (mq->usedCount == 0) + { + if (!(flags & OS_MESSAGE_BLOCK)) + { + (void)OS_RestoreInterrupts(enabled); + return FALSE; + } + else + { + OS_SleepThread(&mq->queueReceive); + } + } + + if (msg != NULL) + { + *msg = mq->msgArray[mq->firstIndex]; + } + + (void)OS_RestoreInterrupts(enabled); + return TRUE; +} |