From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org
Subject: Re: Proposed patch to skip the last atomic decrements in _Sp_counted_base::_M_release
Date: Mon, 7 Dec 2020 15:41:51 +0000 [thread overview]
Message-ID: <20201207154151.GB4100713@redhat.com> (raw)
In-Reply-To: <cad0c97e-4c8d-3849-7ea7-41461cba4aa0@stedding.saclay.inria.fr>
On 07/12/20 16:28 +0100, Marc Glisse wrote:
>Thanks for doing this!
>
>As someone who recently wrote
>
> if (count.load(std::memory_order_relaxed) == 1
> || count.fetch_sub(1, std::memory_order_release) == 1) {
> std::atomic_thread_fence(std::memory_order_acquire);
> // destroy here
> }
>
>for a much simpler reference-counted type without weak references, I am
>happy to see that this can generalize to the more complicated case of
>shared_ptr.
>
>Yes, hardening it may be a bit hard. Some checks with alignof/offsetof
>could make sense, but it is complicated by the fact that things are
>happening in the base class that depend on the derived class. Some
>allocators guarantee a minimum alignment, likely sufficient for your
>case, but again I am not sure how to test for that exactly.
>
>ISTR that libstdc++ uses a surprisingly strong memory barrier when
>incrementing references (acq_rel, where I would expect acquire to
>suffice, although I haven't thought about weak references that much).
>Did you also experiment with that?
That's because we go through the dispatcher functions in
<ext/atomicity.h> which don't take a memory_order argument so use a
conservative ordering. That could be improved.
The dispatchers are beneficial (especially with glibc 2.32 or later)
because they downgrade the atomic ops to non-atomic equivalents when
the program only has a single thread. The proposed patch loses that
benefit, meaning that the 8-byte load will always be done using an
atomic instruction, even when not needed. That should be improved too.
next prev parent reply other threads:[~2020-12-07 15:41 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-07 12:24 Maged Michael
2020-12-07 15:28 ` Marc Glisse
2020-12-07 15:41 ` Jonathan Wakely [this message]
2020-12-08 4:10 ` Maged Michael
2020-12-08 8:01 ` Marc Glisse
2020-12-08 9:56 ` Jonathan Wakely
2020-12-07 15:37 ` Jonathan Wakely
2020-12-08 4:06 ` Maged Michael
2020-12-07 12:30 Maged Michael
2020-12-07 15:57 ` Jonathan Wakely
2020-12-07 16:23 ` Jonathan Wakely
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=20201207154151.GB4100713@redhat.com \
--to=jwakely@redhat.com \
--cc=libstdc++@gcc.gnu.org \
/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).