rt_benchs/communication_techniques/include/mcringbuffer_common.h

95 lines
2.7 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 _MCRINGBUFFER_COMMON_H_
#define _MCRINGBUFFER_COMMON_H_ 1
/* Non standard include */
#include <commtech.h>
#ifndef SHARED_SPACE_SIZE
#define SHARED_SPACE_SIZE (250 * CACHE_LINE_SIZE) // Check with batchSize
#endif
#define SHARED_SPACE_VOIDPTR (SHARED_SPACE_SIZE / sizeof(void *))
struct control
{
volatile unsigned int read;
volatile unsigned int write;
};
struct cons
{
unsigned int localWrite;
unsigned int nextRead;
unsigned int rBatch;
};
struct prod
{
unsigned int localRead;
unsigned int nextWrite;
unsigned int wBatch;
};
struct channel
{
struct control ctrl __attribute__ ((aligned (CACHE_LINE_SIZE)));
struct prod prod __attribute__ ((aligned (CACHE_LINE_SIZE)));
struct cons cons __attribute__ ((aligned (CACHE_LINE_SIZE)));
void * volatile *shared_space __attribute__ ((aligned (CACHE_LINE_SIZE))); // Align only to isolate cons on its cache line
};
__BEGIN_DECLS
extern const unsigned int batchSize;
static inline void send(struct channel *channel, void **addr)
{
while (1)
{
unsigned int afterNextWrite;
afterNextWrite = (channel->prod.nextWrite + 1) % SHARED_SPACE_VOIDPTR;
if (afterNextWrite == channel->prod.localRead)
{
if (afterNextWrite == channel->ctrl.read)
continue;
channel->prod.localRead = channel->ctrl.read;
}
channel->shared_space[channel->prod.nextWrite] = addr;
channel->prod.nextWrite = afterNextWrite;
channel->prod.wBatch++;
if (channel->prod.wBatch >= batchSize)
{
channel->ctrl.write = channel->prod.nextWrite;
channel->prod.wBatch = 0;
}
break;
}
}
__END_DECLS
#endif