Thread scheduling method, electronic equipment and computer readable storage medium

By adjusting the scheduling attributes of low-priority threads in electronic devices when high-priority threads request target read and write locks in write mode in electronic devices, the problem of high-priority thread blocking caused by long scheduling time of low-priority threads is solved, and the problem of high-priority thread blocking is improved. User experience and device performance.

CN119493639APending Publication Date: 2025-02-21HUAWEI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311026748.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-08-14
Publication Date
2025-02-21

AI Technical Summary

Technical Problem

In the prior art, the scheduling time of low-priority threads is long, resulting in long-term blockage of high-priority threads, causing electronic equipment to stutter and affecting user experience.

Method used

When a high priority thread requests a target read and write lock in a write mode, determine the scheduling attribute of the thread that already holds the read mode and set it to the scheduling attribute of the high priority thread to improve the scheduling of the low priority thread Speed, quickly release the read and write lock.

Benefits of technology

Reduces the waiting time of high-priority threads, avoids long-term blockage, improves user experience, and optimizes the performance of electronic devices.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119493639A_ABST
    Figure CN119493639A_ABST
Patent Text Reader

Abstract

The invention is suitable for the technical field of terminals, and particularly relates to a thread scheduling method, electronic equipment and a computer readable storage medium. According to the method, when it is detected that a first thread requests a target read-write lock in a write mode, a second thread is determined, the second thread is a thread with the target read-write lock in a read mode, and the scheduling attribute of the first thread and the scheduling attribute of the second thread are determined. When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, the scheduling attribute of the second thread is set as the scheduling attribute of the first thread, namely, the scheduling attribute of the second thread is improved, so that the second thread can obtain more CPU resources and the like for scheduling, the scheduling speed of the second thread is improved, and the scheduling efficiency of the second thread is improved. Therefore, the second thread can quickly release the target read-write lock, the waiting time for the first thread to obtain the target read-write lock is shortened, the problems that the first thread is blocked for a long time, the electronic equipment is blocked and the like are solved, and the user experience is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application belongs to the field of terminal technology, and in particular, relates to a thread scheduling method, an electronic device, and a computer-readable storage medium. Background Art

[0002] At present, electronic devices such as mobile phones have become indispensable communication tools for people's daily life and work. In order to meet people's needs, more and more applications are installed in electronic devices, resulting in more and more threads in the use of electronic devices, and multiple threads sharing the same resource is becoming more and more common. When multiple threads share the same resource, if multiple threads access the shared resource at the same time, data errors are likely to occur. In order to avoid data errors caused by multiple threads accessing shared resources at the same time, a priority is generally set for each thread so that each thread accesses the shared resource in order from high to low priority.

[0003] However, when a high-priority thread requests to access a shared resource, if a low-priority thread already holds the shared resource, the high-priority thread needs to wait for the low-priority thread to complete scheduling and release the shared resource before it can access the shared resource. Due to its low priority, the low-priority thread may not be able to obtain sufficient central processing unit (CPU) resources in time for scheduling, resulting in a long scheduling time for the low-priority thread, causing the high-priority thread to be blocked for a long time, which can cause problems such as freezing of the electronic device and affect the user experience. Summary of the invention

[0004] The embodiments of the present application provide a thread scheduling method, an electronic device, and a computer-readable storage medium, which can solve the problem in the prior art that the scheduling time of low-priority threads is long, causing long-term blocking of high-priority threads, thereby causing problems such as freezes in electronic devices and affecting user experience.

[0005] In a first aspect, an embodiment of the present application provides a thread scheduling method, the method comprising:

[0006] When it is detected that the first thread requests a target read-write lock in write mode, determining a second thread, where the second thread is a thread that already holds the target read-write lock in read mode;

[0007] Determining a scheduling attribute of the first thread and a scheduling attribute of the second thread;

[0008] When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, the scheduling attribute of the second thread is set to the scheduling attribute of the first thread, so that the second thread schedules the target read-write lock according to the scheduling attribute of the first thread.

[0009] In the thread scheduling method provided above, when it is detected that the first thread requests the target read-write lock in write mode, the electronic device can determine the second thread, which is the thread that already holds the target read-write lock in read mode, and determine the scheduling attributes of the first thread and the scheduling attributes of the second thread. When the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread, the electronic device can set the scheduling attributes of the second thread to the scheduling attributes of the first thread, so that the second thread can be scheduled according to the scheduling attributes of the first thread. That is, when the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread, the electronic device can improve the scheduling attributes of the second thread, so that the second thread can obtain more CPU resources for scheduling, and improve the scheduling speed of the second thread, so that the second thread can quickly release the target read-write lock, reduce the waiting time for the first thread to obtain the target read-write lock, so as to avoid blocking the first thread for a long time, causing the electronic device to freeze and other problems, which can improve the user experience.

[0010] In a possible implementation, the scheduling attribute includes a priority;

[0011] When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, setting the scheduling attribute of the second thread to the scheduling attribute of the first thread includes:

[0012] When the priority of the first thread is higher than the priority of the second thread, the priority of the second thread is set to the priority of the first thread.

[0013] In the thread scheduling method provided by the implementation, when the scheduling attribute includes priority, when the first thread requests a target read-write lock in write mode, if a second thread already holds the target read-write lock in read mode, the electronic device can determine the second thread, and determine the priority of the first thread and the priority of the second thread. When the priority of the first thread is higher than the priority of the second thread, the electronic device can set the priority of the second thread to the priority of the first thread, that is, raise the priority of the second thread to the priority of the first thread, so that the second thread can obtain more CPU resources and / or time slices in time, etc., improve the scheduling speed of the second thread, so that the second thread can quickly release the target read-write lock, so that the first thread can quickly obtain the target read-write lock, reduce the blocking time of the first thread, and improve the user experience.

[0014] In another possible implementation, the scheduling attribute includes load;

[0015] When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, setting the scheduling attribute of the second thread to the scheduling attribute of the first thread includes:

[0016] When the load of the first thread is higher than the load of the second thread, the load of the second thread is set to the load of the first thread.

[0017] In the thread scheduling method provided by the implementation, when the scheduling attribute includes load, when the first thread requests a target read-write lock in write mode, if a second thread already holds the target read-write lock in read mode, the electronic device can determine the second thread, and can determine the load of the first thread and the load of the second thread. When the load of the first thread is higher than the load of the second thread, the electronic device can set the load of the second thread to the load of the first thread, that is, adjust the load of the second thread to the load of the first thread to increase the load of the second thread, so that the second thread can obtain more CPU resources and / or CPU resources with better performance in time, improve the scheduling speed of the second thread, so that the second thread can quickly release the target read-write lock, so that the first thread can quickly obtain the target read-write lock, reduce the blocking time of the first thread, and improve the user experience.

[0018] In another possible implementation, the scheduling attribute includes quality of service QoS;

[0019] When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, setting the scheduling attribute of the second thread to the scheduling attribute of the first thread includes:

[0020] When the QoS of the first thread is higher than the QoS of the second thread, the QoS of the second thread is set to the QoS of the first thread.

[0021] In the thread scheduling method provided by the implementation, when the scheduling attribute includes QoS, when the first thread requests a target read-write lock in write mode, if a second thread already holds the target read-write lock in read mode, the electronic device can determine the second thread, and can determine the QoS of the first thread and the QoS of the second thread. When the QoS of the first thread is higher than the QoS of the second thread, the electronic device can set the QoS of the second thread to the QoS of the first thread, that is, to increase the QoS of the second thread to the QoS of the first thread, so as to improve the service capability of the second thread, increase the scheduling speed of the second thread, so that the second thread can quickly release the target read-write lock, so that the first thread can quickly obtain the target read-write lock, reduce the blocking time of the first thread, and improve the user experience.

[0022] Exemplarily, when the scheduling attributes include at least two of priority, load, and QoS, the electronic device can determine whether the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread based on at least one of the scheduling attributes, thereby determining whether the scheduling attributes of the second thread need to be adjusted.

[0023] Exemplarily, before determining the second thread, the method further includes:

[0024] The second thread that obtains the target read-write lock in read mode is recorded.

[0025] In the thread scheduling method provided in this example, the electronic device can record each second thread that obtains the target read-write lock, for example, the second thread that obtains the target read-write lock can be recorded in the owner of the rwsem structure. When the first thread requests the target read-write lock, the electronic device can accurately determine the second thread based on the record in the owner.

[0026] It should be understood that since multiple read threads can hold the same read-write lock at the same time, and the number of read-write locks in electronic devices is relatively large, if the threads that obtain the read-write locks in read mode are recorded for all read-write locks, it will not only increase the system's memory overhead, but also cause the system's performance to degrade.

[0027] Therefore, in order to reduce the system's memory overhead and reduce the impact on system performance, the electronic device can only record the read thread that obtains the target read-write lock in the owner for the target read-write lock, so as to optimize the thread scheduling for obtaining the target read-write lock. That is, when a thread (such as a read thread or a write thread) obtains the target read-write lock, the thread can be recorded in the owner of the rwsem structure corresponding to the target read-write lock.

[0028] Optionally, the target read-write lock includes mmap-lock.

[0029] In the thread scheduling method provided by this optional manner, the mmap-lock that is prone to priority inversion and affects the performance of the electronic device can be determined as the target read-write lock according to the actual scenario, so as to adjust the scheduling properties of the thread that schedules the mmap-lock to optimize the thread scheduling, improve the performance of the electronic device, and enhance the user experience.

[0030] In a second aspect, an embodiment of the present application provides a thread scheduling method, the method comprising:

[0031] When detecting that the first thread requests the target variable, determining a second thread, the second thread being a thread that already holds the target variable;

[0032] Determining a scheduling attribute of the first thread and a scheduling attribute of the second thread;

[0033] When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, the scheduling attribute of the second thread is set to the scheduling attribute of the first thread, so that the second thread schedules the target variable according to the scheduling attribute of the first thread.

[0034] In the thread scheduling method provided above, when it is detected that the first thread requests the target variable, the electronic device can determine the second thread, which is the thread that already holds the target variable, and determine the scheduling attributes of the first thread and the scheduling attributes of the second thread. When the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread, the electronic device can set the scheduling attributes of the second thread to the scheduling attributes of the first thread, so that the second thread can schedule the target variable according to the scheduling attributes of the first thread. That is, when the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread, the electronic device can improve the scheduling attributes of the second thread, so that the second thread can obtain more CPU resources in time to call the target variable, improve the scheduling speed of the second thread, so that the second thread can quickly release the target variable, reduce the waiting time for the first thread to obtain the target variable, avoid blocking the first thread for a long time, causing the electronic device to freeze and other problems, and improve the user experience.

