1 |
48 |
alirezamon |
/*
|
2 |
|
|
* Copyright (c) 2009-2014, IETR/INSA of Rennes
|
3 |
|
|
* All rights reserved.
|
4 |
|
|
*
|
5 |
|
|
* Redistribution and use in source and binary forms, with or without
|
6 |
|
|
* modification, are permitted provided that the following conditions are met:
|
7 |
|
|
*
|
8 |
|
|
* * Redistributions of source code must retain the above copyright notice,
|
9 |
|
|
* this list of conditions and the following disclaimer.
|
10 |
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
11 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
12 |
|
|
* and/or other materials provided with the distribution.
|
13 |
|
|
* * Neither the name of the IETR/INSA of Rennes nor the names of its
|
14 |
|
|
* contributors may be used to endorse or promote products derived from this
|
15 |
|
|
* software without specific prior written permission.
|
16 |
|
|
*
|
17 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
18 |
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
19 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
20 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
21 |
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
22 |
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
23 |
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
24 |
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
25 |
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
|
26 |
|
|
* WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
27 |
|
|
* SUCH DAMAGE.
|
28 |
|
|
*/
|
29 |
|
|
|
30 |
|
|
/**
|
31 |
|
|
* Ring-buffer FIFO structure
|
32 |
|
|
* Lock-free and cache-efficient implementation
|
33 |
|
|
* Supports 1 producer - N consumers
|
34 |
|
|
*/
|
35 |
|
|
|
36 |
|
|
#define UNUSED_VAR __attribute__ ((unused))
|
37 |
|
|
|
38 |
|
|
typedef struct {
|
39 |
|
|
volatile char padding0[CACHELINE_SIZE]; /** Memory padding */
|
40 |
|
|
unsigned int* read_inds; /** Current reading positions */
|
41 |
|
|
volatile char padding1[CACHELINE_SIZE]; /** Memory padding */
|
42 |
|
|
unsigned int write_ind; /** Current writing position */
|
43 |
|
|
volatile char padding2[CACHELINE_SIZE]; /** Memory padding */
|
44 |
|
|
T *contents; /** Buffer containing the FIFO's elements */
|
45 |
|
|
} FIFO_T(T);
|
46 |
|
|
|
47 |
|
|
UNUSED_VAR static int FIFO_GET_NUM_TOKENS(T)(FIFO_T(T) *fifo, int reader_id) {
|
48 |
|
|
return fifo->write_ind - fifo->read_inds[reader_id];
|
49 |
|
|
}
|
50 |
|
|
|
51 |
|
|
UNUSED_VAR static int FIFO_GET_ROOM(T)(FIFO_T(T) *fifo, int nb_readers, int size) {
|
52 |
|
|
int i;
|
53 |
|
|
int num_tokens, max_num_tokens = 0;
|
54 |
|
|
|
55 |
|
|
for (i = 0; i < nb_readers; i++) {
|
56 |
|
|
num_tokens = fifo->write_ind - fifo->read_inds[i];
|
57 |
|
|
max_num_tokens = max_num_tokens > num_tokens ? max_num_tokens : num_tokens;
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
return size - max_num_tokens;
|
61 |
|
|
}
|
62 |
|
|
|