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 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
27 #if defined(__FreeBSD__)
28 #include <pthread_np.h>
29 #include <sys/param.h>
30 #include <sys/cpuset.h>
37 #include <pulsecore/thread.h>
38 #include <pulsecore/once.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/core-util.h>
41 #include <pulsecore/atomic.h>
42 #include <pulse/xmalloc.h>
44 static pa_once once
= PA_ONCE_INIT
;
45 static volatile unsigned n_run
= 0;
46 static const char * volatile ran_by
= NULL
;
48 static pthread_barrier_t barrier
;
50 static unsigned n_cpu
;
52 #define N_ITERATIONS 500
55 static void once_func(void) {
57 ran_by
= (const char*) pa_thread_get_data(pa_thread_self());
60 static void thread_func(void *data
) {
64 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
65 static pa_atomic_t i_cpu
= PA_ATOMIC_INIT(0);
73 CPU_SET((size_t) (pa_atomic_inc(&i_cpu
) % n_cpu
), &mask
);
74 fail_unless(pthread_setaffinity_np(pthread_self(), sizeof(mask
), &mask
) == 0);
77 pa_log_debug("started up: %s", (char *) data
);
79 r
= pthread_barrier_wait(&barrier
);
80 fail_unless(r
== 0 || r
== PTHREAD_BARRIER_SERIAL_THREAD
);
81 #endif /* HAVE_PTHREAD */
83 pa_run_once(&once
, once_func
);
86 START_TEST (once_test
) {
91 for (n
= 0; n
< N_ITERATIONS
; n
++) {
92 pa_thread
* threads
[N_THREADS
];
95 fail_unless(pthread_barrier_init(&barrier
, NULL
, N_THREADS
) == 0);
101 for (i
= 0; i
< N_THREADS
; i
++)
102 threads
[i
] = pa_thread_new("once", thread_func
, pa_sprintf_malloc("Thread #%i", i
+1));
104 for (i
= 0; i
< N_THREADS
; i
++)
105 pa_thread_join(threads
[i
]);
107 fail_unless(n_run
== 1);
108 pa_log_info("ran by %s", ran_by
);
110 for (i
= 0; i
< N_THREADS
; i
++) {
111 pa_xfree(pa_thread_get_data(threads
[i
]));
112 pa_thread_free(threads
[i
]);
119 fail_unless(pthread_barrier_destroy(&barrier
) == 0);
125 int main(int argc
, char *argv
[]) {
131 if (!getenv("MAKE_CHECK"))
132 pa_log_set_level(PA_LOG_DEBUG
);
134 s
= suite_create("Once");
135 tc
= tcase_create("once");
136 tcase_add_test(tc
, once_test
);
137 /* the default timeout is too small,
138 * set it to a reasonable large one.
140 tcase_set_timeout(tc
, 60 * 60);
141 suite_add_tcase(s
, tc
);
143 sr
= srunner_create(s
);
144 srunner_run_all(sr
, CK_NORMAL
);
145 failed
= srunner_ntests_failed(sr
);
148 return (failed
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;