]> code.delx.au - pulseaudio/blob - src/tests/once-test.c
b39a0e33f4783b3da7e97dc3f0702a3e2061afaf
[pulseaudio] / src / tests / once-test.c
1 /***
2 This file is part of PulseAudio.
3
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.
8
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.
13
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
17 USA.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #ifdef HAVE_PTHREAD
25 #include <pthread.h>
26 #endif
27
28 #include <check.h>
29
30 #include <pulsecore/thread.h>
31 #include <pulsecore/once.h>
32 #include <pulsecore/log.h>
33 #include <pulsecore/core-util.h>
34 #include <pulsecore/atomic.h>
35 #include <pulse/xmalloc.h>
36
37 static pa_once once = PA_ONCE_INIT;
38 static volatile unsigned n_run = 0;
39 static const char * volatile ran_by = NULL;
40 #ifdef HAVE_PTHREAD
41 static pthread_barrier_t barrier;
42 #endif
43 static unsigned n_cpu;
44
45 #define N_ITERATIONS 500
46 #define N_THREADS 100
47
48 static void once_func(void) {
49 n_run++;
50 ran_by = (const char*) pa_thread_get_data(pa_thread_self());
51 }
52
53 static void thread_func(void *data) {
54 #ifdef HAVE_PTHREAD
55 int r;
56
57 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
58 static pa_atomic_t i_cpu = PA_ATOMIC_INIT(0);
59 cpu_set_t mask;
60
61 CPU_ZERO(&mask);
62 CPU_SET((size_t) (pa_atomic_inc(&i_cpu) % n_cpu), &mask);
63 fail_unless(pthread_setaffinity_np(pthread_self(), sizeof(mask), &mask) == 0);
64 #endif
65
66 pa_log_debug("started up: %s", (char *) data);
67
68 r = pthread_barrier_wait(&barrier);
69 fail_unless(r == 0 || r == PTHREAD_BARRIER_SERIAL_THREAD);
70 #endif /* HAVE_PTHREAD */
71
72 pa_run_once(&once, once_func);
73 }
74
75 START_TEST (once_test) {
76 unsigned n, i;
77
78 n_cpu = pa_ncpus();
79
80 for (n = 0; n < N_ITERATIONS; n++) {
81 pa_thread* threads[N_THREADS];
82
83 #ifdef HAVE_PTHREAD
84 fail_unless(pthread_barrier_init(&barrier, NULL, N_THREADS) == 0);
85 #endif
86
87 /* Yes, kinda ugly */
88 pa_zero(once);
89
90 for (i = 0; i < N_THREADS; i++)
91 threads[i] = pa_thread_new("once", thread_func, pa_sprintf_malloc("Thread #%i", i+1));
92
93 for (i = 0; i < N_THREADS; i++)
94 pa_thread_join(threads[i]);
95
96 fail_unless(n_run == 1);
97 pa_log_info("ran by %s", ran_by);
98
99 for (i = 0; i < N_THREADS; i++) {
100 pa_xfree(pa_thread_get_data(threads[i]));
101 pa_thread_free(threads[i]);
102 }
103
104 n_run = 0;
105 ran_by = NULL;
106
107 #ifdef HAVE_PTHREAD
108 fail_unless(pthread_barrier_destroy(&barrier) == 0);
109 #endif
110 }
111 }
112 END_TEST
113
114 int main(int argc, char *argv[]) {
115 int failed = 0;
116 Suite *s;
117 TCase *tc;
118 SRunner *sr;
119
120 if (!getenv("MAKE_CHECK"))
121 pa_log_set_level(PA_LOG_DEBUG);
122
123 s = suite_create("Once");
124 tc = tcase_create("once");
125 tcase_add_test(tc, once_test);
126 /* the default timeout is too small,
127 * set it to a reasonable large one.
128 */
129 tcase_set_timeout(tc, 60 * 60);
130 suite_add_tcase(s, tc);
131
132 sr = srunner_create(s);
133 srunner_run_all(sr, CK_NORMAL);
134 failed = srunner_ntests_failed(sr);
135 srunner_free(sr);
136
137 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
138 }