Inter-thread communication method and electronic device

By directly triggering interrupt events in user mode and using hardware interrupt notification service threads to process data, the problem of large communication delays between threads is solved, efficient data transmission and processing is achieved, and it is suitable for big data scenarios.

CN117407183BActive Publication Date: 2024-09-17HUAWEI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202211164272.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-09-23
Publication Date
2024-09-17
Estimated Expiration
2042-09-23

AI Technical Summary

Technical Problem

In the existing technology, there is a problem of large delay in the communication process between threads, especially when relying on the operating system kernel for IPC routing and data transmission, resulting in untimely response and low throughput.

Method used

By directly triggering interrupt events in user mode, using hardware interrupts to notify service threads to process data, and realizing zero-copy data transmission through shared memory, it reduces dependence on the kernel and improves latency in the IPC routing process.

Benefits of technology

It reduces the communication delay between threads and improves data throughput. It is suitable for big data transmission scenarios and does not need to rely on the operating system kernel for notification and data processing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117407183B
    Figure CN117407183B
Patent Text Reader

Abstract

Provided are a method for inter-thread communication and an electronic device, and relate to the field of terminal technology. The method can notify a service thread to process data based on a hardware interrupt, and map the hardware address of a hardware interrupt controller to the user state address space of a client thread. Whenever a client thread has data to process, the client thread can directly trigger an interrupt event in the user state, and the interrupt event will notify the corresponding service thread. When the service thread processes the interrupt event, it will directly process the data to be processed in the user state without having to process it in the kernel state, thereby reducing the delay of inter-thread communication.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of terminal technology, and in particular to a method and electronic device for inter-thread communication. Background Art

[0002] Currently, in electronic devices, an application can usually divide a task into multiple small tasks (such as processes, threads, etc.), and distribute the multiple small tasks to multiple central processing units for operation. In this way, the processing delay of the task can be reduced and the throughput of data processing can be improved.

[0003] When multiple CPUs process processes and threads respectively, the processes and threads can also communicate with each other to complete some tasks between processes or threads. In addition, the operating system of the electronic device can also provide multiple communication mechanisms for communication between processes or threads. However, the current multiple communication mechanisms all have the problem of large delay in the communication process. Summary of the invention

[0004] The embodiment of the present application provides a method and electronic device for inter-thread communication. When a client thread has data to process, the client thread can directly trigger an interrupt event in the user state, so that the service thread can process the data to be processed in time. And the service thread can also process data in the user state. In this way, there is no need to rely on the operating system kernel to implement the triggering of interrupt events and the notification of interrupt events, thereby reducing the delay of inter-thread communication.

[0005] To achieve the above objectives, the embodiments of the present application adopt the following technical solutions:

[0006] In a first aspect, a method for inter-thread communication is provided, and the method can be applied to an electronic device, wherein the electronic device includes an interrupt controller, a client thread, and a service thread. In the method, when the client thread initiates a data processing request, an interrupt event is triggered in a user state based on a user state address corresponding to the client thread. There is a mapping relationship between the user state address and the hardware address of the interrupt controller. In response to the interrupt event, the target service thread corresponding to the interrupt event obtains the data to be processed in the user state and processes the data to be processed.

[0007] The method can process data based on hardware interrupt notification service thread, and map the hardware address of the hardware interrupt controller to the user state address space of the client thread. Whenever the client thread has data to process, the client thread can directly trigger an interrupt event in the user state, and the interrupt event will notify the corresponding service thread. When the service thread processes the interrupt event, it will directly process the data to be processed in the user state without processing it in the kernel state.

[0008] In one possible implementation of the first aspect, the client thread can also save the data to be processed in the shared memory of the client thread and the service thread. The shared memory is used to save the data to be processed. After the client thread triggers an interrupt event, the data to be processed will be saved in the shared memory. When the service thread processes the interrupt event, it will directly read the data to be processed from the shared memory. It can be seen that both the client thread and the service thread can access the shared memory to obtain data, thereby achieving the purpose of data processing through zero-copy data, which can be applicable to scenarios of large data transmission and processing.

[0009] In an implementation of the first aspect, an association relationship between an interrupt event and its corresponding target processor is stored on the electronic device. If the target service thread is awakened in response to the interrupt event, the target service thread runs on the target processor corresponding to the interrupt event. In this implementation, the target processor corresponding to the interrupt event is determined by the association relationship between the interrupt event and its corresponding target processor, so that the interrupt event is generated on the processor. This method can improve the delay problem caused by the IPC routing process.

[0010] In one possible implementation of the first aspect, if the target service thread is not currently running on the target processor when the interrupt event is triggered, the association relationship between the interrupt event and the target processor is updated. Among them, the target processor in the updated association relationship represents the processor that is currently running the target service thread. When the interrupt event is triggered again, the target processing and target service thread for processing the interrupt event can be quickly located according to the updated association relationship, so that the target service thread can quickly process the interrupt event. This method also does not require the kernel of the electronic device operating system to perform IPC routing and other operations, which can improve the delay problem caused by the IPC routing process.

[0011] In an implementation of the first aspect, a callback function corresponding to an interrupt event is injected into the target service thread in kernel state, and the target service thread obtains the data to be processed from the shared memory by executing the callback function in user state. In this implementation, the target service thread returns to the user state when executing the callback function, thereby achieving the purpose of processing the interrupt event in the user state.

[0012] In an implementation of the first aspect, after the client thread saves the data to be processed, the stack frame pointer corresponding to the callback function points to the address of the data to be processed in the shared memory. Thus, when the target service thread executes the callback function, the callback function can obtain the data to be processed at the address pointed to by the stack frame pointer.

[0013] In one implementation of the first aspect, if the target service thread is currently running when the interrupt event is triggered, the target service thread is interrupted and a callback function is injected into the kernel state; or, if the target service thread is not currently running when the interrupt event is triggered, the target service thread is awakened and a callback function is injected into the kernel state. It can be seen that no matter whether the called target service thread is currently in a running state or a non-running state, after the interrupt event is triggered, the callback function will be injected into the target service thread and the callback function will be executed, thereby achieving the purpose of quickly processing the interrupt event.

[0014] In an implementation of the first aspect, before the client thread initiates a data processing request, multiple service threads may also register corresponding interrupt events respectively. After registration, a mapping relationship exists between the hardware interrupt number of the interrupt event and the thread number of the service thread that registers the interrupt event, and the mapping relationship between the hardware interrupt number and the thread number is stored in the inter-process communication processing information. Thus, after the interrupt event is triggered, the corresponding target service thread can be quickly determined according to the inter-process communication processing information.

[0015] In an implementation method of the first aspect, after registration, the interrupt event is configured with a corresponding callback function, and the hardware interrupt number of the interrupt event is configured with a corresponding virtual interrupt number; the hardware interrupt number of the interrupt event is associated with the callback function, and the callback function obtains the data to be processed through the virtual interrupt number.

[0016] In an implementation of the first aspect, if the target service thread has a pending interrupt flag, the target service thread is injected with a callback function corresponding to the interrupt event in kernel mode. In this implementation, the pending interrupt flag indicates that the target service thread has a pending interrupt event.

[0017] In an implementation of the first aspect, the electronic device deletes the pending interruption mark after the callback function is injected into the target service thread, thereby indicating that the target service thread has no other interruption events to process after processing the current interruption event, and can continue to process other interruption events.

[0018] In an implementation manner of the first aspect, after the interrupt event is triggered, the target service thread is set with an anti-nesting flag. In this implementation manner, the anti-nesting flag indicates that the target service thread is prohibited from being injected with callback functions corresponding to other interrupt events when processing the interrupt event, thereby preventing the target service thread from being interrupted by other interrupt events when processing the interrupt event, and ensuring that the target service thread can completely process an interrupt event.

[0019] In an implementation manner of the first aspect, the electronic device deletes the anti-nesting mark after the target service thread completes processing the data to be processed.

