]>
code.delx.au - pulseaudio/blob - src/tests/thread-test.c
aaa79fb58850f27511def290a2941ee1c593f24e
2 This file is part of PulseAudio.
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.
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.
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
24 #include <pulse/xmalloc.h>
25 #include <pulsecore/thread.h>
26 #include <pulsecore/macro.h>
27 #include <pulsecore/mutex.h>
28 #include <pulsecore/once.h>
29 #include <pulsecore/log.h>
30 #include <pulsecore/core-util.h>
32 static pa_mutex
*mutex
= NULL
;
33 static pa_cond
*cond1
= NULL
, *cond2
= NULL
;
34 static pa_tls
*tls
= NULL
;
36 static int magic_number
= 0;
38 #define THREADS_MAX 20
40 static void once_func(void) {
44 static pa_once once
= PA_ONCE_INIT
;
46 static void thread_func(void *data
) {
47 pa_tls_set(tls
, data
);
49 pa_log("thread_func() for %s starting...", (char*) pa_tls_get(tls
));
56 pa_log("%s waiting ...", (char*) pa_tls_get(tls
));
63 if (magic_number
!= 0)
66 pa_cond_wait(cond1
, mutex
);
72 pa_mutex_unlock(mutex
);
74 pa_run_once(&once
, once_func
);
76 pa_cond_signal(cond2
, 0);
78 pa_log("%s got number %i", (char*) pa_tls_get(tls
), k
);
81 for (n
= 0; n
< k
; n
++)
89 pa_mutex_unlock(mutex
);
91 pa_log("thread_func() for %s done...", (char*) pa_tls_get(tls
));
94 int main(int argc
, char *argv
[]) {
96 pa_thread
* t
[THREADS_MAX
];
98 mutex
= pa_mutex_new(FALSE
, FALSE
);
99 cond1
= pa_cond_new();
100 cond2
= pa_cond_new();
101 tls
= pa_tls_new(pa_xfree
);
103 for (i
= 0; i
< THREADS_MAX
; i
++) {
104 pa_assert_se(t
[i
] = pa_thread_new("test", thread_func
, pa_sprintf_malloc("Thread #%i", i
+1)));
107 pa_mutex_lock(mutex
);
111 for (k
= 0; k
< 100; k
++) {
112 pa_assert(magic_number
== 0);
114 magic_number
= (int) rand() % 0x10000;
116 pa_log("iteration %i (%i)", k
, magic_number
);
118 pa_cond_signal(cond1
, 0);
120 pa_cond_wait(cond2
, mutex
);
126 pa_cond_signal(cond1
, 1);
128 pa_mutex_unlock(mutex
);
130 for (i
= 0; i
< THREADS_MAX
; i
++)
131 pa_thread_free(t
[i
]);
133 pa_mutex_free(mutex
);