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
28 #include <pulsecore/thread.h>
29 #include <pulsecore/once.h>
30 #include <pulsecore/log.h>
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/atomic.h>
33 #include <pulse/xmalloc.h>
35 static pa_once once
= PA_ONCE_INIT
;
36 static volatile unsigned n_run
= 0;
37 static const char * volatile ran_by
= NULL
;
39 static pthread_barrier_t barrier
;
41 static unsigned n_cpu
;
43 #define N_ITERATIONS 500
46 static void once_func(void) {
48 ran_by
= (const char*) pa_thread_get_data(pa_thread_self());
51 static void thread_func(void *data
) {
55 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
56 static pa_atomic_t i_cpu
= PA_ATOMIC_INIT(0);
60 CPU_SET((size_t) (pa_atomic_inc(&i_cpu
) % n_cpu
), &mask
);
61 pa_assert_se(pthread_setaffinity_np(pthread_self(), sizeof(mask
), &mask
) == 0);
64 /* pa_log("started up: %s", data); */
66 r
= pthread_barrier_wait(&barrier
);
67 pa_assert(r
== 0 || r
== PTHREAD_BARRIER_SERIAL_THREAD
);
68 #endif /* HAVE_PTHREAD */
70 pa_run_once(&once
, once_func
);
73 int main(int argc
, char *argv
[]) {
78 for (n
= 0; n
< N_ITERATIONS
; n
++) {
79 pa_thread
* threads
[N_THREADS
];
82 pa_assert_se(pthread_barrier_init(&barrier
, NULL
, N_THREADS
) == 0);
88 for (i
= 0; i
< N_THREADS
; i
++)
89 threads
[i
] = pa_thread_new("once", thread_func
, pa_sprintf_malloc("Thread #%i", i
+1));
91 for (i
= 0; i
< N_THREADS
; i
++)
92 pa_thread_join(threads
[i
]);
94 pa_assert(n_run
== 1);
95 pa_log("ran by %s", ran_by
);
97 for (i
= 0; i
< N_THREADS
; i
++) {
98 pa_xfree(pa_thread_get_data(threads
[i
]));
99 pa_thread_free(threads
[i
]);
106 pa_assert_se(pthread_barrier_destroy(&barrier
) == 0);