1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2014 David Henningsson, Canonical Ltd.
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 /* This test spawns two threads on distinct cpu-cores that pass a value
21 * between each other through shared memory protected by pa_atomic_t.
22 * Thread "left" continuously increments a value and writes its contents to memory.
23 * Thread "right" continuously reads the value and checks whether it was incremented.
24 *
25 * With the pa_atomic_load/pa_atomic_store implementations based on __sync_synchronize,
26 * this will fail after some time (sometimes 2 seconds, sometimes 8 hours) at least
27 * on ARM Cortex-A53 and ARM Cortex-A57 systems.
28 *
29 * On x86_64, it does not.
30 *
31 * The chosen implementation in some way mimics a situation that can also occur
32 * using memfd srbchannel transport.
33 *
34 * NOTE: This is a long-running test, so don't execute in normal test suite.
35 *
36 * */
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #include <unistd.h>
43 #include <check.h>
44
45 #include <pulsecore/thread.h>
46 #include <pulse/rtclock.h>
47 #include <pulse/xmalloc.h>
48 #include <pulsecore/semaphore.h>
49 #include <pthread.h>
50 #include <pulsecore/atomic.h>
51
52 #define MEMORY_SIZE (8 * 2 * 1024 * 1024)
53
54
55 typedef struct io_t {
56 pa_atomic_t *flag;
57 char* memory;
58 cpu_set_t cpuset;
59 } io_t;
60
read_func(void * data)61 static void read_func(void* data) {
62 io_t *io = (io_t *) data;
63 size_t expect = 0;
64 size_t value = 0;
65 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &io->cpuset);
66 while(1) {
67 if(pa_atomic_load(io->flag) == 1) {
68 memcpy(&value, io->memory, sizeof(value));
69 pa_atomic_sub(io->flag, 1);
70 ck_assert_uint_eq(value, expect);
71 ++expect;
72 }
73 }
74 }
75
write_func(void * data)76 static void write_func(void* data) {
77 io_t *io = (io_t *) data;
78 size_t value = 0;
79 pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &io->cpuset);
80 while(1) {
81 if(pa_atomic_load(io->flag) == 0) {
82 memcpy(io->memory, &value, sizeof(value));
83 pa_atomic_add(io->flag, 1);
84 ++value;
85 }
86 }
87 }
88
START_TEST(atomic_test)89 START_TEST (atomic_test) {
90 pa_thread *thread1, *thread2;
91 io_t io1, io2;
92
93 char* memory = pa_xmalloc0(MEMORY_SIZE);
94 pa_atomic_t flag = PA_ATOMIC_INIT(0);
95 memset(memory, 0, MEMORY_SIZE);
96
97 /* intentionally misalign memory since srbchannel also does not
98 * always read/write aligned. Might be a red hering. */
99 io1.memory = io2.memory = memory + 1025;
100 io1.flag = io2.flag = &flag;
101
102 CPU_ZERO(&io1.cpuset);
103 CPU_SET(1, &io1.cpuset);
104 thread1 = pa_thread_new("left", &write_func, &io1);
105
106 CPU_ZERO(&io2.cpuset);
107 CPU_SET(3, &io2.cpuset);
108 thread2 = pa_thread_new("right", &read_func, &io2);
109 pa_thread_free(thread1);
110 pa_thread_free(thread2);
111 pa_xfree(memory);
112 }
113 END_TEST
114
main(int argc,char * argv[])115 int main(int argc, char *argv[]) {
116 int failed = 0;
117 Suite *s;
118 TCase *tc;
119 SRunner *sr;
120
121 if (!getenv("MAKE_CHECK"))
122 pa_log_set_level(PA_LOG_DEBUG);
123
124 s = suite_create("atomic");
125 tc = tcase_create("atomic");
126 tcase_add_test(tc, atomic_test);
127 suite_add_tcase(s, tc);
128
129 sr = srunner_create(s);
130 srunner_run_all(sr, CK_NORMAL);
131 failed = srunner_ntests_failed(sr);
132 srunner_free(sr);
133
134 return (failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
135 }
136