This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH 2/6] nptl: Convert tst-cond11.c to use libsupport
- From: Mike Crowe <mac at mcrowe dot com>
- To: libc-alpha at sourceware dot org
- Date: Wed, 3 Apr 2019 21:55:48 +0100
- Subject: Re: [PATCH 2/6] nptl: Convert tst-cond11.c to use libsupport
- References: <cover.13c2d68f411f9010956e8e52edfbe168964e1e5c.1553797867.git-series.mac@mcrowe.com> <eaf4b0cdf84de5c18643a9d6fe136c2faaf1e771.1553797867.git-series.mac@mcrowe.com> <cfadc041-a35b-2432-f843-705fb2d784aa@linaro.org>
On Wednesday 03 April 2019 at 09:18:56 +0700, Adhemerval Zanella wrote:
> On 29/03/2019 01:31, Mike Crowe wrote:
> > diff --git a/support/timespec.h b/support/timespec.h
> > new file mode 100644
> > index 0000000..d439d62
> > --- /dev/null
> > +++ b/support/timespec.h
> > @@ -0,0 +1,115 @@
[...]
> > +static inline void
> > +timespec_sub (struct timespec *diff, const struct timespec *left,
> > + const struct timespec *right)
> > +{
> > + diff->tv_sec = left->tv_sec - right->tv_sec;
> > + diff->tv_nsec = left->tv_nsec - right->tv_nsec;
> > +
> > + if (diff->tv_nsec < 0)
> > + {
> > + --diff->tv_sec;
> > + diff->tv_nsec += 1000000000;
> > + }
> > +}
> > +
>
> I think we should handle overflow in such tests and assuming always signed
> time_t we can use a simplified version of gnulib (if I get everything right):
>
> ---
> void
> timespec_subtract (struct timespec *r, const struct timespec *a,
> const struct timespec *b)
> {
> time_t rs = a->tv_sec;
> time_t bs = b->tv_sec;
> long int ns = a->tv_nsec - b->tv_nsec;
> long int rns = ns;
>
> if (ns < 0)
> {
> rns = ns + TIMESPEC_HZ;
> if (bs < TYPE_MAXIMUM (time_t))
> bs++;
> else
> {
> rs = TYPE_MINIMUM (time_t);
> rns = 0;
> }
> }
>
> if (!__builtin_sub_overflow (rs, bs, &rs))
> rs -= bs;
> else
> {
> if (rs < 0)
> {
> rs = TYPE_MINIMUM (time_t);
> rns = 0;
> }
> else
> {
> rs = TYPE_MAXIMUM (time_t);
> rns = TIMESPEC_HZ - 1;
> }
> }
>
> *r = (struct timespec) { ns, rns };
> }
That seems to rely on macros from include/intprops.h anyway, so is there
any disadvantage in just using the gnulib version as it stands (along with
a suitable definition of TIMESPEC_HZ)?
But, having said that, this code from gnulib is GPLv3 (and it always has
been), whereas glibc is LGPLv2.1. Surely this means that we can't just copy
the code across?
Mike.