Runloop的再理解(Runloop下)

  • 2019 年 10 月 28 日
  • 笔记

1,Runloop是什么?

我们知道,Runloop可以保证线程不退出,那么,为什么Runloop具有此功效呢?那就要从Runloop的定义说起。

Runloop的字面意思是运行循环,但是我们不要忘了,Runloop其实也是一个对象,它位于CoreFoundation框架下。Runloop对象会提供一个入口函数(定义如下),当程序执行该入口函数后,就会进入一个do-while循环,这就是Runloop能使线程常驻的原因所在

//Runloop中的入口函数  void CFRunLoopRun(void) {  /* DOES CALLOUT */      int32_t result;      do {          result = CFRunLoopRunSpecific(CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 1.0e10, false);          CHECK_FOR_FORK();      } while (kCFRunLoopRunStopped != result && kCFRunLoopRunFinished != result);  }

上面提到,Runloop位于CoreFoundation框架下,其实,说的更为严谨一些的话,Runloop是线程thread内容下的一个小分支,Runloop与线程是息息相关的。

如果找到CFRunloop的源码,我们可以看到,CFRunloop.c的源码也就3900行左右。所以说Runloop其实比Runtime要简单多了!~

CFRunloopRef是一个指针,ref是reference(引用)的缩写,在CoreFoundation框架下,凡是后缀带有ref的类型都是一个指针,比如CFRunloopRef、CFRunloopModeRef、CFRunloopTimerRef、CFRunloopSourceRef、CFRunloopObserverRef这些都是指针。

CFRunloopRef的定义如下:

typedef struct CF_BRIDGED_MUTABLE_TYPE(id) __CFRunLoop * CFRunLoopRef;

这说明,CFRunLoopRef是一个指向__CFRunLoop结构体的指针,__CFRunLoop结构体的定义如下:

