summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2021-12-28 11:39:17 -0800
committerPaul Eggert <eggert@cs.ucla.edu>2021-12-28 12:04:49 -0800
commitaf4148ce0197502ce017418871da592bd7be5d91 (patch)
tree02a6445aedcd4511f885bd36401935e411844796 /lib
parent0f42dafa9e0a8dab8d6f97dd70967cb3d8a922da (diff)
downloadgnulib-af4148ce0197502ce017418871da592bd7be5d91.tar.gz
timespec_getres: new module
* lib/time.in.h (timespec_getres): New decl. * lib/timespec_getres.c, m4/timespec_getres.m4: * modules/timespec_getres, modules/timespec_getres-tests: * tests/test-timespec_getres.c: New files. * m4/time_h.m4 (gl_TIME_H_REQUIRE_DEFAULTS, gl_TIME_H_DEFAULTS): * modules/time (time.h): Support timespec_getres.
Diffstat (limited to 'lib')
-rw-r--r--lib/time.in.h11
-rw-r--r--lib/timespec_getres.c40
2 files changed, 51 insertions, 0 deletions
diff --git a/lib/time.in.h b/lib/time.in.h
index a73fe59cbb..383e9768ba 100644
--- a/lib/time.in.h
+++ b/lib/time.in.h
@@ -120,6 +120,17 @@ _GL_CXXALIAS_SYS (timespec_get, int, (struct timespec *ts, int base));
_GL_CXXALIASWARN (timespec_get);
# endif
+/* Set *TS to the current time resolution, and return BASE.
+ Upon failure, return 0. */
+# if @GNULIB_TIMESPEC_GETRES@
+# if ! @HAVE_TIMESPEC_GETRES@
+_GL_FUNCDECL_SYS (timespec_getres, int, (struct timespec *ts, int base)
+ _GL_ARG_NONNULL ((1)));
+# endif
+_GL_CXXALIAS_SYS (timespec_getres, int, (struct timespec *ts, int base));
+_GL_CXXALIASWARN (timespec_getres);
+# endif
+
/* Sleep for at least RQTP seconds unless interrupted, If interrupted,
return -1 and store the remaining time into RMTP. See
<https://pubs.opengroup.org/onlinepubs/9699919799/functions/nanosleep.html>. */
diff --git a/lib/timespec_getres.c b/lib/timespec_getres.c
new file mode 100644
index 0000000000..6fad0ce2d0
--- /dev/null
+++ b/lib/timespec_getres.c
@@ -0,0 +1,40 @@
+/* Get resolution of a time base.
+ Copyright 2021 Free Software Foundation, Inc.
+
+ This file is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 3 of the
+ License, or (at your option) any later version.
+
+ This file is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include <time.h>
+
+#include "timespec.h"
+
+/* Set TS to resolution of time base BASE.
+ Return BASE if successful, 0 otherwise. */
+int
+timespec_getres (struct timespec *ts, int base)
+{
+ if (base == TIME_UTC)
+ {
+#if defined CLOCK_REALTIME && HAVE_CLOCK_GETRES
+ clock_getres (CLOCK_REALTIME, ts);
+#else
+ long int res = gettime_res ();
+ ts->tv_sec = res / TIMESPEC_HZ;
+ ts->tv_nsec = res % TIMESPEC_HZ;
+#endif
+ return base;
+ }
+ return 0;
+}