Branch data Line data Source code
1 : : /***
2 : : This file is part of PulseAudio.
3 : :
4 : : PulseAudio is free software; you can redistribute it and/or modify
5 : : it under the terms of the GNU Lesser General Public License as published
6 : : by the Free Software Foundation; either version 2.1 of the License,
7 : : or (at your option) any later version.
8 : :
9 : : PulseAudio is distributed in the hope that it will be useful, but
10 : : WITHOUT ANY WARRANTY; without even the implied warranty of
11 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : : General Public License for more details.
13 : :
14 : : You should have received a copy of the GNU Lesser General Public License
15 : : along with PulseAudio; if not, write to the Free Software
16 : : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 : : USA.
18 : : ***/
19 : :
20 : : #ifdef HAVE_CONFIG_H
21 : : #include <config.h>
22 : : #endif
23 : :
24 : : #include <pulse/xmalloc.h>
25 : : #include <pulsecore/thread.h>
26 : : #include <pulsecore/macro.h>
27 : : #include <pulsecore/mutex.h>
28 : : #include <pulsecore/once.h>
29 : : #include <pulsecore/log.h>
30 : : #include <pulsecore/core-util.h>
31 : :
32 : : static pa_mutex *mutex = NULL;
33 : : static pa_cond *cond1 = NULL, *cond2 = NULL;
34 : : static pa_tls *tls = NULL;
35 : :
36 : : static int magic_number = 0;
37 : :
38 : : #define THREADS_MAX 20
39 : :
40 : 1 : static void once_func(void) {
41 : 1 : pa_log("once!");
42 : 1 : }
43 : :
44 : : static pa_once once = PA_ONCE_INIT;
45 : :
46 : 20 : static void thread_func(void *data) {
47 : 20 : pa_tls_set(tls, data);
48 : :
49 : 20 : pa_log_info("thread_func() for %s starting...", (char*) pa_tls_get(tls));
50 : :
51 : 20 : pa_mutex_lock(mutex);
52 : :
53 : : for (;;) {
54 : : int k, n;
55 : :
56 : 120 : pa_log_info("%s waiting ...", (char*) pa_tls_get(tls));
57 : :
58 : : for (;;) {
59 : :
60 [ + + ]: 155 : if (magic_number < 0)
61 : : goto quit;
62 : :
63 [ + + ]: 135 : if (magic_number != 0)
64 : : break;
65 : :
66 : 35 : pa_cond_wait(cond1, mutex);
67 : 35 : }
68 : :
69 : 100 : k = magic_number;
70 : 100 : magic_number = 0;
71 : :
72 : 100 : pa_mutex_unlock(mutex);
73 : :
74 : 100 : pa_run_once(&once, once_func);
75 : :
76 : 100 : pa_cond_signal(cond2, 0);
77 : :
78 : 100 : pa_log_info("%s got number %i", (char*) pa_tls_get(tls), k);
79 : :
80 : : /* Spin! */
81 [ + + ]: 2271122 : for (n = 0; n < k; n++)
82 : 2440026 : pa_thread_yield();
83 : :
84 : 100 : pa_mutex_lock(mutex);
85 : 100 : }
86 : :
87 : : quit:
88 : :
89 : 20 : pa_mutex_unlock(mutex);
90 : :
91 : 20 : pa_log_info("thread_func() for %s done...", (char*) pa_tls_get(tls));
92 : 20 : }
93 : :
94 : 1 : int main(int argc, char *argv[]) {
95 : : int i, k;
96 : : pa_thread* t[THREADS_MAX];
97 : :
98 [ - + ]: 1 : if (!getenv("MAKE_CHECK"))
99 : 0 : pa_log_set_level(PA_LOG_DEBUG);
100 : :
101 : 1 : mutex = pa_mutex_new(FALSE, FALSE);
102 : 1 : cond1 = pa_cond_new();
103 : 1 : cond2 = pa_cond_new();
104 : 1 : tls = pa_tls_new(pa_xfree);
105 : :
106 [ + + ]: 21 : for (i = 0; i < THREADS_MAX; i++) {
107 [ - + ]: 20 : pa_assert_se(t[i] = pa_thread_new("test", thread_func, pa_sprintf_malloc("Thread #%i", i+1)));
108 : : }
109 : :
110 : 1 : pa_mutex_lock(mutex);
111 : :
112 : 1 : pa_log("loop-init");
113 : :
114 [ + + ]: 101 : for (k = 0; k < 100; k++) {
115 [ - + ]: 100 : pa_assert(magic_number == 0);
116 : :
117 : 100 : magic_number = (int) rand() % 0x10000;
118 : :
119 : 100 : pa_log_info("iteration %i (%i)", k, magic_number);
120 : :
121 : 100 : pa_cond_signal(cond1, 0);
122 : :
123 : 100 : pa_cond_wait(cond2, mutex);
124 : : }
125 : :
126 : 1 : pa_log("loop-exit");
127 : :
128 : 1 : magic_number = -1;
129 : 1 : pa_cond_signal(cond1, 1);
130 : :
131 : 1 : pa_mutex_unlock(mutex);
132 : :
133 [ + + ]: 21 : for (i = 0; i < THREADS_MAX; i++)
134 : 20 : pa_thread_free(t[i]);
135 : :
136 : 1 : pa_mutex_free(mutex);
137 : 1 : pa_cond_free(cond1);
138 : 1 : pa_cond_free(cond2);
139 : 1 : pa_tls_free(tls);
140 : :
141 : : return 0;
142 : : }
|