iOS runloop 源碼流程驗證探究
我們通過在 touchbegin 加一個斷點,然后點擊屏幕,然后再控制臺輸入 bt肤晓,來看見調(diào)用堆棧
bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
* frame #0: 0x000000010d53acfd blogTest`-[ViewController touchesBegan:withEvent:](self=0x00007fa075707410, _cmd="touchesBegan:withEvent:", touches=1 element, event=0x0000600000fe8a00) at ViewController.m:149:5
frame #1: 0x0000000111329863 UIKitCore`forwardTouchMethod + 340
frame #2: 0x00000001113296fe UIKitCore`-[UIResponder touchesBegan:withEvent:] + 49
frame #3: 0x00000001113388de UIKitCore`-[UIWindow _sendTouchesForEvent:] + 1867
frame #4: 0x000000011133a4c6 UIKitCore`-[UIWindow sendEvent:] + 4596
frame #5: 0x000000011131553b UIKitCore`-[UIApplication sendEvent:] + 356
frame #6: 0x000000011139671a UIKitCore`__dispatchPreprocessedEventFromEventQueue + 6847
frame #7: 0x00000001113991e0 UIKitCore`__handleEventQueueInternal + 5980
frame #8: 0x000000010df65471 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
frame #9: 0x000000010df6539c CoreFoundation`__CFRunLoopDoSource0 + 76
frame #10: 0x000000010df64b74 CoreFoundation`__CFRunLoopDoSources0 + 180
frame #11: 0x000000010df5f87f CoreFoundation`__CFRunLoopRun + 1263
frame #12: 0x000000010df5f066 CoreFoundation`CFRunLoopRunSpecific + 438
frame #13: 0x0000000115928bb0 GraphicsServices`GSEventRunModal + 65
frame #14: 0x00000001112fcd4d UIKitCore`UIApplicationMain + 1621
frame #15: 0x000000010d53de94 blogTest`main(argc=1, argv=0x00007ffee26c5d60) at main.m:20:12
frame #16: 0x000000010f8c1c25 libdyld.dylib`start + 1
可以看到 是從 CFRunLoopRunSpecific 這個位置開始調(diào)用 runloop 的.
CFRunLoopRunSpecific
我們看下源碼
SInt32 CFRunLoopRunSpecific(CFRunLoopRef rl, CFStringRef modeName, CFTimeInterval seconds, Boolean returnAfterSourceHandled) { /* DOES CALLOUT */
CHECK_FOR_FORK();
if (__CFRunLoopIsDeallocating(rl)) return kCFRunLoopRunFinished;
__CFRunLoopLock(rl);
CFRunLoopModeRef currentMode = __CFRunLoopFindMode(rl, modeName, false);
if (NULL == currentMode || __CFRunLoopModeIsEmpty(rl, currentMode, rl->_currentMode)) {
Boolean did = false;
if (currentMode) __CFRunLoopModeUnlock(currentMode);
__CFRunLoopUnlock(rl);
return did ? kCFRunLoopRunHandledSource : kCFRunLoopRunFinished;
}
volatile _per_run_data *previousPerRun = __CFRunLoopPushPerRunData(rl);
CFRunLoopModeRef previousMode = rl->_currentMode;
rl->_currentMode = currentMode;
int32_t result = kCFRunLoopRunFinished;
if (currentMode->_observerMask & kCFRunLoopEntry ) __CFRunLoopDoObservers(rl, currentMode, kCFRunLoopEntry);
result = __CFRunLoopRun(rl, currentMode, seconds, returnAfterSourceHandled, previousMode);
if (currentMode->_observerMask & kCFRunLoopExit ) __CFRunLoopDoObservers(rl, currentMode, kCFRunLoopExit);
__CFRunLoopModeUnlock(currentMode);
__CFRunLoopPopPerRunData(rl, previousPerRun);
rl->_currentMode = previousMode;
__CFRunLoopUnlock(rl);
return result;
}
我們可以看到這樣一句話啊
// 通知 observer 進入
if (currentMode->_observerMask & kCFRunLoopEntry ) __CFRunLoopDoObservers(rl, currentMode, kCFRunLoopEntry);
// 開始 run
result = __CFRunLoopRun(rl, currentMode, seconds, returnAfterSourceHandled, previousMode);
// 通知 observer 退出
if (currentMode->_observerMask & kCFRunLoopExit ) __CFRunLoopDoObservers(rl, currentMode, kCFRunLoopExit);
我們接著看下 __CFRunLoopRun 這個函數(shù)
這段源碼特別長,但是我們?yōu)榱丝戳鞒蹋@段代碼里面有這樣一段
// 處理 timer 事件
if (rlm->_observerMask & kCFRunLoopBeforeTimers) __CFRunLoopDoObservers(rl, rlm, kCFRunLoopBeforeTimers);米碰、
// 處理 source 事件
if (rlm->_observerMask & kCFRunLoopBeforeSources) __CFRunLoopDoObservers(rl, rlm, kCFRunLoopBeforeSources);
、// 處理block
__CFRunLoopDoBlocks(rl, rlm);
Boolean poll = sourceHandledThisLoop || (0ULL == timeout_context->termTSR);
// 處理 port购城,也就是source1事件
if (MACH_PORT_NULL != dispatchPort && !didDispatchPortLastTime) {
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
msg = (mach_msg_header_t *)msg_buffer;
if (__CFRunLoopServiceMachPort(dispatchPort, &msg, sizeof(msg_buffer), &livePort, 0)) {
goto handle_msg;
}
#elif DEPLOYMENT_TARGET_WINDOWS
if (__CFRunLoopWaitForMultipleObjects(NULL, &dispatchPort, 0, 0, &livePort, NULL)) {
goto handle_msg;
}
#endif
}
// 即將休眠
if (!poll && (rlm->_observerMask & kCFRunLoopBeforeWaiting)) __CFRunLoopDoObservers(rl, rlm, kCFRunLoopBeforeWaiting);
__CFRunLoopSetSleeping(rl)
// 等待別的線程喚醒
__CFRunLoopServiceMachPort(waitSet, &msg, sizeof(msg_buffer), &livePort, poll ? 0 : TIMEOUT_INFINITY);
// 結(jié)束休眠
__CFRunLoopUnsetSleeping(rl);
if (!poll && (rlm->_observerMask & kCFRunLoopAfterWaiting)) __CFRunLoopDoObservers(rl, rlm, kCFRunLoopAfterWaiting);
可以看到吕座,和我們之前分析的是一樣的,先處理 timer事件瘪板,然后處理 sources 事件吴趴,緊接著處理 block,緊接著判斷是否有 source1 事件,有的話就回去處理侮攀,如果沒有锣枝,就會進入即將休眠事件,然后開始休眠兰英,等待消息喚醒它撇叁,然后結(jié)束休眠,都是通過 __CFRunLoopDoObservers 這個函數(shù)來通知的畦贸。
其中 __CFRunLoopServiceMachPort 是讓當(dāng)前線程休眠陨闹,內(nèi)部調(diào)用了 mach_msg 函數(shù),是個內(nèi)核函數(shù)薄坏,用戶態(tài)向內(nèi)核態(tài)的 轉(zhuǎn)化趋厉,他的作用是等待消息喚醒,沒有消息就讓線程休眠胶坠,有消息了就喚醒線程君账。當(dāng)喚醒的時候又從內(nèi)核態(tài)切換到了用戶態(tài)。