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