2 This file is part of PulseAudio.
4 Copyright 2006-2008 Lennart Poettering
5 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
6 Copyright (C) 2012 Canonical Ltd.
8 Contact: Jyri Sarha <Jyri.Sarha@nokia.com>
10 PulseAudio is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as
12 published by the Free Software Foundation; either version 2.1 of the
13 License, or (at your option) any later version.
15 PulseAudio is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public
21 License along with PulseAudio; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <pulse/xmalloc.h>
32 #include <pulsecore/atomic.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/macro.h>
40 #define FLIST_SIZE 128
42 /* Atomic table indices contain
43 sign bit = if set, indicates empty/NULL value
44 tag bits (to avoid the ABA problem)
48 /* Lock free single linked list element. */
49 struct pa_flist_elem {
54 typedef struct pa_flist_elem pa_flist_elem;
60 pa_atomic_t current_tag;
65 /* Stack that contains pointers stored into free list */
67 /* Stack that contains empty list elements */
69 pa_flist_elem table[];
72 /* Lock free pop from linked list stack */
73 static pa_flist_elem *stack_pop(pa_flist *flist, pa_atomic_t *list) {
74 pa_flist_elem *popped;
79 idx = pa_atomic_load(list);
82 popped = &flist->table[idx & flist->index_mask];
83 } while (!pa_atomic_cmpxchg(list, idx, pa_atomic_load(&popped->next)));
88 /* Lock free push to linked list stack */
89 static void stack_push(pa_flist *flist, pa_atomic_t *list, pa_flist_elem *new_elem) {
90 int tag, newindex, next;
93 tag = pa_atomic_inc(&flist->current_tag);
94 newindex = new_elem - flist->table;
95 pa_assert(newindex >= 0 && newindex < (int) flist->size);
96 newindex |= (tag << flist->tag_shift) & flist->tag_mask;
99 next = pa_atomic_load(list);
100 pa_atomic_store(&new_elem->next, next);
101 } while (!pa_atomic_cmpxchg(list, next, newindex));
104 pa_flist *pa_flist_new_with_name(unsigned size, const char *name) {
112 l = pa_xmalloc0(sizeof(pa_flist) + sizeof(pa_flist_elem) * size);
114 l->name = pa_xstrdup(name);
117 while (1 << l->tag_shift < (int) size)
119 l->index_mask = (1 << l->tag_shift) - 1;
120 l->tag_mask = INT_MAX - l->index_mask;
122 pa_atomic_store(&l->stored, -1);
123 pa_atomic_store(&l->empty, -1);
124 for (i=0; i < size; i++) {
125 stack_push(l, &l->empty, &l->table[i]);
130 pa_flist *pa_flist_new(unsigned size) {
131 return pa_flist_new_with_name(size, "unknown");
134 void pa_flist_free(pa_flist *l, pa_free_cb_t free_cb) {
140 while((elem = stack_pop(l, &l->stored)))
141 free_cb(pa_atomic_ptr_load(&elem->ptr));
148 int pa_flist_push(pa_flist *l, void *p) {
153 elem = stack_pop(l, &l->empty);
155 if (pa_log_ratelimit(PA_LOG_DEBUG))
156 pa_log_debug("%s flist is full (don't worry)", l->name);
159 pa_atomic_ptr_store(&elem->ptr, p);
160 stack_push(l, &l->stored, elem);
165 void* pa_flist_pop(pa_flist *l) {
170 elem = stack_pop(l, &l->stored);
174 ptr = pa_atomic_ptr_load(&elem->ptr);
176 stack_push(l, &l->empty, elem);