From patchwork Wed Jun 16 19:52:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Rodgers X-Patchwork-Id: 1493041 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (4096-bit key; secure) header.d=kolabnow.com header.i=@kolabnow.com header.a=rsa-sha256 header.s=dkim20160331 header.b=f7oGbNzQ; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4G4wpN47Qfz9sPf for ; Thu, 17 Jun 2021 05:52:58 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 00A31398B40F for ; Wed, 16 Jun 2021 19:52:56 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx.kolabnow.com (mx.kolabnow.com [95.128.36.42]) by sourceware.org (Postfix) with ESMTPS id 628B53857C7B; Wed, 16 Jun 2021 19:52:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 628B53857C7B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=appliantology.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=appliantology.com Received: from localhost (unknown [127.0.0.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTP id 9652813DF; Wed, 16 Jun 2021 21:52:29 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:date:subject:subject:from:from:received :received:received; s=dkim20160331; t=1623873149; x=1625687550; bh=pPBI2+TwGZ7ncXRywuy2jve403XsnpRY70TXVd5GWHA=; b=f7oGbNzQloYs 3I+7PDu0Bb+Wa7c8Y5Fg96dvYDxaIPP99d87+SVFH/WkAGrlhmelV0RRe1Ef1cOw 2jALD4w4c6l+gTrbBel/mshyXkok8xw3ubxUg4gQODdNewL8U2kVhaVBGysbPlPh 8O49XYOA+nJjco0MaunNbjbX2nogGCEQxwReXnblS2ioYpr+bz32jHQfgYFqXpyi 3iAD+dk0sMh/jZjs+2IkeJ4fb9Y9p6OAwPXEoI6YN1X8RyCSuwtZyMBeA9eft8W5 OPW9AbIQrYelhGUl8YDe+99KiUOcdWiN+PxVNxlE8Nhxvmfjj+W81in8DPFYs+XC qUiTlyG4S68dFPnhKqi40lyzjzTA5ZjelFETLirfyXJxxwMTLlhdee68ul0ukiMx EWBv6iYIPIE1dKtaWlwPSTIgNrddDBC0JTIq8VvnlAagjOarUzzNQosEm2XMc1np hXAMfcwcPcJC5kfoe0rY0PJ+K8W2R93040GgG4HwEnRF9MJXMta3EqXNjZkUFVy1 G1rY1ABwVDgRb2bndA2U33k5eTIjaGYgNS9kycEKbH2WY9kGPbhBrK5ENkyK14UJ VOLRYLYg2yI+6KyQqciGG71EdLlj5a+MrnId3NW/DwfCAgXN/uZ+aPhYh+1AmuYP 5bT/50ibSOF94BZCdR3hD8Iqrt7ebgA= X-Virus-Scanned: amavisd-new at mykolab.com X-Spam-Score: -0.777 X-Spam-Level: X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX autolearn=ham autolearn_force=no version=3.4.2 Received: from mx.kolabnow.com ([127.0.0.1]) by localhost (ext-mx-out001.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id sdxZU1vlGvbx; Wed, 16 Jun 2021 21:52:29 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTPS id E8606B49; Wed, 16 Jun 2021 21:52:28 +0200 (CEST) Received: from ext-subm001.mykolab.com (unknown [10.9.6.1]) by int-mx001.mykolab.com (Postfix) with ESMTPS id A7BBC24E3; Wed, 16 Jun 2021 21:52:26 +0200 (CEST) From: Thomas Rodgers To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH] libstdc++: Fix for deadlock in std::counting_semaphore [PR100806] Date: Wed, 16 Jun 2021 12:52:18 -0700 Message-Id: <20210616195218.1451153-1-rodgert@appliantology.com> In-Reply-To: <20210616183555.1448862-1-rodgert@appliantology.com> References: <20210616183555.1448862-1-rodgert@appliantology.com> MIME-Version: 1.0 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: trodgers@redhat.com Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Same as previous version except removing the copyright notice from the test. libstdc++-v3/ChangeLog: libstdc++/PR100806 * include/bits/semaphore_base.h (__atomic_semaphore::_M_release(): Force _M_release() to wake all waiting threads. * testsuite/30_threads/semaphore/100806.cc: New test. --- libstdc++-v3/include/bits/semaphore_base.h | 4 +- .../testsuite/30_threads/semaphore/100806.cc | 60 +++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 libstdc++-v3/testsuite/30_threads/semaphore/100806.cc diff --git a/libstdc++-v3/include/bits/semaphore_base.h b/libstdc++-v3/include/bits/semaphore_base.h index 9a55978068f..c4565d7e560 100644 --- a/libstdc++-v3/include/bits/semaphore_base.h +++ b/libstdc++-v3/include/bits/semaphore_base.h @@ -256,7 +256,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if (__update > 1) __atomic_notify_address_bare(&_M_counter, true); else - __atomic_notify_address_bare(&_M_counter, false); + __atomic_notify_address_bare(&_M_counter, true); +// FIXME - Figure out why this does not wake a waiting thread +// __atomic_notify_address_bare(&_M_counter, false); } private: diff --git a/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc b/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc new file mode 100644 index 00000000000..938c2793be1 --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc @@ -0,0 +1,60 @@ +// { dg-options "-std=gnu++2a -pthread" } +// { dg-do run { target c++2a } } +// { dg-require-effective-target pthread } +// { dg-require-gthreads "" } +// { dg-add-options libatomic } + +#include +#include +#include + +#include +#include +#include +#include +#include + +std::counting_semaphore<4> semaphore{6}; + +std::mutex mtx; +std::vector results; + +void thread_main(size_t x) +{ + semaphore.acquire(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + semaphore.release(); + { + std::ostringstream stm; + stm << "Thread " << x << " finished."; + std::lock_guard g{ mtx }; + results.push_back(stm.str()); + } +} + +int main() +{ + + constexpr auto nthreads = 10; + + std::vector threads(nthreads); + + + size_t counter{0}; + for(auto& t : threads) + { + t = std::thread(thread_main, counter++); + } + + for(auto& t : threads) + { + t.join(); + { + std::lock_guard g{ mtx }; + for (auto&& r : results) + std::cout << r << '\n'; + std::cout.flush(); + results.clear(); + } + } +}