132 lines
2.4 KiB
C
132 lines
2.4 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <pthread.h>
|
|
#include <unistd.h>
|
|
|
|
/* Non standard include */
|
|
#include <commtech.h>
|
|
#include <specific_comm.h>
|
|
|
|
|
|
__thread struct comm *comm;
|
|
const int batchSize = 50; // Check with SHARED_SPACE_SIZE
|
|
|
|
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->ctrl.read = 0;
|
|
comm->ctrl.write = 0;
|
|
comm->cons.localWrite = 0;
|
|
comm->cons.nextRead = 0;
|
|
comm->cons.rBatch = 0;
|
|
comm->prod.localRead = 0;
|
|
comm->prod.nextWrite = 0;
|
|
comm->prod.wBatch = 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;
|
|
|
|
while (1)
|
|
{
|
|
if (comm->cons.nextRead == comm->cons.localWrite)
|
|
{
|
|
if (comm->cons.nextRead == comm->ctrl.write)
|
|
continue;
|
|
comm->cons.localWrite = comm->ctrl.write;
|
|
}
|
|
result = comm->shared_space[comm->cons.nextRead];
|
|
comm->cons.nextRead = (comm->cons.nextRead + 1) % SHARED_SPACE_VOIDPTR;
|
|
comm->cons.rBatch++;
|
|
if (comm->cons.rBatch >= batchSize)
|
|
{
|
|
comm->ctrl.read = comm->cons.nextRead;
|
|
comm->cons.rBatch = 0;
|
|
}
|
|
break;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
ssize_t recv_some_data(void **buf, size_t count)
|
|
{
|
|
int n;
|
|
|
|
for(n = 0; n < count; n++)
|
|
{
|
|
if (comm->cons.nextRead == comm->cons.localWrite)
|
|
{
|
|
if (comm->cons.nextRead == comm->ctrl.write)
|
|
break;
|
|
comm->cons.localWrite = comm->ctrl.write;
|
|
}
|
|
|
|
/*
|
|
* The behaviour of this is not documented but we know
|
|
* the values inside buf won't change during this affectation
|
|
*/
|
|
*buf++ = comm->shared_space[comm->cons.nextRead];
|
|
comm->cons.nextRead = (comm->cons.nextRead + 1) % SHARED_SPACE_VOIDPTR;
|
|
comm->cons.rBatch++;
|
|
if (comm->cons.rBatch >= batchSize)
|
|
{
|
|
comm->ctrl.read = comm->cons.nextRead;
|
|
comm->cons.rBatch = 0;
|
|
}
|
|
}
|
|
return n;
|
|
}
|