]> code.delx.au - pulseaudio/blob - src/tests/thread-test.c
add a threading primitive API
[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 <sched.h>
27
28 #include <pulsecore/thread.h>
29 #include <pulsecore/mutex.h>
30 #include <pulsecore/log.h>
31 #include <pulsecore/core-util.h>
32 #include <pulse/xmalloc.h>
33
34 static pa_mutex *mutex = NULL;
35 static pa_cond *cond1 = NULL, *cond2 = NULL;
36 static pa_tls *tls = NULL;
37
38 static int magic_number = 0;
39
40 #define THREADS_MAX 20
41
42 static void thread_func(void *data) {
43 pa_tls_set(tls, data);
44
45 pa_log("thread_func() for %s starting...", (char*) pa_tls_get(tls));
46
47 pa_mutex_lock(mutex);
48
49 for (;;) {
50 int k, n;
51
52 pa_log("%s waiting ...", (char*) pa_tls_get(tls));
53
54 for (;;) {
55
56 if (magic_number < 0)
57 goto quit;
58
59 if (magic_number != 0)
60 break;
61
62 pa_cond_wait(cond1, mutex);
63 }
64
65 k = magic_number;
66 magic_number = 0;
67
68 pa_mutex_unlock(mutex);
69
70 pa_cond_signal(cond2, 0);
71
72 pa_log("%s got number %i", (char*) pa_tls_get(tls), k);
73
74 /* Spin! */
75 for (n = 0; n < k; n++)
76 sched_yield();
77
78 pa_mutex_lock(mutex);
79 }
80
81 quit:
82
83 pa_mutex_unlock(mutex);
84
85 pa_log("thread_func() for %s done...", (char*) pa_tls_get(tls));
86 }
87
88 int main(int argc, char *argv[]) {
89 int i, k;
90 pa_thread* t[THREADS_MAX];
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 }