introspection: add missing return value annotations
[platform/upstream/gstreamer.git] / gst / gstatomicqueue.c
1 /* GStreamer
2  * Copyright (C) 2009 Edward Hervey <bilboed@bilboed.com>
3  *               2011 Wim Taymans <wim.taymans@gmail.com>
4  *
5  * gstatomicqueue.c:
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include "gst_private.h"
24
25 #include <string.h>
26
27 #include <gst/gst.h>
28 #include "gstatomicqueue.h"
29 #include "glib-compat-private.h"
30
31 /**
32  * SECTION:gstatomicqueue
33  * @title: GstAtomicQueue
34  * @short_description: An atomic queue implementation
35  *
36  * The #GstAtomicQueue object implements a queue that can be used from multiple
37  * threads without performing any blocking operations.
38  *
39  * Since: 0.10.33
40  */
41
42 G_DEFINE_BOXED_TYPE (GstAtomicQueue, gst_atomic_queue,
43     (GBoxedCopyFunc) gst_atomic_queue_ref,
44     (GBoxedFreeFunc) gst_atomic_queue_unref);
45
46 /* By default the queue uses 2 * sizeof(gpointer) * clp2 (max_items) of
47  * memory. clp2(x) is the next power of two >= than x.
48  *
49  * The queue can operate in low memory mode, in which it consumes almost
50  * half the memory at the expense of extra overhead in the readers. This
51  * is disabled by default because even without LOW_MEM mode, the memory
52  * consumption is still lower than a plain GList.
53  */
54 #undef LOW_MEM
55
56 typedef struct _GstAQueueMem GstAQueueMem;
57
58 struct _GstAQueueMem
59 {
60   gint size;
61   gpointer *array;
62   volatile gint head;
63   volatile gint tail_write;
64   volatile gint tail_read;
65   GstAQueueMem *next;
66   GstAQueueMem *free;
67 };
68
69 static guint
70 clp2 (guint n)
71 {
72   guint res = 1;
73
74   while (res < n)
75     res <<= 1;
76
77   return res;
78 }
79
80 static GstAQueueMem *
81 new_queue_mem (guint size, gint pos)
82 {
83   GstAQueueMem *mem;
84
85   mem = g_new (GstAQueueMem, 1);
86
87   /* we keep the size as a mask for performance */
88   mem->size = clp2 (MAX (size, 16)) - 1;
89   mem->array = g_new0 (gpointer, mem->size + 1);
90   mem->head = pos;
91   mem->tail_write = pos;
92   mem->tail_read = pos;
93   mem->next = NULL;
94   mem->free = NULL;
95
96   return mem;
97 }
98
99 static void
100 free_queue_mem (GstAQueueMem * mem)
101 {
102   g_free (mem->array);
103   g_free (mem);
104 }
105
106 struct _GstAtomicQueue
107 {
108   volatile gint refcount;
109 #ifdef LOW_MEM
110   gint num_readers;
111 #endif
112   GstAQueueMem *head_mem;
113   GstAQueueMem *tail_mem;
114   GstAQueueMem *free_list;
115 };
116
117 static void
118 add_to_free_list (GstAtomicQueue * queue, GstAQueueMem * mem)
119 {
120   do {
121     mem->free = g_atomic_pointer_get (&queue->free_list);
122   } while (!g_atomic_pointer_compare_and_exchange (&queue->free_list,
123           mem->free, mem));
124 }
125
126 static void
127 clear_free_list (GstAtomicQueue * queue)
128 {
129   GstAQueueMem *free_list;
130
131   /* take the free list and replace with NULL */
132   do {
133     free_list = g_atomic_pointer_get (&queue->free_list);
134     if (free_list == NULL)
135       return;
136   } while (!g_atomic_pointer_compare_and_exchange (&queue->free_list, free_list,
137           NULL));
138
139   while (free_list) {
140     GstAQueueMem *next = free_list->free;
141
142     free_queue_mem (free_list);
143
144     free_list = next;
145   }
146 }
147
148 /**
149  * gst_atomic_queue_new:
150  * @initial_size: initial queue size
151  *
152  * Create a new atomic queue instance. @initial_size will be rounded up to the
153  * nearest power of 2 and used as the initial size of the queue.
154  *
155  * Returns: a new #GstAtomicQueue
156  *
157  * Since: 0.10.33
158  */
159 GstAtomicQueue *
160 gst_atomic_queue_new (guint initial_size)
161 {
162   GstAtomicQueue *queue;
163
164   queue = g_new (GstAtomicQueue, 1);
165
166   queue->refcount = 1;
167 #ifdef LOW_MEM
168   queue->num_readers = 0;
169 #endif
170   queue->head_mem = queue->tail_mem = new_queue_mem (initial_size, 0);
171   queue->free_list = NULL;
172
173   return queue;
174 }
175
176 /**
177  * gst_atomic_queue_ref:
178  * @queue: a #GstAtomicQueue
179  *
180  * Increase the refcount of @queue.
181  *
182  * Since: 0.10.33
183  */
184 void
185 gst_atomic_queue_ref (GstAtomicQueue * queue)
186 {
187   g_return_if_fail (queue != NULL);
188
189   g_atomic_int_inc (&queue->refcount);
190 }
191
192 static void
193 gst_atomic_queue_free (GstAtomicQueue * queue)
194 {
195   free_queue_mem (queue->head_mem);
196   if (queue->head_mem != queue->tail_mem)
197     free_queue_mem (queue->tail_mem);
198   clear_free_list (queue);
199   g_free (queue);
200 }
201
202 /**
203  * gst_atomic_queue_unref:
204  * @queue: a #GstAtomicQueue
205  *
206  * Unref @queue and free the memory when the refcount reaches 0.
207  *
208  * Since: 0.10.33
209  */
210 void
211 gst_atomic_queue_unref (GstAtomicQueue * queue)
212 {
213   g_return_if_fail (queue != NULL);
214
215   if (g_atomic_int_dec_and_test (&queue->refcount))
216     gst_atomic_queue_free (queue);
217 }
218
219 /**
220  * gst_atomic_queue_peek:
221  * @queue: a #GstAtomicQueue
222  *
223  * Peek the head element of the queue without removing it from the queue.
224  *
225  * Returns: (transfer none): the head element of @queue or NULL when
226  * the queue is empty.
227  *
228  * Since: 0.10.33
229  */
230 gpointer
231 gst_atomic_queue_peek (GstAtomicQueue * queue)
232 {
233   GstAQueueMem *head_mem;
234   gint head, tail, size;
235
236   g_return_val_if_fail (queue != NULL, NULL);
237
238   while (TRUE) {
239     GstAQueueMem *next;
240
241     head_mem = g_atomic_pointer_get (&queue->head_mem);
242
243     head = g_atomic_int_get (&head_mem->head);
244     tail = g_atomic_int_get (&head_mem->tail_read);
245     size = head_mem->size;
246
247     /* when we are not empty, we can continue */
248     if (G_LIKELY (head != tail))
249       break;
250
251     /* else array empty, try to take next */
252     next = g_atomic_pointer_get (&head_mem->next);
253     if (next == NULL)
254       return NULL;
255
256     /* now we try to move the next array as the head memory. If we fail to do that,
257      * some other reader managed to do it first and we retry */
258     if (!g_atomic_pointer_compare_and_exchange (&queue->head_mem, head_mem,
259             next))
260       continue;
261
262     /* when we managed to swing the head pointer the old head is now
263      * useless and we add it to the freelist. We can't free the memory yet
264      * because we first need to make sure no reader is accessing it anymore. */
265     add_to_free_list (queue, head_mem);
266   }
267
268   return head_mem->array[head & size];
269 }
270
271 /**
272  * gst_atomic_queue_pop:
273  * @queue: a #GstAtomicQueue
274  *
275  * Get the head element of the queue.
276  *
277  * Returns: (transfer full): the head element of @queue or NULL when
278  * the queue is empty.
279  *
280  * Since: 0.10.33
281  */
282 gpointer
283 gst_atomic_queue_pop (GstAtomicQueue * queue)
284 {
285   gpointer ret;
286   GstAQueueMem *head_mem;
287   gint head, tail, size;
288
289   g_return_val_if_fail (queue != NULL, NULL);
290
291 #ifdef LOW_MEM
292   g_atomic_int_inc (&queue->num_readers);
293 #endif
294
295   do {
296     while (TRUE) {
297       GstAQueueMem *next;
298
299       head_mem = g_atomic_pointer_get (&queue->head_mem);
300
301       head = g_atomic_int_get (&head_mem->head);
302       tail = g_atomic_int_get (&head_mem->tail_read);
303       size = head_mem->size;
304
305       /* when we are not empty, we can continue */
306       if G_LIKELY
307         (head != tail)
308             break;
309
310       /* else array empty, try to take next */
311       next = g_atomic_pointer_get (&head_mem->next);
312       if (next == NULL)
313         return NULL;
314
315       /* now we try to move the next array as the head memory. If we fail to do that,
316        * some other reader managed to do it first and we retry */
317       if G_UNLIKELY
318         (!g_atomic_pointer_compare_and_exchange (&queue->head_mem, head_mem,
319                 next))
320             continue;
321
322       /* when we managed to swing the head pointer the old head is now
323        * useless and we add it to the freelist. We can't free the memory yet
324        * because we first need to make sure no reader is accessing it anymore. */
325       add_to_free_list (queue, head_mem);
326     }
327
328     ret = head_mem->array[head & size];
329   } while G_UNLIKELY
330   (!g_atomic_int_compare_and_exchange (&head_mem->head, head, head + 1));
331
332 #ifdef LOW_MEM
333   /* decrement number of readers, when we reach 0 readers we can be sure that
334    * none is accessing the memory in the free list and we can try to clean up */
335   if (g_atomic_int_dec_and_test (&queue->num_readers))
336     clear_free_list (queue);
337 #endif
338
339   return ret;
340 }
341
342 /**
343  * gst_atomic_queue_push:
344  * @queue: a #GstAtomicQueue
345  * @data: the data
346  *
347  * Append @data to the tail of the queue.
348  *
349  * Since: 0.10.33
350  */
351 void
352 gst_atomic_queue_push (GstAtomicQueue * queue, gpointer data)
353 {
354   GstAQueueMem *tail_mem;
355   gint head, tail, size;
356
357   g_return_if_fail (queue != NULL);
358
359   do {
360     while (TRUE) {
361       GstAQueueMem *mem;
362
363       tail_mem = g_atomic_pointer_get (&queue->tail_mem);
364       head = g_atomic_int_get (&tail_mem->head);
365       tail = g_atomic_int_get (&tail_mem->tail_write);
366       size = tail_mem->size;
367
368       /* we're not full, continue */
369       if G_LIKELY
370         (tail - head <= size)
371             break;
372
373       /* else we need to grow the array, we store a mask so we have to add 1 */
374       mem = new_queue_mem ((size << 1) + 1, tail);
375
376       /* try to make our new array visible to other writers */
377       if G_UNLIKELY
378         (!g_atomic_pointer_compare_and_exchange (&queue->tail_mem, tail_mem,
379                 mem)) {
380         /* we tried to swap the new writer array but something changed. This is
381          * because some other writer beat us to it, we free our memory and try
382          * again */
383         free_queue_mem (mem);
384         continue;
385         }
386       /* make sure that readers can find our new array as well. The one who
387        * manages to swap the pointer is the only one who can set the next
388        * pointer to the new array */
389       g_atomic_pointer_set (&tail_mem->next, mem);
390     }
391   } while G_UNLIKELY
392   (!g_atomic_int_compare_and_exchange (&tail_mem->tail_write, tail, tail + 1));
393
394   tail_mem->array[tail & size] = data;
395
396   /* now wait until all writers have completed their write before we move the
397    * tail_read to this new item. It is possible that other writers are still
398    * updating the previous array slots and we don't want to reveal their changes
399    * before they are done. FIXME, it would be nice if we didn't have to busy
400    * wait here. */
401   while G_UNLIKELY
402     (!g_atomic_int_compare_and_exchange (&tail_mem->tail_read, tail, tail + 1));
403 }
404
405 /**
406  * gst_atomic_queue_length:
407  * @queue: a #GstAtomicQueue
408  *
409  * Get the amount of items in the queue.
410  *
411  * Returns: the number of elements in the queue.
412  *
413  * Since: 0.10.33
414  */
415 guint
416 gst_atomic_queue_length (GstAtomicQueue * queue)
417 {
418   GstAQueueMem *head_mem, *tail_mem;
419   gint head, tail;
420
421   g_return_val_if_fail (queue != NULL, 0);
422
423 #ifdef LOW_MEM
424   g_atomic_int_inc (&queue->num_readers);
425 #endif
426
427   head_mem = g_atomic_pointer_get (&queue->head_mem);
428   head = g_atomic_int_get (&head_mem->head);
429
430   tail_mem = g_atomic_pointer_get (&queue->tail_mem);
431   tail = g_atomic_int_get (&tail_mem->tail_read);
432
433 #ifdef LOW_MEM
434   if (g_atomic_int_dec_and_test (&queue->num_readers))
435     clear_free_list (queue);
436 #endif
437
438   return tail - head;
439 }