Initial VES for DANOS vRouter
[demo.git] / vnfs / VESreporting_vFW5.0_DANOS / evel / evel-library / code / evel_library / ring_buffer.c
1 /*************************************************************************//**
2  *
3  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
4  *
5  * Unless otherwise specified, all software contained herein is
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and 
15  * limitations under the License.
16  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
17  ****************************************************************************/
18 /**************************************************************************//**
19  * @file
20  * A ring buffer with multi-threaded synchronization.
21  *
22  ****************************************************************************/
23
24 #include <assert.h>
25 #include <malloc.h>
26
27 #include "ring_buffer.h"
28 #include "evel.h"
29
30 /**************************************************************************//**
31  * Ring buffer initialization.
32  *
33  * Initialize the buffer supplied to the specified size.
34  *
35  * @param   buffer  Pointer to the ring-buffer to be initialized.
36  * @param   size    How many elements to be stored in the ring-buffer.
37  *
38  * @returns Nothing
39 ******************************************************************************/
40 void ring_buffer_initialize(ring_buffer * buffer, int size)
41 {
42   int pthread_rc = 0;
43
44   EVEL_ENTER();
45
46   /***************************************************************************/
47   /* Check assumptions.                                                      */
48   /***************************************************************************/
49   assert(buffer != NULL);
50   assert(size > 0);
51
52   /***************************************************************************/
53   /* Initialize the synchronization objects.                                 */
54   /***************************************************************************/
55   pthread_rc = pthread_mutex_init(&buffer->ring_mutex, NULL);
56   assert(pthread_rc == 0);
57   pthread_rc = pthread_cond_init(&buffer->ring_cv, NULL);
58   assert(pthread_rc == 0);
59
60   /***************************************************************************/
61   /* Allocate the ring buffer itself.                                        */
62   /***************************************************************************/
63   buffer->ring = malloc(size * sizeof(void *));
64   assert(buffer->ring != NULL);
65
66   /***************************************************************************/
67   /* Initialize the ring as empty.                                           */
68   /***************************************************************************/
69   buffer->next_write = 0;
70   buffer->next_read = 0;
71   buffer->size = size;
72
73   EVEL_EXIT();
74 }
75
76 /**************************************************************************//**
77  * Read an element from a ring_buffer.
78  *
79  * Reads an element from the ring_buffer, advancing the next-read position.
80  * Operation is synchronized and therefore MT-safe.  Blocks if no data is
81  * available.
82  *
83  * @param   buffer  Pointer to the ring-buffer to be read.
84  *
85  * @returns Pointer to the element read from the buffer.
86 ******************************************************************************/
87 void * ring_buffer_read(ring_buffer * buffer)
88 {
89   void *msg = NULL;
90   EVEL_DEBUG("RBR: Ring buffer read");
91
92   pthread_mutex_lock(&buffer->ring_mutex);
93   while (1)
94   {
95     EVEL_DEBUG("RBR: got lock. NR=%d NW=%d",
96                buffer->next_read,
97                buffer->next_write);
98     if(buffer->next_read != buffer->next_write)
99     {
100       EVEL_DEBUG("RBR: buffer has item available");
101       msg = (buffer->ring)[buffer->next_read];
102       buffer->ring[buffer->next_read] = NULL;
103       buffer->next_read = (buffer->next_read + 1) % buffer->size;
104       pthread_mutex_unlock(&buffer->ring_mutex);
105       EVEL_DEBUG("RBR: next read location is %d data %lp", buffer->next_read,msg);
106       return msg;
107     }
108     else
109     {
110       EVEL_DEBUG("RBR: Waiting for condition variable");
111       pthread_cond_wait(&buffer->ring_cv, &buffer->ring_mutex);
112       EVEL_DEBUG("RBR: Condition variable wait completed");
113     }
114   }
115   pthread_mutex_unlock(&buffer->ring_mutex);
116   return msg;
117 }
118
119 /**************************************************************************//**
120  * Write an element into a ring_buffer.
121  *
122  * Writes an element into the ring_buffer, advancing the next-write position.
123  * Operation is synchronized and therefore MT-safe.  Fails if the buffer is
124  * full without blocking.
125  *
126  * @param   buffer  Pointer to the ring-buffer to be written.
127  * @param   msg     Pointer to data to be stored in the ring_buffer.
128  *
129  * @returns Number of items written.
130  * @retval  1       The data was written successfully.
131  * @retval  0       The ring_buffer was full so no data written.
132 ******************************************************************************/
133 int ring_buffer_write(ring_buffer * buffer, void * msg)
134 {
135   int item_count = 0;
136   int items_written = 0;
137   EVEL_DEBUG("RBW: Ring Buffer Write message at %lp", msg);
138
139   pthread_mutex_lock(&buffer->ring_mutex);
140   EVEL_DEBUG("RBW: got lock. NR=%d NW=%d SZ=%d",
141              buffer->next_read,
142              buffer->next_write,
143              buffer->size);
144
145   item_count = (buffer->next_write - buffer->next_read) % buffer->size;
146   if (item_count < 0)
147   {
148     item_count += buffer->size;
149   }
150   if (item_count < buffer->size - 1)
151   {
152     EVEL_DEBUG("RBW: %d items in buffer", item_count);
153     buffer->ring[buffer->next_write] = msg;
154     buffer->next_write = (buffer->next_write + 1) % buffer->size;
155     EVEL_DEBUG("RBW: next write location is %d", buffer->next_write);
156     items_written = 1;
157   }
158   else
159   {
160     EVEL_ERROR("RBW: ring buffer full - unable to write event");
161   }
162
163   pthread_mutex_unlock(&buffer->ring_mutex);
164   EVEL_DEBUG("RBW: released lock");
165   pthread_cond_signal(&buffer->ring_cv);
166
167   return items_written;
168 }
169
170 /**************************************************************************//**
171  * Tests whether there is data in the ring_buffer.
172  *
173  * Tests whether there is currently data in the ring_buffer without blocking.
174  *
175  * @param   buffer  Pointer to the ring-buffer to be tested.
176  *
177  * @returns Whether there is data in the ring_buffer.
178  * @retval  0       There isn't any data in the ring_buffer.
179  * @retval  1       There is data in the ring_buffer.
180 ******************************************************************************/
181 int ring_buffer_is_empty(ring_buffer * buffer)
182 {
183   int is_empty = 0;
184   EVEL_DEBUG("RBE: Ring empty check");
185
186   pthread_mutex_lock(&buffer->ring_mutex);
187   is_empty = (buffer->next_read == buffer->next_write);
188   pthread_mutex_unlock(&buffer->ring_mutex);
189
190   EVEL_DEBUG("RBE: Ring state= %d", is_empty);
191   return is_empty;
192 }
193