rt_benchs/communication_techniques/include/csq_common.h

75 lines
2.2 KiB
C

/*
* Copyright (C) 2012 Thomas Preud'homme <thomas.preud-homme@lip6.fr>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef _CSQ_COMMON_COMM_H_
#define _CSQ_COMMON_COMM_H_ 1
/* Non standard include */
#include <commtech.h>
#ifndef BUF_SIZE
#define BUF_SIZE (64 * CACHE_LINE_SIZE)
#endif
#ifndef SLOTS
#define SLOTS 64 /* Value used in the article, section V.A */
#endif
#define SUB_SLOTS (BUF_SIZE / sizeof(void *))
struct lvl_2
{
void * volatile chunk[SUB_SLOTS];
volatile unsigned int flag : 1;
} __attribute__ ((aligned (CACHE_LINE_SIZE)));
struct channel
{
struct lvl_2 queue[SLOTS] __attribute__ ((aligned (CACHE_LINE_SIZE)));
unsigned int head __attribute__ ((aligned(CACHE_LINE_SIZE)));
unsigned int tail __attribute__ ((aligned(CACHE_LINE_SIZE)));
};
__BEGIN_DECLS
// TODO: Make it send only one data
static inline void send(struct channel *channel, void **addr)
{
static __thread unsigned int chkidx = 0;
// If all slots are full, spin
if (!chkidx)
while (channel->queue[channel->tail].flag);
// Enqueue a data item
channel->queue[channel->tail].chunk[chkidx++] = addr;
if (!(chkidx % SUB_SLOTS))
{
chkidx = 0;
channel->queue[channel->tail].flag = 1;
channel->tail = (channel->tail + 1) % SLOTS;
}
}
__END_DECLS
#endif