jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
5.8KB

  1. #ifndef _RINGBUFFER_H
  2. #define _RINGBUFFER_H
  3. #include <sys/types.h>
  4. /** @file ringbuffer.h
  5. *
  6. * A set of library functions to make lock-free ringbuffers available
  7. * to JACK clients. The `capture_client.c' (in the example_clients
  8. * directory) is a fully functioning user of this API.
  9. *
  10. * The key attribute of a ringbuffer is that it can be safely accessed
  11. * by two threads simultaneously -- one reading from the buffer and
  12. * the other writing to it -- without using any synchronization or
  13. * mutual exclusion primitives. For this to work correctly, there can
  14. * only be a single reader and a single writer thread. Their
  15. * identities cannot be interchanged.
  16. */
  17. typedef struct
  18. {
  19. char *buf;
  20. size_t len;
  21. }
  22. jack_ringbuffer_data_t ;
  23. typedef struct
  24. {
  25. char *buf;
  26. volatile size_t write_ptr;
  27. volatile size_t read_ptr;
  28. size_t size;
  29. size_t size_mask;
  30. int mlocked;
  31. }
  32. jack_ringbuffer_t ;
  33. /**
  34. * Allocates a ringbuffer data structure of a specified size. The
  35. * caller must arrange for a call to jack_ringbuffer_free() to release
  36. * the memory associated with the ringbuffer.
  37. *
  38. * @param sz the ringbuffer size in bytes.
  39. *
  40. * @return a pointer to a new jack_ringbuffer_t, if successful; NULL
  41. * otherwise.
  42. */
  43. jack_ringbuffer_t *jack_ringbuffer_create(size_t sz);
  44. /**
  45. * Frees the ringbuffer data structure allocated by an earlier call to
  46. * jack_ringbuffer_create().
  47. *
  48. * @param rb a pointer to the ringbuffer structure.
  49. */
  50. void jack_ringbuffer_free(jack_ringbuffer_t *rb);
  51. /**
  52. * Fill a data structure with a description of the current readable
  53. * data held in the ringbuffer. This description is returned in a two
  54. * element array of jack_ringbuffer_data_t. Two elements are needed
  55. * because the data to be read may be split across the end of the
  56. * ringbuffer.
  57. *
  58. * The first element will always contain a valid @a len field, which
  59. * may be zero or greater. If the @a len field is non-zero, then data
  60. * can be read in a contiguous fashion using the address given in the
  61. * corresponding @a buf field.
  62. *
  63. * If the second element has a non-zero @a len field, then a second
  64. * contiguous stretch of data can be read from the address given in
  65. * its corresponding @a buf field.
  66. *
  67. * @param rb a pointer to the ringbuffer structure.
  68. * @param vec a pointer to a 2 element array of jack_ringbuffer_data_t.
  69. *
  70. */
  71. void jack_ringbuffer_get_read_vector(jack_ringbuffer_t *rb,
  72. jack_ringbuffer_data_t *vec);
  73. /**
  74. * Fill a data structure with a description of the current writable
  75. * space in the ringbuffer. The description is returned in a two
  76. * element array of jack_ringbuffer_data_t. Two elements are needed
  77. * because the space available for writing may be split across the end
  78. * of the ringbuffer.
  79. *
  80. * The first element will always contain a valid @a len field, which
  81. * may be zero or greater. If the @a len field is non-zero, then data
  82. * can be written in a contiguous fashion using the address given in
  83. * the corresponding @a buf field.
  84. *
  85. * If the second element has a non-zero @a len field, then a second
  86. * contiguous stretch of data can be written to the address given in
  87. * the corresponding @a buf field.
  88. *
  89. * @param rb a pointer to the ringbuffer structure.
  90. * @param vec a pointer to a 2 element array of jack_ringbuffer_data_t.
  91. */
  92. void jack_ringbuffer_get_write_vector(jack_ringbuffer_t *rb,
  93. jack_ringbuffer_data_t *vec);
  94. /**
  95. * Read data from the ringbuffer.
  96. *
  97. * @param rb a pointer to the ringbuffer structure.
  98. * @param dest a pointer to a buffer where data read from the
  99. * ringbuffer will go.
  100. * @param cnt the number of bytes to read.
  101. *
  102. * @return the number of bytes read, which may range from 0 to cnt.
  103. */
  104. size_t jack_ringbuffer_read(jack_ringbuffer_t *rb, char *dest, size_t cnt);
  105. /**
  106. * Advance the read pointer.
  107. *
  108. * After data have been read from the ringbuffer using the pointers
  109. * returned by jack_ringbuffer_get_read_vector(), use this function to
  110. * advance the buffer pointers, making that space available for future
  111. * write operations.
  112. *
  113. * @param rb a pointer to the ringbuffer structure.
  114. * @param cnt the number of bytes read.
  115. */
  116. void jack_ringbuffer_read_advance(jack_ringbuffer_t *rb, size_t cnt);
  117. /**
  118. * Return the number of bytes available for reading.
  119. *
  120. * @param rb a pointer to the ringbuffer structure.
  121. *
  122. * @return the number of bytes available to read.
  123. */
  124. size_t jack_ringbuffer_read_space(jack_ringbuffer_t *rb);
  125. /**
  126. * Lock a ringbuffer data block into memory.
  127. *
  128. * Uses the mlock() system call. This is not a realtime operation.
  129. *
  130. * @param rb a pointer to the ringbuffer structure.
  131. */
  132. int jack_ringbuffer_mlock(jack_ringbuffer_t *rb);
  133. /**
  134. * Reset the read and write pointers, making an empty buffer.
  135. *
  136. * This is not thread safe.
  137. *
  138. * @param rb a pointer to the ringbuffer structure.
  139. */
  140. void jack_ringbuffer_reset(jack_ringbuffer_t *rb);
  141. /**
  142. * Write data into the ringbuffer.
  143. *
  144. * @param rb a pointer to the ringbuffer structure.
  145. * @param src a pointer to the data to be written to the ringbuffer.
  146. * @param cnt the number of bytes to write.
  147. *
  148. * @return the number of bytes write, which may range from 0 to cnt
  149. */
  150. size_t jack_ringbuffer_write(jack_ringbuffer_t *rb, char *src, size_t cnt);
  151. /**
  152. * Advance the write pointer.
  153. *
  154. * After data have been written the ringbuffer using the pointers
  155. * returned by jack_ringbuffer_get_write_vector(), use this function
  156. * to advance the buffer pointer, making the data available for future
  157. * read operations.
  158. *
  159. * @param rb a pointer to the ringbuffer structure.
  160. * @param cnt the number of bytes written.
  161. */
  162. void jack_ringbuffer_write_advance(jack_ringbuffer_t *rb, size_t cnt);
  163. /**
  164. * Return the number of bytes available for writing.
  165. *
  166. * @param rb a pointer to the ringbuffer structure.
  167. *
  168. * @return the amount of free space (in bytes) available for writing.
  169. */
  170. size_t jack_ringbuffer_write_space(jack_ringbuffer_t *rb);
  171. #endif