2 This file is part of PulseAudio.
4 Copyright 2006-2008 Lennart Poettering
5 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
7 Contact: Jyri Sarha <Jyri.Sarha@nokia.com>
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.
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.
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
29 #include <pulse/xmalloc.h>
31 #include <pulsecore/atomic.h>
32 #include <pulsecore/log.h>
33 #include <pulsecore/macro.h>
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/macro.h>
39 #define FLIST_SIZE 128
41 /* Lock free single linked list element. */
42 struct pa_flist_elem
{
47 typedef struct pa_flist_elem pa_flist_elem
;
52 /* Stack that contains pointers stored into free list */
53 pa_atomic_ptr_t stored
;
54 /* Stack that contains empty list elements */
55 pa_atomic_ptr_t empty
;
56 pa_flist_elem table
[0];
59 /* Lock free pop from linked list stack */
60 static pa_flist_elem
*stack_pop(pa_atomic_ptr_t
*list
) {
65 poped
= (pa_flist_elem
*) pa_atomic_ptr_load(list
);
66 } while (poped
!= NULL
&& !pa_atomic_ptr_cmpxchg(list
, poped
, pa_atomic_ptr_load(&poped
->next
)));
71 /* Lock free push to linked list stack */
72 static void stack_push(pa_atomic_ptr_t
*list
, pa_flist_elem
*new_elem
) {
77 next
= pa_atomic_ptr_load(list
);
78 pa_atomic_ptr_store(&new_elem
->next
, next
);
79 } while (!pa_atomic_ptr_cmpxchg(list
, next
, new_elem
));
82 pa_flist
*pa_flist_new_with_name(unsigned size
, const char *name
) {
90 l
= pa_xmalloc0(sizeof(pa_flist
) + sizeof(pa_flist_elem
) * size
);
92 l
->name
= pa_xstrdup(name
);
94 pa_atomic_ptr_store(&l
->stored
, NULL
);
95 pa_atomic_ptr_store(&l
->empty
, NULL
);
96 for (i
=0; i
< size
; i
++) {
97 stack_push(&l
->empty
, &l
->table
[i
]);
102 pa_flist
*pa_flist_new(unsigned size
) {
103 return pa_flist_new_with_name(size
, "unknown");
106 void pa_flist_free(pa_flist
*l
, pa_free_cb_t free_cb
) {
112 while((elem
= stack_pop(&l
->stored
)))
113 free_cb(pa_atomic_ptr_load(&elem
->ptr
));
119 int pa_flist_push(pa_flist
*l
, void *p
) {
124 elem
= stack_pop(&l
->empty
);
126 if (pa_log_ratelimit(PA_LOG_DEBUG
))
127 pa_log_debug("%s flist is full (don't worry)", l
->name
);
130 pa_atomic_ptr_store(&elem
->ptr
, p
);
131 stack_push(&l
->stored
, elem
);
136 void* pa_flist_pop(pa_flist
*l
) {
141 elem
= stack_pop(&l
->stored
);
145 ptr
= pa_atomic_ptr_load(&elem
->ptr
);
147 stack_push(&l
->empty
, elem
);