Skip to content

Commit cce3d9a

Browse files
authored
Merge pull request #4622 from 3405691582/MoreTimeTweaks
Attempt to unbreak test timeouts in Windows.
2 parents 1136a6e + cec57d4 commit cce3d9a

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

CoreFoundation/RunLoop.subproj/CFRunLoop.c

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,6 @@ extern void objc_terminate(void);
4848
#define USE_DISPATCH_SOURCE_FOR_TIMERS 0
4949
#endif
5050

51-
static inline uint64_t __CFNanosecondsToTSR(uint64_t ns) {
52-
#if TARGET_OS_MAC || TARGET_OS_LINUX
53-
return ns;
54-
#else
55-
CFTimeInterval ti = ns / 1.0E9;
56-
return __CFTimeIntervalToTSR(ti);
57-
#endif
58-
}
59-
6051
#if USE_DISPATCH_SOURCE_FOR_TIMERS
6152
#if !TARGET_OS_MAC
6253
typedef uint32_t mach_port_t;
@@ -2408,10 +2399,10 @@ static void __CFArmNextTimerInMode(CFRunLoopModeRef rlm, CFRunLoopRef rl) {
24082399

24092400
if (nextSoftDeadline < UINT64_MAX && (nextHardDeadline != rlm->_timerHardDeadline || nextSoftDeadline != rlm->_timerSoftDeadline)) {
24102401
if (CFRUNLOOP_NEXT_TIMER_ARMED_ENABLED()) {
2411-
CFRUNLOOP_NEXT_TIMER_ARMED((unsigned long)(nextSoftDeadline - __CFNanosecondsToTSR(mach_absolute_time())));
2402+
CFRUNLOOP_NEXT_TIMER_ARMED((unsigned long)(nextSoftDeadline - mach_absolute_time()));
24122403
}
24132404

2414-
cf_trace(KDEBUG_EVENT_CFRL_NEXT_TIMER_ARMED, rl, rlm, (nextSoftDeadline - __CFNanosecondsToTSR(mach_absolute_time())), 0);
2405+
cf_trace(KDEBUG_EVENT_CFRL_NEXT_TIMER_ARMED, rl, rlm, (nextSoftDeadline - mach_absolute_time()), 0);
24152406
#if USE_DISPATCH_SOURCE_FOR_TIMERS
24162407
// We're going to hand off the range of allowable timer fire date to dispatch and let it fire when appropriate for the system.
24172408
uint64_t leeway = __CFTSRToNanoseconds(nextHardDeadline - nextSoftDeadline);
@@ -2508,7 +2499,7 @@ static Boolean __CFRunLoopDoTimer(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFRunLo
25082499
CFRetain(rlt);
25092500
__CFRunLoopTimerLock(rlt);
25102501

2511-
if (__CFIsValid(rlt) && rlt->_fireTSR <= __CFNanosecondsToTSR(mach_absolute_time()) && !__CFRunLoopTimerIsFiring(rlt) && rlt->_runLoop == rl) {
2502+
if (__CFIsValid(rlt) && rlt->_fireTSR <= mach_absolute_time() && !__CFRunLoopTimerIsFiring(rlt) && rlt->_runLoop == rl) {
25122503
void *context_info = NULL;
25132504
void (*context_release)(const void *) = NULL;
25142505
if (rlt->_context.retain) {
@@ -2585,7 +2576,7 @@ static Boolean __CFRunLoopDoTimer(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFRunLo
25852576
CRSetCrashLogMessage("A CFRunLoopTimer with an interval of 0 is set to repeat");
25862577
HALT;
25872578
}
2588-
uint64_t currentTSR = __CFNanosecondsToTSR(mach_absolute_time());
2579+
uint64_t currentTSR = mach_absolute_time();
25892580
nextFireTSR = oldFireTSR;
25902581
while (nextFireTSR <= currentTSR) {
25912582
nextFireTSR += intervalTSR;
@@ -2929,7 +2920,7 @@ static Boolean __CFRunLoopWaitForMultipleObjects(__CFPortSet portSet, HANDLE *on
29292920

29302921
/* rl, rlm are locked on entrance and exit */
29312922
static int32_t __CFRunLoopRun(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFTimeInterval seconds, Boolean stopAfterHandle, CFRunLoopModeRef previousMode) {
2932-
uint64_t startTSR = __CFNanosecondsToTSR(mach_absolute_time());
2923+
uint64_t startTSR = mach_absolute_time();
29332924

29342925
if (__CFRunLoopIsStopped(rl)) {
29352926
__CFRunLoopUnsetStopped(rl);
@@ -3186,7 +3177,7 @@ static int32_t __CFRunLoopRun(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFTimeInter
31863177
else if (modeQueuePort != MACH_PORT_NULL && livePort == modeQueuePort) {
31873178
CFRUNLOOP_WAKEUP_FOR_TIMER();
31883179
cf_trace(KDEBUG_EVENT_CFRL_DID_WAKEUP_FOR_TIMER, rl, rlm, livePort, 0);
3189-
if (!__CFRunLoopDoTimers(rl, rlm, __CFNanosecondsToTSR(mach_absolute_time()))) {
3180+
if (!__CFRunLoopDoTimers(rl, rlm, mach_absolute_time())) {
31903181
// Re-arm the next timer, because we apparently fired early
31913182
__CFArmNextTimerInMode(rlm, rl);
31923183
}
@@ -3196,7 +3187,7 @@ static int32_t __CFRunLoopRun(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFTimeInter
31963187
CFRUNLOOP_WAKEUP_FOR_TIMER();
31973188
// On Windows, we have observed an issue where the timer port is set before the time which we requested it to be set. For example, we set the fire time to be TSR 167646765860, but it is actually observed firing at TSR 167646764145, which is 1715 ticks early. The result is that, when __CFRunLoopDoTimers checks to see if any of the run loop timers should be firing, it appears to be 'too early' for the next timer, and no timers are handled.
31983189
// In this case, the timer port has been automatically reset (since it was returned from MsgWaitForMultipleObjectsEx), and if we do not re-arm it, then no timers will ever be serviced again unless something adjusts the timer list (e.g. adding or removing timers). The fix for the issue is to reset the timer here if CFRunLoopDoTimers did not handle a timer itself. 9308754
3199-
if (!__CFRunLoopDoTimers(rl, rlm, __CFNanosecondsToTSR(mach_absolute_time()))) {
3190+
if (!__CFRunLoopDoTimers(rl, rlm, mach_absolute_time())) {
32003191
// Re-arm the next timer
32013192
// Since we'll be resetting the same timer as before
32023193
// with the same deadlines, we need to reset these
@@ -3270,7 +3261,7 @@ static int32_t __CFRunLoopRun(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFTimeInter
32703261

32713262
if (sourceHandledThisLoop && stopAfterHandle) {
32723263
retVal = kCFRunLoopRunHandledSource;
3273-
} else if (termTSR < __CFNanosecondsToTSR(mach_absolute_time())) {
3264+
} else if (termTSR < mach_absolute_time()) {
32743265
retVal = kCFRunLoopRunTimedOut;
32753266
} else if (__CFRunLoopIsStopped(rl)) {
32763267
__CFRunLoopUnsetStopped(rl);
@@ -4527,7 +4518,7 @@ CFRunLoopTimerRef CFRunLoopTimerCreate(CFAllocatorRef allocator, CFAbsoluteTime
45274518
memory->_tolerance = 0.0;
45284519
if (TIMER_DATE_LIMIT < fireDate) fireDate = TIMER_DATE_LIMIT;
45294520
memory->_nextFireDate = fireDate;
4530-
uint64_t now2 = __CFNanosecondsToTSR(mach_absolute_time());
4521+
uint64_t now2 = mach_absolute_time();
45314522
CFAbsoluteTime now1 = CFAbsoluteTimeGetCurrent();
45324523
if (fireDate < now1) {
45334524
memory->_fireTSR = now2;
@@ -4587,7 +4578,7 @@ void CFRunLoopTimerSetNextFireDate(CFRunLoopTimerRef rlt, CFAbsoluteTime fireDat
45874578
if (!__CFIsValid(rlt)) return;
45884579
if (TIMER_DATE_LIMIT < fireDate) fireDate = TIMER_DATE_LIMIT;
45894580
uint64_t nextFireTSR = 0ULL;
4590-
uint64_t now2 = __CFNanosecondsToTSR(mach_absolute_time());
4581+
uint64_t now2 = mach_absolute_time();
45914582
CFAbsoluteTime now1 = CFAbsoluteTimeGetCurrent();
45924583
if (fireDate < now1) {
45934584
nextFireTSR = now2;

0 commit comments

Comments
 (0)