diff options
| -rw-r--r-- | src/pulsecore/rtsig.c | 111 | ||||
| -rw-r--r-- | src/pulsecore/rtsig.h | 44 | 
2 files changed, 155 insertions, 0 deletions
diff --git a/src/pulsecore/rtsig.c b/src/pulsecore/rtsig.c new file mode 100644 index 00000000..1b8cdaa3 --- /dev/null +++ b/src/pulsecore/rtsig.c @@ -0,0 +1,111 @@ +/* $Id$ */ + +/*** +  This file is part of PulseAudio. + +  Copyright 2004-2006 Lennart Poettering +  Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB + +  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 +  Lesser 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 <signal.h> + +#include <pulsecore/macro.h> +#include <pulsecore/flist.h> +#include <pulsecore/once.h> +#include <pulsecore/thread.h> + +#include "rtsig.h" + +static void _free_rtsig(void *p) { +    pa_rtsig_put(PA_PTR_TO_INT(p)); +} + +PA_STATIC_FLIST_DECLARE(rtsig_flist, pa_make_power_of_two(SIGRTMAX-SIGRTMIN+1), NULL); +PA_STATIC_TLS_DECLARE(rtsig_tls, _free_rtsig); + +static pa_atomic_t rtsig_current = PA_ATOMIC_INIT(-1); + +static int rtsig_start = -1, rtsig_end = -1; + +int pa_rtsig_get(void) { +    void *p; +    int sig; +     +    if ((p = pa_flist_pop(PA_STATIC_FLIST_GET(rtsig_flist)))) +        return PA_PTR_TO_INT(p); + +    sig = pa_atomic_inc(&rtsig_current); + +    pa_assert(sig >= SIGRTMIN); +    pa_assert(sig >= rtsig_start); +     +    if (sig > rtsig_end) { +        pa_atomic_dec(&rtsig_current); +        return -1; +    } + +    return sig; +} + +int pa_rtsig_get_for_thread(void) { +    int sig; +    void *p; + +    if ((p = pa_tls_get(PA_STATIC_TLS_GET(rtsig_tls)))) +        return PA_PTR_TO_INT(p); +     +    if ((sig = pa_rtsig_get()) < 0) +        return -1; + +    pa_tls_set(PA_STATIC_TLS_GET(rtsig_tls), PA_INT_TO_PTR(sig)); +    return sig; +} + +void pa_rtsig_put(int sig) { +    pa_assert(sig >= rtsig_start); +    pa_assert(sig <= rtsig_end); + +    pa_assert_se(pa_flist_push(PA_STATIC_FLIST_GET(rtsig_flist), PA_INT_TO_PTR(sig)) >= 0); +} + +void pa_rtsig_configure(int start, int end) { +    int s; +    sigset_t ss; + +    pa_assert(pa_atomic_load(&rtsig_current) == -1); + +    pa_assert(SIGRTMIN <= start); +    pa_assert(start <= end); +    pa_assert(end <= SIGRTMAX); + +    rtsig_start = start; +    rtsig_end = end; + +    sigemptyset(&ss); +     +    for (s = rtsig_start; s <= rtsig_end; s++) +        pa_assert_se(sigaddset(&ss, s) == 0); +     +    pa_assert(pthread_sigmask(SIG_BLOCK, &ss, NULL) == 0); +     +    pa_atomic_store(&rtsig_current, rtsig_start); +} diff --git a/src/pulsecore/rtsig.h b/src/pulsecore/rtsig.h new file mode 100644 index 00000000..48f5f050 --- /dev/null +++ b/src/pulsecore/rtsig.h @@ -0,0 +1,44 @@ +#ifndef foopulsertsighfoo +#define foopulsertsighfoo + +/* $Id$ */ + +/*** +  This file is part of PulseAudio. + +  Copyright 2004-2006 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 +  Lesser 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. +***/ + +#include <poll.h> +#include <sys/types.h> + +/* Return the next unused POSIX Realtime signals */ +int pa_rtsig_get(void); + +/* If not called before in the current thread, return the next unused + * rtsig, and install it in a TLS region and give it up automatically + * when the thread shuts down */ +int pa_rtsig_get_for_thread(void); + +/* Give an rtsig back. */ +void pa_rtsig_put(int sig); + +/* Block all RT signals */ +void pa_rtsig_configure(int start, int end); + +#endif  | 
