xref: /netbsd-src/tests/lib/libc/membar/t_spinlock.c (revision afab4e300d3a9fb07dd8c80daf53d0feb3345706)
1 /*	$NetBSD: t_spinlock.c,v 1.4 2022/08/12 11:21:44 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_spinlock.c,v 1.4 2022/08/12 11:21:44 riastradh Exp $");
31 
32 #include <sys/types.h>
33 
34 #include <sys/atomic.h>
35 #include <sys/param.h>
36 #include <sys/sysctl.h>
37 
38 #include <assert.h>
39 #include <atf-c.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <inttypes.h>
43 #include <pthread.h>
44 #include <stdint.h>
45 #include <stdio.h>
46 #include <unistd.h>
47 
48 #ifdef	BROKEN_ACQUIRE
49 #undef	membar_acquire
50 #define	membar_acquire()	asm volatile("" ::: "memory")
51 #endif	/* BROKEN_ACQUIRE */
52 
53 #ifdef	BROKEN_RELEASE
54 #undef	membar_release
55 #define	membar_release()	asm volatile("" ::: "memory")
56 #endif	/* BROKEN_RELEASE */
57 
58 volatile sig_atomic_t times_up;
59 
60 volatile unsigned lockbit __aligned(COHERENCY_UNIT);
61 
62 volatile struct {
63 	uint64_t v;
64 } __aligned(COHERENCY_UNIT) C[8];
65 uint64_t TC[2];
66 
67 static void
68 lock(void)
69 {
70 
71 	while (atomic_swap_uint(&lockbit, 1))
72 		continue;
73 	membar_acquire();
74 }
75 
76 static void
77 unlock(void)
78 {
79 
80 	membar_release();
81 #ifdef __HAVE_HASHLOCKED_ATOMICS
82 	(void)atomic_cas_uint(&lockbit, 1, 0);
83 #else
84 	lockbit = 0;
85 #endif
86 }
87 
88 static void *
89 thread(void *cookie)
90 {
91 	unsigned me = (unsigned)(uintptr_t)cookie;
92 	uint64_t C_local = 0, C0[__arraycount(C)];
93 	unsigned i;
94 
95 	while (!times_up) {
96 		C_local++;
97 		lock();
98 		for (i = 0; i < __arraycount(C); i++)
99 			C0[i] = C[i].v;
100 		__insn_barrier();
101 		for (i = __arraycount(C); i --> 0;)
102 			C[i].v = C0[i] + 1;
103 		unlock();
104 	}
105 
106 	TC[me] = C_local;
107 
108 	return NULL;
109 }
110 
111 ATF_TC(spinlock);
112 ATF_TC_HEAD(spinlock, tc)
113 {
114 	atf_tc_set_md_var(tc, "descr",
115 	    "Verify membar_acquire/release work for spin locks");
116 }
117 ATF_TC_BODY(spinlock, tc)
118 {
119 	pthread_t t[2];
120 	unsigned i;
121 	int ncpu;
122 	size_t ncpulen = sizeof(ncpu);
123 	int error;
124 
125 	alarm(10);
126 
127 	if (sysctlbyname("hw.ncpu", &ncpu, &ncpulen, NULL, 0) == -1)
128 		atf_tc_fail("hw.ncpu: (%d) %s", errno, strerror(errno));
129 	assert(ncpulen == sizeof(ncpu));
130 	if (ncpu == 1)
131 		atf_tc_skip("membar tests are only for multicore systems");
132 
133 	for (i = 0; i < 2; i++) {
134 		error = pthread_create(&t[i], NULL, &thread,
135 		    (void *)(uintptr_t)i);
136 		if (error)
137 			errc(1, error, "pthread_create");
138 	}
139 	sleep(5);
140 	times_up = 1;
141 	for (i = 0; i < 2; i++) {
142 		error = pthread_join(t[i], NULL);
143 		if (error)
144 			errc(1, error, "pthread_join");
145 	}
146 	for (i = 0; i < __arraycount(C); i++) {
147 		ATF_CHECK_MSG(C[i].v == TC[0] + TC[1], "%d: "
148 		    "%"PRIu64" != %"PRIu64" + %"PRIu64" (off by %"PRIdMAX")",
149 		    i, C[i].v, TC[0], TC[1], TC[0] + TC[1] - C[i].v);
150 	}
151 }
152 
153 ATF_TP_ADD_TCS(tp)
154 {
155 
156 	ATF_TP_ADD_TC(tp, spinlock);
157 	return atf_no_error();
158 }
159