[0035] In a possible implementation, the scheduling attribute includes a priority;

[0036] When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, setting the scheduling attribute of the second thread to the scheduling attribute of the first thread includes:

[0037] When the priority of the first thread is higher than the priority of the second thread, the priority of the second thread is set to the priority of the first thread.

[0038] In another possible implementation, the scheduling attribute includes load;

[0039] When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, setting the scheduling attribute of the second thread to the scheduling attribute of the first thread includes:

[0040] When the load of the first thread is higher than the load of the second thread, the load of the second thread is set to the load of the first thread.

[0041] In another possible implementation, the scheduling attribute includes quality of service QoS;

[0042] When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, setting the scheduling attribute of the second thread to the scheduling attribute of the first thread includes:

[0043] When the QoS of the first thread is higher than the QoS of the second thread, the QoS of the second thread is set to the QoS of the first thread.

[0044] Exemplarily, before determining the second thread, the method further includes:

[0045] The second thread that obtains the target variable is recorded.

[0046] Optionally, the target variable includes a wait bit variable.

[0047] In a third aspect, an embodiment of the present application provides a thread scheduling device, the device comprising:

[0048] A thread determination module, configured to determine a second thread when detecting that a first thread requests a target read-write lock in a write mode, wherein the second thread is a thread that already holds the target read-write lock in a read mode;

[0049] a scheduling attribute determination module, configured to determine a scheduling attribute of the first thread and a scheduling attribute of the second thread;

[0050] A scheduling attribute setting module is used to set the scheduling attribute of the second thread to the scheduling attribute of the first thread when the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, so that the second thread schedules the target read-write lock according to the scheduling attribute of the first thread.

[0051] In a possible implementation, the scheduling attribute includes a priority;

[0052] The scheduling attribute setting module is specifically configured to set the priority of the second thread to the priority of the first thread when the priority of the first thread is higher than the priority of the second thread.

[0053] In another possible implementation, the scheduling attribute includes load;

[0054] The scheduling attribute setting module is further configured to set the load of the second thread to be the load of the first thread when the load of the first thread is higher than the load of the second thread.

[0055] In another possible implementation, the scheduling attribute includes quality of service QoS;

[0056] The scheduling attribute setting module is further configured to set the QoS of the second thread to the QoS of the first thread when the QoS of the first thread is higher than the QoS of the second thread.

[0057] Exemplarily, the device further comprises:

[0058] The thread recording module is used to record the second thread that obtains the target read-write lock in read mode.

[0059] Optionally, the target read-write lock includes mmap-lock.

[0060] In a fourth aspect, an embodiment of the present application provides a thread scheduling device, the device comprising:

[0061] A thread determination module, configured to determine a second thread when detecting that a first thread requests a target variable, the second thread being a thread that already holds the target variable;

[0062] a scheduling attribute determination module, configured to determine a scheduling attribute of the first thread and a scheduling attribute of the second thread;

[0063] A scheduling attribute setting module is used to set the scheduling attribute of the second thread to the scheduling attribute of the first thread when the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, so that the second thread schedules the target variable according to the scheduling attribute of the first thread.

[0064] In a possible implementation, the scheduling attribute includes a priority;

[0065] The scheduling attribute setting module is specifically configured to set the priority of the second thread to the priority of the first thread when the priority of the first thread is higher than the priority of the second thread.

[0066] In another possible implementation, the scheduling attribute includes load;

[0067] The scheduling attribute setting module is specifically configured to set the load of the second thread to be the load of the first thread when the load of the first thread is higher than the load of the second thread.

[0068] In another possible implementation, the scheduling attribute includes quality of service QoS;

[0069] The scheduling attribute setting module is specifically configured to set the QoS of the second thread to the QoS of the first thread when the QoS of the first thread is higher than the QoS of the second thread.

[0070] Exemplarily, the device further includes: a thread recording module, configured to record a second thread that obtains the target variable.

[0071] Optionally, the target variable includes a wait bit variable.

[0072] In a fifth aspect, an embodiment of the present application provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, the electronic device implements the thread scheduling method described in any one of the first aspect or the second aspect.

[0073] In a sixth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program. When the computer program is executed by a computer, the computer implements the thread scheduling method described in any one of the first aspect or the second aspect.

[0074] In a seventh aspect, an embodiment of the present application provides a computer program product. When the computer program product runs on an electronic device, the electronic device executes the thread scheduling method described in any one of the first aspect or the second aspect.

[0075] It can be understood that the beneficial effects of the third to seventh aspects mentioned above can be found in the relevant descriptions in the first or second aspect mentioned above, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS

[0076] Figure 1 is a structural diagram of an electronic device to which the thread scheduling method provided by an embodiment of the present application is applicable;

[0077] Figure 2 It is a schematic diagram of a software architecture to which the thread scheduling method provided in an embodiment of the present application is applicable;

[0078] Figure 3 This is an example diagram of a priority inversion;

[0079] Figure 4 This is an example diagram of solving priority inversion through priority ceiling;

[0080] Figure 5 It is a flowchart of the thread scheduling method provided in Example 1 of the present application;

[0081] Figure 6 This is a structural example diagram of a read-write lock provided in Example 1 of the present application;

[0082] Figure 7 It is a flowchart of the thread scheduling method provided in Example 2 of the present application. DETAILED DESCRIPTION

[0083] It should be understood that when used in the present specification and the appended claims, the term "comprising" indicates the presence of described features, wholes, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or combinations thereof.

[0084] It should also be understood that the term “and / or” used in the specification and appended claims refers to any and all possible combinations of one or more of the associated listed items, and includes these combinations.

[0085] As used in the specification and appended claims of this application, the term "if" can be interpreted as "when" or "uponce" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrase "if it is determined" or "if [described condition or event] is detected" can be interpreted as meaning "uponce it is determined" or "in response to determining" or "uponce [described condition or event] is detected" or "in response to detecting [described condition or event]", depending on the context.

[0086] In addition, in the description of the present application specification and the appended claims, the terms "first", "second", "third", etc. are only used to distinguish the descriptions and cannot be understood as indicating or implying relative importance.

[0087] References to "one embodiment" or "some embodiments" etc. described in the specification of this application mean that one or more embodiments of the present application include specific features, structures or characteristics described in conjunction with the embodiment. Therefore, the statements "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. that appear in different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized in other ways.

[0088] In addition, the “plurality” mentioned in the embodiments of the present application should be interpreted as two or more.

[0089] The steps involved in the thread scheduling method provided in the embodiment of the present application are only examples. Not all steps are required to be executed, or not all information or messages are required. They can be increased or decreased as needed during use. The same step or steps or messages with the same function in different embodiments of the present application can be referenced and learned from each other.

[0090] 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 will appreciate that, with the evolution of network architecture and the emergence of new business scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.

[0091] To facilitate understanding, the key technical terms that may be involved in the embodiments of the present application are first introduced below.

[0092] A task is an activity completed by software, that is, an operation to achieve a certain purpose. A task can be either a process or a thread.

[0093] A process is the execution of a program. A process can be considered as an independent program. A process has a complete data space and code space in the memory. The data and variables owned by a process belong only to it.

[0094] A process is the basic unit of resource allocation and the basic unit of scheduling and operation. For example, when a user runs a program, the system creates a process and allocates resources to the process (such as tables, memory space, disk space, input / output (I / O) devices, etc.). Then, the process is placed in the ready queue of the process. When the process is selected by the scheduler, the system allocates CPU resources to the process, so that the process can actually run.

[0095] A thread is a program that runs independently in a process. In other words, a thread exists in a process. A thread is the basic execution unit of a process. All tasks of a process are executed in a thread. A process includes one or more threads. Each thread shares the address space (such as code, global data or variables) and resources (such as memory resources, I / O resources, CPU resources, etc.) of the process, but each thread has its own stack and local variables. In other words, the global variables of a process are shared by all threads, and the resources allocated to the process by the system are available to all threads.

[0096] A thread is the smallest unit of computing in a process. If a process is understood as a task that is logically completed by the operating system, then a thread represents one of many possible subtasks that complete the task. Threads can be scheduled and run independently on a processor. In the case of multiple processors, multiple threads of a process can run on different processors.

[0097] A timeslice is the time allocated to a thread to run on the CPU. After a thread's timeslice is executed, it will switch to another thread. When there is only one CPU, the CPU can only run one task at the same time, and cannot run multiple tasks at the same time. When there are multiple tasks, the timeslice can be used to implement the interleaving of various processes or programs, so that each process or program appears to be running simultaneously. Timeslices can usually be allocated to each thread by the scheduler of the operating system kernel. Usually, the length of the timeslice allocated to all threads in a system is not equal. Generally, in order to obtain a faster response speed, threads with strong interactivity are allocated longer timeslices than threads with weak interactivity. For example, the interactivity of a thread with a high priority is generally stronger than that of a thread with a low priority. Therefore, the timeslice allocated to a thread with a high priority is longer than that to a thread with a low priority.

[0098] In the embodiments of the present application, the electronic device may be a mobile phone, a tablet computer, a wearable device, a vehicle-mounted device, an augmented reality (AR) / virtual reality (VR) device, a laptop computer, an ultra-mobile personal computer (UMPC), a netbook, a personal digital assistant (PDA), a desktop computer, etc. The embodiments of the present application do not impose any restrictions on the specific type of the electronic device.

[0099] The following first introduces the electronic device involved in the embodiment of the present application. Figure 1 , Figure 1 A schematic structural diagram of an electronic device 100 is shown.

[0100] The electronic device 100 may include a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, an earphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195, etc. The sensor module 180 may include a pressure sensor 180A, a gyroscope sensor 180B, an air pressure sensor 180C, a magnetic sensor 180D, an acceleration sensor 180E, a distance sensor 180F, a proximity light sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, an ambient light sensor 180L, a bone conduction sensor 180M, etc.

[0101] It is to be understood that the structure illustrated in the embodiment of the present application does not constitute a specific limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may include more or fewer components than shown in the figure, or combine some components, or split some components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.

[0102] The processor 110 may include one or more processing units, for example, the processor 110 may include an application processor (AP), a modem processor, a graphics processor (GPU), an image signal processor (ISP), a controller, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU), etc. Different processing units may be independent devices or integrated into one or more processors.

