98 lines
2.1 KiB
C
98 lines
2.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <pthread.h>
|
|
#include <unistd.h>
|
|
|
|
/* Non standard include */
|
|
#include <commtech.h>
|
|
#include <specific_comm.h>
|
|
|
|
|
|
const unsigned int batchSize = 64; // Check with SHARED_SPACE_SIZE
|
|
|
|
void *create_comm_channel(void)
|
|
{
|
|
struct channel *channel;
|
|
|
|
if (!posix_memalign((void *) &channel, CACHE_LINE_SIZE, sizeof(struct channel)))
|
|
{
|
|
if (!posix_memalign((void *) &channel->shared_space, CACHE_LINE_SIZE, SHARED_SPACE_SIZE))
|
|
{
|
|
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;
|
|
}
|
|
else
|
|
free(channel);
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
int end_producer(void *unused __attribute__ ((unused)))
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
int destroy_comm_channel(void *channel)
|
|
{
|
|
free((void *) ((struct channel *) channel)->shared_space);
|
|
free(channel);
|
|
return 0;
|
|
}
|
|
|
|
void *recv_one_data(struct channel *channel)
|
|
{
|
|
void *result;
|
|
|
|
while (1)
|
|
{
|
|
if (channel->cons.nextRead == channel->cons.localWrite)
|
|
{
|
|
if (channel->cons.nextRead == channel->ctrl.write)
|
|
continue;
|
|
channel->cons.localWrite = channel->ctrl.write;
|
|
}
|
|
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)
|
|
{
|
|
channel->ctrl.read = channel->cons.nextRead;
|
|
channel->cons.rBatch = 0;
|
|
}
|
|
break;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
ssize_t recv_some_data(struct channel *channel, void **buf, size_t count)
|
|
{
|
|
unsigned int n;
|
|
|
|
for(n = 0; n < count; n++)
|
|
{
|
|
if (channel->cons.nextRead == channel->cons.localWrite)
|
|
{
|
|
if (channel->cons.nextRead == channel->ctrl.write)
|
|
break;
|
|
channel->cons.localWrite = channel->ctrl.write;
|
|
}
|
|
|
|
*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)
|
|
{
|
|
channel->ctrl.read = channel->cons.nextRead;
|
|
channel->cons.rBatch = 0;
|
|
}
|
|
}
|
|
return n;
|
|
}
|