From 8c1ab65c0f548d20b7f177bdb736daaf603340e1 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 15:54:38 +0200 Subject: Adding upstream version 0.0~git20221206.8b7148f. Signed-off-by: Daniel Baumann --- libc-bottom-half/clocks/clock.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 libc-bottom-half/clocks/clock.c (limited to 'libc-bottom-half/clocks/clock.c') diff --git a/libc-bottom-half/clocks/clock.c b/libc-bottom-half/clocks/clock.c new file mode 100644 index 0000000..6767d73 --- /dev/null +++ b/libc-bottom-half/clocks/clock.c @@ -0,0 +1,35 @@ +#define _WASI_EMULATED_PROCESS_CLOCKS +#include +#include +#include + +_Static_assert( + CLOCKS_PER_SEC == NSEC_PER_SEC, + "This implementation assumes that `clock` is in nanoseconds" +); + +// Snapshot of the monotonic clock at the start of the program. +static __wasi_timestamp_t start; + +// Use a priority of 10 to run fairly early in the implementation-reserved +// constructor priority range. +__attribute__((constructor(10))) +static void init(void) { + (void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &start); +} + +// Define the libc symbol as `__clock` so that we can reliably call it +// from elsewhere in libc. +clock_t __clock(void) { + // Use `MONOTONIC` instead of `PROCESS_CPUTIME_ID` since WASI doesn't have + // an inherent concept of a process. Note that this means we'll incorrectly + // include time from other processes, so this function is only declared by + // the headers if `_WASI_EMULATED_PROCESS_CLOCKS` is defined. + __wasi_timestamp_t now = 0; + (void)__wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC, 0, &now); + return now - start; +} + +// Define a user-visible alias as a weak symbol. +__attribute__((__weak__, __alias__("__clock"))) +clock_t clock(void); -- cgit v1.2.3