[0103] The controller can generate operation control signals according to the instruction operation code and timing signal to complete the control of instruction fetching and execution.

[0104] The processor 110 may also be provided with a memory for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. The memory may store instructions or data that the processor 110 has just used or cyclically used. If the processor 110 needs to use the instruction or data again, it may be directly called from the memory. This avoids repeated access, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.

[0105] In some embodiments, the processor 110 may include one or more interfaces. The interface may include an inter-integrated circuit (I2C) interface, an inter-integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc.

[0106] The I2C interface is a bidirectional synchronous serial bus, including a serial data line (SDA) and a serial clock line (SCL). In some embodiments, the processor 110 may include multiple groups of I2C buses. The processor 110 may be coupled to the touch sensor 180K, the charger, the flash, the camera 193, etc. through different I2C bus interfaces. For example: the processor 110 may be coupled to the touch sensor 180K through the I2C interface, so that the processor 110 communicates with the touch sensor 180K through the I2C bus interface, thereby realizing the touch function of the electronic device 100.

[0107] The I2S interface can be used for audio communication. In some embodiments, the processor 110 can include multiple I2S buses. The processor 110 can be coupled to the audio module 170 via the I2S bus to achieve communication between the processor 110 and the audio module 170. In some embodiments, the audio module 170 can transmit an audio signal to the wireless communication module 160 via the I2S interface to achieve the function of answering a call through a Bluetooth headset.

[0108] The PCM interface can also be used for audio communication, sampling, quantizing and encoding analog signals. In some embodiments, the audio module 170 and the wireless communication module 160 can be coupled via a PCM bus interface. In some embodiments, the audio module 170 can also transmit audio signals to the wireless communication module 160 via the PCM interface to realize the function of answering calls via a Bluetooth headset. Both the I2S interface and the PCM interface can be used for audio communication.

[0109] The UART interface is a universal serial data bus for asynchronous communication. The bus can be a bidirectional communication bus. It converts the data to be transmitted between serial communication and parallel communication. In some embodiments, the UART interface is generally used to connect the processor 110 and the wireless communication module 160. For example, the processor 110 communicates with the Bluetooth module in the wireless communication module 160 through the UART interface to implement the Bluetooth function. In some embodiments, the audio module 170 can transmit an audio signal to the wireless communication module 160 through the UART interface to implement the function of playing music through a Bluetooth headset.

[0110] The MIPI interface can be used to connect the processor 110 with peripheral devices such as the display screen 194 and the camera 193. The MIPI interface includes a camera serial interface (CSI), a display serial interface (DSI), etc. In some embodiments, the processor 110 and the camera 193 communicate via the CSI interface to implement the shooting function of the electronic device 100. The processor 110 and the display screen 194 communicate via the DSI interface to implement the display function of the electronic device 100.

[0111] The GPIO interface can be configured by software. The GPIO interface can be configured as a control signal or as a data signal. In some embodiments, the GPIO interface can be used to connect the processor 110 with the camera 193, the display 194, the wireless communication module 160, the audio module 170, the sensor module 180, etc. The GPIO interface can also be configured as an I2C interface, an I2S interface, a UART interface, a MIPI interface, etc.

[0112] The USB interface 130 is an interface that complies with the USB standard specification, and specifically can be a Mini USB interface, a Micro USB interface, a USB Type C interface, etc. The USB interface 130 can be used to connect a charger to charge the electronic device 100, and can also be used to transfer data between the electronic device 100 and a peripheral device. It can also be used to connect headphones to play audio through the headphones. The interface can also be used to connect other electronic devices, such as AR devices, etc.

[0113] It is understandable that the interface connection relationship between the modules illustrated in the embodiment of the present application is only a schematic illustration and does not constitute a structural limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may also adopt different interface connection methods in the above embodiments, or a combination of multiple interface connection methods.

[0114] The charging management module 140 is used to receive charging input from a charger. The charger may be a wireless charger or a wired charger. In some wired charging embodiments, the charging management module 140 may receive charging input from a wired charger through the USB interface 130. In some wireless charging embodiments, the charging management module 140 may receive wireless charging input through a wireless charging coil of the electronic device 100. While the charging management module 140 is charging the battery 142, it may also power the electronic device through the power management module 141.

[0115] The power management module 141 is used to connect the battery 142, the charging management module 140 and the processor 110. The power management module 141 receives input from the battery 142 and / or the charging management module 140, and supplies power to the processor 110, the internal memory 121, the display screen 194, the camera 193, and the wireless communication module 160. The power management module 141 can also be used to monitor parameters such as battery capacity, battery cycle number, battery health status (leakage, impedance), etc. In some other embodiments, the power management module 141 can also be set in the processor 110. In other embodiments, the power management module 141 and the charging management module 140 can also be set in the same device.

[0116] The wireless communication function of the electronic device 100 can be implemented through the antenna 1, the antenna 2, the mobile communication module 150, the wireless communication module 160, the modem processor and the baseband processor.

[0117] Antenna 1 and antenna 2 are used to transmit and receive electromagnetic wave signals. Each antenna in electronic device 100 can be used to cover a single or multiple communication frequency bands. Different antennas can also be reused to improve the utilization of antennas. For example, antenna 1 can be reused as a diversity antenna for a wireless local area network. In some other embodiments, the antenna can be used in combination with a tuning switch.

[0118] The mobile communication module 150 can provide solutions for wireless communications including 2G / 3G / 4G / 5G, etc., applied to the electronic device 100. The mobile communication module 150 may include at least one filter, a switch, a power amplifier, a low noise amplifier (LNA), etc. The mobile communication module 150 can receive electromagnetic waves from the antenna 1, and filter, amplify, and process the received electromagnetic waves, and transmit them to the modulation and demodulation processor for demodulation. The mobile communication module 150 can also amplify the signal modulated by the modulation and demodulation processor, and convert it into electromagnetic waves for radiation through the antenna 1. In some embodiments, at least some of the functional modules of the mobile communication module 150 can be set in the processor 110. In some embodiments, at least some of the functional modules of the mobile communication module 150 can be set in the same device as at least some of the modules of the processor 110.

[0119] The modem processor may include a modulator and a demodulator. Among them, the modulator is used to modulate the low-frequency baseband signal to be sent into a medium-high frequency signal. The demodulator is used to demodulate the received electromagnetic wave signal into a low-frequency baseband signal. The demodulator then transmits the demodulated low-frequency baseband signal to the baseband processor for processing. After the low-frequency baseband signal is processed by the baseband processor, it is passed to the application processor. The application processor outputs a sound signal through an audio device (not limited to a speaker 170A, a receiver 170B, etc.), or displays an image or video through a display screen 194. In some embodiments, the modem processor may be an independent device. In other embodiments, the modem processor may be independent of the processor 110 and be set in the same device as the mobile communication module 150 or other functional modules.

[0120] The wireless communication module 160 can provide wireless communication solutions including wireless local area networks (WLAN) (such as wireless fidelity (Wi-Fi) networks), bluetooth (BT), global navigation satellite system (GNSS), frequency modulation (FM), near field communication (NFC), infrared (IR), etc., which are applied to the electronic device 100. The wireless communication module 160 can be one or more devices integrating at least one communication processing module. The wireless communication module 160 receives electromagnetic waves via the antenna 2, modulates the frequency of the electromagnetic wave signal and performs filtering, and sends the processed signal to the processor 110. The wireless communication module 160 can also receive the signal to be sent from the processor 110, modulate the frequency of it, amplify it, and convert it into electromagnetic waves for radiation through the antenna 2.

[0121] In some embodiments, the antenna 1 of the electronic device 100 is coupled to the mobile communication module 150, and the antenna 2 is coupled to the wireless communication module 160, so that the electronic device 100 can communicate with the network and other devices through wireless communication technology. The wireless communication technology may include global system for mobile communications (GSM), general packet radio service (GPRS), code division multiple access (CDMA), wideband code division multiple access (WCDMA), time-division code division multiple access (TD-SCDMA), long term evolution (LTE), BT, GNSS, WLAN, NFC, FM, and / or IR technology. The GNSS may include a global positioning system (GPS), a global navigation satellite system (GLONASS), a Beidou navigation satellite system (BDS), a quasi-zenith satellite system (QZSS) and / or a satellite based augmentation system (SBAS).

[0122] The electronic device 100 implements the display function through a GPU, a display screen 194, and an application processor. The GPU is a microprocessor for image processing, which connects the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 110 may include one or more GPUs that execute program instructions to generate or change display information.

[0123] The display screen 194 is used to display images, videos, etc. The display screen 194 includes a display panel. The display panel can be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light emitting diode or an active-matrix organic light emitting diode (AMOLED), a flexible light-emitting diode (FLED), Miniled, MicroLed, Micro-oLed, a quantum dot light-emitting diode (QLED), etc. In some embodiments, the electronic device 100 may include 1 or N display screens 194, where N is a positive integer greater than 1.

[0124] The electronic device 100 can realize the shooting function through ISP, camera 193, video codec, GPU, display screen 194 and application processor.

[0125] ISP is used to process the data fed back by camera 193. For example, when taking a photo, the shutter is opened, and the light is transmitted to the camera photosensitive element through the lens. The light signal is converted into an electrical signal, and the camera photosensitive element transmits the electrical signal to ISP for processing and converts it into an image visible to the naked eye. ISP can also perform algorithm optimization on the noise, brightness, and skin color of the image. ISP can also optimize the exposure, color temperature and other parameters of the shooting scene. In some embodiments, ISP can be set in camera 193.

[0126] The camera 193 is used to capture still images or videos. The object generates an optical image through the lens and projects it onto the photosensitive element. The photosensitive element can be a charge coupled device (CCD) or a complementary metal-oxide-semiconductor (CMOS) phototransistor. The photosensitive element converts the light signal into an electrical signal, and then passes the electrical signal to the ISP to be converted into a digital image signal. The ISP outputs the digital image signal to the DSP for processing. The DSP converts the digital image signal into an image signal in a standard RGB, YUV or other format. In some embodiments, the electronic device 100 may include 1 or N cameras 193, where N is a positive integer greater than 1.

[0127] The digital signal processor is used to process digital signals, and can process not only digital image signals but also other digital signals. For example, when the electronic device 100 is selecting a frequency point, the digital signal processor is used to perform Fourier transform on the frequency point energy.

[0128] Video codecs are used to compress or decompress digital videos. The electronic device 100 may support one or more video codecs. Thus, the electronic device 100 may play or record videos in a variety of coding formats, such as Moving Picture Experts Group (MPEG) 1, MPEG2, MPEG3, MPEG4, etc.

