rt_benchs/communication_techniques/src/communication/asm_cache.c

80 lines
1.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
/* Non standard include */
#include <commtech.h>
#include <private_common.h>
#include <specific_comm.h>
__thread struct comm_channel channel;
int init_thread_comm(struct thread_comm *comm)
{
comm->receiver_idx = 0;
comm->channel = &channel;
comm->channel->state = 0;
comm->channel->idx = 0;
return 0;
}
int end_thread_comm(void)
{
return 0;
}
char *dstr="buffer transition\n";
void _swap_buffer()
{
asm volatile(".globl swap_buffer\n\t"
"swap_buffer:\n\t"
"1:\n\t"
"testl $1, %%gs:channel@NTPOFF + 2 *" toString(BUF_SIZE) "\n\t"
"jnz 1b\n\t"
"movl $1, %%gs:channel@NTPOFF + 2 *" toString(BUF_SIZE) "\n\t"
"jmp *%%eax\n\t"
: : "m"(dstr));
}
void reception(void (*on_receive)(void *))
{
wait_initialization();
/* printf("Activate the consumer...\n"); */
while (cont)
{
int i;
for (i = 0; i < nb_prod; i++)
{
if(tcomms[i].channel->state)
{
int j, n;
/*
* cur->receiver_idx point to the last cache
* line we have read. We go to the next cache
* line "+ (CACHE_LINE_SIZE >> 2)" (because
* the line is full of integer (2^2 octets)
* 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 = tcomms[i].receiver_idx;
n = tcomms[i].receiver_idx + (BUF_SIZE / sizeof(void *));
tcomms[i].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
*/
on_receive((void *) tcomms[i].channel->buf[j]);
}
tcomms[i].channel->state = 0;
}
}
}
}