rt_benchs/communication_techniques/src/communication/lamport.c

102 lines
1.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
/* Non standard include */
#include <commtech.h>
#include <specific_comm.h>
__thread struct comm *comm;
int init_library(void)
{
return 0;
}
int finalize_library(void)
{
return 0;
}
void *create_comm_channel(void)
{
struct comm *comm;
if (!posix_memalign((void *) &comm, CACHE_LINE_SIZE, sizeof(struct comm)))
{
if (!posix_memalign((void *) &comm->shared_space, CACHE_LINE_SIZE, SHARED_SPACE_SIZE))
{
comm->cons_idx = 0;
comm->prod_idx = 0;
return comm;
}
else
free(comm);
}
return NULL;
}
int destroy_comm_channel(void *comm)
{
free((void *) ((struct comm *) comm)->shared_space);
free(comm);
return 0;
}
int init_producer_thread(void *comm_param)
{
comm = (struct comm *) comm_param;
return 0;
}
int finalize_producer_thread(void *unused)
{
comm = NULL;
return 0;
}
int init_consumer_thread(void *comm_param)
{
comm = (struct comm *) comm_param;
return 0;
}
int finalize_consumer_thread(void *unused)
{
comm = NULL;
return 0;
}
void *recv_one_data(void)
{
int cons_idx;
void *result;
cons_idx = comm->cons_idx;
while (cons_idx == comm->prod_idx);
result = (void *) comm->shared_space[cons_idx];
cons_idx = (cons_idx + 1) % SHARED_SPACE_VOIDPTR;
comm->cons_idx = cons_idx;
return result;
}
ssize_t recv_some_data(void **buf, size_t count)
{
int n, cons_idx;
n = 0;
for(cons_idx = comm->cons_idx; cons_idx != comm->prod_idx; cons_idx = (cons_idx + 1) % SHARED_SPACE_VOIDPTR, comm->cons_idx = cons_idx)
{
/*
* The behaviour of this is not documented but we know
* the values inside buf won't change during this affectation
*/
*buf++ = (void *) comm->shared_space[cons_idx];
if (++n == count)
break;
}
return n;
}