From patchwork Mon Jul 25 10:31:39 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 652225 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3rycxv0FYvz9sCY for ; Mon, 25 Jul 2016 20:32:02 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=bBYnLGXe; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=dvFF/DxKsSQbKj42PkNIRh1MKQl/+k9ZAlkGuA5xCS7xEnlsu0Izq v3kZd2RXkYbpoRtawl+S2sOEjlStEVXz18ObXeXsgzAZUOOTBDTnm0CSxssGhW59 /cyGR5ufBSfDYV4AA1ure+Q9kli1dmCnmbZtGpBntIQkK6JsUtAhNI= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=YhAZlQ0795XmEymy0cdbRTrnsMA=; b=bBYnLGXeFMRD5NEr/JtN z32PmJGsGAevF8Vv0H6sRahjJJ7WL6G3OZ3yuSgkDkVtPkNzii6CkS6DmvCMfAxP Q6GvxDM2nNhBq39Gj0VyXgvVVe81Fdst6yCuHJ5uRAKjb9JY+HUSGbew+T5w6vk7 hR/22YamFYy5FSmyquRkEb8= Received: (qmail 128084 invoked by alias); 25 Jul 2016 10:31:53 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 128035 invoked by uid 89); 25 Jul 2016 10:31:52 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.2 required=5.0 tests=BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=rng, weak, Launch, 100000 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Mon, 25 Jul 2016 10:31:41 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 83B9A3DD47; Mon, 25 Jul 2016 10:31:40 +0000 (UTC) Received: from localhost (ovpn-116-61.ams2.redhat.com [10.36.116.61]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u6PAVdgG009741; Mon, 25 Jul 2016 06:31:40 -0400 Date: Mon, 25 Jul 2016 11:31:39 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] Use std::mt19937, std::thread and std::atomic to simplify tests Message-ID: <20160725103139.GA7375@redhat.com> MIME-Version: 1.0 Content-Disposition: inline X-Clacks-Overhead: GNU Terry Pratchett User-Agent: Mutt/1.6.2 (2016-07-01) These tests that hammer shared_ptr and weak_ptr to test the thread-safety of the reference counting were originally written for the TR1 smart pointers, and so pre-date std::thread etc. This refactors them to use C++11 std::thread and std::atomic (with relaxed memory ordering, since we don't need to know the counter value until after we've joined all the worker threads). I also used the std::mt19937 typedef that uses the same parameters as the test lists explicitly (mt19937 produces uint_fast32_t not unsigned long, but that doesn't change anything here) and replaced the non-uniform modulus operation with std::uniform_int_distribution. To verify that after the changes we still test what we're trying to test, I changed the shared_ptr code to remove the synchronization in _M_add_ref_lock and saw the tests fail. * testsuite/20_util/shared_ptr/thread/default_weaktoshared.cc: Use std::mt19937, std::thread and std::atomic to simplify test. * testsuite/20_util/shared_ptr/thread/mutex_weaktoshared.cc: Likewise. Tested x86_64-linux (8 HT cores) and powerpc64-linux (160 cores). Committed to trunk. commit b2f86d9d04f9561b5189eccaa85e5d2b21884de4 Author: Jonathan Wakely Date: Mon Jul 25 10:32:11 2016 +0100 Use std::mt19937, std::thread and std::atomic to simplify tests * testsuite/20_util/shared_ptr/thread/default_weaktoshared.cc: Use std::mt19937, std::thread and std::atomic to simplify test. * testsuite/20_util/shared_ptr/thread/mutex_weaktoshared.cc: Likewise. diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/thread/default_weaktoshared.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/thread/default_weaktoshared.cc index d5b9d4f..a853307 100644 --- a/libstdc++-v3/testsuite/20_util/shared_ptr/thread/default_weaktoshared.cc +++ b/libstdc++-v3/testsuite/20_util/shared_ptr/thread/default_weaktoshared.cc @@ -25,11 +25,11 @@ #include #include #include -#include #include #include - -#include +#include +#include +#include #ifdef _GLIBCXX_HAVE_UNISTD_H #include // To test for _POSIX_THREAD_PRIORITY_SCHEDULING @@ -50,19 +50,13 @@ const unsigned long HAMMER_REPEAT = 100000; const unsigned long KILL_ONE_IN = 1000; struct A - { - static _Atomic_word counter; - A() - { - __gnu_cxx::__atomic_add(&counter, 1); - } - ~A() - { - __gnu_cxx::__atomic_add(&counter, -1); - } - }; +{ + static std::atomic counter; + A() { counter.fetch_add(1, std::memory_order_relaxed); } + ~A() { counter.fetch_sub(1, std::memory_order_relaxed); } +}; -_Atomic_word A::counter = 0; +std::atomic A::counter{ 0 }; typedef std::shared_ptr sp_A_t; typedef std::weak_ptr wp_A_t; @@ -80,16 +74,10 @@ struct shared_and_weak_pools { } }; -void* thread_hammer_and_kill(void* opaque_pools) +void thread_hammer_and_kill(shared_and_weak_pools& pools) { - shared_and_weak_pools& pools = *static_cast(opaque_pools); - // Using the same parameters as in the RNG test cases. - std::mersenne_twister_engine< - unsigned long, 32, 624, 397, 31, - 0x9908b0dful, 11, - 0xfffffffful, 7, - 0x9d2c5680ul, 15, - 0xefc60000ul, 18, 1812433253ul> rng; + std::mt19937 urbg; + std::uniform_int_distribution<> dist(0, KILL_ONE_IN - 1); sp_vector_t::iterator cur_shared = pools.shared_pool.begin(); wp_vector_t::iterator cur_weak = pools.weak_pool.begin(); @@ -107,26 +95,16 @@ void* thread_hammer_and_kill(void* opaque_pools) break; } - if (rng() % KILL_ONE_IN == 0) + if (dist(urbg) == 0) { cur_shared->reset(); ++cur_shared; } } - return 0; } -void* thread_hammer(void* opaque_weak) +void thread_hammer(wp_vector_t& weak_pool) { - wp_vector_t& weak_pool = *static_cast(opaque_weak); - // Using the same parameters as in the RNG test cases. - std::mersenne_twister_engine< - unsigned long, 32, 624, 397, 31, - 0x9908b0dful, 11, - 0xfffffffful, 7, - 0x9d2c5680ul, 15, - 0xefc60000ul, 18, 1812433253ul> rng; - wp_vector_t::iterator cur_weak = weak_pool.begin(); for (unsigned int i = 0; i < HAMMER_REPEAT; ++i) @@ -142,51 +120,38 @@ void* thread_hammer(void* opaque_weak) break; } } - return 0; } -int +void test01() { bool test __attribute__((unused)) = true; sp_vector_t obj_pool(POOL_SIZE); - for(sp_vector_t::iterator cur = obj_pool.begin(); cur != obj_pool.end(); ++cur) - { - cur->reset(new A); - } + for(auto& obj : obj_pool) + obj.reset(new A); + // Obtain weak references. std::vector weak_pool(HAMMER_MAX_THREADS, wp_vector_t(obj_pool.begin(), obj_pool.end())); // Launch threads with pointer to weak reference. - pthread_t threads[HAMMER_MAX_THREADS]; + std::thread threads[HAMMER_MAX_THREADS]; #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500 pthread_setconcurrency (HAMMER_MAX_THREADS); #endif - pthread_attr_t tattr; - pthread_attr_init(&tattr); - shared_and_weak_pools pools(obj_pool, weak_pool[0]); - pthread_create(threads, &tattr, thread_hammer_and_kill, static_cast(&pools)); + threads[0] = std::thread(thread_hammer_and_kill, std::ref(pools)); for (unsigned int worker = 1; worker < HAMMER_MAX_THREADS; worker++) - { - if (pthread_create(&threads[worker], &tattr, - thread_hammer, static_cast(&weak_pool[worker]))) - std::abort(); - } + threads[worker] = std::thread(thread_hammer, std::ref(weak_pool[worker])); + // Wait for threads to complete, then check integrity of reference. - void* status; - for (unsigned int worker = 0; worker < HAMMER_MAX_THREADS; worker++) - { - if (pthread_join(threads[worker], &status)) - std::abort(); - } + for (auto& thread : threads) + thread.join(); + obj_pool.clear(); VERIFY( A::counter == 0 ); - - return 0; } int diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/thread/mutex_weaktoshared.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/thread/mutex_weaktoshared.cc index 9a01716..404fc46 100644 --- a/libstdc++-v3/testsuite/20_util/shared_ptr/thread/mutex_weaktoshared.cc +++ b/libstdc++-v3/testsuite/20_util/shared_ptr/thread/mutex_weaktoshared.cc @@ -25,11 +25,11 @@ #include #include #include -#include #include #include - -#include +#include +#include +#include #ifdef _GLIBCXX_HAVE_UNISTD_H #include // To test for _POSIX_THREAD_PRIORITY_SCHEDULING @@ -50,19 +50,13 @@ const unsigned long HAMMER_REPEAT = 100000; const unsigned long KILL_ONE_IN = 1000; struct A - { - static _Atomic_word counter; - A() - { - __gnu_cxx::__atomic_add(&counter, 1); - } - ~A() - { - __gnu_cxx::__atomic_add(&counter, -1); - } - }; +{ + static std::atomic counter; + A() { counter.fetch_add(1, std::memory_order_relaxed); } + ~A() { counter.fetch_sub(1, std::memory_order_relaxed); } +}; -_Atomic_word A::counter = 0; +std::atomic A::counter{0}; using std::_S_mutex; @@ -82,16 +76,10 @@ struct shared_and_weak_pools { } }; -void* thread_hammer_and_kill(void* opaque_pools) +void thread_hammer_and_kill(shared_and_weak_pools& pools) { - shared_and_weak_pools& pools = *static_cast(opaque_pools); - // Using the same parameters as in the RNG test cases. - std::mersenne_twister_engine< - unsigned long, 32, 624, 397, 31, - 0x9908b0dful, 11, - 0xfffffffful, 7, - 0x9d2c5680ul, 15, - 0xefc60000ul, 18, 1812433253ul> rng; + std::mt19937 urbg; + std::uniform_int_distribution<> dist(0, KILL_ONE_IN - 1); sp_vector_t::iterator cur_shared = pools.shared_pool.begin(); wp_vector_t::iterator cur_weak = pools.weak_pool.begin(); @@ -109,26 +97,16 @@ void* thread_hammer_and_kill(void* opaque_pools) break; } - if (rng() % KILL_ONE_IN == 0) + if (dist(urbg) == 0) { cur_shared->reset(); ++cur_shared; } } - return 0; } -void* thread_hammer(void* opaque_weak) +void thread_hammer(wp_vector_t& weak_pool) { - wp_vector_t& weak_pool = *static_cast(opaque_weak); - // Using the same parameters as in the RNG test cases. - std::mersenne_twister_engine< - unsigned long, 32, 624, 397, 31, - 0x9908b0dful, 11, - 0xfffffffful, 7, - 0x9d2c5680ul, 15, - 0xefc60000ul, 18, 1812433253ul> rng; - wp_vector_t::iterator cur_weak = weak_pool.begin(); for (unsigned int i = 0; i < HAMMER_REPEAT; ++i) @@ -144,51 +122,38 @@ void* thread_hammer(void* opaque_weak) break; } } - return 0; } -int +void test01() { bool test __attribute__((unused)) = true; sp_vector_t obj_pool(POOL_SIZE); - for(sp_vector_t::iterator cur = obj_pool.begin(); cur != obj_pool.end(); ++cur) - { - cur->reset(new A); - } + for(auto& obj : obj_pool) + obj.reset(new A); + // Obtain weak references. std::vector weak_pool(HAMMER_MAX_THREADS, wp_vector_t(obj_pool.begin(), obj_pool.end())); // Launch threads with pointer to weak reference. - pthread_t threads[HAMMER_MAX_THREADS]; + std::thread threads[HAMMER_MAX_THREADS]; #if defined(__sun) && defined(__svr4__) && _XOPEN_VERSION >= 500 pthread_setconcurrency (HAMMER_MAX_THREADS); #endif - pthread_attr_t tattr; - pthread_attr_init(&tattr); - shared_and_weak_pools pools(obj_pool, weak_pool[0]); - pthread_create(threads, &tattr, thread_hammer_and_kill, static_cast(&pools)); + threads[0] = std::thread(thread_hammer_and_kill, std::ref(pools)); for (unsigned int worker = 1; worker < HAMMER_MAX_THREADS; worker++) - { - if (pthread_create(&threads[worker], &tattr, - thread_hammer, static_cast(&weak_pool[worker]))) - std::abort(); - } + threads[worker] = std::thread(thread_hammer, std::ref(weak_pool[worker])); + // Wait for threads to complete, then check integrity of reference. - void* status; - for (unsigned int worker = 0; worker < HAMMER_MAX_THREADS; worker++) - { - if (pthread_join(threads[worker], &status)) - std::abort(); - } + for (auto& thread : threads) + thread.join(); + obj_pool.clear(); VERIFY( A::counter == 0 ); - - return 0; } int