xref: /netbsd-src/tests/lib/libc/membar/t_dekker.c (revision c42dbd0ed2e61fe6eda8590caa852ccf34719964)
1 /*	$NetBSD: t_dekker.c,v 1.3 2022/04/10 11:36:32 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2022 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: t_dekker.c,v 1.3 2022/04/10 11:36:32 riastradh Exp $");
31 
32 #include <sys/atomic.h>
33 #include <sys/param.h>
34 #include <sys/sysctl.h>
35 
36 #include <assert.h>
37 #include <atf-c.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <inttypes.h>
41 #include <pthread.h>
42 #include <stdint.h>
43 #include <stdio.h>
44 #include <unistd.h>
45 
46 #ifdef	BROKEN_SYNC
47 #undef	membar_sync
48 #define	membar_sync()	asm volatile("" ::: "memory")
49 #endif	/* BROKEN_SYNC */
50 
51 volatile sig_atomic_t times_up;
52 
53 volatile unsigned turn __aligned(COHERENCY_UNIT);
54 volatile struct {
55 	unsigned v;
56 } __aligned(COHERENCY_UNIT) waiting[2];
57 __CTASSERT(sizeof(waiting) == 2*COHERENCY_UNIT);
58 
59 volatile uint64_t C;
60 uint64_t TC[2];
61 
62 static void
63 lock(unsigned me)
64 {
65 
66 top:	waiting[me].v = 1;
67 	membar_sync();
68 	while (waiting[1 - me].v) {
69 		if (turn != me) {
70 			waiting[me].v = 0;
71 			while (turn != me)
72 				continue;
73 			goto top;
74 		}
75 	}
76 	membar_acquire();
77 }
78 
79 static void
80 unlock(unsigned me)
81 {
82 
83 	membar_release();
84 	turn = 1 - me;
85 	waiting[me].v = 0;
86 
87 	/*
88 	 * Not needed for correctness, but this helps on Cavium Octeon
89 	 * cnMIPS CPUs which require issuing a sync plunger to unclog
90 	 * store buffers which can otherwise stay clogged for hundreds
91 	 * of thousands of cycles, giving very little concurrency to
92 	 * this test.
93 	 */
94 	membar_producer();
95 }
96 
97 static void *
98 thread(void *cookie)
99 {
100 	unsigned me = (unsigned)(uintptr_t)cookie;
101 	uint64_t C_local = 0;
102 
103 	while (!times_up) {
104 		C_local++;
105 		lock(me);
106 		C++;
107 		unlock(me);
108 	}
109 
110 	TC[me] = C_local;
111 
112 	return NULL;
113 }
114 
115 ATF_TC(dekker);
116 ATF_TC_HEAD(dekker, tc)
117 {
118 	atf_tc_set_md_var(tc, "descr",
119 	    "Verify membar_sync works for Dekker's algorithm");
120 }
121 ATF_TC_BODY(dekker, tc)
122 {
123 	pthread_t t[2];
124 	unsigned i;
125 	int ncpu;
126 	size_t ncpulen = sizeof(ncpu);
127 	int error;
128 
129 	alarm(10);
130 
131 	if (sysctlbyname("hw.ncpu", &ncpu, &ncpulen, NULL, 0) == -1)
132 		atf_tc_fail("hw.ncpu: (%d) %s", errno, strerror(errno));
133 	assert(ncpulen == sizeof(ncpu));
134 	if (ncpu == 1)
135 		atf_tc_skip("membar tests are only for multicore systems");
136 
137 	for (i = 0; i < 2; i++) {
138 		error = pthread_create(&t[i], NULL, &thread,
139 		    (void *)(uintptr_t)i);
140 		if (error)
141 			errc(1, error, "pthread_create");
142 	}
143 	sleep(5);
144 	times_up = 1;
145 	for (i = 0; i < 2; i++) {
146 		error = pthread_join(t[i], NULL);
147 		if (error)
148 			errc(1, error, "pthread_join");
149 	}
150 	ATF_REQUIRE_MSG(C == TC[0] + TC[1],
151 	    "%"PRIu64" != %"PRIu64" + %"PRIu64" (off by %"PRIdMAX")",
152 	    C, TC[0], TC[1], TC[0] + TC[1] - C);
153 }
154 
155 ATF_TP_ADD_TCS(tp)
156 {
157 
158 	ATF_TP_ADD_TC(tp, dekker);
159 	return atf_no_error();
160 }
161