[0020] In an implementation manner of the first aspect, the electronic device uninstalls the callback function after the target service thread completes executing the callback function, so that the target service thread returns to the original execution flow and continues to run.

[0021] In an implementation manner of the first aspect, the service thread may register a corresponding interrupt event through a first calling function.

[0022] In an implementation manner of the first aspect, the service thread may abandon the currently occupied processor through the second calling function and wait to be awakened by an interrupt event.

[0023] In an implementation manner of the first aspect, the electronic device may uninstall the callback function from the target service thread by using a third calling function.

[0024] In a second aspect, an electronic device is provided, comprising a memory and one or more processors; the memory is coupled to the processor; wherein computer program code is stored in the memory, and the computer program code comprises computer instructions, and when the computer instructions are executed by the processor, the electronic device executes a method for inter-thread communication as in any implementation of the first aspect.

[0025] In a third aspect, a computer-readable storage medium is provided, comprising computer instructions, which, when executed on an electronic device, enables the electronic device to execute a method for inter-thread communication as in any implementation of the first aspect.

[0026] In a fourth aspect, a computer program product is provided. When the computer program product is run on a computer, the computer is caused to execute the method for inter-thread communication in any implementation of the first aspect.

[0027] It can be understood that the beneficial effects that can be achieved by the electronic device described in the second aspect, the computer-readable storage medium described in the third aspect, and the computer program product described in the fourth aspect provided above can be referred to the beneficial effects in the first aspect and any possible design method thereof, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS

[0028] Figure 1 A schematic diagram of an inter-process communication without a notification function shown in an embodiment of the present application;

[0029] Figure 2 A schematic diagram of an inter-process communication with a notification function shown in an embodiment of the present application;

[0030] Figure 3 A schematic diagram of direct communication between processes shown in an embodiment of the present application;

[0031] Figure 4A schematic diagram of a system architecture of an electronic device shown in an embodiment of the present application;

[0032] Figure 5 A functional schematic diagram of an interrupt registration module shown in an embodiment of the present application;

[0033] Figure 6 A functional schematic diagram of an interrupt routing module shown in an embodiment of the present application;

[0034] Figure 7 A functional schematic diagram of an interrupt processing module shown in an embodiment of the present application;

[0035] Figure 8 A functional schematic diagram of an interrupt injection module shown in an embodiment of the present application;

[0036] Fig. 9 A functional schematic diagram of a memory mapping module shown in an embodiment of the present application;

[0037] Fig.10 A schematic diagram of a thread communication method shown in an embodiment of the present application;

[0038] Fig.11 A schematic diagram of communication between a user-mode working thread and a user-mode network protocol stack shown in an embodiment of the present application;

[0039] Fig.12 A schematic diagram of a protocol stack thread packet receiving process shown in an embodiment of the present application;

[0040] Fig.13 A schematic diagram of a protocol stack thread packet issuance process shown in an embodiment of the present application;

[0041] Fig.14 A schematic diagram of the structure of an electronic device shown in an embodiment of the present application. DETAILED DESCRIPTION

[0042] The technical solutions in the embodiments of the present application will be described below in conjunction with the drawings in the embodiments of the present application. Among them, in the description of the present application, unless otherwise specified, " / " indicates that the objects associated before and after are in an "or" relationship, for example, A / B can represent A or B; "and / or" in the present application is only a description of the association relationship of the associated objects, indicating that there can be three relationships, for example, A and / or B, which can represent: A exists alone, A and B exist at the same time, and B exists alone, where A and B can be singular or plural. And, in the description of the present application, unless otherwise specified, "multiple" refers to two or more than two. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single items or plural items. For example, at least one of a, b, or c can represent: a, b, c, ab, ac, bc, or abc, where a, b, c can be single or multiple. In addition, in order to facilitate the clear description of the technical solutions of the embodiments of the present application, in the embodiments of the present application, the words "first", "second" and the like are used to distinguish the same items or similar items with substantially the same functions and effects. Those skilled in the art will understand that the words "first", "second" and the like do not limit the quantity and execution order, and the words "first", "second" and the like do not necessarily limit the differences. At the same time, in the embodiments of the present application, the words "exemplary" or "for example" are used to indicate examples, illustrations or explanations. Any embodiment or design described as "exemplary" or "for example" in the embodiments of the present application should not be interpreted as being more preferred or more advantageous than other embodiments or design solutions. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a concrete manner for ease of understanding.

[0043] In addition, the business scenarios described in the embodiments of the present application are intended to more clearly illustrate the technical solutions of the embodiments of the present application, and do not constitute a limitation on the technical solutions provided in the embodiments of the present application. A person of ordinary skill in the art can appreciate that with the emergence of new business scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.

[0044] In order to better understand the solution of the present application, the following introduces the terms involved in the embodiments of the present application:

[0045] (1) Central processing unit (CPU) privilege level

[0046] At present, general-purpose chips support two CPU privilege levels: user mode and kernel mode. Various software systems run at different CPU privilege levels. These software systems have different access rights to the underlying hardware. For example, the operating system (OS) running in kernel mode can directly operate printers and network cards; programs running in user mode rely on the operating system to operate hardware devices, such as text editors printing files and web browsers accessing the Internet.

[0047] When a task (such as a process, thread, etc.) is trapped in the kernel code due to executing a system call, the task can be said to be in the kernel running state (or simply kernel state). When a task is executing non-kernel code (such as application code), the task can be said to be in the user running state (or simply user state).

[0048] (2) Kernel

[0049] The kernel is the internal core program of the operating system, which provides core management calls for external devices such as computers. The address space where the kernel is located is called the kernel space.

[0050] (3) Interruption events

[0051] An interrupt event is an event triggered by hardware. When this event occurs, it interrupts the current execution flow of the CPU and causes the CPU to execute a new execution flow. The execution flow refers to the code flow, instruction flow, etc. executed on the CPU. The execution flow can be run by processes, threads, etc. on the CPU.

[0052] (4) Threads

[0053] A thread is the smallest unit used by the operating system to execute business programs and perform scheduling. A thread refers to a single sequential execution flow.

[0054] (5)Thread context

[0055] Each instruction executed by a thread corresponds to a set of general purpose register (GPR) values, which form a snapshot of the thread's running state. When the thread's execution flow is interrupted by an interrupt event, the operating system saves the GPR value at the time of the interruption in memory, so that after the thread handles the interrupt event, it can resume the original execution flow and continue execution.

[0056] (6)Thread Scheduling

[0057] When the number of threads running in the operating system exceeds the number of CPU cores, the operating system will periodically select a thread to run on the CPU, and other threads will be suspended. This process is called thread scheduling.

[0058] (7)Thread Migration

[0059] In a multi-CPU operating system, when the CPU workload is too heavy, the operating system will migrate the tasks in the CPU's run queue to the run queues of other CPUs and have them run by other CPUs. This process is called thread migration.

[0060] In recent years, with the development of chip manufacturing technology, the number of CPUs integrated into a computer has increased. In order to tap into the parallel computing capabilities of these CPUs, most applications have evolved from single-threaded execution to multi-threaded execution. Through multi-threading, an application can divide a task into multiple small tasks (such as processes, threads, etc.) and distribute multiple small tasks to multiple CPUs for calculation. The benefits of this multi-CPU + multi-threading are: (1) reducing latency, for example, a computer can complete the same scale of business in less time; (2) improving throughput, for example, a computer can complete more business in a unit of time.

[0061] However, multiple processes and threads of an application also need to communicate with each other. Usually, communication between threads occurs between a client thread and a server thread. The client thread refers to the thread that initiates a data processing request during communication, and the server thread refers to the thread that processes the data provided by the client thread in response to the data processing request during communication. During communication, the client thread passes data to the server thread, and the server thread receives and processes the data. The operating system can provide a variety of inter-process communication (IPC) mechanisms, such as shared memory, signal, named pipe, event file (Eventfd), socket, multi-way event waiting mechanism (Epoll), etc. These IPC mechanisms have their own characteristics, but they are roughly divided into two types: IPC without notification function and IPC with notification function.

