public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Christophe Lyon <christophe.lyon@linaro.org>
To: Jonathan Wakely <jwakely@redhat.com>
Cc: Matthias Kretz <m.kretz@gsi.de>,
	"libstdc++" <libstdc++@gcc.gnu.org>,
	 gcc Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH v3] libstdc++: Improve std::lock algorithm
Date: Wed, 23 Jun 2021 09:21:15 +0200	[thread overview]
Message-ID: <CAKdteOYJHRPd5SYM1uvZ1SyUBtYazF6VouC3w+SgOJPLB-6guw@mail.gmail.com> (raw)
In-Reply-To: <CACb0b4k0fY2vQLfQhKN2SuGoMjawBjm2eJ+5u9ev8T44sG421w@mail.gmail.com>

Hi Jonathan,

On Tue, 22 Jun 2021 at 22:34, Jonathan Wakely via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> On Tue, 22 Jun 2021 at 20:51, Jonathan Wakely wrote:
> >
> > On Tue, 22 Jun 2021 at 17:03, Matthias Kretz <m.kretz@gsi.de> wrote:
> > >
> > > On Dienstag, 22. Juni 2021 17:20:41 CEST Jonathan Wakely wrote:
> > > > On Tue, 22 Jun 2021 at 14:21, Matthias Kretz wrote:
> > > > > This does a try_lock on all lockabes even if any of them fails. I think
> > > > > that's
> > > > > not only more expensive but also non-conforming. I think you need to defer
> > > > > locking and then loop from beginning to end to break the loop on the first
> > > > > unsuccessful try_lock.
> > > >
> > > > Oops, good point. I'll add a test for that too. Here's the fixed code:
> > > >
> > > >     template<typename _L0, typename... _Lockables>
> > > >       inline int
> > > >       __try_lock_impl(_L0& __l0, _Lockables&... __lockables)
> > > >       {
> > > > #if __cplusplus >= 201703L
> > > >         if constexpr ((is_same_v<_L0, _Lockables> && ...))
> > > >           {
> > > >             constexpr int _Np = 1 + sizeof...(_Lockables);
> > > >             unique_lock<_L0> __locks[_Np] = {
> > > >                 {__l0, defer_lock}, {__lockables, defer_lock}...
> > > >             };
> > > >             for (int __i = 0; __i < _Np; ++__i)
> > >
> > > I thought coding style requires a { here?
> >
> > Maybe for the compiler, but I don't think libstdc++ has such a rule. I
> > can add the braces though, it's probably better.
> >
> > >
> > > >               if (!__locks[__i].try_lock())
> > > >                 {
> > > >                   const int __failed = __i;
> > > >                   while (__i--)
> > > >                     __locks[__i].unlock();
> > > >                   return __i;
> > >
> > > You meant `return __failed`?
> >
> > Yep, copy&paste error while trying to avoid the TABs in the real code
> > screwing up the gmail formatting :-(
> >
> >
> > > >                 }
> > > >             for (auto& __l : __locks)
> > > >               __l.release();
> > > >             return -1;
> > > >           }
> > > >         else
> > > > #endif
> > > >
> > > > > [...]
> > > > > Yes, if only we had a wrapping integer type that wraps at an arbitrary N.
> > > > > Like
> > > > >
> > > > > unsigned int but with parameter, like:
> > > > >   for (__wrapping_uint<_Np> __k = __idx; __k != __first; --__k)
> > > > >
> > > > >     __locks[__k - 1].unlock();
> > > > >
> > > > > This is the loop I wanted to write, except --__k is simpler to write and
> > > > > __k -
> > > > > 1 would also wrap around to _Np - 1 for __k == 0. But if this is the only
> > > > > place it's not important enough to abstract.
> > > >
> > > > We might be able to use __wrapping_uint in std::seed_seq::generate too, and
> > > > maybe some other places in <random>. But we can add that later if we decide
> > > > it's worth it.
> > >
> > > OK.
> > >
> > > > > I also considered moving it down here. Makes sense unless you want to call
> > > > > __detail::__lock_impl from other functions. And if we want to make it work
> > > > > for
> > > > > pre-C++11 we could do
> > > > >
> > > > >   using __homogeneous
> > > > >
> > > > >     = __and_<is_same<_L1, _L2>, is_same<_L1, _L3>...>;
> > > > >
> > > > >   int __i = 0;
> > > > >   __detail::__lock_impl(__homogeneous(), __i, 0, __l1, __l2, __l3...);
> > > >
> > > > We don't need tag dispatching, we could just do:
> > > >
> > > > if _GLIBCXX17_CONSTEXPR (homogeneous::value)
> > > >  ...
> > > > else
> > > >  ...
> > > >
> > > > because both branches are valid for the homogeneous case, i.e. we aren't
> > > > using if-constexpr to avoid invalid instantiations.
> > >
> > > But for the inhomogeneous case the homogeneous code is invalid (initialization
> > > of C-array of unique_lock<_L1>).
> >
> > Oops, yeah of course.
> >
> > >
> > > > But given that the default -std option is gnu++17 now, I'm OK with the
> > > > iterative version only being used for C++17.
> > >
> > > Fair enough.
>
> Here's what I've tested and pushed to trunk. Thanks for the
> improvement and comments.

This patch causes GCC build failures for bare-metal targets
(aarch64-elf, arm-eabi). For instance on arm-eabi, I'm seeing:

In file included from
/tmp/1229695_7.tmpdir/aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/arm-none-eabi/libstdc++-v3/include/future:38,
                 from
/tmp/1229695_7.tmpdir/aci-gcc-fsf/sources/gcc-fsf/gccsrc/libstdc++-v3/include/precompiled/stdc++.h:105:
/tmp/1229695_7.tmpdir/aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/arm-none-eabi/libstdc++-v3/include/mutex:
In function 'int std::__detail::__try_lock_impl(_Lockable&)':
/tmp/1229695_7.tmpdir/aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/arm-none-eabi/libstdc++-v3/include/mutex:522:53:
error: expected primary-expression before ',' token
  522 |         if (unique_lock<_Lockable> __lock{__lockable, try_to_lock})
      |                                                     ^
In file included from
/tmp/1229695_7.tmpdir/aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/arm-none-eabi/libstdc++-v3/include/future:38,
                 from
/tmp/1229695_7.tmpdir/aci-gcc-fsf/sources/gcc-fsf/gccsrc/libstdc++-v3/include/precompiled/stdc++.h:105:
/tmp/1229695_7.tmpdir/aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/arm-none-eabi/libstdc++-v3/include/mutex:
In function 'int std::__detail::__try_lock_impl(_Lockable&)':
/tmp/1229695_7.tmpdir/aci-gcc-fsf/builds/gcc-fsf-gccsrc/obj-arm-none-eabi/gcc3/arm-none-eabi/libstdc++-v3/include/mutex:522:53:
error: expected primary-expression before ',' token
  522 |         if (unique_lock<_Lockable> __lock{__lockable, try_to_lock})
      |                                                     ^
make[4]: *** [Makefile:1862:
arm-none-eabi/bits/stdc++.h.gch/O2ggnu++0x.gch] Error 1

Can you have a look?

Thanks

Christophe

  reply	other threads:[~2021-06-23  7:21 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-21 17:31 [committed] " Jonathan Wakely
2021-06-22  9:07 ` Matthias Kretz
2021-06-22 12:51   ` [PATCH v2] " Jonathan Wakely
2021-06-22 13:21     ` Matthias Kretz
2021-06-22 15:20       ` Jonathan Wakely
2021-06-22 16:03         ` Matthias Kretz
2021-06-22 19:51           ` Jonathan Wakely
2021-06-22 20:32             ` [PATCH v3] " Jonathan Wakely
2021-06-23  7:21               ` Christophe Lyon [this message]
2021-06-23  9:17                 ` Jonathan Wakely
2021-06-23  9:43                   ` Christophe LYON
2021-06-23 10:11                     ` Jonathan Wakely
2021-06-23 11:16                       ` Christophe LYON

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=CAKdteOYJHRPd5SYM1uvZ1SyUBtYazF6VouC3w+SgOJPLB-6guw@mail.gmail.com \
    --to=christophe.lyon@linaro.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jwakely@redhat.com \
    --cc=libstdc++@gcc.gnu.org \
    --cc=m.kretz@gsi.de \
    /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).