All of lore.kernel.org
 help / color / mirror / Atom feed
From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
To: Helmut Grohne <helmut.grohne@intenta.de>
Cc: Bartosz Golaszewski <brgl@bgdev.pl>,
	Kent Gibson <warthog618@gmail.com>,
	"linux-gpio@vger.kernel.org" <linux-gpio@vger.kernel.org>
Subject: Re: [libgpiod][RFC PATCH] bindings: cxx: demote the line's parent chip reference to a weak_ptr
Date: Mon, 19 Oct 2020 15:06:18 +0200	[thread overview]
Message-ID: <CAMpxmJU0y5Zze3we-NjnLi1fCG69v38fMwvTgCe0JXGK+RxLNQ@mail.gmail.com> (raw)
In-Reply-To: <20201019123801.GA5116@laureti-dev>

On Mon, Oct 19, 2020 at 2:38 PM Helmut Grohne <helmut.grohne@intenta.de> wrote:
>
> On Mon, Oct 19, 2020 at 02:17:49PM +0200, Bartosz Golaszewski wrote:
> > On Fri, Oct 16, 2020 at 3:38 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
> > >
> > > On Fri, Oct 16, 2020 at 12:29 PM Helmut Grohne <helmut.grohne@intenta.de> wrote:
> > > >
> > >
> > > [snip]
> > >
> > > > > +chip::chip(const ::std::weak_ptr<::gpiod_chip>& chip_ptr)
> > > > > +     : _m_chip(chip_ptr)
> > > > > +{
> > > > > +
> > > > > +}
> > > > > +
> > > >
> > > > I think what happens here is that you upgrade a weak_ptr to a
> > > > shared_ptr. Wouldn't it be more natural to request a
> > > >
> > > >     ::std::shared_ptr<::gpiod_chip> &&
> > > >
> > > > here and thus make the ownership-taking more explicit? It would be done
> > > > on the caller-side and thus be more transparent. Stuffing weak_ptrs
> > > > should continue to work.
> > > >
> > >
> > > Sure, sounds good.
> > >
> >
> > After a second look - I'm not sure if this is actually better. By
> > taking weak_ptr reference as argument we benefit from implicit
> > conversion to shared_ptr via shared_ptr's constructor taking weak_ptr
> > as argument. What you propose would require us to always instantiate a
> > shared_ptr in the argument list when calling the chip's constructor
> > and makes code uglier in the end IMO.
>
> On a second look, the use of an rvalue reference is suboptimal indeed.
> The idea behind my change was this: Since chip stores a shared_ptr, it
> can as well consume one. Instead of what I proposed, it should simply
> take it by value (not rvalue):
>
>     ::std::shared_ptr<::gpiod_chip>
>
> An existing shared_ptr can be moved into the constructor and then moved
> into the member variable. Doing so allows passing a shared_ptr around
> without touching reference counts (which are prone to cache line
> bouncing). When passing it by value, the implicit conversion from
> weak_ptr should work again. Thus the caller would increase the reference
> count and the chip would merely gain ownership of the shared_ptr and
> move it around.
>
> For reference, see Scott Myers' Effective Modern C++ "Item 41: Consider
> pass by value for copyable parameters that are cheap to move and always
> copied."
>
> So yeah, it doesn't work the way I wrote initially, because I added the
> rvalue reference.
>
> Helmut

But this still forces us to do

    return chip(::std::shared_ptr<::gpiod_chip>(this->_m_owner));

instead of a much more elegant

    return chip(this->_m_owner);

in line.cpp and there's an even less elegant thing in iter.cpp. Or am
I missing something?

Bartosz

  reply	other threads:[~2020-10-19 13:06 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-16  9:09 [libgpiod][RFC PATCH] bindings: cxx: demote the line's parent chip reference to a weak_ptr Bartosz Golaszewski
2020-10-16 10:29 ` Helmut Grohne
2020-10-16 13:38   ` Bartosz Golaszewski
2020-10-19 12:17     ` Bartosz Golaszewski
2020-10-19 12:38       ` Helmut Grohne
2020-10-19 13:06         ` Bartosz Golaszewski [this message]
2020-10-20  5:57           ` Helmut Grohne
2020-10-20  6:58             ` Bartosz Golaszewski
2020-11-04 15:45               ` Bartosz Golaszewski

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=CAMpxmJU0y5Zze3we-NjnLi1fCG69v38fMwvTgCe0JXGK+RxLNQ@mail.gmail.com \
    --to=bgolaszewski@baylibre.com \
    --cc=brgl@bgdev.pl \
    --cc=helmut.grohne@intenta.de \
    --cc=linux-gpio@vger.kernel.org \
    --cc=warthog618@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.