In-depth study of the thread safety model of PHP and Zend Engine _php tutorial

Source: Internet
Author: User
Tags php source code zts
In the process of reading PHP source code and Learning PHP extension development, I have access to a large number of macros containing the word "TSRM". By looking at the data, it is known that these macros are related to the thread safety mechanism of Zend, and the vast majority of the data suggest that these macros can be used in accordance with established rules, without stating the specific role of these macros. I do not know what is always uncomfortable, so I read the source code and access to a limited amount of information to briefly understand the relevant mechanism, this article is my summary of the research content.
This paper first explains the concept of thread safety and the background of PHP threads security, then studies the thread safety mechanism of PHP zts (Zend thread Safety) and the specific implementation TSRM, the research content including related data structure, implementation details and operation mechanism, Finally, the problem of selective compilation of Zend for single-threaded and multi-threaded environments is studied.

Thread Safety

Thread safety problem, Word is how to secure access to public resources in multi-threaded environment. We know that each thread has only one private stack, sharing the heap of the owning process. In C, when a variable is declared outside of any function, it becomes a global variable that is assigned to the shared storage space of the process, and the different threads refer to the same address space, so if a thread modifies the variable, it affects all threads. This may seem like a convenient means of sharing data with threads, but PHP tends to handle a single request per thread, so you want each thread to have a copy of the global variable, rather than asking for interference between requests.

Early PHP was often used for single-threaded environments where each process started only one thread, so there was no thread-safety issue. There was a scenario in which PHP was used in a multithreaded environment, so Zend introduced the Zend thread safety mechanism (Zend thread Safety, abbreviated as ZTS) to ensure thread security.

the basic principle and realization of ZTS

Basic ideas

The basic idea of zts is very straightforward, does it mean that each global variable needs to have a copy on each thread? Then I'll provide the mechanism:

In a multithreaded environment, the application of global variables is no longer a simple declaration of a variable, but the entire process on the heap allocated a piece of memory space as a "thread global variable Pool", when the process starts to initialize the memory pool, whenever the thread needs to request a global variable, the corresponding method calls TSRM (Thread Safe Resource Manager,zts Implementation) and pass the necessary parameters (such as variable size, etc.), TSRM is responsible for allocating the appropriate memory chunks in the memory pool and returning the reference ID of the memory, so that the next time the thread needs to read and write this variable, You will be responsible for true read and write operations by passing unique reference identities to TSRM,TSRM. This enables thread-safe global variables. The ZTS principle is given:

Thread1 and Thread2 are in the same process, each of which requires a global var,tsrm for both the threads in the global memory pool (the yellow part) each assigned an area and identified by a unique ID. This allows two threads to access their own variables without interfering with each other through TSRM.

Here's a detailed code snippet to see how Zend is implementing this mechanism. Here I use the source code of PHP5.3.8.

TSRM implementation code in the PHP source "tsrm" directory.

Data

The more important data structures in TSRM are two: Tsrm_tls_entry and Tsrm_resource_type. Let's look at Tsrm_tls_entry.

Tsrm_tls_entry defined in the TSRM/TSRM.C:

typedef struct _tsrm_tls_entry tsrm_tls_entry;struct _tsrm_tls_entry {void **storage;int count; thread_t thread_id;tsrm_tls_entry *next;}

Each tsrm_tls_entry structure is responsible for representing all the global variable resources of a thread, where the thread_id storage thread Id,count records the number of global variables, and next refers to the next node. Storage can be seen as an array of pointers, where each element is a global variable that points to this node representing the thread. Finally, the tsrm_tls_entry of each thread is formed into a linked list structure, and the linked header pointer is assigned to a global static variable tsrm_tls_table. Note that because tsrm_tls_table is a real global variable, all threads share this variable, which enables memory management consistency between threads. The tsrm_tls_entry and tsrm_tls_table structures are as follows:

The internal structure of the Tsrm_resource_type is relatively simple:

typedef struct {size_t size;ts_allocate_ctor ctor;ts_allocate_dtor dtor;int done;} Tsrm_resource_type;

As mentioned above, Tsrm_tls_entry is thread-based (one node per thread), and Tsrm_resource_type is a resource (or global variable), and each time a new resource is allocated, a tsrm_resource_type is created. All Tsrm_resource_type are composed of tsrm_resource_table in an array (linear table), whose subscript is the ID of the resource. Each Tsrm_resource_type stores the size of this resource and the construction, destructor, and method pointers. To some extent, tsrm_resource_table can be seen as a hash table, key is a resource id,value is tsrm_resource_type structure.

Please click on the next page for more information!

    • Total 4 Pages:
    • Previous page
    • 1
    • 2
    • 3
    • 4
    • Next page

http://www.bkjia.com/PHPjc/363868.html www.bkjia.com true http://www.bkjia.com/PHPjc/363868.html techarticle in the process of reading PHP source code and Learning PHP extension development, I have access to a large number of macros containing TSRM words. By looking at the data, we know that these macros are related to the thread safety mechanism of Zend, and the vast ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.