RunLoop的实现原理

///RunLoop的实现

int CFRunLoopRunSpecific(runloop,modeName,seconds,stopAfterHandle)

 {

    ///首先根据modeName找到对应mode

    CFRunLoopModeRef currentMode = __CFRunLoopFindMode(runloop,modeName,false);

    //如果mode里没有source/timer/observer,直接返回。

    if(__CFRunLoopModeIsEmpty(currentMode))

       return;

    // 1.通知Observers: RunLoop即将进入loop。

    __CFRunLoopDoObservers(runloop,currentMode,kCFRunLoopEntry);

 //内部函数,进入loop

    __CFRunLoopRun(runloop,currentMode,seconds,returnAfterSourceHandled)

 {

       Boolean sourceHandledThisLoop = NO;

        int retVal = 0;

        do {

            // 2.通知Observers: RunLoop即将触发Timer回调。

            __CFRunLoopDoObservers(runloop,currentMode,kCFRunLoopBeforeTimers);

            // 3.通知Observers: RunLoop即将触发Source0(非port)回调。

            __CFRunLoopDoObservers(runloop,currentMode,kCFRunLoopBeforeSources);

            ///执行被加入的block

            __CFRunLoopDoBlocks(runloop,currentMode);

            // 4. RunLoop触发Source0(非port)回调。

            sourceHandledThisLoop = __CFRunLoopDoSources0(runloop,currentMode,stopAfterHandle);

            //执行被加入的block

            __CFRunLoopDoBlocks(runloop,currentMode);

            // 5.如果有Source1(基于port)处于ready状态,直接处理这个Source1然后跳转去处理消息。

            if(__Source0DidDispatchPortLastTime) 

        {

                Boolean hasMsg = __CFRunLoopServiceMachPort(dispatchPort,&msg)

                if(hasMsg)goto handle_msg;

            }

            //通知Observers: RunLoop的线程即将进入休眠(sleep)。

            if(!sourceHandledThisLoop) 

{                __CFRunLoopDoObservers(runloop,currentMode,kCFRunLoopBeforeWaiting);

            }

            // 7.调用mach_msg等待接受mach_port的消息。线程将进入休眠,直到被下面某一个事件唤醒。

            //一个基于port的Source的事件。

            //一个Timer到时间了

            // RunLoop自身的超时时间到了

            //被其他什么调用者手动唤醒

            __CFRunLoopServiceMachPort(waitSet,&msg,sizeof(msg_buffer),&livePort){

                mach_msg(msg,MACH_RCV_MSG,port);// thread wait for receive msg

            }

            // 8.通知Observers: RunLoop的线程刚刚被唤醒了。

            __CFRunLoopDoObservers(runloop,currentMode,kCFRunLoopAfterWaiting);

            //收到消息,处理消息。

            handle_msg:

            // 9.1如果一个Timer到时间了,触发这个Timer的回调。

            if(msg_is_timer){

                __CFRunLoopDoTimers(runloop,currentMode,mach_absolute_time())

            } 

            // 9.2如果有dispatch到main_queue的block,执行block。

            else if(msg_is_dispatch){

                __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__(msg);

            } 

            /// 9.3如果一个Source1(基于port)发出事件了,处理这个事件

            else {

                CFRunLoopSourceRef source1 = __CFRunLoopModeFindSourceForMachPort(runloop,currentMode,livePort);

                sourceHandledThisLoop = __CFRunLoopDoSource1(runloop,currentMode,source1,msg);

                if(sourceHandledThisLoop){

                    mach_msg(reply,MACH_SEND_MSG,reply);

                }

            }

            //执行加入到Loop的block

            __CFRunLoopDoBlocks(runloop,currentMode);

            if(sourceHandledThisLoop && stopAfterHandle){

                ///进入loop时参数说处理完事件就返回。

                retVal = kCFRunLoopRunHandledSource;

            } else if(timeout){

                //超出传入参数标记的超时时间了

                retVal = kCFRunLoopRunTimedOut;

            } else if(__CFRunLoopIsStopped(runloop)){

                //被外部调用者强制停止了

                retVal = kCFRunLoopRunStopped;

            } else if(__CFRunLoopModeIsEmpty(runloop,currentMode)){

                // source/timer/observer一个都没有了

                retVal = kCFRunLoopRunFinished;

            }

            //如果没超时,mode里没空,loop也没被停止,那继续loop。

        } while(retVal == 0);

    }

    // 10.通知Observers: RunLoop即将退出。

    __CFRunLoopDoObservers(rl,currentMode,kCFRunLoopExit);

}

你可能感兴趣的:(RunLoop的实现原理)