Swoole tour-previous article

  c, php

Write at the front

Swoole has gone through several milestones. we need to constantly sum up and review our own development process on our way forward. just as we call it “learning from past experiences and learning from new ones”, this series of articles will be divided into three parts: the first, the second and the third.

  1. The first chapter mainly introduces the concept of synergetic process and the main scheme and technology of realizing synergetic process in several versions of Swoole.
  2. The second part mainly goes deep into Zend to analyze the principle and implementation of PHP.
  3. The latter part mainly supplements and analyzes the realization of Concorde 4. x.

The Soft Text Begins Officially

What is the tour?

The concept actually appeared very early. Take a wiki paragraph:According to Donald Knuth, the term coroutine was coined by Melvin Conway in 1958, after he applied it to construction of an assembly program.The first published explanation of the coroutine appeared later, in 1963.Concorde has a longer history than C language. According to its concept, Concorde is a kind of subprogram that can transfer program control through yield. Concorde is not the relationship between callers and callees, but is symmetrical and equal to each other. Concorde is completely controlled by user-mode programs, so it has also become a user-mode thread. Coordination is scheduled by users in a non-preemptive manner, not by the operating system. Because of this, there is no overhead of system scheduling context switching, and the coordination process has the characteristics of light weight, high efficiency, fast speed, etc. (Most of them are in nonpreemptive, but golang also joined preemptive scheduling on January 4. One of the synergetics has a dead cycle, so the other synergetics will not starve to death. The CPU needs to be released at the necessary time, and SWOLE added this feature in V4.3.2).

Xiecheng has been so popular in recent years, due in large part to golang’s popularity and rapid development in China, which is loved by many developers. At present, there are many languages that support coordination, such as golang, lua, python, c#, javascript, etc. You can also use a very short code with c/c++ to pull out the model of synergetic process. Of course PHP also has its own synergetic implementation, i.e. generator, which we will not discuss here.


SWOLE initially entered the public eye as a high-performance network communication engine. SWOLE 1. X’s coding is mainly asynchronous callback. Although its performance is very efficient, many developers will find that writing business codes in asynchronous callback mode is contrary to normal human thinking as the complexity of project engineering increases. Especially when callbacks are nested in multiple layers, not only the development and maintenance costs increase exponentially, but also the probability of errors increases greatly. The ideal encoding method is: synchronous encoding can achieve asynchronous non-blocking performance. Therefore, Swoole started the exploration of synergetics very early.

The original synergetic version was implemented based on the PHP generator GeneratorsYield, which can be referred to the early blog of PHP great god Nikita.coroutineIntroduction. The event-driven combination of PHP and Swoole can refer to Tencent’s open source team.TSFFramework, we also used the framework in many production projects, really let everyone feel the pleasure of writing asynchronous code in synchronous programming, however, the reality is always cruel, this way has several fatal shortcomings:

  • All logic that is voluntarily relinquished requires the yield keyword. This will bring a great probability of mistakes to programmers, resulting in a shift from understanding coordination to understanding the principles of Generators grammar.
  • Because the grammar cannot be compatible with the old project, the reconstruction of the old project is complicated and costly.

This makes it difficult to use old and new projects with ease.


2. The synergetic processes after X are all based on the original synergetic processes of the kernel, without the yield keyword. The version of 2.0 is a very important milestone. It realizes the stack management of php and goes deep into zend kernel to operate PHP stack at the time of synergetic creation, switching and ending. The Swoole document also introduces a lot of details about the implementation of each version. This article only gives a brief introduction to the synergetic driver technology of each version.The native coordinators have the management of php stack. We will take a separate article to analyze the management and switching of PHP stack.

2.x mainly uses setjmp/longjmp to realize coordination. Many C projects mainly use this method to realize try-catch-finally. You can also refer to the usage of Zend kernel. The return value of setjmp’s first call is 0. when longjmp jumps, the return value of setjmp is the value passed to longjmp. Setjmp/longjmp only has the ability to control flow jump. Although PC and stack pointer can be restored, stack frame cannot be restored, so many problems will occur. For example, when longjmp was in use, the scope of setjmp had already exited and the stack frame at that time had been destroyed. Undefined behavior appears. Suppose there is such a call chain:

func0() -> func1() -> … -> funcN()

Only in the case of setjmp in func{i} () and longjmp in func{i+k} (), the behavior of the program can be expected.


3.x is a version with a very short life cycle, which is mainly used for reference.fiber-extThe project uses the vm interrupts mechanism of PHP7, which can set the tag bits in the vm, check the tag bits when executing some instructions (e.g. jump and function call, etc.), and if it hits, execute the corresponding hook function to switch the stack of the VM, thus realizing coprocessing. Although we have fully realized the function of synergetic process, we have not made much progress with respect to 2.x, so we gave up this version and directly entered the iteration of 4.x for further analysis in our subsequent articles.


4.x Concorde is the current version of Swoole Concorde. It draws on the shortcomings and problems of the previous version and introducesPHP+CDouble stack management and maintenance, perfect support for PHP syntax, detailed analysis we put at the end of the series.


The first part of the tour ended, and in the next part we will go deep into Zend’s analysis of the implementation of Swoole’s native PHP part of the tour.