jack2 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.

236 lines
8.2KB

  1. /*
  2. Copyright (C) 2000 Paul Davis
  3. Copyright (C) 2003 Rohan Drape
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. $Id: ringbuffer.h,v 1.2.2.1 2006/06/20 14:44:00 letz Exp $
  16. */
  17. #ifndef _RINGBUFFER_H
  18. #define _RINGBUFFER_H
  19. #ifdef __cplusplus
  20. extern "C"
  21. {
  22. #endif
  23. #include <sys/types.h>
  24. /** @file ringbuffer.h
  25. *
  26. * A set of library functions to make lock-free ringbuffers available
  27. * to JACK clients. The `capture_client.c' (in the example_clients
  28. * directory) is a fully functioning user of this API.
  29. *
  30. * The key attribute of a ringbuffer is that it can be safely accessed
  31. * by two threads simultaneously -- one reading from the buffer and
  32. * the other writing to it -- without using any synchronization or
  33. * mutual exclusion primitives. For this to work correctly, there can
  34. * only be a single reader and a single writer thread. Their
  35. * identities cannot be interchanged.
  36. */
  37. typedef struct {
  38. char *buf;
  39. size_t len;
  40. }
  41. jack_ringbuffer_data_t ;
  42. typedef struct {
  43. char *buf;
  44. volatile size_t write_ptr;
  45. volatile size_t read_ptr;
  46. size_t size;
  47. size_t size_mask;
  48. int mlocked;
  49. }
  50. jack_ringbuffer_t ;
  51. /**
  52. * Allocates a ringbuffer data structure of a specified size. The
  53. * caller must arrange for a call to jack_ringbuffer_free() to release
  54. * the memory associated with the ringbuffer.
  55. *
  56. * @param sz the ringbuffer size in bytes.
  57. *
  58. * @return a pointer to a new jack_ringbuffer_t, if successful; NULL
  59. * otherwise.
  60. */
  61. jack_ringbuffer_t *jack_ringbuffer_create(size_t sz);
  62. /**
  63. * Frees the ringbuffer data structure allocated by an earlier call to
  64. * jack_ringbuffer_create().
  65. *
  66. * @param rb a pointer to the ringbuffer structure.
  67. */
  68. void jack_ringbuffer_free(jack_ringbuffer_t *rb);
  69. /**
  70. * Fill a data structure with a description of the current readable
  71. * data held in the ringbuffer. This description is returned in a two
  72. * element array of jack_ringbuffer_data_t. Two elements are needed
  73. * because the data to be read may be split across the end of the
  74. * ringbuffer.
  75. *
  76. * The first element will always contain a valid @a len field, which
  77. * may be zero or greater. If the @a len field is non-zero, then data
  78. * can be read in a contiguous fashion using the address given in the
  79. * corresponding @a buf field.
  80. *
  81. * If the second element has a non-zero @a len field, then a second
  82. * contiguous stretch of data can be read from the address given in
  83. * its corresponding @a buf field.
  84. *
  85. * @param rb a pointer to the ringbuffer structure.
  86. * @param vec a pointer to a 2 element array of jack_ringbuffer_data_t.
  87. *
  88. */
  89. void jack_ringbuffer_get_read_vector(const jack_ringbuffer_t *rb,
  90. jack_ringbuffer_data_t *vec);
  91. /**
  92. * Fill a data structure with a description of the current writable
  93. * space in the ringbuffer. The description is returned in a two
  94. * element array of jack_ringbuffer_data_t. Two elements are needed
  95. * because the space available for writing may be split across the end
  96. * of the ringbuffer.
  97. *
  98. * The first element will always contain a valid @a len field, which
  99. * may be zero or greater. If the @a len field is non-zero, then data
  100. * can be written in a contiguous fashion using the address given in
  101. * the corresponding @a buf field.
  102. *
  103. * If the second element has a non-zero @a len field, then a second
  104. * contiguous stretch of data can be written to the address given in
  105. * the corresponding @a buf field.
  106. *
  107. * @param rb a pointer to the ringbuffer structure.
  108. * @param vec a pointer to a 2 element array of jack_ringbuffer_data_t.
  109. */
  110. void jack_ringbuffer_get_write_vector(const jack_ringbuffer_t *rb,
  111. jack_ringbuffer_data_t *vec);
  112. /**
  113. * Read data from the ringbuffer.
  114. *
  115. * @param rb a pointer to the ringbuffer structure.
  116. * @param dest a pointer to a buffer where data read from the
  117. * ringbuffer will go.
  118. * @param cnt the number of bytes to read.
  119. *
  120. * @return the number of bytes read, which may range from 0 to cnt.
  121. */
  122. size_t jack_ringbuffer_read(jack_ringbuffer_t *rb, char *dest, size_t cnt);
  123. /**
  124. * Read data from the ringbuffer. Opposed to jack_ringbuffer_read()
  125. * this function does not move the read pointer. Thus it's
  126. * a convenient way to inspect data in the ringbuffer in a
  127. * continous fashion. The price is that the data is copied
  128. * into a user provided buffer. For "raw" non-copy inspection
  129. * of the data in the ringbuffer use jack_ringbuffer_get_read_vector().
  130. *
  131. * @param rb a pointer to the ringbuffer structure.
  132. * @param dest a pointer to a buffer where data read from the
  133. * ringbuffer will go.
  134. * @param cnt the number of bytes to read.
  135. *
  136. * @return the number of bytes read, which may range from 0 to cnt.
  137. */
  138. size_t jack_ringbuffer_peek(jack_ringbuffer_t *rb, char *dest, size_t cnt);
  139. /**
  140. * Advance the read pointer.
  141. *
  142. * After data have been read from the ringbuffer using the pointers
  143. * returned by jack_ringbuffer_get_read_vector(), use this function to
  144. * advance the buffer pointers, making that space available for future
  145. * write operations.
  146. *
  147. * @param rb a pointer to the ringbuffer structure.
  148. * @param cnt the number of bytes read.
  149. */
  150. void jack_ringbuffer_read_advance(jack_ringbuffer_t *rb, size_t cnt);
  151. /**
  152. * Return the number of bytes available for reading.
  153. *
  154. * @param rb a pointer to the ringbuffer structure.
  155. *
  156. * @return the number of bytes available to read.
  157. */
  158. size_t jack_ringbuffer_read_space(const jack_ringbuffer_t *rb);
  159. /**
  160. * Lock a ringbuffer data block into memory.
  161. *
  162. * Uses the mlock() system call. This is not a realtime operation.
  163. *
  164. * @param rb a pointer to the ringbuffer structure.
  165. */
  166. int jack_ringbuffer_mlock(jack_ringbuffer_t *rb);
  167. /**
  168. * Reset the read and write pointers, making an empty buffer.
  169. *
  170. * This is not thread safe.
  171. *
  172. * @param rb a pointer to the ringbuffer structure.
  173. */
  174. void jack_ringbuffer_reset(jack_ringbuffer_t *rb);
  175. /**
  176. * Write data into the ringbuffer.
  177. *
  178. * @param rb a pointer to the ringbuffer structure.
  179. * @param src a pointer to the data to be written to the ringbuffer.
  180. * @param cnt the number of bytes to write.
  181. *
  182. * @return the number of bytes write, which may range from 0 to cnt
  183. */
  184. size_t jack_ringbuffer_write(jack_ringbuffer_t *rb, const char *src,
  185. size_t cnt);
  186. /**
  187. * Advance the write pointer.
  188. *
  189. * After data have been written the ringbuffer using the pointers
  190. * returned by jack_ringbuffer_get_write_vector(), use this function
  191. * to advance the buffer pointer, making the data available for future
  192. * read operations.
  193. *
  194. * @param rb a pointer to the ringbuffer structure.
  195. * @param cnt the number of bytes written.
  196. */
  197. void jack_ringbuffer_write_advance(jack_ringbuffer_t *rb, size_t cnt);
  198. /**
  199. * Return the number of bytes available for writing.
  200. *
  201. * @param rb a pointer to the ringbuffer structure.
  202. *
  203. * @return the amount of free space (in bytes) available for writing.
  204. */
  205. size_t jack_ringbuffer_write_space(const jack_ringbuffer_t *rb);
  206. #ifdef __cplusplus
  207. }
  208. #endif
  209. #endif