Re: [PATCH] libstdc++: Enable without gthreads

2020-11-20 Thread Jonathan Wakely via Gcc-patches

On 19/11/20 19:08 +, Jonathan Wakely wrote:

On 19/11/20 13:36 +, Jonathan Wakely wrote:

On 16/11/20 14:43 -0800, Thomas Rodgers wrote:

This patch looks good to me.


Committed now.


This patch was also needed, but I don't understand why I didn't see
the FAILs on gcc135 in teh cfarm.


PCH. My test config on gcc135 uses PCH, and so all headers were
included. The host I tested on later uses --disable-libstdcxx-pch and
so saw the failure.

Mystery solved.



Re: [PATCH] libstdc++: Enable without gthreads

2020-11-19 Thread Jonathan Wakely via Gcc-patches

On 19/11/20 13:36 +, Jonathan Wakely wrote:

On 16/11/20 14:43 -0800, Thomas Rodgers wrote:

This patch looks good to me.


Committed now.


This patch was also needed, but I don't understand why I didn't see
the FAILs on gcc135 in teh cfarm.

Anyway, tested x86_64-linux, committed to trunk.



commit 5e6a43158d2e5b26616716c50badedd3400c6bea
Author: Jonathan Wakely 
Date:   Thu Nov 19 16:17:33 2020

libstdc++: Add missing header to some tests

These tests use std::this_thread::sleep_for without including .

libstdc++-v3/ChangeLog:

* testsuite/30_threads/async/async.cc: Include .
* testsuite/30_threads/future/members/93456.cc: Likewise.

diff --git a/libstdc++-v3/testsuite/30_threads/async/async.cc b/libstdc++-v3/testsuite/30_threads/async/async.cc
index 1c779bfbcad4..b06c2553c952 100644
--- a/libstdc++-v3/testsuite/30_threads/async/async.cc
+++ b/libstdc++-v3/testsuite/30_threads/async/async.cc
@@ -22,6 +22,7 @@
 
 
 #include 
+#include 
 #include 
 
 using namespace std;
diff --git a/libstdc++-v3/testsuite/30_threads/future/members/93456.cc b/libstdc++-v3/testsuite/30_threads/future/members/93456.cc
index 8d6a5148ce3c..9d1cbcef0013 100644
--- a/libstdc++-v3/testsuite/30_threads/future/members/93456.cc
+++ b/libstdc++-v3/testsuite/30_threads/future/members/93456.cc
@@ -22,6 +22,7 @@
 
 
 #include 
+#include 
 #include 
 #include 
 #include 


Re: [PATCH] libstdc++: Enable without gthreads

2020-11-19 Thread Tom Tromey
> "Jonathan" == Jonathan Wakely  writes:

Jonathan> Here's a slightly more conservative version of the patch. This moves
Jonathan> std::thread and this_thread::get_id() and this_thread::yield() to a
Jonathan> new header, and makes *most* of std::thread defined without gthreads
Jonathan> (because we need the nested thread::id type to be returned from
Jonathan> this_thread::get_id()). But it doesn't declare the std::thread
Jonathan> constructor that creates new threads.
...
Jonathan> Both this and the previous patch require some GDB changes, because GDB
Jonathan> currently assumes that if std::thread is declared in  that it
Jonathan> is usable and multiple threads are supported. That's no longer true,
Jonathan> because we would declare a useless std::thread after this patch. Tom
Jonathan> Tromey has patches to make GDB handle this though.

It turns out that with this approach, there's nothing to do in gdb,
because luckily the configure check looks to see if the constructor is
usable:

AC_CACHE_CHECK([for std::thread],
   gdb_cv_cxx_std_thread,
   [AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
[[#include 
  void callback() { }]],
[[std::thread t(callback);]])],

I will probably still check in the patch to catch system_error when
starting a thread, though.

thanks,
Tom


Re: [PATCH] libstdc++: Enable without gthreads

2020-11-19 Thread Jonathan Wakely via Gcc-patches

On 16/11/20 14:43 -0800, Thomas Rodgers wrote:

This patch looks good to me.


Committed now.


It would be great to find a way to do a similar refactoring of 
condition_variable.


Yes, probably once stage 1 opens for GCC 12.



On Nov 12, 2020, at 9:07 AM, Jonathan Wakely via Libstdc++ 
 wrote:

On 11/11/20 17:31 +, Jonathan Wakely wrote:

On 11/11/20 16:13 +, Jonathan Wakely wrote:

This makes it possible to use std::thread in single-threaded builds.
All member functions are available, but attempting to create a new
thread will throw an exception.

The main benefit for most targets is that other headers such as 
do not need to include the whole of  just to be able to create a
std::thread. That avoids including  and std::jthread where
not required.


I forgot to mention that this patch also reduces the size of the
 header, by only including  instead of the
