Home | History | Annotate | Line # | Download | only in membar
t_spinlock.c revision 1.1
      1  1.1  riastrad /*	$NetBSD: t_spinlock.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_spinlock.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_ACQUIRE
     52  1.1  riastrad #undef	membar_acquire
     53  1.1  riastrad #define	membar_acquire()	asm volatile("" ::: "memory")
     54  1.1  riastrad #endif	/* BROKEN_ACQUIRE */
     55  1.1  riastrad 
     56  1.1  riastrad #ifdef	BROKEN_RELEASE
     57  1.1  riastrad #undef	membar_release
     58  1.1  riastrad #define	membar_release()	asm volatile("" ::: "memory")
     59  1.1  riastrad #endif	/* BROKEN_RELEASE */
     60  1.1  riastrad 
     61  1.1  riastrad volatile sig_atomic_t times_up;
     62  1.1  riastrad 
     63  1.1  riastrad volatile unsigned lockbit __aligned(COHERENCY_UNIT);
     64  1.1  riastrad 
     65  1.1  riastrad volatile struct {
     66  1.1  riastrad 	uint64_t v;
     67  1.1  riastrad } __aligned(COHERENCY_UNIT) C[8];
     68  1.1  riastrad uint64_t TC[2];
     69  1.1  riastrad 
     70  1.1  riastrad static void
     71  1.1  riastrad lock(void)
     72  1.1  riastrad {
     73  1.1  riastrad 
     74  1.1  riastrad 	while (atomic_swap_uint(&lockbit, 1))
     75  1.1  riastrad 		continue;
     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(void)
     81  1.1  riastrad {
     82  1.1  riastrad 
     83  1.1  riastrad 	membar_release();
     84  1.1  riastrad 	lockbit = 0;
     85  1.1  riastrad }
     86  1.1  riastrad 
     87  1.1  riastrad static void
     88  1.1  riastrad alarm_handler(int signo)
     89  1.1  riastrad {
     90  1.1  riastrad 
     91  1.1  riastrad 	(void)signo;
     92  1.1  riastrad 	times_up = 1;
     93  1.1  riastrad }
     94  1.1  riastrad 
     95  1.1  riastrad static void *
     96  1.1  riastrad thread(void *cookie)
     97  1.1  riastrad {
     98  1.1  riastrad 	unsigned me = (unsigned)(uintptr_t)cookie;
     99  1.1  riastrad 	uint64_t C_local = 0, C0[__arraycount(C)];
    100  1.1  riastrad 	unsigned i;
    101  1.1  riastrad 
    102  1.1  riastrad 	while (!times_up) {
    103  1.1  riastrad 		C_local++;
    104  1.1  riastrad 		lock();
    105  1.1  riastrad 		for (i = 0; i < __arraycount(C); i++)
    106  1.1  riastrad 			C0[i] = C[i].v;
    107  1.1  riastrad 		__insn_barrier();
    108  1.1  riastrad 		for (i = __arraycount(C); i --> 0;)
    109  1.1  riastrad 			C[i].v = C0[i] + 1;
    110  1.1  riastrad 		unlock();
    111  1.1  riastrad 	}
    112  1.1  riastrad 
    113  1.1  riastrad 	TC[me] = C_local;
    114  1.1  riastrad 
    115  1.1  riastrad 	return NULL;
    116  1.1  riastrad }
    117  1.1  riastrad 
    118  1.1  riastrad ATF_TC(spinlock);
    119  1.1  riastrad ATF_TC_HEAD(spinlock, tc)
    120  1.1  riastrad {
    121  1.1  riastrad 	atf_tc_set_md_var(tc, "descr",
    122  1.1  riastrad 	    "Verify membar_acquire/release work for spin locks");
    123  1.1  riastrad }
    124  1.1  riastrad ATF_TC_BODY(spinlock, tc)
    125  1.1  riastrad {
    126  1.1  riastrad 	pthread_t t[2];
    127  1.1  riastrad 	unsigned i;
    128  1.1  riastrad 	int ncpu;
    129  1.1  riastrad 	size_t ncpulen = sizeof(ncpu);
    130  1.1  riastrad 	int error;
    131  1.1  riastrad 
    132  1.1  riastrad 	if (sysctlbyname("hw.ncpu", &ncpu, &ncpulen, NULL, 0) == -1)
    133  1.1  riastrad 		atf_tc_fail("hw.ncpu: (%d) %s", errno, strerror(errno));
    134  1.1  riastrad 	assert(ncpulen == sizeof(ncpu));
    135  1.1  riastrad 	if (ncpu == 1)
    136  1.1  riastrad 		atf_tc_skip("membar tests are only for multicore systems");
    137  1.1  riastrad 
    138  1.1  riastrad 	if (signal(SIGALRM, alarm_handler) == SIG_ERR)
    139  1.1  riastrad 		err(1, "signal(SIGALRM");
    140  1.1  riastrad 	alarm(5);
    141  1.1  riastrad 	for (i = 0; i < 2; i++) {
    142  1.1  riastrad 		error = pthread_create(&t[i], NULL, &thread,
    143  1.1  riastrad 		    (void *)(uintptr_t)i);
    144  1.1  riastrad 		if (error)
    145  1.1  riastrad 			errc(1, error, "pthread_create");
    146  1.1  riastrad 	}
    147  1.1  riastrad 	for (i = 0; i < 2; i++) {
    148  1.1  riastrad 		error = pthread_join(t[i], NULL);
    149  1.1  riastrad 		if (error)
    150  1.1  riastrad 			errc(1, error, "pthread_join");
    151  1.1  riastrad 	}
    152  1.1  riastrad 	for (i = 0; i < __arraycount(C); i++) {
    153  1.1  riastrad 		ATF_CHECK_MSG(C[i].v == TC[0] + TC[1], "%d: "
    154  1.1  riastrad 		    "%"PRIu64" != %"PRIu64" + %"PRIu64" (off by %"PRIdMAX")",
    155  1.1  riastrad 		    i, C[i].v, TC[0], TC[1], TC[0] + TC[1] - C[i].v);
    156  1.1  riastrad 	}
    157  1.1  riastrad }
    158  1.1  riastrad 
    159  1.1  riastrad ATF_TP_ADD_TCS(tp)
    160  1.1  riastrad {
    161  1.1  riastrad 
    162  1.1  riastrad 	ATF_TP_ADD_TC(tp, spinlock);
    163  1.1  riastrad 	return atf_no_error();
    164  1.1  riastrad }
    165