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.

142 lines
4.5KB

  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. */
  18. typedef struct
  19. {
  20. char *buf;
  21. size_t len;
  22. }
  23. jack_ringbuffer_data_t ;
  24. typedef struct
  25. {
  26. char *buf;
  27. volatile size_t write_ptr;
  28. volatile size_t read_ptr;
  29. size_t size;
  30. size_t size_mask;
  31. int mlocked;
  32. }
  33. jack_ringbuffer_t ;
  34. /**
  35. * jack_ringbuffer_create
  36. *
  37. * Allocates a ringbuffer data structure of a specified size. The
  38. * caller must arrange for a call to jack_ringbuffer_free to release
  39. * the memory associated with the ringbuffer.
  40. *
  41. * @param sz the ringbuffer size in bytes.
  42. *
  43. * @return a pointer to a new jack_ringbuffer_t, if successful; NULL
  44. * otherwise.
  45. */
  46. jack_ringbuffer_t *jack_ringbuffer_create(size_t sz);
  47. void jack_ringbuffer_free(jack_ringbuffer_t *rb);
  48. size_t jack_ringbuffer_write_space(jack_ringbuffer_t *rb);
  49. size_t jack_ringbuffer_read_space(jack_ringbuffer_t *rb);
  50. /**
  51. * jack_ringbuffer_read
  52. *
  53. * read a specified number of bytes from the ringbuffer.
  54. *
  55. * @param rb a pointer to the ringbuffer structure
  56. * @param dest a pointer to a buffer where the data read from the ringbuffer
  57. * will be placed
  58. * @param cnt the number of bytes to be read
  59. *
  60. * @return the number of bytes read, which may range from 0 to cnt
  61. */
  62. size_t jack_ringbuffer_read(jack_ringbuffer_t *rb, char *dest, size_t cnt);
  63. /**
  64. * jack_ringbuffer_write
  65. *
  66. * write a specified number of bytes from the ringbuffer.
  67. *
  68. * @param rb a pointer to the ringbuffer structure
  69. * @param src a pointer to a buffer where the data written to the ringbuffer
  70. * will be read from
  71. * @param cnt the number of bytes to be write
  72. *
  73. * @return the number of bytes write, which may range from 0 to cnt
  74. */
  75. size_t jack_ringbuffer_write(jack_ringbuffer_t *rb, char *src, size_t cnt);
  76. /**
  77. * jack_ringbuffer_get_read_vector
  78. *
  79. * fill a data structure with a description of the current readable data
  80. * held in the ringbuffer. the description is returned in a 2 element
  81. * array of jack_ringbuffer_data_t. two elements are necessary
  82. * because the data to be read may be split across the end of the ringbuffer.
  83. *
  84. * the first element will always contain
  85. * a valid len field, which may be zero or greater. if the len field
  86. * is non-zero, then data can be read in a contiguous fashion using the address given
  87. * in the corresponding buf field.
  88. *
  89. * if the second element has a non-zero len field, then a second contiguous
  90. * stretch of data can be read from the address given in the corresponding buf
  91. * field.
  92. *
  93. * @param rb a pointer to the ringbuffer structure
  94. * @param vec a pointer to a 2 element array of jack_ringbuffer_data_t
  95. *
  96. */
  97. void jack_ringbuffer_get_read_vector(jack_ringbuffer_t *rb, jack_ringbuffer_data_t *vec);
  98. /**
  99. * jack_ringbuffer_get_write_vector
  100. *
  101. * fill a data structure with a description of the current writable space
  102. * in the ringbuffer. the description is returned in a 2 element
  103. * array of jack_ringbuffer_data_t. two elements are necessary
  104. * because the space available to write in may be split across the end
  105. * of the ringbuffer.
  106. *
  107. * the first element will always contain
  108. * a valid len field, which may be zero or greater. if the len field
  109. * is non-zero, then data can be written in a contiguous fashion using the address given
  110. * in the corresponding buf field.
  111. *
  112. * if the second element has a non-zero len field, then a second contiguous
  113. * stretch of data can be written to the address given in the corresponding buf
  114. * field.
  115. *
  116. * @param rb a pointer to the ringbuffer structure
  117. * @param vec a pointer to a 2 element array of jack_ringbuffer_data_t
  118. *
  119. */
  120. void jack_ringbuffer_get_write_vector(jack_ringbuffer_t *rb, jack_ringbuffer_data_t *vec);
  121. int jack_ringbuffer_mlock(jack_ringbuffer_t *rb);
  122. void jack_ringbuffer_reset(jack_ringbuffer_t *rb);
  123. void jack_ringbuffer_write_advance(jack_ringbuffer_t *rb, size_t cnt);
  124. void jack_ringbuffer_read_advance(jack_ringbuffer_t *rb, size_t cnt);
  125. #endif