[0062] like Figure 1As shown in the figure, when inter-thread communication is performed based on the IPC mechanism without notification function, the Client thread first prepares the data to be processed and stores the data and the Server thread number in the data buffer. Since the Client thread will not notify the Server thread that the data is ready, the Server thread needs to obtain the Server thread number and the data to be processed from the data buffer by polling. When the Server thread number matches the Server thread that currently initiates the polling, the Server thread will start processing the corresponding data to be processed.

[0063] exist Figure 1 In the inter-thread communication process shown in the figure, the server thread cannot find the data to be processed in time by polling, which may cause the server thread to respond untimely, thereby increasing the IPC delay.

[0064] like Figure 2 As shown in the figure, when inter-thread communication is performed based on the IPC mechanism with notification function, after the Client thread prepares the data, it will store the data to be processed and the Server thread number in the data cache. After that, the Client thread notifies the Server thread through the operating system that the data is ready, so that the Server thread can process the data to be processed. In this process, first, the kernel of the operating system selects the CPU running the Server thread through the load balancing algorithm and adds the corresponding Server thread to the task queue. Then, the kernel determines the Server thread to execute the task and the data to be processed by polling the task queue. Finally, the determined Server thread processes the data to be processed.

[0065] exist Figure 2 In the inter-thread communication process shown in the figure, several IPC mechanisms can be combined to implement an efficient and feature-rich IPC solution. For example, shared memory + event file + multi-way event waiting mechanism. The client thread passes data to the server thread through the shared memory mechanism. The server thread waits for notification from the client thread through the multi-way event waiting mechanism. After the data is passed, the client thread notifies the server thread through the event file mechanism, and the notification needs to be made through the kernel.

[0066] It can be seen that the above Figure 2This IPC solution uses the kernel as a communication relay, and the main links of IPC (for example, initiation, routing, and response, etc.) all rely on the kernel. The client thread relies on the kernel to send notifications to the server thread, and the server thread also needs to periodically fall into the kernel query notification. This way of relying on the kernel for communication also makes it impossible for the server thread to process data in a timely manner, thereby increasing the time consumption of IPC response. In addition, the kernel uses a load balancing algorithm to select a CPU to execute the server thread, which will increase the latency of the IPC routing process. In addition, the kernel uses a scheduler to arrange the timing of the server thread to go online. When the system load is large, the timing of going online is unpredictable, which will also increase the latency of the IPC scheduling process.

[0067] In some current IPC solutions, the IPC latency can be reduced by simplifying the IPC processing logic, or by making improvements in the hardware.

[0068] For example, see Figure 3 , by simplifying the IPC processing logic to reduce the IPC latency, a solution of direct switching between processes can be adopted. This solution is a pure software solution, and IPC processing can be performed through the kernel. Among them, the kernel can specifically simplify the IPC processing logic to the minimum, including only the necessary stages. When executing a process call, the kernel skips the process scheduling stage and directly switches the process to the corresponding context for execution. This solution can simply and effectively avoid the uncertain delay caused by kernel scheduling when the callee is in the ready state. However, this solution still requires kernel intervention to complete the context switch, and there is still the problem of increasing IPC latency. Moreover, this solution requires data to be transferred by copying, which is not suitable for application scenarios of large data transmission. In addition, if the caller and the callee are running on different cores, it is necessary to notify the callee to run through methods such as inter-core interrupts, which will also increase the IPC latency.

[0069] As another example, a hardware coordination solution can be used to reduce IPC latency by making improvements in hardware. This solution first designs a hardware architecture for cross-process calls, and proposes a corresponding software design solution based on the architecture. The method provides support for both context switching and fast data transmission in the underlying hardware. Such a solution of context switching primitives for cross-process calls supported by hardware can implement a context switching method similar to function calls, thereby reducing the IPC latency caused by kernel intervention in context switching. In addition, the hardware supports a zero-copy data transmission mechanism, which can reduce the IPC latency caused by data copy transmission. However, this solution requires major modifications to the memory management unit (MMU) module in the CPU, and the hardware part is difficult to implement.

[0070] From the above content, it can be seen that the current IPC solutions all have the problem of increasing IPC latency or are difficult to implement in hardware.

[0071] Based on the above content, the embodiment of the present application provides a method for inter-thread communication, which can notify the Server thread to process data based on the hardware interrupt, and map the hardware address of the hardware interrupt controller to the user state address space of the Client thread. Whenever the Client thread has data to process, the Client thread can directly trigger an interrupt event in the user state, and the interrupt event will notify the corresponding Server thread. When the Server thread processes the interrupt event, it will directly process the data to be processed in the user state without processing it in the kernel state.

[0072] Since hardware interrupts are asynchronous, notifiable, and timely, whenever the Client thread has data to process, the Server thread can respond and process it in a timely manner. In addition, the Client thread can trigger interrupt events in user mode, and the Server thread can also process interrupt events in user mode without relying on the kernel for notification, thereby reducing the latency of communication between threads. Furthermore, the CPU association relationship corresponding to the interrupt event (such as CPU affinity) can be used to directly determine the CPU running the Server thread, thereby generating an interrupt on the CPU. After that, the Server thread running on the CPU can start processing the data to be processed until it is completed. This approach can improve the latency problem caused by the IPC routing process, and when an interrupt event is triggered, the CPU will change the current execution flow of the thread to prioritize the interrupt event.

[0073] In addition, the above method uses shared memory to save the data to be processed. After the Client thread triggers an interrupt event, the data to be processed will be saved in the shared memory. When the Server thread processes the interrupt event, it will directly read the data to be processed from the shared memory. It can be seen that both the Client thread and the Server thread can access the shared memory to obtain data, thereby achieving the purpose of data processing through zero-copy data, which can be applied to the scenario of large data transmission and processing.

[0074] The method for inter-thread communication in the embodiment of the present application can be applied to electronic devices including an interrupt controller and one or more CPUs, such as computers, laptops, servers, mobile phones, tablet computers, etc.

[0075] For example, the electronic device is a computer. Figure 4 , the electronic device may include an application layer, a user mode layer, a kernel mode layer and a hardware layer.

[0076] The application layer may include a variety of applications that rely on IPC, such as applications that communicate using mechanisms such as pipes, signals, and fast userspace mutexes (Futexes) (referred to as application 1, application 2, application N, etc.).

[0077] In some embodiments, the client thread that triggers the interrupt event and the called server thread may come from an application program in the application layer.

[0078] The user-mode layer can include an IPC call interface module and a memory mapping module. These two modules provide the communication interface required by the application and map the memory-mapped I / O (MMIO) register file of the interrupt controller to the user-mode address, thereby mapping the hardware address of the interrupt controller to the user-mode address space of the thread that triggers the interrupt event. In addition, the IPC call interface module and the memory mapping module also provide a usage interface similar to shared memory.

[0079] In some embodiments, the IPC call interface module and the memory mapping module can also enable the Client thread and the Server thread in the application layer to complete the processing of interrupt events in the user mode.

[0080] The kernel state layer may include an interrupt registration module, an interrupt routing module, an interrupt processing module, and an interrupt injection module. Among them, the interrupt registration module is used to register the corresponding interrupt event and callback function for the called Server thread. The interrupt routing module is used to determine the target CPU for processing the interrupt event after the interrupt event is triggered. The interrupt processing module is used to respond to the interrupt event and enable the target Server thread corresponding to the interrupt event to process the interrupt event, and the target Server thread runs on the target CPU. The interrupt injection module is used to inject the callback function corresponding to the interrupt event into the target Server thread in the kernel state. When the target Server thread returns to the user state, it immediately obtains the data to be processed corresponding to the interrupt event through the callback function and processes the data. Among them, the data to be processed corresponding to the interrupt event is the data provided by the Client thread that triggers the interrupt event.

