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