]> code.delx.au - pulseaudio/blob - src/tests/thread-test.c
Also wrap yield functionality so that it can be platform independent.
[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 mutex = pa_mutex_new(0);
91 cond1 = pa_cond_new();
92 cond2 = pa_cond_new();
93 tls = pa_tls_new(pa_xfree);
94
95 for (i = 0; i < THREADS_MAX; i++) {
96 t[i] = pa_thread_new(thread_func, pa_sprintf_malloc("Thread #%i", i+1));
97 assert(t[i]);
98 }
99
100 pa_mutex_lock(mutex);
101
102 pa_log("loop-init");
103
104 for (k = 0; k < 100; k++) {
105 assert(magic_number == 0);
106
107
108 magic_number = (int) rand() % 0x10000;
109
110 pa_log("iteration %i (%i)", k, magic_number);
111
112 pa_cond_signal(cond1, 0);
113
114 pa_cond_wait(cond2, mutex);
115 }
116
117 pa_log("loop-exit");
118
119 magic_number = -1;
120 pa_cond_signal(cond1, 1);
121
122 pa_mutex_unlock(mutex);
123
124 for (i = 0; i < THREADS_MAX; i++)
125 pa_thread_free(t[i]);
126
127 pa_mutex_free(mutex);
128 pa_cond_free(cond1);
129 pa_cond_free(cond2);
130 pa_tls_free(tls);
131
132 return 0;
133 }