]> code.delx.au - pulseaudio/blob - src/pulsecore/flist.c
e342a5795b21a4cf9bf61589c51befb0c2b06ecb
[pulseaudio] / src / pulsecore / flist.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006-2008 Lennart Poettering
5 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
6
7 Contact: Jyri Sarha <Jyri.Sarha@nokia.com>
8
9 PulseAudio is free software; you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License as
11 published by the Free Software Foundation; either version 2.1 of the
12 License, or (at your option) any later version.
13
14 PulseAudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include <pulse/xmalloc.h>
30
31 #include <pulsecore/atomic.h>
32 #include <pulsecore/log.h>
33 #include <pulsecore/thread.h>
34 #include <pulsecore/macro.h>
35 #include <pulsecore/core-util.h>
36 #include <pulsecore/macro.h>
37
38 #include "flist.h"
39
40 #define FLIST_SIZE 128
41
42 /* Lock free single linked list element. */
43 struct pa_flist_elem {
44 pa_atomic_ptr_t next;
45 pa_atomic_ptr_t ptr;
46 };
47
48 typedef struct pa_flist_elem pa_flist_elem;
49
50 struct pa_flist {
51 const char *name;
52 unsigned size;
53 /* Stack that contains pointers stored into free list */
54 pa_atomic_ptr_t stored;
55 /* Stack that contains empty list elements */
56 pa_atomic_ptr_t empty;
57 pa_flist_elem table[0];
58 };
59
60 /* Lock free pop from linked list stack */
61 static pa_flist_elem *stack_pop(pa_atomic_ptr_t *list) {
62 pa_flist_elem *poped;
63 pa_assert(list);
64
65 do {
66 poped = (pa_flist_elem *) pa_atomic_ptr_load(list);
67 } while (poped != NULL && !pa_atomic_ptr_cmpxchg(list, poped, pa_atomic_ptr_load(&poped->next)));
68
69 return poped;
70 }
71
72 /* Lock free push to linked list stack */
73 static void stack_push(pa_atomic_ptr_t *list, pa_flist_elem *new_elem) {
74 pa_flist_elem *next;
75 pa_assert(list);
76
77 do {
78 next = pa_atomic_ptr_load(list);
79 pa_atomic_ptr_store(&new_elem->next, next);
80 } while (!pa_atomic_ptr_cmpxchg(list, next, new_elem));
81 }
82
83 pa_flist *pa_flist_new_with_name(unsigned size, const char *name) {
84 pa_flist *l;
85 unsigned i;
86 pa_assert(name);
87
88 if (!size)
89 size = FLIST_SIZE;
90
91 l = pa_xmalloc0(sizeof(pa_flist) + sizeof(pa_flist_elem) * size);
92
93 l->name = pa_xstrdup(name);
94 l->size = size;
95 pa_atomic_ptr_store(&l->stored, NULL);
96 pa_atomic_ptr_store(&l->empty, NULL);
97 for (i=0; i < size; i++) {
98 stack_push(&l->empty, &l->table[i]);
99 }
100 return l;
101 }
102
103 pa_flist *pa_flist_new(unsigned size) {
104 return pa_flist_new_with_name(size, "unknown");
105 }
106
107 void pa_flist_free(pa_flist *l, pa_free_cb_t free_cb) {
108 pa_assert(l);
109 pa_assert(l->name);
110
111 if (free_cb) {
112 pa_flist_elem *elem;
113 while((elem = stack_pop(&l->stored)))
114 free_cb(pa_atomic_ptr_load(&elem->ptr));
115 }
116
117 pa_xfree(l);
118 }
119
120 int pa_flist_push(pa_flist *l, void *p) {
121 pa_flist_elem *elem;
122 pa_assert(l);
123 pa_assert(p);
124
125 elem = stack_pop(&l->empty);
126 if (elem == NULL) {
127 if (pa_log_ratelimit(PA_LOG_DEBUG))
128 pa_log_debug("%s flist is full (don't worry)", l->name);
129 return -1;
130 }
131 pa_atomic_ptr_store(&elem->ptr, p);
132 stack_push(&l->stored, elem);
133
134 return 0;
135 }
136
137 void* pa_flist_pop(pa_flist *l) {
138 pa_flist_elem *elem;
139 void *ptr;
140 pa_assert(l);
141
142 elem = stack_pop(&l->stored);
143 if (elem == NULL)
144 return NULL;
145
146 ptr = pa_atomic_ptr_load(&elem->ptr);
147
148 stack_push(&l->empty, elem);
149
150 return ptr;
151 }