source of runloop

The above is the complete source code of the structure of CFRunLoop and CFRunLoopMode (too long for my mother, I don't need to read it), let me simplify it and leave the important ones. Look at the following code (you can take a closer look to deepen your impression):

copy code
 
 
copy code

The above is the simplified structure of the more critical RunLoop and  RunLoopMode  , as can be seen from the above source code:

RunLoop  object has a port for being woken up  _wakeUpPort, a current running mode called  _currentMode, and several  _modes, _commonModes, _commonModeItems(commonModes这2个东西后面详细讲). There are many modes of runLoop, that is  _modes, but only one  _currentMode, RunLoop can only run in one mode at a time, if you need to switch the mode, you can only exit the loop, it is impossible to run in multiple modes at the same time (this is one of the reasons why iOS runs smoothly ).

It can be seen from  the composition of runLoopMode  : mode manages all events (Source/Timer/Observer is called Mode Item), and RunLoop manages several modes.

In these two structures, all five of our classes have been involved. I will talk about their relationship in detail later. Here is a brief look. I have an impression of them. If you are familiar with them, let’s look at  CFRunLoopSourceRef first .

CFRunLoopSourceRef

In my RunLoopMode data structure code, you can see these two things  CFMutableSetRef _source0 and CFMutableSetRef _source1 , first of all, these two things are Set (collection), and the collection stores a bunch of data structures (here can correspond to the blue background above Looking at the picture, this is the part of the Source collection in the picture), what exactly is this source, I also wrote in the comments of the RunLoopMode structure, they are actually a data structure  CFRunLoopSourceRef . What about the  CFRunLoopSourceRef  structure? Let's look at its structure code below: 

copy code
 
 
copy code

There are three data structures posted in the above code. Don't look at the other redundant ones. Just look at the part I commented. The first data structure __CFRunLoopSource contains a _context member, and its type is CFRunLoopSourceContext or CFRunLoopSourceContext1, which is the back two data structures.

You can focus on the line I commented. The difference between CFRunLoopSourceContext (actually the data structure of source0) and CFRunLoopSourceContext1 (source1) is that CFRunLoopSourceContext1 (source1) has an additional mach_port_t port for receiving messages. mach_port_t What is this thing? I don’t need to worry about it here for the time being. It can be simply a few words. Mach is the heart of the iOS system kernel. It manages the resources of the processor. I will write an article about some of its structure and principles in the future. article to describe its structure and working principle, and now I will put the words back on the topic and not go too far.

Here is a brief summary:

  • CFRunLoopSourceRef  is where the event is generated;
  • There are two versions of this  CFRunLoopSourceRef  , source0 and source1;
  • source0 only contains a callback (function pointer), and cannot initiate events actively.  CFRunLoopSourceSignal(source) is required  to mark the Source as pending, and CFRunLoopWakeUp(runloop)  wakes up RunLoop and lets it process events
  • source1 contains  mach_port  and a callback (function pointer), which is used to send messages to each other through the kernel and other threads, and can actively wake up RunLoop.

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

 

      • We bind two parameters here, one is the function triggered by the signal, and the other is the parameter of the function. As for the purpose of other parameters, you can see the description of Apple's official documentation: 

    • 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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324597209&siteId=291194637