1 /*************************************************************************//**
3 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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
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.
16 ****************************************************************************/
18 #ifndef RING_BUFFER_INCLUDED
19 #define RING_BUFFER_INCLUDED
21 /**************************************************************************//**
23 * Ring buffer to handle message requests.
25 ****************************************************************************/
29 /**************************************************************************//**
30 * Ring buffer structure.
31 *****************************************************************************/
32 typedef struct ring_buffer
38 pthread_cond_t ring_cv;
39 pthread_mutex_t ring_mutex;
42 /**************************************************************************//**
43 * Ring buffer initialization.
45 * Initialize the buffer supplied to the specified size.
47 * @param buffer Pointer to the ring-buffer to be initialized.
48 * @param size How many elements to be stored in the ring-buffer.
51 ******************************************************************************/
52 void ring_buffer_initialize(ring_buffer * buffer, int size);
54 /**************************************************************************//**
55 * Read an element from a ring_buffer.
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
61 * @param buffer Pointer to the ring-buffer to be read.
63 * @returns Pointer to the element read from the buffer.
64 ******************************************************************************/
65 void * ring_buffer_read(ring_buffer * buffer);
67 /**************************************************************************//**
68 * Write an element into a ring_buffer.
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.
74 * @param buffer Pointer to the ring-buffer to be written.
75 * @param msg Pointer to data to be stored in the ring_buffer.
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);
83 /**************************************************************************//**
84 * Tests whether there is data in the ring_buffer.
86 * Tests whether there is currently data in the ring_buffer without blocking.
88 * @param buffer Pointer to the ring-buffer to be tested.
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);