1 /**************************************************************************//**
3 * A ring buffer with multi-threaded synchronization.
8 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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 *****************************************************************************/
25 #include "ring_buffer.h"
28 /**************************************************************************//**
29 * Ring buffer initialization.
31 * Initialize the buffer supplied to the specified size.
33 * @param buffer Pointer to the ring-buffer to be initialized.
34 * @param size How many elements to be stored in the ring-buffer.
37 ******************************************************************************/
38 void ring_buffer_initialize(ring_buffer * buffer, int size)
44 /***************************************************************************/
45 /* Check assumptions. */
46 /***************************************************************************/
47 assert(buffer != NULL);
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);
58 /***************************************************************************/
59 /* Allocate the ring buffer itself. */
60 /***************************************************************************/
61 buffer->ring = malloc(size * sizeof(void *));
62 assert(buffer->ring != NULL);
64 /***************************************************************************/
65 /* Initialize the ring as empty. */
66 /***************************************************************************/
67 buffer->next_write = 0;
68 buffer->next_read = 0;
74 /**************************************************************************//**
75 * Read an element from a ring_buffer.
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
81 * @param buffer Pointer to the ring-buffer to be read.
83 * @returns Pointer to the element read from the buffer.
84 ******************************************************************************/
85 void * ring_buffer_read(ring_buffer * buffer)
88 EVEL_DEBUG("RBR: Ring buffer read");
90 pthread_mutex_lock(&buffer->ring_mutex);
93 EVEL_DEBUG("RBR: got lock. NR=%d NW=%d",
96 if(buffer->next_read != buffer->next_write)
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);
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");
113 EVEL_DEBUG("RBR: Ring buffer read returning data at %lp", msg);
117 /**************************************************************************//**
118 * Write an element into a ring_buffer.
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.
124 * @param buffer Pointer to the ring-buffer to be written.
125 * @param msg Pointer to data to be stored in the ring_buffer.
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)
134 int items_written = 0;
135 EVEL_DEBUG("RBW: Ring Buffer Write message at %lp", msg);
137 pthread_mutex_lock(&buffer->ring_mutex);
138 EVEL_DEBUG("RBW: got lock. NR=%d NW=%d SZ=%d",
143 item_count = (buffer->next_write - buffer->next_read) % buffer->size;
146 item_count += buffer->size;
148 if (item_count < buffer->size - 1)
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);
158 EVEL_ERROR("RBW: ring buffer full - unable to write event");
161 pthread_mutex_unlock(&buffer->ring_mutex);
162 EVEL_DEBUG("RBW: released lock");
163 pthread_cond_signal(&buffer->ring_cv);
165 return items_written;
168 /**************************************************************************//**
169 * Tests whether there is data in the ring_buffer.
171 * Tests whether there is currently data in the ring_buffer without blocking.
173 * @param buffer Pointer to the ring-buffer to be tested.
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)
182 EVEL_DEBUG("RBE: Ring empty check");
184 pthread_mutex_lock(&buffer->ring_mutex);
185 is_empty = (buffer->next_read == buffer->next_write);
186 pthread_mutex_unlock(&buffer->ring_mutex);
188 EVEL_DEBUG("RBE: Ring state= %d", is_empty);