[0129] NPU is a neural network (NN) computing processor. By drawing on the structure of biological neural networks, such as the transmission mode between neurons in the human brain, it can quickly process input information and can also continuously self-learn. Through NPU, applications such as intelligent cognition of electronic device 100 can be realized, such as image recognition, face recognition, voice recognition, text understanding, etc.

[0130] The external memory interface 120 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 100. The external memory card communicates with the processor 110 through the external memory interface 120 to implement a data storage function, such as storing music, video and other files in the external memory card.

[0131] The internal memory 121 can be used to store computer executable program codes, which include instructions. The internal memory 121 may include a program storage area and a data storage area. Among them, the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc. The data storage area may store data created during the use of the electronic device 100 (such as audio data, a phone book, etc.), etc. In addition, the internal memory 121 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, a universal flash storage (UFS), etc. The processor 110 executes various functional applications and data processing of the electronic device 100 by running instructions stored in the internal memory 121, and / or instructions stored in a memory provided in the processor.

[0132] The electronic device 100 can implement audio functions such as music playing and recording through the audio module 170, the speaker 170A, the receiver 170B, the microphone 170C, the headphone jack 170D, and the application processor.

[0133] The audio module 170 is used to convert digital audio information into analog audio signal output, and is also used to convert analog audio input into digital audio signals. The audio module 170 can also be used to encode and decode audio signals. In some embodiments, the audio module 170 can be arranged in the processor 110, or some functional modules of the audio module 170 can be arranged in the processor 110.

[0134] The speaker 170A, also called a "speaker", is used to convert an audio electrical signal into a sound signal. The electronic device 100 can listen to music or listen to a hands-free call through the speaker 170A.

[0135] The receiver 170B, also called a "earpiece", is used to convert audio electrical signals into sound signals. When the electronic device 100 receives a call or voice message, the voice can be received by placing the receiver 170B close to the human ear.

[0136] Microphone 170C, also called "microphone" or "microphone", is used to convert sound signals into electrical signals. When making a call or sending a voice message, the user can speak by putting their mouth close to microphone 170C to input the sound signal into microphone 170C. The electronic device 100 can be provided with at least one microphone 170C. In other embodiments, the electronic device 100 can be provided with two microphones 170C, which can not only collect sound signals but also realize noise reduction function. In other embodiments, the electronic device 100 can also be provided with three, four or more microphones 170C to collect sound signals, reduce noise, identify the sound source, realize directional recording function, etc.

[0137] The earphone interface 170D is used to connect a wired earphone and can be a USB interface 130, or a 3.5 mm open mobile terminal platform (OMTP) standard interface or a cellular telecommunications industry association of the USA (CTIA) standard interface.

[0138] The pressure sensor 180A is used to sense the pressure signal and can convert the pressure signal into an electrical signal. In some embodiments, the pressure sensor 180A can be set on the display screen 194. There are many types of pressure sensors 180A, such as resistive pressure sensors, inductive pressure sensors, capacitive pressure sensors, etc. The capacitive pressure sensor can be a parallel plate including at least two conductive materials. When a force acts on the pressure sensor 180A, the capacitance between the electrodes changes. The electronic device 100 determines the intensity of the pressure according to the change in capacitance. When a touch operation acts on the display screen 194, the electronic device 100 detects the touch operation intensity according to the pressure sensor 180A. The electronic device 100 can also calculate the touch position according to the detection signal of the pressure sensor 180A. In some embodiments, touch operations acting on the same touch position but with different touch operation intensities can correspond to different operation instructions. For example: when a touch operation with a touch operation intensity less than the first pressure threshold acts on the short message application icon, an instruction to view the short message is executed. When a touch operation with a touch operation intensity greater than or equal to the first pressure threshold acts on the short message application icon, an instruction to create a new short message is executed.

[0139] The gyro sensor 180B can be used to determine the motion posture of the electronic device 100. In some embodiments, the angular velocity of the electronic device 100 around three axes (i.e., x, y, and z axes) can be determined by the gyro sensor 180B. The gyro sensor 180B can be used for anti-shake shooting. For example, when the shutter is pressed, the gyro sensor 180B detects the angle of the electronic device 100 shaking, calculates the distance that the lens module needs to compensate based on the angle, and allows the lens to offset the shaking of the electronic device 100 through reverse movement to achieve anti-shake. The gyro sensor 180B can also be used for navigation and somatosensory game scenes.

[0140] The air pressure sensor 180C is used to measure air pressure. In some embodiments, the electronic device 100 calculates the altitude through the air pressure value measured by the air pressure sensor 180C to assist positioning and navigation.

[0141] The magnetic sensor 180D includes a Hall sensor. The electronic device 100 can use the magnetic sensor 180D to detect the opening and closing of the flip leather case. In some embodiments, when the electronic device 100 is a flip phone, the electronic device 100 can detect the opening and closing of the flip cover according to the magnetic sensor 180D. Then, according to the detected opening and closing state of the leather case or the opening and closing state of the flip cover, the flip cover can be automatically unlocked.

[0142] The acceleration sensor 180E can detect the magnitude of the acceleration of the electronic device 100 in all directions (generally three axes). When the electronic device 100 is stationary, the magnitude and direction of gravity can be detected. It can also be used to identify the posture of the electronic device and is applied to applications such as horizontal and vertical screen switching and pedometers.

[0143] The distance sensor 180F is used to measure the distance. The electronic device 100 can measure the distance by infrared or laser. In some embodiments, when shooting a scene, the electronic device 100 can use the distance sensor 180F to measure the distance to achieve fast focusing.

[0144] The proximity light sensor 180G may include, for example, a light emitting diode (LED) and a light detector, such as a photodiode. The light emitting diode may be an infrared light emitting diode. The electronic device 100 emits infrared light outward through the light emitting diode. The electronic device 100 uses a photodiode to detect infrared reflected light from nearby objects. When sufficient reflected light is detected, it can be determined that there is an object near the electronic device 100. When insufficient reflected light is detected, the electronic device 100 can determine that there is no object near the electronic device 100. The electronic device 100 can use the proximity light sensor 180G to detect that the user holds the electronic device 100 close to the ear to talk, so as to automatically turn off the screen to save power. The proximity light sensor 180G can also be used in leather case mode and pocket mode to automatically unlock and lock the screen.

[0145] The ambient light sensor 180L is used to sense the ambient light brightness. The electronic device 100 can adaptively adjust the brightness of the display screen 194 according to the perceived ambient light brightness. The ambient light sensor 180L can also be used to automatically adjust the white balance when taking pictures. The ambient light sensor 180L can also cooperate with the proximity light sensor 180G to detect whether the electronic device 100 is in a pocket to prevent accidental touches.

[0146] The fingerprint sensor 180H is used to collect fingerprints. The electronic device 100 can use the collected fingerprint characteristics to implement fingerprint unlocking, access application locks, fingerprint photography, fingerprint call answering, etc.

[0147] The temperature sensor 180J is used to detect temperature. In some embodiments, the electronic device 100 uses the temperature detected by the temperature sensor 180J to execute a temperature processing strategy. For example, when the temperature reported by the temperature sensor 180J exceeds a threshold, the electronic device 100 reduces the performance of a processor located near the temperature sensor 180J to reduce power consumption and implement thermal protection. In other embodiments, when the temperature is lower than another threshold, the electronic device 100 heats the battery 142 to avoid abnormal shutdown of the electronic device 100 due to low temperature. In other embodiments, when the temperature is lower than another threshold, the electronic device 100 performs a boost on the output voltage of the battery 142 to avoid abnormal shutdown caused by low temperature.

[0148] The touch sensor 180K is also called a "touch control device". The touch sensor 180K can be set on the display screen 194. The touch sensor 180K and the display screen 194 form a touch screen, also called a "touch control screen". The touch sensor 180K is used to detect touch operations acting on or near it. The touch sensor can pass the detected touch operation to the application processor to determine the type of touch event. Visual output related to the touch operation can be provided through the display screen 194. In other embodiments, the touch sensor 180K can also be set on the surface of the electronic device 100, which is different from the position of the display screen 194.

[0149] The bone conduction sensor 180M can obtain a vibration signal. In some embodiments, the bone conduction sensor 180M can obtain a vibration signal of a vibrating bone block of the vocal part of the human body. The bone conduction sensor 180M can also contact the human pulse to receive a blood pressure beat signal. In some embodiments, the bone conduction sensor 180M can also be set in an earphone and combined into a bone conduction earphone. The audio module 170 can parse out a voice signal based on the vibration signal of the vibrating bone block of the vocal part obtained by the bone conduction sensor 180M to realize a voice function. The application processor can parse the heart rate information based on the blood pressure beat signal obtained by the bone conduction sensor 180M to realize a heart rate detection function.

[0150] The key 190 includes a power key, a volume key, etc. The key 190 may be a mechanical key or a touch key. The electronic device 100 may receive key input and generate key signal input related to user settings and function control of the electronic device 100.

[0151] Motor 191 can generate vibration prompts. Motor 191 can be used for incoming call vibration prompts, and can also be used for touch vibration feedback. For example, touch operations acting on different applications (such as taking pictures, audio playback, etc.) can correspond to different vibration feedback effects. For touch operations acting on different areas of the display screen 194, motor 191 can also correspond to different vibration feedback effects. Different application scenarios (for example: time reminders, receiving messages, alarm clocks, games, etc.) can also correspond to different vibration feedback effects. The touch vibration feedback effect can also support customization.

[0152] Indicator 192 may be an indicator light, which may be used to indicate charging status, power changes, messages, missed calls, notifications, etc.

[0153] The SIM card interface 195 is used to connect a SIM card. The SIM card can be connected to and separated from the electronic device 100 by inserting it into the SIM card interface 195 or pulling it out from the SIM card interface 195. The electronic device 100 can support 1 or N SIM card interfaces, where N is a positive integer greater than 1. The SIM card interface 195 can support Nano SIM cards, Micro SIM cards, SIM cards, and the like. Multiple cards can be inserted into the same SIM card interface 195 at the same time. The types of the multiple cards can be the same or different. The SIM card interface 195 can also be compatible with different types of SIM cards. The SIM card interface 195 can also be compatible with external memory cards. The electronic device 100 interacts with the network through the SIM card to implement functions such as calls and data communications. In some embodiments, the electronic device 100 uses an eSIM, i.e., an embedded SIM card. The eSIM card can be embedded in the electronic device 100 and cannot be separated from the electronic device 100.

