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
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>
37 static pa_once once
= PA_ONCE_INIT
;
38 static volatile unsigned n_run
= 0;
39 static const char * volatile ran_by
= NULL
;
41 static pthread_barrier_t barrier
;
43 static unsigned n_cpu
;
45 #define N_ITERATIONS 500
48 static void once_func(void) {
50 ran_by
= (const char*) pa_thread_get_data(pa_thread_self());
53 static void thread_func(void *data
) {
57 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
58 static pa_atomic_t i_cpu
= PA_ATOMIC_INIT(0);
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);
66 pa_log_debug("started up: %s", (char *) data
);
68 r
= pthread_barrier_wait(&barrier
);
69 fail_unless(r
== 0 || r
== PTHREAD_BARRIER_SERIAL_THREAD
);
70 #endif /* HAVE_PTHREAD */
72 pa_run_once(&once
, once_func
);
75 START_TEST (once_test
) {
80 for (n
= 0; n
< N_ITERATIONS
; n
++) {
81 pa_thread
* threads
[N_THREADS
];
84 fail_unless(pthread_barrier_init(&barrier
, NULL
, N_THREADS
) == 0);
90 for (i
= 0; i
< N_THREADS
; i
++)
91 threads
[i
] = pa_thread_new("once", thread_func
, pa_sprintf_malloc("Thread #%i", i
+1));
93 for (i
= 0; i
< N_THREADS
; i
++)
94 pa_thread_join(threads
[i
]);
96 fail_unless(n_run
== 1);
97 pa_log_info("ran by %s", ran_by
);
99 for (i
= 0; i
< N_THREADS
; i
++) {
100 pa_xfree(pa_thread_get_data(threads
[i
]));
101 pa_thread_free(threads
[i
]);
108 fail_unless(pthread_barrier_destroy(&barrier
) == 0);
114 int main(int argc
, char *argv
[]) {
120 if (!getenv("MAKE_CHECK"))
121 pa_log_set_level(PA_LOG_DEBUG
);
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.
129 tcase_set_timeout(tc
, 60 * 60);
130 suite_add_tcase(s
, tc
);
132 sr
= srunner_create(s
);
133 srunner_run_all(sr
, CK_NORMAL
);
134 failed
= srunner_ntests_failed(sr
);
137 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;