After apply this patch, pulseaudio starts with segmentation fault..<div>my pulseadudio version is 0.9.21</div><div><br></div><div>thanks<br><br><div class="gmail_quote">2010/8/24 <span dir="ltr"><<a href="mailto:oku@iki.fi" target="_blank">oku@iki.fi</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>From: Jyri Sarha <<a href="mailto:jyri.sarha@nokia.com" target="_blank">jyri.sarha@nokia.com</a>><br>
<br>
</div>The old free list implementation used objects in FIFO style. This is<br>
bad because it tries keep all the objects ever used alive and in<br>
memory. This minimizes the changes that an allocated object is already<br>
in cache. When there is shortage of physical memory this may also<br>
increase change that newly allocated object is swapped out. LIFO<br>
(e.g. stack) style free list should help these issues. Like the old<br>
one the new implementation is also lock free. This version (v2.0) of<br>
the patch has a potential weakness fixed.<br>
---<br>
<div> src/pulsecore/flist.c | 215 ++++++++++++++-----------------------------------<br>
1 files changed, 61 insertions(+), 154 deletions(-)<br>
<br>
</div>diff --git a/src/pulsecore/flist.c b/src/pulsecore/flist.c<br>
index 7e5ee24..4503e0b 100644<br>
--- a/src/pulsecore/flist.c<br>
+++ b/src/pulsecore/flist.c<br>
@@ -1,7 +1,9 @@<br>
/***<br>
This file is part of PulseAudio.<br>
<br>
- Copyright 2006-2008 Lennart Poettering<br>
+ Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).<br>
+<br>
+ Contact: Jyri Sarha <<a href="mailto:Jyri.Sarha@nokia.com" target="_blank">Jyri.Sarha@nokia.com</a>><br>
<br>
PulseAudio is free software; you can redistribute it and/or modify<br>
it under the terms of the GNU Lesser General Public License as<br>
@@ -34,198 +36,103 @@<br>
<br>
#include "flist.h"<br>
<br>
-/* Algorithm is not perfect, in a few corner cases it will fail to pop<br>
- * from the flist although it isn't empty, and fail to push into the<br>
- * flist, although it isn't full.<br>
- *<br>
- * We keep a fixed size array of entries, each item is an atomic<br>
- * pointer.<br>
- *<br>
- * To accelerate finding of used/unused cells we maintain a read and a<br>
- * write index which is used like a ring buffer. After each push we<br>
- * increase the write index and after each pop we increase the read<br>
- * index.<br>
- *<br>
- * The indexes are incremented atomically and are never truncated to<br>
- * the buffer size. Instead we assume that the buffer size is a power<br>
- * of two and that the truncation can thus be done by applying a<br>
- * simple AND on read.<br>
- *<br>
- * To make sure that we do not look for empty cells indefinitely we<br>
- * maintain a length value which stores the number of used cells. From<br>
- * this value the number of unused cells is easily calculated. Please<br>
- * note that the length value is not updated atomically with the read<br>
- * and write index and might thus be a few cells off the real<br>
- * value. To deal with this we always look for N_EXTRA_SCAN extra<br>
- * cells when pushing/popping entries.<br>
- *<br>
- * It might make sense to replace this implementation with a link list<br>
- * stack or queue, which however requires DCAS to be simple. Patches<br>
- * welcome.<br>
- *<br>
- * Please note that this algorithm is home grown.*/<br>
-<br>
#define FLIST_SIZE 128<br>
-#define N_EXTRA_SCAN 3<br>
<br>
-/* For debugging purposes we can define _Y to put and extra thread<br>
- * yield between each operation. */<br>
+struct pa_flist_elem {<br>
+ pa_atomic_ptr_t next;<br>
+ pa_atomic_ptr_t ptr;<br>
+};<br>
<br>
-#ifdef PROFILE<br>
-#define _Y pa_thread_yield()<br>
-#else<br>
-#define _Y do { } while(0)<br>
-#endif<br>
+typedef struct pa_flist_elem pa_flist_elem;<br>
<br>
struct pa_flist {<br>
unsigned size;<br>
- pa_atomic_t length;<br>
- pa_atomic_t read_idx;<br>
- pa_atomic_t write_idx;<br>
+ pa_atomic_ptr_t stored;<br>
+ pa_atomic_ptr_t empty;<br>
+ pa_flist_elem table[0];<br>
};<br>
<br>
-#define PA_FLIST_CELLS(x) ((pa_atomic_ptr_t*) ((uint8_t*) (x) + PA_ALIGN(sizeof(struct pa_flist))))<br>
+static pa_flist_elem *stack_pop(pa_atomic_ptr_t *list) {<br>
+ pa_flist_elem *poped;<br>
+ pa_flist_elem *next;<br>
+ pa_assert(list);<br>
+<br>
+ do {<br>
+ poped = (pa_flist_elem *) pa_atomic_ptr_load(list);<br>
+ next = pa_atomic_ptr_load(&poped->next);<br>
+ } while (poped != NULL && !pa_atomic_ptr_cmpxchg(list, poped, next));<br>
+<br>
+ return poped;<br>
+}<br>
+<br>
+static void stack_push(pa_atomic_ptr_t *list, pa_flist_elem *new_elem) {<br>
+ pa_flist_elem *next;<br>
+ pa_assert(list);<br>
+<br>
+ do {<br>
+ next = pa_atomic_ptr_load(list);<br>
+ pa_atomic_ptr_store(&new_elem->next, next);<br>
+ } while (!pa_atomic_ptr_cmpxchg(list, next, new_elem));<br>
+}<br>
<br>
pa_flist *pa_flist_new(unsigned size) {<br>
pa_flist *l;<br>
+ unsigned i;<br>
<br>
if (!size)<br>
size = FLIST_SIZE;<br>
<br>
- pa_assert(pa_is_power_of_two(size));<br>
-<br>
- l = pa_xmalloc0(PA_ALIGN(sizeof(pa_flist)) + (sizeof(pa_atomic_ptr_t) * size));<br>
+ l = pa_xmalloc0(sizeof(pa_flist) + sizeof(pa_flist_elem) * size);<br>
<br>
l->size = size;<br>
-<br>
- pa_atomic_store(&l->read_idx, 0);<br>
- pa_atomic_store(&l->write_idx, 0);<br>
- pa_atomic_store(&l->length, 0);<br>
-<br>
+ pa_atomic_ptr_store(&l->stored, NULL);<br>
+ pa_atomic_ptr_store(&l->empty, NULL);<br>
+ for (i=0; i < size; i++) {<br>
+ stack_push(&l->empty, &l->table[i]);<br>
+ }<br>
return l;<br>
}<br>
<br>
-static unsigned reduce(pa_flist *l, unsigned value) {<br>
- return value & (l->size - 1);<br>
-}<br>
-<br>
void pa_flist_free(pa_flist *l, pa_free_cb_t free_cb) {<br>
pa_assert(l);<br>
<br>
if (free_cb) {<br>
- pa_atomic_ptr_t*cells;<br>
- unsigned idx;<br>
-<br>
- cells = PA_FLIST_CELLS(l);<br>
-<br>
- for (idx = 0; idx < l->size; idx ++) {<br>
- void *p;<br>
-<br>
- if ((p = pa_atomic_ptr_load(&cells[idx])))<br>
- free_cb(p);<br>
- }<br>
+ pa_flist_elem *elem;<br>
+ while((elem = stack_pop(&l->stored)))<br>
+ free_cb(pa_atomic_ptr_load(&elem->ptr));<br>
}<br>
<br>
pa_xfree(l);<br>
}<br>
<br>
-int pa_flist_push(pa_flist*l, void *p) {<br>
- unsigned idx, n;<br>
- pa_atomic_ptr_t*cells;<br>
-#ifdef PROFILE<br>
- unsigned len;<br>
-#endif<br>
-<br>
+int pa_flist_push(pa_flist *l, void *p) {<br>
+ pa_flist_elem *elem;<br>
pa_assert(l);<br>
pa_assert(p);<br>
<br>
- cells = PA_FLIST_CELLS(l);<br>
-<br>
- n = l->size + N_EXTRA_SCAN - (unsigned) pa_atomic_load(&l->length);<br>
-<br>
-#ifdef PROFILE<br>
- len = n;<br>
-#endif<br>
-<br>
- _Y;<br>
- idx = reduce(l, (unsigned) pa_atomic_load(&l->write_idx));<br>
-<br>
- for (; n > 0 ; n--) {<br>
-<br>
- _Y;<br>
-<br>
- if (pa_atomic_ptr_cmpxchg(&cells[idx], NULL, p)) {<br>
-<br>
- _Y;<br>
- pa_atomic_inc(&l->write_idx);<br>
-<br>
- _Y;<br>
- pa_atomic_inc(&l->length);<br>
-<br>
- return 0;<br>
- }<br>
-<br>
- _Y;<br>
- idx = reduce(l, idx + 1);<br>
+ elem = stack_pop(&l->empty);<br>
+ if (elem == NULL) {<br>
+ pa_log_warn("flist is full");<br>
+ return -1;<br>
}<br>
+ pa_atomic_ptr_store(&elem->ptr, p);<br>
+ stack_push(&l->stored, elem);<br>
<br>
-#ifdef PROFILE<br>
- if (len > N_EXTRA_SCAN)<br>
- pa_log_warn("Didn't find free cell after %u iterations.", len);<br>
-#endif<br>
-<br>
- return -1;<br>
+ return 0;<br>
}<br>
<br>
-void* pa_flist_pop(pa_flist*l) {<br>
- unsigned idx, n;<br>
- pa_atomic_ptr_t *cells;<br>
-#ifdef PROFILE<br>
- unsigned len;<br>
-#endif<br>
-<br>
+void* pa_flist_pop(pa_flist *l) {<br>
+ pa_flist_elem *elem;<br>
+ void *ptr;<br>
pa_assert(l);<br>
<br>
- cells = PA_FLIST_CELLS(l);<br>
-<br>
- n = (unsigned) pa_atomic_load(&l->length) + N_EXTRA_SCAN;<br>
+ elem = stack_pop(&l->stored);<br>
+ if (elem == NULL)<br>
+ return NULL;<br>
<br>
-#ifdef PROFILE<br>
- len = n;<br>
-#endif<br>
-<br>
- _Y;<br>
- idx = reduce(l, (unsigned) pa_atomic_load(&l->read_idx));<br>
-<br>
- for (; n > 0 ; n--) {<br>
- void *p;<br>
-<br>
- _Y;<br>
- p = pa_atomic_ptr_load(&cells[idx]);<br>
-<br>
- if (p) {<br>
-<br>
- _Y;<br>
- if (!pa_atomic_ptr_cmpxchg(&cells[idx], p, NULL))<br>
- continue;<br>
+ ptr = pa_atomic_ptr_load(&elem->ptr);<br>
<br>
- _Y;<br>
- pa_atomic_inc(&l->read_idx);<br>
-<br>
- _Y;<br>
- pa_atomic_dec(&l->length);<br>
-<br>
- return p;<br>
- }<br>
-<br>
- _Y;<br>
- idx = reduce(l, idx+1);<br>
- }<br>
-<br>
-#ifdef PROFILE<br>
- if (len > N_EXTRA_SCAN)<br>
- pa_log_warn("Didn't find used cell after %u iterations.", len);<br>
-#endif<br>
+ stack_push(&l->empty, elem);<br>
<br>
- return NULL;<br>
+ return ptr;<br>
}<br>
<font color="#888888">--<br>
1.6.3.3<br>
</font><div><div></div><div><br>
_______________________________________________<br>
pulseaudio-discuss mailing list<br>
<a href="mailto:pulseaudio-discuss@mail.0pointer.de" target="_blank">pulseaudio-discuss@mail.0pointer.de</a><br>
<a href="https://tango.0pointer.de/mailman/listinfo/pulseaudio-discuss" target="_blank">https://tango.0pointer.de/mailman/listinfo/pulseaudio-discuss</a><br>
</div></div></blockquote></div><br>
</div>