From: Helmut Grohne <helmut.grohne@intenta.de>
To: Bartosz Golaszewski <brgl@bgdev.pl>
Cc: "open list:GPIO SUBSYSTEM" <linux-gpio@vger.kernel.org>,
"Bartosz Golaszewski" <bartekgola@gmail.com>
Subject: Re: [libgpiod] cxx bindings: time_point vs duration
Date: Thu, 15 Oct 2020 11:35:26 +0200 [thread overview]
Message-ID: <20201015093526.GA10891@laureti-dev> (raw)
In-Reply-To: <CAMRc=Md=ZeKLS-SKKgmq+V9VCt+7xdjNiwz89Ms-vOeTcwZBOw@mail.gmail.com>
Hi,
On Thu, Oct 15, 2020 at 11:26:47AM +0200, Bartosz Golaszewski wrote:
> I probably just didn't know any better. :) I'm a kernel developer and
> writing these bindings was basically me learning C++.
Oh, ok. Then let me elaborte on the difference of duration and
time_point a little and explain the implications of changing the type.
A duration denots an interval in time. A time_point denotes a specific
point in time with respect to a particular clock. Every clock has an
epoch and you can convert a time_stamp to a duration by computing the
time_since_epoch. Comparing time_points with different clocks is thus
meaningless. Internally a time_point is represented exactly like a
duration. It just has the connection to the clock.
So what clock would you use here? There are a few standard clocks such
as std::chrono::steady_clock. This clock has an unspecified epoch. The
epoch will not change during use, but e.g. after a reboot of the system,
the epoch may be different. A clock also knows whether time increases
monotonically and a steady_clock does. On the other hand, the
system_clock has a well-specified epoch, but it is not required to be
steady. If you change the time on your machine, your system_clock may go
backwards. While system_clock may be what we want here, it has an
unspecified representation. libgpiod wants a specific representation
though to preserve the high precision of the included timestamps. We
therefore cannot use any of the standard clocks.
libgpiod should add its own clock class. The clock is never instaniated.
It is more a collection of functionality and a tag to be included in
templates to differentiate types. I think your clock would look like
this:
struct gpiod_clock {
using duration = std::chrono::nanoseconds;
using rep = duration::rep;
using period = duration::period;
using time_point = std::chrono::time_point<gpiod_clock>;
static constexpr bool is_steady = std::chrono::system_clock::is_steady;
static time_point now() {
return time_point(std::chrono::system_clock::now().time_since_epoch());
}
};
(The code might not work as is, but you get the idea.)
In essence, it's a system_clock with a different underlying duration
type for representing high resolution timestamps.
Even though changing the type does likely not change the binary
representation of timestamps, it still consitutes an API break and an
ABI break (due to changed symbol names).
> Thanks for the suggestion - it's a good moment to make it, because
> we're in the process of changing the API to accommodate the new uAPI
> that will be released in v5.10 so I'll definitely make sure to change
> it too.
Ok. I'm not particularly enthusiastic about updating client code all the
time for covering upstream API breaks, but sometimes it is unavoidable.
Seems to be the case here.
> Are you by any chance well versed in C++ and would like to help out by
> giving me some advice? I want to fix the way GPIO line objects
> reference their owning chips but I'm not sure how to.
I would consider myself experienced in C++.
As far as I can see, the chip class uses the pimpl pattern, so a chip
practically is a reference counted pointer to the actual gpiod_chip. A
line has a plain chip member and this seems totally fine. As long as the
line exists, so does the underlying chip. Is this not what you intended?
What is less clear to me is the connection between a line and its
underlying gpiod_line. It is unclear to me who "owns" a gpiod_line and
who is responsible for disposing it. Since the line class is copy
constructible, the line class clearly cannot own a gpiod_line. I would
question whether this is a good decision. I'm not sure that a line must
be copyable. It should be movable. So if you delete the copy constructor
and the copy assignment for the line class, then you could argue that a
line owns its referenced gpiod_line and then it could automatically
handle release of resources upon destruction.
Does this help?
Helmut
next prev parent reply other threads:[~2020-10-15 9:35 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-15 8:38 [libgpiod] cxx bindings: time_point vs duration Helmut Grohne
2020-10-15 9:26 ` Bartosz Golaszewski
2020-10-15 9:35 ` Helmut Grohne [this message]
2020-10-15 10:05 ` Bartosz Golaszewski
2020-10-15 10:57 ` Helmut Grohne
2020-10-15 11:43 ` Bartosz Golaszewski
2020-10-15 12:13 ` Helmut Grohne
2020-10-15 12:16 ` Bartosz Golaszewski
2020-10-21 13:57 ` Jack Winch
2020-10-21 14:35 ` Jack Winch
2020-10-21 15:14 ` Bartosz Golaszewski
2020-10-21 15:44 ` Jack Winch
2020-10-22 6:39 ` Helmut Grohne
2020-10-22 9:09 ` Jack Winch
2020-10-22 9:35 ` Bartosz Golaszewski
2020-10-22 9:47 ` Jack Winch
2020-10-22 11:55 ` Bartosz Golaszewski
2020-10-22 12:22 ` Jack Winch
2020-10-23 16:22 ` 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=20201015093526.GA10891@laureti-dev \
--to=helmut.grohne@intenta.de \
--cc=bartekgola@gmail.com \
--cc=brgl@bgdev.pl \
--cc=linux-gpio@vger.kernel.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 NNTP newsgroup(s).