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
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>
33 static pa_mutex *mutex = NULL;
34 static pa_cond *cond1 = NULL, *cond2 = NULL;
35 static pa_tls *tls = NULL;
37 static int magic_number = 0;
39 #define THREADS_MAX 20
41 static void once_func(void) {
45 static pa_once_t once = PA_ONCE_INIT;
47 static void thread_func(void *data) {
48 pa_tls_set(tls, data);
50 pa_log("thread_func() for %s starting...", (char*) pa_tls_get(tls));
57 pa_log("%s waiting ...", (char*) pa_tls_get(tls));
64 if (magic_number != 0)
67 pa_cond_wait(cond1, mutex);
73 pa_mutex_unlock(mutex);
75 pa_once(&once, once_func);
77 pa_cond_signal(cond2, 0);
79 pa_log("%s got number %i", (char*) pa_tls_get(tls), k);
82 for (n = 0; n < k; n++)
90 pa_mutex_unlock(mutex);
92 pa_log("thread_func() for %s done...", (char*) pa_tls_get(tls));
95 int main(int argc, char *argv[]) {
97 pa_thread* t[THREADS_MAX];
99 assert(pa_thread_is_running(pa_thread_self()));
101 mutex = pa_mutex_new(0);
102 cond1 = pa_cond_new();
103 cond2 = pa_cond_new();
104 tls = pa_tls_new(pa_xfree);
106 for (i = 0; i < THREADS_MAX; i++) {
107 t[i] = pa_thread_new(thread_func, pa_sprintf_malloc("Thread #%i", i+1));
111 pa_mutex_lock(mutex);
115 for (k = 0; k < 100; k++) {
116 assert(magic_number == 0);
119 magic_number = (int) rand() % 0x10000;
121 pa_log("iteration %i (%i)", k, magic_number);
123 pa_cond_signal(cond1, 0);
125 pa_cond_wait(cond2, mutex);
131 pa_cond_signal(cond1, 1);
133 pa_mutex_unlock(mutex);
135 for (i = 0; i < THREADS_MAX; i++)
136 pa_thread_free(t[i]);
138 pa_mutex_free(mutex);