From patchwork Wed Jun 16 18:35:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Rodgers X-Patchwork-Id: 1493027 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=8.43.85.97; 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=04it0f1c; dkim-atps=neutral Received: from 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 RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4G4v6R0xdfz9sWM for ; Thu, 17 Jun 2021 04:36:45 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C58B4398E44F for ; Wed, 16 Jun 2021 18:36:41 +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.41]) by sourceware.org (Postfix) with ESMTPS id B84CC397F43A; Wed, 16 Jun 2021 18:36:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B84CC397F43A 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-out002.mykolab.com (Postfix) with ESMTP id AA0D8E8D; Wed, 16 Jun 2021 20:36:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kolabnow.com; h= content-transfer-encoding:mime-version:message-id:date:date :subject:subject:from:from:received:received:received; s= dkim20160331; t=1623868564; x=1625682965; bh=s/4K3VZHegeAG1rvAE8 vNwwj/ptGjBdAQtiP0O08ZHA=; b=04it0f1cYGnh6vrDhWQ4s9bkrZ1TGcElkyj ex2m71F3/iPvfEcRPnIKFnqgcVd7sk5fhq0hwgAVl/8ntibsB+pk7eeSEZoTcZi2 8jUo+OPTMUqUlR8n1a03whGhvc2pztNHHqeCgcrqzzO8KRuE/4r9dEZblbbFPVu8 tkzdnh9WTx9voVebomq1nQ/qBT8i2nEb3srH6+4P86Ih7t/OizCF3MZIEWGdFegY HJlHgCKqiWNvtvFysrWuWxEQlyI+3OSDkr3JhFQI9JqtEuDTY4k5UUCUjexKloNZ MOsvut71/WpfeWY4M+3bVZypsq9rZrImtWC0LxqXm6zb1EIbsEnkhVv1zKJRTWci OKkWy46NLxxqe8kdjvZhAHk00KLaqkrdhstYYaAVg+JdAJYehGHaLPxf43hqDAa1 euTFAnFkh9jlv2/Dr+LcL6HySCxgNgj8mrdFMjB96aXfLBtKfP8C89xrObOLSlNY 8rRpNIPKrUk9XzpOqHuIsorW4AdU+78f35X7GJLxW3+mIda/jA7JeXQsTIKSv4OH lF6stAQpjala3+9JFj0b/DwXYMS311NWR/hUZhvHD9nTQxHO0GQ0BAkqBBml2aj5 559vqLrVbtMIN33lBuKTqzqqjy0wgmC3le4eqOyEVk2ELitd9dV8nTaCnpXrIs6v K0EHFGig= X-Virus-Scanned: amavisd-new at mykolab.com X-Spam-Score: -0.778 X-Spam-Level: X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_SHORT, 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-out002.mykolab.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id kJhmYp_vs41q; Wed, 16 Jun 2021 20:36:04 +0200 (CEST) Received: from int-mx001.mykolab.com (unknown [10.9.13.1]) by ext-mx-out002.mykolab.com (Postfix) with ESMTPS id 90273DBF; Wed, 16 Jun 2021 20:36:04 +0200 (CEST) Received: from ext-subm003.mykolab.com (unknown [10.9.6.3]) by int-mx001.mykolab.com (Postfix) with ESMTPS id 82CE02C90; Wed, 16 Jun 2021 20:36:02 +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 11:35:55 -0700 Message-Id: <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" This is an 'interim' fix. For now it forces all waiting threads to wake on _M_release(). This isn't exactly efficient but resolves the issue in the immediate term. 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 | 77 +++++++++++++++++++ 2 files changed, 80 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..483779caf0a --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/semaphore/100806.cc @@ -0,0 +1,77 @@ +// Copyright (C) 2020-2021 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { 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(); + } + } +}