rt_benchs/communication_techniques/include/mcringbuffer_comm.h

71 lines
1.5 KiB
C

#ifndef _SPECIFIC_COMM_H_
#define _SPECIFIC_COMM_H_ 1
/* Non standard include */
#include <commtech.h>
#define SHARED_SPACE_SIZE (250 * CACHE_LINE_SIZE) // Check with batchSize
#define SHARED_SPACE_VOIDPTR (SHARED_SPACE_SIZE / sizeof(void *))
struct control
{
volatile unsigned int read;
volatile unsigned int write;
};
struct cons
{
unsigned int localWrite;
unsigned int nextRead;
unsigned int rBatch;
};
struct prod
{
unsigned int localRead;
unsigned int nextWrite;
unsigned int wBatch;
};
struct channel
{
struct control ctrl __attribute__ ((aligned (CACHE_LINE_SIZE)));
struct prod prod __attribute__ ((aligned (CACHE_LINE_SIZE)));
struct cons cons __attribute__ ((aligned (CACHE_LINE_SIZE)));
void * volatile *shared_space __attribute__ ((aligned (CACHE_LINE_SIZE))); // Align only to isolate cons on its cache line
};
__BEGIN_DECLS
extern const unsigned int batchSize;
static inline void send(struct channel *channel, void **addr)
{
while (1)
{
unsigned int afterNextWrite;
afterNextWrite = (channel->prod.nextWrite + 1) % SHARED_SPACE_VOIDPTR;
if (afterNextWrite == channel->prod.localRead)
{
if (afterNextWrite == channel->ctrl.read)
continue;
channel->prod.localRead = channel->ctrl.read;
}
channel->shared_space[channel->prod.nextWrite] = addr;
channel->prod.nextWrite = afterNextWrite;
channel->prod.wBatch++;
if (channel->prod.wBatch >= batchSize)
{
channel->ctrl.write = channel->prod.nextWrite;
channel->prod.wBatch = 0;
}
break;
}
}
__END_DECLS
#endif