Add a lockless ringbuffer
[openal-soft.git] / Alc / alcRing.c
blob6ad7dd19f9dc61772cd5244ea98cee0a0d342e78
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include "config.h"
23 #include <string.h>
24 #include <stdlib.h>
26 #include "alMain.h"
27 #include "threads.h"
28 #include "compat.h"
31 struct RingBuffer {
32 ALubyte *mem;
34 ALsizei frame_size;
35 ALsizei length;
36 ALint read_pos;
37 ALint write_pos;
39 almtx_t mtx;
43 RingBuffer *CreateRingBuffer(ALsizei frame_size, ALsizei length)
45 RingBuffer *ring = calloc(1, sizeof(*ring) + ((length+1) * frame_size));
46 if(ring)
48 ring->mem = (ALubyte*)(ring+1);
50 ring->frame_size = frame_size;
51 ring->length = length+1;
52 ring->read_pos = 0;
53 ring->write_pos = 0;
55 almtx_init(&ring->mtx, almtx_plain);
57 return ring;
60 void DestroyRingBuffer(RingBuffer *ring)
62 if(ring)
64 almtx_destroy(&ring->mtx);
65 free(ring);
69 ALsizei RingBufferSize(RingBuffer *ring)
71 ALsizei s;
73 almtx_lock(&ring->mtx);
74 s = (ring->write_pos-ring->read_pos+ring->length) % ring->length;
75 almtx_unlock(&ring->mtx);
77 return s;
80 void WriteRingBuffer(RingBuffer *ring, const ALubyte *data, ALsizei len)
82 int remain;
84 almtx_lock(&ring->mtx);
86 remain = (ring->read_pos-ring->write_pos-1+ring->length) % ring->length;
87 if(remain < len) len = remain;
89 if(len > 0)
91 remain = ring->length - ring->write_pos;
92 if(remain < len)
94 memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
95 remain*ring->frame_size);
96 memcpy(ring->mem, data+(remain*ring->frame_size),
97 (len-remain)*ring->frame_size);
99 else
100 memcpy(ring->mem+(ring->write_pos*ring->frame_size), data,
101 len*ring->frame_size);
103 ring->write_pos += len;
104 ring->write_pos %= ring->length;
107 almtx_unlock(&ring->mtx);
110 void ReadRingBuffer(RingBuffer *ring, ALubyte *data, ALsizei len)
112 int remain;
114 almtx_lock(&ring->mtx);
116 remain = ring->length - ring->read_pos;
117 if(remain < len)
119 memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), remain*ring->frame_size);
120 memcpy(data+(remain*ring->frame_size), ring->mem, (len-remain)*ring->frame_size);
122 else
123 memcpy(data, ring->mem+(ring->read_pos*ring->frame_size), len*ring->frame_size);
125 ring->read_pos += len;
126 ring->read_pos %= ring->length;
128 almtx_unlock(&ring->mtx);
132 /* NOTE: This lockless ringbuffer implementation is copied from JACK, extended
133 * to include an element size. Consequently, parameters and return values for a
134 * size or count is in 'elements', not bytes. Additionally, it only supports
135 * single-consumer/single-provider opreation. */
136 struct ll_ringbuffer {
137 volatile size_t write_ptr;
138 volatile size_t read_ptr;
139 size_t size;
140 size_t size_mask;
141 size_t elem_size;
142 int mlocked;
144 alignas(16) char buf[];
147 /* Create a new ringbuffer to hold at least `sz' elements of `elem_sz' bytes.
148 * The number of elements is rounded up to the next power of two. */
149 ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz)
151 ll_ringbuffer_t *rb;
152 ALuint power_of_two;
154 power_of_two = NextPowerOf2(sz);
155 if(power_of_two < sz)
156 return NULL;
158 rb = al_malloc(16, sizeof(*rb) + power_of_two*elem_sz);
159 if(!rb) return NULL;
161 rb->size = power_of_two;
162 rb->size_mask = rb->size - 1;
163 rb->elem_size = elem_sz;
164 rb->write_ptr = 0;
165 rb->read_ptr = 0;
166 rb->mlocked = 0;
167 return rb;
170 /* Free all data associated with the ringbuffer `rb'. */
171 void ll_ringbuffer_free(ll_ringbuffer_t *rb)
173 if(rb)
175 #ifdef USE_MLOCK
176 if(rb->mlocked)
177 munlock(rb, sizeof(*rb) + rb->size*rb->elem_size);
178 #endif /* USE_MLOCK */
179 al_free(rb);
183 /* Lock the data block of `rb' using the system call 'mlock'. */
184 int ll_ringbuffer_mlock(ll_ringbuffer_t *rb)
186 #ifdef USE_MLOCK
187 if(!rb->locked && mlock(rb, sizeof(*rb) + rb->size*rb->elem_size))
188 return -1;
189 #endif /* USE_MLOCK */
190 rb->mlocked = 1;
191 return 0;
194 /* Reset the read and write pointers to zero. This is not thread safe. */
195 void ll_ringbuffer_reset(ll_ringbuffer_t *rb)
197 rb->read_ptr = 0;
198 rb->write_ptr = 0;
199 memset(rb->buf, 0, rb->size*rb->elem_size);
202 /* Return the number of bytes available for reading. This is the number of
203 * bytes in front of the read pointer and behind the write pointer. */
204 size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb)
206 size_t w = rb->write_ptr;
207 size_t r = rb->read_ptr;
208 return (rb->size+w-r) & rb->size_mask;
210 /* Return the number of bytes available for writing. This is the number of
211 * bytes in front of the write pointer and behind the read pointer. */
212 size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb)
214 size_t w = rb->write_ptr;
215 size_t r = rb->read_ptr;
216 return (rb->size+r-w-1) & rb->size_mask;
219 /* The copying data reader. Copy at most `cnt' bytes from `rb' to `dest'.
220 * Returns the actual number of bytes copied. */
221 size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt)
223 size_t free_cnt;
224 size_t cnt2;
225 size_t to_read;
226 size_t n1, n2;
228 free_cnt = ll_ringbuffer_read_space(rb);
229 if(free_cnt == 0) return 0;
231 to_read = (cnt > free_cnt) ? free_cnt : cnt;
232 cnt2 = rb->read_ptr + to_read;
233 if(cnt2 > rb->size)
235 n1 = rb->size - rb->read_ptr;
236 n2 = cnt2 & rb->size_mask;
238 else
240 n1 = to_read;
241 n2 = 0;
244 memcpy(dest, &(rb->buf[rb->read_ptr*rb->elem_size]), n1*rb->elem_size);
245 rb->read_ptr = (rb->read_ptr + n1) & rb->size_mask;
246 if(n2)
248 memcpy(dest + n1*rb->elem_size, &(rb->buf[rb->read_ptr*rb->elem_size]), n2*rb->elem_size);
249 rb->read_ptr = (rb->read_ptr + n2) & rb->size_mask;
251 return to_read;
254 /* The copying data reader w/o read pointer advance. Copy at most `cnt' bytes
255 * from `rb' to `dest'. Returns the actual number of bytes copied. */
256 size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt)
258 size_t free_cnt;
259 size_t cnt2;
260 size_t to_read;
261 size_t n1, n2;
262 size_t tmp_read_ptr;
264 tmp_read_ptr = rb->read_ptr;
265 free_cnt = ll_ringbuffer_read_space(rb);
266 if(free_cnt == 0) return 0;
268 to_read = (cnt > free_cnt) ? free_cnt : cnt;
269 cnt2 = tmp_read_ptr + to_read;
270 if(cnt2 > rb->size)
272 n1 = rb->size - tmp_read_ptr;
273 n2 = cnt2 & rb->size_mask;
275 else
277 n1 = to_read;
278 n2 = 0;
281 memcpy(dest, &(rb->buf[tmp_read_ptr*rb->elem_size]), n1*rb->elem_size);
282 tmp_read_ptr = (tmp_read_ptr + n1) & rb->size_mask;
283 if(n2)
284 memcpy(dest + n1*rb->elem_size, &(rb->buf[tmp_read_ptr*rb->elem_size]), n2*rb->elem_size);
285 return to_read;
288 /* The copying data writer. Copy at most `cnt' bytes to `rb' from `src'.
289 * Returns the actual number of bytes copied. */
290 size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt)
292 size_t free_cnt;
293 size_t cnt2;
294 size_t to_write;
295 size_t n1, n2;
297 free_cnt = ll_ringbuffer_write_space(rb);
298 if(free_cnt == 0) return 0;
300 to_write = (cnt > free_cnt) ? free_cnt : cnt;
301 cnt2 = rb->write_ptr + to_write;
302 if(cnt2 > rb->size)
304 n1 = rb->size - rb->write_ptr;
305 n2 = cnt2 & rb->size_mask;
307 else
309 n1 = to_write;
310 n2 = 0;
313 memcpy(&(rb->buf[rb->write_ptr*rb->elem_size]), src, n1*rb->elem_size);
314 rb->write_ptr = (rb->write_ptr + n1) & rb->size_mask;
315 if(n2)
317 memcpy(&(rb->buf[rb->write_ptr*rb->elem_size]), src + n1*rb->elem_size, n2*rb->elem_size);
318 rb->write_ptr = (rb->write_ptr + n2) & rb->size_mask;
320 return to_write;
323 /* Advance the read pointer `cnt' places. */
324 void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt)
326 size_t tmp = (rb->read_ptr + cnt) & rb->size_mask;
327 rb->read_ptr = tmp;
330 /* Advance the write pointer `cnt' places. */
331 void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt)
333 size_t tmp = (rb->write_ptr + cnt) & rb->size_mask;
334 rb->write_ptr = tmp;
337 /* The non-copying data reader. `vec' is an array of two places. Set the values
338 * at `vec' to hold the current readable data at `rb'. If the readable data is
339 * in one segment the second segment has zero length. */
340 void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t * vec)
342 size_t free_cnt;
343 size_t cnt2;
344 size_t w, r;
346 w = rb->write_ptr;
347 r = rb->read_ptr;
348 free_cnt = (rb->size+w-r) & rb->size_mask;
350 cnt2 = r + free_cnt;
351 if(cnt2 > rb->size)
353 /* Two part vector: the rest of the buffer after the current write ptr,
354 * plus some from the start of the buffer. */
355 vec[0].buf = (char*)&(rb->buf[r*rb->elem_size]);
356 vec[0].len = rb->size - r;
357 vec[1].buf = (char*)rb->buf;
358 vec[1].len = cnt2 & rb->size_mask;
360 else
362 /* Single part vector: just the rest of the buffer */
363 vec[0].buf = (char*)&(rb->buf[r*rb->elem_size]);
364 vec[0].len = free_cnt;
365 vec[1].buf = NULL;
366 vec[1].len = 0;
370 /* The non-copying data writer. `vec' is an array of two places. Set the values
371 * at `vec' to hold the current writeable data at `rb'. If the writeable data
372 * is in one segment the second segment has zero length. */
373 void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec)
375 size_t free_cnt;
376 size_t cnt2;
377 size_t w, r;
379 w = rb->write_ptr;
380 r = rb->read_ptr;
381 free_cnt = (rb->size+r-w-1) & rb->size_mask;
383 cnt2 = w + free_cnt;
384 if(cnt2 > rb->size)
386 /* Two part vector: the rest of the buffer after the current write ptr,
387 * plus some from the start of the buffer. */
388 vec[0].buf = (char*)&(rb->buf[w*rb->elem_size]);
389 vec[0].len = rb->size - w;
390 vec[1].buf = (char*)rb->buf;
391 vec[1].len = cnt2 & rb->size_mask;
393 else
395 vec[0].buf = (char*)&(rb->buf[w*rb->elem_size]);
396 vec[0].len = free_cnt;
397 vec[1].buf = NULL;
398 vec[1].len = 0;