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.

421 lines
11KB

  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. ISO/POSIX C version of Paul Davis's lock free ringbuffer C++ code.
  16. This is safe for the case of one read thread and one write thread.
  17. */
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #ifdef USE_MLOCK
  21. #include <sys/mman.h>
  22. #endif /* USE_MLOCK */
  23. #include "JackCompilerDeps.h"
  24. /* Portable definitions for acquire and release fences */
  25. #if defined(_MSC_VER)
  26. #if defined(_M_AMD64) || defined(_M_IX86) || defined(_M_X64)
  27. /* Only compiler fences are needed on x86. In fact, GCC
  28. * will generate no instructions for acq/rel fences on
  29. * x86 */
  30. #include <intrin.h>
  31. #define JACK_ACQ_FENCE() _ReadBarrier()
  32. #define JACK_REL_FENCE() _WriteBarrier()
  33. #else
  34. /* Use full memory fence for non-x86 systems with msvc */
  35. #include <windows.h>
  36. #define JACK_ACQ_FENCE() MemoryBarrier()
  37. #define JACK_REL_FENCE() MemoryBarrier()
  38. #endif
  39. #elif defined(__GNUC__)
  40. #ifdef __ATOMIC_ACQUIRE
  41. #define JACK_ACQ_FENCE() __atomic_thread_fence(__ATOMIC_ACQUIRE)
  42. #define JACK_REL_FENCE() __atomic_thread_fence(__ATOMIC_RELEASE)
  43. #else
  44. /* Fallback to the legacy __sync builtin (full memory fence) */
  45. #define JACK_ACQ_FENCE() __sync_synchronize()
  46. #define JACK_REL_FENCE() __sync_synchronize()
  47. #endif
  48. #else
  49. #define JACK_ACQ_FENCE()
  50. #define JACK_REL_FENCE()
  51. #endif
  52. typedef struct {
  53. char *buf;
  54. size_t len;
  55. }
  56. jack_ringbuffer_data_t ;
  57. typedef struct {
  58. char *buf;
  59. size_t write_ptr;
  60. size_t read_ptr;
  61. size_t size;
  62. size_t size_mask;
  63. int mlocked;
  64. }
  65. jack_ringbuffer_t ;
  66. LIB_EXPORT jack_ringbuffer_t *jack_ringbuffer_create(size_t sz);
  67. LIB_EXPORT void jack_ringbuffer_free(jack_ringbuffer_t *rb);
  68. LIB_EXPORT void jack_ringbuffer_get_read_vector(const jack_ringbuffer_t *rb,
  69. jack_ringbuffer_data_t *vec);
  70. LIB_EXPORT void jack_ringbuffer_get_write_vector(const jack_ringbuffer_t *rb,
  71. jack_ringbuffer_data_t *vec);
  72. LIB_EXPORT size_t jack_ringbuffer_read(jack_ringbuffer_t *rb, char *dest, size_t cnt);
  73. LIB_EXPORT size_t jack_ringbuffer_peek(jack_ringbuffer_t *rb, char *dest, size_t cnt);
  74. LIB_EXPORT void jack_ringbuffer_read_advance(jack_ringbuffer_t *rb, size_t cnt);
  75. LIB_EXPORT size_t jack_ringbuffer_read_space(const jack_ringbuffer_t *rb);
  76. LIB_EXPORT int jack_ringbuffer_mlock(jack_ringbuffer_t *rb);
  77. LIB_EXPORT void jack_ringbuffer_reset(jack_ringbuffer_t *rb);
  78. LIB_EXPORT void jack_ringbuffer_reset_size (jack_ringbuffer_t * rb, size_t sz);
  79. LIB_EXPORT size_t jack_ringbuffer_write(jack_ringbuffer_t *rb, const char *src,
  80. size_t cnt);
  81. void jack_ringbuffer_write_advance(jack_ringbuffer_t *rb, size_t cnt);
  82. size_t jack_ringbuffer_write_space(const jack_ringbuffer_t *rb);
  83. /* Create a new ringbuffer to hold at least `sz' bytes of data. The
  84. actual buffer size is rounded up to the next power of two. */
  85. LIB_EXPORT jack_ringbuffer_t *
  86. jack_ringbuffer_create (size_t sz)
  87. {
  88. int power_of_two;
  89. jack_ringbuffer_t *rb;
  90. if ((rb = (jack_ringbuffer_t *) malloc (sizeof (jack_ringbuffer_t))) == NULL) {
  91. return NULL;
  92. }
  93. for (power_of_two = 1; 1 << power_of_two < sz; power_of_two++);
  94. rb->size = 1 << power_of_two;
  95. rb->size_mask = rb->size;
  96. rb->size_mask -= 1;
  97. rb->write_ptr = 0;
  98. rb->read_ptr = 0;
  99. if ((rb->buf = (char *) malloc (rb->size)) == NULL) {
  100. free (rb);
  101. return NULL;
  102. }
  103. rb->mlocked = 0;
  104. return rb;
  105. }
  106. /* Free all data associated with the ringbuffer `rb'. */
  107. LIB_EXPORT void
  108. jack_ringbuffer_free (jack_ringbuffer_t * rb)
  109. {
  110. #ifdef USE_MLOCK
  111. if (rb->mlocked) {
  112. munlock (rb->buf, rb->size);
  113. }
  114. #endif /* USE_MLOCK */
  115. free (rb->buf);
  116. free (rb);
  117. }
  118. /* Lock the data block of `rb' using the system call 'mlock'. */
  119. LIB_EXPORT int
  120. jack_ringbuffer_mlock (jack_ringbuffer_t * rb)
  121. {
  122. #ifdef USE_MLOCK
  123. if (mlock (rb->buf, rb->size)) {
  124. return -1;
  125. }
  126. #endif /* USE_MLOCK */
  127. rb->mlocked = 1;
  128. return 0;
  129. }
  130. /* Reset the read and write pointers to zero. This is not thread
  131. safe. */
  132. LIB_EXPORT void
  133. jack_ringbuffer_reset (jack_ringbuffer_t * rb)
  134. {
  135. rb->read_ptr = 0;
  136. rb->write_ptr = 0;
  137. memset(rb->buf, 0, rb->size);
  138. }
  139. /* Reset the read and write pointers to zero. This is not thread
  140. safe. */
  141. LIB_EXPORT void
  142. jack_ringbuffer_reset_size (jack_ringbuffer_t * rb, size_t sz)
  143. {
  144. rb->size = sz;
  145. rb->size_mask = rb->size;
  146. rb->size_mask -= 1;
  147. rb->read_ptr = 0;
  148. rb->write_ptr = 0;
  149. }
  150. /* Return the number of bytes available for reading. This is the
  151. number of bytes in front of the read pointer and behind the write
  152. pointer. */
  153. LIB_EXPORT size_t
  154. jack_ringbuffer_read_space (const jack_ringbuffer_t * rb)
  155. {
  156. size_t w, r;
  157. w = rb->write_ptr;
  158. JACK_ACQ_FENCE();
  159. r = rb->read_ptr;
  160. return (w - r) & rb->size_mask;
  161. }
  162. /* Return the number of bytes available for writing. This is the
  163. number of bytes in front of the write pointer and behind the read
  164. pointer. */
  165. LIB_EXPORT size_t
  166. jack_ringbuffer_write_space (const jack_ringbuffer_t * rb)
  167. {
  168. size_t w, r;
  169. w = rb->write_ptr;
  170. r = rb->read_ptr;
  171. JACK_ACQ_FENCE();
  172. return (r - w - 1) & rb->size_mask;
  173. }
  174. /* The copying data reader. Copy at most `cnt' bytes from `rb' to
  175. `dest'. Returns the actual number of bytes copied. */
  176. LIB_EXPORT size_t
  177. jack_ringbuffer_read (jack_ringbuffer_t * rb, char *dest, size_t cnt)
  178. {
  179. size_t free_cnt;
  180. size_t cnt2;
  181. size_t to_read;
  182. size_t n1, n2;
  183. if ((free_cnt = jack_ringbuffer_read_space (rb)) == 0) {
  184. return 0;
  185. }
  186. to_read = cnt > free_cnt ? free_cnt : cnt;
  187. /* note: relaxed load here, rb->read_ptr cannot be
  188. * modified from writing thread */
  189. cnt2 = rb->read_ptr + to_read;
  190. if (cnt2 > rb->size) {
  191. n1 = rb->size - rb->read_ptr;
  192. n2 = cnt2 & rb->size_mask;
  193. } else {
  194. n1 = to_read;
  195. n2 = 0;
  196. }
  197. memcpy (dest, &(rb->buf[rb->read_ptr]), n1);
  198. JACK_REL_FENCE(); /* ensure pointer increment happens after copy */
  199. rb->read_ptr = (rb->read_ptr + n1) & rb->size_mask;
  200. if (n2) {
  201. memcpy (dest + n1, &(rb->buf[rb->read_ptr]), n2);
  202. JACK_REL_FENCE(); /* ensure pointer increment happens after copy */
  203. rb->read_ptr = (rb->read_ptr + n2) & rb->size_mask;
  204. }
  205. return to_read;
  206. }
  207. /* The copying data reader w/o read pointer advance. Copy at most
  208. `cnt' bytes from `rb' to `dest'. Returns the actual number of bytes
  209. copied. */
  210. LIB_EXPORT size_t
  211. jack_ringbuffer_peek (jack_ringbuffer_t * rb, char *dest, size_t cnt)
  212. {
  213. size_t free_cnt;
  214. size_t cnt2;
  215. size_t to_read;
  216. size_t n1, n2;
  217. size_t tmp_read_ptr;
  218. tmp_read_ptr = rb->read_ptr;
  219. if ((free_cnt = jack_ringbuffer_read_space (rb)) == 0) {
  220. return 0;
  221. }
  222. to_read = cnt > free_cnt ? free_cnt : cnt;
  223. cnt2 = tmp_read_ptr + to_read;
  224. if (cnt2 > rb->size) {
  225. n1 = rb->size - tmp_read_ptr;
  226. n2 = cnt2 & rb->size_mask;
  227. } else {
  228. n1 = to_read;
  229. n2 = 0;
  230. }
  231. memcpy (dest, &(rb->buf[tmp_read_ptr]), n1);
  232. tmp_read_ptr = (tmp_read_ptr + n1) & rb->size_mask;
  233. if (n2) {
  234. memcpy (dest + n1, &(rb->buf[tmp_read_ptr]), n2);
  235. }
  236. return to_read;
  237. }
  238. /* The copying data writer. Copy at most `cnt' bytes to `rb' from
  239. `src'. Returns the actual number of bytes copied. */
  240. LIB_EXPORT size_t
  241. jack_ringbuffer_write (jack_ringbuffer_t * rb, const char *src, size_t cnt)
  242. {
  243. size_t free_cnt;
  244. size_t cnt2;
  245. size_t to_write;
  246. size_t n1, n2;
  247. if ((free_cnt = jack_ringbuffer_write_space (rb)) == 0) {
  248. return 0;
  249. }
  250. to_write = cnt > free_cnt ? free_cnt : cnt;
  251. /* note: relaxed load here, rb->write_ptr cannot be
  252. * modified from reading thread */
  253. cnt2 = rb->write_ptr + to_write;
  254. if (cnt2 > rb->size) {
  255. n1 = rb->size - rb->write_ptr;
  256. n2 = cnt2 & rb->size_mask;
  257. } else {
  258. n1 = to_write;
  259. n2 = 0;
  260. }
  261. memcpy (&(rb->buf[rb->write_ptr]), src, n1);
  262. JACK_REL_FENCE(); /* ensure pointer increment happens after copy */
  263. rb->write_ptr = (rb->write_ptr + n1) & rb->size_mask;
  264. if (n2) {
  265. memcpy (&(rb->buf[rb->write_ptr]), src + n1, n2);
  266. JACK_REL_FENCE(); /* ensure pointer increment happens after copy */
  267. rb->write_ptr = (rb->write_ptr + n2) & rb->size_mask;
  268. }
  269. return to_write;
  270. }
  271. /* Advance the read pointer `cnt' places. */
  272. LIB_EXPORT void
  273. jack_ringbuffer_read_advance (jack_ringbuffer_t * rb, size_t cnt)
  274. {
  275. size_t tmp = (rb->read_ptr + cnt) & rb->size_mask;
  276. JACK_REL_FENCE(); /* ensure pointer increment happens after copy (by user) */
  277. rb->read_ptr = tmp;
  278. }
  279. /* Advance the write pointer `cnt' places. */
  280. LIB_EXPORT void
  281. jack_ringbuffer_write_advance (jack_ringbuffer_t * rb, size_t cnt)
  282. {
  283. size_t tmp = (rb->write_ptr + cnt) & rb->size_mask;
  284. JACK_REL_FENCE(); /* ensure pointer increment happens after copy (by user) */
  285. rb->write_ptr = tmp;
  286. }
  287. /* The non-copying data reader. `vec' is an array of two places. Set
  288. the values at `vec' to hold the current readable data at `rb'. If
  289. the readable data is in one segment the second segment has zero
  290. length. */
  291. LIB_EXPORT void
  292. jack_ringbuffer_get_read_vector (const jack_ringbuffer_t * rb,
  293. jack_ringbuffer_data_t * vec)
  294. {
  295. size_t free_cnt;
  296. size_t cnt2;
  297. size_t r;
  298. r = rb->read_ptr;
  299. free_cnt = jack_ringbuffer_read_space(rb);
  300. cnt2 = r + free_cnt;
  301. if (cnt2 > rb->size) {
  302. /* Two part vector: the rest of the buffer after the current write
  303. ptr, plus some from the start of the buffer. */
  304. vec[0].buf = &(rb->buf[r]);
  305. vec[0].len = rb->size - r;
  306. vec[1].buf = rb->buf;
  307. vec[1].len = cnt2 & rb->size_mask;
  308. } else {
  309. /* Single part vector: just the rest of the buffer */
  310. vec[0].buf = &(rb->buf[r]);
  311. vec[0].len = free_cnt;
  312. vec[1].len = 0;
  313. }
  314. }
  315. /* The non-copying data writer. `vec' is an array of two places. Set
  316. the values at `vec' to hold the current writeable data at `rb'. If
  317. the writeable data is in one segment the second segment has zero
  318. length. */
  319. LIB_EXPORT void
  320. jack_ringbuffer_get_write_vector (const jack_ringbuffer_t * rb,
  321. jack_ringbuffer_data_t * vec)
  322. {
  323. size_t free_cnt;
  324. size_t cnt2;
  325. size_t w;
  326. w = rb->write_ptr;
  327. free_cnt = jack_ringbuffer_write_space(rb);
  328. cnt2 = w + free_cnt;
  329. if (cnt2 > rb->size) {
  330. /* Two part vector: the rest of the buffer after the current write
  331. ptr, plus some from the start of the buffer. */
  332. vec[0].buf = &(rb->buf[w]);
  333. vec[0].len = rb->size - w;
  334. vec[1].buf = rb->buf;
  335. vec[1].len = cnt2 & rb->size_mask;
  336. } else {
  337. vec[0].buf = &(rb->buf[w]);
  338. vec[0].len = free_cnt;
  339. vec[1].len = 0;
  340. }
  341. }