Initial VES for DANOS vRouter
[demo.git] / vnfs / VESreporting_vFW5.0_DANOS / evel / evel-library / code / evel_library / ring_buffer.h
1 /*************************************************************************//**
2  *
3  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and 
14  * limitations under the License.
15  *
16  ****************************************************************************/
17
18 #ifndef RING_BUFFER_INCLUDED
19 #define RING_BUFFER_INCLUDED
20
21 /**************************************************************************//**
22  * @file
23  * Ring  buffer to handle message requests.
24  *
25  ****************************************************************************/
26
27 #include <pthread.h>
28
29 /**************************************************************************//**
30  * Ring buffer structure.
31  *****************************************************************************/
32 typedef struct ring_buffer
33 {
34     int size;
35     int next_write;
36     int next_read;
37     void ** ring;
38     pthread_cond_t ring_cv;
39     pthread_mutex_t ring_mutex;
40 } ring_buffer;
41
42 /**************************************************************************//**
43  * Ring buffer initialization.
44  *
45  * Initialize the buffer supplied to the specified size.
46  *
47  * @param   buffer  Pointer to the ring-buffer to be initialized.
48  * @param   size    How many elements to be stored in the ring-buffer.
49  *
50  * @returns Nothing
51 ******************************************************************************/
52 void ring_buffer_initialize(ring_buffer * buffer, int size);
53
54 /**************************************************************************//**
55  * Read an element from a ring_buffer.
56  *
57  * Reads an element from the ring_buffer, advancing the next-read position.
58  * Operation is synchronized and therefore MT-safe.  Blocks if no data is
59  * available.
60  *
61  * @param   buffer  Pointer to the ring-buffer to be read.
62  *
63  * @returns Pointer to the element read from the buffer.
64 ******************************************************************************/
65 void * ring_buffer_read(ring_buffer * buffer);
66
67 /**************************************************************************//**
68  * Write an element into a ring_buffer.
69  *
70  * Writes an element into the ring_buffer, advancing the next-write position.
71  * Operation is synchronized and therefore MT-safe.  Fails if the buffer is
72  * full without blocking.
73  *
74  * @param   buffer  Pointer to the ring-buffer to be written.
75  * @param   msg     Pointer to data to be stored in the ring_buffer.
76  *
77  * @returns Number of items written.
78  * @retval  1       The data was written successfully.
79  * @retval  0       The ring_buffer was full so no data written.
80 ******************************************************************************/
81 int ring_buffer_write(ring_buffer * buffer, void * msg);
82
83 /**************************************************************************//**
84  * Tests whether there is data in the ring_buffer.
85  *
86  * Tests whether there is currently data in the ring_buffer without blocking.
87  *
88  * @param   buffer  Pointer to the ring-buffer to be tested.
89  *
90  * @returns Whether there is data in the ring_buffer.
91  * @retval  0       There isn't any data in the ring_buffer.
92  * @retval  1       There is data in the ring_buffer.
93 ******************************************************************************/
94 int ring_buffer_is_empty(ring_buffer * buffer);
95
96 #endif