summaryrefslogtreecommitdiff
path: root/gst/gstatomicqueue.c
blob: a80089c649a53507bf2a6e04f84e9b265556cfdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* GStreamer
 * Copyright (C) 2009 Edward Hervey <bilboed@bilboed.com>
 *               2011 Wim Taymans <wim.taymans@gmail.com>
 *
 * gstatomicqueue.c:
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "gst_private.h"

#include <string.h>

#include <gst/gst.h>
#include "gstatomicqueue.h"
#include "glib-compat-private.h"

/**
 * SECTION:gstatomicqueue
 * @title: GstAtomicQueue
 * @short_description: An atomic queue implementation
 *
 * The #GstAtomicQueue object implements a queue that can be used from multiple
 * threads without performing any blocking operations.
 */

G_DEFINE_BOXED_TYPE (GstAtomicQueue, gst_atomic_queue,
    (GBoxedCopyFunc) gst_atomic_queue_ref,
    (GBoxedFreeFunc) gst_atomic_queue_unref);

/* By default the queue uses 2 * sizeof(gpointer) * clp2 (max_items) of
 * memory. clp2(x) is the next power of two >= than x.
 *
 * The queue can operate in low memory mode, in which it consumes almost
 * half the memory at the expense of extra overhead in the readers. This
 * is disabled by default because even without LOW_MEM mode, the memory
 * consumption is still lower than a plain GList.
 */
#undef LOW_MEM

typedef struct _GstAQueueMem GstAQueueMem;

struct _GstAQueueMem
{
  gint size;
  gpointer *array;
  volatile gint head;
  volatile gint tail_write;
  volatile gint tail_read;
  GstAQueueMem *next;
  GstAQueueMem *free;
};

static guint
clp2 (guint n)
{
  guint res = 1;

  while (res < n)
    res <<= 1;

  return res;
}

static GstAQueueMem *
new_queue_mem (guint size, gint pos)
{
  GstAQueueMem *mem;

  mem = g_new (GstAQueueMem, 1);

  /* we keep the size as a mask for performance */
  mem->size = clp2 (MAX (size, 16)) - 1;
  mem->array = g_new0 (gpointer, mem->size + 1);
  mem->head = pos;
  mem->tail_write = pos;
  mem->tail_read = pos;
  mem->next = NULL;
  mem->free = NULL;

  return mem;
}

static void
free_queue_mem (GstAQueueMem * mem)
{
  g_free (mem->array);
  g_free (mem);
}

struct _GstAtomicQueue
{
  volatile gint refcount;
#ifdef LOW_MEM
  gint num_readers;
#endif
  GstAQueueMem *head_mem;
  GstAQueueMem *tail_mem;
  GstAQueueMem *free_list;
};

static void
add_to_free_list (GstAtomicQueue * queue, GstAQueueMem * mem)
{
  do {
    mem->free = g_atomic_pointer_get (&queue->free_list);
  } while (!g_atomic_pointer_compare_and_exchange (&queue->free_list,
          mem->free, mem));
}

static void
clear_free_list (GstAtomicQueue * queue)
{
  GstAQueueMem *free_list;

  /* take the free list and replace with NULL */
  do {
    free_list = g_atomic_pointer_get (&queue->free_list);
    if (free_list == NULL)
      return;
  } while (!g_atomic_pointer_compare_and_exchange (&queue->free_list, free_list,
          NULL));

  while (free_list) {
    GstAQueueMem *next = free_list->free;

    free_queue_mem (free_list);

    free_list = next;
  }
}

/**
 * gst_atomic_queue_new:
 * @initial_size: initial queue size
 *
 * Create a new atomic queue instance. @initial_size will be rounded up to the
 * nearest power of 2 and used as the initial size of the queue.
 *
 * Returns: a new #GstAtomicQueue
 */
