From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 5E73A385781C for ; Fri, 1 Oct 2021 19:43:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5E73A385781C Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-424-Uzwiyx9oNZeWYzEJpQghhg-1; Fri, 01 Oct 2021 15:43:55 -0400 X-MC-Unique: Uzwiyx9oNZeWYzEJpQghhg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 19F9B1023F55; Fri, 1 Oct 2021 19:43:55 +0000 (UTC) Received: from localhost (unknown [10.33.36.47]) by smtp.corp.redhat.com (Postfix) with ESMTP id B530C19C59; Fri, 1 Oct 2021 19:43:54 +0000 (UTC) Date: Fri, 1 Oct 2021 20:43:54 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Make std::jthread support pointers to member functions [PR 100612] Message-ID: MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="ZhE5ApbWagROMNIe" Content-Disposition: inline X-Spam-Status: No, score=-13.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: , X-List-Received-Date: Fri, 01 Oct 2021 19:43:58 -0000 --ZhE5ApbWagROMNIe Content-Type: text/plain; charset=us-ascii Content-Disposition: inline This adds a non-standard extension to support initializing a std::jthread with a pointer to a member function that expects a stop_token to be added to the arguments. That use case is not supported by C++20, because the stop_token would get added as the first argument, which is where the object argument needs to be to invoke a pointer to member function. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: PR libstdc++/100612 * include/std/thread (__pmf_expects_stop_token): New variable template to detect a pointer to member function that needs a stop_token to be added to the arguments. (jthread::__S_create): Use __pmf_expects_stop_token. (jthread::__S_create_pmf): New function. * testsuite/30_threads/jthread/100612.cc: New test. Tested powerpc64le-linux. Committed to trunk. --ZhE5ApbWagROMNIe Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" commit 34e9407b3b4298bd587e0df2e0047679019b66cf Author: Jonathan Wakely Date: Thu May 20 22:36:16 2021 libstdc++: Make std::jthread support pointers to member functions [PR 100612] This adds a non-standard extension to support initializing a std::jthread with a pointer to a member function that expects a stop_token to be added to the arguments. That use case is not supported by C++20, because the stop_token would get added as the first argument, which is where the object argument needs to be to invoke a pointer to member function. Signed-off-by: Jonathan Wakely libstdc++-v3/ChangeLog: PR libstdc++/100612 * include/std/thread (__pmf_expects_stop_token): New variable template to detect a pointer to member function that needs a stop_token to be added to the arguments. (jthread::__S_create): Use __pmf_expects_stop_token. (jthread::__S_create_pmf): New function. * testsuite/30_threads/jthread/100612.cc: New test. diff --git a/libstdc++-v3/include/std/thread b/libstdc++-v3/include/std/thread index f51392ab42c..46519086aae 100644 --- a/libstdc++-v3/include/std/thread +++ b/libstdc++-v3/include/std/thread @@ -99,6 +99,16 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #ifdef __cpp_lib_jthread +#ifndef __STRICT_ANSI__ + template + constexpr bool __pmf_expects_stop_token = false; + + template + constexpr bool __pmf_expects_stop_token<_Callable, _Obj, _Args...> + = __and_>, + is_invocable<_Callable, _Obj, stop_token, _Args...>>::value; +#endif + /// A thread that can be requested to stop and automatically joined. class jthread { @@ -211,6 +221,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION static thread _S_create(stop_source& __ssrc, _Callable&& __f, _Args&&... __args) { +#ifndef __STRICT_ANSI__ + if constexpr (__pmf_expects_stop_token<_Callable, _Args...>) + return _S_create_pmf(__ssrc, __f, std::forward<_Args>(__args)...); + else +#endif if constexpr(is_invocable_v, stop_token, decay_t<_Args>...>) return thread{std::forward<_Callable>(__f), __ssrc.get_token(), @@ -226,6 +241,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } } +#ifndef __STRICT_ANSI__ + template + static thread + _S_create_pmf(stop_source& __ssrc, _Callable __f, _Obj&& __obj, + _Args&&... __args) + { + return thread{__f, std::forward<_Obj>(__obj), __ssrc.get_token(), + std::forward<_Args>(__args)...}; + } +#endif + stop_source _M_stop_source; thread _M_thread; }; diff --git a/libstdc++-v3/testsuite/30_threads/jthread/100612.cc b/libstdc++-v3/testsuite/30_threads/jthread/100612.cc new file mode 100644 index 00000000000..d6c81706b8f --- /dev/null +++ b/libstdc++-v3/testsuite/30_threads/jthread/100612.cc @@ -0,0 +1,24 @@ +// { dg-options "-std=gnu++20" } +// { dg-do compile { target c++20 } } +// { dg-additional-options "-pthread" { target pthread } } +// { dg-require-gthreads "" } + +#include + +void +test_pfm() +{ + // PR libstdc++/100612 + struct X + { + void run(std::stop_token) { } + void run_arg(int) { } + void run_args(std::stop_token, int, int) { } + }; + + X x; + + std::jthread{&X::run, &x}; + std::jthread{&X::run_arg, &x, 1}; + std::jthread{&X::run_args, &x, 1, 1}; +} --ZhE5ApbWagROMNIe--