Discussion:
Defining timespec in time.h or sys/time.h?
Steve Ellcey
2017-10-18 21:16:18 UTC
Permalink
I have a question about who defines timespec, time.h or sys/time.h.
I am trying to build blender (which is part of SPEC 2017) and
it has a file, blender/source/blender/blenlib/intern/threads.c,
that basically has:

#include <sys/time.h>
int foo(struct timespec *timeout)
{
return (timeout->tv_sec);
}

If I use my installed compiler (gcc 5.4, libc 2.23) and compile with
-std=c11, this works fine.  If I use top-of-tree GCC and GLIBC and
-std=c11, I get this:

x.c:2:16: warning: ‘struct timespec’ declared inside parameter list will not be visible outside of this definition or declaration
 int foo(struct timespec *timeout)
                ^~~~~~~~
x.c: In function ‘foo’:
x.c:4:17: error: dereferencing pointer to incomplete type ‘struct timespec’
  return (timeout->tv_sec);


It looks like the older sys/time.h would include time.h but the current one
doesn't.  Is this change intentional?  I am guessing it is.  Does the ISO C11
standard says that you need to include time.h and not sys/time.h to get
the timespec structure?  I don't have a copy of the standard but that would
seem to be the implication of this code in the current time.h:

#if defined __USE_POSIX199309 || defined __USE_ISOC11
# include <bits/types/struct_timespec.h>
#endif

Should I (we, if anyone already has contacts) try to get the blender/SPEC code
changed?  I know that I can work around this with -std=gnu99 or -std=gnu11
but I would rather not have to do that and if the code is not correct (for some
definition of correct) I think we should try to get it fixed rather than work
around it.

Steve Ellcey
***@cavium.com
Joseph Myers
2017-10-18 23:27:54 UTC
Permalink
Post by Steve Ellcey
It looks like the older sys/time.h would include time.h but the current one
doesn't.  Is this change intentional?  I am guessing it is.  Does the ISO C11
standard says that you need to include time.h and not sys/time.h to get
the timespec structure?  I don't have a copy of the standard but that would
There is no such header as sys/time.h (or indeed sys/anything.h) in ISO C.
There is also no such header in base POSIX; sys/time.h is XSI only.
sys/time.h is permitted to include sys/select.h which is permitted to
include time.h, but there is no requirement for it to do so.
--
Joseph S. Myers
***@codesourcery.com
Zack Weinberg
2017-10-19 00:44:53 UTC
Permalink
Post by Joseph Myers
Post by Steve Ellcey
It looks like the older sys/time.h would include time.h but the current one
doesn't. Is this change intentional? I am guessing it is. Does the ISO C11
standard says that you need to include time.h and not sys/time.h to get
the timespec structure? I don't have a copy of the standard but that would
There is no such header as sys/time.h (or indeed sys/anything.h) in ISO C.
There is also no such header in base POSIX; sys/time.h is XSI only.
sys/time.h is permitted to include sys/select.h which is permitted to
include time.h, but there is no requirement for it to do so.
Adding to this: struct timespec has always been specified to live in
time.h; it potentially appears in sys/time.h *only* by virtue of the
"is allowed to include" chain through sys/select.h. Also, almost
everything specified by POSIX for sys/time.h is "OBSOLESCENT" as of
Issue 7 (the only exceptions are struct timeval, select(), and
utimes()), which means the header itself is probably on the way out.

(This is maybe a little premature of POSIX, considering that setitimer
can do things timer_* can't, but that's another discussion.)

zw

Loading...