From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id GCRFJKYcGWhjUhYAWB0awg (envelope-from ) for ; Mon, 05 May 2025 16:16:38 -0400 Received: by simark.ca (Postfix, from userid 112) id 924121E10E; Mon, 5 May 2025 16:16:38 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-9.0 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED, RCVD_IN_VALIDITY_RPBL,RCVD_IN_VALIDITY_SAFE autolearn=unavailable autolearn_force=no version=4.0.1 Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 2033E1E089 for ; Mon, 5 May 2025 16:16:38 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EE12B385735A for ; Mon, 5 May 2025 20:16:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EE12B385735A Received: from simark.ca (simark.ca [158.69.221.121]) by sourceware.org (Postfix) with ESMTPS id 0F2CA385781B for ; Mon, 5 May 2025 20:15:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0F2CA385781B Authentication-Results: sourceware.org; dmarc=fail (p=none dis=none) header.from=efficios.com Authentication-Results: sourceware.org; spf=fail smtp.mailfrom=efficios.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0F2CA385781B Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=158.69.221.121 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1746476151; cv=none; b=JX30uIM5InyqRaebrU15j6u+uqWkn4uDHWooUCjKyjJQLIu9evvk6BfQdNV2HGjmCmMPwMbgMGx0UZGY7A8p8g+HRPEykRtrbs6TjhkiMHqhKgBvzBX8SfK80Ftkw6EsTSurbxl/2BCSKSbv/SQpVT+qsTUF7kL1/KPr/7HdxJI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1746476151; c=relaxed/simple; bh=joHcufddIWSSiR7j18LDXDt6ciaSjhA0V2pN4qDU3+c=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=VW6pjs/xyDGXqZWQsIA0bFJLHJvYMv0CbyQ56N9rL9RWmMp1NzTrgpn1CEGeNkKRNZULwtTAx3A5BOBr77ODiMTh+0a0vv00JmCOwhJzxszJOj8GVc/4kfzr4g0Bv3Vx6W5Fw+hA9+Kxf/os7PhCD5lCJAxbFtb07SYOTZxPkMo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0F2CA385781B Received: by simark.ca (Postfix, from userid 112) id AA7D91E126; Mon, 5 May 2025 16:15:50 -0400 (EDT) Received: from smarchi-efficios.internal.efficios.com (96-127-217-162.qc.cable.ebox.net [96.127.217.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id EF78D1E089; Mon, 5 May 2025 16:15:48 -0400 (EDT) From: Simon Marchi To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH 1/6] gdb: re-work parallel-for-selftests.c Date: Mon, 5 May 2025 16:15:25 -0400 Message-ID: <20250505201548.184917-1-simon.marchi@efficios.com> X-Mailer: git-send-email 2.49.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org From: Simon Marchi I find this file difficult to work with and modify, due to how it uses the preprocessor to include itself, to generate variations of the test functions. Change it to something a bit more C++-y, with a test function that accepts a callback to invoke the foreach function under test. Change-Id: Ibf1e2907380a88a4f8e4b4b88df2b0dfd0e9b6c8 --- gdb/unittests/parallel-for-selftests.c | 137 ++++++++++--------------- 1 file changed, 56 insertions(+), 81 deletions(-) diff --git a/gdb/unittests/parallel-for-selftests.c b/gdb/unittests/parallel-for-selftests.c index 841d914d75b0..c9a1689acfb0 100644 --- a/gdb/unittests/parallel-for-selftests.c +++ b/gdb/unittests/parallel-for-selftests.c @@ -17,13 +17,6 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -/* This file is divided in two parts: - - FOR_EACH-undefined, and - - FOR_EACH-defined. - The former includes the latter, more than once, with different values for - FOR_EACH. The FOR_EACH-defined part reads like a regular function. */ -#ifndef FOR_EACH - #include "gdbsupport/selftest.h" #include "gdbsupport/parallel-for.h" @@ -49,37 +42,70 @@ struct save_restore_n_threads int n_threads; }; -/* Define test_par using TEST in the FOR_EACH-defined part. */ -#define TEST test_par -#define FOR_EACH gdb::parallel_for_each -#include "parallel-for-selftests.c" -#undef FOR_EACH -#undef TEST - -/* Define test_seq using TEST in the FOR_EACH-defined part. */ -#define TEST test_seq -#define FOR_EACH gdb::sequential_for_each -#include "parallel-for-selftests.c" -#undef FOR_EACH -#undef TEST +using foreach_callback_t = gdb::function_view; +using do_foreach_t = gdb::function_view; static void -test (int n_threads) +test_one (int n_threads, do_foreach_t do_foreach) { - test_par (n_threads); - test_seq (n_threads); + save_restore_n_threads saver; + gdb::thread_pool::g_thread_pool->set_thread_count (n_threads); + + { + constexpr int upper_bound = 1000; + std::atomic counter (0); + do_foreach (0, upper_bound, + [&] (int start, int end) { counter += end - start; }); + SELF_CHECK (counter == upper_bound); + } + + { + std::atomic counter (0); + do_foreach (0, 0, [&] (int start, int end) { counter += end - start; }); + SELF_CHECK (counter == 0); + } + + { + /* Check that if there are fewer tasks than threads, then we won't + end up with a null result. */ + std::vector> intresults; + std::atomic any_empty_tasks (false); + + do_foreach (0, 1, + [&] (int start, int end) + { + if (start == end) + any_empty_tasks = true; + + return std::make_unique (end - start); + }); + + SELF_CHECK (!any_empty_tasks); + SELF_CHECK (std::all_of (intresults.begin (), intresults.end (), + [] (const std::unique_ptr &entry) + { return entry != nullptr; })); + } } static void -test_n_threads () +test_parallel_for_each () { - test (0); - test (1); - test (3); + const std::vector for_each_functions + { + [] (int start, int end, foreach_callback_t callback) + { gdb::parallel_for_each (1, start, end, callback); }, + [] (int start, int end, foreach_callback_t callback) + { gdb::sequential_for_each (1, start, end, callback);} + }; + + for (int n_threads : { 0, 1, 3 }) + for (const auto &for_each_function : for_each_functions) + test_one (n_threads, for_each_function); } -} -} +} /* namespace parallel_for */ +} /* namespace selftests */ #endif /* CXX_STD_THREAD */ @@ -89,57 +115,6 @@ _initialize_parallel_for_selftests () { #ifdef CXX_STD_THREAD selftests::register_test ("parallel_for", - selftests::parallel_for::test_n_threads); + selftests::parallel_for::test_parallel_for_each); #endif /* CXX_STD_THREAD */ } - -#else /* FOR_EACH */ - -static void -TEST (int n_threads) -{ - save_restore_n_threads saver; - gdb::thread_pool::g_thread_pool->set_thread_count (n_threads); - -#define NUMBER 10000 - - std::atomic counter (0); - FOR_EACH (1, 0, NUMBER, - [&] (int start, int end) - { - counter += end - start; - }); - SELF_CHECK (counter == NUMBER); - - counter = 0; - FOR_EACH (1, 0, 0, - [&] (int start, int end) - { - counter += end - start; - }); - SELF_CHECK (counter == 0); - -#undef NUMBER - - /* Check that if there are fewer tasks than threads, then we won't - end up with a null result. */ - std::vector> intresults; - std::atomic any_empty_tasks (false); - - FOR_EACH (1, 0, 1, - [&] (int start, int end) - { - if (start == end) - any_empty_tasks = true; - return std::make_unique (end - start); - }); - SELF_CHECK (!any_empty_tasks); - SELF_CHECK (std::all_of (intresults.begin (), - intresults.end (), - [] (const std::unique_ptr &entry) - { - return entry != nullptr; - })); -} - -#endif /* FOR_EACH */ base-commit: f3d834df28734ebc82931808960fe63401127c06 -- 2.49.0