struct __CFRunLoop {      CFRuntimeBase _base;      pthread_mutex_t _lock;  /* locked for accessing mode list */      __CFPort _wakeUpPort;   // 内核向该端口发送消息可以唤醒runloop      Boolean _unused;      volatile _per_run_data *_perRunData; // reset for runs of the run loop      pthread_t _pthread;             //RunLoop对应的线程      uint32_t _winthread;      CFMutableSetRef _commonModes;    //存储的是字符串,记录所有标记为common的mode      CFMutableSetRef _commonModeItems;//存储所有commonMode的item(source、timer、observer)      CFRunLoopModeRef _currentMode;   //当前运行的mode      CFMutableSetRef _modes;          //存储的是CFRunLoopModeRef      struct _block_item *_blocks_head;//doblocks的时候用到      struct _block_item *_blocks_tail;      CFTypeRef _counterpart;  };

2,Runloop Mode

CFRunloopModeRef的定义如下:

typedef struct __CFRunLoopMode * CFRunLoopModeRef;

这说明,CFRunLoopModeRef是一个指向__CFRunLoopMode结构体的指针,__CFRunLoopMode结构体的定义如下:

struct __CFRunLoopMode {      CFRuntimeBase _base;      pthread_mutex_t _lock;  /* must have the run loop locked before locking this */      CFStringRef _name;   //mode名称      Boolean _stopped;    //mode是否被终止      char _padding[3];      //几种事件      CFMutableSetRef _sources0;  //sources0      CFMutableSetRef _sources1;  //sources1      CFMutableArrayRef _observers; //通知      CFMutableArrayRef _timers;    //定时器      CFMutableDictionaryRef _portToV1SourceMap; //字典  key是mach_port_t,value是CFRunLoopSourceRef      __CFPortSet _portSet; //保存所有需要监听的port,比如_wakeUpPort,_timerPort都保存在这个数组中      CFIndex _observerMask;  #if USE_DISPATCH_SOURCE_FOR_TIMERS      dispatch_source_t _timerSource;      dispatch_queue_t _queue;      Boolean _timerFired; // set to true by the source when a timer has fired      Boolean _dispatchTimerArmed;  #endif  #if USE_MK_TIMER_TOO      mach_port_t _timerPort;      Boolean _mkTimerArmed;  #endif  #if DEPLOYMENT_TARGET_WINDOWS      DWORD _msgQMask;      void (*_msgPump)(void);  #endif      uint64_t _timerSoftDeadline; /* TSR */      uint64_t _timerHardDeadline; /* TSR */  };

Mode可以视为事件的管家,一个Mode管理着各种事件。以上代码可见,一个CFRunLoopMode有一个name,若干sources0、sources1、observer、timer和若干port,可见事件都是由Mode在管理,而Runloop管理Mode

从源码很容易看出,Runloop总是运行在某种特定的CFRunLoopModeRef下,因为每次运行__CFRunLoopRun()函数的时候都必须要指定Mode,如下:

static int32_t __CFRunLoopRun(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFTimeInterval seconds, Boolean stopAfterHandle, CFRunLoopModeRef previousMode) __attribute__((noinline));

通过CFRunloopRef对应结构体的定义可知,每个Runloop都可以包含若干个Mode,每个Mode又包含多个source/observer/timer。每次运行__CFRunLoopRun()函数的时候都必须要指定Mode,这个Mode就是_currentMode。当切换Mode时必须退出当前Mode,然后重新进入Runloop以保证不同Mode的observer/timer/source互不影响。

3,Runloop的作用

我们现在可以知道,Runloop有如下两个作用:

(1)使线程常驻,保证线程不退出

(2)处理程序中的各种事件,比如timer事件、UI事件、网络事件等

其实,除了上述两点之外,Runloop还有一个很重要的作用:节约CPU资源,提高程序的性能(当有事件需要处理的时候,Runloop就去处理;当没有事件的时候,Runloop就会休息,不再占用CPU)

Runloop是一个循环,但是它这个循环比较高级。一般的while循环会导致CPU进入忙等待状态,而Runloop是一种“闲等待”。Runloop不会一直占据CPU资源,只有当有事件需要处理的时候,才会占用部分CPU,当没有事件的时候,Runloop就会休息,不再占据CPU。这也是Runloop设计精巧的地方。

4,Runloop与线程的关系

第一,Runloop与线程是一一对应的。在内部,二者是根据key-value的形式存在NSMutableDictionary中的。

第二,线程的Runloop是依附于当前线程的。也就是说,当前线程的Runloop是需要根据当前线程来创建的

第三,除了程序的主线程之外,其他各个子线程的Runloop都是需要自己手动开启的(主线程的runloop由系统为我们自动开启)。

5,Runloop Source

Runloop Source分为Source、Observer、Timer三种,他们统称为ModeItem。

(1)CFRunloopSource

根据官方的描述,CFRunloopSource是对input sources的抽象。CFRunloopSource分为source0和source1两个版本,它的结构如下:

struct __CFRunLoopSource {      CFRuntimeBase _base;      uint32_t _bits; //用于标记Signaled状态,source0只有在被标记为Signaled状态,才会被处理      pthread_mutex_t _lock;      CFIndex _order;         /* immutable */      CFMutableBagRef _runLoops;      union {          CFRunLoopSourceContext version0;     /* immutable, except invalidation */          CFRunLoopSourceContext1 version1;    /* immutable, except invalidation */      } _context;  };

source0是非系统内核事件,即App内部事件,由App自己管理的UIEvent、CFSocket都是 source0当一个source0事件准备执行的时候,必须要先把它标记为signal状态。以下是source0的结构体:

typedef struct {      CFIndex version;      void *  info;      const void *(*retain)(const void *info);      void    (*release)(const void *info);      CFStringRef (*copyDescription)(const void *info);      Boolean (*equal)(const void *info1, const void *info2);      CFHashCode  (*hash)(const void *info);      void    (*schedule)(void *info, CFRunLoopRef rl, CFStringRef mode);      void    (*cancel)(void *info, CFRunLoopRef rl, CFStringRef mode);      void    (*perform)(void *info);  } CFRunLoopSourceContext;

source0是非基于port的。只包含了一个回调(函数指针),它并不能主动触发事件。使用的时候,你需要先调用CFRunLoopSourceSignal(CFRunLoopSourceRef source),将这个source标记为待处理,然后手动调用CFRunLoopWakeUp(CFRunLoopRef rl)来唤醒Runloop,让它来处理这个事件

source1是系统内核事件,它由runloop和内核管理,source1带有mach_port_t,可以接收内核消息并触发回调,以下是soure1的结构体:

typedef struct {      CFIndex version;      void *  info;      const void *(*retain)(const void *info);      void    (*release)(const void *info);      CFStringRef (*copyDescription)(const void *info);      Boolean (*equal)(const void *info1, const void *info2);      CFHashCode  (*hash)(const void *info);  #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) || (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)      mach_port_t (*getPort)(void *info);      void *  (*perform)(void *msg, CFIndex size, CFAllocatorRef allocator, void *info);  #else      void *  (*getPort)(void *info);      void    (*perform)(void *info);  #endif  } CFRunLoopSourceContext1;

source1除了包含回调指针外,还包含一个mach_port_t,source1可以监听系统端口,通过内核和其他线程通信,接收、分发系统事件,它能够主动唤醒Runloop

(2)CFRunloopObserver

CFRunloopObserver是观察者,可以观察Runloop的各种状态,并抛出回调。其结构体定义如下:

struct __CFRunLoopObserver {      CFRuntimeBase _base;      pthread_mutex_t _lock;      CFRunLoopRef _runLoop;      CFIndex _rlCount;      CFOptionFlags _activities;      /* immutable */      CFIndex _order;         /* immutable */      CFRunLoopObserverCallBack _callout; /* immutable */      CFRunLoopObserverContext _context;  /* immutable, except invalidation */  };

CFRunloopObserver可以观察的状态有如下6种:

/* Run Loop Observer Activities */  typedef CF_OPTIONS(CFOptionFlags, CFRunLoopActivity) {      kCFRunLoopEntry = (1UL << 0), //即将进入run loop      kCFRunLoopBeforeTimers = (1UL << 1), //即将处理timer      kCFRunLoopBeforeSources = (1UL << 2),//即将处理source      kCFRunLoopBeforeWaiting = (1UL << 5),//即将进入休眠      kCFRunLoopAfterWaiting = (1UL << 6),//被唤醒但是还没开始处理事件      kCFRunLoopExit = (1UL << 7),//run loop已经退出      kCFRunLoopAllActivities = 0x0FFFFFFFU  };

Runloop通过监控Source来决定有没有任务要做,除此之外,我们还可以用Runloop Observer来监控Runloop本身的状态。Runloop可以监控上面的Runloop事件,具体流程如下图:

(3)CFRunloopTimer

CFRunloopTimer是定时器,可以在设定的时间点抛出回调,它的结构如下:

struct __CFRunLoopTimer {      CFRuntimeBase _base;      uint16_t _bits;  //标记fire状态      pthread_mutex_t _lock;      CFRunLoopRef _runLoop;        //添加该timer的runloop      CFMutableSetRef _rlModes;     //存放所有 包含该timer的 mode的 modeName,意味着一个timer可能会在多个mode中存在      CFAbsoluteTime _nextFireDate;      CFTimeInterval _interval;     //理想时间间隔  /* immutable */      CFTimeInterval _tolerance;    //时间偏差      /* mutable */      uint64_t _fireTSR;          /* TSR units */      CFIndex _order;         /* immutable */      CFRunLoopTimerCallBack _callout;    /* immutable */      CFRunLoopTimerContext _context; /* immutable, except invalidation */  };

CFRunloopTimer和NSTimer是toll-free bridge的,可以混用

CFRunloopTimer包含一个时间长度和一个回调(函数指针),当其加入到Runloop时,Runloop会注册对应的时间点,当时间点到时,Runloop会被唤醒以执行那个回调


接下来我将花很大的一个篇幅去介绍Runloop的源码。

Runloop源码解析

<一>Runloop实现

1,获取Runloop

从苹果开放的API来看,不允许我们直接创建Runloop对象,只能通过以下几个函数来获取Runloop:

  • CFRunloopRef CFRunloopGetCurrent(void)
  • CFRunloopRef CFRunloopGetMain(void)
  • +(NSRunloop *)currentRunloop
  • +(NSRunloop *)mainRunloop

前两个是CoreFoundation中的API,后两个是Foundation中的API。

那么Runloop是什么时候被创建的呢?我们从下面几个函数内部看看。

(1)CFRunloopGetCurrent

//取当前所在线程的RunLoop  CFRunLoopRef CFRunLoopGetCurrent(void) {      CHECK_FOR_FORK();      CFRunLoopRef rl = (CFRunLoopRef)_CFGetTSD(__CFTSDKeyRunLoop);      if (rl) return rl;      //传入当前线程      return _CFRunLoopGet0(pthread_self());  }

在CFRunLoopGetCurrent函数内部调用了_CFRunLoopGet0,传入的参数是当前线程pthread_self()。这里可以看出,CFRunLoopGetCurrent函数必须要在线程内部调用,才能获取到当前线程的Runloop。也就是说,子线程的Runloop必须要在子线程内部获取。

(2)CFRunloopGetMain

//取主线程的RunLoop  CFRunLoopRef CFRunLoopGetMain(void) {      CHECK_FOR_FORK();      static CFRunLoopRef __main = NULL; // no retain needed      //传入主线程      if (!__main) __main = _CFRunLoopGet0(pthread_main_thread_np()); // no CAS needed      return __main;  }

在CFRunLoopGetMain函数内部也调用了_CFRunLoopGet0(),传入的参数是主线程pthread_main_thread_np()。可以看出,CFRunLoopGetMain函数不管是在主线程还是在子线程中调用,都可以获取到主线程的Runloop

(3)CFRunloopGet0

前面两个函数都是使用了CFRunloopGet0实现传入线程参数的函数,下面我们来看看CFRunloopGet0的结构是怎样的:

static CFMutableDictionaryRef __CFRunLoops = NULL;  static CFSpinLock_t loopsLock = CFSpinLockInit;    // t==0 is a synonym for "main thread" that always works  //根据线程取RunLoop  CF_EXPORT CFRunLoopRef _CFRunLoopGet0(pthread_t t) {      if (pthread_equal(t, kNilPthreadT)) {          t = pthread_main_thread_np();      }      __CFSpinLock(&loopsLock);      //如果存储RunLoop的字典不存在      if (!__CFRunLoops) {          __CFSpinUnlock(&loopsLock);          //创建一个临时字典dict          CFMutableDictionaryRef dict = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, NULL, &kCFTypeDictionaryValueCallBacks);          //创建主线程的RunLoop          CFRunLoopRef mainLoop = __CFRunLoopCreate(pthread_main_thread_np());          //把主线程的RunLoop保存到dict中,key是线程,value是RunLoop          CFDictionarySetValue(dict, pthreadPointer(pthread_main_thread_np()), mainLoop);          //此处NULL和__CFRunLoops指针都指向NULL,匹配,所以将dict写到__CFRunLoops          if (!OSAtomicCompareAndSwapPtrBarrier(NULL, dict, (void * volatile *)&__CFRunLoops)) {              //释放dict              CFRelease(dict);          }          //释放mainrunloop          CFRelease(mainLoop);          __CFSpinLock(&loopsLock);      }      //以上说明,第一次进来的时候,不管是getMainRunloop还是get子线程的runloop,主线程的runloop总是会被创建      //从字典__CFRunLoops中获取传入线程t的runloop      CFRunLoopRef loop = (CFRunLoopRef)CFDictionaryGetValue(__CFRunLoops, pthreadPointer(t));      __CFSpinUnlock(&loopsLock);      //如果没有获取到      if (!loop) {          //根据线程t创建一个runloop          CFRunLoopRef newLoop = __CFRunLoopCreate(t);          __CFSpinLock(&loopsLock);          loop = (CFRunLoopRef)CFDictionaryGetValue(__CFRunLoops, pthreadPointer(t));          if (!loop) {              //把newLoop存入字典__CFRunLoops,key是线程t              CFDictionarySetValue(__CFRunLoops, pthreadPointer(t), newLoop);              loop = newLoop;          }          // don't release run loops inside the loopsLock, because CFRunLoopDeallocate may end up taking it          __CFSpinUnlock(&loopsLock);          CFRelease(newLoop);      }      //如果传入线程就是当前线程      if (pthread_equal(t, pthread_self())) {          _CFSetTSD(__CFTSDKeyRunLoop, (void *)loop, NULL);          if (0 == _CFGetTSD(__CFTSDKeyRunLoopCntr)) {              //注册一个回调,当线程销毁时,销毁对应的RunLoop              _CFSetTSD(__CFTSDKeyRunLoopCntr, (void *)(PTHREAD_DESTRUCTOR_ITERATIONS-1), (void (*)(void *))__CFFinalizeRunLoop);          }      }      return loop;  }

这段代码可以得出以下结论:

  • Runloop和线程是一一对应的,对应的方式是以key-value的方式保存在一个全局字典中,key是线程,value是runloop。
  • 不管获取子线程的runloop还是获取主线程的runloop,当存储线程与runloop对应关系的全局字典不存在时,都会创建并初始化该全局字典。而在初始化存储线程与runloop对应关系的全局字典时,主线程的Runloop会被创建
  • 子线程的runloop会在第一次被获取的时候创建,如果不获取的话就一直不会被创建
  • Runloop会在线程销毁的时候销毁

2,添加Mode

在CoreFoundation中,针对Mode的操作,苹果只开放了3个API(Cocoa中也有功能一样的函数,不再列出):

  • CFRunloopAddCommonMode(CFRunloopRef rl, CFStringRef mode)
  • CFStringRef CFRunloopCopyCurrentMode(CFRunloopRef rl)
  • CFArrayRef CFRunloopCopyAllModes(CFRunloopRef rl)
CFRunLoopAddCommonMode  Adds a mode to the set of run loop common modes.(向当前RunLoop的common modes中添加一个mode)    CFRunLoopCopyCurrentMode  Returns the name of the mode in which a given run loop is currently running.(返回当前运行的mode的name)    CFRunLoopCopyAllModes  Returns an array that contains all the defined modes for a CFRunLoop object.(返回当前RunLoop的所有mode)

我们没有办法直接创建一个CFRunloopMode对象,但是我们可以调用CFRunLoopAddCommonMode传入一个字符串向Runloop中添加Mode,传入的字符串即为Mode的名字,Mode对象应该是此时在Runloop内部创建的。下面看源码:

(1)CFRunloopAddCommonMode

void CFRunLoopAddCommonMode(CFRunLoopRef rl, CFStringRef modeName) {      CHECK_FOR_FORK();      if (__CFRunLoopIsDeallocating(rl)) return;      __CFRunLoopLock(rl);      //看rl中是否已经有这个mode,如果有就什么都不做      if (!CFSetContainsValue(rl->_commonModes, modeName)) {          CFSetRef set = rl->_commonModeItems ? CFSetCreateCopy(kCFAllocatorSystemDefault, rl->_commonModeItems) : NULL;          //把modeName添加到RunLoop的_commonModes中          CFSetAddValue(rl->_commonModes, modeName);          if (NULL != set) {              CFTypeRef context[2] = {rl, modeName};              /* add all common-modes items to new mode */              //这里调用CFRunLoopAddSource/CFRunLoopAddObserver/CFRunLoopAddTimer的时候会调用              //__CFRunLoopFindMode(rl, modeName, true),CFRunLoopMode对象在这个时候被创建              CFSetApplyFunction(set, (__CFRunLoopAddItemsToCommonMode), (void *)context);              CFRelease(set);          }      } else {      }      __CFRunLoopUnlock(rl);  }

可以看出:

  • modeName不能重复,modeName是mode的唯一标识符
  • Runloop的_commonModes数组存放所有被标记为common的mode的名称
  • 添加commonMode会把_commonModeItems数组中的所有source同步到新添加的Mode中
  • CFRunLoopMode对象在__CFRunLoopAddItemsToCommonMode函数中调用__CFRunLoopFindMode时被创建

(2)CFRunloopCopyCurrentMode/CFRunloopCopyAllModes

CFRunLoopCopyCurrentMode和CFRunLoopCopyAllModes的内部逻辑比较简单,直接取Runloop的_currentMode和_modes返回即可。

3,添加RunloopSource(Mode Item)

我们可以通过以下接口添加/移除各种事件:

  • void CFRunLoopAddSource(CFRunLoopRef rl, CFRunLoopSourceRef source, CFStringRef mode)
  • void CFRunLoopRemoveSource(CFRunLoopRef rl, CFRunLoopSourceRef source, CFStringRef mode)
  • void CFRunLoopAddObserver(CFRunLoopRef rl, CFRunLoopObserverRef observer, CFStringRef mode)
  • void CFRunLoopRemoveObserver(CFRunLoopRef rl, CFRunLoopObserverRef observer, CFStringRef * mode)
  • void CFRunLoopAddTimer(CFRunLoopRef rl, CFRunLoopTimerRef timer, CFStringRef mode)
  • void CFRunLoopRemoveTimer(CFRunLoopRef rl, CFRunLoopTimerRef timer, CFStringRef mode)

(1)CFRunloopAddSource

CFRunloopAddSource的代码结构如下:

//添加source事件  void CFRunLoopAddSource(CFRunLoopRef rl, CFRunLoopSourceRef rls, CFStringRef modeName) {    /* DOES CALLOUT */      CHECK_FOR_FORK();      if (__CFRunLoopIsDeallocating(rl)) return;      if (!__CFIsValid(rls)) return;      Boolean doVer0Callout = false;      __CFRunLoopLock(rl);      //如果是kCFRunLoopCommonModes      if (modeName == kCFRunLoopCommonModes) {          //如果runloop的_commonModes存在,则copy一个新的复制给set          CFSetRef set = rl->_commonModes ? CFSetCreateCopy(kCFAllocatorSystemDefault, rl->_commonModes) : NULL;         //如果runl _commonModeItems为空          if (NULL == rl->_commonModeItems) {              //先初始化              rl->_commonModeItems = CFSetCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeSetCallBacks);          }          //把传入的CFRunLoopSourceRef加入_commonModeItems          CFSetAddValue(rl->_commonModeItems, rls);          //如果刚才set copy到的数组里有数据          if (NULL != set) {              CFTypeRef context[2] = {rl, rls};              /* add new item to all common-modes */              //则把set里的所有mode都执行一遍__CFRunLoopAddItemToCommonModes函数              CFSetApplyFunction(set, (__CFRunLoopAddItemToCommonModes), (void *)context);              CFRelease(set);          }          //以上分支的逻辑就是,如果你往kCFRunLoopCommonModes里面添加一个source,那么所有_commonModes里的mode都会添加这个source      } else {          //根据modeName查找mode          CFRunLoopModeRef rlm = __CFRunLoopFindMode(rl, modeName, true);          //如果_sources0不存在,则初始化_sources0,_sources0和_portToV1SourceMap          if (NULL != rlm && NULL == rlm->_sources0) {              rlm->_sources0 = CFSetCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeSetCallBacks);              rlm->_sources1 = CFSetCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeSetCallBacks);              rlm->_portToV1SourceMap = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, NULL, NULL);          }          //如果_sources0和_sources1中都不包含传入的source          if (NULL != rlm && !CFSetContainsValue(rlm->_sources0, rls) && !CFSetContainsValue(rlm->_sources1, rls)) {              //如果version是0,则加到_sources0              if (0 == rls->_context.version0.version) {                  CFSetAddValue(rlm->_sources0, rls);                  //如果version是1,则加到_sources1              } else if (1 == rls->_context.version0.version) {                  CFSetAddValue(rlm->_sources1, rls);                  __CFPort src_port = rls->_context.version1.getPort(rls->_context.version1.info);                  if (CFPORT_NULL != src_port) {                      //此处只有在加到source1的时候才会把souce和一个mach_port_t对应起来                      //可以理解为,source1可以通过内核向其端口发送消息来主动唤醒runloop                      CFDictionarySetValue(rlm->_portToV1SourceMap, (const void *)(uintptr_t)src_port, rls);                      __CFPortSetInsert(src_port, rlm->_portSet);                  }              }              __CFRunLoopSourceLock(rls);              //把runloop加入到source的_runLoops中              if (NULL == rls->_runLoops) {                  rls->_runLoops = CFBagCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeBagCallBacks); // sources retain run loops!              }              CFBagAddValue(rls->_runLoops, rl);              __CFRunLoopSourceUnlock(rls);              if (0 == rls->_context.version0.version) {                  if (NULL != rls->_context.version0.schedule) {                      doVer0Callout = true;                  }              }          }          if (NULL != rlm) {              __CFRunLoopModeUnlock(rlm);          }      }      __CFRunLoopUnlock(rl);      if (doVer0Callout) {          // although it looses some protection for the source, we have no choice but          // to do this after unlocking the run loop and mode locks, to avoid deadlocks          // where the source wants to take a lock which is already held in another          // thread which is itself waiting for a run loop/mode lock          rls->_context.version0.schedule(rls->_context.version0.info, rl, modeName); /* CALLOUT */      }  }

通过添加Source的这段代码可以得出如下结论:

  • 如果modeName传入kCFRunLoopCommonModes,则该source会被保存到RunLoop的_commonModeItems中
  • 如果modeName传入kCFRunLoopCommonModes,则该source会被添加到所有commonMode中
  • 如果modeName传入的不是kCFRunLoopCommonModes,则会先查找该Mode,如果没有,会创建一个
  • 同一个source在一个mode中只能被添加一次

(2)CFRunloopRemoveSource

remove操作与add操作的逻辑基本一致,很容易理解。

//移除source  void CFRunLoopRemoveSource(CFRunLoopRef rl, CFRunLoopSourceRef rls, CFStringRef modeName) { /* DOES CALLOUT */      CHECK_FOR_FORK();      Boolean doVer0Callout = false, doRLSRelease = false;      __CFRunLoopLock(rl);      //如果是kCFRunLoopCommonModes,则从_commonModes的所有mode中移除该source      if (modeName == kCFRunLoopCommonModes) {          if (NULL != rl->_commonModeItems && CFSetContainsValue(rl->_commonModeItems, rls)) {              CFSetRef set = rl->_commonModes ? CFSetCreateCopy(kCFAllocatorSystemDefault, rl->_commonModes) : NULL;              CFSetRemoveValue(rl->_commonModeItems, rls);              if (NULL != set) {                  CFTypeRef context[2] = {rl, rls};                  /* remove new item from all common-modes */                  CFSetApplyFunction(set, (__CFRunLoopRemoveItemFromCommonModes), (void *)context);                  CFRelease(set);              }          } else {          }      } else {          //根据modeName查找mode,如果不存在,返回NULL          CFRunLoopModeRef rlm = __CFRunLoopFindMode(rl, modeName, false);          if (NULL != rlm && ((NULL != rlm->_sources0 && CFSetContainsValue(rlm->_sources0, rls)) || (NULL != rlm->_sources1 && CFSetContainsValue(rlm->_sources1, rls)))) {              CFRetain(rls);              //根据source版本做对应的remove操作              if (1 == rls->_context.version0.version) {                  __CFPort src_port = rls->_context.version1.getPort(rls->_context.version1.info);                  if (CFPORT_NULL != src_port) {                      CFDictionaryRemoveValue(rlm->_portToV1SourceMap, (const void *)(uintptr_t)src_port);                      __CFPortSetRemove(src_port, rlm->_portSet);                  }              }              CFSetRemoveValue(rlm->_sources0, rls);              CFSetRemoveValue(rlm->_sources1, rls);              __CFRunLoopSourceLock(rls);              if (NULL != rls->_runLoops) {                  CFBagRemoveValue(rls->_runLoops, rl);              }              __CFRunLoopSourceUnlock(rls);              if (0 == rls->_context.version0.version) {                  if (NULL != rls->_context.version0.cancel) {                      doVer0Callout = true;                  }              }              doRLSRelease = true;          }          if (NULL != rlm) {              __CFRunLoopModeUnlock(rlm);          }      }      __CFRunLoopUnlock(rl);      if (doVer0Callout) {          // although it looses some protection for the source, we have no choice but          // to do this after unlocking the run loop and mode locks, to avoid deadlocks          // where the source wants to take a lock which is already held in another          // thread which is itself waiting for a run loop/mode lock          rls->_context.version0.cancel(rls->_context.version0.info, rl, modeName);   /* CALLOUT */      }      if (doRLSRelease) CFRelease(rls);  }

(3)添加Observer和Timer

添加Observer和Timer的内部逻辑和添加Source大体类似。

区别在于observer和timer只能被添加到一个Runloop的一个或或多个mode中。比如一个timer被添加到了主线程的runloop中,则不能再把该timer添加到子线程的runloop;而source则没有这个限制,不管是哪个runloop,只要是mode中没有,就可以添加。

这个区别在Source、Timer和Observer的结构体定义中也可以发现,CFRunloopSource结构体中有保存Runloop对象的数组,而CFRunloopTimer和CFRunloopSourceObserver只有单个runloop对象。

<二>Runloop运行

在CoreFoundation中,我们可以通过以下两个API让runloop运行:

  • void CFRunLoopRun(void)

在默认的mode下运行当前线程的RunLoop。

  • CFRunLoopRunResult CFRunLoopRunInMode(CFStringRef mode, CFTimeInterval seconds, Boolean returnAfterSourceHandled)

在指定mode下运行当前线程的RunLoop。

1,CFRunloopRun

//默认运行runloop的kCFRunLoopDefaultMode  void CFRunLoopRun(void) {   /* DOES CALLOUT */      int32_t result;      do {          //默认在kCFRunLoopDefaultMode下运行runloop          result = CFRunLoopRunSpecific(CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 1.0e10, false);          CHECK_FOR_FORK();      } while (kCFRunLoopRunStopped != result && kCFRunLoopRunFinished != result);  }

在CFRunLoopRun函数中调用了CFRunLoopRunSpecific函数,runloop参数传入当前runloop对象,modeName参数传入kCFRunloopDefaultMode,这说明是在默认mode下运行当前线程的runloop

2,CFRunloopRunInMode

SInt32 CFRunLoopRunInMode(CFStringRef modeName, CFTimeInterval seconds, Boolean returnAfterSourceHandled) {     /* DOES CALLOUT */      CHECK_FOR_FORK();      return CFRunLoopRunSpecific(CFRunLoopGetCurrent(), modeName, seconds, returnAfterSourceHandled);  }

在CFRunLoopRunInMode函数中也调用了CFRunLoopRunSpecific函数,runloop参数传入当前Runloop对象,modeName参数继续传递CFRunLoopRunInMode中传入的modeName,这说明,CFRunLoopRunInMode是在指定mode下运行当前线程的runloop

在这里,我们也可以看出,虽然Runloop有很多个Mode,但是runloop在run的时候必须只能指定其中一个mode,被指定的mode即为currentMode

上面这两个函数都看不出来runloop是怎么run起来的,接下来我们继续探索CFRunLoopRunSpecific函数里面都干了什么,看看runloop具体是怎么run的。

3,CFRunloopRunSpecific

/*   * 指定mode运行runloop   * @param rl 当前运行的runloop   * @param modeName 需要运行的mode的name   * @param seconds  runloop的超时时间   * @param returnAfterSourceHandled 是否处理完事件就返回   */  SInt32 CFRunLoopRunSpecific(CFRunLoopRef rl, CFStringRef modeName, CFTimeInterval seconds, Boolean returnAfterSourceHandled) {     /* DOES CALLOUT */      CHECK_FOR_FORK();      if (__CFRunLoopIsDeallocating(rl)) return kCFRunLoopRunFinished;      __CFRunLoopLock(rl);      //根据modeName找到本次运行的mode      CFRunLoopModeRef currentMode = __CFRunLoopFindMode(rl, modeName, false);      //如果没找到 || mode中没有注册任何事件,则就此停止,不进入循环      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);      //取上一次运行的mode      CFRunLoopModeRef previousMode = rl->_currentMode;      //如果本次mode和上次的mode一致      rl->_currentMode = currentMode;      //初始化一个result为kCFRunLoopRunFinished      int32_t result = kCFRunLoopRunFinished;        // 1.通知observer即将进入runloop      if (currentMode->_observerMask & kCFRunLoopEntry ) __CFRunLoopDoObservers(rl, currentMode, kCFRunLoopEntry);      result = __CFRunLoopRun(rl, currentMode, seconds, returnAfterSourceHandled, previousMode);      //10.通知observer已退出runloop      if (currentMode->_observerMask & kCFRunLoopExit ) __CFRunLoopDoObservers(rl, currentMode, kCFRunLoopExit);        __CFRunLoopModeUnlock(currentMode);      __CFRunLoopPopPerRunData(rl, previousPerRun);      rl->_currentMode = previousMode;      __CFRunLoopUnlock(rl);      return result;  }

通过CFRunloopRunSpecific的内部逻辑,我们可以得出:

  • 如果指定了一个不存在的mode来运行runloop,那么会失败,mode不会被创建,所以这里传入的mode必须是存在的
  • 如果指定了一个mode,但这个mode中不包含任何的modeItem,那么Runloop也不会运行,所以必须要传入至少包含一个modeItem的mode。
  • 在进入runloop之前通知observer,状态是kCFRunLoopEntry
  • 在退出runloop之后通知observer,状态是kCFRunLoopExit

runloop运行的最核心的函数是__CFRunLoopRun,接下来我们分析__CFRunLoopRun的源码。

4,__CFRunloopRun

/**   *  运行run loop   *   *  @param rl              运行的RunLoop对象   *  @param rlm             运行的mode   *  @param seconds         run loop超时时间   *  @param stopAfterHandle true:run loop处理完事件就退出  false:一直运行直到超时或者被手动终止   *  @param previousMode    上一次运行的mode   *   *  @return 返回4种状态   */  static int32_t __CFRunLoopRun(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFTimeInterval seconds, Boolean stopAfterHandle, CFRunLoopModeRef previousMode) {      //获取系统启动后的CPU运行时间,用于控制超时时间      uint64_t startTSR = mach_absolute_time();        //如果RunLoop或者mode是stop状态,则直接return,不进入循环      if (__CFRunLoopIsStopped(rl)) {          __CFRunLoopUnsetStopped(rl);          return kCFRunLoopRunStopped;      } else if (rlm->_stopped) {          rlm->_stopped = false;          return kCFRunLoopRunStopped;      }        //mach端口,在内核中,消息在端口之间传递。初始为0      mach_port_name_t dispatchPort = MACH_PORT_NULL;      //判断是否为主线程      Boolean libdispatchQSafe = pthread_main_np() && ((HANDLE_DISPATCH_ON_BASE_INVOCATION_ONLY && NULL == previousMode) || (!HANDLE_DISPATCH_ON_BASE_INVOCATION_ONLY && 0 == _CFGetTSD(__CFTSDKeyIsInGCDMainQ)));      //如果在主线程 && runloop是主线程的runloop && 该mode是commonMode,则给mach端口赋值为主线程收发消息的端口      if (libdispatchQSafe && (CFRunLoopGetMain() == rl) && CFSetContainsValue(rl->_commonModes, rlm->_name)) dispatchPort = _dispatch_get_main_queue_port_4CF();    #if USE_DISPATCH_SOURCE_FOR_TIMERS      mach_port_name_t modeQueuePort = MACH_PORT_NULL;      if (rlm->_queue) {          //mode赋值为dispatch端口_dispatch_runloop_root_queue_perform_4CF          modeQueuePort = _dispatch_runloop_root_queue_get_port_4CF(rlm->_queue);          if (!modeQueuePort) {              CRASH("Unable to get port for run loop mode queue (%d)", -1);          }      }  #endif        //GCD管理的定时器,用于实现runloop超时机制      dispatch_source_t timeout_timer = NULL;      struct __timeout_context *timeout_context = (struct __timeout_context *)malloc(sizeof(*timeout_context));        //立即超时      if (seconds <= 0.0) { // instant timeout          seconds = 0.0;          timeout_context->termTSR = 0ULL;      }      //seconds为超时时间,超时时执行__CFRunLoopTimeout函数      else if (seconds <= TIMER_INTERVAL_LIMIT) {          dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, DISPATCH_QUEUE_OVERCOMMIT);          timeout_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);          dispatch_retain(timeout_timer);          timeout_context->ds = timeout_timer;          timeout_context->rl = (CFRunLoopRef)CFRetain(rl);          timeout_context->termTSR = startTSR + __CFTimeIntervalToTSR(seconds);          dispatch_set_context(timeout_timer, timeout_context); // source gets ownership of context          dispatch_source_set_event_handler_f(timeout_timer, __CFRunLoopTimeout);          dispatch_source_set_cancel_handler_f(timeout_timer, __CFRunLoopTimeoutCancel);          uint64_t ns_at = (uint64_t)((__CFTSRToTimeInterval(startTSR) + seconds) * 1000000000ULL);          dispatch_source_set_timer(timeout_timer, dispatch_time(1, ns_at), DISPATCH_TIME_FOREVER, 1000ULL);          dispatch_resume(timeout_timer);      }      //永不超时      else { // infinite timeout          seconds = 9999999999.0;          timeout_context->termTSR = UINT64_MAX;      }        //标志位默认为true      Boolean didDispatchPortLastTime = true;      //记录最后runloop状态,用于return      int32_t retVal = 0;      do {          //初始化一个存放内核消息的缓冲池          uint8_t msg_buffer[3 * 1024];  #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI          mach_msg_header_t *msg = NULL;          mach_port_t livePort = MACH_PORT_NULL;  #elif DEPLOYMENT_TARGET_WINDOWS          HANDLE livePort = NULL;          Boolean windowsMessageReceived = false;  #endif          //取所有需要监听的port          __CFPortSet waitSet = rlm->_portSet;            //设置RunLoop为可以被唤醒状态          __CFRunLoopUnsetIgnoreWakeUps(rl);            //2.通知observer,即将触发timer回调,处理timer事件          if (rlm->_observerMask & kCFRunLoopBeforeTimers) __CFRunLoopDoObservers(rl, rlm, kCFRunLoopBeforeTimers);          //3.通知observer,即将触发Source0回调          if (rlm->_observerMask & kCFRunLoopBeforeSources) __CFRunLoopDoObservers(rl, rlm, kCFRunLoopBeforeSources);            //执行加入当前runloop的block          __CFRunLoopDoBlocks(rl, rlm);            //4.处理source0事件          //有事件处理返回true,没有事件返回false          Boolean sourceHandledThisLoop = __CFRunLoopDoSources0(rl, rlm, stopAfterHandle);          if (sourceHandledThisLoop) {              //执行加入当前runloop的block              __CFRunLoopDoBlocks(rl, rlm);          }            //如果没有Sources0事件处理 并且 没有超时,poll为false          //如果有Sources0事件处理 或者 超时,poll都为true          Boolean poll = sourceHandledThisLoop || (0ULL == timeout_context->termTSR);            //第一次do..whil循环不会走该分支,因为didDispatchPortLastTime初始化是true          if (MACH_PORT_NULL != dispatchPort && !didDispatchPortLastTime) {  #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI              //从缓冲区读取消息              msg = (mach_msg_header_t *)msg_buffer;              //5.接收dispatchPort端口的消息,(接收source1事件)              if (__CFRunLoopServiceMachPort(dispatchPort, &msg, sizeof(msg_buffer), &livePort, 0)) {                  //如果接收到了消息的话,前往第9步开始处理msg                  goto handle_msg;              }  #elif DEPLOYMENT_TARGET_WINDOWS              if (__CFRunLoopWaitForMultipleObjects(NULL, &dispatchPort, 0, 0, &livePort, NULL)) {                  goto handle_msg;              }  #endif          }            didDispatchPortLastTime = false;            //6.通知观察者RunLoop即将进入休眠          if (!poll && (rlm->_observerMask & kCFRunLoopBeforeWaiting)) __CFRunLoopDoObservers(rl, rlm, kCFRunLoopBeforeWaiting);          //设置RunLoop为休眠状态          __CFRunLoopSetSleeping(rl);          // do not do any user callouts after this point (after notifying of sleeping)            // Must push the local-to-this-activation ports in on every loop          // iteration, as this mode could be run re-entrantly and we don't          // want these ports to get serviced.            __CFPortSetInsert(dispatchPort, waitSet);            __CFRunLoopModeUnlock(rlm);          __CFRunLoopUnlock(rl);    #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI  #if USE_DISPATCH_SOURCE_FOR_TIMERS          //这里有个内循环,用于接收等待端口的消息          //进入此循环后,线程进入休眠,直到收到新消息才跳出该循环,继续执行run loop          do {              if (kCFUseCollectableAllocator) {                  objc_clear_stack(0);                  memset(msg_buffer, 0, sizeof(msg_buffer));              }              msg = (mach_msg_header_t *)msg_buffer;              //7.接收waitSet端口的消息              __CFRunLoopServiceMachPort(waitSet, &msg, sizeof(msg_buffer), &livePort, poll ? 0 : TIMEOUT_INFINITY);              //收到消息之后,livePort的值为msg->msgh_local_port,              if (modeQueuePort != MACH_PORT_NULL && livePort == modeQueuePort) {                  // Drain the internal queue. If one of the callout blocks sets the timerFired flag, break out and service the timer.                  while (_dispatch_runloop_root_queue_perform_4CF(rlm->_queue));                  if (rlm->_timerFired) {                      // Leave livePort as the queue port, and service timers below                      rlm->_timerFired = false;                      break;                  } else {                      if (msg && msg != (mach_msg_header_t *)msg_buffer) free(msg);                  }              } else {                  // Go ahead and leave the inner loop.                  break;              }          } while (1);  #else          if (kCFUseCollectableAllocator) {              objc_clear_stack(0);              memset(msg_buffer, 0, sizeof(msg_buffer));          }          msg = (mach_msg_header_t *)msg_buffer;          __CFRunLoopServiceMachPort(waitSet, &msg, sizeof(msg_buffer), &livePort, poll ? 0 : TIMEOUT_INFINITY);  #endif      #elif DEPLOYMENT_TARGET_WINDOWS          // Here, use the app-supplied message queue mask. They will set this if they are interested in having this run loop receive windows messages.          __CFRunLoopWaitForMultipleObjects(waitSet, NULL, poll ? 0 : TIMEOUT_INFINITY, rlm->_msgQMask, &livePort, &windowsMessageReceived);  #endif            __CFRunLoopLock(rl);          __CFRunLoopModeLock(rlm);            // Must remove the local-to-this-activation ports in on every loop          // iteration, as this mode could be run re-entrantly and we don't          // want these ports to get serviced. Also, we don't want them left          // in there if this function returns.            __CFPortSetRemove(dispatchPort, waitSet);              __CFRunLoopSetIgnoreWakeUps(rl);            // user callouts now OK again          //取消runloop的休眠状态          __CFRunLoopUnsetSleeping(rl);          //8.通知观察者runloop被唤醒          if (!poll && (rlm->_observerMask & kCFRunLoopAfterWaiting)) __CFRunLoopDoObservers(rl, rlm, kCFRunLoopAfterWaiting);            //9.处理收到的消息      handle_msg:;          __CFRunLoopSetIgnoreWakeUps(rl);    #if DEPLOYMENT_TARGET_WINDOWS          if (windowsMessageReceived) {              // These Win32 APIs cause a callout, so make sure we're unlocked first and relocked after              __CFRunLoopModeUnlock(rlm);              __CFRunLoopUnlock(rl);                if (rlm->_msgPump) {                  rlm->_msgPump();              } else {                  MSG msg;                  if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE | PM_NOYIELD)) {                      TranslateMessage(&msg);                      DispatchMessage(&msg);                  }              }                __CFRunLoopLock(rl);              __CFRunLoopModeLock(rlm);              sourceHandledThisLoop = true;                // To prevent starvation of sources other than the message queue, we check again to see if any other sources need to be serviced              // Use 0 for the mask so windows messages are ignored this time. Also use 0 for the timeout, because we're just checking to see if the things are signalled right now -- we will wait on them again later.              // NOTE: Ignore the dispatch source (it's not in the wait set anymore) and also don't run the observers here since we are polling.              __CFRunLoopSetSleeping(rl);              __CFRunLoopModeUnlock(rlm);              __CFRunLoopUnlock(rl);                __CFRunLoopWaitForMultipleObjects(waitSet, NULL, 0, 0, &livePort, NULL);                __CFRunLoopLock(rl);              __CFRunLoopModeLock(rlm);              __CFRunLoopUnsetSleeping(rl);              // If we have a new live port then it will be handled below as normal          }      #endif          if (MACH_PORT_NULL == livePort) {              CFRUNLOOP_WAKEUP_FOR_NOTHING();              // handle nothing              //通过CFRunloopWake唤醒          } else if (livePort == rl->_wakeUpPort) {              CFRUNLOOP_WAKEUP_FOR_WAKEUP();              //什么都不干,跳回2重新循环              // do nothing on Mac OS  #if DEPLOYMENT_TARGET_WINDOWS              // Always reset the wake up port, or risk spinning forever              ResetEvent(rl->_wakeUpPort);  #endif          }  #if USE_DISPATCH_SOURCE_FOR_TIMERS          //如果是定时器事件          else if (modeQueuePort != MACH_PORT_NULL && livePort == modeQueuePort) {              CFRUNLOOP_WAKEUP_FOR_TIMER();              //9.1 处理timer事件              if (!__CFRunLoopDoTimers(rl, rlm, mach_absolute_time())) {                  // Re-arm the next timer, because we apparently fired early                  __CFArmNextTimerInMode(rlm, rl);              }          }  #endif  #if USE_MK_TIMER_TOO          //如果是定时器事件          else if (rlm->_timerPort != MACH_PORT_NULL && livePort == rlm->_timerPort) {              CFRUNLOOP_WAKEUP_FOR_TIMER();              // 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.              // 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             //9.1处理timer事件              if (!__CFRunLoopDoTimers(rl, rlm, mach_absolute_time())) {                  // Re-arm the next timer                  __CFArmNextTimerInMode(rlm, rl);              }          }  #endif          //如果是dispatch到main queue的block          else if (livePort == dispatchPort) {              CFRUNLOOP_WAKEUP_FOR_DISPATCH();              __CFRunLoopModeUnlock(rlm);              __CFRunLoopUnlock(rl);              _CFSetTSD(__CFTSDKeyIsInGCDMainQ, (void *)6, NULL);  #if DEPLOYMENT_TARGET_WINDOWS              void *msg = 0;  #endif              //9.2执行block              __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__(msg);              _CFSetTSD(__CFTSDKeyIsInGCDMainQ, (void *)0, NULL);              __CFRunLoopLock(rl);              __CFRunLoopModeLock(rlm);              sourceHandledThisLoop = true;              didDispatchPortLastTime = true;          } else {              CFRUNLOOP_WAKEUP_FOR_SOURCE();              // Despite the name, this works for windows handles as well              CFRunLoopSourceRef rls = __CFRunLoopModeFindSourceForMachPort(rl, rlm, livePort);              // 有source1事件待处理              if (rls) {  #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI                  mach_msg_header_t *reply = NULL;                  //9.2 处理source1事件                  sourceHandledThisLoop = __CFRunLoopDoSource1(rl, rlm, rls, msg, msg->msgh_size, &reply) || sourceHandledThisLoop;                  if (NULL != reply) {                      (void)mach_msg(reply, MACH_SEND_MSG, reply->msgh_size, 0, MACH_PORT_NULL, 0, MACH_PORT_NULL);                      CFAllocatorDeallocate(kCFAllocatorSystemDefault, reply);                  }  #elif DEPLOYMENT_TARGET_WINDOWS                  sourceHandledThisLoop = __CFRunLoopDoSource1(rl, rlm, rls) || sourceHandledThisLoop;  #endif              }          }  #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI          if (msg && msg != (mach_msg_header_t *)msg_buffer) free(msg);  #endif            __CFRunLoopDoBlocks(rl, rlm);            if (sourceHandledThisLoop && stopAfterHandle) {              //进入run loop时传入的参数,处理完事件就返回              retVal = kCFRunLoopRunHandledSource;          }else if (timeout_context->termTSR < mach_absolute_time()) {              //run loop超时              retVal = kCFRunLoopRunTimedOut;          }else if (__CFRunLoopIsStopped(rl)) {              //run loop被手动终止              __CFRunLoopUnsetStopped(rl);              retVal = kCFRunLoopRunStopped;          }else if (rlm->_stopped) {              //mode被终止              rlm->_stopped = false;              retVal = kCFRunLoopRunStopped;          }else if (__CFRunLoopModeIsEmpty(rl, rlm, previousMode)) {              //mode中没有要处理的事件              retVal = kCFRunLoopRunFinished;          }          //除了上面这几种情况,都继续循环      } while (0 == retVal);        if (timeout_timer) {          dispatch_source_cancel(timeout_timer);          dispatch_release(timeout_timer);      } else {          free(timeout_context);      }        return retVal;  }

__CFRunLoopRun中的代码是runloop的核心代码,以上代码的逻辑如下:

  1. 通知Observer即将进入runloop
  2. 通知Observer即将处理timer
  3. 处理block
  4. 通知Observer即将处理Source0
  5. 处理source0
  6. 处理block
  7. 如果有source1,跳到第11步
  8. 通知Observer,runloop即将进入休眠
  9. runloop休眠,等待被唤醒(source0、source1、timer、dispatch、CFRunloopPerform等手动唤醒)
  10. 通知Observer,runloop即将被唤醒
  11. 处理唤醒runloop的事件。如果是timer唤醒则处理timer;如果不是,则处理source1
  12. 如果runloop中没有timer和source,或者线程被强制退出,则通知Observer即将退出runloop;如果一切正常,则回到上面第2步。

图示流程如下:

5,__CFRunloopServiceMachPort

__CFRunLoopServiceMachPort函数在runloop中起到了至关重要的作用,下面给出了详细注释。

/**   *  接收指定内核端口的消息   *   *  @param port        接收消息的端口   *  @param buffer      消息缓冲区   *  @param buffer_size 消息缓冲区大小   *  @param livePort    暂且理解为活动的端口,接收消息成功时候值为msg->msgh_local_port,超时时为MACH_PORT_NULL   *  @param timeout     超时时间,单位是ms,如果超时,则RunLoop进入休眠状态   *   *  @return 接收消息成功时返回true 其他情况返回false   */  static Boolean __CFRunLoopServiceMachPort(mach_port_name_t port, mach_msg_header_t **buffer, size_t buffer_size, mach_port_t *livePort, mach_msg_timeout_t timeout) {      Boolean originalBuffer = true;      kern_return_t ret = KERN_SUCCESS;      for (;;) {      /* In that sleep of death what nightmares may come ... */          mach_msg_header_t *msg = (mach_msg_header_t *)*buffer;          msg->msgh_bits = 0;  //消息头的标志位          msg->msgh_local_port = port;  //源(发出的消息)或者目标(接收的消息)          msg->msgh_remote_port = MACH_PORT_NULL; //目标(发出的消息)或者源(接收的消息)          msg->msgh_size = buffer_size;  //消息缓冲区大小,单位是字节          msg->msgh_id = 0;  //唯一id            if (TIMEOUT_INFINITY == timeout) { CFRUNLOOP_SLEEP(); } else { CFRUNLOOP_POLL(); }            //通过mach_msg发送或者接收的消息都是指针,          //如果直接发送或者接收消息体,会频繁进行内存复制,损耗性能          //所以XNU使用了单一内核的方式来解决该问题,所有内核组件都共享同一个地址空间,因此传递消息时候只需要传递消息的指针          ret = mach_msg(msg,                         MACH_RCV_MSG|MACH_RCV_LARGE|((TIMEOUT_INFINITY != timeout) ? MACH_RCV_TIMEOUT : 0)|MACH_RCV_TRAILER_TYPE(MACH_MSG_TRAILER_FORMAT_0)|MACH_RCV_TRAILER_ELEMENTS(MACH_RCV_TRAILER_AV),                         0,                         msg->msgh_size,                         port,                         timeout,                         MACH_PORT_NULL);          CFRUNLOOP_WAKEUP(ret);            //接收/发送消息成功,给livePort赋值为msgh_local_port          if (MACH_MSG_SUCCESS == ret) {              *livePort = msg ? msg->msgh_local_port : MACH_PORT_NULL;              return true;          }            //MACH_RCV_TIMEOUT          //超出timeout时间没有收到消息,返回MACH_RCV_TIMED_OUT          //此时释放缓冲区,把livePort赋值为MACH_PORT_NULL          if (MACH_RCV_TIMED_OUT == ret) {              if (!originalBuffer) free(msg);              *buffer = NULL;              *livePort = MACH_PORT_NULL;              return false;          }            //MACH_RCV_LARGE          //如果接收缓冲区太小,则将过大的消息放在队列中,并且出错返回MACH_RCV_TOO_LARGE,          //这种情况下,只返回消息头,调用者可以分配更多的内存          if (MACH_RCV_TOO_LARGE != ret) break;          //此处给buffer分配更大内存          buffer_size = round_msg(msg->msgh_size + MAX_TRAILER_SIZE);          if (originalBuffer) *buffer = NULL;          originalBuffer = false;          *buffer = realloc(*buffer, buffer_size);      }      HALT;      return false;  }

6,小结

runloop其实很简单,它是一个对象,它和线程是一一对应的,每个线程都有一个对应的runloop对象,主线程的runloop会在程序启动时自动创建,子线程的runloop会在第一次被获取的时候创建

以上。