2e8b6b8c3a29d684454867dacabb0c1806430094
[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
30 /**
31  * SECTION:gstatomicqueue
32  * @title: GstAtomicQueue
33  * @short_description: An atomic queue implementation
34  *
35  * The #GstAtomicQueue object implements a queue that can be used from multiple
36  * threads without performing any blocking operations.
37  *
38  * Since: 0.10.33
39  */
40
41 /* By default the queue uses 2 * sizeof(gpointer) * clp2 (max_items) of
42  * memory. clp2(x) is the next power of two >= than x.
43  *
44  * The queue can operate in low memory mode, in which it consumes almost
45  * half the memory at the expense of extra overhead in the readers. This
46  * is disabled by default because even without LOW_MEM mode, the memory
47  * consumption is still lower than a plain GList.
48  */
49 #undef LOW_MEM
50
51 typedef struct _GstAQueueMem GstAQueueMem;
52
53 struct _GstAQueueMem
54 {
55   gint size;
56   gpointer *array;
57   volatile gint head;
58   volatile gint tail;
59   GstAQueueMem *next;
60   GstAQueueMem *free;
61 };
62
63 static guint
64 clp2 (guint n)
65 {
66   guint res = 1;
67
68   while (res < n)
69     res <<= 1;
70
71   return res;
72 }
73
74 static GstAQueueMem *
75 new_queue_mem (guint size, gint pos)
76 {
77   GstAQueueMem *mem;
78
79   mem = g_new (GstAQueueMem, 1);
80
81   /* we keep the size as a mask for performance */
82   mem->size = clp2 (MAX (size, 16)) - 1;
83   mem->array = g_new0 (gpointer, mem->size + 1);
84   mem->head = pos;
85   mem->tail = pos;
86   mem->next = NULL;
87   mem->free = NULL;
88
89   return mem;
90 }
91
92 static void
93 free_queue_mem (GstAQueueMem * mem)
94 {
95   g_free (mem->array);
96   g_free (mem);
97 }
98
99 struct _GstAtomicQueue
100 {
101   volatile gint refcount;
102 #ifdef LOW_MEM
103   gint num_readers;
104 #endif
105   GstAQueueMem *head_mem;
106   GstAQueueMem *tail_mem;
107   GstAQueueMem *free_list;
108 };
109
110 static void
111 add_to_free_list (GstAtomicQueue * queue, GstAQueueMem * mem)
112 {
113   do {
114     mem->free = g_atomic_pointer_get (&queue->free_list);
115   } while (!g_atomic_pointer_compare_and_exchange ((gpointer *) &
116           queue->free_list, mem->free, mem));
117 }
118
119 static void
120 clear_free_list (GstAtomicQueue * queue)
121 {
122   GstAQueueMem *free_list;
123
124   /* take the free list and replace with NULL */
125   do {
126     free_list = g_atomic_pointer_get (&queue->free_list);
127     if (free_list == NULL)
128       return;
129   } while (!g_atomic_pointer_compare_and_exchange ((gpointer *) &
130           queue->free_list, free_list, NULL));
131
132   while (free_list) {
133     GstAQueueMem *next = free_list->free;
134
135     free_queue_mem (free_list);
136
137     free_list = next;
138   }
139 }
140
141 /**
142  * gst_atomic_queue_new:
143  * @initial_size: initial queue size
144  *
145  * Create a new atomic queue instance. @initial_size will be rounded up to the
146  * nearest power of 2 and used as the initial size of the queue.
147  *
148  * Returns: a new #GstAtomicQueue
149  *
150  * Since: 0.10.33
151  */
152 GstAtomicQueue *
153 gst_atomic_queue_new (guint initial_size)
154 {
155   GstAtomicQueue *queue;
156
157   queue = g_new (GstAtomicQueue, 1);
158
159   queue->refcount = 1;
160 #ifdef LOW_MEM
161   queue->num_readers = 0;
162 #endif
163   queue->head_mem = queue->tail_mem = new_queue_mem (initial_size, 0);
164   queue->free_list = NULL;
165
166   return queue;
167 }
168
169 /**
170  * gst_atomic_queue_ref:
171  * @queue: a #GstAtomicQueue
172  *
173  * Increase the refcount of @queue.
174  */
175 void
176 gst_atomic_queue_ref (GstAtomicQueue * queue)
177 {
178   g_return_if_fail (queue != NULL);
179
180   g_atomic_int_inc (&queue->refcount);
181 }
182
183 static void
184 gst_atomic_queue_free (GstAtomicQueue * queue)
185 {
186   free_queue_mem (queue->head_mem);
187   if (queue->head_mem != queue->tail_mem)
188     free_queue_mem (queue->tail_mem);
189   clear_free_list (queue);
190   g_free (queue);
191 }
192
193 /**
194  * gst_atomic_queue_unref:
195  * @queue: a #GstAtomicQueue
196  *
197  * Unref @queue and free the memory when the refcount reaches 0.
198  */
199 void
200 gst_atomic_queue_unref (GstAtomicQueue * queue)
201 {
202   g_return_if_fail (queue != NULL);
203
204   if (g_atomic_int_dec_and_test (&queue->refcount))
205     gst_atomic_queue_free (queue);
206 }
207
208 /**
209  * gst_atomic_queue_peek:
210  * @queue: a #GstAtomicQueue
211  *
212  * Peek the head element of the queue without removing it from the queue.
213  *
214  * Returns: the head element of @queue or NULL when the queue is empty.
215  */
216 gpointer
217 gst_atomic_queue_peek (GstAtomicQueue * queue)
218 {
219   GstAQueueMem *head_mem;
220   gint head, tail, size;
221
222   g_return_val_if_fail (queue != NULL, NULL);
223
224   while (TRUE) {
225     GstAQueueMem *next;
226
227     head_mem = g_atomic_pointer_get (&queue->head_mem);
228
229     head = g_atomic_int_get (&head_mem->head);
230     tail = g_atomic_int_get (&head_mem->tail);
231     size = head_mem->size;
232
233     /* when we are not empty, we can continue */
234     if (G_LIKELY (head != tail))
235       break;
236
237     /* else array empty, try to take next */
238     next = g_atomic_pointer_get (&head_mem->next);
239     if (next == NULL)
240       return NULL;
241
242     /* now we try to move the next array as the head memory. If we fail to do that,
243      * some other reader managed to do it first and we retry */
244     if (!g_atomic_pointer_compare_and_exchange ((gpointer *) &
245             queue->head_mem, head_mem, next))
246       continue;
247
248     /* when we managed to swing the head pointer the old head is now
249      * useless and we add it to the freelist. We can't free the memory yet
250      * because we first need to make sure no reader is accessing it anymore. */
251     add_to_free_list (queue, head_mem);
252   }
253
254   return head_mem->array[head & size];
255 }
256
257 /**
258  * gst_atomic_queue_pop:
259  * @queue: a #GstAtomicQueue
260  *
261  * Get the head element of the queue.
262  *
263  * Returns: the head element of @queue or NULL when the queue is empty.
264  */
265 gpointer
266 gst_atomic_queue_pop (GstAtomicQueue * queue)
267 {
268   gpointer ret;
269   GstAQueueMem *head_mem;
270   gint head, tail, size;
271
272   g_return_val_if_fail (queue != NULL, NULL);
273
274 #ifdef LOW_MEM
275   g_atomic_int_inc (&queue->num_readers);
276 #endif
277
278   do {
279     while (TRUE) {
280       GstAQueueMem *next;
281
282       head_mem = g_atomic_pointer_get (&queue->head_mem);
283
284       head = g_atomic_int_get (&head_mem->head);
285       tail = g_atomic_int_get (&head_mem->tail);
286       size = head_mem->size;
287
288       /* when we are not empty, we can continue */
289       if (G_LIKELY (head != tail))
290         break;
291
292       /* else array empty, try to take next */
293       next = g_atomic_pointer_get (&head_mem->next);
294       if (next == NULL)
295         return NULL;
296
297       /* now we try to move the next array as the head memory. If we fail to do that,
298        * some other reader managed to do it first and we retry */
299       if (!g_atomic_pointer_compare_and_exchange ((gpointer *) &
300               queue->head_mem, head_mem, next))
301         continue;
302
303       /* when we managed to swing the head pointer the old head is now
304        * useless and we add it to the freelist. We can't free the memory yet
305        * because we first need to make sure no reader is accessing it anymore. */
306       add_to_free_list (queue, head_mem);
307     }
308
309     ret = head_mem->array[head & size];
310   } while (!g_atomic_int_compare_and_exchange (&head_mem->head, head,
311           head + 1));
312
313 #ifdef LOW_MEM
314   /* decrement number of readers, when we reach 0 readers we can be sure that
315    * none is accessing the memory in the free list and we can try to clean up */
316   if (g_atomic_int_dec_and_test (&queue->num_readers))
317     clear_free_list (queue);
318 #endif
319
320   return ret;
321 }
322
323 /**
324  * gst_atomic_queue_push:
325  * @queue: a #GstAtomicQueue
326  * @data: the data
327  *
328  * Append @data to the tail of the queue.
329  */
330 void
331 gst_atomic_queue_push (GstAtomicQueue * queue, gpointer data)
332 {
333   GstAQueueMem *tail_mem;
334   gint head, tail, size;
335
336   g_return_if_fail (queue != NULL);
337
338   do {
339     while (TRUE) {
340       GstAQueueMem *mem;
341
342       tail_mem = g_atomic_pointer_get (&queue->tail_mem);
343       head = g_atomic_int_get (&tail_mem->head);
344       tail = g_atomic_int_get (&tail_mem->tail);
345       size = tail_mem->size;
346
347       /* we're not full, continue */
348       if (tail - head <= size)
349         break;
350
351       /* else we need to grow the array, we store a mask so we have to add 1 */
352       mem = new_queue_mem ((size << 1) + 1, tail);
353
354       /* try to make our new array visible to other writers */
355       if (!g_atomic_pointer_compare_and_exchange ((gpointer *) &
356               queue->tail_mem, tail_mem, mem)) {
357         /* we tried to swap the new writer array but something changed. This is
358          * because some other writer beat us to it, we free our memory and try
359          * again */
360         free_queue_mem (mem);
361         continue;
362       }
363       /* make sure that readers can find our new array as well. The one who
364        * manages to swap the pointer is the only one who can set the next
365        * pointer to the new array */
366       g_atomic_pointer_set (&tail_mem->next, mem);
367     }
368   } while (!g_atomic_int_compare_and_exchange (&tail_mem->tail, tail,
369           tail + 1));
370
371   tail_mem->array[tail & size] = data;
372 }
373
374 /**
375  * gst_atomic_queue_length:
376  * @queue: a #GstAtomicQueue
377  *
378  * Get the amount of items in the queue.
379  *
380  * Returns: the number of elements in the queue.
381  */
382 guint
383 gst_atomic_queue_length (GstAtomicQueue * queue)
384 {
385   GstAQueueMem *head_mem, *tail_mem;
386   gint head, tail;
387
388   g_return_val_if_fail (queue != NULL, 0);
389
390 #ifdef LOW_MEM
391   g_atomic_int_inc (&queue->num_readers);
392 #endif
393
394   head_mem = g_atomic_pointer_get (&queue->head_mem);
395   head = g_atomic_int_get (&head_mem->head);
396
397   tail_mem = g_atomic_pointer_get (&queue->tail_mem);
398   tail = g_atomic_int_get (&tail_mem->tail);
399
400 #ifdef LOW_MEM
401   if (g_atomic_int_dec_and_test (&queue->num_readers))
402     clear_free_list (queue);
403 #endif
404
405   return tail - head;
406 }