#ifndef _SPECIFIC_COMM_H_ #define _SPECIFIC_COMM_H_ 1 /* Non standard include */ #include #define SHARED_SPACE_SIZE (2 * BUF_SIZE) #define SHARED_SPACE_VOIDPTR (SHARED_SPACE_SIZE / sizeof(void *)) struct control { volatile int read; volatile int write; }; struct cons { int localWrite; int nextRead; int rBatch; }; struct prod { int localRead; int nextWrite; int wBatch; }; struct comm { struct control ctrl __attribute__ ((aligned (CACHE_LINE_SIZE))); struct prod prod __attribute__ ((aligned (CACHE_LINE_SIZE))); struct cons cons __attribute__ ((aligned (CACHE_LINE_SIZE))); void * volatile *shared_space __attribute__ ((aligned (CACHE_LINE_SIZE))); // Align only to isolate cons on its cache line }; __BEGIN_DECLS extern __thread struct comm *comm; extern const int batchSize; static inline void send(void **addr) { while (1) { int afterNextWrite = (comm->prod.nextWrite + 1) % SHARED_SPACE_VOIDPTR; if (afterNextWrite == comm->prod.localRead) { if (afterNextWrite == comm->ctrl.read) continue; comm->prod.localRead = comm->ctrl.read; } comm->shared_space[comm->prod.nextWrite] = addr; comm->prod.nextWrite = afterNextWrite; comm->prod.wBatch++; if (comm->prod.wBatch >= batchSize) { comm->ctrl.write = comm->prod.nextWrite; comm->prod.wBatch = 0; } break; } } __END_DECLS #endif