1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FILLP_LF_RING_H
17 #define FILLP_LF_RING_H
18 #include <stdlib.h>
19 #include <string.h>
20 #include <stdio.h>
21 
22 #include "utils.h"
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define LF_RING_NAMESIZE 32
29 
30 #ifndef LF_RING_PAUSE_REP_COUNT
31 /** Yield after pause num of times, no yield if FTDP_RING_PAUSE_REP not defined. */
32 #define LF_RING_PAUSE_REP_COUNT 0
33 #endif
34 struct FillpLfRing {
35     char name[LF_RING_NAMESIZE];
36 
37     /* Ring producer status */
38     struct _prod {
39         volatile FILLP_ULONG head;
40         volatile FILLP_ULONG tail;
41     } prod;
42 
43     struct _cons {
44         volatile FILLP_ULONG head;
45         volatile FILLP_ULONG tail;
46     } cons;
47 
48     FILLP_ULONG size;
49     FILLP_BOOL prodSafe;
50     FILLP_BOOL consSafe;
51     FILLP_UINT8 padd[6];
52     /* Should be last element of the structure. DO NOT reorder or change */
53     void *ringCache[1]; /* Data */
54 };
55 
56 /*******************************************************************************
57     Function    : FillpLfRingInit
58 
59     Description : This function will be invoked to init the lock-free queue.
60 
61     Input       : ring - lock-free queue to be initialized
62                   name - name to be added to the lock-free queue
63                   size - size of lock-free queue
64 
65     Output      : ring - initialized lock-free queue
66 
67     Return      : None
68 *******************************************************************************/
69 void FillpLfRingInit(struct FillpLfRing *ring, char *name, size_t size);
70 
71 /*******************************************************************************
72     Function    : FillpLfRingCalMemSize
73 
74     Description : This function will be invoked to calculate the memory size
75                   required for lock-free queue, based on size.
76 
77     Input       : size - no of items of lock-free queue
78 
79     Output      : None
80 
81     Return      : Memory size of lock-free queue
82 *******************************************************************************/
83 size_t FillpLfRingCalMemSize(size_t size);
84 
85 /* multi-consumers safe */
86 /*******************************************************************************
87     Function    : FillpLfRingMpEnqueue
88 
89     Description : This function will be invoked to add data to lock-free queue.
90 
91     Input       : ring - lock-free queue to be initialized
92                   dataTable - data table to be added to lock-free queue
93                   count -
94 
95     Output      : None
96 
97     Return      : Memory size of lock-free queue
98 *******************************************************************************/
99 FillpErrorType FillpLfRingMpEnqueue(struct FillpLfRing *ring, void **dataTable, FILLP_UINT count);
100 
101 /* multi-consumers safe */
102 /*******************************************************************************
103     Function    : FillpLfRingMcDequeue
104 
105     Description : This function will be invoked to remove a data to lock-free
106                   queue.
107 
108     Input       : ring - lock-free queue to be initialized
109                   dataTable - data table to be added to lock-free queue
110                   count -
111 
112     Output      : None
113 
114     Return      : ERR_OK if success, or Error codes on failures
115 *******************************************************************************/
116 FILLP_INT FillpLfRingMcDequeue(struct FillpLfRing *ring, void **dataTable, FILLP_UINT count);
117 
118 FILLP_INT FillpRingEmpty(const struct FillpLfRing *r);
119 
120 
121 void FillpLfRingSetConsSafe(struct FillpLfRing *ring, FILLP_BOOL safe);
122 
123 void FillpLfRingSetProdSafe(struct FillpLfRing *ring, FILLP_BOOL safe);
124 
125 FILLP_INT FillpRingFreeEntries(const struct FillpLfRing *r);
126 
127 FILLP_ULONG FillpRingValidOnes(struct FillpLfRing *r);
128 
129 #ifdef __cplusplus
130 }
131 #endif
132 #endif /* FILLP_LF_RING_H */
133