rt_benchs/communication_techniques/src/communication/shared_mem.c

57 lines
1.2 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
/* Non standard include */
#include <commtech.h>
#include <private_common.h>
#include <specific_comm.h>
__thread volatile void **shared_space;
__thread volatile int cons_idx = 0;
__thread volatile int prod_idx = 0;
int init_thread_comm(struct thread_comm *comm)
{
if (posix_memalign((void *) &shared_space, CACHE_LINE_SIZE, SHARED_SPACE_SIZE))
{
fprintf(stderr, "Unable to allocate space for shared mem communication\n");
return -1;
}
comm->shared_space = shared_space;
comm->cons_idx = &cons_idx;
comm->prod_idx = &prod_idx;
return 0;
}
int end_thread_comm(void)
{
return 0;
}
void reception(void (*on_receive)(void *))
{
wait_initialization();
/* printf("Activate the consumer...\n"); */
while(cont)
{
int i;
for (i = 0; i < nb_prod; i++)
{
int cons_idx;
for(cons_idx = *tcomms[i].cons_idx; cons_idx != *tcomms[i].prod_idx; cons_idx = (cons_idx + 1) % SHARED_SPACE_VOIDPTR, *tcomms[i].cons_idx = cons_idx)
{
/*
* The behaviour of this is not documented but we know
* the values inside buf won't change during this affectation
*/
on_receive((void *) tcomms[i].shared_space[cons_idx]);
}
}
}
}