GstAtomicQueue *
gst_atomic_queue_new (guint initial_size)
{
  GstAtomicQueue *queue;

  queue = g_new (GstAtomicQueue, 1);

  queue->refcount = 1;
#ifdef LOW_MEM
  queue->num_readers = 0;
#endif
  queue->head_mem = queue->tail_mem = new_queue_mem (initial_size, 0);
  queue->free_list = NULL;

  return queue;
}

/**
 * gst_atomic_queue_ref:
 * @queue: a #GstAtomicQueue
 *
 * Increase the refcount of @queue.
 */
void
gst_atomic_queue_ref (GstAtomicQueue * queue)
{
  g_return_if_fail (queue != NULL);

  g_atomic_int_inc (&queue->refcount);
}

static void
gst_atomic_queue_free (GstAtomicQueue * queue)
{
  free_queue_mem (queue->head_mem);
  if (queue->head_mem != queue->tail_mem)
    free_queue_mem (queue->tail_mem);
  clear_free_list (queue);
  g_free (queue);
}

/**
 * gst_atomic_queue_unref:
 * @queue: a #GstAtomicQueue
 *
 * Unref @queue and free the memory when the refcount reaches 0.
 */
void
gst_atomic_queue_unref (GstAtomicQueue * queue)
{
  g_return_if_fail (queue != NULL);

  if (g_atomic_int_dec_and_test (&queue->refcount))
    gst_atomic_queue_free (queue);
}

/**
 * gst_atomic_queue_peek:
 * @queue: a #GstAtomicQueue
 *
 * Peek the head element of the queue without removing it from the queue.
 *
 * Returns: (transfer none) (nullable): the head element of @queue or
 * %NULL when the queue is empty.
 */
gpointer
gst_atomic_queue_peek (GstAtomicQueue * queue)
{
  GstAQueueMem *head_mem;
  gint head, tail, size;

  g_return_val_if_fail (queue != NULL, NULL);

  while (TRUE) {
    GstAQueueMem *next;

    head_mem = g_atomic_pointer_get (&queue->head_mem);

    head = g_atomic_int_get (&head_mem->head);
    tail = g_atomic_int_get (&head_mem->tail_read);
    size = head_mem->size;

    /* when we are not empty, we can continue */
    if (G_LIKELY (head != tail))
      break;

    /* else array empty, try to take next */
    next = g_atomic_pointer_get (&head_mem->next);
    if (next == NULL)
      return NULL;

    /* now we try to move the next array as the head memory. If we fail to do that,
     * some other reader managed to do it first and we retry */
    if (!g_atomic_pointer_compare_and_exchange (&queue->head_mem, head_mem,
            next))
      continue;

    /* when we managed to swing the head pointer the old head is now
     * useless and we add it to the freelist. We can't free the memory yet
     * because we first need to make sure no reader is accessing it anymore. */
    add_to_free_list (queue, head_mem);
  }

  return head_mem->array[head & size];
}

/**
 * gst_atomic_queue_pop:
 * @queue: a #GstAtomicQueue
 *
 * Get the head element of the queue.
 *
 * Returns: (transfer full): the head element of @queue or %NULL when
 * the queue is empty.
 */
