# HG changeset patch # User Merten Sach # Date 1331662069 -3600 # Node ID 95a03e43148029fccf4848e743a2f053069f4867 # Parent 7ba5a3a6102d9f199ca77de3ccf9528a616f4559 corrected comments and variable renaming diff -r 7ba5a3a6102d -r 95a03e431480 LossyCom.c --- a/LossyCom.c Tue Mar 13 18:22:12 2012 +0100 +++ b/LossyCom.c Tue Mar 13 19:07:49 2012 +0100 @@ -1,4 +1,3 @@ - /* * For a detailed description see header file. */ @@ -9,44 +8,44 @@ #include "VMS_Implementations/VMS_impl/vmalloc.h" /* - * Initializes the central exchange structure. + * Initializes the central exchange structure and sets all trigger counter to 0 * Allocates memory to fit the number of endpoints. * Returns NULL if an error occurs. */ lossyCom__exchange_t* lossyCom__initialize(uint16_t numEndpoints) { - lossyCom__exchange_t* exchange; + lossyCom__exchange_t* centralExchange; - exchange = VMS_WL__malloc(sizeof(lossyCom__exchange_t)); - if(exchange == NULL) + centralExchange = VMS_WL__malloc(sizeof(lossyCom__exchange_t)); + if(centralExchange == NULL) return NULL; - exchange->BroadcastTriggerCounter = 0; - exchange->numEndpoints = numEndpoints; - exchange->outboxArray = VMS_WL__malloc(sizeof(lossyCom__msg_t)*numEndpoints); - if(exchange->outboxArray == NULL){ - VMS_WL__free(exchange); + centralExchange->broadcastTriggerCounter = 0; + centralExchange->numEndpoints = numEndpoints; + centralExchange->outboxArray = VMS_WL__malloc(sizeof(lossyCom__msg_t)*numEndpoints); + if(centralExchange->outboxArray == NULL){ + VMS_WL__free(centralExchange); return NULL; } - exchange->p2pTriggerCounter = VMS_WL__malloc(sizeof(uint16_t)*numEndpoints); - if(exchange->p2pTriggerCounter == NULL){ - VMS_WL__free(exchange->outboxArray); - VMS_WL__free(exchange); + centralExchange->p2pTriggerCounters = VMS_WL__malloc(sizeof(uint16_t)*numEndpoints); + if(centralExchange->p2pTriggerCounters == NULL){ + VMS_WL__free(centralExchange->outboxArray); + VMS_WL__free(centralExchange); return NULL; } //reset all point 2 point trigger counter - memset((void*)exchange->p2pTriggerCounter, 0, sizeof(uint16_t)*numEndpoints); + memset((void*)centralExchange->p2pTriggerCounters, 0, sizeof(uint16_t)*numEndpoints); - return exchange; + return centralExchange; } /* * Connects the local endpoint to the central exchange structure. - * This registers a message handler that handles all the incomming messages. + * This registers a message handler that handles all incoming messages. * Also an endpointID is set this ID has to be between 0 and total number of - * endpoints-1 and the number has to be unique. + * endpoints-1 and unique. */ void lossyCom__initialize_endpoint(lossyCom__endpoint_t* localEndpoint, lossyCom__exchange_t* centralExchange, @@ -61,13 +60,17 @@ localEndpoint->msgHandlerData = msgHandlerData; } +/* + * Prepare a lossyCom__msg_t in the correct format that contains the trigger + * value, the receiver endpoint ID and the message body + */ inline lossyCom__msg_t prepareMsg(uint16_t triggerValue, lossyCom__endpointID_t receiverEndpointID, - lossyCom__msgBody_t msg) + lossyCom__msgBody_t msgBody) { lossyCom__msg_t msgDraft; - msgDraft = (0 | msg); + msgDraft = (0 | msgBody); msgDraft |= ((lossyCom__msg_t)receiverEndpointID << ENDPOINT_ID_SHIFT); msgDraft |= ((lossyCom__msg_t)triggerValue << TRIGGER_SHIFT); @@ -84,7 +87,7 @@ uint16_t increasedTrigger; lossyCom__msg_t msg; - increasedTrigger = centralExchange->BroadcastTriggerCounter +1; + increasedTrigger = centralExchange->broadcastTriggerCounter +1; //build message msg = prepareMsg(increasedTrigger, BROADCAST_ID, msgBody); @@ -93,7 +96,7 @@ centralExchange->outboxArray[localEndpoint->endpointID] = msg; //update broadcast trigger counter - centralExchange->BroadcastTriggerCounter = increasedTrigger; + centralExchange->broadcastTriggerCounter = increasedTrigger; } /* @@ -109,7 +112,7 @@ uint16_t increasedTrigger; increasedTrigger = - centralExchange->p2pTriggerCounter[receiverEndpointID] +1; + centralExchange->p2pTriggerCounters[receiverEndpointID] +1; //build message msg = prepareMsg(increasedTrigger, receiverEndpointID, msgBody); @@ -118,7 +121,7 @@ centralExchange->outboxArray[localEndpoint->endpointID] = msg; //write back increased trigger counter - centralExchange->p2pTriggerCounter[receiverEndpointID] = increasedTrigger; + centralExchange->p2pTriggerCounters[receiverEndpointID] = increasedTrigger; } int inline isUnreceivedMsg(uint16_t msgTrigger, @@ -153,9 +156,9 @@ centralExchange = localEndpoint->centralExchange; //save trigger counter to know find valid messages - broadcastTriggerSnapshot = centralExchange->BroadcastTriggerCounter; + broadcastTriggerSnapshot = centralExchange->broadcastTriggerCounter; p2pTriggerSnapshot = - centralExchange->p2pTriggerCounter[localEndpoint->endpointID]; + centralExchange->p2pTriggerCounters[localEndpoint->endpointID]; //new message arrived if trigger counter is higher than the last time read if( broadcastTriggerSnapshot > localEndpoint->lastReceivedBroadcastTrigger || diff -r 7ba5a3a6102d -r 95a03e431480 LossyCom.h --- a/LossyCom.h Tue Mar 13 18:22:12 2012 +0100 +++ b/LossyCom.h Tue Mar 13 19:07:49 2012 +0100 @@ -4,9 +4,11 @@ * their status and pass work around. This is to avoid idling cores that occupy * the Masterlock. * - * The communication entities poll a central trigger counter that increases when - * there are new messages. However it's not guaranteed that the increase of the - * counter reflects the number of new messages. + * The communication entities poll a central trigger counter and per receiver + * trigger counter that increase when there are new messages. + * However it's not guaranteed that the increase of the counter reflects the + * number of new messages. The new trigger counter can even be lower than the + * last see counter for some receiver. * * The messages consist of a single 64bit word so it can be written in one * operation and therefore avoiding inconsistent messages. @@ -15,10 +17,13 @@ * when acquiring the communication endpoint. * * The message passing is handled by a central struct that is used w/o any lock. - * It consists of a array messages for the number of communicating entities. The - * number of entities is fixed at initialization. + * It consists of a array messages for the number of communicating endpoints. + * The number of endpoints is fixed at initialization time. */ +#ifndef LOSSYCOM_H +#define LOSSYCOM_H + #include /*********************************** @@ -58,8 +63,8 @@ * Central communication structure. */ typedef struct{ - volatile uint16_t BroadcastTriggerCounter; - volatile uint16_t* p2pTriggerCounter; + volatile uint16_t broadcastTriggerCounter; + volatile uint16_t* p2pTriggerCounters; uint16_t numEndpoints; lossyCom__msg_t* outboxArray; }lossyCom__exchange_t; @@ -68,12 +73,12 @@ * Endpoint data structure. */ typedef struct { + lossyCom__exchange_t* centralExchange; + lossyCom__endpointID_t endpointID; + lossyCom__msgHandler msgHandler; + void* msgHandlerData; uint16_t lastReceivedBroadcastTrigger; uint16_t lastReceivedp2pTrigger; - lossyCom__endpointID_t endpointID; - lossyCom__exchange_t* centralExchange; - lossyCom__msgHandler msgHandler; - void* msgHandlerData; } lossyCom__endpoint_t; /*********************************** @@ -95,4 +100,6 @@ lossyCom__endpointID_t receiverEndpointID, lossyCom__msgBody_t msg); -void lossyCom__receiveMsg(lossyCom__endpoint_t* localEndpoint); \ No newline at end of file +void lossyCom__receiveMsg(lossyCom__endpoint_t* localEndpoint); + +#endif //LOSSYCOM_H \ No newline at end of file