public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
To: Matthias Kretz <m.kretz@gsi.de>
Cc: "libstdc++" <libstdc++@gcc.gnu.org>,
	gcc Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH v2] libstdc++: Improve std::lock algorithm
Date: Tue, 22 Jun 2021 20:51:03 +0100	[thread overview]
Message-ID: <CACb0b4k0UTK=c3f=02gKDjvngmbUKoJsWq40y-xmavn6oZ+N6g@mail.gmail.com> (raw)
In-Reply-To: <9108135.T7Z3S40VBb@minbar>

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.


  reply	other threads:[~2021-06-22 19:51 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 [this message]
2021-06-22 20:32             ` [PATCH v3] " Jonathan Wakely
2021-06-23  7:21               ` Christophe Lyon
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='CACb0b4k0UTK=c3f=02gKDjvngmbUKoJsWq40y-xmavn6oZ+N6g@mail.gmail.com' \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --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).