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
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>
34 static pa_mutex *mutex = NULL;
35 static pa_cond *cond1 = NULL, *cond2 = NULL;
36 static pa_tls *tls = NULL;
38 static int magic_number = 0;
40 #define THREADS_MAX 20
42 static void thread_func(void *data) {
43 pa_tls_set(tls, data);
45 pa_log("thread_func() for %s starting...", (char*) pa_tls_get(tls));
52 pa_log("%s waiting ...", (char*) pa_tls_get(tls));
59 if (magic_number != 0)
62 pa_cond_wait(cond1, mutex);
68 pa_mutex_unlock(mutex);
70 pa_cond_signal(cond2, 0);
72 pa_log("%s got number %i", (char*) pa_tls_get(tls), k);
75 for (n = 0; n < k; n++)
83 pa_mutex_unlock(mutex);
85 pa_log("thread_func() for %s done...", (char*) pa_tls_get(tls));
88 int main(int argc, char *argv[]) {
90 pa_thread* t[THREADS_MAX];
92 mutex = pa_mutex_new(0);
93 cond1 = pa_cond_new();
94 cond2 = pa_cond_new();
95 tls = pa_tls_new(pa_xfree);
97 for (i = 0; i < THREADS_MAX; i++) {
98 t[i] = pa_thread_new(thread_func, pa_sprintf_malloc("Thread #%i", i+1));
102 pa_mutex_lock(mutex);
106 for (k = 0; k < 100; k++) {
107 assert(magic_number == 0);
110 magic_number = (int) rand() % 0x10000;
112 pa_log("iteration %i (%i)", k, magic_number);
114 pa_cond_signal(cond1, 0);
116 pa_cond_wait(cond2, mutex);
122 pa_cond_signal(cond1, 1);
124 pa_mutex_unlock(mutex);
126 for (i = 0; i < THREADS_MAX; i++)
127 pa_thread_free(t[i]);
129 pa_mutex_free(mutex);