]>
code.delx.au - pulseaudio/blob - src/tests/thread-test.c
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
26 #include <pulse/xmalloc.h>
27 #include <pulsecore/thread.h>
28 #include <pulsecore/macro.h>
29 #include <pulsecore/mutex.h>
30 #include <pulsecore/once.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/core-util.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 once_func(void) {
46 static pa_once once
= PA_ONCE_INIT
;
48 static void thread_func(void *data
) {
49 pa_tls_set(tls
, data
);
51 pa_log_info("thread_func() for %s starting...", (char*) pa_tls_get(tls
));
58 pa_log_info("%s waiting ...", (char*) pa_tls_get(tls
));
65 if (magic_number
!= 0)
68 pa_cond_wait(cond1
, mutex
);
74 pa_mutex_unlock(mutex
);
76 pa_run_once(&once
, once_func
);
78 pa_cond_signal(cond2
, 0);
80 pa_log_info("%s got number %i", (char*) pa_tls_get(tls
), k
);
83 for (n
= 0; n
< k
; n
++)
91 pa_mutex_unlock(mutex
);
93 pa_log_info("thread_func() for %s done...", (char*) pa_tls_get(tls
));
96 START_TEST (thread_test
) {
98 pa_thread
* t
[THREADS_MAX
];
100 if (!getenv("MAKE_CHECK"))
101 pa_log_set_level(PA_LOG_DEBUG
);
103 mutex
= pa_mutex_new(false, false);
104 cond1
= pa_cond_new();
105 cond2
= pa_cond_new();
106 tls
= pa_tls_new(pa_xfree
);
108 for (i
= 0; i
< THREADS_MAX
; i
++) {
109 t
[i
] = pa_thread_new("test", thread_func
, pa_sprintf_malloc("Thread #%i", i
+1));
110 fail_unless(t
[i
] != 0);
113 pa_mutex_lock(mutex
);
117 for (k
= 0; k
< 100; k
++) {
118 pa_assert(magic_number
== 0);
120 magic_number
= (int) rand() % 0x10000;
122 pa_log_info("iteration %i (%i)", k
, magic_number
);
124 pa_cond_signal(cond1
, 0);
126 pa_cond_wait(cond2
, mutex
);
132 pa_cond_signal(cond1
, 1);
134 pa_mutex_unlock(mutex
);
136 for (i
= 0; i
< THREADS_MAX
; i
++)
137 pa_thread_free(t
[i
]);
139 pa_mutex_free(mutex
);
146 int main(int argc
, char *argv
[]) {
152 s
= suite_create("Thread");
153 tc
= tcase_create("thread");
154 tcase_add_test(tc
, thread_test
);
155 suite_add_tcase(s
, tc
);
157 sr
= srunner_create(s
);
158 srunner_run_all(sr
, CK_NORMAL
);
159 failed
= srunner_ntests_failed(sr
);
162 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;