From patchwork Tue Sep 8 18:06:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Rodgers X-Patchwork-Id: 1359892 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@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=appliantology.com 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=I1cnvIEh; 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 4BmClK2t3hz9sTd for ; Wed, 9 Sep 2020 04:06:36 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 615693850424; Tue, 8 Sep 2020 18:06:32 +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 6544E3861036; Tue, 8 Sep 2020 18:06:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 6544E3861036 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=appliantology.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=rodgert@appliantology.com Received: from localhost (unknown [127.0.0.1]) by ext-mx-out001.mykolab.com (Postfix) with ESMTP id 01129A47; Tue, 8 Sep 2020 20:06:28 +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=1599588386; x=1601402787; bh=cxOvzQ8G1K8qC9MqG0U DINhoosvZIq1zcPFRwCfsXi0=; b=I1cnvIEhoUSWBGWoLB4evbTp7swD4LFQ7fm Rk8JUSd8Uy+eQvFwi/LA420flol9/09vkhD/l5ECBUSWRS7Fh2GanubWG+FeqbIW oWCXBx8JcdziPOJZktvM+75d9RESkDM+W2lHePgUuMJ6m1tBYfa/wWVPtSeXNAqV tr8EvoUNgOi/FyXeJCgJxdyd1DUfl5x02HY7VsvcZXZH15+ogt1Uv8tMQwANRo7h 4Rg6k4RaSPAagacQOmRy9Nj8F83rEz7MEwPpOzbgeEz3HeHHXMo0vYtPUDy3u4TA s9IrD2r1zj8DIMHBplhUXi3EsFwZbf+pmY6rFGzBvss2q7PdhY6du0f65KxAlcT8 jNbBDEItV1PqwEZ0dV1R8CE+acbrM+w/jQnDI5dk8rIgJ1DubTzN1+PKCOeyrx1r Nk15cNBVqY1pG81eDQiU4wLZzVqkhZJg1G2IbCFEL8D/Cxrhx9Dn+ywD1PlJgK7v gUlrAhb7ycSrXQ8F2agk0XkpvA9S/1GFi/1RfKo3SnDHz8uanCHueGJHmc07lIuo ZM4ObHxXxOx4dPkVKFxkcJYzXElyIsxSqi2wwYkt1Z0rvkL0w8dIZ3ybToy+cqP8 S0DhnBtWvJ6y8d1/7SMMVSaF4Gjo6DJdrgzF9VAJxkM2IqQDNiOFDPRADioHl3Tq qW+IEvZU= X-Virus-Scanned: amavisd-new at mykolab.com X-Spam-Score: -1.899 X-Spam-Level: X-Spam-Status: No, score=-9.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_SHORT, SPF_HELO_NONE, SPF_NONE, TXREP 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 swSqaTv1SYE7; Tue, 8 Sep 2020 20:06:26 +0200 (CEST) Received: from int-mx002.mykolab.com (unknown [10.9.13.2]) by ext-mx-out001.mykolab.com (Postfix) with ESMTPS id 70EEF5CC; Tue, 8 Sep 2020 20:06:26 +0200 (CEST) Received: from ext-subm002.mykolab.com (unknown [10.9.6.2]) by int-mx002.mykolab.com (Postfix) with ESMTPS id 0DD463610; Tue, 8 Sep 2020 20:06:26 +0200 (CEST) From: Thomas Rodgers To: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Subject: [PATCH] Add support for atomic_flag::wait/notify_one/notify_all Date: Tue, 8 Sep 2020 11:06:17 -0700 Message-Id: <20200908180617.1280094-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@gcc.gnu.org Sender: "Gcc-patches" * include/bits/atomic_base.h (__atomic_flag::wait): Define. (__atomic_flag::notify_one): Likewise. (__atomic_flag::notify_all): Likewise. * testsuite/29_atomics/atomic_flag/wait_notify/1.cc: New test. --- libstdc++-v3/include/bits/atomic_base.h | 23 +++++++ .../29_atomics/atomic_flag/wait_notify/1.cc | 61 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 libstdc++-v3/testsuite/29_atomics/atomic_flag/wait_notify/1.cc diff --git a/libstdc++-v3/include/bits/atomic_base.h b/libstdc++-v3/include/bits/atomic_base.h index c121d993fee..a7ddd03d544 100644 --- a/libstdc++-v3/include/bits/atomic_base.h +++ b/libstdc++-v3/include/bits/atomic_base.h @@ -229,6 +229,29 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __atomic_load(&_M_i, &__v, int(__m)); return __v == __GCC_ATOMIC_TEST_AND_SET_TRUEVAL; } + + _GLIBCXX_ALWAYS_INLINE void + wait(bool __old, + memory_order __m = memory_order_seq_cst) const noexcept + { + std::__atomic_wait(&_M_i, __old, + [__m, this, __old]() + { return this->test(__m) != __old; }); + } + + // TODO add const volatile overload + + _GLIBCXX_ALWAYS_INLINE void + notify_one() const noexcept + { std::__atomic_notify(&_M_i, false); } + + // TODO add const volatile overload + + _GLIBCXX_ALWAYS_INLINE void + notify_all() const noexcept + { std::__atomic_notify(&_M_i, true); } + + // TODO add const volatile overload #endif // C++20 _GLIBCXX_ALWAYS_INLINE void diff --git a/libstdc++-v3/testsuite/29_atomics/atomic_flag/wait_notify/1.cc b/libstdc++-v3/testsuite/29_atomics/atomic_flag/wait_notify/1.cc new file mode 100644 index 00000000000..6de7873ecc2 --- /dev/null +++ b/libstdc++-v3/testsuite/29_atomics/atomic_flag/wait_notify/1.cc @@ -0,0 +1,61 @@ +// { dg-options "-std=gnu++2a -pthread" } +// { dg-do run { target c++2a } } +// { dg-require-effective-target pthread } +// { dg-require-gthreads "" } + +// Copyright (C) 2020 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 +// . + +#include +#include +#include +#include +#include +#include + +#include + +int +main() +{ + using namespace std::literals::chrono_literals; + + std::mutex m; + std::condition_variable cv; + + std::atomic_flag a; + std::atomic_flag b; + std::thread t([&] + { + cv.notify_one(); + a.wait(false); + b.test_and_set(); + b.notify_one(); + }); + + std::unique_lock l(m); + cv.wait(l); + std::this_thread::sleep_for(100ms); + a.test_and_set(); + a.notify_one(); + b.wait(false); + t.join(); + + VERIFY( a.test() ); + VERIFY( b.test() ); + return 0; +}