Huge trailing whitespace cleanup. Let's keep the tree pure from here on,
[platform/upstream/pulseaudio.git] / 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/once.h>
29 #include <pulsecore/log.h>
30 #include <pulsecore/core-util.h>
31 #include <pulse/xmalloc.h>
32
33 static pa_mutex *mutex = NULL;
34 static pa_cond *cond1 = NULL, *cond2 = NULL;
35 static pa_tls *tls = NULL;
36
37 static int magic_number = 0;
38
39 #define THREADS_MAX 20
40
41 static void once_func(void) {
42     pa_log("once!");
43 }
44
45 static pa_once_t once = PA_ONCE_INIT;
46
47 static void thread_func(void *data) {
48     pa_tls_set(tls, data);
49
50     pa_log("thread_func() for %s starting...", (char*) pa_tls_get(tls));
51
52     pa_mutex_lock(mutex);
53
54     for (;;) {
55         int k, n;
56
57         pa_log("%s waiting ...", (char*) pa_tls_get(tls));
58
59         for (;;) {
60
61             if (magic_number < 0)
62                 goto quit;
63
64             if (magic_number != 0)
65                 break;
66
67             pa_cond_wait(cond1, mutex);
68         }
69
70         k = magic_number;
71         magic_number = 0;
72
73         pa_mutex_unlock(mutex);
74
75         pa_once(&once, once_func);
76
77         pa_cond_signal(cond2, 0);
78
79         pa_log("%s got number %i", (char*) pa_tls_get(tls), k);
80
81         /* Spin! */
82         for (n = 0; n < k; n++)
83             pa_thread_yield();
84
85         pa_mutex_lock(mutex);
86     }
87
88 quit:
89
90     pa_mutex_unlock(mutex);
91
92     pa_log("thread_func() for %s done...", (char*) pa_tls_get(tls));
93 }
94
95 int main(int argc, char *argv[]) {
96     int i, k;
97     pa_thread* t[THREADS_MAX];
98
99     assert(pa_thread_is_running(pa_thread_self()));
100
101     mutex = pa_mutex_new(0);
102     cond1 = pa_cond_new();
103     cond2 = pa_cond_new();
104     tls = pa_tls_new(pa_xfree);
105
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]);
109     }
110
111     pa_mutex_lock(mutex);
112
113     pa_log("loop-init");
114
115     for (k = 0; k < 100; k++) {
116         assert(magic_number == 0);
117
118
119         magic_number = (int) rand() % 0x10000;
120
121         pa_log("iteration %i (%i)", k, magic_number);
122
123         pa_cond_signal(cond1, 0);
124
125         pa_cond_wait(cond2, mutex);
126     }
127
128     pa_log("loop-exit");
129
130     magic_number = -1;
131     pa_cond_signal(cond1, 1);
132
133     pa_mutex_unlock(mutex);
134
135     for (i = 0; i < THREADS_MAX; i++)
136         pa_thread_free(t[i]);
137
138     pa_mutex_free(mutex);
139     pa_cond_free(cond1);
140     pa_cond_free(cond2);
141     pa_tls_free(tls);
142
143     return 0;
144 }