From 50e599792eb43b2d40e30c95228178c91002798d Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Fri, 29 Apr 2022 00:50:39 +0000 Subject: [PATCH] [stdlib] Port SE-0329 Clock APIs to WASI WASI doesn't have "suspending-time" concept, so use `CLOCK_MONOTONIC` for both "continuous" and "suspending" clocks. WASI's "monotonic" doesn't guarantee any underlying system call implementation, but most of major implementations use `CLOCK_MONOTONIC` for all underlying platforms. This partially buildfixes this platform. See also: https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#variant-cases --- stdlib/public/Concurrency/Clock.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/stdlib/public/Concurrency/Clock.cpp b/stdlib/public/Concurrency/Clock.cpp index c9586628a167a..bfeb1d4d06309 100644 --- a/stdlib/public/Concurrency/Clock.cpp +++ b/stdlib/public/Concurrency/Clock.cpp @@ -42,7 +42,7 @@ void swift_get_time( clock_gettime(CLOCK_MONOTONIC_RAW, &continuous); *seconds = continuous.tv_sec; *nanoseconds = continuous.tv_nsec; -#elif defined(__OpenBSD__) && HAS_TIME +#elif (defined(__OpenBSD__) || defined(__wasi__)) && HAS_TIME struct timespec continuous; clock_gettime(CLOCK_MONOTONIC, &continuous); *seconds = continuous.tv_sec; @@ -76,6 +76,11 @@ void swift_get_time( clock_gettime(CLOCK_UPTIME_RAW, &suspending); *seconds = suspending.tv_sec; *nanoseconds = suspending.tv_nsec; +#elif defined(__wasi__) && HAS_TIME + struct timespec suspending; + clock_gettime(CLOCK_MONOTONIC, &suspending); + *seconds = suspending.tv_sec; + *nanoseconds = suspending.tv_nsec; #elif defined(__OpenBSD__) && HAS_TIME struct timespec suspending; clock_gettime(CLOCK_UPTIME, &suspending); @@ -121,7 +126,7 @@ switch (clock_id) { clock_getres(CLOCK_MONOTONIC_RAW, &continuous); *seconds = continuous.tv_sec; *nanoseconds = continuous.tv_nsec; -#elif defined(__OpenBSD__) && HAS_TIME +#elif (defined(__OpenBSD__) || defined(__wasi__)) && HAS_TIME struct timespec continuous; clock_getres(CLOCK_MONOTONIC, &continuous); *seconds = continuous.tv_sec; @@ -144,6 +149,10 @@ switch (clock_id) { clock_getres(CLOCK_UPTIME_RAW, &suspending); *seconds = suspending.tv_sec; *nanoseconds = suspending.tv_nsec; +#elif defined(__wasi__) && HAS_TIME + clock_getres(CLOCK_MONOTONIC, &suspending); + *seconds = suspending.tv_sec; + *nanoseconds = suspending.tv_nsec; #elif defined(__OpenBSD__) && HAS_TIME clock_getres(CLOCK_UPTIME, &suspending); *seconds = suspending.tv_sec;