[0154] The software system of the electronic device 100 may adopt a layered architecture, an event-driven architecture, a micro-core architecture, a micro-service architecture, or a cloud architecture. For example, the software system of the electronic device 100 may adopt an Android operating system (OS), a Harmony OS, or an IOS with a layered architecture. The embodiment of the present application takes the Android system with a layered architecture as an example to exemplify the software structure of the electronic device 100.

[0155] Figure 2 It is a software structure block diagram of the electronic device 100 according to an embodiment of the present application.

[0156] The layered architecture divides the software into several layers, each with clear roles and division of labor. The layers communicate with each other through software interfaces. In some embodiments, the Android system is divided into four layers, from top to bottom: the application layer, the application framework layer, the Android runtime (Android runtime) and the system library, and the kernel layer.

[0157] The application layer can include a series of application packages.

[0158] like Figure 2 As shown, the application package may include camera, gallery, calendar, call, map, navigation, WLAN, Bluetooth, music, video, short message and other applications.

[0159] The application framework layer provides an application programming interface (API) and a programming framework for the applications in the application layer. The application framework layer includes some predefined functions.

[0160] like Figure 2 As shown, the application framework layer may include a window manager, a content provider, a view system, a phone manager, a resource manager, a notification manager, and the like.

[0161] The window manager is used to manage window programs. The window manager can obtain the display screen size, determine whether there is a status bar, lock the screen, capture the screen, etc.

[0162] Content providers are used to store and retrieve data and make it accessible to applications. The data may include videos, images, audio, calls made and received, browsing history and bookmarks, phone books, etc.

[0163] The view system includes visual controls, such as controls for displaying text, controls for displaying images, etc. The view system can be used to build applications. A display interface can be composed of one or more views. For example, a display interface including a text notification icon can include a view for displaying text and a view for displaying images.

[0164] The phone manager is used to provide communication functions of the electronic device 100, such as management of call status (including connecting, hanging up, etc.).

[0165] The resource manager provides various resources for applications, such as localized strings, icons, images, layout files, video files, and so on.

[0166] The notification manager enables applications to display notification information in the status bar. It can be used to convey notification-type messages and can disappear automatically after a short stay without user interaction. For example, the notification manager is used to notify download completion, message reminders, etc. The notification manager can also be a notification that appears in the system top status bar in the form of a chart or scroll bar text, such as notifications of applications running in the background, or a notification that appears on the screen in the form of a dialog window. For example, a text message is displayed in the status bar, a prompt sound is emitted, an electronic device vibrates, an indicator light flashes, etc.

[0167] Android Runtime includes core libraries and virtual machines. Android runtime is responsible for scheduling and management of the Android system.

[0168] The core library consists of two parts: one part is the function that needs to be called by the Java language, and the other part is the Android core library.

[0169] The application layer and the application framework layer run in a virtual machine. The virtual machine executes the Java files of the application layer and the application framework layer as binary files. The virtual machine is used to perform functions such as object life cycle management, stack management, thread management, security and exception management, and garbage collection.

[0170] The system library may include multiple functional modules, such as surface manager, media library, 3D graphics processing library (such as OpenGL ES), 2D graphics engine (such as SGL), etc.

[0171] The surface manager is used to manage the display subsystem and provide the fusion of 2D and 3D layers for multiple applications.

[0172] The media library supports playback and recording of a variety of commonly used audio and video formats, as well as static image files, etc. The media library can support a variety of audio and video encoding formats, such as: MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, etc.

[0173] The 3D graphics processing library is used to implement 3D graphics drawing, image rendering, compositing, and layer processing.

[0174] A 2D graphics engine is a drawing engine for 2D drawings.

[0175] The kernel layer is the layer between hardware and software. The kernel layer contains at least display driver, camera driver, audio driver, and sensor driver.

[0176] Mobile phones and other electronic devices have become indispensable communication tools for people's daily life and work. In order to meet people's needs, more and more applications are installed in electronic devices, resulting in more and more threads in the use of electronic devices, and multiple threads sharing the same resource is becoming more and more common. When multiple threads share the same resource, if multiple threads access the shared resource at the same time, data errors are likely to occur. In order to avoid data errors caused by multiple threads accessing shared resources at the same time, a priority is generally set for each thread so that each thread accesses the shared resource in order from high to low priority.

[0177] However, when a high-priority thread requests to access a shared resource, if a low-priority thread already holds the shared resource, the high-priority thread needs to wait for the low-priority thread to complete scheduling and release the shared resource before it can access the shared resource. Due to its low priority, the low-priority thread may not be able to obtain sufficient CPU resources in time for scheduling, resulting in a long scheduling time for the low-priority thread and a long waiting time for the high-priority thread, that is, causing the high-priority thread to be blocked for a long time and a priority inversion, which can cause problems such as freezing of the electronic device and affect the user experience.

[0178] See also Figure 3 , Figure 3An example diagram of priority inversion is shown. In this example, an electronic device includes threads P1, P2, and P3, and the priority of thread P1 is lower than that of thread P2, and the priority of thread P2 is lower than that of thread P3.

[0179] like Figure 3 As shown, at time T1, thread P1 obtains the shared resource for scheduling. At time T2, thread P3 can start scheduling and request to obtain the shared resource at time T3. Since the shared resource is still held by thread P1 at time T3, thread P3 cannot obtain the shared resource and enters the sleep waiting state.

[0180] At time T4, thread P2, which does not need to access the shared resource, starts to be scheduled and run. Since the priority of thread P2 is higher than that of thread P1, thread P2 can seize more CPU resources, causing thread P1 to fail to obtain sufficient CPU resources and enter a sleep waiting state.

[0181] Assume that at time T5, thread P2 completes scheduling and releases CPU resources. At this time, thread P1 can reacquire sufficient CPU resources for scheduling. Assume that at time T6, thread P1 completes scheduling and releases the shared resources. At this time, thread P3 can acquire the shared resources for scheduling.

[0182] That is Figure 3 As shown, since thread P1 has a low priority, it cannot obtain sufficient CPU resources for scheduling in time, resulting in high-priority thread P3 being in a sleep waiting state from time T3 to time T6, causing high-priority thread P3 to be blocked for a long time, affecting the user experience.

[0183] Generally, a priority ceiling can be used to solve the problem of priority inversion. The priority ceiling means adjusting the priority of the thread that obtains a shared resource to the highest priority among all threads that may access the shared resource, so that the thread that obtains the shared resource can obtain sufficient CPU resources in time to complete the scheduling faster, thereby releasing the shared resource faster, reducing the waiting time for other threads to obtain the shared resource, and reducing the blocking of other threads.

[0184] See also Figure 4 , Figure 4 An example diagram of solving priority inversion through a priority ceiling is shown. In this example, the threads that may access a shared resource in an electronic device include thread P1, thread P2, and thread P3, and the priority of thread P1 is lower than the priority of thread P2, and the priority of thread P2 is lower than the priority of thread P3.

[0185] like Figure 4 As shown, thread P1 can start running at time T1 and can obtain shared resources at time T3. When thread P1 obtains the shared resources at time T3, since the priority of thread P1 is not the highest priority, the electronic device can set the priority of thread P1 to the highest priority, that is, the priority of thread P1 can be set to the priority of thread P3, so that thread P1 can obtain more CPU resources in time, can complete scheduling faster, and thus release the shared resources faster.

[0186] After thread P1 releases the shared resource, the electronic device can restore the priority of thread P1 to its original priority. Assuming that at time T6, thread P1 completes scheduling and releases the shared resource, then at time T6, the electronic device can restore the priority of thread P1 to its original priority.

[0187] like Figure 4 As shown, thread P2 can start running at time T2 and can request the shared resource at time T4. At time T4, since the shared resource is still held by thread P1, thread P2 cannot obtain the shared resource and enters the sleep waiting state. When thread P1 completes scheduling at time T6 and releases the shared resource, thread P2 can restart and obtain the shared resource.

[0188] When thread P2 obtains the shared resource, since the priority of thread P2 is not the highest priority, the electronic device can set the priority of thread P2 to the highest priority, that is, the priority of thread P2 can be set to the priority of thread P3, so that thread P2 can obtain more CPU resources in time, complete scheduling faster, and thus release the shared resource faster.

[0189] After thread P2 releases the shared resource, the electronic device can restore the priority of thread P2 to the original priority. Assuming that at time T8, thread P2 completes scheduling and releases the shared resource, then at time T8, the electronic device can restore the priority of thread P2 to the original priority.

[0190] like Figure 4As shown, thread P3 can start running at time T5 and can request the shared resource at time T7. At time T7, since the shared resource is still held by thread P2, thread P3 cannot obtain the shared resource and enters a sleep waiting state. When thread P2 completes scheduling at time T8 and releases the shared resource, thread P3 can restart and obtain the shared resource. When thread P3 obtains the shared resource, since thread P3 has the highest priority, thread P3 can obtain more CPU resources in time and complete scheduling faster.

[0191] From the above description, it can be seen that the priority ceiling can improve the scheduling speed of thread P1 and thread P2, reduce the waiting time for thread P2 and thread P3 to obtain the shared resource, and thus reduce the blocking time of thread P2 and thread P3. However, in the priority ceiling, regardless of whether there is thread blocking, the electronic device will set the priority of the thread that has obtained the shared resource to the highest priority, which greatly increases the priority setting overhead. In addition, due to the large number and type of shared resources in electronic devices, this method of setting the priority of the thread that has obtained any shared resource to the highest priority will result in more high-priority threads in the system, causing low-priority threads to be in a waiting state for a long time, causing system instability and affecting the performance of the electronic device.

[0192] Among them, priority inversion is mainly caused by the kernel's read-write lock. It should be understood that when multiple threads share the same resource, multiple threads will read the shared resource at the same time without causing data errors. However, once one thread writes to the shared resource, other threads will read / write the shared resource, which will cause data read / write errors. Therefore, a read-write lock can be set in the electronic device to access the shared resource.

[0193] Read-write locks can divide the visitors (i.e., threads) of shared resources into readers (i.e., read threads) and writers (i.e., write threads). Read threads only read shared resources and do not change them. Write threads can write to shared resources and change them. That is, read-write locks can include read-write locks in read mode and read-write locks in write mode. Multiple read threads can hold read-write locks in read mode at the same time, while for write threads, only one write thread can hold a read-write lock in write mode at any one time.

