From owner-svn-src-head@FreeBSD.ORG Sat Jun 8 22:44:50 2013 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.FreeBSD.org [8.8.178.115]) by hub.freebsd.org (Postfix) with ESMTP id 2FA15D96; Sat, 8 Jun 2013 22:44:49 +0000 (UTC) (envelope-from ed@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) by mx1.freebsd.org (Postfix) with ESMTP id D390817E8; Sat, 8 Jun 2013 22:44:49 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.7/8.14.7) with ESMTP id r58MinLm042824; Sat, 8 Jun 2013 22:44:49 GMT (envelope-from ed@svn.freebsd.org) Received: (from ed@localhost) by svn.freebsd.org (8.14.7/8.14.5/Submit) id r58MinML042822; Sat, 8 Jun 2013 22:44:49 GMT (envelope-from ed@svn.freebsd.org) Message-Id: <201306082244.r58MinML042822@svn.freebsd.org> From: Ed Schouten Date: Sat, 8 Jun 2013 22:44:49 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r251558 - head/tools/regression/include/stdatomic X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 08 Jun 2013 22:44:50 -0000 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 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 + * 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include + +/* + * 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); +}