whole of . That could be done separately from the rest of the
changes here.

It would be possible to split std::thread and this_thread::get_id()
into a new header without also making them work without gthreads.

That would still reduce the size of the  header, because it
wouldn't need the whole of . But it wouldn't get rid of
preprocessor checks for _GLIBCXX_HAS_GTHREADS in .

Allowing std::this_thread::get_id() and std::this_thread::yield() to
work without threads seems worth doing (we already make
std::this_thread::sleep_until and std::this_thread::sleep_for work
without threads).


Here's a slightly more conservative version of the patch. This moves
std::thread and this_thread::get_id() and this_thread::yield() to a
new header, and makes *most* of std::thread defined without gthreads
(because we need the nested thread::id type to be returned from
this_thread::get_id()). But it doesn't declare the std::thread
constructor that creates new threads.

That means std::thread is present, but you can't even try to create
new threads. This means we don't need to export the std::thread
symbols from libstdc++.so for a target where they are unusable and
just throw an exception.

This still has the main benefits of making  include a lot less
code, and removing some #if conditions in .

One other change from the previous patch worth mentioning is that I've
made  include  so that
std::reference_wrapper (and std::ref and std::cref) are defined by
. That isn't required, but it is a tiny header and being able
to use std::ref to pass lvalues to new threads without including
all of  seems like a kindness to users.

Both this and the previous patch require some GDB changes, because GDB
currently assumes that if std::thread is declared in  that it
is usable and multiple threads are supported. That's no longer true,
because we would declare a useless std::thread after this patch. Tom
Tromey has patches to make GDB handle this though.

Tested powerpc64le-linux, --enable-threads and --disable-threads.

Thoughts?









Re: [PATCH] libstdc++: Enable without gthreads

2020-11-16 Thread Thomas Rodgers
This patch looks good to me.

It would be great to find a way to do a similar refactoring of 
condition_variable.

> On Nov 12, 2020, at 9:07 AM, Jonathan Wakely via Libstdc++ 
>  wrote:
> 
> On 11/11/20 17:31 +, Jonathan Wakely wrote:
>> On 11/11/20 16:13 +, Jonathan Wakely wrote:
>>> This makes it possible to use std::thread in single-threaded builds.
>>> All member functions are available, but attempting to create a new
>>> thread will throw an exception.
>>> 
>>> The main benefit for most targets is that other headers such as 
>>> do not need to include the whole of  just to be able to create a
>>> std::thread. That avoids including  and std::jthread where
>>> not required.
>> 
>> I forgot to mention that this patch also reduces the size of the
>>  header, by only including  instead of the
>> whole of . That could be done separately from the rest of the
>> changes here.
>> 
>> It would be possible to split std::thread and this_thread::get_id()
>> into a new header without also making them work without gthreads.
>> 
>> That would still reduce the size of the  header, because it
>> wouldn't need the whole of . But it wouldn't get rid of
>> preprocessor checks for _GLIBCXX_HAS_GTHREADS in .
>> 
>> Allowing std::this_thread::get_id() and std::this_thread::yield() to
>> work without threads seems worth doing (we already make
>> std::this_thread::sleep_until and std::this_thread::sleep_for work
>> without threads).
> 
> Here's a slightly more conservative version of the patch. This moves
> std::thread and this_thread::get_id() and this_thread::yield() to a
> new header, and makes *most* of std::thread defined without gthreads
> (because we need the nested thread::id type to be returned from
> this_thread::get_id()). But it doesn't declare the std::thread
> constructor that creates new threads.
> 
> That means std::thread is present, but you can't even try to create
> new threads. This means we don't need to export the std::thread
> symbols from libstdc++.so for a target where they are unusable and
> just throw an exception.
> 
> This still has the main benefits of making  include a lot less
> code, and removing some #if conditions in .
> 
> One other change from the previous patch worth mentioning is that I've
> made  include  so that
> std::reference_wrapper (and std::ref and std::cref) are defined by
> . That isn't required, but it is a tiny header and being able
> to use std::ref to pass lvalues to new threads without including
> all of  seems like a kindness to users.
> 
> Both this and the previous patch require some GDB changes, because GDB
> currently assumes that if std::thread is declared in  that it
> is usable and multiple threads are supported. That's no longer true,
> because we would declare a useless std::thread after this patch. Tom
> Tromey has patches to make GDB handle this though.
> 
> Tested powerpc64le-linux, --enable-threads and --disable-threads.
> 
> Thoughts?
> 
> 
> 



Re: [PATCH] libstdc++: Enable without gthreads

2020-11-12 Thread Jonathan Wakely via Gcc-patches

On 11/11/20 17:31 +, Jonathan Wakely wrote:

On 11/11/20 16:13 +, Jonathan Wakely wrote:

