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