pulseaudio/src/pulsecore/aupdate.c
Peter Meerwald 68cc36140b core: Annotate variables only used within assert()s to be PA_UNUSED
supresses a warning when compiling with NDEBUG:

pulsecore/aupdate.c: In function 'pa_aupdate_read_end':
pulsecore/aupdate.c:82:14: warning: variable 'n' set but not used [-Wunused-but-set-variable]
     unsigned n;

pulsecore/sink-input.c: In function 'pa_sink_input_unlink':
pulsecore/sink-input.c:648:27: warning: variable 'p' set but not used [-Wunused-but-set-variable]
     pa_source_output *o, *p = NULL;

pulsecore/sink-input.c: In function 'find_filter_sink_input':
pulsecore/sink-input.c:1523:14: warning: unused variable 'i' [-Wunused-variable]
     unsigned i = 0;

pulsecore/sink-input.c: In function 'pa_sink_input_start_move':
pulsecore/sink-input.c:1569:27: warning: variable 'p' set but not used [-Wunused-but-set-variable]
     pa_source_output *o, *p = NULL;

  CC       pulsecore/libpulsecore_5.0_la-sink.lo
pulsecore/sink.c: In function 'pa_sink_unlink':
pulsecore/sink.c:673:24: warning: variable 'j' set but not used [-Wunused-but-set-variable]
     pa_sink_input *i, *j = NULL;

   pulsecore/source-output.c: In function 'find_filter_source_output':
pulsecore/source-output.c:1179:9: warning: unused variable 'i' [-Wunused-variable]
     int i = 0;

  CC       pulsecore/libpulsecore_5.0_la-source.lo
pulsecore/source.c: In function 'pa_source_unlink':
pulsecore/source.c:616:27: warning: variable 'j' set but not used [-Wunused-but-set-variable]
     pa_source_output *o, *j = NULL;

Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net>
2014-11-17 13:24:51 +01:00

137 lines
3.1 KiB
C

/***
This file is part of PulseAudio.
Copyright 2009 Lennart Poettering
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.
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
General Public License for more details.
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
#include <pulse/xmalloc.h>
#include <pulsecore/semaphore.h>
#include <pulsecore/macro.h>
#include <pulsecore/mutex.h>
#include "aupdate.h"
#define MSB (1U << (sizeof(unsigned)*8U-1))
#define WHICH(n) (!!((n) & MSB))
#define COUNTER(n) ((n) & ~MSB)
struct pa_aupdate {
pa_atomic_t read_lock;
pa_mutex *write_lock;
pa_semaphore *semaphore;
bool swapped;
};
pa_aupdate *pa_aupdate_new(void) {
pa_aupdate *a;
a = pa_xnew(pa_aupdate, 1);
pa_atomic_store(&a->read_lock, 0);
a->write_lock = pa_mutex_new(false, false);
a->semaphore = pa_semaphore_new(0);
return a;
}
void pa_aupdate_free(pa_aupdate *a) {
pa_assert(a);
pa_mutex_free(a->write_lock);
pa_semaphore_free(a->semaphore);
pa_xfree(a);
}
unsigned pa_aupdate_read_begin(pa_aupdate *a) {
unsigned n;
pa_assert(a);
/* Increase the lock counter */
n = (unsigned) pa_atomic_inc(&a->read_lock);
/* When n is 0 we have about 2^31 threads running that all try to
* access the data at the same time, oh my! */
pa_assert(COUNTER(n)+1 > 0);
/* The uppermost bit tells us which data to look at */
return WHICH(n);
}
void pa_aupdate_read_end(pa_aupdate *a) {
unsigned PA_UNUSED n;
pa_assert(a);
/* Decrease the lock counter */
n = (unsigned) pa_atomic_dec(&a->read_lock);
/* Make sure the counter was valid */
pa_assert(COUNTER(n) > 0);
/* Post the semaphore */
pa_semaphore_post(a->semaphore);
}
unsigned pa_aupdate_write_begin(pa_aupdate *a) {
unsigned n;
pa_assert(a);
pa_mutex_lock(a->write_lock);
n = (unsigned) pa_atomic_load(&a->read_lock);
a->swapped = false;
return !WHICH(n);
}
unsigned pa_aupdate_write_swap(pa_aupdate *a) {
unsigned n;
pa_assert(a);
for (;;) {
n = (unsigned) pa_atomic_load(&a->read_lock);
/* If the read counter is > 0 wait; if it is 0 try to swap the lists */
if (COUNTER(n) > 0)
pa_semaphore_wait(a->semaphore);
else if (pa_atomic_cmpxchg(&a->read_lock, (int) n, (int) (n ^ MSB)))
break;
}
a->swapped = true;
return WHICH(n);
}
void pa_aupdate_write_end(pa_aupdate *a) {
pa_assert(a);
if (!a->swapped)
pa_aupdate_write_swap(a);
pa_mutex_unlock(a->write_lock);
}