rt_benchs/communication_techniques/src/communication/asm_cache.c

151 lines
3.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
/* Non standard include */
#include <commtech.h>
#include <specific_comm.h>
__thread union comm comm;
int init_library(void)
{
return 0;
}
int finalize_library(void)
{
return 0;
}
void *create_comm_channel(void)
{
struct cons *cons;
if (!posix_memalign((void *) &cons, CACHE_LINE_SIZE, sizeof(struct cons)))
{
cons->receiver_idx = 0;
if (!posix_memalign((void *) &cons->channel, CACHE_LINE_SIZE, sizeof(struct channel)))
{
cons->channel->state = 0;
cons->channel->idx = 0;
return cons;
}
else
free(cons);
}
return NULL;
}
int destroy_comm_channel(void *cons)
{
free(cons->channel);
free(cons);
return 0;
}
int init_producer_thread(void *cons)
{
comm.channel = cons->channel;
return 0;
}
int finalize_producer_thread(void *cons)
{
comm.channel = NULL;
return 0;
}
int init_consumer_thread(void *cons)
{
comm.cons = cons;
return 0;
}
int finalize_consumer_thread(void *cons)
{
comm.cons = NULL;
return 0;
}
void _swap_buffer()
{
asm volatile(".globl swap_buffer\n\t"
"swap_buffer:\n\t"
"1:\n\t"
"testl $1, %%gs:comm@NTPOFF + 2 *" toString(BUF_SIZE) "\n\t"
"jnz 1b\n\t"
"movl $1, %%gs:comm@NTPOFF + 2 *" toString(BUF_SIZE) "\n\t"
"jmp *%%eax\n\t"
: : "m"(dstr));
}
/*
* Copy at max count received data into buf
* @param buf The buffer in which received data must be copied into
* @return Number of data received and copied into buf
*
* @warning recv_one_data should not be used in conjonction of
* recv_some_data
*/
void *recv_one_data(void)
{
static __thread i;
void *result;
if (unlikely(i % (BUF_SIZE / sizeof(void *))))
while (!comm.cons->channel->state);
result = (void *) comm.cons->channel->buf[i++];
i %= (2 * BUF_SIZE) / sizeof(void *);
if (unlikely(i % (BUF_SIZE / sizeof(void *))))
comm.cons->channel->state = 0;
return result;
}
/*
* Copy at max count received data into buf
* @param buf The buffer in which received data must be copied into
* @return Number of data received and copied into buf
*
* @warning recv_some_data should not be used in conjonction of
* recv_one_data
* @warning count must be a multiple of BUF_SIZE
*/
ssize_t recv_some_data(void **buf, size_t count)
{
int i, n;
void **buf_ptr, **buf_end;
n = 0;
buf_ptr = buf;
buf_end = buf + count + 1;
while (comm.cons->channel->state && end != buf)
{
int j, n;
/*
* cur->receiver_idx point to the last buffer we have read.
* We go to the next cache line "+ (BUF_SIZE / sizeof(void *))"
* (because the line is full of void * and then if we are after
* the second cache line we correct the pointer to point to the
* first one (this is done by the modulo).
*/
j = comm.cons->receiver_idx;
n = comm.cons->receiver_idx + (BUF_SIZE / sizeof(void *));
comm.cons->receiver_idx = n % ((2 * BUF_SIZE) / sizeof(void *));
for(; j<n; j++)
{
/*
* The behaviour of this is not documented but we know
* the values inside buf won't change during this affectation
*/
*buf_ptr++ = (void *) comm.cons->channel->buf[j];
n++;
}
comm.cons->channel->state = 0;
}
return n;
}