[0081] The functions that can be implemented by the above-mentioned interrupt registration module, interrupt routing module, interrupt processing module and interrupt injection module in the kernel state layer can also be regarded as the functions that can be implemented by the kernel of the operating system.

[0082] The hardware layer may include a generic interrupt controller (GIC) as a hardware structure, wherein the interrupt controller may generate a hardware interrupt, ie, an interrupt event, and transmit IPC data to be processed (eg, data to be processed) corresponding to the interrupt event.

[0083] In the embodiment of the present application, the role of the interrupt event is to enable the Server thread to process the IPC data in a timely manner, and the process of the Server thread processing the IPC data can also be regarded as the process of the Server thread processing the interrupt event. The interrupt event is registered by the Server thread and triggered by the Client thread.

[0084] In some embodiments, see Figure 5 When the server thread registers an interrupt event, the above interrupt registration module can achieve the following functions:

[0085] (1) Querying available interrupt numbers: The interrupt registration module obtains the hardware interrupt number of an unoccupied interrupt event by querying the interrupt controller.

[0086] (2) Binding virtual interrupt number: The interrupt registration module assigns a virtual interrupt number to the hardware interrupt number of the interrupt event. The virtual interrupt number can be used as a parameter of the callback function of the interrupt event. The server thread can obtain the pending data corresponding to the interrupt event through the callback function, and the callback function can obtain the pending data corresponding to the interrupt event according to the virtual interrupt number of the interrupt event.

[0087] In the embodiment of the present application, the hardware interrupt number is provided or used by the hardware structure, and the virtual interrupt number is provided or used by the software, kernel, process, thread, etc. in the operating system.

[0088] (3) Binding callback function: The interrupt registration module binds a callback function to the hardware interrupt number of the interrupt event, or sets a callback function, that is, one interrupt event can correspond to one callback function. Among them, the callback function is a user-state callback function. When the server thread processes the interrupt event, it can execute the callback function corresponding to the interrupt event in the user state, so as to obtain and process the data to be processed in the user state.

[0089] (4) Binding the current thread number: The interrupt registration module binds the thread number to the hardware interrupt number of the interrupt event, where the thread number can represent the thread number of the server thread that registers the interrupt event, that is, one interrupt event can correspond to one server thread. When the interrupt event is triggered, the target server thread corresponding to the interrupt event can process the interrupt event.

[0090] In the process of binding the thread number, the CPU affinity of the interrupt event can also be set through the interrupt controller, so that after the interrupt event is triggered, the corresponding target CPU can be determined according to the CPU affinity of the interrupt event, and the target Server thread corresponding to the interrupt event can be run on the target CPU to process the data to be processed. Among them, the CPU affinity of the interrupt event can represent the association relationship, correspondence relationship, etc. between the interrupt event and the corresponding CPU. Usually, one interrupt event can correspond to one CPU. In some implementations, the CPU affinity of the interrupt event can be represented by the mapping relationship between the hardware interrupt number of the interrupt event and the CPU number of the target CPU.

[0091] When the target Server thread corresponding to the interrupt event is not currently running on the target CPU corresponding to the interrupt event, the interrupt controller may also update the CPU affinity corresponding to the interrupt event, and update the CPU running the target Server thread to the target CPU.

[0092] When the target Server thread corresponding to the interrupt event is not currently running, the target Server thread can be awakened and controlled to run on the target CPU corresponding to the interrupt event.

[0093] From the above functions, we can know that after the interrupt event is registered, one interrupt event corresponds to a callback function, a target server thread and a target CPU, and the target server thread runs on the target CPU. In addition, there is a mapping relationship between the interrupt event and its corresponding target server thread, and there is also a mapping relationship between the hardware interrupt number of the interrupt event and the thread number of the target server thread corresponding to the interrupt event.

[0094] In some embodiments, IPC processing information including the above-mentioned mapping relationship can also be introduced into the kernel state layer, such as an IPC process table, which can include the following fields: the hardware interrupt number assigned to the server thread, the server thread number for processing IPC data, the callback function for processing IPC data, and the stack frame pointer corresponding to the callback function. The above-mentioned interrupt registration module can establish a one-to-one mapping relationship between the server thread number and the hardware interrupt number of the interrupt event through the IPC process table. That is, the corresponding interrupt event can be determined according to the server thread through the IPC process table, and the corresponding server thread can also be determined according to the interrupt event.

[0095] In the embodiment of the present application, the Server thread that is called (or called the IPC receiving end) can initiate the registration process of the interrupt event, and the specific registration process is implemented by the above-mentioned interrupt registration module.

[0096] In addition to the above-mentioned key functions, in some embodiments, a first calling function can also be introduced as a user-mode programming interface of the interrupt registration module, such as a system call: bind_uirq. The first calling function is initiated by the Server thread and receives two parameters: a callback function and a stack frame pointer corresponding to the callback function. Among them, the callback function can obtain the data to be processed according to the virtual interrupt number of the interrupt event. The stack frame pointer points to a memory block, and the memory block will be used as a stack frame when executing the callback function. The memory block can save the data to be processed of the interrupt event corresponding to the callback function. The memory block represents an independent memory space, and the data to be processed is stored in the memory space by the Client thread, and then the callback function is executed by the Server thread, and the data to be processed is read from the memory space, thereby achieving the purpose of zero copy of the data. When the Server thread initiates the above-mentioned first calling function, the interrupt registration module in the kernel state layer can realize the above-mentioned function, that is, the Server thread can register the interrupt event through the above-mentioned first calling function. When the above-mentioned first calling function is successfully executed, it indicates that the registration of the interrupt event is completed, and the above-mentioned first calling function will return a virtual interrupt number of the interrupt event to the Server thread.

[0097] In some embodiments, after an interrupt event is triggered, the interrupt routing module can track the target CPU that handles the interrupt event, that is, track the target CPU that runs the target Server thread, through the CPU affinity interface of the interrupt event provided by the interrupt controller. Figure 6 If the target Server thread is not running on the target CPU, it means that the target Server thread has been migrated to a new CPU. At this time, it is necessary to update the CPU affinity of the interrupt event through the programming interface of the interrupt controller and set the routing target of the interrupt event to the new CPU after the target Server thread is migrated, that is, to associate or correspond the interrupt event with the CPU number of the new CPU.

[0098] In some embodiments, see Figure 7 When the target Server thread responds to the interrupt event and starts to process the interrupt event, the above interrupt processing module can implement the following functions:

[0099] (1) Query the IPC processing table: The interrupt processing module reads the hardware interrupt number of the triggered interrupt event (i.e., the interrupt event to be processed) from the interrupt controller, and then queries whether the corresponding hardware interrupt number exists in the IPC processing table. If it can be found, it means that the interrupt event is an interrupt event that can be used to process IPC data and should be processed by the Server thread in user mode.

[0100] (2) Setting the pending interrupt flag of the service thread: The interrupt processing module sets the pending interrupt flag (Pending UIRQ) corresponding to the target Server thread. Since the called target Server thread may have been scheduled to process other data or events and cannot process IPC data in time, configuring the pending interrupt flag for it can enable the target Server thread to process the corresponding interrupt event or IPC data in time after being awakened or interrupting the current execution flow.

[0101] (3) Detecting the running status of the service thread: The interrupt processing module detects the running status of the target Server thread when it is called. If the target Server thread is running the execution flow, the execution flow of the target Server thread can be interrupted and the interrupt injection module in the embodiment of the present application can be used to achieve the purpose of the target Server thread quickly processing IPC data. If the target Server thread is not running, the interrupt processing module will actively wake up the target Server thread. When the target Server thread is called into the target CPU corresponding to the interrupt event, the interrupt injection module in the embodiment of the present application can be used to achieve the purpose of the target Server thread quickly processing IPC data.

[0102] In some embodiments, in order to balance response latency and system throughput, the Server thread will actively give up the CPU after processing key business to avoid CPU idling and energy consumption, so that the Server thread will be in a non-running state. When the Server thread needs to run again, it needs to be awakened.

