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