2006-09-09 21:05:31 +00:00
|
|
|
/***
|
|
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-06-27 19:18:19 +02:00
|
|
|
Copyright 2006-2008 Lennart Poettering
|
2009-11-30 16:55:27 +02:00
|
|
|
Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
2012-03-04 06:07:37 +01:00
|
|
|
Copyright (C) 2012 Canonical Ltd.
|
2009-11-30 16:55:27 +02:00
|
|
|
|
|
|
|
|
Contact: Jyri Sarha <Jyri.Sarha@nokia.com>
|
2007-02-13 15:35:19 +00:00
|
|
|
|
2006-09-09 21:05:31 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU Lesser General Public License as
|
|
|
|
|
published by the Free Software Foundation; either version 2.1 of the
|
|
|
|
|
License, or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-09-09 21:05:31 +00:00
|
|
|
PulseAudio 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
|
|
|
|
|
Lesser General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-09-09 21:05:31 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
|
License along with PulseAudio; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
2006-09-09 21:05:31 +00:00
|
|
|
|
|
|
|
|
#include <pulsecore/atomic.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
|
|
|
|
#include <pulsecore/core-util.h>
|
|
|
|
|
#include <pulsecore/macro.h>
|
2006-09-09 21:05:31 +00:00
|
|
|
|
|
|
|
|
#include "flist.h"
|
|
|
|
|
|
|
|
|
|
#define FLIST_SIZE 128
|
|
|
|
|
|
2012-03-04 06:07:37 +01:00
|
|
|
/* Atomic table indices contain
|
|
|
|
|
sign bit = if set, indicates empty/NULL value
|
|
|
|
|
tag bits (to avoid the ABA problem)
|
|
|
|
|
actual index bits
|
|
|
|
|
*/
|
|
|
|
|
|
2009-11-30 16:55:27 +02:00
|
|
|
/* Lock free single linked list element. */
|
|
|
|
|
struct pa_flist_elem {
|
2012-03-04 06:07:37 +01:00
|
|
|
pa_atomic_t next;
|
2009-11-30 16:55:27 +02:00
|
|
|
pa_atomic_ptr_t ptr;
|
|
|
|
|
};
|
2006-09-09 21:05:31 +00:00
|
|
|
|
2009-11-30 16:55:27 +02:00
|
|
|
typedef struct pa_flist_elem pa_flist_elem;
|
2006-09-09 21:05:31 +00:00
|
|
|
|
|
|
|
|
struct pa_flist {
|
2010-11-26 18:38:24 +02:00
|
|
|
const char *name;
|
2006-09-09 21:05:31 +00:00
|
|
|
unsigned size;
|
2012-03-04 06:07:37 +01:00
|
|
|
|
|
|
|
|
pa_atomic_t current_tag;
|
|
|
|
|
int index_mask;
|
|
|
|
|
int tag_shift;
|
|
|
|
|
int tag_mask;
|
|
|
|
|
|
2009-11-30 16:55:27 +02:00
|
|
|
/* Stack that contains pointers stored into free list */
|
2012-03-04 06:07:37 +01:00
|
|
|
pa_atomic_t stored;
|
2009-11-30 16:55:27 +02:00
|
|
|
/* Stack that contains empty list elements */
|
2012-03-04 06:07:37 +01:00
|
|
|
pa_atomic_t empty;
|
2011-09-02 14:11:52 +02:00
|
|
|
pa_flist_elem table[];
|
2006-09-09 21:05:31 +00:00
|
|
|
};
|
|
|
|
|
|
2009-11-30 16:55:27 +02:00
|
|
|
/* Lock free pop from linked list stack */
|
2012-03-04 06:07:37 +01:00
|
|
|
static pa_flist_elem *stack_pop(pa_flist *flist, pa_atomic_t *list) {
|
|
|
|
|
pa_flist_elem *popped;
|
|
|
|
|
int idx;
|
2009-11-30 16:55:27 +02:00
|
|
|
pa_assert(list);
|
|
|
|
|
|
|
|
|
|
do {
|
2012-03-04 06:07:37 +01:00
|
|
|
idx = pa_atomic_load(list);
|
|
|
|
|
if (idx < 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
popped = &flist->table[idx & flist->index_mask];
|
|
|
|
|
} while (!pa_atomic_cmpxchg(list, idx, pa_atomic_load(&popped->next)));
|
2009-11-30 16:55:27 +02:00
|
|
|
|
2012-03-04 06:07:37 +01:00
|
|
|
return popped;
|
2009-11-30 16:55:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Lock free push to linked list stack */
|
2012-03-04 06:07:37 +01:00
|
|
|
static void stack_push(pa_flist *flist, pa_atomic_t *list, pa_flist_elem *new_elem) {
|
|
|
|
|
int tag, newindex, next;
|
2009-11-30 16:55:27 +02:00
|
|
|
pa_assert(list);
|
|
|
|
|
|
2012-03-04 06:07:37 +01:00
|
|
|
tag = pa_atomic_inc(&flist->current_tag);
|
|
|
|
|
newindex = new_elem - flist->table;
|
|
|
|
|
pa_assert(newindex >= 0 && newindex < (int) flist->size);
|
|
|
|
|
newindex |= (tag << flist->tag_shift) & flist->tag_mask;
|
|
|
|
|
|
2009-11-30 16:55:27 +02:00
|
|
|
do {
|
2012-03-04 06:07:37 +01:00
|
|
|
next = pa_atomic_load(list);
|
|
|
|
|
pa_atomic_store(&new_elem->next, next);
|
|
|
|
|
} while (!pa_atomic_cmpxchg(list, next, newindex));
|
2009-11-30 16:55:27 +02:00
|
|
|
}
|
2006-09-09 21:05:31 +00:00
|
|
|
|
2010-11-26 18:38:24 +02:00
|
|
|
pa_flist *pa_flist_new_with_name(unsigned size, const char *name) {
|
2006-09-09 21:05:31 +00:00
|
|
|
pa_flist *l;
|
2009-11-30 16:55:27 +02:00
|
|
|
unsigned i;
|
2011-01-13 16:44:41 +02:00
|
|
|
pa_assert(name);
|
2006-09-09 21:05:31 +00:00
|
|
|
|
|
|
|
|
if (!size)
|
|
|
|
|
size = FLIST_SIZE;
|
|
|
|
|
|
2009-11-30 16:55:27 +02:00
|
|
|
l = pa_xmalloc0(sizeof(pa_flist) + sizeof(pa_flist_elem) * size);
|
2006-09-09 21:05:31 +00:00
|
|
|
|
2011-01-13 16:44:41 +02:00
|
|
|
l->name = pa_xstrdup(name);
|
2006-09-09 21:05:31 +00:00
|
|
|
l->size = size;
|
2012-03-04 06:07:37 +01:00
|
|
|
|
|
|
|
|
while (1 << l->tag_shift < (int) size)
|
|
|
|
|
l->tag_shift++;
|
|
|
|
|
l->index_mask = (1 << l->tag_shift) - 1;
|
|
|
|
|
l->tag_mask = INT_MAX - l->index_mask;
|
|
|
|
|
|
|
|
|
|
pa_atomic_store(&l->stored, -1);
|
|
|
|
|
pa_atomic_store(&l->empty, -1);
|
2009-11-30 16:55:27 +02:00
|
|
|
for (i=0; i < size; i++) {
|
2012-03-04 06:07:37 +01:00
|
|
|
stack_push(l, &l->empty, &l->table[i]);
|
2009-11-30 16:55:27 +02:00
|
|
|
}
|
2006-09-09 21:05:31 +00:00
|
|
|
return l;
|
|
|
|
|
}
|
|
|
|
|
|
2010-11-26 18:38:24 +02:00
|
|
|
pa_flist *pa_flist_new(unsigned size) {
|
|
|
|
|
return pa_flist_new_with_name(size, "unknown");
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-09 21:05:31 +00:00
|
|
|
void pa_flist_free(pa_flist *l, pa_free_cb_t free_cb) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(l);
|
2011-01-13 16:44:41 +02:00
|
|
|
pa_assert(l->name);
|
2006-09-09 21:05:31 +00:00
|
|
|
|
|
|
|
|
if (free_cb) {
|
2009-11-30 16:55:27 +02:00
|
|
|
pa_flist_elem *elem;
|
2012-03-04 06:07:37 +01:00
|
|
|
while((elem = stack_pop(l, &l->stored)))
|
2009-11-30 16:55:27 +02:00
|
|
|
free_cb(pa_atomic_ptr_load(&elem->ptr));
|
2006-09-09 21:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
2011-08-12 00:17:39 +02:00
|
|
|
pa_xfree((char *) l->name);
|
2006-09-09 21:05:31 +00:00
|
|
|
pa_xfree(l);
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-30 16:55:27 +02:00
|
|
|
int pa_flist_push(pa_flist *l, void *p) {
|
|
|
|
|
pa_flist_elem *elem;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(l);
|
|
|
|
|
pa_assert(p);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-03-04 06:07:37 +01:00
|
|
|
elem = stack_pop(l, &l->empty);
|
2009-11-30 16:55:27 +02:00
|
|
|
if (elem == NULL) {
|
2011-01-22 01:08:36 +01:00
|
|
|
if (pa_log_ratelimit(PA_LOG_DEBUG))
|
2010-11-26 18:38:24 +02:00
|
|
|
pa_log_debug("%s flist is full (don't worry)", l->name);
|
2009-11-30 16:55:27 +02:00
|
|
|
return -1;
|
2006-09-09 21:05:31 +00:00
|
|
|
}
|
2009-11-30 16:55:27 +02:00
|
|
|
pa_atomic_ptr_store(&elem->ptr, p);
|
2012-03-04 06:07:37 +01:00
|
|
|
stack_push(l, &l->stored, elem);
|
2006-09-09 21:05:31 +00:00
|
|
|
|
2009-11-30 16:55:27 +02:00
|
|
|
return 0;
|
2006-09-09 21:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
2009-11-30 16:55:27 +02:00
|
|
|
void* pa_flist_pop(pa_flist *l) {
|
|
|
|
|
pa_flist_elem *elem;
|
|
|
|
|
void *ptr;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(l);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-03-04 06:07:37 +01:00
|
|
|
elem = stack_pop(l, &l->stored);
|
2009-11-30 16:55:27 +02:00
|
|
|
if (elem == NULL)
|
|
|
|
|
return NULL;
|
2009-09-08 23:46:23 +02:00
|
|
|
|
2009-11-30 16:55:27 +02:00
|
|
|
ptr = pa_atomic_ptr_load(&elem->ptr);
|
2008-06-27 19:18:19 +02:00
|
|
|
|
2012-03-04 06:07:37 +01:00
|
|
|
stack_push(l, &l->empty, elem);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2009-11-30 16:55:27 +02:00
|
|
|
return ptr;
|
2006-09-09 21:05:31 +00:00
|
|
|
}
|