• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, see <http://www.gnu.org/licenses/>.
16 ***/
17 
18 #ifdef HAVE_CONFIG_H
19 #include <config.h>
20 #endif
21 
22 #ifdef HAVE_PTHREAD
23 #include <pthread.h>
24 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
25 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
26 #ifdef __FreeBSD__
27 #include <pthread_np.h>
28 #endif
29 #include <sys/param.h>
30 #include <sys/cpuset.h>
31 #endif
32 #endif
33 #endif
34 
35 #include <check.h>
36 
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>
43 
44 static pa_once once = PA_ONCE_INIT;
45 static volatile unsigned n_run = 0;
46 static const char * volatile ran_by = NULL;
47 #ifdef HAVE_PTHREAD
48 static pthread_barrier_t barrier;
49 #endif
50 static unsigned n_cpu;
51 
52 #define N_ITERATIONS 500
53 #define N_THREADS 100
54 
once_func(void)55 static void once_func(void) {
56     n_run++;
57     ran_by = (const char*) pa_thread_get_data(pa_thread_self());
58 }
59 
thread_func(void * data)60 static void thread_func(void *data) {
61 #ifdef HAVE_PTHREAD
62     int r;
63 
64 #ifdef HAVE_PTHREAD_SETAFFINITY_NP
65     static pa_atomic_t i_cpu = PA_ATOMIC_INIT(0);
66 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
67     cpuset_t mask;
68 #else
69     cpu_set_t mask;
70 #endif
71 
72     CPU_ZERO(&mask);
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);
75 #endif
76 
77     pa_log_debug("started up: %s", (char *) data);
78 
79     r = pthread_barrier_wait(&barrier);
80     fail_unless(r == 0 || r == PTHREAD_BARRIER_SERIAL_THREAD);
81 #endif /* HAVE_PTHREAD */
82 
83     pa_run_once(&once, once_func);
84 }
85 
START_TEST(once_test)86 START_TEST (once_test) {
87     unsigned n, i;
88 
89     n_cpu = pa_ncpus();
90 
91     for (n = 0; n < N_ITERATIONS; n++) {
92         pa_thread* threads[N_THREADS];
93 
94 #ifdef HAVE_PTHREAD
95         fail_unless(pthread_barrier_init(&barrier, NULL, N_THREADS) == 0);
96 #endif
97 
98         /* Yes, kinda ugly */
99         pa_zero(once);
100 
101         for (i = 0; i < N_THREADS; i++)
102             threads[i] = pa_thread_new("once", thread_func, pa_sprintf_malloc("Thread #%i", i+1));
103 
104         for (i = 0; i < N_THREADS; i++)
105             pa_thread_join(threads[i]);
106 
107         fail_unless(n_run == 1);
108         pa_log_info("ran by %s", ran_by);
109 
110         for (i = 0; i < N_THREADS; i++) {
111             pa_xfree(pa_thread_get_data(threads[i]));
112             pa_thread_free(threads[i]);
113         }
114 
115         n_run = 0;
116         ran_by = NULL;
117 
118 #ifdef HAVE_PTHREAD
119         fail_unless(pthread_barrier_destroy(&barrier) == 0);
120 #endif
121     }
122 }
123 END_TEST
124 
main(int argc,char * argv[])125 int main(int argc, char *argv[]) {
126     int failed = 0;
127     Suite *s;
128     TCase *tc;
129     SRunner *sr;
130 
131     if (!getenv("MAKE_CHECK"))
132         pa_log_set_level(PA_LOG_DEBUG);
133 
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.
139      */
140     tcase_set_timeout(tc, 60 * 60);
141     suite_add_tcase(s, tc);
142 
143     sr = srunner_create(s);
144     srunner_run_all(sr, CK_NORMAL);
145     failed = srunner_ntests_failed(sr);
146     srunner_free(sr);
147 
148     return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
149 }
150