52 lines
1.1 KiB
C
52 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <pthread.h>
|
|
#include <unistd.h>
|
|
|
|
/* Non standard include */
|
|
#include <common_comm.h>
|
|
#include <specific_comm.h>
|
|
|
|
|
|
__thread uintptr_t *shared_space;
|
|
__thread int cons_idx = 0;
|
|
__thread int prod_idx = 0;
|
|
|
|
struct communication_assoc *create_comm_assoc(void)
|
|
{
|
|
struct communication_assoc *assoc;
|
|
|
|
shared_space = (uintptr_t *) malloc(SHARED_SPACE_SIZE * sizeof(uintptr_t));
|
|
assoc = (struct communication_assoc *) malloc(sizeof(struct communication_assoc));
|
|
assoc->tid = pthread_self();
|
|
assoc->shared_space = shared_space;
|
|
assoc->cons_idx = &cons_idx;
|
|
assoc->prod_idx = &prod_idx;
|
|
return assoc;
|
|
}
|
|
|
|
void reception(void (*on_receive)(uintptr_t))
|
|
{
|
|
wait_initialization();
|
|
/* printf("Activate the consumer...\n"); */
|
|
while(cont)
|
|
{
|
|
struct communication_assoc *cur;
|
|
|
|
discover_new_producers();
|
|
cur = assoc_root.next;
|
|
while(cur != &assoc_root)
|
|
{
|
|
int cons_idx;
|
|
|
|
for(cons_idx = *cur->cons_idx; cons_idx != *cur->prod_idx; cons_idx = (cons_idx + 1) % SHARED_SPACE_SIZE, *cur->cons_idx = cons_idx)
|
|
{
|
|
uintptr_t tmp;
|
|
tmp = cur->shared_space[cons_idx];
|
|
on_receive(tmp);
|
|
}
|
|
cur = cur->next;
|
|
}
|
|
}
|
|
}
|