Update some atomic memory ordering
[openal-soft.git] / Alc / alcRing.c
blob2cb001bff7e2361ca74f1b729e429edfb2b2257f
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 "almalloc.h"
29 #include "compat.h"
32 /* NOTE: This lockless ringbuffer implementation is copied from JACK, extended
33 * to include an element size. Consequently, parameters and return values for a
34 * size or count is in 'elements', not bytes. Additionally, it only supports
35 * single-consumer/single-provider operation. */
36 struct ll_ringbuffer {
37 ATOMIC(size_t) write_ptr;
38 ATOMIC(size_t) read_ptr;
39 size_t size;
40 size_t size_mask;
41 size_t elem_size;
42 int mlocked;
44 alignas(16) char buf[];
47 /* Create a new ringbuffer to hold at least `sz' elements of `elem_sz' bytes.
48 * The number of elements is rounded up to the next power of two. */
49 ll_ringbuffer_t *ll_ringbuffer_create(size_t sz, size_t elem_sz)
51 ll_ringbuffer_t *rb;
52 ALuint power_of_two;
54 power_of_two = NextPowerOf2(sz);
55 if(power_of_two < sz)
56 return NULL;
58 rb = al_malloc(16, sizeof(*rb) + power_of_two*elem_sz);
59 if(!rb) return NULL;
61 ATOMIC_INIT(&rb->write_ptr, 0);
62 ATOMIC_INIT(&rb->read_ptr, 0);
63 rb->size = power_of_two;
64 rb->size_mask = rb->size - 1;
65 rb->elem_size = elem_sz;
66 rb->mlocked = 0;
67 return rb;
70 /* Free all data associated with the ringbuffer `rb'. */
71 void ll_ringbuffer_free(ll_ringbuffer_t *rb)
73 if(rb)
75 #ifdef USE_MLOCK
76 if(rb->mlocked)
77 munlock(rb, sizeof(*rb) + rb->size*rb->elem_size);
78 #endif /* USE_MLOCK */
79 al_free(rb);
83 /* Lock the data block of `rb' using the system call 'mlock'. */
84 int ll_ringbuffer_mlock(ll_ringbuffer_t *rb)
86 #ifdef USE_MLOCK
87 if(!rb->mlocked && mlock(rb, sizeof(*rb) + rb->size*rb->elem_size))
88 return -1;
89 #endif /* USE_MLOCK */
90 rb->mlocked = 1;
91 return 0;
94 /* Reset the read and write pointers to zero. This is not thread safe. */
95 void ll_ringbuffer_reset(ll_ringbuffer_t *rb)
97 ATOMIC_STORE(&rb->write_ptr, 0, almemory_order_release);
98 ATOMIC_STORE(&rb->read_ptr, 0, almemory_order_release);
99 memset(rb->buf, 0, rb->size*rb->elem_size);
102 /* Return the number of elements available for reading. This is the number of
103 * elements in front of the read pointer and behind the write pointer. */
104 size_t ll_ringbuffer_read_space(const ll_ringbuffer_t *rb)
106 size_t w = ATOMIC_LOAD(&rb->write_ptr, almemory_order_acquire) & rb->size_mask;
107 size_t r = ATOMIC_LOAD(&rb->read_ptr, almemory_order_acquire) & rb->size_mask;
108 return (w-r) & rb->size_mask;
110 /* Return the number of elements available for writing. This is the number of
111 * elements in front of the write pointer and behind the read pointer. */
112 size_t ll_ringbuffer_write_space(const ll_ringbuffer_t *rb)
114 size_t w = ATOMIC_LOAD(&rb->write_ptr, almemory_order_acquire) & rb->size_mask;
115 size_t r = ATOMIC_LOAD(&rb->read_ptr, almemory_order_acquire) & rb->size_mask;
116 return (r-w-1) & rb->size_mask;
119 /* The copying data reader. Copy at most `cnt' elements from `rb' to `dest'.
120 * Returns the actual number of elements copied. */
121 size_t ll_ringbuffer_read(ll_ringbuffer_t *rb, char *dest, size_t cnt)
123 size_t read_ptr;
124 size_t free_cnt;
125 size_t cnt2;
126 size_t to_read;
127 size_t n1, n2;
129 free_cnt = ll_ringbuffer_read_space(rb);
130 if(free_cnt == 0) return 0;
132 to_read = (cnt > free_cnt) ? free_cnt : cnt;
133 read_ptr = ATOMIC_LOAD(&rb->read_ptr, almemory_order_relaxed) & rb->size_mask;
135 cnt2 = read_ptr + to_read;
136 if(cnt2 > rb->size)
138 n1 = rb->size - read_ptr;
139 n2 = cnt2 & rb->size_mask;
141 else
143 n1 = to_read;
144 n2 = 0;
147 memcpy(dest, &rb->buf[read_ptr*rb->elem_size], n1*rb->elem_size);
148 read_ptr += n1;
149 if(n2)
151 memcpy(dest + n1*rb->elem_size, &rb->buf[(read_ptr&rb->size_mask)*rb->elem_size],
152 n2*rb->elem_size);
153 read_ptr += n2;
155 ATOMIC_STORE(&rb->read_ptr, read_ptr, almemory_order_release);
156 return to_read;
159 /* The copying data reader w/o read pointer advance. Copy at most `cnt'
160 * elements from `rb' to `dest'. Returns the actual number of elements copied.
162 size_t ll_ringbuffer_peek(ll_ringbuffer_t *rb, char *dest, size_t cnt)
164 size_t free_cnt;
165 size_t cnt2;
166 size_t to_read;
167 size_t n1, n2;
168 size_t read_ptr;
170 free_cnt = ll_ringbuffer_read_space(rb);
171 if(free_cnt == 0) return 0;
173 to_read = (cnt > free_cnt) ? free_cnt : cnt;
174 read_ptr = ATOMIC_LOAD(&rb->read_ptr, almemory_order_relaxed) & rb->size_mask;
176 cnt2 = read_ptr + to_read;
177 if(cnt2 > rb->size)
179 n1 = rb->size - read_ptr;
180 n2 = cnt2 & rb->size_mask;
182 else
184 n1 = to_read;
185 n2 = 0;
188 memcpy(dest, &rb->buf[read_ptr*rb->elem_size], n1*rb->elem_size);
189 if(n2)
191 read_ptr += n1;
192 memcpy(dest + n1*rb->elem_size, &rb->buf[(read_ptr&rb->size_mask)*rb->elem_size],
193 n2*rb->elem_size);
195 return to_read;
198 /* The copying data writer. Copy at most `cnt' elements to `rb' from `src'.
199 * Returns the actual number of elements copied. */
200 size_t ll_ringbuffer_write(ll_ringbuffer_t *rb, const char *src, size_t cnt)
202 size_t write_ptr;
203 size_t free_cnt;
204 size_t cnt2;
205 size_t to_write;
206 size_t n1, n2;
208 free_cnt = ll_ringbuffer_write_space(rb);
209 if(free_cnt == 0) return 0;
211 to_write = (cnt > free_cnt) ? free_cnt : cnt;
212 write_ptr = ATOMIC_LOAD(&rb->write_ptr, almemory_order_relaxed) & rb->size_mask;
214 cnt2 = write_ptr + to_write;
215 if(cnt2 > rb->size)
217 n1 = rb->size - write_ptr;
218 n2 = cnt2 & rb->size_mask;
220 else
222 n1 = to_write;
223 n2 = 0;
226 memcpy(&rb->buf[write_ptr*rb->elem_size], src, n1*rb->elem_size);
227 write_ptr += n1;
228 if(n2)
230 memcpy(&rb->buf[(write_ptr&rb->size_mask)*rb->elem_size], src + n1*rb->elem_size,
231 n2*rb->elem_size);
232 write_ptr += n2;
234 ATOMIC_STORE(&rb->write_ptr, write_ptr, almemory_order_release);
235 return to_write;
238 /* Advance the read pointer `cnt' places. */
239 void ll_ringbuffer_read_advance(ll_ringbuffer_t *rb, size_t cnt)
241 ATOMIC_ADD(&rb->read_ptr, cnt, almemory_order_acq_rel);
244 /* Advance the write pointer `cnt' places. */
245 void ll_ringbuffer_write_advance(ll_ringbuffer_t *rb, size_t cnt)
247 ATOMIC_ADD(&rb->write_ptr, cnt, almemory_order_acq_rel);
250 /* The non-copying data reader. `vec' is an array of two places. Set the values
251 * at `vec' to hold the current readable data at `rb'. If the readable data is
252 * in one segment the second segment has zero length. */
253 void ll_ringbuffer_get_read_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t * vec)
255 size_t free_cnt;
256 size_t cnt2;
257 size_t w, r;
259 w = ATOMIC_LOAD(&rb->write_ptr, almemory_order_acquire) & rb->size_mask;
260 r = ATOMIC_LOAD(&rb->read_ptr, almemory_order_acquire) & rb->size_mask;
261 free_cnt = (w-r) & rb->size_mask;
263 cnt2 = r + free_cnt;
264 if(cnt2 > rb->size)
266 /* Two part vector: the rest of the buffer after the current write ptr,
267 * plus some from the start of the buffer. */
268 vec[0].buf = (char*)&rb->buf[r*rb->elem_size];
269 vec[0].len = rb->size - r;
270 vec[1].buf = (char*)rb->buf;
271 vec[1].len = cnt2 & rb->size_mask;
273 else
275 /* Single part vector: just the rest of the buffer */
276 vec[0].buf = (char*)&rb->buf[r*rb->elem_size];
277 vec[0].len = free_cnt;
278 vec[1].buf = NULL;
279 vec[1].len = 0;
283 /* The non-copying data writer. `vec' is an array of two places. Set the values
284 * at `vec' to hold the current writeable data at `rb'. If the writeable data
285 * is in one segment the second segment has zero length. */
286 void ll_ringbuffer_get_write_vector(const ll_ringbuffer_t *rb, ll_ringbuffer_data_t *vec)
288 size_t free_cnt;
289 size_t cnt2;
290 size_t w, r;
292 w = ATOMIC_LOAD(&rb->write_ptr, almemory_order_acquire) & rb->size_mask;
293 r = ATOMIC_LOAD(&rb->read_ptr, almemory_order_acquire) & rb->size_mask;
294 free_cnt = (r-w-1) & rb->size_mask;
296 cnt2 = w + free_cnt;
297 if(cnt2 > rb->size)
299 /* Two part vector: the rest of the buffer after the current write ptr,
300 * plus some from the start of the buffer. */
301 vec[0].buf = (char*)&rb->buf[w*rb->elem_size];
302 vec[0].len = rb->size - w;
303 vec[1].buf = (char*)rb->buf;
304 vec[1].len = cnt2 & rb->size_mask;
306 else
308 vec[0].buf = (char*)&rb->buf[w*rb->elem_size];
309 vec[0].len = free_cnt;
310 vec[1].buf = NULL;
311 vec[1].len = 0;