public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Thomas Rodgers <rodgert@appliantology.com>
To: Jonathan Wakely <jwakely@redhat.com>
Cc: libstdc++ <libstdc++@gcc.gnu.org>,
	gcc-patches List <gcc-patches@gcc.gnu.org>,
	tom@tromey.com
Subject: Re: [PATCH] libstdc++: Enable <thread> without gthreads
Date: Mon, 16 Nov 2020 14:43:35 -0800	[thread overview]
Message-ID: <C6A151A6-68FB-4CB5-8B6B-86CD9F1A6E78@appliantology.com> (raw)
In-Reply-To: <20201112170745.GC503596@redhat.com>

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++ <libstdc++@gcc.gnu.org> wrote:
> 
> On 11/11/20 17:31 +0000, Jonathan Wakely wrote:
>> On 11/11/20 16:13 +0000, 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 <future>
>>> do not need to include the whole of <thread> just to be able to create a
>>> std::thread. That avoids including <stop_token> and std::jthread where
>>> not required.
>> 
>> I forgot to mention that this patch also reduces the size of the
>> <thread> header, by only including <bits/unique_ptr.h> instead of the
>> whole of <memory>. 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 <future> header, because it
>> wouldn't need the whole of <thread>. But it wouldn't get rid of
>> preprocessor checks for _GLIBCXX_HAS_GTHREADS in <stop_token>.
>> 
>> 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 <future> include a lot less
> code, and removing some #if conditions in <stop_token>.
> 
> One other change from the previous patch worth mentioning is that I've
> made <bits/std_thread.h> include <bits/refwrap.h> so that
> std::reference_wrapper (and std::ref and std::cref) are defined by
> <thread>. 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 <functional> 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 <thread> 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?
> 
> 
> <patch.txt>


  reply	other threads:[~2020-11-16 22:43 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-11 16:13 Jonathan Wakely
2020-11-11 17:31 ` Jonathan Wakely
2020-11-12 17:07   ` Jonathan Wakely
2020-11-16 22:43     ` Thomas Rodgers [this message]
2020-11-19 13:36       ` Jonathan Wakely
2020-11-19 19:08         ` Jonathan Wakely
2020-11-20 11:52           ` Jonathan Wakely
2020-11-19 18:25     ` Tom Tromey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=C6A151A6-68FB-4CB5-8B6B-86CD9F1A6E78@appliantology.com \
    --to=rodgert@appliantology.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=tom@tromey.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).