Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 1352638

Browse files
authored
mach_absolute_time as the primary clock source on macOS (corefx#30391) (#18505) (#18526)
macOS 10.12+ supports clock_gettime (HAVE_CLOCK_MONOTONIC is defined) However, mach_absolute_time has better resolution and should be used as the primary clock source.
1 parent 0a47706 commit 1352638

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

src/pal/src/misc/time.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,11 @@ QueryPerformanceCounter(
202202
PERF_ENTRY(QueryPerformanceCounter);
203203
ENTRY("QueryPerformanceCounter()\n");
204204
do
205-
#if HAVE_CLOCK_MONOTONIC
205+
#if HAVE_MACH_ABSOLUTE_TIME
206+
{
207+
lpPerformanceCount->QuadPart = (LONGLONG)mach_absolute_time();
208+
}
209+
#elif HAVE_CLOCK_MONOTONIC
206210
{
207211
struct timespec ts;
208212
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
@@ -214,10 +218,6 @@ QueryPerformanceCounter(
214218
lpPerformanceCount->QuadPart =
215219
(LONGLONG)ts.tv_sec * (LONGLONG)tccSecondsToNanoSeconds + (LONGLONG)ts.tv_nsec;
216220
}
217-
#elif HAVE_MACH_ABSOLUTE_TIME
218-
{
219-
lpPerformanceCount->QuadPart = (LONGLONG)mach_absolute_time();
220-
}
221221
#elif HAVE_GETHRTIME
222222
{
223223
lpPerformanceCount->QuadPart = (LONGLONG)gethrtime();
@@ -264,9 +264,7 @@ QueryPerformanceFrequency(
264264
BOOL retval = TRUE;
265265
PERF_ENTRY(QueryPerformanceFrequency);
266266
ENTRY("QueryPerformanceFrequency()\n");
267-
#if HAVE_GETHRTIME || HAVE_READ_REAL_TIME || HAVE_CLOCK_MONOTONIC
268-
lpFrequency->QuadPart = (LONGLONG)tccSecondsToNanoSeconds;
269-
#elif HAVE_MACH_ABSOLUTE_TIME
267+
#if HAVE_MACH_ABSOLUTE_TIME
270268
// use denom == 0 to indicate that s_TimebaseInfo is uninitialised.
271269
if (s_TimebaseInfo.denom == 0)
272270
{
@@ -277,9 +275,11 @@ QueryPerformanceFrequency(
277275
{
278276
lpFrequency->QuadPart = (LONGLONG)tccSecondsToNanoSeconds * ((LONGLONG)s_TimebaseInfo.denom / (LONGLONG)s_TimebaseInfo.numer);
279277
}
278+
#elif HAVE_GETHRTIME || HAVE_READ_REAL_TIME || HAVE_CLOCK_MONOTONIC
279+
lpFrequency->QuadPart = (LONGLONG)tccSecondsToNanoSeconds;
280280
#else
281281
lpFrequency->QuadPart = (LONGLONG)tccSecondsToMicroSeconds;
282-
#endif // HAVE_GETHRTIME || HAVE_READ_REAL_TIME || HAVE_CLOCK_MONOTONIC
282+
#endif // HAVE_MACH_ABSOLUTE_TIME
283283
LOGEXIT("QueryPerformanceFrequency\n");
284284
PERF_EXIT(QueryPerformanceFrequency);
285285
return retval;
@@ -338,7 +338,17 @@ GetTickCount64()
338338
{
339339
ULONGLONG retval = 0;
340340

341-
#if HAVE_CLOCK_MONOTONIC_COARSE || HAVE_CLOCK_MONOTONIC
341+
#if HAVE_MACH_ABSOLUTE_TIME
342+
{
343+
// use denom == 0 to indicate that s_TimebaseInfo is uninitialised.
344+
if (s_TimebaseInfo.denom == 0)
345+
{
346+
ASSERT("s_TimebaseInfo is uninitialized.\n");
347+
goto EXIT;
348+
}
349+
retval = (mach_absolute_time() * s_TimebaseInfo.numer / s_TimebaseInfo.denom) / tccMillieSecondsToNanoSeconds;
350+
}
351+
#elif HAVE_CLOCK_MONOTONIC_COARSE || HAVE_CLOCK_MONOTONIC
342352
{
343353
clockid_t clockType =
344354
#if HAVE_CLOCK_MONOTONIC_COARSE
@@ -354,16 +364,6 @@ GetTickCount64()
354364
}
355365
retval = (ts.tv_sec * tccSecondsToMillieSeconds)+(ts.tv_nsec / tccMillieSecondsToNanoSeconds);
356366
}
357-
#elif HAVE_MACH_ABSOLUTE_TIME
358-
{
359-
// use denom == 0 to indicate that s_TimebaseInfo is uninitialised.
360-
if (s_TimebaseInfo.denom == 0)
361-
{
362-
ASSERT("s_TimebaseInfo is uninitialized.\n");
363-
goto EXIT;
364-
}
365-
retval = (mach_absolute_time() * s_TimebaseInfo.numer / s_TimebaseInfo.denom) / tccMillieSecondsToNanoSeconds;
366-
}
367367
#elif HAVE_GETHRTIME
368368
{
369369
retval = (ULONGLONG)(gethrtime() / tccMillieSecondsToNanoSeconds);

0 commit comments

Comments
 (0)