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>
|
|
|
|
|
|
|
|
|
|
|
|
void *create_comm_channel(void)
|
|
|
|
{
|
2011-04-11 16:18:49 +02:00
|
|
|
struct channel *channel;
|
2010-09-22 18:15:57 +02:00
|
|
|
|
2011-04-11 16:18:49 +02:00
|
|
|
if (!posix_memalign((void *) &channel, CACHE_LINE_SIZE, sizeof(struct channel)))
|
2010-09-22 18:15:57 +02:00
|
|
|
{
|
2011-04-11 16:18:49 +02:00
|
|
|
if (!posix_memalign((void *) &channel->shared_space, CACHE_LINE_SIZE, SHARED_SPACE_SIZE))
|
2010-09-22 18:15:57 +02:00
|
|
|
{
|
2011-04-11 16:18:49 +02:00
|
|
|
channel->cons_idx = 0;
|
|
|
|
channel->prod_idx = 0;
|
|
|
|
return channel;
|
2010-09-22 18:15:57 +02:00
|
|
|
}
|
|
|
|
else
|
2011-04-11 16:18:49 +02:00
|
|
|
free(channel);
|
2010-09-22 18:15:57 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-04-11 16:18:49 +02:00
|
|
|
int destroy_comm_channel(void *channel)
|
2010-09-22 18:15:57 +02:00
|
|
|
{
|
2011-04-11 16:18:49 +02:00
|
|
|
free((void *) ((struct channel *) channel)->shared_space);
|
|
|
|
free(channel);
|
2010-09-22 18:15:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-11 16:18:49 +02:00
|
|
|
void *recv_one_data(struct channel *channel)
|
2010-09-22 18:15:57 +02:00
|
|
|
{
|
2011-05-10 13:43:55 +02:00
|
|
|
unsigned int cons_idx;
|
2010-09-22 18:15:57 +02:00
|
|
|
void *result;
|
|
|
|
|
2011-04-11 16:18:49 +02:00
|
|
|
cons_idx = channel->cons_idx;
|
|
|
|
while (cons_idx == channel->prod_idx);
|
|
|
|
result = channel->shared_space[cons_idx];
|
2010-09-22 18:15:57 +02:00
|
|
|
cons_idx = (cons_idx + 1) % SHARED_SPACE_VOIDPTR;
|
2011-04-11 16:18:49 +02:00
|
|
|
channel->cons_idx = cons_idx;
|
2010-09-22 18:15:57 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-04-11 16:18:49 +02:00
|
|
|
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, cons_idx;
|
2010-09-22 18:15:57 +02:00
|
|
|
|
|
|
|
n = 0;
|
2011-04-11 16:18:49 +02:00
|
|
|
for(cons_idx = channel->cons_idx; cons_idx != channel->prod_idx; cons_idx = (cons_idx + 1) % SHARED_SPACE_VOIDPTR, channel->cons_idx = cons_idx)
|
2010-09-22 18:15:57 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The behaviour of this is not documented but we know
|
|
|
|
* the values inside buf won't change during this affectation
|
|
|
|
*/
|
2011-04-11 16:18:49 +02:00
|
|
|
*buf++ = channel->shared_space[cons_idx];
|
2010-09-22 18:15:57 +02:00
|
|
|
if (++n == count)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|