comparison examples/basic_libpd/ringbuffer.h @ 338:1802f99cd77f prerelease

Initial support for thread-safe libpd (stil missing updated libpd binary), untested
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 05 Jun 2016 21:46:44 +0100
parents
children
comparison
equal deleted inserted replaced
337:5e2780bfbfed 338:1802f99cd77f
1 /*
2 * Copyright (c) 2012 Peter Brinkmann (peter.brinkmann@gmail.com)
3 *
4 * For information on usage and redistribution, and for a DISCLAIMER OF ALL
5 * WARRANTIES, see the file, "LICENSE.txt," in this distribution.
6 *
7 * See https://github.com/libpd/libpd/wiki for documentation
8 *
9 */
10
11 #ifndef __OPENSL_RING_BUFFER_H__
12 #define __OPENSL_RING_BUFFER_H__
13
14 // Simple lock-free ring buffer implementation for one writer thread and one
15 // consumer thread.
16 typedef struct ring_buffer {
17 int size;
18 char *buf_ptr;
19 int write_idx;
20 int read_idx;
21 } ring_buffer;
22
23 // Creates a ring buffer (returns NULL on failure).
24 ring_buffer *rb_create(int size);
25
26 // Deletes a ring buffer.
27 void rb_free(ring_buffer *buffer);
28
29 // Returns the number of bytes that can currently be written; safe to be called
30 // from any thread.
31 int rb_available_to_write(ring_buffer *buffer);
32
33 // Returns the number of bytes that can currently be read; safe to be called
34 // from any thread.
35 int rb_available_to_read(ring_buffer *buffer);
36
37 // Writes bytes from n sources to the ring buffer (if the ring buffer has
38 // enough space). The varargs are pairs of type (const char*, int) giving a
39 // pointer to a buffer and the number of bytes to be copied. Only to be called
40 // from a single writer thread.
41 // Returns 0 on success.
42 int rb_write_to_buffer(ring_buffer *buffer, int n, ...);
43
44 // Reads the given number of bytes fromthe ring buffer to dest if the ring
45 // buffer has enough data. Only to be called from a single reader thread.
46 // Returns 0 on success.
47 int rb_read_from_buffer(ring_buffer *buffer, char *dest, int len);
48
49 #endif