This makes it possible to use std::thread in single-threaded builds.
All member functions are available, but attempting to create a new
thread will throw an exception.

The main benefit for most targets is that other headers such as 
do not need to include the whole of  just to be able to create a
std::thread. That avoids including  and std::jthread where
not required.


I forgot to mention that this patch also reduces the size of the
 header, by only including  instead of the
whole of . That could be done separately from the rest of the
changes here.

It would be possible to split std::thread and this_thread::get_id()
into a new header without also making them work without gthreads.

That would still reduce the size of the  header, because it
wouldn't need the whole of . But it wouldn't get rid of
preprocessor checks for _GLIBCXX_HAS_GTHREADS in .

Allowing std::this_thread::get_id() and std::this_thread::yield() to
work without threads seems worth doing (we already make
std::this_thread::sleep_until and std::this_thread::sleep_for work
without threads).


Here's a slightly more conservative version of the patch. This moves
std::thread and this_thread::get_id() and this_thread::yield() to a
new header, and makes *most* of std::thread defined without gthreads
(because we need the nested thread::id type to be returned from
this_thread::get_id()). But it doesn't declare the std::thread
constructor that creates new threads.

That means std::thread is present, but you can't even try to create
new threads. This means we don't need to export the std::thread
symbols from libstdc++.so for a target where they are unusable and
just throw an exception.

This still has the main benefits of making  include a lot less
code, and removing some #if conditions in .

One other change from the previous patch worth mentioning is that I've
made  include  so that
std::reference_wrapper (and std::ref and std::cref) are defined by
. That isn't required, but it is a tiny header and being able
to use std::ref to pass lvalues to new threads without including
all of  seems like a kindness to users.

Both this and the previous patch require some GDB changes, because GDB
currently assumes that if std::thread is declared in  that it
is usable and multiple threads are supported. That's no longer true,
because we would declare a useless std::thread after this patch. Tom
Tromey has patches to make GDB handle this though.

Tested powerpc64le-linux, --enable-threads and --disable-threads.

Thoughts?


commit 68a99d44890957d6c5b128116a6af6bb4bcfaad3
Author: Jonathan Wakely 
Date:   Thu Nov 12 15:26:02 2020

libstdc++: Move std::thread to a new header

This makes it possible to use std::thread without including the whole of
. It also makes this_thread::get_id() and this_thread::yield()
available even when there is no gthreads support (e.g. when GCC is built
with --disable-threads or --enable-threads=single).

