Merge commit 'origin/master-tx'
[profile/ivi/pulseaudio-panda.git] / src / pulsecore / rtsig.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2004-2006 Lennart Poettering
5   Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7   PulseAudio is free software; you can redistribute it and/or modify
8   it under the terms of the GNU Lesser General Public License as
9   published by the Free Software Foundation; either version 2.1 of the
10   License, or (at your option) any later version.
11
12   PulseAudio is distributed in the hope that it will be useful, but
13   WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17   You should have received a copy of the GNU Lesser General Public
18   License along with PulseAudio; if not, write to the Free Software
19   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20   USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <signal.h>
28
29 #include <pulsecore/macro.h>
30 #include <pulsecore/flist.h>
31 #include <pulsecore/once.h>
32 #include <pulsecore/thread.h>
33 #include <pulsecore/core-util.h>
34
35 #include "rtsig.h"
36
37 #ifdef SIGRTMIN
38
39 static void _free_rtsig(void *p) {
40     pa_rtsig_put(PA_PTR_TO_INT(p));
41 }
42
43 PA_STATIC_FLIST_DECLARE(rtsig_flist, pa_make_power_of_two((unsigned) (SIGRTMAX-SIGRTMIN+1)), NULL);
44 PA_STATIC_TLS_DECLARE(rtsig_tls, _free_rtsig);
45
46 static pa_atomic_t rtsig_current = PA_ATOMIC_INIT(-1);
47
48 static int rtsig_start = -1, rtsig_end = -1;
49
50 int pa_rtsig_get(void) {
51     void *p;
52     int sig;
53
54     if ((p = pa_flist_pop(PA_STATIC_FLIST_GET(rtsig_flist))))
55         return PA_PTR_TO_INT(p);
56
57     sig = pa_atomic_dec(&rtsig_current);
58
59     pa_assert(sig <= SIGRTMAX);
60     pa_assert(sig <= rtsig_end);
61
62     if (sig < rtsig_start) {
63         pa_atomic_inc(&rtsig_current);
64         return -1;
65     }
66
67     return sig;
68 }
69
70 int pa_rtsig_get_for_thread(void) {
71     int sig;
72     void *p;
73
74     if ((p = PA_STATIC_TLS_GET(rtsig_tls)))
75         return PA_PTR_TO_INT(p);
76
77     if ((sig = pa_rtsig_get()) < 0)
78         return -1;
79
80     PA_STATIC_TLS_SET(rtsig_tls, PA_INT_TO_PTR(sig));
81     return sig;
82 }
83
84 void pa_rtsig_put(int sig) {
85     pa_assert(sig >= rtsig_start);
86     pa_assert(sig <= rtsig_end);
87
88     pa_assert_se(pa_flist_push(PA_STATIC_FLIST_GET(rtsig_flist), PA_INT_TO_PTR(sig)) >= 0);
89 }
90
91 void pa_rtsig_configure(int start, int end) {
92     int s;
93     sigset_t ss;
94
95     pa_assert(pa_atomic_load(&rtsig_current) == -1);
96
97     pa_assert(SIGRTMIN <= start);
98     pa_assert(start <= end);
99     pa_assert(end <= SIGRTMAX);
100
101     rtsig_start = start;
102     rtsig_end = end;
103
104     sigemptyset(&ss);
105
106     for (s = rtsig_start; s <= rtsig_end; s++)
107         pa_assert_se(sigaddset(&ss, s) == 0);
108
109     pa_assert(pthread_sigmask(SIG_BLOCK, &ss, NULL) == 0);
110
111     /* We allocate starting from the end */
112     pa_atomic_store(&rtsig_current, rtsig_end);
113 }
114
115 #else /* SIGRTMIN */
116
117 int pa_rtsig_get(void) {
118     return -1;
119 }
120
121 int pa_rtsig_get_for_thread(void) {
122     return -1;
123 }
124
125 void pa_rtsig_put(int sig) {
126 }
127
128 void pa_rtsig_configure(int start, int end) {
129 }
130
131 #endif /* SIGRTMIN */