[0103] In addition to the above key functions, in some embodiments, a second calling function can be introduced as a user-mode programming interface, such as a system call: wait_uirq. The server thread can call the second calling function to give up the CPU, and at the same time monitor all virtual interrupt numbers registered through the above first calling function, waiting to be awakened by the interrupt event corresponding to the virtual interrupt number.

[0104] When the interrupt event registered by the Server thread is triggered by the Client thread, the execution flow of the Server thread will be interrupted immediately and start processing the interrupt event, that is, processing the pending IPC data. In addition, after the interrupt event is triggered, the above interrupt processing module will also promptly notify the called Server thread to process the IPC data.

[0105] In some embodiments, see Figure 8 When the target Server thread responds to an interrupt event and handles the interrupt event, the above interrupt injection module can achieve the following functions:

[0106] (1) Detecting the pending interruption flag of the service thread: When the current execution flow of the target server thread is interrupted, the interruption injection module can detect the pending interruption flag of the target server thread.

[0107] (2) Save the original user state context: If the target server thread sets the pending interrupt flag, it means that the target server thread has pending interrupt events or IPC data to be processed. At this time, the interrupt injection module can inject the callback function corresponding to the interrupt event into the target server thread. When the target server thread processes the interrupt event, it can execute the callback function in the user state and obtain the pending IPC data through the callback function.

[0108] Furthermore, in order not to destroy the original execution flow of the target Server thread, the interrupt injection module can also save the user state context of the original execution flow in the kernel state layer, so that the target Server thread can continue to run the original execution flow after processing the interrupt event.

[0109] (3) Replace the new user state context: After injecting the callback function, the interrupt injection module can also set the callback function pointer PC and the stack frame pointer SP in the current user state context of the target Server thread to the callback function address and the address of the IPC data to be processed. The callback function pointer and the stack frame pointer are registered in the IPC processing table of the kernel state layer through the above-mentioned first calling function.

[0110] After replacing the new user state context, the target Server thread can call back the function pointer to execute the callback function and obtain the IPC data to be processed according to the stack frame pointer.

[0111] (4) Setting an anti-nesting flag: In order to prevent the interrupt injection module from repeatedly installing the callback function corresponding to the interrupt event into the user mode layer, that is, to prevent the callback function from being injected into the execution flow of the target Server thread, an anti-nesting flag (Injected) can be introduced into the target Server thread in the embodiment of the present application. In addition, the flag should be set after the interrupt event is triggered and before the target Server thread starts to execute the callback function. This prevents the target Server thread from being interrupted by other interrupt events while processing the interrupt event and being injected with callback functions corresponding to other interrupt events again, thereby ensuring that the target Server thread can completely process an interrupt event.

[0112] (5) Deleting the pending interruption mark of the service thread: Because the interruption injection module starts to inject the callback function into the execution flow of the target server thread after the execution flow of the target server thread will be interrupted immediately, after the injection, the target server thread can execute the callback function when processing the interruption event. Then, after the callback function is injected, the pending interruption mark of the target server thread can be deleted, indicating that after the target server thread processes the current interruption event, there are no other interruption events to be processed, and other interruption events can be processed.

[0113] (6) Restoring the original user state context: When the callback function of the target server thread is executed, the interrupt injection module will unload the callback function in the target server thread execution flow so that the target server thread can be restored to the original execution flow and continue to run.

[0114] (7) Deleting the anti-nesting flag: Since the callback function has been executed, there is no problem of repeated interrupt injection. Therefore, the interrupt injection module will delete the anti-nesting flag of the target server thread after the callback function is executed. Alternatively, the interrupt injection module can also delete the anti-nesting flag of the target server thread after the target server thread has processed the pending IPC data.

[0115] In addition to the above-mentioned key functions, in some embodiments, a third calling function may be introduced as a user-mode programming interface, such as a system call: uirq_exit. The interrupt injection module unloads the callback function from the execution flow of the target Server thread through the third calling function.

[0116] In the above content, in order to allow the target Server thread to start processing IPC data as soon as possible, the interrupt injection module injects a callback function into the execution flow of the target Server thread.

[0117] In some embodiments, when an interrupt event is triggered, if the target server thread corresponding to the interrupt event is currently running, the execution flow of the target server thread will be interrupted, and the interrupt injection module will inject a callback function into the execution flow of the target server thread. In this way, no matter which CPU the target server thread is running on, the method in the embodiment of the present application can interrupt the target server thread and inject a callback function.

[0118] In other embodiments, when an interrupt event is triggered, if the target Server thread corresponding to the interrupt event is not currently running, the target Server thread will be awakened and scheduled to the target CPU corresponding to the interrupt event, and the interrupt injection module will inject a callback function into the execution flow of the target Server thread.

[0119] When the target Server thread starts processing the interrupt event, the callback function can be directly executed to obtain the IPC data to be processed and process the data.

[0120] In some embodiments, see Fig. 9, because the interrupt event triggered by the Client thread is responded by the Server thread, when the Server thread calls the bind_uirq system call to register the interrupt event, a virtual MMIO register file corresponding to the interrupt event will be generated in the file system. The above-mentioned memory mapping module can map the hardware address of the interrupt controller to the address space of the user state address of the Client thread, wherein the mapping relationship between the hardware address and the user state address can be saved in the MMIO register file, that is, the hardware address of the interrupt controller is exposed to the user state in the form of an MMIO register file. Thus, the Client thread can trigger an interrupt event in the user state based on the user state address corresponding to the Client thread. Exemplarily, the Client thread can access the MMIO register file in the user state. Since the MMIO register file includes the mapping relationship between the hardware address and the user state address, the data processing request or control signal initiated by the Client thread can be sent to the hardware address through the mapping relationship, thereby controlling the interrupt controller to generate an interrupt event and achieving the purpose of triggering the interrupt event.

[0121] In some embodiments, the interrupt controller can also provide interfaces for triggering interrupt events, and these interfaces can be programming interfaces of MMIO registers of the interrupt controller. In this case, the mapping relationship between the hardware interface and the user state address can be saved in the MMIO register file.

[0122] The interrupt event triggered by the client thread through the MMIO register file will only be notified to the target server thread that registered the MMIO register file. The client thread can directly control the interrupt controller to trigger the interrupt event in the user state. In this way, the operation of directly triggering the interrupt event in the user state does not need to rely on the kernel to notify the target server thread to handle the interrupt event, which further improves the performance of the IPC initiation link.

[0123] The memory mapping module can use the user-mode device input and output (UIO) programming interface of the operating system kernel to access the MMIO register of the interrupt controller, thereby achieving the purpose of mapping the hardware address of the interrupt controller to the user-mode address.

[0124] In some embodiments, the above-mentioned IPC call interface module can provide an interface for triggering an interrupt event. In addition, the IPC call interface module uses the programming interface of the MMIO register on different hardware platforms to trigger the corresponding interrupt event. For example, on ARM GIC, the IPC call interface module only needs to write a non-zero value into an unsigned 32-bit integer pointer to trigger an interrupt event.

[0125] Based on the above Figure 4When the electronic device shown implements the method for inter-thread communication provided in the embodiment of the present application, refer to Fig.10 , may include the following steps S11-S13.

[0126] S11. The server thread in the electronic device registers an interrupt event.

[0127] The server thread may register an interrupt event through an interrupt registration module in the kernel state layer. If there are multiple server threads in the electronic device, each server thread may register an interrupt event.

[0128] During the process of registering an interrupt event, the interrupt registration module assigns a virtual interrupt number to the hardware interrupt number of the interrupt event and binds the corresponding callback function to the hardware interrupt number. Each interrupt event corresponds to a callback function, and the virtual interrupt number of the interrupt event can be used as a parameter of the callback function. The callback function can obtain the to-be-processed data corresponding to the interrupt event according to the virtual interrupt number of the interrupt event.

