Add a missing pa_xfree.
[pulseaudio.git] / src / tests / thread-test.c
blob72dde6cbb942201727ba8d4fbbbd3a81fe807fca
1 /* $Id$ */
3 /***
4 This file is part of PulseAudio.
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.
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.
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 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <pulsecore/thread.h>
27 #include <pulsecore/mutex.h>
28 #include <pulsecore/once.h>
29 #include <pulsecore/log.h>
30 #include <pulsecore/core-util.h>
31 #include <pulse/xmalloc.h>
33 static pa_mutex *mutex = NULL;
34 static pa_cond *cond1 = NULL, *cond2 = NULL;
35 static pa_tls *tls = NULL;
37 static int magic_number = 0;
39 #define THREADS_MAX 20
41 static void once_func(void) {
42 pa_log("once!");
45 static pa_once once = PA_ONCE_INIT;
47 static void thread_func(void *data) {
48 pa_tls_set(tls, data);
50 pa_log("thread_func() for %s starting...", (char*) pa_tls_get(tls));
52 pa_mutex_lock(mutex);
54 for (;;) {
55 int k, n;
57 pa_log("%s waiting ...", (char*) pa_tls_get(tls));
59 for (;;) {
61 if (magic_number < 0)
62 goto quit;
64 if (magic_number != 0)
65 break;
67 pa_cond_wait(cond1, mutex);
70 k = magic_number;
71 magic_number = 0;
73 pa_mutex_unlock(mutex);
75 pa_run_once(&once, once_func);
77 pa_cond_signal(cond2, 0);
79 pa_log("%s got number %i", (char*) pa_tls_get(tls), k);
81 /* Spin! */
82 for (n = 0; n < k; n++)
83 pa_thread_yield();
85 pa_mutex_lock(mutex);
88 quit:
90 pa_mutex_unlock(mutex);
92 pa_log("thread_func() for %s done...", (char*) pa_tls_get(tls));
95 int main(int argc, char *argv[]) {
96 int i, k;
97 pa_thread* t[THREADS_MAX];
99 assert(pa_thread_is_running(pa_thread_self()));
101 mutex = pa_mutex_new(FALSE, FALSE);
102 cond1 = pa_cond_new();
103 cond2 = pa_cond_new();
104 tls = pa_tls_new(pa_xfree);
106 for (i = 0; i < THREADS_MAX; i++) {
107 t[i] = pa_thread_new(thread_func, pa_sprintf_malloc("Thread #%i", i+1));
108 assert(t[i]);
111 pa_mutex_lock(mutex);
113 pa_log("loop-init");
115 for (k = 0; k < 100; k++) {
116 assert(magic_number == 0);
119 magic_number = (int) rand() % 0x10000;
121 pa_log("iteration %i (%i)", k, magic_number);
123 pa_cond_signal(cond1, 0);
125 pa_cond_wait(cond2, mutex);
128 pa_log("loop-exit");
130 magic_number = -1;
131 pa_cond_signal(cond1, 1);
133 pa_mutex_unlock(mutex);
135 for (i = 0; i < THREADS_MAX; i++)
136 pa_thread_free(t[i]);
138 pa_mutex_free(mutex);
139 pa_cond_free(cond1);
140 pa_cond_free(cond2);
141 pa_tls_free(tls);
143 return 0;