gpointer
gst_atomic_queue_pop (GstAtomicQueue * queue)
{
  gpointer ret;
  GstAQueueMem *head_mem;
  gint head, tail, size;

  g_return_val_if_fail (queue != NULL, NULL);

#ifdef LOW_MEM
  g_atomic_int_inc (&queue->num_readers);
#endif

  do {
    while (TRUE) {
      GstAQueueMem *next;

      head_mem = g_atomic_pointer_get (&queue->head_mem);

      head = g_atomic_int_get (&head_mem->head);
      tail = g_atomic_int_get (&head_mem->tail_read);
      size = head_mem->size;

      /* when we are not empty, we can continue */
      if G_LIKELY
        (head != tail)
            break;

      /* else array empty, try to take next */
      next = g_atomic_pointer_get (&head_mem->next);
      if (next == NULL)
        return NULL;

      /* now we try to move the next array as the head memory. If we fail to do that,
       * some other reader managed to do it first and we retry */
      if G_UNLIKELY
        (!g_atomic_pointer_compare_and_exchange (&queue->head_mem, head_mem,
                next))
            continue;

      /* when we managed to swing the head pointer the old head is now
       * useless and we add it to the freelist. We can't free the memory yet
       * because we first need to make sure no reader is accessing it anymore. */
      add_to_free_list (queue, head_mem);
    }

    ret = head_mem->array[head & size];
  } while G_UNLIKELY
  (!g_atomic_int_compare_and_exchange (&head_mem->head, head, head + 1));

#ifdef LOW_MEM
  /* decrement number of readers, when we reach 0 readers we can be sure that
   * none is accessing the memory in the free list and we can try to clean up */
  if (g_atomic_int_dec_and_test (&queue->num_readers))
    clear_free_list (queue);
#endif

  return ret;
}

/**
 * gst_atomic_queue_push:
 * @queue: a #GstAtomicQueue
 * @data: the data
 *
 * Append @data to the tail of the queue.
 */
void
gst_atomic_queue_push (GstAtomicQueue * queue, gpointer data)
{
  GstAQueueMem *tail_mem;
  gint head, tail, size;

  g_return_if_fail (queue != NULL);

  do {
    while (TRUE) {
      GstAQueueMem *mem;

      tail_mem = g_atomic_pointer_get (&queue->tail_mem);
      head = g_atomic_int_get (&tail_mem->head);
      tail = g_atomic_int_get (&tail_mem->tail_write);
      size = tail_mem->size;

      /* we're not full, continue */
      if G_LIKELY
        (tail - head <= size)
            break;

      /* else we need to grow the array, we store a mask so we have to add 1 */
      mem = new_queue_mem ((size << 1) + 1, tail);

      /* try to make our new array visible to other writers */
      if G_UNLIKELY
        (!g_atomic_pointer_compare_and_exchange (&queue->tail_mem, tail_mem,
                mem)) {
        /* we tried to swap the new writer array but something changed. This is
         * because some other writer beat us to it, we free our memory and try
         * again */
        free_queue_mem (mem);
        continue;
        }
      /* make sure that readers can find our new array as well. The one who
       * manages to swap the pointer is the only one who can set the next
       * pointer to the new array */
      g_atomic_pointer_set (&tail_mem->next, mem);
    }
  } while G_UNLIKELY
  (!g_atomic_int_compare_and_exchange (&tail_mem->tail_write, tail, tail + 1));

  tail_mem->array[tail & size] = data;

  /* now wait until all writers have completed their write before we move the
   * tail_read to this new item. It is possible that other writers are still
   * updating the previous array slots and we don't want to reveal their changes
   * before they are done. FIXME, it would be nice if we didn't have to busy
   * wait here. */
  while G_UNLIKELY
    (!g_atomic_int_compare_and_exchange (&tail_mem->tail_read, tail, tail + 1));
}

/**
 * gst_atomic_queue_length:
 * @queue: a #GstAtomicQueue
 *
 * Get the amount of items in the queue.
 *
 * Returns: the number of elements in the queue.
 */
guint
gst_atomic_queue_length (GstAtomicQueue * queue)
{
  GstAQueueMem *head_mem, *tail_mem;
  gint head, tail;

  g_return_val_if_fail (queue != NULL, 0);

#ifdef LOW_MEM
  g_atomic_int_inc (&queue->num_readers);
#endif

  head_mem = g_atomic_pointer_get (&queue->head_mem);
  head = g_atomic_int_get (&head_mem->head);

  tail_mem = g_atomic_pointer_get (&queue->tail_mem);
  tail = g_atomic_int_get (&tail_mem->tail_read);

#ifdef LOW_MEM
  if (g_atomic_int_dec_and_test (&queue->num_readers))
    clear_free_list (queue);
#endif

  return tail - head;
}