In order for the std::thread::id return type of this_thread::get_id() to
be defined, std:thread itself is defined unconditionally. However the
constructor that creates new threads is not defined for single-threaded
builds. The thread::join() and thread::detach() member functions are
defined inline for single-threaded builds and just throw an exception
(because we know the thread cannot be joinable if the constructor that
creates joinable threads doesn't exit).

The thread::hardware_concurrency() member function is also defined
inline and returns 0 (as suggested by the standard when the value "is
not computable or well-defined").

The main benefit for most targets is that other headers such as 
do not need to include the whole of  just to be able to create a
std::thread. That avoids including  and std::jthread where
not required.

This also means we can use this_thread::get_id() and this_thread::yield()
in  instead of using the gthread functions directly. This
removes some preprocessor conditionals, simplifying the code.

libstdc++-v3/ChangeLog:

* include/Makefile.am: Add new  header.
* include/Makefile.in: Regenerate.
* include/std/future: Include new header instead of .
* include/std/stop_token: Include new header instead of
.
(stop_token::_S_yield()): Use this_thread::yield().
(_Stop_state_t::_M_requester): Change type to std::thread::id.
(_Stop_state_t::_M_request_stop()): Use this_thread::get_id().
(_Stop_state_t::_M_remove_callback(_Stop_cb*)): Likewise.
Use __is_single_threaded() to decide whether to synchronize.
* include/std/thread (thread, operator==, this_thread::get_id)
(this_thread::yield): Move to new header.
(operator<=>, operator!=, operator<, operator<=, 

Re: [PATCH] libstdc++: Enable without gthreads

2020-11-11 Thread Jonathan Wakely via Gcc-patches

On 11/11/20 16:13 +, Jonathan Wakely wrote:

This makes it possible to use std::thread in single-threaded builds.
All member functions are available, but attempting to create a new
thread will throw an exception.

The main benefit for most targets is that other headers such as 
do not need to include the whole of  just to be able to create a
std::thread. That avoids including  and std::jthread where
not required.


I forgot to mention that this patch also reduces the size of the
 header, by only including  instead of the
whole of . That could be done separately from the rest of the
changes here.

It would be possible to split std::thread and this_thread::get_id()
into a new header without also making them work without gthreads.

That would still reduce the size of the  header, because it
wouldn't need the whole of . But it wouldn't get rid of
preprocessor checks for _GLIBCXX_HAS_GTHREADS in .

Allowing std::this_thread::get_id() and std::this_thread::yield() to
work without threads seems worth doing (we already make
std::this_thread::sleep_until and std::this_thread::sleep_for work
without threads).



[PATCH] libstdc++: Enable without gthreads

2020-11-11 Thread Jonathan Wakely via Gcc-patches
This makes it possible to use std::thread in single-threaded builds.
All member functions are available, but attempting to create a new
thread will throw an exception.

The main benefit for most targets is that other headers such as 
do not need to include the whole of  just to be able to create a
std::thread. That avoids including  and std::jthread where
not required.

This also means we can use std::thread::id in  instead of
using the gthread wrappers directly.

libstdc++-v3/ChangeLog:

* include/Makefile.am: Add new  header.
* include/Makefile.in: Regenerate.
* include/std/future: Include new header instead of .
* include/std/stop_token: Include new header instead of
.
(stop_token::_S_yield()): Use this_thread::yield().
(_Stop_state_t::_M_requester): Change type to std::thread::id.
(_Stop_state_t::_M_request_stop()): Use this_thread::get_id().
(_Stop_state_t::_M_remove_callback(_Stop_cb*)): Likewise.
Use __is_single_threaded() to decide whether to synchronize.
* include/std/thread (thread, operator==, this_thread::get_id)
(this_thread::yield): Move to new header.
(operator<=>, operator!=, operator<, operator<=, operator>)
(operator>=, hash, operator<<): Define even when
gthreads not available.
* src/c++11/thread.cc (_GLIBCXX_NPROCS): Define to 0 when
gthreads not available.
(thread::_State::~_State(), thread::join(), thread::detach())
(thread::_M_start_thread(_State_ptr, void(*)()))
(thread::hardware_concurrency()): Define even when gthreads
not available.
* include/bits/std_thread.h: New file.
(thread, operator==, this_thread::get_id, this_thread::yield):
Define even when gthreads not available.

I'm sending this for consideration, I haven't pushed it.

This removes a number of ugly preprocessor checks for
_GLIBCXX_HAS_GTHREADS because things like std::this_thread::get_id()
and std::this_thread::yield() are always available.

The patch is missing changes to the testsuite to remove some (but
certainly not all) of the { dg-require-gthreads "" } directives. That
hasn't been done yet because it isn't sufficient. The testsuite also
filters out any test with the string "thread" in its path (see
testsuite/libstdc++-dg/conformance.exp) so that needs to be changed if
we want any tests under 30_threads to run for non-gthreads targets.

A follow-up patch could make futures and promises available on targets
with no gthreads support. For example, to use with coroutines. That
needs a little more work, as we'd need a non-gthreads version of
__atomic_futex_unsigned.

Thoughts?

commit 8bd28626e5f9b28192d0e868e922f26d4f40187e
Author: Jonathan Wakely 
Date:   Wed Nov 11 15:43:03 2020

libstdc++: Enable  without gthreads

This makes it possible to use std::thread in single-threaded builds.
All member functions are available, but attempting to create a new
thread will throw an exception.

The main benefit for most targets is that other headers such as 
do not need to include the whole of  just to be able to create a
std::thread. That avoids including  and std::jthread where
not required.

This also means we can use std::thread::id in  instead of
using the gthread wrappers directly.

libstdc++-v3/ChangeLog:

* include/Makefile.am: Add new  header.
* include/Makefile.in: Regenerate.
* include/std/future: Include new header instead of .
* include/std/stop_token: Include new header instead of
.
(stop_token::_S_yield()): Use this_thread::yield().
(_Stop_state_t::_M_requester): Change type to std::thread::id.
(_Stop_state_t::_M_request_stop()): Use this_thread::get_id().
(_Stop_state_t::_M_remove_callback(_Stop_cb*)): Likewise.
Use __is_single_threaded() to decide whether to synchronize.
* include/std/thread (thread, operator==, this_thread::get_id)
(this_thread::yield): Move to new header.
(operator<=>, operator!=, operator<, operator<=, operator>)
(operator>=, hash, operator<<): Define even when
gthreads not available.
* src/c++11/thread.cc (_GLIBCXX_NPROCS): Define to 0 when
gthreads not available.
(thread::_State::~_State(), thread::join(), thread::detach())
(thread::_M_start_thread(_State_ptr, void(*)()))
(thread::hardware_concurrency()): Define even when gthreads
not available.
* include/bits/std_thread.h: New file.
(thread, operator==, this_thread::get_id, this_thread::yield):
Define even when gthreads not available.

diff --git a/libstdc++-v3/include/Makefile.am b/libstdc++-v3/include/Makefile.am
index 292d89da8ba7..7979f1c589d6 100644
--- a/libstdc++-v3/include/Makefile.am
+++