[0194] When the read-write lock is in write mode, that is, when a writer thread holds the read-write lock in write mode, all threads requesting the read-write lock will be blocked before the writer thread releases the read-write lock.

[0195] When the read-write lock is in read mode, that is, when there are read threads (such as thread A and thread B) holding the read-write lock in read mode, all threads requesting read mode can access the read-write lock, but the thread requesting the read-write lock in write mode will be blocked, that is, the thread requesting the read-write lock in write mode needs to wait for all threads holding the read-write lock in read mode to release the read-write lock, for example, it needs to wait until both thread A and thread B release the read-write lock before it can obtain the read-write lock in write mode.

[0196] It should be noted that, through statistics, it is found that when a read-write lock is used to access shared resources, when a thread requests a read-write lock in write mode, if there is a thread that already holds a read-write lock in read mode, it is easy to cause the thread requesting the read-write lock in write mode to be blocked. Among them, when the blocked thread is a thread with a higher priority, a priority inversion problem will occur, that is, a priority inversion problem is easy to occur between the thread requesting the read-write lock in write mode and the thread that already holds the read-write lock in read mode, thereby causing problems such as freezing of electronic devices and affecting user experience.

[0197] [Example 1]

[0198] In order to solve the problem of priority inversion caused by a read-write lock, the first embodiment of the present application provides a thread scheduling method. The thread scheduling method provided by the first embodiment of the present application will be described in detail below in conjunction with the accompanying drawings and specific application scenarios.

[0199] See also Figure 5 , Figure 5 FIG. 1 shows a schematic flow chart of a thread scheduling method provided in Embodiment 1 of the present application. The method can be applied to electronic devices. Figure 5 As shown, the method may include:

[0200] S501. When it is detected that a first thread requests a target read-write lock in write mode, a second thread is determined, where the second thread is a thread that already holds the target read-write lock in read mode.

[0201] S502: Determine scheduling attributes of the first thread and the second thread.

[0202] S503: When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, set the scheduling attribute of the second thread to the scheduling attribute of the first thread, so that the second thread schedules the target read-write lock according to the scheduling attribute of the first thread.

[0203] In an embodiment of the present application, when it is detected that the first thread requests a target read-write lock in write mode, the electronic device can determine the second thread, which is the thread that already holds the target read-write lock in read mode, and determine the scheduling attributes of the first thread and the scheduling attributes of the second thread. When the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread, the electronic device can set the scheduling attributes of the second thread to the scheduling attributes of the first thread, so that the second thread can be scheduled according to the scheduling attributes of the first thread. That is, the scheduling attributes of the second thread are improved so that the second thread can obtain more CPU resources for scheduling, and the scheduling speed of the second thread is improved so that the second thread can quickly release the target read-write lock, reduce the waiting time for the first thread to obtain the target read-write lock, avoid blocking the first thread for a long time, causing the electronic device to freeze and other problems, and improve the user experience.

[0204] Optionally, the structure of the read-write lock may be a rwsem structure (i.e., reader / writer semaphore), and the rwsem structure may be as shown below:

[0205] struct rw_semaphore{

[0206] atomic_long_t count;

[0207] atomic_long_t owner;

[0208] raw_spinlock_t wait_lock;

[0209] struct list_head wait_list;

[0210] };

[0211] Here, count refers to a state identifier, which can be used to identify whether there is a thread holding a read-write lock in write mode, or to identify the number of threads holding a read-write lock in read mode. That is, count can be used to identify whether there is a writer thread holding a read-write lock, or the number of reader threads holding a read-write lock. Count can be divided into multiple parts, for example, two parts, one of which can be used to identify whether there is a writer thread holding a read-write lock. The other part can be used to identify the number of reader threads holding a read-write lock. For example, taking a 32-bit system as an example, count can contain 24 bits, of which 1 bit can be used to identify whether there is a writer thread holding a read-write lock, and the other 23 bits can be used to identify the number of reader threads holding the read-write lock.

[0212] wait_list refers to the waiting queue (i.e., waiter queue). When a reading thread or a writing thread cannot obtain the read-write lock and is blocked, the thread can be mounted in the waiting queue. The waiting queue uses the tail insertion method by default and wakes up from the head. In other words, when a thread cannot obtain the read-write lock and is blocked, the thread can be mounted at the tail of the waiting queue. When the thread holding the read-write lock releases the read-write lock, the thread can be woken up from the head of the waiting queue to obtain the read-write lock, that is, the thread at the head of the waiting queue will be woken up to obtain the read-write lock. If the thread at the head of the waiting queue is a reading thread, then multiple reading threads in the waiting queue may be woken up to obtain the read-write lock.

[0213] wait_lock is a spin lock. When a thread cannot obtain the read-write lock, the thread does not need to enter the sleep state, that is, it does not need to be mounted in the waiting queue, but spins in place, waiting to obtain the read-write lock. These threads that are not mounted in the waiting queue form a group of spin queues in the spinning state (i.e., spinner queue).

[0214] The owner is the holder, that is, the thread that has acquired the read-write lock. In other words, the owner can be used to record the relevant information of the thread that has acquired the read-write lock.

[0215] For example, owner can be used to record relevant information of a writer thread that obtains a read-write lock, or can be used to record relevant information of a reader thread that obtains a read-write lock. That is, when a writer thread obtains a read-write lock, owner can be used to record relevant information of the writer thread. When one or more reader threads obtain a read-write lock, owner can be used to record relevant information of the one or more reader threads.

[0216] For example, when a certain writing thread obtains the read-write lock, the task_struct pointer of the writing thread can be filled into the owner, and the task_struct pointer of the writing thread will not be deleted from the owner until the writing thread releases the read-write lock. For example, when a certain reading thread obtains the read-write lock, the task_struct pointer of the reading thread can be filled into the owner, and the task_struct pointer of the writing thread will not be deleted from the owner until the reading thread releases the read-write lock. For example, when multiple reading threads obtain the read-write lock, the task_struct pointers of these multiple reading threads can be filled into the owner respectively, so that the electronic device can accurately locate the reading thread holding the read-write lock according to the task_struct pointers of each reading thread recorded in the owner.

[0217] See also Figure 6 , Figure 6A structural example diagram of a read-write lock provided in Embodiment 1 of the present application is shown. This example is exemplified by taking multiple read threads acquiring the read-write lock as an example.

[0218] like Figure 6 As shown, when multiple read threads (such as thread A, thread B, and thread C) acquire the read-write lock, the electronic device can record each read thread in the owner of the rwsem structure. For example, the task_struct pointer of thread A, the task_struct pointer of thread B, and the task_struct pointer of thread C are recorded.

[0219] In the process of multiple read threads holding the read-write lock, suppose a write thread (for example, thread D) requests the read-write lock. Since the read-write lock is already held by the read thread, thread D cannot obtain the read-write lock. At this time, the electronic device can mount thread D in the waiting queue, that is, it can mount thread D at the head of the waiting queue.

[0220] Among them, after thread D requests the read-write lock, if there are other read threads or write threads requesting the read-write lock, the electronic device can mount the other read threads or write threads to the waiting queue, for example, mounting the other read threads or write threads behind thread D. Figure 6 In the example, multiple read threads (such as thread E and thread F) are used for explanation. Therefore, when multiple read threads (i.e., thread A, thread B, and thread C) all release the read-write lock, thread D at the head of the waiting queue can acquire the read-write lock.

[0221] That is, when a read thread (such as the second thread) holds the read-write lock, if a write thread (such as the first thread) requests the read-write lock, the first thread needs to wait for the second thread to complete the scheduling and release the read-write lock before it can acquire the read-write lock. When the scheduling time of the second thread is long, for example, when the second thread cannot obtain enough CPU resources in time to schedule threads due to low scheduling properties, the first thread will be blocked for a long time, reducing the user experience.

[0222] Therefore, in the embodiment of the present application, when the first thread requests a read-write lock in write mode, if a second thread already holds the read-write lock in read mode, the electronic device can accurately determine the second thread based on the information recorded in the owner. When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, the electronic device can set the scheduling attribute of the second thread to the scheduling attribute of the first thread, that is, to increase the scheduling attribute of the second thread to the scheduling attribute of the first thread, so that the second thread can obtain more CPU resources in time, etc., so that the second thread can quickly perform a read operation to quickly release the read-write lock, so that the first thread can quickly obtain the read-write lock, reduce the waiting time of the first thread, avoid blocking the first thread for a long time, and improve the user experience.

[0223] It should be understood that only one write thread can hold a read-write lock in write mode at the same time. When the write thread obtains the read-write lock in write mode, the write thread can be recorded in the owner of the rwsem structure. However, since multiple read threads can hold the same read-write lock at the same time, and there are many read-write locks in electronic devices, if all read-write locks are recorded as the thread that obtains the read-write lock in read mode, it will not only increase the system's memory overhead, but also cause system performance degradation.

[0224] In one example, in order to reduce the system's memory overhead and reduce the impact on system performance, in an embodiment of the present application, only the read thread that obtains the target read-write lock can be recorded in the owner for the target read-write lock, so as to optimize the thread scheduling for obtaining the target read-write lock. That is, when a thread (such as a read thread or a write thread) obtains the target read-write lock, the thread can be recorded in the owner of the rwsem structure corresponding to the target read-write lock.

[0225] Therefore, during the thread scheduling process, when the first thread requests the target read-write lock in write mode, the electronic device can determine the second thread based on the information recorded in the owner, that is, determine the thread that has held the target read-write lock in read mode, and can determine the scheduling attributes of the first thread and the scheduling attributes of the second thread. When the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread, the electronic device can set the scheduling attributes of the second thread to the scheduling attributes of the first thread to increase the scheduling attributes of the second thread, increase the scheduling speed of the second thread, so that the second thread can quickly release the target read-write lock, so that the first thread can quickly obtain the target read-write lock, reduce the blocking time of the first thread, and improve the user experience.

[0226] Optionally, the target reader-writer lock may be one or more of all the reader-writer locks of the electronic device. It is understandable that the target reader-writer lock may be specifically determined according to the actual scenario, and the embodiment of the present application does not impose any limitation on this.

[0227] For example, according to actual scenarios, a read-write lock that is prone to priority inversion and affects the performance of the electronic device may be determined as a target read-write lock.

[0228] For example, statistics show that priority inversion is prone to occur during the use of mmap_lock. Therefore, mmap-lock can be determined as the target read-write lock. mmap_lock is designed for the process to protect its own virtual address space from concurrent access by multiple threads. If you want to access the virtual address space of the process, you need to hold mmap_lock to access it.

