rt_benchs/communication_techniques/src/communication/pipe.c

114 lines
1.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdint.h>
/* Non standard include */
#include <commtech.h>
#include <specific_comm.h>
__thread struct comm *comm;
int init_library(void)
{
return 0;
}
int finalize_library(void)
{
return 0;
}
void *create_comm_channel(void)
{
struct comm *comm;
int flags;
comm = malloc(sizeof(comm));
if (comm != NULL)
{
if (!pipe(comm->pipefd))
{
flags = fcntl(comm->pipefd[READ_IDX], F_GETFL);
fcntl(comm->pipefd[READ_IDX], F_SETFL, flags | O_NONBLOCK);
return comm;
}
else
free(comm);
}
return NULL;
}
int destroy_comm_channel(void *comm)
{
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, **res_ptr;
int n, nb_read;
nb_read = 0;
res_ptr = &result;
do
{
n = read(comm->pipefd[READ_IDX], res_ptr, sizeof(void *));
if (n > 0)
{
nb_read += n;
res_ptr = (void **) ((uintptr_t) res_ptr + n);
}
} while (nb_read < sizeof(void *));
return result;
}
ssize_t recv_some_data(void **buf, size_t count)
{
int n, nb_read, nb_bytes;
nb_bytes = count * sizeof(void *);
nb_read = read(comm->pipefd[READ_IDX], buf, nb_bytes);
if (nb_read <= 0)
return 0;
buf = (void **) ((uintptr_t) buf + nb_read);
while (nb_read % sizeof(void *))
{
n = read(comm->pipefd[READ_IDX], buf, sizeof(void *) - (nb_read % sizeof(void *)));
if (n > 0)
{
nb_read += n;
buf = (void **) ((uintptr_t) buf + n);
}
}
return nb_read / sizeof(void *);
}