/* ******************* ******************************* C SOURCE FILE ******************************* ** ******************* ** ** ** ** project : The C Kernel ** ** filename : CKMBOX.C ** ** version : 2 ** ** last revised : August 22, 2004 ** ** ** ***************************************************************************** ** ** ** Copyright (c) 1998-2004, P.K. van der Vlugt ** ** All rights reserved. ** ** ** ***************************************************************************** VERSION HISTORY: ---------------- Version : 1 Date : June 01, 2003 Revised by : P.K. van der Vlugt Description : Original version. Version : 2 Date : August 22, 2004 Revised by : P.K. van der Vlugt Description : * Changed module name to comply to new kernel module naming convention. */ #define _CKMBOX_C_SRC /****************************************************************************/ /** **/ /** MODULES USED **/ /** **/ /****************************************************************************/ #include "ckmbox.h" #include "ckmsg.h" #include "ckqueue.h" /****************************************************************************/ /** **/ /** DEFINITIONS AND MACROS **/ /** **/ /****************************************************************************/ /****************************************************************************/ /** **/ /** TYPEDEFS AND STRUCTURES **/ /** **/ /****************************************************************************/ /****************************************************************************/ /** **/ /** PROTOTYPES OF LOCAL FUNCTIONS **/ /** **/ /****************************************************************************/ #if NR_OF_MBOXS > 0 && NR_OF_MSGS > 0 static void init (int8u id); /****************************************************************************/ /** **/ /** EXPORTED VARIABLES **/ /** **/ /****************************************************************************/ MboxType Mbox[NR_OF_MBOXS]; /****************************************************************************/ /** **/ /** GLOBAL VARIABLES **/ /** **/ /****************************************************************************/ /****************************************************************************/ /** **/ /** EXPORTED FUNCTIONS **/ /** **/ /****************************************************************************/ /****************************************************************************/ void MboxInit (void) /****************************************************************************/ { int8u id; /**** Initialize all id's ****/ for (id = 0; id < NR_OF_MBOXS; id++) init (id); } /****************************************************************************/ int8u MboxAlloc (void) /****************************************************************************/ { int8u id; for (id = 0; id < NR_OF_MBOXS; id++) { /**** Search for a free Id ****/ if (!Mbox[id].req) { /**** Allocate associated queue ****/ Mbox[id].qid = QueueAlloc (); /**** Check if OK ****/ if (Mbox[id].qid == NR_OF_QUEUES) return (NR_OF_MBOXS); break; } } /**** If found mark id as allocated ****/ if (id < NR_OF_MBOXS) Mbox[id].req = TRUE; return (id); } /****************************************************************************/ void MboxFree (int8u id) /****************************************************************************/ { int8u msgId; /**** Cannot free illegal id ****/ if (id >= NR_OF_MBOXS) return; /**** Free all the queued messages ****/ msgId = Mbox[id].msgHead; while (msgId != Mbox[id].msgTail) { MsgFree (msgId); msgId = Msg[msgId].next; } /**** Free the associated queue ****/ QueueFree (Mbox[id].qid); init (id); } /****************************************************************************/ int8u EnqueueMsg (int8u id, int32u msg) /****************************************************************************/ { int8u msgId, msgTail; /**** Allocate a new message ****/ msgId = MsgAlloc (); if (msgId >= NR_OF_MSGS) return (NR_OF_MSGS); /**** Fill in the message field ****/ SetMsg (msgId, msg); /**** Insert message in mailbox ****/ msgTail = Mbox[id].msgTail; if (msgTail >= NR_OF_MSGS) { /**** No messages in mailbox ****/ Mbox[id].msgHead = msgId; Mbox[id].msgTail = msgId; } else { /**** Add message to tail ****/ Msg[msgTail].next = msgId; Mbox[id].msgTail = msgId; } return (msgId); } /****************************************************************************/ int8u DequeueMsg (int8u id, int32u *msgPtr) /****************************************************************************/ { int8u msgHead, msgTail; /**** Get message head ****/ msgHead = Mbox[id].msgHead; if (msgHead >= NR_OF_MSGS) return (NR_OF_MSGS); /**** Get message tail ****/ msgTail = Mbox[id].msgTail; if (msgHead == msgTail) { /**** Last message was dequeued ****/ Mbox[id].msgHead = NR_OF_MSGS; Mbox[id].msgTail = NR_OF_MSGS; } else /**** Not empty: adjust head ****/ Mbox[id].msgHead = Msg[msgHead].next; /**** Get the message ****/ *msgPtr = GetMsg (msgHead); /**** And free message item ****/ MsgFree (msgHead); return (msgHead); } /****************************************************************************/ /** **/ /** LOCAL FUNCTIONS **/ /** **/ /****************************************************************************/ /****************************************************************************/ static void init (int8u id) /****************************************************************************/ { Mbox[id].req = FALSE; Mbox[id].msgHead = NR_OF_MSGS; Mbox[id].msgTail = NR_OF_MSGS; Mbox[id].qid = NR_OF_QUEUES; /**** Mark Mbox mode with FIFO_Q as default ****/ Mbox[id].mode = 0; } #endif /****************************************************************************/ /** **/ /** EOF **/ /** **/ /****************************************************************************/