[0229] It should be understood that the read-write lock generally needs to be initialized before use, for example, the count in the rwsem structure needs to be set to 0, the wait_lock spin lock needs to be set to unlocked, and the wait_list needs to be set to an empty linked list, etc. In an embodiment of the present application, when initializing the read-write lock, the electronic device can mark the read-write lock to determine whether the read-write lock is a target read-write lock. That is, after determining that some read-write locks are target read-write locks, for example, when determining that mmap-lock is a target read-write lock, the electronic device can mark the read-write lock as a target read-write lock when initializing the read-write lock (e.g., mmap-lock), that is, during the process creation process, when initializing mmap-lock, the electronic device can mark mmap-lock as a target read-write lock.

[0230] The following describes in detail the process of determining the scheduling attributes of the first thread and the second thread, and setting the scheduling attributes of the second thread to the scheduling attributes of the first thread when the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread.

[0231] Exemplarily, the scheduling attributes may include at least one of priority, load, and quality of service (QoS).

[0232] In one example, when the scheduling attributes include priority, when scheduling the target read-write lock, the electronic device can determine whether the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread based on the priority of the first thread and the priority of the second thread, so as to determine whether the scheduling attributes of the second thread need to be adjusted.

[0233] That is, when the first thread requests the target read-write lock in write mode, if the second thread already holds the target read-write lock in read mode, the electronic device can accurately determine the second thread based on the information recorded in the owner, and can determine the priority of the first thread and the priority of the second thread. When the priority of the first thread is higher than the priority of the second thread, the electronic device can set the priority of the second thread to the priority of the first thread, that is, raise the priority of the second thread to the priority of the first thread, so that the second thread can obtain more CPU resources and / or time slices in time, improve the scheduling speed of the second thread, and allow the second thread to quickly release the target read-write lock, so that the first thread can quickly obtain the target read-write lock, reduce the blocking time of the first thread, and improve the user experience.

[0234] In another example, when the scheduling attributes include load, when scheduling the target read-write lock, the electronic device can determine whether the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread based on the load of the first thread and the load of the second thread, so as to determine whether the scheduling attributes of the second thread need to be adjusted.

[0235] That is, when the first thread requests a target read-write lock in write mode, if a second thread already holds the target read-write lock in read mode, the electronic device can accurately determine the second thread based on the information recorded in the owner, and determine the load of the first thread and the load of the second thread. When the load of the first thread is higher than the load of the second thread, the electronic device can set the load of the second thread to the load of the first thread, that is, adjust the load of the second thread to the load of the first thread to increase the load of the second thread, so that the second thread can obtain more CPU resources and / or CPU resources with better performance in time, improve the scheduling speed of the second thread, so that the second thread can quickly release the target read-write lock, so that the first thread can quickly obtain the target read-write lock, reduce the blocking time of the first thread, and improve the user experience.

[0236] In another example, when the scheduling attributes include QoS, when scheduling the target read-write lock, the electronic device can determine whether the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread based on the QoS of the first thread and the QoS of the second thread, so as to determine whether the scheduling attributes of the second thread need to be adjusted.

[0237] That is, when the first thread requests a target read-write lock in write mode, if a second thread already holds the target read-write lock in read mode, the electronic device can accurately determine the second thread based on the information recorded in the owner, and determine the QoS of the first thread and the QoS of the second thread. When the QoS of the first thread is higher than the QoS of the second thread, the electronic device can set the QoS of the second thread to the QoS of the first thread, that is, to increase the QoS of the second thread to the QoS of the first thread, so as to improve the service capability of the second thread, increase the scheduling speed of the second thread, so that the second thread can quickly release the target read-write lock, so that the first thread can quickly obtain the target read-write lock, reduce the blocking time of the first thread, and improve the user experience.

[0238] In another example, when the scheduling attributes include at least two of priority, load, and quality of service (QoS), the electronic device can determine whether the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread based on at least one of the scheduling attributes, thereby determining whether the scheduling attributes of the second thread need to be adjusted.

[0239] For example, when the scheduling attribute includes priority and load, the electronic device may determine whether the priority of the first thread is higher than the priority of the second thread, and / or may determine whether the load of the first thread is higher than the load of the second thread. When the priority of the first thread is higher than the priority of the second thread, and / or the load of the first thread is higher than the load of the second thread, the electronic device may determine that the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread.

[0240] That is, when the first thread requests a target read-write lock in write mode, if a second thread already holds the target read-write lock in read mode, the electronic device can accurately determine the second thread based on the information recorded in the owner. Subsequently, the electronic device can determine the priority of the first thread and the priority of the second thread, and / or determine the load of the first thread and the load of the second thread.

[0241] When the priority of the first thread is higher than the priority of the second thread, the electronic device can set the priority of the second thread to the priority of the first thread, that is, the priority of the second thread can be raised to the priority of the first thread, so that the second thread can obtain more CPU resources and / or time slices in time, etc., thereby improving the scheduling speed of the second thread, so that the second thread can quickly release the target read-write lock, thereby allowing the first thread to quickly obtain the target read-write lock, reducing the blocking time of the first thread, and improving the user experience.

[0242] Alternatively, when the load of the first thread is higher than the load of the second thread, the electronic device can set the load of the second thread to the load of the first thread, that is, the load of the second thread can be adjusted to the load of the first thread, so that the second thread can obtain more CPU resources and / or CPU resources with better performance in a timely manner, thereby improving the scheduling speed of the second thread, so that the second thread can quickly release the target read-write lock, thereby allowing the first thread to quickly obtain the target read-write lock, reducing the blocking time of the first thread, and improving the user experience.

[0243] Alternatively, when the priority of the first thread is higher than the priority of the second thread, and the load of the first thread is higher than the load of the second thread, the electronic device can set the priority of the second thread to the priority of the first thread, and set the load of the second thread to the load of the first thread, that is, the priority of the second thread can be increased to the priority of the first thread, and the load of the second thread can be adjusted to the load of the first thread, so that the second thread can obtain more CPU resources and / or time slices in time, or the second thread can obtain more CPU resources and / or CPU resources with better performance in time, improve the scheduling speed of the second thread, so that the second thread can quickly release the target read-write lock, so that the first thread can quickly obtain the target read-write lock, thereby reducing the blocking time of the first thread and improving the user experience.

[0244] It should be noted that the embodiment of the present application does not impose any specific restrictions on the method for determining the priority, load or QoS of the first thread (or the second thread), which can be determined specifically according to the actual scenario.

[0245] [Example 2]

[0246] It is understandable that threads exist in processes, and a process may include one or more threads. When a process includes multiple threads, each thread can share the address space (e.g., including code, and global data or variables) and resources (e.g., memory resources, I / O resources, CPU resources, etc.) of the process. In other words, the global variables of a process can be shared by all threads included in the process, and each global variable can only be held by one thread at the same time. Therefore, when multiple threads share the same variable, each thread generally needs to hold the shared variable in a certain order to avoid data errors. For example, each thread can hold the shared variable in order from high to low according to the priority of each thread.

[0247] Among them, when multiple threads can call the same variable, priority inversion is also prone to occur. For example, when a thread (such as thread A) requests a variable, if there is a thread (such as thread B) that already holds the variable, thread A needs to wait for thread B that already holds the variable to release the variable before it can obtain the variable, causing thread A to be blocked. When the blocked thread A is a thread with a higher priority, priority inversion will occur, causing problems such as freezing of electronic devices, affecting user experience.

[0248] In order to solve the problem of priority inversion caused by variable sharing, the second embodiment of the present application provides a thread scheduling method. The thread scheduling method provided by the second embodiment of the present application will be described in detail below in conjunction with the accompanying drawings and specific application scenarios.

[0249] See also Figure 7 , Figure 7 FIG. 1 shows a schematic flow chart of a thread scheduling method provided in Embodiment 2 of the present application. The method can be applied to electronic devices. Figure 7 As shown, the method may include:

[0250] S701. When it is detected that a first thread requests a target variable, a second thread is determined, where the second thread is a thread that already holds the target variable.

[0251] S702: Determine scheduling attributes of the first thread and the second thread.

[0252] S703: When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, set the scheduling attribute of the second thread to the scheduling attribute of the first thread, so that the second thread schedules the target variable according to the scheduling attribute of the first thread.

[0253] In an embodiment of the present application, when it is detected that the first thread requests the target variable, the electronic device can determine the second thread, which is the thread that already holds the target variable, and determine the scheduling attributes of the first thread and the scheduling attributes of the second thread. When the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread, the electronic device can set the scheduling attributes of the second thread to the scheduling attributes of the first thread, so that the second thread can schedule the target variable according to the scheduling attributes of the first thread. That is, the scheduling attributes of the second thread are improved so that the second thread can obtain more CPU resources in time to call the target variable, improve the scheduling speed of the second thread, so that the second thread can quickly release the target variable, reduce the waiting time for the first thread to obtain the target variable, avoid blocking the first thread for a long time, causing the electronic device to freeze and other problems, and improve the user experience.

[0254] Optionally, when a thread obtains the target variable, the electronic device may record the relevant information of the thread. For example, the relevant information of the thread may be recorded in a structure corresponding to the target variable. For example, when a thread obtains the target variable, the task_struct pointer of the thread may be filled into the owner in the corresponding structure, and the task struct pointer of the thread will not be deleted from the owner until the thread releases the target variable.

[0255] In one example, since there are many processes in an electronic device and the number of variables included in each process is also large, if all variables are recorded as threads that obtain the variable, not only will the system's memory overhead increase, but also the system's performance will be reduced. Therefore, in order to reduce the system's memory overhead and reduce the impact on system performance, in an embodiment of the present application, only the target variable can be recorded as the thread that obtains the target variable, so as to optimize the thread scheduling for obtaining the target variable.

[0256] Optionally, the target variable may be one or more of all variables of the electronic device. It is understandable that the target variable may be specifically determined according to the actual scenario, and the embodiments of the present application do not impose any restrictions on this. Exemplarily, according to the actual scenario, a variable that is prone to cause priority inversion and affects the performance of the electronic device may be determined as the target variable.

[0257] For example, it is found through statistics that priority inversion is prone to occur during the use of wait bits, which affects the performance of electronic devices. Therefore, wait bits can be determined as target variables.

[0258] The following describes in detail the process of determining the scheduling attributes of the first thread and the second thread, and setting the scheduling attributes of the second thread to the scheduling attributes of the first thread when the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread.

