giuliomoro@338: /* giuliomoro@338: * Copyright (c) 2012 Peter Brinkmann (peter.brinkmann@gmail.com) giuliomoro@338: * giuliomoro@338: * For information on usage and redistribution, and for a DISCLAIMER OF ALL giuliomoro@338: * WARRANTIES, see the file, "LICENSE.txt," in this distribution. giuliomoro@338: * giuliomoro@338: * See https://github.com/libpd/libpd/wiki for documentation giuliomoro@338: * giuliomoro@338: */ giuliomoro@338: giuliomoro@338: #ifndef __OPENSL_RING_BUFFER_H__ giuliomoro@338: #define __OPENSL_RING_BUFFER_H__ giuliomoro@338: giuliomoro@338: // Simple lock-free ring buffer implementation for one writer thread and one giuliomoro@338: // consumer thread. giuliomoro@338: typedef struct ring_buffer { giuliomoro@338: int size; giuliomoro@338: char *buf_ptr; giuliomoro@338: int write_idx; giuliomoro@338: int read_idx; giuliomoro@338: } ring_buffer; giuliomoro@338: giuliomoro@338: // Creates a ring buffer (returns NULL on failure). giuliomoro@338: ring_buffer *rb_create(int size); giuliomoro@338: giuliomoro@338: // Deletes a ring buffer. giuliomoro@338: void rb_free(ring_buffer *buffer); giuliomoro@338: giuliomoro@338: // Returns the number of bytes that can currently be written; safe to be called giuliomoro@338: // from any thread. giuliomoro@338: int rb_available_to_write(ring_buffer *buffer); giuliomoro@338: giuliomoro@338: // Returns the number of bytes that can currently be read; safe to be called giuliomoro@338: // from any thread. giuliomoro@338: int rb_available_to_read(ring_buffer *buffer); giuliomoro@338: giuliomoro@338: // Writes bytes from n sources to the ring buffer (if the ring buffer has giuliomoro@338: // enough space). The varargs are pairs of type (const char*, int) giving a giuliomoro@338: // pointer to a buffer and the number of bytes to be copied. Only to be called giuliomoro@338: // from a single writer thread. giuliomoro@338: // Returns 0 on success. giuliomoro@338: int rb_write_to_buffer(ring_buffer *buffer, int n, ...); giuliomoro@338: giuliomoro@338: // Reads the given number of bytes fromthe ring buffer to dest if the ring giuliomoro@338: // buffer has enough data. Only to be called from a single reader thread. giuliomoro@338: // Returns 0 on success. giuliomoro@338: int rb_read_from_buffer(ring_buffer *buffer, char *dest, int len); giuliomoro@338: giuliomoro@338: #endif