libc: Add timespec_getres(3) as per C23.
This also adds support for TIME_MONOTONIC to timespec_get(3). Reviewed by: allanjude Differential Revision: https://reviews.freebsd.org/D41524
This commit is contained in:
parent
b8b6bef43f
commit
9b5d724cad
@ -182,9 +182,15 @@ time_t posix2time(time_t t);
|
|||||||
#if defined(__BSD_VISIBLE) || __ISO_C_VISIBLE >= 2011 || \
|
#if defined(__BSD_VISIBLE) || __ISO_C_VISIBLE >= 2011 || \
|
||||||
(defined(__cplusplus) && __cplusplus >= 201703)
|
(defined(__cplusplus) && __cplusplus >= 201703)
|
||||||
#include <sys/_timespec.h>
|
#include <sys/_timespec.h>
|
||||||
/* ISO/IEC 9899:201x 7.27.2.5 The timespec_get function */
|
/* ISO/IEC 9899:2011 7.27.2.5 The timespec_get function */
|
||||||
#define TIME_UTC 1 /* time elapsed since epoch */
|
#define TIME_UTC 1 /* time elapsed since epoch */
|
||||||
int timespec_get(struct timespec *ts, int base);
|
int timespec_get(struct timespec *ts, int base);
|
||||||
|
#if defined (__BSD_VISIBLE) || __ISO_C_VISIBLE >= 2023
|
||||||
|
/* ISO/IEC 9899:2024 7.29.1 Components of time */
|
||||||
|
#define TIME_MONOTONIC 2 /* monotonic time */
|
||||||
|
/* ISO/IEC 9899:2024 7.29.2.7 The timespec_getres function */
|
||||||
|
int timespec_getres(struct timespec *, int);
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
__END_DECLS
|
__END_DECLS
|
||||||
|
@ -156,6 +156,7 @@ SRCS+= __getosreldate.c \
|
|||||||
time.c \
|
time.c \
|
||||||
times.c \
|
times.c \
|
||||||
timespec_get.c \
|
timespec_get.c \
|
||||||
|
timespec_getres.c \
|
||||||
timezone.c \
|
timezone.c \
|
||||||
tls.c \
|
tls.c \
|
||||||
ttyname.c \
|
ttyname.c \
|
||||||
@ -319,6 +320,7 @@ MAN+= alarm.3 \
|
|||||||
time.3 \
|
time.3 \
|
||||||
times.3 \
|
times.3 \
|
||||||
timespec_get.3 \
|
timespec_get.3 \
|
||||||
|
timespec_getres.3 \
|
||||||
timezone.3 \
|
timezone.3 \
|
||||||
ttyname.3 \
|
ttyname.3 \
|
||||||
tzset.3 \
|
tzset.3 \
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
.\" POSSIBILITY OF SUCH DAMAGE.
|
.\" POSSIBILITY OF SUCH DAMAGE.
|
||||||
.\"
|
.\"
|
||||||
.Dd August 10, 2018
|
.Dd August 21, 2023
|
||||||
.Dt TIMESPEC_GET 3
|
.Dt TIMESPEC_GET 3
|
||||||
.Os
|
.Os
|
||||||
.Sh NAME
|
.Sh NAME
|
||||||
@ -55,6 +55,14 @@ In
|
|||||||
.Fx ,
|
.Fx ,
|
||||||
this corresponds to
|
this corresponds to
|
||||||
.Dv CLOCK_REALTIME .
|
.Dv CLOCK_REALTIME .
|
||||||
|
.Pp
|
||||||
|
The base
|
||||||
|
.Dv TIME_MONOTONIC
|
||||||
|
returns a monotonically-increasing time since an unspecified point in the past.
|
||||||
|
In
|
||||||
|
.Fx ,
|
||||||
|
this corresponds to
|
||||||
|
.Dv CLOCK_MONOTONIC .
|
||||||
.Sh RETURN VALUES
|
.Sh RETURN VALUES
|
||||||
The
|
The
|
||||||
.Nm
|
.Nm
|
||||||
@ -66,7 +74,8 @@ on failure.
|
|||||||
.Sh SEE ALSO
|
.Sh SEE ALSO
|
||||||
.Xr clock_gettime 2 ,
|
.Xr clock_gettime 2 ,
|
||||||
.Xr gettimeofday 2 ,
|
.Xr gettimeofday 2 ,
|
||||||
.Xr time 3
|
.Xr time 3 ,
|
||||||
|
.Xr timespec_getres 3
|
||||||
.Sh STANDARDS
|
.Sh STANDARDS
|
||||||
The
|
The
|
||||||
.Nm
|
.Nm
|
||||||
@ -76,6 +85,10 @@ of
|
|||||||
.Dv TIME_UTC
|
.Dv TIME_UTC
|
||||||
conforms to
|
conforms to
|
||||||
.St -isoC-2011 .
|
.St -isoC-2011 .
|
||||||
|
.\" The
|
||||||
|
.\" .Dv TIME_MONOTONIC
|
||||||
|
.\" base conforms to
|
||||||
|
.\" -isoC-2023 .
|
||||||
.Sh HISTORY
|
.Sh HISTORY
|
||||||
This interface first appeared in
|
This interface first appeared in
|
||||||
.Fx 12 .
|
.Fx 12 .
|
||||||
|
@ -44,6 +44,10 @@ timespec_get(struct timespec *ts, int base)
|
|||||||
if (clock_gettime(CLOCK_REALTIME, ts) == -1)
|
if (clock_gettime(CLOCK_REALTIME, ts) == -1)
|
||||||
return 0;
|
return 0;
|
||||||
break;
|
break;
|
||||||
|
case TIME_MONOTONIC:
|
||||||
|
if (clock_gettime(CLOCK_MONOTONIC, ts) == -1)
|
||||||
|
return 0;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
51
lib/libc/gen/timespec_getres.3
Normal file
51
lib/libc/gen/timespec_getres.3
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
.\"-
|
||||||
|
.\" Copyright (c) 2023 Dag-Erling Smørgrav
|
||||||
|
.\"
|
||||||
|
.\" SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
.\"
|
||||||
|
.Dd August 21, 2023
|
||||||
|
.Dt TIMESPEC_GETRES 3
|
||||||
|
.Os
|
||||||
|
.Sh NAME
|
||||||
|
.Nm timespec_getres
|
||||||
|
.Nd get clock resolution
|
||||||
|
.Sh LIBRARY
|
||||||
|
.Lb libc
|
||||||
|
.Sh SYNOPSIS
|
||||||
|
.In time.h
|
||||||
|
.Ft int
|
||||||
|
.Fn timespec_getres "struct timespec *ts" "int base"
|
||||||
|
.Sh DESCRIPTION
|
||||||
|
If
|
||||||
|
.Fa ts
|
||||||
|
is non-null and
|
||||||
|
.Fa base
|
||||||
|
refers to a supported time base as described in
|
||||||
|
.Xr timespec_get 3 ,
|
||||||
|
the
|
||||||
|
.Nm
|
||||||
|
function fills in the structure pointed to by
|
||||||
|
.Fa ts
|
||||||
|
to reflect the resolution of that time base.
|
||||||
|
.Sh RETURN VALUES
|
||||||
|
The
|
||||||
|
.Nm
|
||||||
|
function returns the value of
|
||||||
|
.Fa base
|
||||||
|
if successful and zero otherwise.
|
||||||
|
.Sh SEE ALSO
|
||||||
|
.Xr clock_getres 2 ,
|
||||||
|
.Xr timespec_get 3
|
||||||
|
.\" .Sh STANDARDS
|
||||||
|
.\" The
|
||||||
|
.\" .Nm
|
||||||
|
.\" function conforms to
|
||||||
|
.\" .St -isoC-2023 .
|
||||||
|
.Sh HISTORY
|
||||||
|
This interface first appeared in
|
||||||
|
.Fx 14 .
|
||||||
|
.Sh AUTHORS
|
||||||
|
The
|
||||||
|
.Nm
|
||||||
|
function and this manual page were written by
|
||||||
|
.An Dag-Erling Sm\(/orgrav Aq Mt des@FreeBSD.org .
|
24
lib/libc/gen/timespec_getres.c
Normal file
24
lib/libc/gen/timespec_getres.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/*-
|
||||||
|
* Copyright (c) 2023 Dag-Erling Smørgrav
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
timespec_getres(struct timespec *ts, int base)
|
||||||
|
{
|
||||||
|
|
||||||
|
switch (base) {
|
||||||
|
case TIME_UTC:
|
||||||
|
if (clock_getres(CLOCK_REALTIME, ts) == 0)
|
||||||
|
return (base);
|
||||||
|
break;
|
||||||
|
case TIME_MONOTONIC:
|
||||||
|
if (clock_getres(CLOCK_MONOTONIC, ts) == 0)
|
||||||
|
return (base);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user