概览
一个CFRunLoopSource对象就是对一个能被添加到runloop中的输入源的抽象。CFRunLoopSource通常用来产生异步事件,比如消息到达网络端口或者用户执行方法。
一个输入源类型一般定义了创建和操作该类型对象的API,正如它是一个与run loop相独立的实体,并提供了为某个对象创建一个CFRunLoopSource对象的方法(函数)。这个输入源对象可以注册到runloop中,并作为run loop与真实输入源之间的媒介。输入源的例子包括CFMachPort,CFMessagePort和CFSocket.
有两类CFRunLoopSurce,Source0和Source1。
Sources0,如此命名是因为其上下文结构体中由程序管理的版本字段为0.当一个源准备触发时,程序的某个部分(可能是独立线程中等待事件的代码)必须调用CFRunLoopSourceSignal()函数来告知run loop该源正在准备触发。当前CFSocket是基于Source0实现的。
Source1,由run loop和内核来管理。这类型的源使用mach port来通信标记自己准备触发。当一条消息到达源的端口上时,该源被内核自动标记。当该源触发后,消息中内容就交给该源来处理。当前CFMachPort、CFMessagePort是基于Source1实现的。
当创建自定义的run loop输入源时,你能自己选择类型(Source0或者Source1)。
一个run loop输入源能在同时被添加到多个run loop和多个run loop mode中。当源被标记后,无论哪一个run loop第一次发现了该标记信号都会触发该源。将一个源添加到多个线程的多个run loop中可以用来管理处理数据离散集合的worker threads的线程池,比如,一个client-server消息通过网络发送或者管理线程将任务置入队列中时,当消息到达或者任务置入队列,这个源就会标记触发,随后一个随机的线程就会收到并处理这个请求。
原文
A CFRunLoopSource object is an abstraction of an input source that can be put into a run loop. Input sources typically generate asynchronous events, such as messages arriving on a network port or actions performed by the user.
An input source type normally defines an API for creating and operating on objects of the type, as if it were a separate entity from the run loop, then provides a function to create a CFRunLoopSource for an object. The run loop source can then be registered with the run loop and act as an intermediary between the run loop and the actual input source type object. Examples of input sources include CFMachPort, CFMessagePort, and CFSocket.
There are two categories of sources. Version 0 sources, so named because the
version
field of their context structure is 0, are managed manually by the application. When a source is ready to fire, some part of the application, perhaps code on a separate thread waiting for an event, must callCFRunLoopSourceSignal
to tell the run loop that the source is ready to fire. The run loop source for CFSocket is currently implemented as a version 0 source.Version 1 sources are managed by the run loop and kernel. These sources use Mach ports to signal when the sources are ready to fire. A source is automatically signaled by the kernel when a message arrives on the source’s Mach port. The contents of the message are given to the source to process when the source is fired. The run loop sources for CFMachPort and CFMessagePort are currently implemented as version 1 sources.
When creating your own custom run loop source, you can choose which version works best for you.
A run loop source can be registered in multiple run loops and run loop modes at the same time. When the source is signaled, whichever run loop that happens to detect the signal first will fire the source. Adding a source to multiple threads’ run loops can be used to manage a pool of “worker” threads that is processing discrete sets of data, such as client-server messages over a network or entries in a job queue filled by a “manager” thread. As messages arrive or jobs get added to the queue, the source gets signaled and a random thread receives and processes the request.