1 /*************************************************************************//**
3 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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 /**************************************************************************//**
20 * A ring buffer with multi-threaded synchronization.
22 ****************************************************************************/
27 #include "ring_buffer.h"
30 /**************************************************************************//**
31 * Ring buffer initialization.
33 * Initialize the buffer supplied to the specified size.
35 * @param buffer Pointer to the ring-buffer to be initialized.
36 * @param size How many elements to be stored in the ring-buffer.
39 ******************************************************************************/
40 void ring_buffer_initialize(ring_buffer * buffer, int size)
46 /***************************************************************************/
47 /* Check assumptions. */
48 /***************************************************************************/
49 assert(buffer != NULL);
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);
60 /***************************************************************************/
61 /* Allocate the ring buffer itself. */
62 /***************************************************************************/
63 buffer->ring = malloc(size * sizeof(void *));
64 assert(buffer->ring != NULL);
66 /***************************************************************************/
67 /* Initialize the ring as empty. */
68 /***************************************************************************/
69 buffer->next_write = 0;
70 buffer->next_read = 0;
76 /**************************************************************************//**
77 * Read an element from a ring_buffer.
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
83 * @param buffer Pointer to the ring-buffer to be read.
85 * @returns Pointer to the element read from the buffer.
86 ******************************************************************************/
87 void * ring_buffer_read(ring_buffer * buffer)
90 EVEL_DEBUG("RBR: Ring buffer read");
92 pthread_mutex_lock(&buffer->ring_mutex);
95 EVEL_DEBUG("RBR: got lock. NR=%d NW=%d",
98 if(buffer->next_read != buffer->next_write)
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 EVEL_DEBUG("RBR: next read location is %d", buffer->next_read);
105 pthread_mutex_unlock(&buffer->ring_mutex);
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");
115 EVEL_DEBUG("RBR: Ring buffer read returning data at %lp", msg);
119 /**************************************************************************//**
120 * Write an element into a ring_buffer.
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.
126 * @param buffer Pointer to the ring-buffer to be written.
127 * @param msg Pointer to data to be stored in the ring_buffer.
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)
136 int items_written = 0;
137 EVEL_DEBUG("RBW: Ring Buffer Write message at %lp", msg);
139 pthread_mutex_lock(&buffer->ring_mutex);
140 EVEL_DEBUG("RBW: got lock. NR=%d NW=%d SZ=%d",
145 item_count = (buffer->next_write - buffer->next_read) % buffer->size;
148 item_count += buffer->size;
150 if (item_count < buffer->size - 1)
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);
160 EVEL_ERROR("RBW: ring buffer full - unable to write event");
163 pthread_mutex_unlock(&buffer->ring_mutex);
164 EVEL_DEBUG("RBW: released lock");
165 pthread_cond_signal(&buffer->ring_cv);
167 return items_written;
170 /**************************************************************************//**
171 * Tests whether there is data in the ring_buffer.
173 * Tests whether there is currently data in the ring_buffer without blocking.
175 * @param buffer Pointer to the ring-buffer to be tested.
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)
184 EVEL_DEBUG("RBE: Ring empty check");
186 pthread_mutex_lock(&buffer->ring_mutex);
187 is_empty = (buffer->next_read == buffer->next_write);
188 pthread_mutex_unlock(&buffer->ring_mutex);
190 EVEL_DEBUG("RBE: Ring state= %d", is_empty);