2009-06-10 09:58:50 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
/* Non standard include */
|
2009-06-18 14:48:07 +02:00
|
|
|
#include <commtech.h>
|
|
|
|
#include <private_common.h>
|
2009-06-10 09:58:50 +02:00
|
|
|
#include <specific_comm.h>
|
|
|
|
|
|
|
|
|
2009-06-18 14:48:07 +02:00
|
|
|
__thread volatile void **shared_space;
|
2009-06-12 00:36:31 +02:00
|
|
|
__thread volatile int cons_idx = 0;
|
|
|
|
__thread volatile int prod_idx = 0;
|
2009-06-10 09:58:50 +02:00
|
|
|
|
2009-06-24 22:25:28 +02:00
|
|
|
int init_thread_comm(struct thread_comm *comm)
|
2009-06-10 09:58:50 +02:00
|
|
|
{
|
2009-06-25 14:01:18 +02:00
|
|
|
if (posix_memalign((void *) &shared_space, CACHE_LINE_SIZE, SHARED_SPACE_SIZE))
|
2009-06-25 13:42:41 +02:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Unable to allocate space for shared mem communication\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2009-06-18 14:48:07 +02:00
|
|
|
comm->shared_space = shared_space;
|
|
|
|
comm->cons_idx = &cons_idx;
|
|
|
|
comm->prod_idx = &prod_idx;
|
2009-06-24 22:25:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int end_thread_comm(void)
|
|
|
|
{
|
|
|
|
return 0;
|
2009-06-10 09:58:50 +02:00
|
|
|
}
|
|
|
|
|
2009-06-16 12:58:30 +02:00
|
|
|
void reception(void (*on_receive)(void *))
|
2009-06-10 09:58:50 +02:00
|
|
|
{
|
|
|
|
wait_initialization();
|
|
|
|
/* printf("Activate the consumer...\n"); */
|
|
|
|
while(cont)
|
|
|
|
{
|
2009-06-18 14:48:07 +02:00
|
|
|
int i;
|
2009-06-10 09:58:50 +02:00
|
|
|
|
2009-06-18 14:48:07 +02:00
|
|
|
for (i = 0; i < nb_prod; i++)
|
2009-06-10 09:58:50 +02:00
|
|
|
{
|
2009-06-11 23:42:15 +02:00
|
|
|
int cons_idx;
|
2009-06-10 09:58:50 +02:00
|
|
|
|
2009-06-18 14:48:07 +02:00
|
|
|
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)
|
2009-06-10 09:58:50 +02:00
|
|
|
{
|
2009-06-18 14:48:07 +02:00
|
|
|
/*
|
|
|
|
* 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]);
|
2009-06-11 23:42:15 +02:00
|
|
|
}
|
2009-06-10 09:58:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|