]> git.proxmox.com Git - wasi-libc.git/blob - libc-bottom-half/clocks/times.c
Emulate `clock`, `times`, and `getrusage` using the monotonic clock.
[wasi-libc.git] / libc-bottom-half / clocks / times.c
1 #define _WASI_EMULATED_PROCESS_CLOCKS
2 #include <time.h>
3 #include <sys/times.h>
4 #include <wasi/api.h>
5 #include <common/time.h>
6
7 _Static_assert(
8 CLOCKS_PER_SEC == NSEC_PER_SEC,
9 "This implementation assumes that `clock` is in nanoseconds"
10 );
11
12 clock_t __clock(void);
13
14 clock_t times(struct tms *buffer) {
15 __wasi_timestamp_t user = __clock();
16 *buffer = (struct tms){
17 .tms_utime = user,
18 .tms_cutime = user
19 };
20
21 __wasi_timestamp_t realtime = 0;
22 (void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &realtime);
23 return realtime;
24 }