diff --git a/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c b/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c index 48445bdec..df3d0e723 100644 --- a/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c +++ b/libc-bottom-half/cloudlibc/src/libc/time/clock_gettime.c @@ -21,10 +21,10 @@ int __clock_gettime(clockid_t clock_id, struct timespec *tp) { if (tp == NULL) return 0; - if (clock_id == CLOCK_MONOTONIC) { + if (clock_id->id == CLOCKID_MONOTONIC) { monotonic_clock_instant_t ns = monotonic_clock_now(); *tp = instant_to_timespec(ns); - } else if (clock_id == CLOCK_REALTIME) { + } else if (clock_id->id == CLOCKID_REALTIME) { wasilibc_timestamp_t time_result; #ifdef __wasip2__ wall_clock_now(&time_result); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7409c4a76..9eb102d2c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -679,12 +679,23 @@ add_wasilibc_test(futex_wait_mismatch.c) add_wasilibc_test(futex_flags.c) if (NOT TEST_WITH_V8) add_wasilibc_test(futex_wait_timeout.c) + + # This is tested without a shared library to assist with setting test + # properties, and this is expected to trap on non-threaded targets + # since futex can't block. + add_wasilibc_test(pthread_timedwait_block.c NOSHARED) + if (NOT WASILIBC_MULTITHREADING) + set_tests_properties(pthread_timedwait_block.wasm PROPERTIES + PASS_REGULAR_EXPRESSION "wasm `unreachable` instruction executed") + endif() endif() if (WASILIBC_MULTITHREADING AND NOT TEST_WITH_V8) add_wasilibc_test(futex_wake_one.c) add_wasilibc_test(futex_wake_all.c) endif() + + # ========= wasi-libc tests for non-main entrypoints ========================= function(add_wit_bindgen_library name wit world out_dir) diff --git a/test/src/pthread_timedwait_block.c b/test/src/pthread_timedwait_block.c new file mode 100644 index 000000000..461eadf03 --- /dev/null +++ b/test/src/pthread_timedwait_block.c @@ -0,0 +1,24 @@ +#include "test.h" +#include +#include +#include +#include + +#define TEST(c) \ + do { \ + if (!(c)) \ + t_error("%s failed\n", #c); \ + } while (0) + +int main(void) { + pthread_cond_t cond = PTHREAD_COND_INITIALIZER; + struct timespec now; + TEST(clock_gettime(CLOCK_REALTIME, &now) == 0); + now.tv_nsec += 50 * 1000000; + if (now.tv_nsec >= 1000000000) { + now.tv_sec++; + now.tv_nsec -= 1000000000; + } + TEST(pthread_cond_timedwait(&cond, NULL, &now) == ETIMEDOUT); + return t_status; +}