rt_benchs/communication_techniques/src/communication/mcringbuffer.c

93 lines
2.1 KiB
C
Raw Normal View History

2010-09-22 18:15:57 +02:00
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
/* Non standard include */
#include <commtech.h>
#include <specific_comm.h>
2011-05-10 13:43:55 +02:00
const unsigned int batchSize = 64; // Check with SHARED_SPACE_SIZE
2010-09-22 18:15:57 +02:00
void *create_comm_channel(void)
{
struct channel *channel;
2010-09-22 18:15:57 +02:00
if (!posix_memalign((void *) &channel, CACHE_LINE_SIZE, sizeof(struct channel)))
2010-09-22 18:15:57 +02:00
{
if (!posix_memalign((void *) &channel->shared_space, CACHE_LINE_SIZE, SHARED_SPACE_SIZE))
2010-09-22 18:15:57 +02:00
{
channel->ctrl.read = 0;
channel->ctrl.write = 0;
channel->cons.localWrite = 0;
channel->cons.nextRead = 0;
channel->cons.rBatch = 0;
channel->prod.localRead = 0;
channel->prod.nextWrite = 0;
channel->prod.wBatch = 0;
return channel;
2010-09-22 18:15:57 +02:00
}
else
free(channel);
2010-09-22 18:15:57 +02:00
}
return NULL;
}
int destroy_comm_channel(void *channel)
2010-09-22 18:15:57 +02:00
{
free((void *) ((struct channel *) channel)->shared_space);
free(channel);
2010-09-22 18:15:57 +02:00
return 0;
}
void *recv_one_data(struct channel *channel)
2010-09-22 18:15:57 +02:00
{
void *result;
while (1)
{
if (channel->cons.nextRead == channel->cons.localWrite)
2010-09-22 18:15:57 +02:00
{
if (channel->cons.nextRead == channel->ctrl.write)
2010-09-22 18:15:57 +02:00
continue;
channel->cons.localWrite = channel->ctrl.write;
2010-09-22 18:15:57 +02:00
}
result = channel->shared_space[channel->cons.nextRead];
channel->cons.nextRead = (channel->cons.nextRead + 1) % SHARED_SPACE_VOIDPTR;
channel->cons.rBatch++;
if (channel->cons.rBatch >= batchSize)
2010-09-22 18:15:57 +02:00
{
channel->ctrl.read = channel->cons.nextRead;
channel->cons.rBatch = 0;
2010-09-22 18:15:57 +02:00
}
break;
}
return result;
}
ssize_t recv_some_data(struct channel *channel, void **buf, size_t count)
2010-09-22 18:15:57 +02:00
{
2011-05-10 13:43:55 +02:00
unsigned int n;
2010-09-22 18:15:57 +02:00
for(n = 0; n < count; n++)
{
if (channel->cons.nextRead == channel->cons.localWrite)
2010-09-22 18:15:57 +02:00
{
if (channel->cons.nextRead == channel->ctrl.write)
2010-09-22 18:15:57 +02:00
break;
channel->cons.localWrite = channel->ctrl.write;
2010-09-22 18:15:57 +02:00
}
*buf++ = channel->shared_space[channel->cons.nextRead];
channel->cons.nextRead = (channel->cons.nextRead + 1) % SHARED_SPACE_VOIDPTR;
channel->cons.rBatch++;
if (channel->cons.rBatch >= batchSize)
2010-09-22 18:15:57 +02:00
{
channel->ctrl.read = channel->cons.nextRead;
channel->cons.rBatch = 0;
2010-09-22 18:15:57 +02:00
}
}
return n;
}