* [committed] libstdc++: Make std::jthread support pointers to member functions [PR 100612]
@ 2021-10-01 19:43 Jonathan Wakely
2021-10-01 20:25 ` Ville Voutilainen
0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Wakely @ 2021-10-01 19:43 UTC (permalink / raw)
To: libstdc++, gcc-patches
[-- Attachment #1: Type: text/plain, Size: 820 bytes --]
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 <jwakely@redhat.com>
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.
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 3625 bytes --]
commit 34e9407b3b4298bd587e0df2e0047679019b66cf
Author: Jonathan Wakely <jwakely@redhat.com>
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 <jwakely@redhat.com>
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<typename _Callable, typename... _Args>
+ constexpr bool __pmf_expects_stop_token = false;
+
+ template<typename _Callable, typename _Obj, typename... _Args>
+ constexpr bool __pmf_expects_stop_token<_Callable, _Obj, _Args...>
+ = __and_<is_member_function_pointer<remove_reference_t<_Callable>>,
+ 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<decay_t<_Callable>, 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<typename _Callable, typename _Obj, typename... _Args>
+ 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 <thread>
+
+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};
+}
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [committed] libstdc++: Make std::jthread support pointers to member functions [PR 100612]
2021-10-01 19:43 [committed] libstdc++: Make std::jthread support pointers to member functions [PR 100612] Jonathan Wakely
@ 2021-10-01 20:25 ` Ville Voutilainen
2021-10-01 20:55 ` Jonathan Wakely
0 siblings, 1 reply; 3+ messages in thread
From: Ville Voutilainen @ 2021-10-01 20:25 UTC (permalink / raw)
To: Jonathan Wakely; +Cc: libstdc++, gcc-patches List
On Fri, 1 Oct 2021 at 23:19, Jonathan Wakely via Libstdc++
<libstdc++@gcc.gnu.org> wrote:
>
> 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.
Yeah, and the use-case is supported by applying a wrapper that does
the right kind of argument binding, like
shown in the BZ. Why are we doing this?
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [committed] libstdc++: Make std::jthread support pointers to member functions [PR 100612]
2021-10-01 20:25 ` Ville Voutilainen
@ 2021-10-01 20:55 ` Jonathan Wakely
0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2021-10-01 20:55 UTC (permalink / raw)
To: Ville Voutilainen; +Cc: libstdc++, gcc-patches List
On Fri, 1 Oct 2021 at 21:26, Ville Voutilainen wrote:
>
> On Fri, 1 Oct 2021 at 23:19, Jonathan Wakely via Libstdc++
> <libstdc++@gcc.gnu.org> wrote:
> >
> > 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.
>
> Yeah, and the use-case is supported by applying a wrapper that does
> the right kind of argument binding, like
> shown in the BZ. Why are we doing this?
The lambda workaround is not as convenient as having it Just Work. so
it seemed like a useful extension to try, with no downside. It doesn't
even change the is_constructible<thread, ...> result , because the
std::thread constructor isn't constrained, so is_constructible<thread,
...> is true for nearly all arg types anyway. The only
"non-conformance" is that we don't diagnose the
is_invocable_v<decay_t<F>, decay_t<Args>...> requirement if F is a
pointer-to-member-function.
Maybe I should have sent it as an RFC, or just left it in my fork not
pushed to trunk. If a proposal comes before the committee and gets
rejected then we can remove it again.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2021-10-01 20:55 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-01 19:43 [committed] libstdc++: Make std::jthread support pointers to member functions [PR 100612] Jonathan Wakely
2021-10-01 20:25 ` Ville Voutilainen
2021-10-01 20:55 ` Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).