[0259] Exemplarily, the scheduling attributes may include at least one of priority, load, and QoS.

[0260] In one example, when the scheduling attributes include priority, when scheduling the target variable, the electronic device can determine whether the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread based on the priority of the first thread and the priority of the second thread, so as to determine whether the scheduling attributes of the second thread need to be adjusted.

[0261] That is, when the first thread requests the target variable, if the second thread already holds the target variable, the electronic device can accurately determine the second thread based on the information recorded in the owner, and can determine the priority of the first thread and the priority of the second thread. When the priority of the first thread is higher than the priority of the second thread, the electronic device can set the priority of the second thread to the priority of the first thread, that is, raise the priority of the second thread to the priority of the first thread, so that the second thread can obtain more CPU resources and / or time slices in time, improve the scheduling speed of the second thread, and enable the second thread to quickly release the target variable, so that the first thread can quickly obtain the target variable, reduce the blocking time of the first thread, and improve the user experience.

[0262] In another example, when the scheduling attributes include load, when scheduling the target variable, the electronic device can determine whether the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread based on the load of the first thread and the load of the second thread, so as to determine whether the scheduling attributes of the second thread need to be adjusted.

[0263] That is to say, when the first thread requests the target variable, if the second thread already holds the target variable, the electronic device can accurately determine the second thread based on the information recorded in the owner, and determine the load of the first thread and the load of the second thread. When the load of the first thread is higher than the load of the second thread, the electronic device can set the load of the second thread to the load of the first thread, that is, adjust the load of the second thread to the load of the first thread to increase the load of the second thread, so that the second thread can obtain more CPU resources and / or CPU resources with better performance in time, improve the scheduling speed of the second thread, so that the second thread can quickly release the target variable, so that the first thread can quickly obtain the target variable, reduce the blocking time of the first thread, and improve the user experience.

[0264] In another example, when the scheduling attributes include QoS, when scheduling the target variable, the electronic device can determine whether the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread based on the QoS of the first thread and the QoS of the second thread, so as to determine whether the scheduling attributes of the second thread need to be adjusted.

[0265] That is, when the first thread requests the target variable, if the second thread already holds the target variable, the electronic device can accurately determine the second thread based on the information recorded in the owner, and determine the QoS of the first thread and the QoS of the second thread. When the QoS of the first thread is higher than the QoS of the second thread, the electronic device can set the QoS of the second thread to the QoS of the first thread, that is, to increase the QoS of the second thread to the QoS of the first thread, so as to improve the service capability of the second thread, increase the scheduling speed of the second thread, so that the second thread can quickly release the target variable, so that the first thread can quickly obtain the target variable, reduce the blocking time of the first thread, and improve the user experience.

[0266] In another example, when the scheduling attributes include at least two of priority, load, and quality of service (QoS), the electronic device can determine whether the scheduling attributes of the first thread are higher than the scheduling attributes of the second thread based on at least one of the scheduling attributes, thereby determining whether the scheduling attributes of the second thread need to be adjusted.

[0267] For example, when the scheduling attribute includes priority and load, the electronic device may determine whether the priority of the first thread is higher than the priority of the second thread, and / or may determine whether the load of the first thread is higher than the load of the second thread. When the priority of the first thread is higher than the priority of the second thread, and / or the load of the first thread is higher than the load of the second thread, the electronic device may determine that the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread.

[0268] That is, when the first thread requests the target variable, if the second thread already holds the target variable, the electronic device can accurately determine the second thread based on the information recorded in the owner. Subsequently, the electronic device can determine the priority of the first thread and the priority of the second thread, and / or determine the load of the first thread and the load of the second thread.

[0269] When the priority of the first thread is higher than the priority of the second thread, the electronic device can set the priority of the second thread to the priority of the first thread, that is, the priority of the second thread can be raised to the priority of the first thread, so that the second thread can obtain more CPU resources and / or time slices in time, etc., improve the scheduling speed of the second thread, so that the second thread can quickly release the target variable, so that the first thread can quickly obtain the target variable, reduce the blocking time of the first thread, and improve the user experience.

[0270] Alternatively, when the load of the first thread is higher than the load of the second thread, the electronic device can set the load of the second thread to the load of the first thread, that is, the load of the second thread can be adjusted to the load of the first thread, so that the second thread can obtain more CPU resources and / or CPU resources with better performance in a timely manner, thereby improving the scheduling speed of the second thread, so that the second thread can quickly release the target variable, thereby allowing the first thread to quickly obtain the target variable, reducing the blocking time of the first thread, and improving the user experience.

[0271] Alternatively, when the priority of the first thread is higher than the priority of the second thread, and the load of the first thread is higher than the load of the second thread, the electronic device can set the priority of the second thread to the priority of the first thread, and set the load of the second thread to the load of the first thread, that is, the priority of the second thread can be increased to the priority of the first thread, and the load of the second thread can be adjusted to the load of the first thread, so that the second thread can obtain more CPU resources and / or time slices in time, and / or the second thread can obtain more CPU resources and / or CPU resources with better performance in time, improve the scheduling speed of the second thread, so that the second thread can quickly release the target variable, so that the first thread can quickly obtain the target variable, reduce the blocking time of the first thread, and improve the user experience.

[0272] It should be understood that the size of the serial numbers of the steps in the above embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.

[0273] Corresponding to the thread scheduling method described in the above embodiment, the embodiment of the present application also provides a thread scheduling device, and each module of the device can correspond to each step of implementing the thread scheduling method.

[0274] It should be noted that the information interaction, execution process, etc. between the above-mentioned devices / units are based on the same concept as the method embodiment of the present application. Their specific functions and technical effects can be found in the method embodiment part and will not be repeated here.

[0275] The technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In practical applications, the above-mentioned function allocation can be completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, which will not be repeated here.

[0276] The present application also provides an electronic device, the electronic device comprising at least one memory, at least one processor, and a computer program stored in the at least one memory and executable on the at least one processor, wherein when the processor executes the computer program, the electronic device implements the steps in any of the above-mentioned method embodiments. Exemplarily, the structure of the electronic device can be as follows: Figure 1 shown.

[0277] An embodiment of the present application further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program. When the computer program is executed by a computer, the computer implements the steps in any of the above method embodiments.

[0278] An embodiment of the present application provides a computer program product. When the computer program product is executed on an electronic device, the electronic device implements the steps in any of the above method embodiments.

[0279] 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 computer-readable storage medium. Based on this understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, which can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable storage medium may at least include: any entity or device that can carry the computer program code to the device / electronic device, a recording medium, a computer memory, a read-only memory (ROM), a random access memory (RAM), an electric carrier signal, a telecommunication signal, and a software distribution medium. For example, a USB flash drive, a mobile hard disk, a magnetic disk or an optical disk. In some jurisdictions, according to legislation and patent practice, a computer-readable storage medium cannot be an electric carrier signal and a telecommunication signal.

[0280] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0281] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0282] In the embodiments provided in the present application, it should be understood that the disclosed devices / electronic devices and methods can be implemented in other ways. For example, the device / electronic device embodiments described above are merely 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 system, 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.

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

[0284] The embodiments described above are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, a person skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application, and should all be included in the protection scope of the present application.

Claims

1. A thread scheduling method, characterized in that: The method comprises: When it is detected that the first thread requests a target read-write lock in write mode, determining a second thread, where the second thread is a thread that already holds the target read-write lock in read mode; Determining a scheduling attribute of the first thread and a scheduling attribute of the second thread; When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, the scheduling attribute of the second thread is set to the scheduling attribute of the first thread, so that the second thread schedules the target read-write lock according to the scheduling attribute of the first thread.

2. The method according to claim 1, characterized in that The scheduling attributes include priority; When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, setting the scheduling attribute of the second thread to the scheduling attribute of the first thread includes: When the priority of the first thread is higher than the priority of the second thread, the priority of the second thread is set to the priority of the first thread.

3. The method according to claim 1 or 2, characterized in that: The scheduling attributes include load; When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, setting the scheduling attribute of the second thread to the scheduling attribute of the first thread includes: When the load of the first thread is higher than the load of the second thread, the load of the second thread is set to the load of the first thread.

4. The method according to any one of claims 1 to 3, characterized in that The scheduling attributes include quality of service QoS; When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, setting the scheduling attribute of the second thread to the scheduling attribute of the first thread includes: When the QoS of the first thread is higher than the QoS of the second thread, the QoS of the second thread is set to the QoS of the first thread.

5. The method according to any one of claims 1 to 4, characterized in that Before determining the second thread, the method further includes: The second thread that obtains the target read-write lock in read mode is recorded.

6. The method according to any one of claims 1 to 5, characterized in that The target read-write lock includes mmap-lock.

7. A thread scheduling method, characterized in that: The method comprises: When detecting that the first thread requests the target variable, determining a second thread, the second thread being a thread that already holds the target variable; Determining a scheduling attribute of the first thread and a scheduling attribute of the second thread; When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, the scheduling attribute of the second thread is set to the scheduling attribute of the first thread, so that the second thread schedules the target variable according to the scheduling attribute of the first thread.

8. The method according to claim 7, characterized in that The scheduling attributes include priority; When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, setting the scheduling attribute of the second thread to the scheduling attribute of the first thread includes: When the priority of the first thread is higher than the priority of the second thread, the priority of the second thread is set to the priority of the first thread.

9. The method according to claim 7 or 8, characterized in that: The scheduling attributes include load; When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, setting the scheduling attribute of the second thread to the scheduling attribute of the first thread includes: When the load of the first thread is higher than the load of the second thread, the load of the second thread is set to the load of the first thread.

10. The method according to any one of claims 7 to 9, characterized in that The scheduling attributes include quality of service QoS; When the scheduling attribute of the first thread is higher than the scheduling attribute of the second thread, setting the scheduling attribute of the second thread to the scheduling attribute of the first thread includes: When the QoS of the first thread is higher than the QoS of the second thread, the QoS of the second thread is set to the QoS of the first thread.

11. The method according to any one of claims 7 to 10, characterized in that Before determining the second thread, the method further includes: The second thread that obtains the target variable is recorded.

12. The method according to any one of claims 7 to 11, characterized in that The target variable includes a waitbit variable.

13. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the electronic device implements the thread scheduling method according to any one of claims 1 to 6 or claims 7 to 12.

14. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a computer, the computer is enabled to implement the thread scheduling method according to any one of claims 1 to 6 or claims 7 to 12.