#include #include #include #include /* Non standard include */ #include #include __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) { void *result; int cons_idx, prod_idx; cons_idx = comm->cons_idx; prod_idx = comm->prod_idx; if (cons_idx == prod_idx) while(prod_idx == comm->prod_idx); /* * The behaviour of this is not documented but we know the * values inside shared_space won't change during this * affectation */ 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, prod_idx; n = 0; cons_idx = comm->cons_idx; do { prod_idx = comm->prod_idx; for(; cons_idx != prod_idx; cons_idx = (cons_idx + 1) % SHARED_SPACE_VOIDPTR) { /* * 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) { comm->cons_idx = cons_idx; return n; } } } while (prod_idx != comm->prod_idx); comm->cons_idx = cons_idx; return n; }