Skip to content

Commit 26c7f11

Browse files
committed
[runloop] Set up some symbols for generic code.
CFRunLoop has a recurring abstraction for platform-specific code, but it is a little leaky. Plug these leaks: ensure `MACH_PORT_NULL` in the generic, non-platform context is rewritten as `CFPORT_NULL`, and ensure that `kern_return_t` and `KERN_SUCCESS` are defined properly.
1 parent 512e412 commit 26c7f11

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

CoreFoundation/RunLoop.subproj/CFRunLoop.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ typedef int kern_return_t;
158158
#else
159159

160160
static _CFThreadRef const kNilPthreadT = (_CFThreadRef)0;
161+
typedef int kern_return_t;
162+
#define KERN_SUCCESS 0
161163
#define pthreadPointer(a) a
162164
#define lockCount(a) a
163165
#endif
@@ -651,7 +653,7 @@ static void __CFRunLoopModeDeallocate(CFTypeRef cf) {
651653
dispatch_release(rlm->_queue);
652654
}
653655
#endif
654-
if (MACH_PORT_NULL != rlm->_timerPort) mk_timer_destroy(rlm->_timerPort);
656+
if (CFPORT_NULL != rlm->_timerPort) mk_timer_destroy(rlm->_timerPort);
655657
_CFRecursiveMutexDestroy(&rlm->_lock);
656658
memset((char *)cf + sizeof(CFRuntimeBase), 0x7C, sizeof(struct __CFRunLoopMode) - sizeof(CFRuntimeBase));
657659
}
@@ -839,8 +841,8 @@ static CFRunLoopModeRef __CFRunLoopCopyMode(CFRunLoopRef rl, CFStringRef modeNam
839841
#if USE_DISPATCH_SOURCE_FOR_TIMERS
840842
rlm->_timerFired = false;
841843
rlm->_queue = _dispatch_runloop_root_queue_create_4CF("Run Loop Mode Queue", 0);
842-
mach_port_t queuePort = _dispatch_runloop_root_queue_get_port_4CF(rlm->_queue);
843-
if (queuePort == MACH_PORT_NULL) CRASH("*** Unable to create run loop mode queue port. (%d) ***", -1);
844+
__CFPort queuePort = _dispatch_runloop_root_queue_get_port_4CF(rlm->_queue);
845+
if (queuePort == CFPORT_NULL) CRASH("*** Unable to create run loop mode queue port. (%d) ***", -1);
844846
rlm->_timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, rlm->_queue);
845847

846848
__block Boolean *timerFiredPointer = &(rlm->_timerFired);
@@ -857,7 +859,7 @@ static CFRunLoopModeRef __CFRunLoopCopyMode(CFRunLoopRef rl, CFStringRef modeNam
857859

858860
#endif
859861
rlm->_timerPort = mk_timer_create();
860-
if (rlm->_timerPort == MACH_PORT_NULL) {
862+
if (rlm->_timerPort == CFPORT_NULL) {
861863
CRASH("*** Unable to create timer Port (%d) ***", rlm->_timerPort);
862864
}
863865
ret = __CFPortSetInsert(rlm->_timerPort, rlm->_portSet);
@@ -2734,7 +2736,7 @@ static int32_t __CFRunLoopRun(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFTimeInter
27342736
Boolean poll = sourceHandledThisLoop || (0ULL == termTSR);
27352737

27362738
#if __HAS_DISPATCH__
2737-
if (MACH_PORT_NULL != dispatchPort && !didDispatchPortLastTime) {
2739+
if (CFPORT_NULL != dispatchPort && !didDispatchPortLastTime) {
27382740
#if TARGET_OS_MAC
27392741
msg = (mach_msg_header_t *)msg_buffer;
27402742
if (__CFRunLoopServiceMachPort(dispatchPort, &msg, sizeof(msg_buffer), &livePort, 0, &voucherState, NULL, rl, rlm)) {
@@ -2864,7 +2866,7 @@ static int32_t __CFRunLoopRun(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFTimeInter
28642866

28652867

28662868
#endif
2867-
if (MACH_PORT_NULL == livePort) {
2869+
if (CFPORT_NULL == livePort) {
28682870
CFRUNLOOP_WAKEUP_FOR_NOTHING();
28692871
cf_trace(KDEBUG_EVENT_CFRL_DID_WAKEUP_FOR_NOTHING, rl, rlm, livePort, 0);
28702872
// handle nothing
@@ -2887,7 +2889,7 @@ static int32_t __CFRunLoopRun(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFTimeInter
28872889
}
28882890
}
28892891
#endif
2890-
else if (rlm->_timerPort != MACH_PORT_NULL && livePort == rlm->_timerPort) {
2892+
else if (rlm->_timerPort != CFPORT_NULL && livePort == rlm->_timerPort) {
28912893
CFRUNLOOP_WAKEUP_FOR_TIMER();
28922894
// 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.
28932895
// 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

0 commit comments

Comments
 (0)