[0129] In addition, there is a mapping relationship between the hardware interrupt number of the interrupt event and the thread number of the server thread that registers the interrupt event. Therefore, when the interrupt event is triggered, the thread number that processes the interrupt event can be found according to the hardware interrupt number of the interrupt event, and the corresponding target server thread can be determined according to the thread number. The interrupt registration module can also save the hardware interrupt number and the corresponding thread number of the registered interrupt event in the IPC processing information (such as the IPC table) for easy search.

[0130] Also, when the Server thread registers an interrupt event, the memory mapping module can map the hardware address of the interrupt controller to the user state address of the Client thread, or map the MMIO register file of the interrupt controller to the address space of the Client thread.

[0131] S12: When the Client thread in the electronic device initiates a data processing request, an interrupt event is triggered in the user state based on the user state address corresponding to the Client thread.

[0132] In some embodiments, when the Client thread initiates a data processing request, it may also access the user-mode MMIO register file in the user mode to trigger an interrupt event.

[0133] In some embodiments, after receiving data sent by an application of an electronic device, the Client thread may initiate a data processing request, trigger an interrupt event, and thus call the Server thread to process the pending data provided by the Client thread. The pending data may be data prepared or preprocessed by the Client thread on the data sent by the application.

[0134] When an interrupt event is triggered, the above-mentioned IPC may call the interface module to provide an interface for triggering the interrupt event, thereby realizing communication between the Client thread and the interrupt controller.

[0135] In the aforementioned embodiment, the memory mapping module can map the hardware address of the interrupt controller to the user state address of the Client thread, so the Client thread can access the hardware address of the interrupt controller based on the user state address, thereby triggering the interrupt controller to generate an interrupt event. When the electronic device has IPC data to be processed, the IPC data can be sent to the Client thread first. The Client thread will trigger the interrupt event in the user state instead of relying on the operating system kernel to trigger the interrupt event, which can reduce the delay of communication between threads.

[0136] In some embodiments, after the Client thread triggers an interrupt event, the IPC data to be processed can also be saved in a sharable preset memory block, waiting to be read and processed by the target Server thread. The preset memory block can be shared with the Client thread and the Server thread, so that the Client thread and the Server thread can both store data in the preset memory block and read data from the preset memory block.

[0137] In some embodiments, the interrupt event triggered by the Client thread may be any one of the multiple interrupt events registered by the multiple Server threads in the aforementioned embodiments.

[0138] S13. In the electronic device, in response to an interrupt event, a target Server thread corresponding to the interrupt event obtains the data to be processed in a user state and processes the data to be processed.

[0139] After the interrupt event is triggered, the target server thread corresponding to the interrupt event in the electronic device will respond to the interrupt event, and the execution flow of the target server thread is interrupted to start processing the interrupt event. When determining the target server thread, the interrupt processing module in the electronic device can search for the corresponding thread number in the IPC processing information according to the hardware interrupt number of the interrupt event, and determine the server thread corresponding to the found thread number as the target server thread.

[0140] Since hardware interrupts are asynchronous, notifiable, and timely, whenever the Client thread has data to process, the Server thread can respond and process it in a timely manner. In addition, the Client thread can trigger interrupt events in user mode, and the Server thread can also process interrupt events in user mode without relying on the kernel for notification, thereby reducing the delay in communication between threads.

[0141] In some embodiments, after an interrupt event is triggered, the interrupt controller in the electronic device determines the target CPU for processing the interrupt event based on the CPU affinity of the interrupt event. Usually, the target CPU runs the target Server thread. If the target Server thread is not running on the target CPU, the interrupt controller can update the CPU affinity of the interrupt event, associate the interrupt event with the CPU currently running the target Server thread, and use this CPU as the new target CPU. When the interrupt event is triggered again, the target CPU and target Server thread for processing the interrupt event can be quickly located based on the CPU affinity of the interrupt event, so that the target Server thread can quickly process the interrupt event. This method also does not require the kernel of the electronic device operating system to perform IPC routing and other operations, and can improve the delay problem caused by the IPC routing process. For example, the delay of the IPC routing link can be reduced from 5 to 10us to 100ns, greatly improving the data throughput of communication between threads.

[0142] In other embodiments, after an interrupt event is triggered, the interrupt processing module in the electronic device can read the hardware interrupt number of the interrupt event from the interrupt controller, and then query whether there is a corresponding hardware interrupt number in the IPC processing table. If it can be queried, it means that the interrupt event has been registered and can be used to process IPC data, and it can be processed by the target Server thread in user mode. If it cannot be queried, it means that the interrupt event has not been registered and cannot be used to process IPC data, then the interrupt controller can regenerate an interrupt event. Afterwards, the interrupt processing module can query the IPC processing table again until the hardware interrupt number of the interrupt event is queried. This ensures that the interrupt event has a corresponding Server thread to process, and ensures the validity of the interrupt event.

[0143] In some embodiments, after an interrupt event is triggered and the interrupt processing module finds the target Server thread corresponding to the interrupt event in the IPC processing table, the interrupt processing module in the electronic device can configure the corresponding pending interrupt flag for the target Server thread. Thus, after the target Server thread is awakened or the current execution flow is interrupted, the corresponding interrupt event or IPC data can be processed in a timely manner. And after the callback function is injected into the target Server thread, the interrupt injection module of the electronic device can delete the pending interrupt flag of the target Server thread.

[0144] In some embodiments, when an interrupt event is triggered and the interrupt processing module finds the target Server thread corresponding to the interrupt event in the IPC processing table, the interrupt processing module in the electronic device can also detect the current running state of the target Server thread. If the target Server thread is currently running an execution flow, the interrupt controller can interrupt the execution flow of the target Server thread, and the interrupt injection module can inject a callback function into the target Server thread, so that the target Server thread can execute the callback function in the user state, and read the IPC data to be processed from the preset memory block through the callback function.

[0145] If the target Server thread is not currently running, the interrupt processing module can actively wake up the target Server thread. When the target Server thread is transferred to the corresponding target CPU by the interrupt processing module, the interrupt injection module can inject a callback function into the target Server thread, so that the target Server thread can execute the callback function in user mode and read the IPC data to be processed from the preset memory block through the callback function.

[0146] It can be seen that no matter whether the called target Server thread is currently in a running state or a non-running state, after the interrupt event is triggered, the callback function will be injected into the target Server thread and the callback function will be executed, thereby achieving the purpose of quickly processing the interrupt event.

[0147] The target Server thread is in user state when executing the callback function, so that the target Server thread can process the pending IPC data in user state.

[0148] In some embodiments, when the target Server thread obtains the data to be processed in the user state, it can read the data to be processed stored by the Client thread from the above-mentioned preset memory block through the callback function. In this way, in the method of using shared memory to store the data to be processed, the Client thread can access the shared memory to store the data, and the target Server thread can access the shared memory to obtain the data, thereby achieving the purpose of data processing through zero copy data, which can be applicable to the scenario of large data transmission and processing.

[0149] The above-mentioned inter-thread communication method of the embodiment of the present application can enable the electronic device to process IPC data based on hardware interrupts and map the hardware address of the interrupt controller to the user state address. Whenever the Client thread has IPC data to be processed, the Client thread can directly trigger an interrupt event in the user state. When the Server thread processes the interrupt event, it will directly obtain and process the data to be processed in the user state without processing it in the kernel state, thereby realizing inter-thread communication without relying on the kernel, and reducing the delay of inter-thread communication.

[0150] In the aforementioned embodiments, since the interrupt registration module, the interrupt routing module, the interrupt processing module, the interrupt injection module, the kernel, the client thread, the service thread, etc. are all included in the electronic device, the processing process performed by the interrupt registration module, the interrupt routing module, the interrupt processing module, the interrupt injection module, the kernel, the client thread, the service thread, etc. can also be regarded as the processing process of the electronic device.

[0151] The following describes the method of inter-thread communication in an embodiment of the present application by taking the communication between a user-mode working thread and a user-mode network protocol stack as an example.

