svn commit: r251558 - head/tools/regression/include/stdatomic

Ed Schouten ed at FreeBSD.org
Sat Jun 8 22:44:50 UTC 2013


Author: ed
Date: Sat Jun  8 22:44:49 2013
New Revision: 251558
URL: http://svnweb.freebsd.org/changeset/base/251558

Log:
  Add testing utility for behavior of atomic ops.
  
  This small utility performs a sequence of atomic operations with random
  parameters on an atomic variable. For every type, we also create 16
  variables, to ensure that we test the correctness at different
  alignments.

Added:
  head/tools/regression/include/stdatomic/
  head/tools/regression/include/stdatomic/Makefile   (contents, props changed)
  head/tools/regression/include/stdatomic/logic.c   (contents, props changed)

Added: head/tools/regression/include/stdatomic/Makefile
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/tools/regression/include/stdatomic/Makefile	Sat Jun  8 22:44:49 2013	(r251558)
@@ -0,0 +1,7 @@
+# $FreeBSD$
+
+PROG=	logic
+WARNS=6
+NO_MAN=
+
+.include <bsd.prog.mk>

Added: head/tools/regression/include/stdatomic/logic.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/tools/regression/include/stdatomic/logic.c	Sat Jun  8 22:44:49 2013	(r251558)
@@ -0,0 +1,128 @@
+/*-
+ * Copyright (c) 2013 Ed Schouten <ed at FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <assert.h>
+#include <stdatomic.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+/*
+ * Tool for testing the logical behaviour of operations on atomic
+ * integer types. These tests make no attempt to actually test whether
+ * the functions are atomic or provide the right barrier semantics.
+ *
+ * For every type, we create an array of 16 elements and repeat the test
+ * on every element in the array. This allows us to test whether the
+ * atomic operations have no effect on surrounding values. This is
+ * especially useful for the smaller integer types, as it may be the
+ * case that these operations are implemented by processing entire words
+ * (e.g. on MIPS).
+ */
+
+static inline intmax_t
+rndnum(void)
+{
+	intmax_t v;
+
+	arc4random_buf(&v, sizeof(v));
+	return (v);
+}
+
+#define	DO_FETCH_TEST(T, a, name, result) do {				\
+	T v1 = atomic_load(a);						\
+	T v2 = rndnum();						\
+	assert(atomic_##name(a, v2) == v1); 				\
+	assert(atomic_load(a) == (T)(result)); 				\
+} while (0)
+
+#define	DO_COMPARE_EXCHANGE_TEST(T, a, name) do {			\
+	T v1 = atomic_load(a);						\
+	T v2 = rndnum();						\
+	T v3 = rndnum();						\
+	if (atomic_compare_exchange_##name(a, &v2, v3))			\
+		assert(v1 == v2);					\
+	else								\
+		assert(atomic_compare_exchange_##name(a, &v2, v3));	\
+	assert(atomic_load(a) == v3);					\
+} while (0)
+
+#define	DO_ALL_TESTS(T, a) do {						\
+	{								\
+		T v1 = rndnum();					\
+		atomic_init(a, v1);					\
+		assert(atomic_load(a) == v1);				\
+	}								\
+	{								\
+		T v1 = rndnum();					\
+		atomic_store(a, v1);					\
+		assert(atomic_load(a) == v1);				\
+	}								\
+									\
+	DO_FETCH_TEST(T, a, exchange, v2);				\
+	DO_FETCH_TEST(T, a, fetch_add, v1 + v2);			\
+	DO_FETCH_TEST(T, a, fetch_and, v1 & v2);			\
+	DO_FETCH_TEST(T, a, fetch_or, v1 | v2);				\
+	DO_FETCH_TEST(T, a, fetch_sub, v1 - v2);			\
+	DO_FETCH_TEST(T, a, fetch_xor, v1 ^ v2);			\
+									\
+	DO_COMPARE_EXCHANGE_TEST(T, a, weak);				\
+	DO_COMPARE_EXCHANGE_TEST(T, a, strong);				\
+} while (0)
+
+#define	TEST_TYPE(T) do {						\
+	int j;								\
+	struct { _Atomic(T) v[16]; } list, cmp;				\
+	arc4random_buf(&cmp, sizeof(cmp));				\
+	for (j = 0; j < 16; j++) {					\
+		list = cmp;						\
+		DO_ALL_TESTS(T, &list.v[j]);				\
+		list.v[j] = cmp.v[j];					\
+		assert(memcmp(&list, &cmp, sizeof(list)) == 0);		\
+	}								\
+} while (0)
+
+int
+main(void)
+{
+	int i;
+
+	for (i = 0; i < 1000; i++) {
+		TEST_TYPE(int8_t);
+		TEST_TYPE(uint8_t);
+		TEST_TYPE(int16_t);
+		TEST_TYPE(uint16_t);
+		TEST_TYPE(int32_t);
+		TEST_TYPE(uint32_t);
+		TEST_TYPE(int64_t);
+		TEST_TYPE(uint64_t);
+	}
+
+	return (0);
+}


More information about the svn-src-all mailing list