2013-04-22 18:30:08 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009-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.
|
|
|
|
*/
|
|
|
|
|
2009-06-11 23:42:15 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
/* Non standard include */
|
2009-06-18 14:48:07 +02:00
|
|
|
#include <commtech.h>
|
2009-06-11 23:42:15 +02:00
|
|
|
#include <specific_comm.h>
|
|
|
|
|
|
|
|
|
2010-09-22 18:15:57 +02:00
|
|
|
void *create_comm_channel(void)
|
|
|
|
{
|
2011-04-11 16:18:49 +02:00
|
|
|
struct channel *channel;
|
2010-09-22 18:15:57 +02:00
|
|
|
|
2011-04-11 16:18:49 +02:00
|
|
|
if (!posix_memalign((void *) &channel, CACHE_LINE_SIZE, sizeof(struct channel)))
|
2009-06-25 13:42:41 +02:00
|
|
|
{
|
2011-04-11 16:18:49 +02:00
|
|
|
if (!posix_memalign((void *) &channel->shared_space, CACHE_LINE_SIZE, SHARED_SPACE_SIZE))
|
2010-09-22 18:15:57 +02:00
|
|
|
{
|
2011-04-11 16:18:49 +02:00
|
|
|
channel->cons_idx = 0;
|
|
|
|
channel->prod_idx = 0;
|
|
|
|
return channel;
|
2010-09-22 18:15:57 +02:00
|
|
|
}
|
|
|
|
else
|
2011-04-11 16:18:49 +02:00
|
|
|
free(channel);
|
2009-06-25 13:42:41 +02:00
|
|
|
}
|
2010-09-22 18:15:57 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-01-30 19:40:50 +01:00
|
|
|
int end_producer(void *unused __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-11 16:18:49 +02:00
|
|
|
int destroy_comm_channel(void *channel)
|
2010-09-22 18:15:57 +02:00
|
|
|
{
|
2011-04-11 16:18:49 +02:00
|
|
|
free((void *) ((struct channel *) channel)->shared_space);
|
|
|
|
free(channel);
|
2010-09-22 18:15:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-11 16:18:49 +02:00
|
|
|
void *recv_one_data(struct channel *channel)
|
2010-09-22 18:15:57 +02:00
|
|
|
{
|
|
|
|
void *result;
|
2011-05-10 13:43:55 +02:00
|
|
|
unsigned int cons_idx, prod_idx;
|
2010-09-22 18:15:57 +02:00
|
|
|
|
2011-04-11 16:18:49 +02:00
|
|
|
cons_idx = channel->cons_idx;
|
|
|
|
prod_idx = channel->prod_idx;
|
2010-09-22 18:15:57 +02:00
|
|
|
if (cons_idx == prod_idx)
|
2011-04-11 16:18:49 +02:00
|
|
|
while(prod_idx == channel->prod_idx);
|
2010-09-22 18:15:57 +02:00
|
|
|
/*
|
|
|
|
* The behaviour of this is not documented but we know the
|
|
|
|
* values inside shared_space won't change during this
|
|
|
|
* affectation
|
|
|
|
*/
|
2011-04-11 16:18:49 +02:00
|
|
|
result = channel->shared_space[cons_idx];
|
2010-09-22 18:15:57 +02:00
|
|
|
cons_idx = (cons_idx + 1) % SHARED_SPACE_VOIDPTR;
|
2011-04-11 16:18:49 +02:00
|
|
|
channel->cons_idx = cons_idx;
|
2010-09-22 18:15:57 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-04-11 16:18:49 +02:00
|
|
|
ssize_t recv_some_data(struct channel *channel, void **buf, size_t count)
|
2010-09-22 18:15:57 +02:00
|
|
|
{
|
2011-05-10 13:43:55 +02:00
|
|
|
unsigned int n, cons_idx, prod_idx;
|
2010-09-22 18:15:57 +02:00
|
|
|
|
|
|
|
n = 0;
|
2011-04-11 16:18:49 +02:00
|
|
|
cons_idx = channel->cons_idx;
|
2010-09-22 18:15:57 +02:00
|
|
|
do
|
|
|
|
{
|
2011-04-11 16:18:49 +02:00
|
|
|
prod_idx = channel->prod_idx;
|
2010-09-22 18:15:57 +02:00
|
|
|
for(; cons_idx != prod_idx; cons_idx = (cons_idx + 1) % SHARED_SPACE_VOIDPTR)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The behaviour of this is not documented but we know
|
|
|
|
* the values inside buf won't change during this affectation
|
|
|
|
*/
|
2011-04-11 16:18:49 +02:00
|
|
|
*buf++ = channel->shared_space[cons_idx];
|
2010-09-22 18:15:57 +02:00
|
|
|
if (++n == count)
|
2009-06-11 23:42:15 +02:00
|
|
|
{
|
2011-04-11 16:18:49 +02:00
|
|
|
channel->cons_idx = cons_idx;
|
2010-09-22 18:15:57 +02:00
|
|
|
return n;
|
|
|
|
}
|
2009-06-11 23:42:15 +02:00
|
|
|
}
|
2011-04-11 16:18:49 +02:00
|
|
|
} while (prod_idx != channel->prod_idx);
|
|
|
|
channel->cons_idx = cons_idx;
|
2010-09-22 18:15:57 +02:00
|
|
|
return n;
|
2009-06-11 23:42:15 +02:00
|
|
|
}
|