runloop的source

以上是完整的 CFRunLoop 和 CFRunLoopMode 的结构体源码(太长了我的妈,用不着看完),下面我精简一下,把重要的留下,看如下代码(可以仔细看一下,加深印象):

复制代码
 
 
复制代码

上面是精简出来比较关键的 RunLoop 和 RunLoopMode 的结构体,从上面源码可以看出:

一个 RunLoop 对象有一个用来被唤醒的端口 _wakeUpPort,一个当前运行的 mode 叫 _currentMode,以及若干个 _modes_commonModes_commonModeItems(commonModes这2个东西后面详细讲)。runLoop 有很多 mode,即 _modes,但是只有一个 _currentMode,RunLoop 一次只能运行在一个 mode 下,如果需要切换 Mode,只能退出 Loop,不可能在多个 Mode 下同时运行(这是iOS运行流畅的原因之一)。

从 runLoopMode 的组成可以看出来:mode管理了所有的事件(Source/Timer/Observer 被称为 Mode Item),而 RunLoop 管理着若干个 mode。

这两个结构体中,已经涉及到了我们的所有五个类了,关于他们的关系我后面会详细说,这里简单的看看,对他们有个印象,混个脸熟,先来看 CFRunLoopSourceRef

CFRunLoopSourceRef

在我 RunLoopMode 数据结构代码中可以看到这两个东西 CFMutableSetRef _source0 和 CFMutableSetRef _source1,首先这两个东西是 Set(集合),集合中存放的是一堆数据结构(这里就可以对应上面蓝色底那张图来看,这是那种图图里面的Source集合的部分),那这个 source 到底是个什么东西呢,在 RunLoopMode 结构体的注释中我也写了,他们其实也是一个数据结构 CFRunLoopSourceRef。那 CFRunLoopSourceRef 结构又是怎样的呢,我们再来看下面它的结构代码: 

复制代码
 
 
复制代码

上面代码贴出来了三个数据结构,其他多余的别看,光看我注释的部分就行,其中第一个数据结构 __CFRunLoopSource 包含一个 _context 成员,他的类型是 CFRunLoopSourceContext 或者是 CFRunLoopSourceContext1,也就是后面两个数据结构。

大家可以从我重点看我注释的行 CFRunLoopSourceContext(其实就是source0的数据结构)和 CFRunLoopSourceContext1(source1) 的区别就在于 CFRunLoopSourceContext1(source1) 多了一个 mach_port_t 接收消息的端口。mach_port_t 这又是个什么玩意儿,这里暂时不用管,可以简单的啰嗦两句,mach是iOS系统内核的心脏,他管理着处理器的资源,关于它的一些结构和原理,我以后会写一篇文章来描述它的结构和工作原理,现在我还是把话收回来说主题,不走远了。

这里简单总结一下:

  • CFRunLoopSourceRef 是事件产生的地方;
  • 这个 CFRunLoopSourceRef 有两个版本就是 source0 和 source1;
  • source0只包含一个回调(函数指针),不能主动出发事件,需要 CFRunLoopSourceSignal(source) 将 Source 标记为待处理,CFRunLoopWakeUp(runloop) 唤醒 RunLoop,让其处理事件
  • source1包含 mach_port 和一个回调(函数指针),用于通过内核和其它线程相互发送消息,能主动唤醒 RunLoop。

http://www.cnblogs.com/CrazyD0u/p/6481092.html

扫描二维码关注公众号,回复: 31930 查看本文章
      • 我们在这里绑定了两个参数一个是signal触发的函数,一个是函数的参数,至于其他参数的用途,可以看看苹果官方文档的说明: 

    • version 
    • Version number of the structure. Must be 0. 
    • info 
    • An arbitrary pointer to program-defined data, which can be associated with the CFRunLoopSource at creation time. This pointer is passed to all the callbacks defined in the context. 
    • retain 
    • A retain callback for your program-defined info pointer. Can be NULL. 
    • release 
    • A release callback for your program-defined info pointer. Can be NULL. 
    • copyDescription 
    • A copy description callback for your program-defined info pointer. Can be NULL. 
    • equal 
    • An equality test callback for your program-defined info pointer. Can be NULL. 
    • hash 
    • A hash calculation callback for your program-defined info pointer. Can be NULL. 
    • schedule 
    • A scheduling callback for the run loop source. This callback is called when the source is added to a run loop mode. Can be NULL. 
    • cancel 
    • A cancel callback for the run loop source. This callback is called when the source is removed from a run loop mode. Can be NULL. 
    • perform 
    • A perform callback for the run loop source. This callback is called when the source has fired.

猜你喜欢

转载自www.cnblogs.com/feng9exe/p/8876147.html