In-depth study of PHP and ZendEngine thread security model _ PHP Tutorial

Source: Internet
Author: User
Tags zts
In-depth study of the thread security model of PHP and ZendEngine. When I read the PHP source code and learned PHP extension development, I was exposed to a large number of macros containing TSRM words. By checking the information, we know that these macros are related to the Zend thread security mechanism. most of them are reading the PHP source code and learning the PHP extension development process, I am exposed to a large number of macros that contain the word "TSRM. By checking the information, we know that these macros are related to the thread security mechanism of Zend. In most documents, we recommend that you use these macros according to the established rules without explaining the specific functions of these macros. I don't know what's going on is always uncomfortable, so I will read the source code and read the limited information to briefly understand the relevant mechanisms. This article is my summary of the study.
This article first explains the concept of Thread security and the background of Thread security in PHP, and then studies in detail the Thread security mechanism ZTS (Zend Thread Safety) of PHP and the specific implementation of TSRM, the research includes the relevant data structure, implementation details, and operating mechanism. Finally, the Zend's selective compilation problem for single-thread and multi-thread environments is studied.

Thread security

Thread security issues are simply how to securely access public resources in a multi-threaded environment. We know that each thread has only one private stack and shares the heap of the process. In C, when a variable is declared outside of any function, it becomes a global variable, which will be allocated to the shared storage space of the process, different threads reference the same address space. Therefore, if a thread modifies this variable, it will affect all threads. This seems to provide convenience for threads to share data. However, PHP usually processes one request for each thread. Therefore, each thread is expected to have a copy of the global variable without mutual interference between requests.

Early PHP was often used in a single-threaded environment. every process started only one thread, so there was no thread security problem. Later, PHP was used in a multi-threaded environment. Therefore, Zend introduced the Zend Thread Safety mechanism (ZTS) to ensure Thread security.

Basic principles and implementation of ZTSBasic ideas

The basic idea of ZTS is intuitive. isn't it that every global variable must have a copy in every thread? I will provide the following mechanism:

In a multi-threaded environment, applying for a global variable is no longer simply declaring a variable. Instead, the whole process allocates a memory space on the heap to use it as the "thread global variable pool ", initialize the memory pool when the process starts. whenever a Thread needs to apply for a global variable, call TSRM through the corresponding method (Thread Safe Resource Manager, the specific implementation of ZTS) and pass the necessary parameters (such as the variable size), TSRM is responsible for allocating the corresponding memory block in the memory pool and returning the reference ID of this memory, so that the next time this thread needs to read and write this variable, TSRM is responsible for real read/write operations by passing a unique reference identifier to TSRM. In this way, global variables of thread security are implemented. The principles of ZTS are given:

Thread1 and Thread2 belong to the same process. each of them requires a Global Var variable, and TSRM allocates a region for each of them in the Global thread memory pool (yellow part, they are identified by a unique ID, so that the two threads can access their own variables through TSRM without interfering with each other.

The following code snippet shows how Zend implements this mechanism. Here I use the source code of PHP5.3.8.

The TSRM implementation code is under the "TSRM" Directory of the PHP source code.

Data structure

There are two important data structures in TSRM: tsrm_tls_entry and tsrm_resource_type. Next, let's take a look at tsrm_tls_entry.

Tsrm_tls_entry is defined in 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 represents all global variable resources of a thread. thread_id stores the thread ID, count records the number of global variables, and next points to the next node. Storage can be seen as a pointer array, where each element is a global variable pointing to the current node representing the thread. The tsrm_tls_entry of each thread is eventually formed into a linked list structure, and the linked list header pointer is assigned to a global static variable tsrm_tls_table. Note: Because tsrm_tls_table is a genuine global variable, all threads will share this variable, which achieves memory management consistency between threads. The tsrm_tls_entry and tsrm_tls_table structures are as follows:

The internal structure of 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 a thread (each thread has one node), and tsrm_resource_type is a resource (or global variable). every time a new resource is allocated, A tsrm_resource_type is created. All tsrm_resource_types form a tsrm_resource_table in an array (linear table). the subscript is the ID of the resource. Each tsrm_resource_type stores the size, structure, and Destructor pointer of the resource. To some extent, tsrm_resource_table can be considered as a hash table. The key is the resource ID, and the value is the tsrm_resource_type structure.

For more information, click the next page!

  • Four pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • 4
  • Next page

Bytes. By checking the information, we know that these macros are related to the thread security mechanism of Zend...

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.