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