[0152] Currently, many applications that are sensitive to network performance use the user-mode network protocol stack (Lwip) to bypass the kernel protocol stack and improve their own network performance. Fig.11 , Lwip protocol stack can create several protocol stack threads, such as protocol stack thread 1, protocol stack thread 2, protocol stack thread 3, etc. Each protocol stack thread can read and write the hardware queue of the underlying network card through memory, such as hardware queue 1, hardware queue 2, hardware queue 3, etc. The network application contains several worker threads, each of which will create a network connection. The uplink and downlink data of these network connections must be routed to the protocol stack thread for processing. For performance reasons, the protocol stack thread will always listen to the hardware queue to process data. When the data of a network connection is processed by the protocol stack thread and sent to the worker thread for processing, the protocol stack thread will notify the user-state routing thread to let the user-state routing thread wake up the worker thread to process the network data.

[0153] Because there are a lot of IPC communications between the protocol stack thread, the user-mode routing thread and the working thread. The traditional IPC mechanism will consume a lot of time in the kernel. In order to improve the IPC communication efficiency between the working thread and the protocol stack thread, in the embodiment of the present application, the interrupt event can be used to accelerate the IPC efficiency between the protocol stack thread and the working thread, and the interrupt controller can be used to replace the user-mode routing thread, thereby eliminating the IPC notification delay between the protocol stack thread and the routing thread, and the routing thread and the working thread.

[0154] The above-mentioned protocol stack thread may include two processes: packet receiving and packet sending.

[0155] See also Fig.12 In the process of receiving packets by the protocol stack thread, the following steps S21-S27 may be included. The protocol stack thread may be regarded as the Client thread in the aforementioned embodiment, and the worker thread may be regarded as the Server thread in the aforementioned embodiment.

[0156] S21. The working thread registers an interrupt event for each network connection.

[0157] Whenever the network connection receives a message, the protocol stack thread notifies the worker thread to process the data corresponding to the message through an interrupt event. The data corresponding to the message can be regarded as the data to be processed in the above embodiment, or the IPC data to be processed.

[0158] S22, the protocol stack thread polls the network card hardware queue, reads the original message from it, and saves the to-be-processed data corresponding to the original message in the large page memory.

[0159] Among them, the large page memory is shared by the working thread and the protocol stack thread, and data zero copy can be achieved. The large page memory can also be regarded as the preset memory block or shared memory in the above embodiment.

[0160] S23, after the protocol stack thread processes the original message, it accesses the MMIO register file to trigger an interrupt, that is, the protocol stack thread controls the interrupt controller to generate an interrupt event by accessing the MMIO register file.

[0161] S24, the interrupt routing module of the electronic device determines the target CPU for running the working thread according to the CPU affinity of the interrupt event, wherein the working thread corresponding to the interrupt event is running on the target CPU.

[0162] S25. The interrupt controller generates an interrupt on the target CPU, interrupting the execution flow of the working thread.

[0163] S26. The interrupt injection module of the electronic device injects a callback function into the working thread.

[0164] S27. The working thread can read and process the data to be processed from the large page memory through the callback function in the user state.

[0165] In the above S25, if the working thread corresponding to the interrupt event is in an idle state without running the execution process, the interrupt processing module of the electronic device can also wake up the working thread, so that the working thread is migrated to the target CPU for execution. Afterwards, a callback function is injected into the working thread.

[0166] In the above S25, if the working thread corresponding to the interrupt event is not running on the target CPU, the interrupt controller will generate an interrupt event on the CPU running the working thread, and the interrupt controller can synchronously update the CPU affinity of the interrupt event, that is, associate or correspond the interrupt event with the current CPU.

[0167] In some embodiments, after the above steps S21-S27, the performance of the protocol stack packet receiving process that relies on interrupt events in the embodiments of the present application and the performance of the protocol stack packet receiving process that relies on the kernel commonly used can be seen in the following Tables 1 and 2:

[0168] Table 1

[0169]

[0170] Table 2

[0171]

[0172] It can be seen from the contents of Table 1 and Table 2 above that the throughput of the protocol stack packet receiving process that relies on interrupt events in the embodiment of the present application is greatly improved compared to the throughput of the protocol stack packet receiving process that is currently commonly used and relies on the kernel.

[0173] See also Fig.13 In the process of sending packets by the protocol stack thread, the following steps S31-S37 may be included, wherein the protocol stack thread may be regarded as the server thread in the aforementioned embodiment, and the working thread may be regarded as the client thread in the aforementioned embodiment.

[0174] S31. The protocol stack thread registers a hardware interrupt.

[0175] Whenever the working thread needs to send data via the network connection, the working thread will notify the protocol stack thread through an interrupt event. The data to be sent can be regarded as the data to be processed in the above embodiment, or the IPC data to be processed.

[0176] S32. The working thread puts the data to be sent into the large page memory.

[0177] Among them, the large page memory is shared by the working thread and the protocol stack thread, and data zero copy can be achieved. The large page memory can also be regarded as the preset memory block or shared memory in the above embodiment.

[0178] S33, the working thread accesses the MMIO register file to trigger an interrupt, that is, the working thread controls the interrupt controller to generate an interrupt event by accessing the MMIO register file.

[0179] S34, the interrupt routing module of the electronic device determines the target CPU running the protocol stack thread according to the CPU affinity of the interrupt event, wherein the protocol stack thread corresponding to the interrupt event is running on the target CPU.

[0180] S35. The interrupt controller generates an interrupt on the target CPU, interrupting the execution flow of the protocol stack thread.

[0181] S36. The interrupt injection module of the electronic device injects a callback function into the protocol stack thread.

[0182] S37. The protocol stack thread can read and process the data to be sent from the large page memory through the callback function in the user state.

[0183] In the above S35, if the protocol stack thread corresponding to the interrupt event is in an idle state without running the execution process, the interrupt processing module of the electronic device can also wake up the protocol stack thread, so that the protocol stack thread is migrated to the target CPU for execution. Afterwards, a callback function is injected into the protocol stack thread.

[0184] In the above S35, if the protocol stack thread corresponding to the interrupt event is not running on the target CPU, the interrupt controller will generate an interrupt event on the CPU running the protocol stack thread, and the interrupt controller can synchronously update the CPU affinity of the interrupt event, that is, associate or correspond the interrupt event with the current CPU.

[0185] In some embodiments, after the above steps S31-S37, the performance of the protocol stack packet sending process that relies on interrupt events in the embodiments of the present application and the performance of the protocol stack packet sending process that relies on the kernel commonly used can be seen in the following Tables 3 and 4:

[0186] Table 3

[0187]

[0188] Table 4

[0189]

[0190] It can be seen from the contents of Tables 3 and 4 above that the throughput of the protocol stack packet sending process that relies on interrupt events in the embodiment of the present application is greatly improved compared to the throughput of the protocol stack packet sending process that currently relies on the kernel, and the delay of the protocol stack packet sending process that relies on interrupt events in the embodiment of the present application is also greatly improved compared to the delay of the protocol stack packet sending process that currently relies on the kernel.

[0191] From the above content, it can be seen that in the inter-thread communication method that relies on hardware interrupts in the embodiment of the present application, whenever the Client thread has IPC data to process (such as the above-mentioned packet receiving, packet sending, etc.), the Client thread can directly trigger an interrupt event in the user state. When the Server thread handles the interrupt event, it will directly obtain the data to be processed in the user state for processing without processing in the kernel state, thereby realizing inter-thread communication without relying on the kernel, and can reduce the delay of inter-thread communication.

[0192] In addition, the interrupt controller can directly determine the CPU running the server thread through the CPU affinity corresponding to the interrupt event, thereby generating an interrupt on the CPU. After that, the server thread running on the CPU can start processing the data to be processed until it is completed. This method can improve the delay problem caused by the IPC routing process.

[0193] In addition, the target Server thread can directly read the IPC data to be processed from the preset memory block without copying the IPC data, thereby achieving the purpose of zero copy of data.

