From d875a7aa0318451ba7b632357a553deda8fb0bd3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 7 Feb 2026 01:43:05 +0000 Subject: [PATCH 1/9] Initial plan From 649ce230232ab7e6e1d0ee3c82cf2739414824d6 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 7 Feb 2026 01:49:57 +0000 Subject: [PATCH 2/9] Remove HAVE_CLOCK_MONOTONIC and dead fallback code Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com> --- eng/native/tryrun.browser.cmake | 3 --- eng/native/tryrun.cmake | 3 --- eng/native/tryrun_ios_tvos.cmake | 1 - src/coreclr/pal/src/config.h.in | 1 - src/coreclr/pal/src/configure.cmake | 14 -------------- src/coreclr/pal/src/synchmgr/synchmanager.cpp | 8 ++++---- src/mono/cmake/config.h.in | 2 -- src/mono/cmake/configure.cmake | 1 - src/mono/mono/eventpipe/ep-rt-mono.c | 16 +--------------- src/mono/mono/utils/mono-time.c | 2 -- src/native/libs/Common/pal_config.h.in | 1 - .../System.Native/pal_crossprocessmutex.c | 2 +- src/native/libs/System.Native/pal_threading.c | 4 ++-- src/native/libs/configure.cmake | 19 ------------------- src/native/minipal/configure.cmake | 1 - src/native/minipal/minipalconfig.h.in | 1 - src/native/minipal/time.c | 8 ++------ 17 files changed, 10 insertions(+), 77 deletions(-) diff --git a/eng/native/tryrun.browser.cmake b/eng/native/tryrun.browser.cmake index 86e28c9dd2c9a1..60438de23a0fab 100644 --- a/eng/native/tryrun.browser.cmake +++ b/eng/native/tryrun.browser.cmake @@ -130,9 +130,6 @@ set(HAVE_CFSETSPEED 1 CACHE INTERNAL "") set(HAVE_CHMOD 1 CACHE INTERNAL "") set(HAVE_CLOCK_GETTIME_NSEC_NP "" CACHE INTERNAL "") set(HAVE_CLOCK_MONOTONIC_COARSE 1 CACHE INTERNAL "") -set(HAVE_CLOCK_MONOTONIC_COMPILED TRUE CACHE INTERNAL "") -set_cache_value(HAVE_CLOCK_MONOTONIC_EXITCODE 0) -set(HAVE_CLOCK_MONOTONIC 1 CACHE INTERNAL "") set(HAVE_CLOCK_THREAD_CPUTIME_COMPILED TRUE CACHE INTERNAL "") set_cache_value(HAVE_CLOCK_THREAD_CPUTIME_EXITCODE 255) set(HAVE_CLOCK_THREAD_CPUTIME "" CACHE INTERNAL "") diff --git a/eng/native/tryrun.cmake b/eng/native/tryrun.cmake index 32ce86cc0fae94..2090f8a618d3d8 100644 --- a/eng/native/tryrun.cmake +++ b/eng/native/tryrun.cmake @@ -54,7 +54,6 @@ if(DARWIN AND NOT DEFINED ANDROID_BUILD) set_cache_value(HAVE_BROKEN_FIFO_KEVENT_EXITCODE 1) set_cache_value(HAVE_BROKEN_FIFO_SELECT_EXITCODE 1) set_cache_value(HAVE_CLOCK_MONOTONIC_COARSE_EXITCODE 1) - set_cache_value(HAVE_CLOCK_MONOTONIC_EXITCODE 0) set_cache_value(HAVE_CLOCK_REALTIME_EXITCODE 0) set_cache_value(HAVE_CLOCK_THREAD_CPUTIME_EXITCODE 0) set_cache_value(HAVE_CLOCK_GETTIME_NSEC_NP_EXITCODE 0) @@ -75,7 +74,6 @@ if(DARWIN AND NOT DEFINED ANDROID_BUILD) endif() else() set_cache_value(HAVE_CLOCK_MONOTONIC_COARSE_EXITCODE 0) - set_cache_value(HAVE_CLOCK_MONOTONIC_EXITCODE 0) set_cache_value(HAVE_CLOCK_REALTIME_EXITCODE 0) set_cache_value(HAVE_CLOCK_THREAD_CPUTIME_EXITCODE 0) set_cache_value(HAVE_MMAP_DEV_ZERO_EXITCODE 0) @@ -96,7 +94,6 @@ else() if (FREEBSD) set_cache_value(HAVE_SHM_OPEN_THAT_WORKS_WELL_ENOUGH_WITH_MMAP 1) - set_cache_value(HAVE_CLOCK_MONOTONIC 1) set_cache_value(HAVE_CLOCK_REALTIME 1) set_cache_value(HAVE_BROKEN_FIFO_KEVENT_EXITCODE 1) set_cache_value(HAVE_PROCFS_STAT 0) diff --git a/eng/native/tryrun_ios_tvos.cmake b/eng/native/tryrun_ios_tvos.cmake index 7b98f6dcd055ef..fa8af4020363a8 100644 --- a/eng/native/tryrun_ios_tvos.cmake +++ b/eng/native/tryrun_ios_tvos.cmake @@ -5,7 +5,6 @@ endmacro() set_cache_value(HAVE_SCHED_GETCPU_EXITCODE 1) set_cache_value(HAVE_CLOCK_MONOTONIC_COARSE_EXITCODE 1) -set_cache_value(HAVE_CLOCK_MONOTONIC_EXITCODE 0) # TODO: these are taken from macOS, check these whether they're correct for iOS diff --git a/src/coreclr/pal/src/config.h.in b/src/coreclr/pal/src/config.h.in index a30f661035d1c4..49795de0a3dc41 100644 --- a/src/coreclr/pal/src/config.h.in +++ b/src/coreclr/pal/src/config.h.in @@ -87,7 +87,6 @@ #cmakedefine01 HAVE_WORKING_GETTIMEOFDAY #cmakedefine01 HAVE_WORKING_CLOCK_GETTIME #cmakedefine01 HAVE_CLOCK_THREAD_CPUTIME -#cmakedefine01 HAVE_CLOCK_MONOTONIC #cmakedefine01 HAVE_PTHREAD_CONDATTR_SETCLOCK #cmakedefine01 MMAP_ANON_IGNORES_PROTECTION #cmakedefine01 ONE_SHARED_MAPPING_PER_FILEREGION_PER_PROCESS diff --git a/src/coreclr/pal/src/configure.cmake b/src/coreclr/pal/src/configure.cmake index 9acda1fcb12e09..f0687feb656c85 100644 --- a/src/coreclr/pal/src/configure.cmake +++ b/src/coreclr/pal/src/configure.cmake @@ -366,21 +366,7 @@ int main() }" HAVE_WORKING_CLOCK_GETTIME) set(CMAKE_REQUIRED_LIBRARIES) -set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_RT_LIBS}) -check_cxx_source_runs(" -#include -#include -#include - -int main() -{ - int ret; - struct timespec ts; - ret = clock_gettime(CLOCK_MONOTONIC, &ts); - exit(ret); -}" HAVE_CLOCK_MONOTONIC) -set(CMAKE_REQUIRED_LIBRARIES) check_library_exists(${PTHREAD_LIBRARY} pthread_condattr_setclock "" HAVE_PTHREAD_CONDATTR_SETCLOCK) diff --git a/src/coreclr/pal/src/synchmgr/synchmanager.cpp b/src/coreclr/pal/src/synchmgr/synchmanager.cpp index b6c44754210805..cde266449dd9d3 100644 --- a/src/coreclr/pal/src/synchmgr/synchmanager.cpp +++ b/src/coreclr/pal/src/synchmgr/synchmanager.cpp @@ -2671,7 +2671,7 @@ namespace CorUnix VolatileStore(pdwWaitState, TWS_ACTIVE); m_tsThreadState = TS_STARTING; -#if HAVE_CLOCK_MONOTONIC && HAVE_PTHREAD_CONDATTR_SETCLOCK +#if HAVE_PTHREAD_CONDATTR_SETCLOCK attrsPtr = &attrs; iRet = pthread_condattr_init(&attrs); if (0 != iRet) @@ -2699,7 +2699,7 @@ namespace CorUnix pthread_condattr_destroy(&attrs); goto IPrC_exit; } -#endif // HAVE_CLOCK_MONOTONIC && HAVE_PTHREAD_CONDATTR_SETCLOCK +#endif // HAVE_PTHREAD_CONDATTR_SETCLOCK iEagains = 0; Mutex_retry: @@ -2995,7 +2995,7 @@ namespace CorUnix PAL_ERROR palErr = NO_ERROR; int iRet; -#if HAVE_CLOCK_MONOTONIC && HAVE_PTHREAD_CONDATTR_SETCLOCK +#if HAVE_PTHREAD_CONDATTR_SETCLOCK if (fPreferMonotonicClock) { iRet = clock_gettime(CLOCK_MONOTONIC, ptsAbsTmo); @@ -3022,7 +3022,7 @@ namespace CorUnix #error "Don't know how to get hi-res current time on this platform" #endif #endif // HAVE_WORKING_CLOCK_GETTIME, HAVE_WORKING_GETTIMEOFDAY -#if HAVE_CLOCK_MONOTONIC && HAVE_PTHREAD_CONDATTR_SETCLOCK +#if HAVE_PTHREAD_CONDATTR_SETCLOCK } #endif if (0 == iRet) diff --git a/src/mono/cmake/config.h.in b/src/mono/cmake/config.h.in index b5224211265cdb..4fe413349369af 100644 --- a/src/mono/cmake/config.h.in +++ b/src/mono/cmake/config.h.in @@ -260,8 +260,6 @@ /* Define to 1 if you have the `sigaction' function. */ #cmakedefine HAVE_SIGACTION 1 -/* CLOCK_MONOTONIC */ -#cmakedefine HAVE_CLOCK_MONOTONIC 1 /* clockid_t */ #cmakedefine HAVE_CLOCKID_T 1 diff --git a/src/mono/cmake/configure.cmake b/src/mono/cmake/configure.cmake index 6b9e021f1437bc..e7daccb7af7675 100644 --- a/src/mono/cmake/configure.cmake +++ b/src/mono/cmake/configure.cmake @@ -112,7 +112,6 @@ check_function_exists(clock_gettime HAVE_CLOCK_GETTIME) check_symbol_exists(madvise "sys/mman.h" HAVE_MADVISE) check_symbol_exists(pthread_mutexattr_setprotocol "pthread.h" HAVE_DECL_PTHREAD_MUTEXATTR_SETPROTOCOL) -check_symbol_exists(CLOCK_MONOTONIC "time.h" HAVE_CLOCK_MONOTONIC) check_symbol_exists(sys_signame "signal.h" HAVE_SYSSIGNAME) check_symbol_exists(pthread_jit_write_protect_np "pthread.h" HAVE_PTHREAD_JIT_WRITE_PROTECT_NP) diff --git a/src/mono/mono/eventpipe/ep-rt-mono.c b/src/mono/mono/eventpipe/ep-rt-mono.c index a6ea9092673d29..f6d6711ada2427 100644 --- a/src/mono/mono/eventpipe/ep-rt-mono.c +++ b/src/mono/mono/eventpipe/ep-rt-mono.c @@ -386,21 +386,13 @@ static const int64_t SECS_BETWEEN_1601_AND_1970_EPOCHS = 11644473600LL; static const int64_t SECS_TO_100NS = 10000000; static const int64_t MSECS_TO_MIS = 1000; -/* clock_gettime () is found by configure on Apple builds, but its only present from ios 10, macos 10.12, tvos 10 and watchos 3 */ -#if defined (HAVE_CLOCK_MONOTONIC) && (defined(HOST_IOS) || defined(HOST_OSX) || defined(HOST_WATCHOS) || defined(HOST_TVOS)) -#undef HAVE_CLOCK_MONOTONIC -#endif - -#ifndef HAVE_CLOCK_MONOTONIC -static const int64_t MISECS_TO_NS = 1000; -#endif - static int64_t system_time_to_int64 ( time_t sec, long nsec); + void ep_rt_mono_system_time_get (EventPipeSystemTime *system_time) { @@ -466,15 +458,9 @@ system_time_to_int64 ( int64_t ep_rt_mono_system_timestamp_get (void) { -#if HAVE_CLOCK_MONOTONIC struct timespec time; if (clock_gettime (CLOCK_REALTIME, &time) == 0) return system_time_to_int64 (time.tv_sec, time.tv_nsec); -#else - struct timeval time; - if (gettimeofday (&time, NULL) == 0) - return system_time_to_int64 (time.tv_sec, time.tv_usec * MISECS_TO_NS); -#endif else return system_time_to_int64 (0, 0); } diff --git a/src/mono/mono/utils/mono-time.c b/src/mono/mono/utils/mono-time.c index cf5c07fc688b22..3788553f4e0ef8 100644 --- a/src/mono/mono/utils/mono-time.c +++ b/src/mono/mono/utils/mono-time.c @@ -190,9 +190,7 @@ mono_clock_get_time_ns (mono_clock_id_t clk_id) void mono_clock_init (mono_clock_id_t *clk_id) { -#ifdef HAVE_CLOCK_MONOTONIC *clk_id = CLOCK_MONOTONIC; -#endif } void diff --git a/src/native/libs/Common/pal_config.h.in b/src/native/libs/Common/pal_config.h.in index 32176492bf488c..898516d20d55c3 100644 --- a/src/native/libs/Common/pal_config.h.in +++ b/src/native/libs/Common/pal_config.h.in @@ -97,7 +97,6 @@ #cmakedefine01 HAVE_LINUX_ERRQUEUE_H #cmakedefine01 HAVE_GETDOMAINNAME_SIZET #cmakedefine01 HAVE_INOTIFY -#cmakedefine01 HAVE_CLOCK_MONOTONIC #cmakedefine01 HAVE_CLOCK_REALTIME #cmakedefine01 HAVE_CLOCK_GETTIME_NSEC_NP #cmakedefine01 HAVE_PTHREAD_CONDATTR_SETCLOCK diff --git a/src/native/libs/System.Native/pal_crossprocessmutex.c b/src/native/libs/System.Native/pal_crossprocessmutex.c index aba44eb633a5d0..0d18731c6e9f8c 100644 --- a/src/native/libs/System.Native/pal_crossprocessmutex.c +++ b/src/native/libs/System.Native/pal_crossprocessmutex.c @@ -41,7 +41,7 @@ static int32_t AcquirePThreadMutexWithTimeout(pthread_mutex_t* mutex, int32_t ti timeoutTimeSpec.tv_nsec = (timeoutMilliseconds % 1000) * 1000 * 1000; error = pthread_mutex_reltimedlock_np(mutex, &timeoutTimeSpec); -#elif HAVE_PTHREAD_MUTEX_CLOCKLOCK && HAVE_CLOCK_MONOTONIC +#elif HAVE_PTHREAD_MUTEX_CLOCKLOCK error = clock_gettime(CLOCK_MONOTONIC, &timeoutTimeSpec); assert(error == 0); diff --git a/src/native/libs/System.Native/pal_threading.c b/src/native/libs/System.Native/pal_threading.c index db6133d49df6b9..3085ff6b23a57a 100644 --- a/src/native/libs/System.Native/pal_threading.c +++ b/src/native/libs/System.Native/pal_threading.c @@ -64,7 +64,7 @@ LowLevelMonitor* SystemNative_LowLevelMonitor_Create(void) return NULL; } -#if HAVE_PTHREAD_CONDATTR_SETCLOCK && HAVE_CLOCK_MONOTONIC +#if HAVE_PTHREAD_CONDATTR_SETCLOCK pthread_condattr_t conditionAttributes; error = pthread_condattr_init(&conditionAttributes); if (error != 0) @@ -176,7 +176,7 @@ int32_t SystemNative_LowLevelMonitor_TimedWait(LowLevelMonitor *monitor, int32_t error = pthread_cond_timedwait_relative_np(&monitor->Condition, &monitor->Mutex, &timeoutTimeSpec); #else -#if HAVE_PTHREAD_CONDATTR_SETCLOCK && HAVE_CLOCK_MONOTONIC +#if HAVE_PTHREAD_CONDATTR_SETCLOCK error = clock_gettime(CLOCK_MONOTONIC, &timeoutTimeSpec); assert(error == 0); #else diff --git a/src/native/libs/configure.cmake b/src/native/libs/configure.cmake index 4a697287d3bd54..ee95e1ca6e2a4b 100644 --- a/src/native/libs/configure.cmake +++ b/src/native/libs/configure.cmake @@ -558,28 +558,24 @@ if(CLR_CMAKE_TARGET_IOS) # Manually set results from check_c_source_runs() since it's not possible to actually run it during CMake configure checking unset(HAVE_SHM_OPEN_THAT_WORKS_WELL_ENOUGH_WITH_MMAP) unset(HAVE_ALIGNED_ALLOC) # only exists on iOS 13+ - set(HAVE_CLOCK_MONOTONIC 1) set(HAVE_CLOCK_REALTIME 1) unset(HAVE_FORK) # exists but blocked by kernel elseif(CLR_CMAKE_TARGET_MACCATALYST) # Manually set results from check_c_source_runs() since it's not possible to actually run it during CMake configure checking unset(HAVE_SHM_OPEN_THAT_WORKS_WELL_ENOUGH_WITH_MMAP) unset(HAVE_ALIGNED_ALLOC) # only exists on iOS 13+ - set(HAVE_CLOCK_MONOTONIC 1) set(HAVE_CLOCK_REALTIME 1) unset(HAVE_FORK) # exists but blocked by kernel elseif(CLR_CMAKE_TARGET_TVOS) # Manually set results from check_c_source_runs() since it's not possible to actually run it during CMake configure checking unset(HAVE_SHM_OPEN_THAT_WORKS_WELL_ENOUGH_WITH_MMAP) unset(HAVE_ALIGNED_ALLOC) # only exists on iOS 13+ - set(HAVE_CLOCK_MONOTONIC 1) set(HAVE_CLOCK_REALTIME 1) unset(HAVE_FORK) # exists but blocked by kernel elseif(CLR_CMAKE_TARGET_ANDROID) # Manually set results from check_c_source_runs() since it's not possible to actually run it during CMake configure checking unset(HAVE_SHM_OPEN_THAT_WORKS_WELL_ENOUGH_WITH_MMAP) unset(HAVE_ALIGNED_ALLOC) # only exists on newer Android - set(HAVE_CLOCK_MONOTONIC 1) set(HAVE_CLOCK_REALTIME 1) elseif(CLR_CMAKE_TARGET_WASI) set(HAVE_FORK 0) @@ -617,21 +613,6 @@ else() " HAVE_SHM_OPEN_THAT_WORKS_WELL_ENOUGH_WITH_MMAP) - check_c_source_runs( - " - #include - #include - #include - int main(void) - { - int ret; - struct timespec ts; - ret = clock_gettime(CLOCK_MONOTONIC, &ts); - exit(ret); - return 0; - } - " - HAVE_CLOCK_MONOTONIC) check_c_source_runs( " diff --git a/src/native/minipal/configure.cmake b/src/native/minipal/configure.cmake index 9acaac6ddcc356..65a33a2568d70e 100644 --- a/src/native/minipal/configure.cmake +++ b/src/native/minipal/configure.cmake @@ -13,7 +13,6 @@ check_function_exists(fsync HAVE_FSYNC) check_symbol_exists(arc4random_buf "stdlib.h" HAVE_ARC4RANDOM_BUF) check_symbol_exists(getrandom "sys/random.h" HAVE_GETRANDOM) check_symbol_exists(O_CLOEXEC fcntl.h HAVE_O_CLOEXEC) -check_symbol_exists(CLOCK_MONOTONIC time.h HAVE_CLOCK_MONOTONIC) check_symbol_exists(CLOCK_MONOTONIC_COARSE time.h HAVE_CLOCK_MONOTONIC_COARSE) check_symbol_exists(clock_gettime_nsec_np time.h HAVE_CLOCK_GETTIME_NSEC_NP) diff --git a/src/native/minipal/minipalconfig.h.in b/src/native/minipal/minipalconfig.h.in index 84a619fc8c4096..8b72efb5b23207 100644 --- a/src/native/minipal/minipalconfig.h.in +++ b/src/native/minipal/minipalconfig.h.in @@ -8,7 +8,6 @@ #cmakedefine01 HAVE_RESOURCE_H #cmakedefine01 HAVE_O_CLOEXEC #cmakedefine01 HAVE_SYSCTLBYNAME -#cmakedefine01 HAVE_CLOCK_MONOTONIC #cmakedefine01 HAVE_CLOCK_MONOTONIC_COARSE #cmakedefine01 HAVE_CLOCK_GETTIME_NSEC_NP #cmakedefine01 BIGENDIAN diff --git a/src/native/minipal/time.c b/src/native/minipal/time.c index 26ee0a7613cd9c..3706c85eda1445 100644 --- a/src/native/minipal/time.c +++ b/src/native/minipal/time.c @@ -97,7 +97,7 @@ int64_t minipal_hires_ticks(void) { #if HAVE_CLOCK_GETTIME_NSEC_NP return (int64_t)clock_gettime_nsec_np(CLOCK_UPTIME_RAW); -#elif HAVE_CLOCK_MONOTONIC +#else struct timespec ts; int result = clock_gettime(CLOCK_MONOTONIC, &ts); if (result != 0) @@ -106,8 +106,6 @@ int64_t minipal_hires_ticks(void) } return ((int64_t)(ts.tv_sec) * (int64_t)(tccSecondsToNanoSeconds)) + (int64_t)(ts.tv_nsec); -#else - #error "minipal_hires_ticks requires clock_gettime_nsec_np or clock_gettime to be supported." #endif } @@ -115,7 +113,7 @@ int64_t minipal_lowres_ticks(void) { #if HAVE_CLOCK_GETTIME_NSEC_NP return (int64_t)clock_gettime_nsec_np(CLOCK_UPTIME_RAW) / (int64_t)(tccMilliSecondsToNanoSeconds); -#elif HAVE_CLOCK_MONOTONIC +#else struct timespec ts; // emscripten exposes CLOCK_MONOTONIC_COARSE but doesn't implement it @@ -140,8 +138,6 @@ int64_t minipal_lowres_ticks(void) } return ((int64_t)(ts.tv_sec) * (int64_t)(tccSecondsToMilliSeconds)) + ((int64_t)(ts.tv_nsec) / (int64_t)(tccMilliSecondsToNanoSeconds)); -#else - #error "minipal_lowres_ticks requires clock_gettime_nsec_np or clock_gettime to be supported." #endif } From 78a34a73ccc4fc5c02d09a41b0a1fec4a0ae4b27 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 7 Feb 2026 02:25:01 +0000 Subject: [PATCH 3/9] Remove fPreferMonotonicClock parameter from GetAbsoluteTimeout The parameter was always TRUE, so simplify by removing it and always preferring CLOCK_MONOTONIC when available. Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com> --- src/coreclr/pal/src/synchmgr/synchmanager.cpp | 37 +++++++------------ src/coreclr/pal/src/synchmgr/synchmanager.hpp | 3 +- 2 files changed, 15 insertions(+), 25 deletions(-) diff --git a/src/coreclr/pal/src/synchmgr/synchmanager.cpp b/src/coreclr/pal/src/synchmgr/synchmanager.cpp index cde266449dd9d3..4aec8e0067ca33 100644 --- a/src/coreclr/pal/src/synchmgr/synchmanager.cpp +++ b/src/coreclr/pal/src/synchmgr/synchmanager.cpp @@ -386,7 +386,7 @@ namespace CorUnix if (dwTimeout != INFINITE) { // Calculate absolute timeout - palErr = GetAbsoluteTimeout(dwTimeout, &tsAbsTmo, /*fPreferMonotonicClock*/ TRUE); + palErr = GetAbsoluteTimeout(dwTimeout, &tsAbsTmo); if (NO_ERROR != palErr) { ERROR("Failed to convert timeout to absolute timeout\n"); @@ -1051,7 +1051,7 @@ namespace CorUnix ptnwdWorkerThreadNativeData = &pSynchManager->m_pthrWorker->synchronizationInfo.m_tnwdNativeData; - palErr = GetAbsoluteTimeout(WorkerThreadTerminationTimeout, &tsAbsTmo, /*fPreferMonotonicClock*/ TRUE); + palErr = GetAbsoluteTimeout(WorkerThreadTerminationTimeout, &tsAbsTmo); if (NO_ERROR != palErr) { ERROR("Failed to convert timeout to absolute timeout\n"); @@ -2990,31 +2990,25 @@ namespace CorUnix Converts a relative timeout to an absolute one. --*/ - PAL_ERROR CPalSynchronizationManager::GetAbsoluteTimeout(DWORD dwTimeout, struct timespec * ptsAbsTmo, BOOL fPreferMonotonicClock) + PAL_ERROR CPalSynchronizationManager::GetAbsoluteTimeout(DWORD dwTimeout, struct timespec * ptsAbsTmo) { PAL_ERROR palErr = NO_ERROR; int iRet; #if HAVE_PTHREAD_CONDATTR_SETCLOCK - if (fPreferMonotonicClock) + iRet = clock_gettime(CLOCK_MONOTONIC, ptsAbsTmo); +#elif HAVE_WORKING_CLOCK_GETTIME + // Not every platform implements a (working) clock_gettime + iRet = clock_gettime(CLOCK_REALTIME, ptsAbsTmo); +#elif HAVE_WORKING_GETTIMEOFDAY + // Not every platform implements a (working) gettimeofday + struct timeval tv; + iRet = gettimeofday(&tv, NULL); + if (0 == iRet) { - iRet = clock_gettime(CLOCK_MONOTONIC, ptsAbsTmo); + ptsAbsTmo->tv_sec = tv.tv_sec; + ptsAbsTmo->tv_nsec = tv.tv_usec * tccMicroSecondsToNanoSeconds; } - else - { -#endif -#if HAVE_WORKING_CLOCK_GETTIME - // Not every platform implements a (working) clock_gettime - iRet = clock_gettime(CLOCK_REALTIME, ptsAbsTmo); -#elif HAVE_WORKING_GETTIMEOFDAY - // Not every platform implements a (working) gettimeofday - struct timeval tv; - iRet = gettimeofday(&tv, NULL); - if (0 == iRet) - { - ptsAbsTmo->tv_sec = tv.tv_sec; - ptsAbsTmo->tv_nsec = tv.tv_usec * tccMicroSecondsToNanoSeconds; - } #else #ifdef DBI_COMPONENT_MONO return ERROR_INTERNAL_ERROR; @@ -3022,9 +3016,6 @@ namespace CorUnix #error "Don't know how to get hi-res current time on this platform" #endif #endif // HAVE_WORKING_CLOCK_GETTIME, HAVE_WORKING_GETTIMEOFDAY -#if HAVE_PTHREAD_CONDATTR_SETCLOCK - } -#endif if (0 == iRet) { ptsAbsTmo->tv_sec += dwTimeout / tccSecondsToMilliSeconds; diff --git a/src/coreclr/pal/src/synchmgr/synchmanager.hpp b/src/coreclr/pal/src/synchmgr/synchmanager.hpp index 4a577261a783b0..32939732a5ff9e 100644 --- a/src/coreclr/pal/src/synchmgr/synchmanager.hpp +++ b/src/coreclr/pal/src/synchmgr/synchmanager.hpp @@ -775,8 +775,7 @@ namespace CorUnix static PAL_ERROR GetAbsoluteTimeout( DWORD dwTimeout, - struct timespec * ptsAbsTmo, - BOOL fPreferMonotonicClock); + struct timespec * ptsAbsTmo); }; } From cbeadd8acb34c5cd359de35b998e443d3b5fe6be Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 7 Feb 2026 02:35:54 +0000 Subject: [PATCH 4/9] Fix Windows build: guard CLOCK_MONOTONIC with ifdef CLOCK_MONOTONIC is a POSIX constant not available on Windows. Guard the assignment with #ifdef to prevent build failures on Windows/WASM builds. Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com> --- src/mono/mono/utils/mono-time.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mono/mono/utils/mono-time.c b/src/mono/mono/utils/mono-time.c index 3788553f4e0ef8..db0bc074970491 100644 --- a/src/mono/mono/utils/mono-time.c +++ b/src/mono/mono/utils/mono-time.c @@ -190,7 +190,9 @@ mono_clock_get_time_ns (mono_clock_id_t clk_id) void mono_clock_init (mono_clock_id_t *clk_id) { +#ifdef CLOCK_MONOTONIC *clk_id = CLOCK_MONOTONIC; +#endif } void From 3ca931511ab96734ad399148f05e1217e62c0083 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 7 Feb 2026 03:01:27 +0000 Subject: [PATCH 5/9] Remove mono_clock_init and use CLOCK_MONOTONIC directly Deleted all instances of mono_clock_init() and inlined the initialization. On Darwin, the host_get_clock_service call is now directly in mini-posix.c. On Linux/non-Darwin, sampling_clock is already set to CLOCK_MONOTONIC directly. Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com> --- src/mono/mono/mini/mini-posix.c | 9 ++++++++- src/mono/mono/utils/mono-time.c | 27 --------------------------- src/mono/mono/utils/mono-time.h | 1 - 3 files changed, 8 insertions(+), 29 deletions(-) diff --git a/src/mono/mono/mini/mini-posix.c b/src/mono/mono/mini/mini-posix.c index 4c36fd6b3ffeea..119e6e37581bdb 100644 --- a/src/mono/mono/mini/mini-posix.c +++ b/src/mono/mono/mini/mini-posix.c @@ -476,7 +476,14 @@ static clock_serv_t sampling_clock; static void clock_init_for_profiler (MonoProfilerSampleMode mode) { - mono_clock_init (&sampling_clock); + kern_return_t ret; + + do { + ret = host_get_clock_service (mach_host_self (), SYSTEM_CLOCK, &sampling_clock); + } while (ret == KERN_ABORTED); + + if (ret != KERN_SUCCESS) + g_error ("%s: host_get_clock_service () returned %d", __func__, ret); } static void diff --git a/src/mono/mono/utils/mono-time.c b/src/mono/mono/utils/mono-time.c index db0bc074970491..2120ec005502ef 100644 --- a/src/mono/mono/utils/mono-time.c +++ b/src/mono/mono/utils/mono-time.c @@ -141,19 +141,6 @@ mono_100ns_datetime_from_timeval (struct timeval tv) #if defined(HOST_DARWIN) -void -mono_clock_init (mono_clock_id_t *clk_id) -{ - kern_return_t ret; - - do { - ret = host_get_clock_service (mach_host_self (), SYSTEM_CLOCK, clk_id); - } while (ret == KERN_ABORTED); - - if (ret != KERN_SUCCESS) - g_error ("%s: host_get_clock_service () returned %d", __func__, ret); -} - void mono_clock_cleanup (mono_clock_id_t clk_id) { @@ -187,14 +174,6 @@ mono_clock_get_time_ns (mono_clock_id_t clk_id) // Can we assume clock_gettime exists on all modern POSIX systems? Maybe add a better check for it in configure.ac? #elif defined(__linux__) || defined (TARGET_WASM) -void -mono_clock_init (mono_clock_id_t *clk_id) -{ -#ifdef CLOCK_MONOTONIC - *clk_id = CLOCK_MONOTONIC; -#endif -} - void mono_clock_cleanup (mono_clock_id_t clk_id) { @@ -217,12 +196,6 @@ mono_clock_get_time_ns (mono_clock_id_t clk_id) #else -void -mono_clock_init (mono_clock_id_t *clk_id) -{ - // TODO: need to implement this function for Windows -} - void mono_clock_cleanup (mono_clock_id_t clk_id) { diff --git a/src/mono/mono/utils/mono-time.h b/src/mono/mono/utils/mono-time.h index b62352ab26a8cf..c4b5950646ff23 100644 --- a/src/mono/mono/utils/mono-time.h +++ b/src/mono/mono/utils/mono-time.h @@ -34,7 +34,6 @@ typedef clockid_t mono_clock_id_t; typedef void *mono_clock_id_t; #endif -void mono_clock_init (mono_clock_id_t *clk_id); void mono_clock_cleanup (mono_clock_id_t clk_id); guint64 mono_clock_get_time_ns (mono_clock_id_t clk_id); From 3e2ab4dab08f035a99680ca025008c07230efa2d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 7 Feb 2026 03:13:38 +0000 Subject: [PATCH 6/9] Revert "Remove mono_clock_init and use CLOCK_MONOTONIC directly" This reverts commit 3ca931511ab96734ad399148f05e1217e62c0083. Keeping the mono_clock_init() wrapper function as requested. Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com> --- src/mono/mono/mini/mini-posix.c | 9 +-------- src/mono/mono/utils/mono-time.c | 27 +++++++++++++++++++++++++++ src/mono/mono/utils/mono-time.h | 1 + 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/mono/mono/mini/mini-posix.c b/src/mono/mono/mini/mini-posix.c index 119e6e37581bdb..4c36fd6b3ffeea 100644 --- a/src/mono/mono/mini/mini-posix.c +++ b/src/mono/mono/mini/mini-posix.c @@ -476,14 +476,7 @@ static clock_serv_t sampling_clock; static void clock_init_for_profiler (MonoProfilerSampleMode mode) { - kern_return_t ret; - - do { - ret = host_get_clock_service (mach_host_self (), SYSTEM_CLOCK, &sampling_clock); - } while (ret == KERN_ABORTED); - - if (ret != KERN_SUCCESS) - g_error ("%s: host_get_clock_service () returned %d", __func__, ret); + mono_clock_init (&sampling_clock); } static void diff --git a/src/mono/mono/utils/mono-time.c b/src/mono/mono/utils/mono-time.c index 2120ec005502ef..db0bc074970491 100644 --- a/src/mono/mono/utils/mono-time.c +++ b/src/mono/mono/utils/mono-time.c @@ -141,6 +141,19 @@ mono_100ns_datetime_from_timeval (struct timeval tv) #if defined(HOST_DARWIN) +void +mono_clock_init (mono_clock_id_t *clk_id) +{ + kern_return_t ret; + + do { + ret = host_get_clock_service (mach_host_self (), SYSTEM_CLOCK, clk_id); + } while (ret == KERN_ABORTED); + + if (ret != KERN_SUCCESS) + g_error ("%s: host_get_clock_service () returned %d", __func__, ret); +} + void mono_clock_cleanup (mono_clock_id_t clk_id) { @@ -174,6 +187,14 @@ mono_clock_get_time_ns (mono_clock_id_t clk_id) // Can we assume clock_gettime exists on all modern POSIX systems? Maybe add a better check for it in configure.ac? #elif defined(__linux__) || defined (TARGET_WASM) +void +mono_clock_init (mono_clock_id_t *clk_id) +{ +#ifdef CLOCK_MONOTONIC + *clk_id = CLOCK_MONOTONIC; +#endif +} + void mono_clock_cleanup (mono_clock_id_t clk_id) { @@ -196,6 +217,12 @@ mono_clock_get_time_ns (mono_clock_id_t clk_id) #else +void +mono_clock_init (mono_clock_id_t *clk_id) +{ + // TODO: need to implement this function for Windows +} + void mono_clock_cleanup (mono_clock_id_t clk_id) { diff --git a/src/mono/mono/utils/mono-time.h b/src/mono/mono/utils/mono-time.h index c4b5950646ff23..b62352ab26a8cf 100644 --- a/src/mono/mono/utils/mono-time.h +++ b/src/mono/mono/utils/mono-time.h @@ -34,6 +34,7 @@ typedef clockid_t mono_clock_id_t; typedef void *mono_clock_id_t; #endif +void mono_clock_init (mono_clock_id_t *clk_id); void mono_clock_cleanup (mono_clock_id_t clk_id); guint64 mono_clock_get_time_ns (mono_clock_id_t clk_id); From 39af12f6e39e6de7fdc977d567c305714e9ae9c0 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Fri, 6 Feb 2026 23:38:20 -0800 Subject: [PATCH 7/9] Update src/mono/mono/eventpipe/ep-rt-mono.c Co-authored-by: Aaron R Robinson --- src/mono/mono/eventpipe/ep-rt-mono.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/mono/mono/eventpipe/ep-rt-mono.c b/src/mono/mono/eventpipe/ep-rt-mono.c index f6d6711ada2427..60efe175faf07d 100644 --- a/src/mono/mono/eventpipe/ep-rt-mono.c +++ b/src/mono/mono/eventpipe/ep-rt-mono.c @@ -392,7 +392,6 @@ system_time_to_int64 ( time_t sec, long nsec); - void ep_rt_mono_system_time_get (EventPipeSystemTime *system_time) { From b800389372f6e1e9d6ba879b20069000dc8412a3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 7 Feb 2026 15:46:44 +0000 Subject: [PATCH 8/9] Simplify assert statements in minipal time.c Changed assert statements to use the simpler form: assert(result == 0 && "message") instead of if (result != 0) { assert(!"message"); } Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com> --- src/native/minipal/time.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/native/minipal/time.c b/src/native/minipal/time.c index 3706c85eda1445..288ce98c3f9c3e 100644 --- a/src/native/minipal/time.c +++ b/src/native/minipal/time.c @@ -100,10 +100,7 @@ int64_t minipal_hires_ticks(void) #else struct timespec ts; int result = clock_gettime(CLOCK_MONOTONIC, &ts); - if (result != 0) - { - assert(!"clock_gettime(CLOCK_MONOTONIC) failed"); - } + assert(result == 0 && "clock_gettime(CLOCK_MONOTONIC) failed"); return ((int64_t)(ts.tv_sec) * (int64_t)(tccSecondsToNanoSeconds)) + (int64_t)(ts.tv_nsec); #endif @@ -128,14 +125,11 @@ int64_t minipal_lowres_ticks(void) #endif int result = clock_gettime(clockType, &ts); - if (result != 0) - { #if HAVE_CLOCK_MONOTONIC_COARSE && !defined(__EMSCRIPTEN__) - assert(!"clock_gettime(CLOCK_MONOTONIC_COARSE) failed"); + assert(result == 0 && "clock_gettime(CLOCK_MONOTONIC_COARSE) failed"); #else - assert(!"clock_gettime(CLOCK_MONOTONIC) failed"); + assert(result == 0 && "clock_gettime(CLOCK_MONOTONIC) failed"); #endif - } return ((int64_t)(ts.tv_sec) * (int64_t)(tccSecondsToMilliSeconds)) + ((int64_t)(ts.tv_nsec) / (int64_t)(tccMilliSecondsToNanoSeconds)); #endif From b589df00c1f73ed4a82668a8c1bab8f8436bb3e7 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 7 Feb 2026 08:25:16 -0800 Subject: [PATCH 9/9] Apply suggestions from code review --- src/native/minipal/time.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/native/minipal/time.c b/src/native/minipal/time.c index 288ce98c3f9c3e..3f1d7c1259f4a6 100644 --- a/src/native/minipal/time.c +++ b/src/native/minipal/time.c @@ -99,7 +99,8 @@ int64_t minipal_hires_ticks(void) return (int64_t)clock_gettime_nsec_np(CLOCK_UPTIME_RAW); #else struct timespec ts; - int result = clock_gettime(CLOCK_MONOTONIC, &ts); + int result; + result = clock_gettime(CLOCK_MONOTONIC, &ts); assert(result == 0 && "clock_gettime(CLOCK_MONOTONIC) failed"); return ((int64_t)(ts.tv_sec) * (int64_t)(tccSecondsToNanoSeconds)) + (int64_t)(ts.tv_nsec); @@ -124,7 +125,8 @@ int64_t minipal_lowres_ticks(void) const clockid_t clockType = CLOCK_MONOTONIC; #endif - int result = clock_gettime(clockType, &ts); + int result; + result = clock_gettime(clockType, &ts); #if HAVE_CLOCK_MONOTONIC_COARSE && !defined(__EMSCRIPTEN__) assert(result == 0 && "clock_gettime(CLOCK_MONOTONIC_COARSE) failed"); #else