xref: /illumos-gate/usr/src/lib/libc/port/gen/timespec_cstd.c (revision d3aba68370c737a5035fef1f4c6a95a1ba0d815f)
1*d3aba683SRobert Mustacchi /*
2*d3aba683SRobert Mustacchi  * This file and its contents are supplied under the terms of the
3*d3aba683SRobert Mustacchi  * Common Development and Distribution License ("CDDL"), version 1.0.
4*d3aba683SRobert Mustacchi  * You may only use this file in accordance with the terms of version
5*d3aba683SRobert Mustacchi  * 1.0 of the CDDL.
6*d3aba683SRobert Mustacchi  *
7*d3aba683SRobert Mustacchi  * A full copy of the text of the CDDL should have accompanied this
8*d3aba683SRobert Mustacchi  * source.  A copy of the CDDL is also available via the Internet at
9*d3aba683SRobert Mustacchi  * http://www.illumos.org/license/CDDL.
10*d3aba683SRobert Mustacchi  */
11*d3aba683SRobert Mustacchi 
12*d3aba683SRobert Mustacchi /*
13*d3aba683SRobert Mustacchi  * Copyright (c) 2015 Joyent, Inc.
14*d3aba683SRobert Mustacchi  * Copyright 2025 Oxide Computer Company
15*d3aba683SRobert Mustacchi  */
16*d3aba683SRobert Mustacchi 
17*d3aba683SRobert Mustacchi /*
18*d3aba683SRobert Mustacchi  * Implementation of C11 and C23 timespec_*() functions. Note, the standard does
19*d3aba683SRobert Mustacchi  * not have us return values of errno if there are failures for whatever reason,
20*d3aba683SRobert Mustacchi  * only zero. At least we don't have to preserve errno though.
21*d3aba683SRobert Mustacchi  */
22*d3aba683SRobert Mustacchi 
23*d3aba683SRobert Mustacchi #include <time.h>
24*d3aba683SRobert Mustacchi #include <stdbool.h>
25*d3aba683SRobert Mustacchi 
26*d3aba683SRobert Mustacchi static bool
timespec_base_to_clock(int base,clockid_t * clock)27*d3aba683SRobert Mustacchi timespec_base_to_clock(int base, clockid_t *clock)
28*d3aba683SRobert Mustacchi {
29*d3aba683SRobert Mustacchi 	switch (base) {
30*d3aba683SRobert Mustacchi 	case TIME_UTC:
31*d3aba683SRobert Mustacchi 		*clock = CLOCK_REALTIME;
32*d3aba683SRobert Mustacchi 		break;
33*d3aba683SRobert Mustacchi 	case TIME_MONOTONIC:
34*d3aba683SRobert Mustacchi 		*clock = CLOCK_HIGHRES;
35*d3aba683SRobert Mustacchi 		break;
36*d3aba683SRobert Mustacchi 	case TIME_ACTIVE:
37*d3aba683SRobert Mustacchi 		*clock = CLOCK_PROCESS_CPUTIME_ID;
38*d3aba683SRobert Mustacchi 		break;
39*d3aba683SRobert Mustacchi 	case TIME_THREAD_ACTIVE:
40*d3aba683SRobert Mustacchi 		*clock = CLOCK_THREAD_CPUTIME_ID;
41*d3aba683SRobert Mustacchi 		break;
42*d3aba683SRobert Mustacchi 	case TIME_THREAD_ACTIVE_USR:
43*d3aba683SRobert Mustacchi 		*clock = CLOCK_VIRTUAL;
44*d3aba683SRobert Mustacchi 		break;
45*d3aba683SRobert Mustacchi 	default:
46*d3aba683SRobert Mustacchi 		return (false);
47*d3aba683SRobert Mustacchi 	}
48*d3aba683SRobert Mustacchi 
49*d3aba683SRobert Mustacchi 	return (true);
50*d3aba683SRobert Mustacchi }
51*d3aba683SRobert Mustacchi 
52*d3aba683SRobert Mustacchi int
timespec_get(struct timespec * ts,int base)53*d3aba683SRobert Mustacchi timespec_get(struct timespec *ts, int base)
54*d3aba683SRobert Mustacchi {
55*d3aba683SRobert Mustacchi 	clockid_t clock;
56*d3aba683SRobert Mustacchi 
57*d3aba683SRobert Mustacchi 	if (!timespec_base_to_clock(base, &clock))
58*d3aba683SRobert Mustacchi 		return (0);
59*d3aba683SRobert Mustacchi 
60*d3aba683SRobert Mustacchi 	if (clock_gettime(clock, ts) != 0)
61*d3aba683SRobert Mustacchi 		return (0);
62*d3aba683SRobert Mustacchi 
63*d3aba683SRobert Mustacchi 	return (base);
64*d3aba683SRobert Mustacchi }
65*d3aba683SRobert Mustacchi 
66*d3aba683SRobert Mustacchi int
timespec_getres(struct timespec * ts,int base)67*d3aba683SRobert Mustacchi timespec_getres(struct timespec *ts, int base)
68*d3aba683SRobert Mustacchi {
69*d3aba683SRobert Mustacchi 	clockid_t clock;
70*d3aba683SRobert Mustacchi 
71*d3aba683SRobert Mustacchi 	if (!timespec_base_to_clock(base, &clock))
72*d3aba683SRobert Mustacchi 		return (0);
73*d3aba683SRobert Mustacchi 
74*d3aba683SRobert Mustacchi 	if (clock_getres(clock, ts) != 0)
75*d3aba683SRobert Mustacchi 		return (0);
76*d3aba683SRobert Mustacchi 
77*d3aba683SRobert Mustacchi 	return (base);
78*d3aba683SRobert Mustacchi }
79