[0194] In the above embodiment, the method of inter-thread communication in the embodiment of the present application is exemplified by taking the user-mode working thread and the user-mode network protocol stack processing network data as an example. In some other embodiments, the method of inter-thread communication in the embodiment of the present application can also be applied to other data processing scenarios, such as processing data in the field of artificial intelligence. The specific processing process can refer to the content of the above embodiment, and can also achieve the effect of the above embodiment, which will not be repeated here.

[0195] It is understandable that in order to implement the above functions, the electronic device includes hardware and / or software modules corresponding to the execution of each function. In combination with the algorithm steps of each example described in the embodiments disclosed herein, the present application can be implemented in the form of hardware or a combination of hardware and computer software. Whether a function is executed in the form of hardware or computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application in combination with the embodiments, but such implementation should not be considered to be beyond the scope of the present application.

[0196] In this embodiment, the electronic device can be divided into functional modules according to the above method example. For example, each functional module can be divided according to each function, or two or more functions can be integrated into one processing module. The above integrated module can be implemented in the form of hardware. It should be noted that the division of modules in this embodiment is schematic and is only a logical function division. There may be other division methods in actual implementation.

[0197] The present application also provides an electronic device, such as Fig.14 As shown, the electronic device may include one or more processors 1001 , a memory 1002 and a communication interface 1003 .

[0198] The memory 1002 and the communication interface 1003 are coupled to the processor 1001. For example, the memory 1002, the communication interface 1003 and the processor 1001 may be coupled together via a bus 1004.

[0199] The communication interface 1003 is used to transmit data with other devices. The memory 1002 stores computer program code. The computer program code includes computer instructions. When the computer instructions are executed by the processor 1001, the electronic device executes the method for inter-thread communication in the embodiment of the present application.

[0200] Among them, the processor 1001 can be a processor or a controller, for example, a central processing unit (CPU), a general processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, transistor logic devices, hardware components or any combination thereof. It can implement or execute various exemplary logic blocks, modules and circuits described in conjunction with the present disclosure. The processor can also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, and the like.

[0201] The bus 1004 may be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. The bus 1004 may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Fig.14 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0202] An embodiment of the present application further provides a computer-readable storage medium, in which a computer program code is stored. When the processor executes the computer program code, the electronic device executes the relevant method steps in the method embodiment.

[0203] The embodiment of the present application also provides a computer program product. When the computer program product is run on a computer, it enables the computer to execute the relevant method steps in the above method embodiment.

[0204] Among them, the electronic device, computer storage medium or computer program product provided in this application is used to execute the corresponding method provided above. Therefore, the beneficial effects that can be achieved can refer to the beneficial effects in the corresponding method provided above, and will not be repeated here.

[0205] Through the description of the above implementation methods, technical personnel in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional modules is used as an example. In actual applications, the above-mentioned functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above.

[0206] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the modules or units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another device, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0207] The units described as separate components may or may not be physically separated, and the components shown as units may be one physical unit or multiple physical units, that is, they may be located in one place or distributed in multiple different places. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.

[0208] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0209] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application is essentially or the part that contributes or all or part of the technical solution can be embodied in the form of a software product, which is stored in a storage medium, including several instructions to enable a device (which can be a single-chip microcomputer, chip, etc.) or a processor (processor) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read only memory (ROM), random access memory (RAM), disk or optical disk and other media that can store program code.

[0210] The above contents are only specific implementation methods of the present application, but the protection scope of the present application is not limited thereto. Any changes or substitutions within the technical scope disclosed in the present application shall be included in the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the protection scope of the claims.

Claims

1. A method for inter-thread communication, characterized in that: Applied to an electronic device, the electronic device includes an interrupt controller, a client thread and a service thread; an inter-process communication calling interface exists between the interrupt controller and the client thread, and the hardware interrupt number of the registered interrupt event and the corresponding thread number are stored in the inter-process communication processing information, the method includes: In response to a data processing request initiated by the client thread, in a user state, based on a user state address corresponding to the client thread and a mapping relationship between the user state address and a hardware address of the interrupt controller, the hardware address of the interrupt controller is accessed through the inter-process communication calling interface, thereby triggering the interrupt controller to generate an interrupt event; In response to the interrupt event, searching for a corresponding thread number in the inter-process communication processing information according to the hardware interrupt number of the interrupt event, and determining a service thread corresponding to the found thread number as a target service thread; The target service thread corresponding to the interrupt event obtains the data to be processed in the user state, and processes the data to be processed.

2. The method according to claim 1, characterized in that: The method further comprises: The client thread stores the data to be processed in a shared memory between the client thread and the service thread.

3. The method according to any one of claims 1 to 2, characterized in that: The electronic device stores an association relationship between the interrupt event and its corresponding target processor; the method further includes: If the target service thread is awakened in response to the interrupt event, the target service thread runs on the target processor corresponding to the interrupt event.

4. The method according to claim 3, characterized in that The method further comprises: If the target service thread is not currently running on the target processor when the interrupt event is triggered, the association relationship between the interrupt event and the target processor is updated; in the updated association relationship, the target processor represents the processor currently running the target service thread.

5. The method according to claim 2, characterized in that: The target service thread corresponding to the interrupt event obtains the data to be processed in the user state, including: The target service thread is injected with a callback function corresponding to the interrupt event in kernel state; The target service thread obtains the to-be-processed data from the shared memory by executing the callback function in the user state.

6. The method according to claim 5, characterized in that After the client thread saves the data to be processed, the stack frame pointer corresponding to the callback function points to the address of the data to be processed in the shared memory.

7. The method according to claim 5, characterized in that The target service thread is injected with a callback function corresponding to the interrupt event in the kernel state, including: If the target service thread is currently running when the interrupt event is triggered, the target service thread is interrupted and the callback function is injected into the kernel state; or, If the target service thread is not currently running when the interrupt event is triggered, the target service thread is awakened and the callback function is injected into the kernel state.

8. The method according to any one of claims 1-2, characterized in that: Before the client thread initiates a data processing request, the method further includes: The multiple service threads register corresponding interrupt events respectively; after registration, there is a mapping relationship between the hardware interrupt number of the interrupt event and the thread number of the service thread that registers the interrupt event, and the mapping relationship between the hardware interrupt number and the thread number is saved in the inter-process communication processing information.

9. The method according to claim 8, characterized in that After registration, the interrupt event is configured with a corresponding callback function, and the hardware interrupt number of the interrupt event is configured with a corresponding virtual interrupt number; the hardware interrupt number of the interrupt event is associated with the callback function, and the callback function obtains the data to be processed through the virtual interrupt number.

10. The method according to claim 5, characterized in that The target service thread is injected with a callback function corresponding to the interrupt event in the kernel state, including: If the target service thread has a pending interrupt flag, a callback function corresponding to the interrupt event is injected into the target service thread in kernel mode; the pending interrupt flag indicates that the target service thread has a pending interrupt event.

11. The method according to claim 10, characterized in that The method further comprises: After the callback function is injected into the target service thread, the pending interruption mark is deleted.

12. The method according to any one of claims 1-2, characterized in that: After the interrupt event is triggered, an anti-nesting flag is set on the target service thread; the anti-nesting flag indicates that the target service thread is prohibited from being injected with callback functions corresponding to other interrupt events when processing the interrupt event.

13. The method according to claim 12, characterized in that The method further comprises: After the target service thread finishes processing the data to be processed, the anti-nesting mark is deleted.

14. The method according to claim 5, characterized in that The method further comprises: After the target service thread finishes executing the callback function, the callback function is uninstalled.

15. An electronic device, characterized in that: The electronic device includes a memory and one or more processors; the memory is coupled to the processor; wherein computer program code is stored in the memory, and the computer program code includes computer instructions, and when the computer instructions are executed by the processor, the electronic device executes the method for inter-thread communication as described in any one of claims 1-14.

16. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores instructions, and when the instructions are executed on a computer, the computer executes the method for inter-thread communication according to any one of claims 1 to 14.