The unpac monorepo manager self-hosting as a monorepo using unpac
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

make caml_time_counter concurrent-safe on macOS (#12234)

Also time counter a uint64_t instead of casting between
signed/unsigned longs on all the platforms. The type
of most of the monotonic counter interfaces is an uint64,
except for Windows.

Fixes #12096

authored by

Anil Madhavapeddy and committed by
GitHub
cd01cac2 3358a2eb

+23 -32
+3
Changes
··· 14 14 tables will go in the readonly segment, where they belong. 15 15 (Antonin Décimo, review by Gabriel Scherer and Xavier Leroy) 16 16 17 + - #12234: make instrumented time calculation more thread-safe on macOS. 18 + (Anil Madhavapeddy, review by Daniel Bünzli and Xavier Leroy) 19 + 17 20 ### Code generation and optimizations: 18 21 19 22 ### Standard library:
+5 -8
configure
··· 16206 16206 *-apple-darwin*) : 16207 16207 16208 16208 16209 - for ac_func in mach_timebase_info mach_absolute_time 16209 + for ac_func in clock_gettime_nsec_np 16210 16210 do : 16211 - as_ac_var=`printf "%s\n" "ac_cv_func_$ac_func" | $as_tr_sh` 16212 - ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" 16213 - if eval test \"x\$"$as_ac_var"\" = x"yes" 16211 + ac_fn_c_check_func "$LINENO" "clock_gettime_nsec_np" "ac_cv_func_clock_gettime_nsec_np" 16212 + if test "x$ac_cv_func_clock_gettime_nsec_np" = xyes 16214 16213 then : 16215 - cat >>confdefs.h <<_ACEOF 16216 - #define `printf "%s\n" "HAVE_$ac_func" | $as_tr_cpp` 1 16217 - _ACEOF 16214 + printf "%s\n" "#define HAVE_CLOCK_GETTIME_NSEC_NP 1" >>confdefs.h 16218 16215 16219 16216 has_monotonic_clock=true 16220 - printf "%s\n" "#define HAS_MACH_ABSOLUTE_TIME 1" >>confdefs.h 16217 + printf "%s\n" "#define HAS_CLOCK_GETTIME_NSEC_NP 1" >>confdefs.h 16221 16218 16222 16219 16223 16220 else $as_nop
+2 -2
configure.ac
··· 1484 1484 [*-*-windows], 1485 1485 [has_monotonic_clock=true], 1486 1486 [*-apple-darwin*], [ 1487 - AC_CHECK_FUNCS([mach_timebase_info mach_absolute_time], 1487 + AC_CHECK_FUNCS([clock_gettime_nsec_np], 1488 1488 [ 1489 1489 has_monotonic_clock=true 1490 - AC_DEFINE([HAS_MACH_ABSOLUTE_TIME]) 1490 + AC_DEFINE([HAS_CLOCK_GETTIME_NSEC_NP]) 1491 1491 ], 1492 1492 [has_monotonic_clock=false])], 1493 1493 [AC_COMPILE_IFELSE([AC_LANG_SOURCE([[
+1 -1
runtime/caml/osdeps.h
··· 151 151 millisecond). This makes it useful for benchmarking and timeouts, but not 152 152 for telling the time. The units are always nanoseconds, but the achieved 153 153 resolution may be less. The starting point is unspecified. */ 154 - extern int64_t caml_time_counter(void); 154 + extern uint64_t caml_time_counter(void); 155 155 156 156 extern void caml_init_os_params(void); 157 157
+1 -1
runtime/caml/s.h.in
··· 304 304 305 305 #undef HAS_POSIX_MONOTONIC_CLOCK 306 306 307 - #undef HAS_MACH_ABSOLUTE_TIME 307 + #undef HAS_CLOCK_GETTIME_NSEC_NP 308 308 309 309 #undef HAS_GNU_GETAFFINITY_NP 310 310 #undef HAS_BSD_GETAFFINITY_NP
+9 -18
runtime/unix.c
··· 45 45 #endif 46 46 #ifdef HAS_POSIX_MONOTONIC_CLOCK 47 47 #include <time.h> 48 - #elif HAS_MACH_ABSOLUTE_TIME 49 - #include <mach/mach_time.h> 48 + #elif HAS_CLOCK_GETTIME_NSEC_NP 49 + #include <time.h> 50 50 #endif 51 51 #ifdef HAS_DIRENT 52 52 #include <dirent.h> ··· 431 431 #endif 432 432 } 433 433 434 - int64_t caml_time_counter(void) 434 + uint64_t caml_time_counter(void) 435 435 { 436 - #if defined(HAS_MACH_ABSOLUTE_TIME) 437 - static mach_timebase_info_data_t time_base = {0}; 438 - uint64_t now; 439 - 440 - if (time_base.denom == 0) { 441 - if (mach_timebase_info(&time_base) != KERN_SUCCESS) 442 - return 0; 443 - } 444 - 445 - now = mach_absolute_time(); 446 - return (int64_t)((now * time_base.numer) / time_base.denom); 436 + #if defined(HAS_CLOCK_GETTIME_NSEC_NP) 437 + return (clock_gettime_nsec_np(CLOCK_UPTIME_RAW)); 447 438 #elif defined(HAS_POSIX_MONOTONIC_CLOCK) 448 439 struct timespec t; 449 440 clock_gettime(CLOCK_MONOTONIC, &t); 450 441 return 451 - (int64_t)t.tv_sec * (int64_t)1000000000 + 452 - (int64_t)t.tv_nsec; 442 + (uint64_t)t.tv_sec * (uint64_t)1000000000 + 443 + (uint64_t)t.tv_nsec; 453 444 #elif defined(HAS_GETTIMEOFDAY) 454 445 struct timeval t; 455 446 gettimeofday(&t, 0); 456 447 return 457 - (int64_t)t.tv_sec * (int64_t)1000000000 + 458 - (int64_t)t.tv_usec * (int64_t)1000; 448 + (uint64_t)t.tv_sec * (uint64_t)1000000000 + 449 + (uint64_t)t.tv_usec * (uint64_t)1000; 459 450 #else 460 451 # error "No timesource available" 461 452 #endif
+2 -2
runtime/win32.c
··· 1137 1137 clock_period = (1000000000.0 / frequency.QuadPart); 1138 1138 } 1139 1139 1140 - int64_t caml_time_counter(void) 1140 + uint64_t caml_time_counter(void) 1141 1141 { 1142 1142 LARGE_INTEGER now; 1143 1143 1144 1144 QueryPerformanceCounter(&now); 1145 - return (int64_t)(now.QuadPart * clock_period); 1145 + return (uint64_t)(now.QuadPart * clock_period); 1146 1146 } 1147 1147 1148 1148 void *caml_plat_mem_map(uintnat size, uintnat alignment, int reserve_only)