Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > BestEffortMessaging
changeset 0:29d8b41926f0
Initial commit - compiles but has still the wrong name
author | Merten Sach <msach@mailbox.tu-berlin.de> |
---|---|
date | Wed, 07 Mar 2012 19:46:37 +0100 |
parents | |
children | 826448e34e80 |
files | LossyCom.c LossyCom.h |
diffstat | 2 files changed, 190 insertions(+), 0 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/LossyCom.c Wed Mar 07 19:46:37 2012 +0100 1.3 @@ -0,0 +1,101 @@ 1.4 + 1.5 +/* 1.6 + * For a detailed description see header file. 1.7 + */ 1.8 + 1.9 +#include "LossyCom.h" 1.10 + 1.11 +#include "VMS_Implementations/VMS_impl/vmalloc.h" 1.12 + 1.13 +/* 1.14 + * Initializes the central exchange structure. 1.15 + * Allocates memory to fit the number of endpoints. 1.16 + * Returns NULL if an error occurs. 1.17 + */ 1.18 +lossyCom__exchange_t* lossyCom__initialize(uint16_t numEndpoints) 1.19 +{ 1.20 + lossyCom__exchange_t* exchange; 1.21 + 1.22 + exchange = VMS_WL__malloc(sizeof(lossyCom__exchange_t)); 1.23 + if(exchange == NULL) 1.24 + return NULL; 1.25 + 1.26 + exchange->triggerCounter = 0; 1.27 + exchange->numEndpoints = numEndpoints; 1.28 + exchange->outboxArray = VMS_WL__malloc(sizeof(lossyCom__msg_t)*numEndpoints); 1.29 + if(exchange->outboxArray == NULL){ 1.30 + VMS_WL__free(exchange); 1.31 + return NULL; 1.32 + } 1.33 + 1.34 + return exchange; 1.35 +} 1.36 + 1.37 +void lossyCom__initialize_endpoint(lossyCom__endpoint_t* localEndpoint, 1.38 + lossyCom__exchange_t* centralExchange, 1.39 + lossyCom__endpointID_t endpointID, 1.40 + lossyCom__msgHandler* msgHandler) 1.41 +{ 1.42 + localEndpoint->localTriggerCopy = 0; 1.43 + localEndpoint->endpointID = endpointID; 1.44 + localEndpoint->centralExchangePtr = centralExchange; 1.45 + localEndpoint->msgHandler = msgHandler; 1.46 +} 1.47 + 1.48 +void inline lossyCom__broadcastMsg(lossyCom__endpoint_t* localEndpoint, 1.49 + lossyCom__msgBody_t msg) 1.50 +{ 1.51 + lossyCom__sendMsg(localEndpoint, 1.52 + BROADCAST_ID, 1.53 + msg); 1.54 +} 1.55 + 1.56 +void inline lossyCom__sendMsg(lossyCom__endpoint_t* localEndpoint, 1.57 + lossyCom__endpointID_t receiverEndpointID, 1.58 + lossyCom__msgBody_t msg) 1.59 +{ 1.60 + lossyCom__msg_t msgDraft; 1.61 + uint16_t triggerCopy; 1.62 + 1.63 + msgDraft = (0 | msg); 1.64 + msgDraft |= ((lossyCom__msg_t)receiverEndpointID << ENDPOINT_ID_SHIFT); 1.65 + 1.66 + triggerCopy = localEndpoint->centralExchangePtr->triggerCounter +1; 1.67 + msgDraft |= ((lossyCom__msg_t)triggerCopy << TRIGGER_SHIFT); 1.68 + 1.69 + //write msg to central exchange 1.70 + localEndpoint->centralExchangePtr->outboxArray[localEndpoint->endpointID] = 1.71 + msgDraft; 1.72 + 1.73 + localEndpoint->centralExchangePtr->triggerCounter = triggerCopy; 1.74 +} 1.75 + 1.76 +void inline lossyCom__receiveMsg(lossyCom__endpoint_t* localEndpoint) 1.77 +{ 1.78 + uint16_t remoteTriggerCopy; 1.79 + lossyCom__endpointID_t senderEndpointID; 1.80 + lossyCom__msg_t msgCopy; 1.81 + uint16_t msgTrigger; 1.82 + lossyCom__msgBody_t msgBody; 1.83 + 1.84 + senderEndpointID = 0; 1.85 + remoteTriggerCopy = localEndpoint->centralExchangePtr->triggerCounter; 1.86 + //new message arrived if trigger counter is higher than the last time read 1.87 + while(senderEndpointID < localEndpoint->centralExchangePtr->numEndpoints) 1.88 + { 1.89 + if(senderEndpointID != localEndpoint->endpointID) 1.90 + { 1.91 + msgCopy = localEndpoint->centralExchangePtr->outboxArray[senderEndpointID]; 1.92 + msgTrigger = 0xFFFF & (msgCopy >> TRIGGER_SHIFT); 1.93 + if(msgTrigger > localEndpoint->localTriggerCopy && 1.94 + msgTrigger <= remoteTriggerCopy) 1.95 + { 1.96 + msgBody = 0xFFFFFFFF & msgCopy; 1.97 + (*(localEndpoint->msgHandler))(senderEndpointID, msgBody); 1.98 + } 1.99 + } 1.100 + senderEndpointID++; 1.101 + } 1.102 + //save last parsed msg 1.103 + localEndpoint->localTriggerCopy = remoteTriggerCopy; 1.104 +} 1.105 \ No newline at end of file
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/LossyCom.h Wed Mar 07 19:46:37 2012 +0100 2.3 @@ -0,0 +1,89 @@ 2.4 +/* 2.5 + * This describes a best effort communication channel. Mainly designed for 2.6 + * communication between Core-Controllers, so they can inform each other about 2.7 + * their status and pass work around. This is to avoid idling cores that occupy 2.8 + * the Masterlock. 2.9 + * 2.10 + * The communication entities poll a central trigger counter that increases when 2.11 + * there are new messages. However it's not guaranteed that the increase of the 2.12 + * counter reflects the number of new messages. 2.13 + * 2.14 + * The messages consist of a single 64bit word so it can be written in one 2.15 + * operation and therefore avoiding inconsistent messages. 2.16 + * 2.17 + * The messages are handled with a handler function provided by the receiver 2.18 + * when acquiring the communication endpoint. 2.19 + * 2.20 + * The message passing is handled by a central struct that is used w/o any lock. 2.21 + * It consists of a array messages for the number of communicating entities. The 2.22 + * number of entities is fixed at initialization. 2.23 + */ 2.24 + 2.25 +#include <stdint.h> 2.26 + 2.27 + 2.28 + 2.29 +/*********************************** 2.30 + * Type Definitions 2.31 + ***********************************/ 2.32 + 2.33 +#define BROADCAST_ID 65535 2.34 +#define ENDPOINT_ID_SHIFT 32 2.35 +#define TRIGGER_SHIFT 48 2.36 + 2.37 +/* 2.38 + * A message consists of the 2.39 + * 16bit saved trigger value when the message was sent 2.40 + * 16bit receiver identifier, this is the index of the slot in the array 2.41 + * 65536 is the broadcast identifier 2.42 + * 32bit message body, the message format is defined by the endpoints 2.43 + */ 2.44 +typedef uint64_t lossyCom__msg_t; 2.45 +typedef uint32_t lossyCom__msgBody_t; 2.46 +typedef uint16_t lossyCom__endpointID_t; 2.47 + 2.48 +/* 2.49 + * Message Handler that has two arguments the sender endpoint ID and the Msg 2.50 + * Example: 2.51 + * void handler_func(lossyCom__endpointID_t senderID, lossyCom__msgBody_t msg); 2.52 + */ 2.53 +typedef void (*lossyCom__msgHandler) (lossyCom__endpointID_t, lossyCom__msgBody_t); 2.54 + 2.55 +/* 2.56 + * Central communication structure. 2.57 + */ 2.58 +typedef struct{ 2.59 + uint16_t triggerCounter; 2.60 + uint16_t numEndpoints; 2.61 + lossyCom__msg_t* outboxArray; 2.62 +}lossyCom__exchange_t; 2.63 + 2.64 +/* 2.65 + * Endpoint data structure. 2.66 + */ 2.67 +typedef struct { 2.68 + uint16_t localTriggerCopy; 2.69 + lossyCom__endpointID_t endpointID; 2.70 + lossyCom__exchange_t* centralExchangePtr; 2.71 + lossyCom__msgHandler* msgHandler; 2.72 +} lossyCom__endpoint_t; 2.73 + 2.74 +/*********************************** 2.75 + * Function Declarations 2.76 + ***********************************/ 2.77 + 2.78 +lossyCom__exchange_t* lossyCom__initialize(uint16_t numEndpoints); 2.79 + 2.80 +void lossyCom__initialize_endpoint(lossyCom__endpoint_t* localEndpoint, 2.81 + lossyCom__exchange_t* exchange, 2.82 + lossyCom__endpointID_t endpointID, 2.83 + lossyCom__msgHandler* msgHandler); 2.84 + 2.85 +void lossyCom__broadcastMsg(lossyCom__endpoint_t* localEndpoint, 2.86 + lossyCom__msgBody_t msg); 2.87 + 2.88 +void lossyCom__sendMsg(lossyCom__endpoint_t* localEndpoint, 2.89 + lossyCom__endpointID_t receiverEndpointID, 2.90 + lossyCom__msgBody_t msg); 2.91 + 2.92 +void lossyCom__receiveMsg(lossyCom__endpoint_t* localEndpoint); 2.93 \ No newline at end of file