Home | History | Annotate | Line # | Download | only in membar
t_dekker.c revision 1.1
      1  1.1  riastrad /*	$NetBSD: t_dekker.c,v 1.1 2022/04/08 23:35:52 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.1  riastrad __RCSID("$NetBSD: t_dekker.c,v 1.1 2022/04/08 23:35:52 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 <signal.h>
     43  1.1  riastrad #include <stdint.h>
     44  1.1  riastrad #include <stdio.h>
     45  1.1  riastrad #include <unistd.h>
     46  1.1  riastrad 
     47  1.1  riastrad /* XXX */
     48  1.1  riastrad #define	membar_acquire()	membar_enter()
     49  1.1  riastrad #define	membar_release()	membar_exit()
     50  1.1  riastrad 
     51  1.1  riastrad #ifdef	BROKEN_SYNC
     52  1.1  riastrad #undef	membar_sync
     53  1.1  riastrad #define	membar_sync()	asm volatile("" ::: "memory")
     54  1.1  riastrad #endif	/* BROKEN_SYNC */
     55  1.1  riastrad 
     56  1.1  riastrad volatile sig_atomic_t times_up;
     57  1.1  riastrad 
     58  1.1  riastrad volatile unsigned turn __aligned(COHERENCY_UNIT);
     59  1.1  riastrad volatile struct {
     60  1.1  riastrad 	unsigned v;
     61  1.1  riastrad } __aligned(COHERENCY_UNIT) waiting[2];
     62  1.1  riastrad __CTASSERT(sizeof(waiting) == 2*COHERENCY_UNIT);
     63  1.1  riastrad 
     64  1.1  riastrad volatile uint64_t C;
     65  1.1  riastrad uint64_t TC[2];
     66  1.1  riastrad 
     67  1.1  riastrad static void
     68  1.1  riastrad lock(unsigned me)
     69  1.1  riastrad {
     70  1.1  riastrad 
     71  1.1  riastrad top:	waiting[me].v = 1;
     72  1.1  riastrad 	membar_sync();
     73  1.1  riastrad 	while (waiting[1 - me].v) {
     74  1.1  riastrad 		if (turn != me) {
     75  1.1  riastrad 			waiting[me].v = 0;
     76  1.1  riastrad 			while (turn != me)
     77  1.1  riastrad 				continue;
     78  1.1  riastrad 			goto top;
     79  1.1  riastrad 		}
     80  1.1  riastrad 	}
     81  1.1  riastrad 	membar_acquire();
     82  1.1  riastrad }
     83  1.1  riastrad 
     84  1.1  riastrad static void
     85  1.1  riastrad unlock(unsigned me)
     86  1.1  riastrad {
     87  1.1  riastrad 
     88  1.1  riastrad 	membar_release();
     89  1.1  riastrad 	turn = 1 - me;
     90  1.1  riastrad 	waiting[me].v = 0;
     91  1.1  riastrad 
     92  1.1  riastrad 	/*
     93  1.1  riastrad 	 * Not needed for correctness, but this helps on Cavium Octeon
     94  1.1  riastrad 	 * cnMIPS CPUs which require issuing a sync plunger to unclog
     95  1.1  riastrad 	 * store buffers which can otherwise stay clogged for hundreds
     96  1.1  riastrad 	 * of thousands of cycles, giving very little concurrency to
     97  1.1  riastrad 	 * this test.
     98  1.1  riastrad 	 */
     99  1.1  riastrad 	membar_producer();
    100  1.1  riastrad }
    101  1.1  riastrad 
    102  1.1  riastrad static void
    103  1.1  riastrad alarm_handler(int signo)
    104  1.1  riastrad {
    105  1.1  riastrad 
    106  1.1  riastrad 	(void)signo;
    107  1.1  riastrad 	times_up = 1;
    108  1.1  riastrad }
    109  1.1  riastrad 
    110  1.1  riastrad static void *
    111  1.1  riastrad thread(void *cookie)
    112  1.1  riastrad {
    113  1.1  riastrad 	unsigned me = (unsigned)(uintptr_t)cookie;
    114  1.1  riastrad 	uint64_t C_local = 0;
    115  1.1  riastrad 
    116  1.1  riastrad 	while (!times_up) {
    117  1.1  riastrad 		C_local++;
    118  1.1  riastrad 		lock(me);
    119  1.1  riastrad 		C++;
    120  1.1  riastrad 		unlock(me);
    121  1.1  riastrad 	}
    122  1.1  riastrad 
    123  1.1  riastrad 	TC[me] = C_local;
    124  1.1  riastrad 
    125  1.1  riastrad 	return NULL;
    126  1.1  riastrad }
    127  1.1  riastrad 
    128  1.1  riastrad ATF_TC(dekker);
    129  1.1  riastrad ATF_TC_HEAD(dekker, tc)
    130  1.1  riastrad {
    131  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
    132  1.1  riastrad 	    "Verify membar_sync works for Dekker's algorithm");
    133  1.1  riastrad }
    134  1.1  riastrad ATF_TC_BODY(dekker, tc)
    135  1.1  riastrad {
    136  1.1  riastrad 	pthread_t t[2];
    137  1.1  riastrad 	unsigned i;
    138  1.1  riastrad 	int ncpu;
    139  1.1  riastrad 	size_t ncpulen = sizeof(ncpu);
    140  1.1  riastrad 	int error;
    141  1.1  riastrad 
    142  1.1  riastrad 	if (sysctlbyname("hw.ncpu", &ncpu, &ncpulen, NULL, 0) == -1)
    143  1.1  riastrad 		atf_tc_fail("hw.ncpu: (%d) %s", errno, strerror(errno));
    144  1.1  riastrad 	assert(ncpulen == sizeof(ncpu));
    145  1.1  riastrad 	if (ncpu == 1)
    146  1.1  riastrad 		atf_tc_skip("membar tests are only for multicore systems");
    147  1.1  riastrad 
    148  1.1  riastrad 	if (signal(SIGALRM, alarm_handler) == SIG_ERR)
    149  1.1  riastrad 		err(1, "signal(SIGALRM");
    150  1.1  riastrad 	alarm(5);
    151  1.1  riastrad 	for (i = 0; i < 2; i++) {
    152  1.1  riastrad 		error = pthread_create(&t[i], NULL, &thread,
    153  1.1  riastrad 		    (void *)(uintptr_t)i);
    154  1.1  riastrad 		if (error)
    155  1.1  riastrad 			errc(1, error, "pthread_create");
    156  1.1  riastrad 	}
    157  1.1  riastrad 	for (i = 0; i < 2; i++) {
    158  1.1  riastrad 		error = pthread_join(t[i], NULL);
    159  1.1  riastrad 		if (error)
    160  1.1  riastrad 			errc(1, error, "pthread_join");
    161  1.1  riastrad 	}
    162  1.1  riastrad 	ATF_REQUIRE_MSG(C == TC[0] + TC[1],
    163  1.1  riastrad 	    "%"PRIu64" != %"PRIu64" + %"PRIu64" (off by %"PRIdMAX")",
    164  1.1  riastrad 	    C, TC[0], TC[1], TC[0] + TC[1] - C);
    165  1.1  riastrad }
    166  1.1  riastrad 
    167  1.1  riastrad ATF_TP_ADD_TCS(tp)
    168  1.1  riastrad {
    169  1.1  riastrad 
    170  1.1  riastrad 	ATF_TP_ADD_TC(tp, dekker);
    171  1.1  riastrad 	return atf_no_error();
    172  1.1  riastrad }
    173