Thread scheduling method, electronic equipment and computer readable storage medium
By recording the blocking relationship between threads and prioritizing the scheduling of threads that need to be locked quickly and the threads with the longest waiting time, the problem of long-term blocking of threads is solved, thread scheduling is optimized, and system efficiency is improved.
Patent Information
- Application Number
- CN202410205490.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-02-23
- Publication Date
- 2025-01-07
- Estimated Expiration
- 2044-02-23
AI Technical Summary
The existing thread scheduling scheme cannot effectively solve the problem of long-term thread blocking, affecting the overall process of the application.
By recording the blocking relationship between threads in the operating system, priority is given to scheduling threads that require fast locking and threads with the longest waiting time, and use marking and automatic wake-up mechanisms to optimize thread scheduling and reduce waiting time.
It effectively avoids long-term blockage of threads, optimizes the overall process of the application, reduces lag time, and improves system efficiency.
Smart Images

Figure CN119271364A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of terminal technology, and in particular to a thread scheduling method, an electronic device, and a computer-readable storage medium. Background Art
[0002] There are multiple threads running in the electronic device, and the threads may be blocked due to reasons such as waiting for thread locks. When any thread is waiting to acquire a thread lock, it will first enter the waiting queue of the corresponding thread lock. When the current thread holding the thread lock releases the thread lock, it will query one or more threads waiting to hold the lock in the waiting queue of the thread lock, and wake up one or more threads in the waiting queue in the order of waiting for the thread lock to hold the thread lock.
[0003] The existing thread scheduling scheme can only be based on the waiting queue of thread locks and schedule them in the order of applying for thread locks. It cannot effectively solve the technical problem of long-term thread blocking and affects the overall application process. 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 are used to solve the technical problem of long-term thread blocking.
[0005] To achieve the above objectives, the embodiments of the present application adopt the following technical solutions:
[0006] In the first aspect, a thread scheduling method is provided, which is applied to electronic devices, which may be mobile phones, tablets and other devices. The electronic device includes an operating system, which runs on a processor of the electronic device. The operating system can create threads and schedule threads. In the thread scheduling method provided in the present application, the operating system prioritizes some threads according to the blocking relationship between threads, so as to avoid long-term blocking caused by waiting for thread locks and other reasons as much as possible, and optimize the overall application process. In the thread scheduling method provided in this embodiment, the main steps can be executed by the operating system, and some steps can also be executed by threads created by the operating system.
[0007] There is a first thread among multiple threads created by the operating system. The first thread waits to hold the first thread lock, and the first thread is in the waiting queue of the first thread lock. The current thread holding the first thread lock is the second thread, and there may be other threads in the waiting queue of the first thread lock. In the waiting queue of the first thread lock, some of the other threads may be located before the first thread, thus blocking the first thread. The first thread may need to be scheduled as soon as possible due to a high priority or other reasons. If the first thread is blocked for a long time, it will have a greater impact on the overall application process, that is, the first thread is the thread that needs to be locked first.
[0008] The operating system may search for the first thread that is blocked by other threads and needs to be locked with priority in the waiting queue of the first thread lock. For example, the operating system may search for the first thread that is blocked by other threads and needs to be locked with priority based on factors such as thread priority and a predefined priority lock judgment rule. Then, the operating system may add a first mark to the first thread, and the first mark indicates that the first thread is a thread that needs to be locked with priority.
[0009] In this way, when the second thread releases the first thread lock, the first thread with the first mark can preferentially acquire the first thread lock. The first thread preferentially acquires the first thread lock here refers to the time when other threads before the first thread acquire the first thread in the first thread lock.
[0010] There are many ways for the operating system to find the blocking relationship between threads. For example, the operating system can query the blocking relationship between threads based on the waiting queue of each thread lock or the thread lock application record. For another example, the operating system can also create a data set to record the blocking relationship between threads at different times. The corresponding blocking relationship between threads can be found by querying the element value in the data set. The data set used can be a data structure such as a table or a bitmap.
[0011] The thread scheduling method provided in the present application can find out the first threads that need to be locked first among all the threads in the electronic device and add a first mark to them. In this way, the first threads with the first mark added can be locked first, so as to avoid the influence of long-term blocking on the overall process of the application as soon as possible, thereby optimizing the thread scheduling scheme.
[0012] In a possible implementation of the first aspect, the operating system searches for a first thread that is blocked by other threads and needs to be locked with priority; the operating system may find the first thread, in which case the operating system may add a first tag to the first thread. Of course, the operation may not find the first thread. If the first thread that needs to be locked with priority is not found, the operating system may search for the thread with the longest waiting time to lock with priority. In this way, the operating system can not only allow the first thread that needs to be locked with priority to be locked, but also allow the thread with a longer waiting time to be locked with priority.
[0013] The operating system searches for the third thread with the longest waiting time among all threads in the waiting queue of the first thread lock. If the waiting queue of the first thread lock is a first-in-first-out queue, the operating system can use the thread with the highest ranking as the third thread according to the order of the threads in the waiting queue of the first thread lock. The operating system can also search for the third thread with the earliest application time, that is, the longest waiting time, according to the time when each thread applies for the first thread lock.
[0014] The operating system searches for the third thread with the longest waiting time among all threads in the waiting queue of the first thread lock, and adds a second mark to the third thread, wherein the second mark indicates that the third thread is the thread with the longest waiting time. In this way, when the second thread currently holding the first thread lock releases the first thread lock, the third thread with the second mark added can obtain the first thread lock.
[0015] The thread scheduling method provided in this embodiment is that when the operating system is scheduling threads, if there is no thread that needs to be locked first, the operating system will prioritize scheduling the thread that has been waiting for the longest time for the thread lock, so as to avoid some threads waiting too long for obtaining the thread lock, so as to optimize the overall application process.
[0016] In a possible implementation of the first aspect, the operating system adds a first mark to indicate the first thread that needs priority locking. Specifically, the operating system sets a priority lock pointer, which points to the thread that needs priority locking, for marking the thread that needs priority locking.
[0017] The operating system points the priority lock pointer to the first thread in the waiting queue of the first thread lock. When the current lock holding thread of the first thread lock, i.e., the second thread, releases the first thread lock, the second thread wakes up the first thread pointed to by the priority lock pointer, so that the first thread acquires the first thread lock.
[0018] In a possible implementation of the first aspect, the provided thread scheduling method also includes a solution for automatically waking up threads. There are two conditions required for a thread that needs to be locked to run: one is to hold the corresponding thread lock, and the other is to be scheduled by the operating system. Some threads with lower priorities cannot obtain computing resources due to their low priority even if they obtain the corresponding thread lock, and they cannot be scheduled by the operating system. Even if they are locked, they will be in a dormant state and cannot run. Some threads with higher priorities can obtain computing resources, but they need to wait to obtain the thread lock and cannot run. The thread scheduling solution provided in this implementation matches the automatic wake-up function for threads with higher priorities waiting to obtain thread locks. These threads can wake up threads that are locked but in a dormant state, so that threads that are locked but in a dormant state are scheduled first.
[0019] Specifically, if the fourth thread created by the operating system has a higher priority or other attribute parameters match, the operating system can configure an automatic wake-up function for the fourth thread. In this way, if the fourth thread applies for the second thread lock and enters the waiting queue of the second thread lock, the operating system matches the automatic wake-up function for the fourth thread, so that the fourth thread can automatically wake up at multiple automatic wake-up times after entering the waiting queue of the second thread lock.
[0020] In a specific implementation, the specific implementation method of configuring the automatic wake-up function for the thread by the operating system may be a timer. For example, if the fourth thread is the main thread, the operating system may configure a timer corresponding to the automatic wake-up function for the fourth thread. The timer sends a timed wake-up message to the fourth thread at each automatic wake-up time, and the timed wake-up message is used to wake up the fourth thread at each automatic wake-up time.
[0021] At each automatic wake-up moment, the fourth thread wakes itself up and wakes up the fifth thread that is currently locked but not running. The fifth thread refers to the thread that currently holds the second thread lock and is directly or indirectly blocked waiting for the second thread to lock the fourth thread. After the fourth thread finds the fifth thread that currently holds the second thread lock, if the fifth thread has not been scheduled, the fourth thread adds a third mark to the fifth thread so that the operating system preferentially schedules the fifth thread with the third mark added. In this way, the fourth thread wakes up the fifth thread that currently holds the second thread lock, so that the fifth thread is preferentially scheduled, and the fourth thread waiting to obtain the second thread lock can also obtain the second thread lock relatively early.
[0022] In a possible implementation of the first aspect, a scheme in which the fourth thread searches for the fifth thread at different wake-up times is further defined. The timer corresponding to the automatic wake-up function matched by the operating system for the fourth thread corresponds to multiple automatic wake-up times, and there is a certain time interval between adjacent automatic wake-up times, and the time interval between adjacent automatic wake-up times can be a fixed time. For ease of description, it is defined as the first automatic wake-up time and the second automatic wake-up time. In the actual scheme, from the time when the fourth thread applies for the second thread lock and enters the waiting queue of the second thread lock to the time when the fourth thread obtains the second thread lock, the timer can determine an automatic wake-up time at intervals of a fixed time, and the number of automatic wake-up times can be multiple.
[0023] At the first automatic wake-up moment, the fourth thread searches for the lock-holding thread locked by the second thread at the first automatic wake-up moment as the fifth thread corresponding to the first automatic wake-up moment. At the second automatic wake-up moment, the fourth thread searches for the lock-holding thread locked by the second thread at the second automatic wake-up moment as the fifth thread corresponding to the second automatic wake-up moment. The fifth thread corresponding to the first automatic wake-up moment and the fifth thread corresponding to the second automatic wake-up moment may be the same thread or different threads.
[0024] That is to say, the fourth thread will wake itself up at every automatic wake-up moment and search for the thread currently holding the second thread lock. If the thread currently holding the second thread lock is not scheduled, the fourth thread will wake up the thread currently holding the second thread lock and not scheduled so that it can be scheduled first.
[0025] If the lock holding thread at the first automatic wake-up time is still in a locked but unscheduled state at the second automatic wake-up time, the lock holding thread awakened by the fourth thread at the second automatic wake-up time is still the same thread awakened at the first automatic wake-up time.
[0026] If the locked thread at the first automatic wake-up time has been scheduled or the second thread lock has been released before the second automatic wake-up time, then the locked thread awakened by the fourth thread at the second automatic wake-up time is different from the locked thread awakened at the first automatic wake-up time.
[0027] In a possible implementation of the first aspect, the implementation scheme of the fourth thread searching for the fifth thread is limited. Specifically, the fourth thread first searches for the sixth thread that directly blocks the fourth thread, and the sixth thread may have acquired the second thread lock, or may also be blocked by other threads, that is, it has not yet acquired the second thread lock.
[0028] In one case, if the sixth thread that directly blocks the fourth thread acquires the second thread lock, the fourth thread can use the sixth thread as the fifth thread, and then determine whether to wake up the fifth thread according to the scheduling state of the fifth thread.
[0029] In another case, if the sixth thread that directly blocks the fourth thread does not obtain the second thread lock, the fourth thread needs to continue to search for the seventh thread that directly blocks the sixth thread, and then process it according to the lock status of the seventh thread. If the seventh thread obtains the second thread lock, the seventh thread is used as the fifth thread, and the fourth thread determines whether to wake up the fifth thread or the seventh thread according to the scheduling status of the fifth thread or the seventh thread. If the seventh thread also does not obtain the second thread lock, the fourth thread needs to further search for other threads that directly block the seventh thread, that is, recursively search in sequence according to the blocking relationship between threads until the thread that indirectly blocks the fourth thread and holds the second thread lock is found as the fifth thread.
[0030] This implementation method searches for a thread that directly blocks or indirectly blocks the fourth thread and holds the second thread lock, and can effectively wake up the currently locked thread to be scheduled preferentially, so as to speed up the overall application process.
[0031] In a possible implementation of the first aspect, a scheme for recording blocking relationships between threads through a bitmap is defined. An electronic device stores a bitmap; wherein the bitmap records the blocking relationships between all threads in the electronic device. The bitmap includes a row-column matrix, and the element value of the mth row and nth column in the row-column matrix indicates the blocking relationship between thread m and thread n, and m and n are both thread numbers. The element value is a first value indicating that there is no blocking between threads, and the element value is a second value indicating that there is blocking between threads. In a possible implementation of the first aspect, the first value is 0 and the second value is 1.
[0032] For example, if the value of the element in the 2nd column of the 1st row is 0, it indicates that thread 1 is not blocked by thread 2, and if the value of the element in the 2nd column of the 1st row is 1, it indicates that thread 1 is blocked by thread 2. For another example, if the value of the element in the 1st column of the 2nd row is 0, it indicates that thread 2 is not blocked by thread 1, and if the value of the element in the 1st column of the 2nd row is 1, it indicates that thread 2 is blocked by thread 1.
[0033] Specifically, the thread scheduling method provided by the electronic device may specifically include the following schemes for querying the blocking relationship between threads through a bitmap: the operating system queries the element value in the first target row corresponding to the first thread in the bitmap to obtain the blocking relationship between threads in the electronic device. There are multiple column element values in the first target row, and different column element values indicate situations in which threads with corresponding numbers block the first thread. It should be noted that the first thread here is not limited to thread 1, the first target row is not limited to the first row in the bitmap, the subsequent second thread, etc. is not limited to thread 2, and the second target column is not limited to the second column in the bitmap, and the subsequent related descriptions are similar.
[0034] In a possible implementation of the first aspect, a specific acquisition scheme for a bitmap recording inter-thread blocking relationships is further limited. The operating system of the electronic device creates a bitmap, and the thread whose inter-thread blocking relationship changes actively modifies the element value in the bitmap to update the inter-thread blocking relationship in real time. Other threads can search for element values in each row and column of the bitmap to query the real-time inter-thread blocking relationship, including their own inter-thread blocking relationship and the inter-thread blocking relationship associated with other threads.
[0035] For ease of description, the following will explain from the perspective of the eighth thread how to modify the element value in the bitmap when the blocking relationship between threads associated with the thread changes, especially the implementation scheme when the three main nodes of applying for thread locks, acquiring thread locks, and releasing thread locks involve changes in the blocking relationship between threads. The eighth thread can be any thread created by the operating system.
[0036] Specifically, the scheme involving bitmap creation and modification in the thread scheduling method provided in this embodiment may include: the operating system creates a bitmap and sets the element value in the bitmap to a first value. The eighth thread modifies the element value in the bitmap according to the change of the blocking relationship between threads.
[0037] The operating system may create a bitmap when generating the first thread, and the number of rows and columns of the bitmap may be equal to the maximum number of threads that the operating system can create. When the operating system creates the bitmap, there is no blocking between threads, and the operating system may first set all element values in the bitmap to a first value, which indicates that there is no blocking between threads.
[0038] When the eighth thread applies for a thread lock, a blocking relationship between threads is added. When the eighth thread releases a thread lock, the blocking relationship between threads is reduced. The solution of recording the blocking relationship between threads through a bitmap is simple, fast and easy to find. It does not require adding additional members to the task structure. It is applicable to multiple types of thread locks and does not require periodic judgment of the blocking state. The operating system can effectively identify blocked threads, quickly schedule them, and reduce the time of application freezes.
[0039] In a possible implementation of the first aspect, a scheme for modifying element values in a bitmap involved in applying for a thread lock is defined. The step of the eighth thread modifying the element values in the bitmap according to the change in the blocking relationship between threads may specifically include: when the eighth thread applies for the third thread lock at the first moment, searching for the ninth thread that blocks the eighth thread; and the eighth thread modifies the element values of the second target columns corresponding to each ninth thread in the first target row corresponding to the eighth thread in the bitmap to the second value.
[0040] The eighth thread applies for the third thread lock at the first moment. The third thread lock is currently held by other threads, and there are other threads in the waiting queue of the third thread lock that have previously applied to obtain the third thread lock. The lock-holding thread of the third thread lock and all threads in the waiting queue of the third thread lock block the eighth thread from obtaining the third thread lock, and is recorded as the ninth thread.
[0041] When the eighth thread applies for the third thread lock, it searches for all the ninth threads that block the eighth thread. To record the relationship of the newly added ninth thread blocking the eighth thread, the eighth thread modifies the element value of the second target column corresponding to each ninth thread in the first target row corresponding to the eighth thread in the bitmap to the second value. In this way, the eighth thread or other threads can query the element values of all columns in the first target row to obtain which threads block the eighth thread and determine the blocking relationship between threads associated with the eighth thread.
[0042] In a possible implementation of the first aspect, a scheme for modifying the element value in the bitmap involved in acquiring the thread lock is defined. After the eighth thread modifies the element value of the second target column corresponding to each ninth thread in the first target row corresponding to the eighth thread in the bitmap to the second value, when the eighth thread acquires the third thread lock at the second moment, the ninth thread that previously held the third thread lock and applied for the third thread lock first no longer continues to block the eighth thread. The eighth thread modifies the element value of each second target column in the first target row in the bitmap to the first value to indicate that the ninth thread that previously blocked the eighth thread no longer continues to block the eighth thread.
[0043] In addition, when the eighth thread acquires the third thread lock, it will block other threads currently waiting to acquire the third thread lock, and the eighth thread can also record the newly added blocking relationship between threads. Specifically, when the eighth thread acquires the third thread lock, it first searches for all threads in the waiting queue of the third thread lock at the second moment, and records them as the tenth thread. The eighth thread modifies the element value of the third target row corresponding to each tenth thread in the first target column of the bitmap to the second value. In this way, the eighth thread can query the element value of each row in the first target column to obtain which threads are blocked by the eighth thread and determine the blocking relationship between threads associated with the eighth thread.
[0044] In a possible implementation of the first aspect, a scheme for modifying element values in a bitmap involved in releasing thread locks is defined. After the eighth thread modifies the element values of the third target rows corresponding to the tenth thread in the first target column of the bitmap to the second value, when the eighth thread releases the first thread lock at the third moment, the eighth thread no longer continues to block the tenth thread currently waiting to acquire the third thread lock. The eighth thread modifies the element values of the third target rows in the first target column of the bitmap to the first value to indicate that the eighth thread that previously blocked the tenth thread no longer continues to block the tenth thread.
[0045] In a second aspect, the present application provides an electronic device, which includes a memory and a processor, wherein an operating system of the electronic device runs on the processor, and the memory is coupled to the processor; the memory stores computer execution instructions; the operating system schedules the processor to execute the computer execution instructions stored in the memory, so that the electronic device executes a thread scheduling method as described in any one of the first aspects.
[0046] In a possible implementation manner of the second aspect, the operating system is used to:
[0047] In the waiting queue of the first thread lock, search for the first thread; wherein the first thread refers to a thread that is blocked by other threads and needs to be locked first among all threads in the waiting queue of the first thread lock, and the current locking thread of the first thread lock is the second thread;
[0048] The operating system adds a first marker to the first thread;
[0049] The second thread releases the first thread lock, and the first thread added with the first mark acquires the first thread lock.
[0050] In a possible implementation manner of the second aspect, the operating system is further used for:
[0051] If the first thread is not found, the operating system searches for a third thread in the waiting queue of the first thread lock; wherein the third thread refers to a thread with the longest waiting time among all threads in the waiting queue of the first thread lock;
[0052] The operating system adds a second marker to the third thread;
[0053] The second thread releases the first thread lock, and the third thread with the second mark added acquires the first thread lock.
[0054] In a possible implementation of the second aspect, the operating system adds a first mark to the first thread, the second thread releases the first thread lock, and the first thread with the first mark acquires the first thread lock includes:
[0055] The operating system points the priority lock pointer to the first thread in the waiting queue of the first thread lock; wherein the priority lock pointer points to the first thread indicating that the first thread has added a first mark;
[0056] When the second thread releases the first thread lock, the second thread wakes up the first thread pointed to by the priority lock pointer, so that the first thread acquires the first thread lock.
[0057] In a possible implementation of the second aspect, the thread scheduling method further includes:
[0058] The fourth thread applies for the second thread lock and enters the waiting queue of the second thread lock; the fourth thread has the function of automatically waking up at multiple automatic wake-up times;
[0059] At each automatic wake-up moment, the fourth thread searches for the fifth thread; wherein the fifth thread is the current lock-holding thread of the second thread lock, and the fifth thread directly or indirectly blocks the fourth thread;
[0060] If the fifth thread is not scheduled, the fourth thread adds a third mark to the fifth thread;
[0061] The electronic device preferentially schedules the fifth thread to which the third mark is added.
[0062] In a possible implementation manner of the second aspect, at each automatic wake-up moment, the fourth thread, according to the step of searching for the fifth thread, includes:
[0063] At the first automatic wake-up moment, the fourth thread searches for the locked thread locked by the second thread at the first automatic wake-up moment as the fifth thread corresponding to the first automatic wake-up moment;
[0064] At the second automatic wake-up moment, the fourth thread searches for the lock-holding thread locked by the second thread at the second automatic wake-up moment as the fifth thread corresponding to the second automatic wake-up moment;
[0065] The fifth thread corresponding to the first automatic awakening moment and the fifth thread corresponding to the second automatic awakening moment are the same thread or different threads.
[0066] In a possible implementation of the second aspect, the step of the fourth thread searching for the fifth thread includes:
[0067] The fourth thread searches for the sixth thread that directly blocks the fourth thread;
[0068] If the sixth thread directly blocks the fourth thread from acquiring the second thread lock, the sixth thread is used as the fifth thread;
[0069] If the sixth thread that directly blocks the fourth thread does not acquire the second thread lock, find the seventh thread that directly blocks the sixth thread;
[0070] If the seventh thread acquires the second thread lock, the seventh thread is used as the fifth thread.
[0071] In a possible implementation manner of the second aspect, before the step of the fourth thread applying for the second thread lock and entering the waiting queue of the second thread lock, the method further includes:
[0072] If the fourth thread is the main thread, the operating system configures a timer corresponding to the automatic wake-up function for the fourth thread;
[0073] The timer sends a timed wake-up message to the fourth thread at each automatic wake-up moment; the timed wake-up message is used to wake up the fourth thread at each automatic wake-up moment.
[0074] In a possible implementation of the second aspect, the electronic device stores a bitmap; wherein the bitmap records the blocking relationship between all threads in the electronic device, the bitmap includes a row-column matrix, the element value of the mth row and the nth column in the row-column matrix indicates the blocking relationship between thread m and thread n, the element value is a first value indicating that the threads are not blocked, the element value is a second value indicating that the threads are blocked, and m and n are both thread numbers;
[0075] Thread scheduling methods also include:
[0076] The operating system queries the element value in the first target row corresponding to the first thread in the bitmap to obtain the blocking relationship between threads in the electronic device.
[0077] In a possible implementation of the second aspect, the thread scheduling method further includes:
[0078] The operating system creates a bitmap and sets the element value in the bitmap to the first value;
[0079] The eighth thread modifies the element value in the bitmap according to the change of the blocking relationship between threads; wherein the eighth thread is the thread whose blocking relationship between threads changes.
[0080] In a possible implementation manner of the second aspect, the step of the eighth thread modifying the element value in the bitmap according to the change of the blocking relationship between threads includes:
[0081] When the eighth thread applies for the third thread lock at the first moment, it searches for the ninth thread; wherein the ninth thread is all threads that block the eighth thread at the first moment, and the ninth thread includes the lock holding thread of the third thread lock at the first moment and all threads in the waiting queue of the third thread lock;
[0082] The eighth thread modifies the element value of the second target column corresponding to each ninth thread in the first target row corresponding to the eighth thread in the bitmap to a second value.
[0083] In a possible implementation manner of the second aspect, after the eighth thread modifies the element value of the second target column corresponding to each ninth thread in the first target row corresponding to the eighth thread in the bitmap to a second value, the thread scheduling method further includes:
[0084] When the eighth thread acquires the lock of the third thread at the second moment, the eighth thread modifies the element value of each second target column in the first target row of the bitmap to the first value;
[0085] The eighth thread searches for the tenth thread; wherein the tenth thread is all threads in the waiting queue locked by the third thread at the second moment;
[0086] The eighth thread modifies the element value of the third target row corresponding to each tenth thread in the first target column of the bitmap to a second value.
[0087] In a possible implementation manner of the second aspect, after the eighth thread modifies the element value of the third target row corresponding to each tenth thread in the first target column of the bitmap to the second value, the thread scheduling method further includes:
[0088] When the eighth thread releases the lock of the first thread at the third moment, the eighth thread modifies the element value of each third target row in the first target column in the bitmap to the first value.
[0089] In a possible implementation of the second aspect, the first value is 0 and the second value is 1.
[0090] In a third aspect, an electronic device is provided, which has the function of implementing the thread scheduling method of the first aspect. The function can be implemented by hardware, or by hardware executing corresponding software. The hardware or software includes one or more modules corresponding to the above functions.
[0091] In a fourth aspect, a computer-readable storage medium is provided, wherein instructions are stored in the computer-readable storage medium, and when the computer-readable storage medium is run on a computer, the computer can execute any one of the thread scheduling methods in the first aspect.
[0092] In a fifth aspect, a computer program product is provided, including a computer program, and when the computer program is executed by a processor, the thread scheduling method as described in any one of the first aspects is implemented.
[0093] Among them, the technical effects brought about by any design method in the second to fifth aspects can refer to the technical effects brought about by different design methods in the first aspect, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0094] Figure 1 A schematic diagram of the waiting queue for the thread lock;
[0095] Figure 2 A flowchart of a thread scheduling method provided in an embodiment of the present application;
[0096] Figure 3 A schematic diagram of a process of using a bitmap to record blocking relationships between threads involved in a thread scheduling method provided in an embodiment of the present application;
[0097] Figure 4 A schematic diagram of a process for obtaining thread locks in priority involved in a thread scheduling method provided in an embodiment of the present application;
[0098] Figure 5 A schematic diagram of a thread lock and a priority lock pointer involved in a thread scheduling method provided in an embodiment of the present application;
[0099] Figure 6 A schematic diagram of a process of prioritizing thread scheduling involved in a thread scheduling method provided in an embodiment of the present application;
[0100] Figure 7 A schematic diagram of a thread lock and a priority scheduling pointer involved in a thread scheduling method provided in an embodiment of the present application;
[0101] Figure 8 A schematic diagram of the process flow of the fast path and the slow path involved in the thread scheduling method provided in an embodiment of the present application;
[0102] Fig. 9 A schematic diagram comparing the effects before and after thread scheduling involved in the thread scheduling method provided in an embodiment of the present application;
[0103] Fig.10 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0104] The following is a description of exemplary embodiments of the present application in conjunction with the accompanying drawings, including various details of the embodiments of the present application to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the present application. Similarly, for the sake of clarity and conciseness, the description of well-known functions and structures is omitted in the following description.
[0105] To facilitate understanding, some technical common sense involved in the embodiments of the present application is first introduced.
[0106] Electronic devices include an operating system (OS) and a central processing unit (CPU). The operating system is mainly used to manage various computing tasks in electronic devices, responsible for scheduling computing tasks and allocating computing resources, and the CPU is mainly used to execute various computing tasks. There are multiple applications running on the operating system, and different applications have different functions.
[0107] A process is the carrier of application running and an independent unit for resource allocation and task scheduling of the operating system. A process is an execution process of a program. A thread is a single sequential control flow in program execution. It is the smallest unit of program execution flow and the basic unit of CPU scheduling and dispatching. A process can have one or more threads. A thread usually consists of a thread number (Identity Document, ID), a current instruction pointer (Program Counter, PC), registers and a stack. Electronic devices can refer to and distinguish each thread by thread ID. Thread ID starts from 0 and is numbered in the order in which the threads are created. The thread ID in electronic devices is usually between 0 and 32767.
[0108] When an application starts, the operating system creates a process and starts running a thread, which is called the main thread. The main thread creates multiple child threads. Within a process, the priority of the main thread is relatively high, and the priority of the child thread is usually lower than that of the main thread.
[0109] A process can include memory space allocated by the operating system and multiple threads created. All threads in a process can access the resources in the memory space corresponding to the process. Each thread of the process accesses the resources in the memory space and can perform read and modify operations on the resources. The modification operations can be operations such as adding, modifying and deleting. In order to avoid errors that may be caused when multiple threads access and occupy the same resource, electronic devices can also introduce thread locks. Thread locks are a thread control tool that is used to control the access rights and order of multiple threads to shared resources.
[0110] For example, thread A acquires thread lock 1 before starting to access shared resource 1, and releases thread lock 1 after finishing accessing shared resource 1; during the period when thread A holds thread lock 1, the shared resource is usually not accessed by other threads. Similarly, if thread B needs to access shared resource 1, thread B also needs to acquire thread lock 1 first. If thread lock 1 is held by thread A when thread B applies for thread lock 1, thread B needs to wait until thread A finishes accessing shared resource 1 and releases thread lock 1 before it can acquire thread lock 1 and access shared resource 1.
[0111] The types of thread locks mainly include: read-write lock (RWsem Lock), mutual exclusion lock (Mutex), etc. Read-write locks are used to control the read and write functions of threads on shared resources. Read-write locks can allow multiple threads to read the same resource at the same time, but only allow one thread to write to the same resource at the same time.
[0112] Mutex locks are used to restrict threads' access to shared resources. When the current thread holds a mutex lock, only the current thread can access the shared resource. Other threads need to wait until the current thread releases the mutex lock before they can acquire the mutex lock or the lock to access the shared resource.
[0113] For example, if the current thread applies for a mutex lock and the mutex lock is held by other threads, the current thread will give up the CPU, and the CPU can first execute the computing tasks of other threads. When the current thread obtains the mutex lock, it will occupy the CPU to execute computing tasks.
[0114] During the operation of the electronic device, the types and numbers of thread locks introduced are different, and the types and numbers of threads applying for each thread lock may also be different. The operating system can create a corresponding waiting queue for each thread lock, and the waiting queue includes threads waiting to obtain the thread lock.
[0115] For example Figure 1 As shown in (a), thread 1 holds thread lock 1, and threads 2, 3, 4, 5, etc. are waiting to acquire thread lock 1. Threads 2, 3, 4, 5, etc. are recorded in the waiting queue of thread lock 1. In other words, thread 1 holds the thread lock, and threads 2, 3, 4, 5, etc. are blocked by thread 1 because they are waiting to acquire thread lock 1.
[0116] Another example Figure 1 As shown in (b), thread 7 holds thread lock 2. The waiting queue of thread lock 2 includes threads 10, 13, 14, and 15. Threads 10, 13, 14, and 15 are blocked by thread 7 because they are waiting to acquire thread lock 2.
[0117] During the operation of an electronic device, the reason why a thread is in a blocked state may also include waiting for external resources, other threads or trigger conditions, and waiting for a thread lock is a major cause of thread blocking. It should be noted that the situation where waiting for a thread lock causes thread blocking not only occurs between multiple threads in the same process, but also between multiple threads in different processes. The thread blocking involved in the embodiments of the present application may refer to the blocking of multiple threads in the same process, or the blocking of multiple threads in different processes, without limitation.
[0118] The thread scheduling scheme implemented by the existing electronic device includes: when any thread is waiting to acquire a thread lock, it will first enter the waiting queue of the corresponding thread lock. When the current thread holding the thread lock releases the thread lock, it will query one or more threads waiting to hold the lock in the waiting queue of the thread lock, and wake up one or more threads in the waiting queue in the order of waiting for the thread lock to hold the thread lock. The existing thread scheduling scheme can only be based on the waiting queue of the thread lock, and is scheduled in sequence according to the order of applying for the thread lock. It cannot effectively solve the technical problem of long-term thread blocking, which affects the process.
[0119] For example, thread 1, which currently holds the thread lock, has a low priority and has difficulty obtaining computing resources allocated by the operating system. Thread 2, which is waiting to obtain the thread lock, has a high priority, but enters a long blocking state because it needs to wait for a long time for thread 1 to release the thread lock. Generally speaking, the blocking time of a thread with a higher priority has a greater impact on the process.
[0120] Based on this, an embodiment of the present application provides a thread scheduling method, which is applied to an electronic device. The electronic device records the blocking relationship between threads, and optimizes the thread scheduling scheme by querying the blocking relationship between threads to speed up the process. The electronic device to which the thread scheduling method provided in this embodiment is applied may include a personal computer (PC), a tablet computer, a laptop computer, a portable computer (such as a mobile phone), a wearable electronic device (such as a smart watch), an augmented reality (AR) device, a virtual reality (VR) device, an on-board computer, and other electronic devices with a processor. The following embodiments do not impose any special restrictions on the specific form of the electronic device.
[0121] like Figure 2 , which is a flow chart of a thread scheduling method provided in an embodiment of the present application. The thread scheduling method provided in this embodiment is mainly executed by an operating system of an electronic device. Specifically, the thread scheduling method provided in this embodiment mainly includes the following two operations:
[0122] S21: The operating system records the blocking relationship between threads;
[0123] S22: The operating system optimizes the thread scheduling scheme based on the blocking relationship between threads.
[0124] The process of the electronic device performing the above two operations will be described in detail below.
[0125] In the first part, the electronic device executes S21, and the operating system of the electronic device can use multiple methods to record the blocking relationship between threads.
[0126] Multiple threads are running simultaneously in an electronic device. Each thread may be blocked by other threads or each thread may block other threads due to factors such as waiting for thread locks, external resources, trigger conditions, etc. In addition, the blocking relationship between threads may be the same or different at different times.
[0127] For example, at the first moment, based on thread lock 1, thread A blocks thread B, and thread B blocks thread C. At the second moment after the first moment, based on thread lock 1, thread B blocks thread C. At the third moment after the second moment, based on thread lock 2, thread C blocks thread A and thread B.
[0128] The blocking relationship between threads recorded by the electronic device may include: whether each thread is blocked by other threads at the current moment, and whether each thread blocks other threads. The electronic device may use a data set in one or more formats such as a table, a mapping table, an array, etc. to record the blocking relationship between threads. When the electronic device records the blocking relationship between threads in the data set, each thread may be referred to by its ID.
[0129] In one implementation, the electronic device may record the blocking relationship between threads through the waiting queue of each thread lock.
[0130] Assume a thread lock, recorded as the first thread lock. The first thread lock is currently held by the first thread, and there are one or more threads waiting to obtain the first thread lock in the waiting queue of the first thread lock, recorded as the second thread. The first thread blocks each second thread, and each second thread is blocked by the first thread. The electronic device queries the lock-holding thread of each thread lock and the threads in the waiting queue, and can also obtain the blocking relationship between threads.
[0131] In another specific implementation, the electronic device may use a bitmap as a data set for recording blocking relationships between threads.
[0132] A bitmap is a data structure consisting of a row-column matrix, where the value of each element in the row-column matrix is 0 or 1. The electronic device can set the number of rows and columns of the row-column matrix according to the size of the required data set, and record multiple data through the element values in the row-column matrix.
[0133] In one example, the electronic device creates a row and column matrix of a bitmap, the row sequence number and the sequence number both correspond to the thread ID, and the element value corresponds to the blocking state between threads.
[0134] The number of threads in the electronic device is recorded as A. All threads can be sorted according to thread ID as follows: thread 0, thread 1, thread 2, ..., thread (A-2) and thread (A-1). The electronic device can use an A*A row-column matrix to represent the blocking relationship between threads. The element value of each row-column matrix is 0 or 1, where 0 means no blocking and 1 means blocking.
[0135] For example, the i-th row, the k-th column indicates the state where thread i is blocked by thread k. If the value of the element in the i-th row, the k-th column is 1, it indicates that thread i is blocked by thread k. If the value of the element in the i-th row, the k-th column is 0, it indicates that thread i is not blocked by thread k.
[0136] For another example, the kth row, i-th column indicates the state that thread k is blocked by thread i. If the element value of the kth row, i-th column is 1, it means that thread k is blocked by thread i. If the element value of the kth row, i-th column is 0, it means that thread k is not blocked by thread i.
[0137] As shown in Table 1, it is an example of a bitmap for recording the blocking relationship between threads in an electronic device. For ease of understanding, in this example, it is assumed that the number of threads A is 10, and the electronic device records the blocking relationship between threads through a 10*10 row and column matrix.
[0138] Table 1
[0139]
[0140] As shown in Table 1, the values of the elements in the 0th row and columns indicate the state of thread 0 being blocked by other threads, and the data in the 0th column and rows indicate the blocking state of thread 0 to other threads. The element value 0 indicates no blocking, and 1 indicates blocking. It should be noted that, for any i<10, the element value of the i-th row and i-th column is 0, that is, the thread will not be blocked by itself.
[0141] For example, in row 1, the values of the elements corresponding to columns 0, 2, 3, and 6 are 1, indicating that thread 1 is blocked by threads 0, 2, 3, and 6. In row 1, the values of the elements corresponding to columns 1, 4, 5, 7, 8, and 9 are 0, indicating that thread 1 is not blocked by threads 4, 5, 7, 8, and 9.
[0142] For another example, in column 4, the element values corresponding to rows 3, 6, and 7 are 1, indicating that thread 4 blocks threads 3, 6, and 7. In column 4, the element values corresponding to rows 0, 1, 2, 4, 5, 8, and 9 are 0, indicating that thread 4 does not block threads 0, 1, 2, 4, 5, 8, and 9.
[0143] In another specific example, the electronic device may also update the element values in the bitmap in real time to update the recorded blocking relationship between threads.
[0144] As shown in Table 1 above, it is a bitmap corresponding to the blocking relationship between threads at the first moment recorded by the electronic device. As shown in Table 2 below, it can be a bitmap corresponding to the blocking relationship between threads at the second moment recorded by the electronic device. The second moment is a moment after the first moment.
[0145] Table 2
[0146]
[0147] As shown in Table 2, relative to Table 1, the element value of row 0, column 7 changes from 1 to 0, and the element value of row 0, column 8 changes from 1 to 0. The element values of all columns in row 1 change to 0. That is, at the second moment, thread 0 is no longer blocked by thread 7, thread 0 is currently blocked by thread 8, and thread 1 is not currently blocked by any other thread.
[0148] The electronic device updates the element values in the bitmap in real time according to the changes in the blocking relationship between threads. In this way, the electronic device can directly query the element values in the current bitmap to obtain the real-time blocking relationship between threads.
[0149] In a specific implementation, the subject that executes S21 in the electronic device may be an operating system in the electronic device, or may be each thread in the electronic device.
[0150] The operating system in the electronic device has thread management and scheduling functions, and can monitor changes in blocking relationships between threads. The operating system creates a bitmap and records the blocking relationships between threads through the bitmap. The operating system can also monitor changes in blocking relationships between threads and timely update the element values in the bitmap that represent the blocking relationships. It should be noted that the operating system here can refer to the entire operating system of the electronic device, or it can refer to the scheduler responsible for thread scheduling in the operating system.
[0151] Each thread in the electronic device may also create a corresponding row and column matrix in the bitmap, or update the element value in the corresponding row and column matrix. Each thread may update the element value of the corresponding row and column in the bitmap according to the change of its blocking relationship.
[0152] In an example, the process of the operating system executing S21 may specifically include:
[0153] S21 a: create bitmap;
[0154] S21 b: When applying for thread lock, update the bitmap;
[0155] S21c: When acquiring the thread lock, update the bitmap;
[0156] S21d: When the thread lock is released, the bitmap is updated.
[0157] Among them, S21 a is a step of creating a bitmap that needs to be executed first, and S21 b-S21d are steps of updating the bitmap according to the blocking relationship between threads, which will be explained in detail later.
[0158] When the electronic device is turned on or the operating system creates the first thread, the operating system executes S21 a to create a bitmap. The bitmap created by the operating system may be a row-column matrix including 1 row and 1 column. After that, each time the operating system creates a new thread, the operating system or the newly created thread adds 1 row and 1 column to the initial bitmap to update the size of the row-column matrix of the bitmap.
[0159] In another case, when the electronic device is turned on or the operating system creates the first thread, the operating system can directly create a bitmap containing a larger row and column matrix so that the row and column matrix can basically cover the total number of threads of the electronic device. In this way, when the operating system creates a new thread, it does not need to enlarge the size of the bitmap.
[0160] like Figure 3 As shown, the operating system may perform step S21a, which may include:
[0161] S211: The operating system creates a bitmap; wherein the bitmap includes a row-column matrix, and the number of rows and columns of the row-column matrix are both the maximum number of threads.
[0162] S212: The operating system sets the element value in the bitmap to a first value; wherein the first value indicates that there is no blocking between threads.
[0163] The newly created bitmap of the operating system includes a row-column matrix. The number of rows and columns of the row-column matrix can be the maximum number of threads that the operating system can create, that is, the maximum number of threads. The maximum number of threads of the operating system can reach 32768, that is, 2 15 Table 3 is a schematic diagram of a partial row and column matrix of a bitmap. Table 3 shows the bitmap regions corresponding to 10 threads, which is only used for a simple example and is not intended to limit the solution.
[0164] For a current thread a, the number of threads A that block thread a is the number B of elements with a value of 1 in all 32768 columns on row a. 36768 = 2 15 , then the number of threads blocking thread a is A=2 B , 2 B ≤2 15 *2, that is, 2 B ≤2 16 The binary counting modes in the electronic device include 8-bit binary, 16-bit binary, 32-bit binary and 64-bit binary. Then, the electronic device can use a 16-bit or 32-bit integer to record the number of threads A that block thread a.
[0165] The bitmap created by the operating system, all element values of the row and column matrix of the bitmap can have two values, recorded as a first value and a second value. The first value indicates that the threads are not blocked, and the second value indicates that the threads are blocked. In an example, the first value can be 0 and the second value can be 1.
[0166] When the operating system creates a new bitmap, it first sets all element values in the bitmap to the first value, indicating that there is no inter-thread blocking at the beginning.
[0167] Table 3
[0168]
[0169] During the operation of the electronic device, the blocking relationship between threads changes, and the bitmap can be updated by the thread whose blocking relationship has changed.
[0170] The main reason for thread blocking is waiting for thread locks, that is, whether a thread acquires thread locks is the main factor causing changes in the blocking relationship between threads. The relationship between threads and thread locks includes: thread applying for thread locks, thread acquiring thread locks, and thread releasing thread locks. When the relationship between threads and thread locks changes, the blocking relationship between threads associated with the threads changes. The threads can update the element values of their associated areas in the bitmap according to the changes in the blocking relationship between threads.
[0171] Corresponding to the three relationships between threads and thread locks mentioned above, the thread scheduling method provided in this embodiment involves updating the blocking relationship between threads, mainly including the following three scenarios:
[0172] In the first scenario, the thread applies for a thread lock, a blocked relationship is added, and S21 b is executed;
[0173] In the second scenario, the thread acquires the thread lock, a blocking relationship is added, and S21c is executed;
[0174] In the third scenario, the thread releases the thread lock, reduces the blocking relationship, and executes S21d.
[0175] For the first scenario above, the thread applies for a thread lock, adds a blocked relationship, and executes the solution S21 b, which mainly includes the following steps:
[0176] S213: The eighth thread applies for the third thread lock at the first moment and searches for the ninth thread; wherein the ninth thread is all threads that block the eighth thread at the first moment, and the ninth thread includes the thread holding the third thread lock at the first moment and all threads in the waiting queue of the third thread lock.
[0177] S214: The eighth thread modifies the element value of the second target column corresponding to each ninth thread in the first target row corresponding to the eighth thread in the bitmap to a second value; the second value indicates inter-thread blocking.
[0178] In the thread scheduling method of an electronic device, any thread created by the operating system can apply for a thread lock. For ease of description, the currently described thread is recorded as the eighth thread, and the thread lock applied for by the eighth thread is recorded as the third thread lock. The third thread lock can be any type of thread lock. At a certain moment in the running process of the eighth thread, it is necessary to apply for the third thread lock, and this moment is recorded as the first moment. It should be noted that the eighth thread is limited here for the purpose of consistent description of the entire text, and does not limit the numbering, creation order, etc. of the eighth thread. It is only used to explain the thread scheduling scheme that can be applied to the eighth thread. The eighth thread can be any thread created by the operating system, and the subsequent name restrictions for each thread and each thread lock are the same.
[0179] The eighth thread applies for the third thread lock at the first moment, and a new blocking relationship will occur between the eighth thread and other threads in the electronic device. The new blocking relationship that occurs to the eighth thread is mainly divided into two situations: the first situation is that the eighth thread applies for the third thread lock at the first moment and the application fails; the second situation is that the eighth thread applies for the third thread lock at the first moment and the application succeeds.
[0180] For the first case, the eighth thread fails to apply for the third thread lock, usually because the third thread lock may be currently held by a certain thread. In this case, there may be one or more other threads waiting to obtain the third thread lock in the waiting queue of the third thread lock, and these one or more other threads waiting to obtain the third thread lock applied for the third thread lock before the first moment. The thread currently holding the third thread lock and the threads in the waiting queue of the third thread lock will block the eighth thread. For ease of description, the thread currently holding the third thread lock and the threads in the waiting queue of the third thread lock are recorded as the ninth thread, and the number of ninth threads may be one or more.
[0181] The eighth thread applies for the third thread lock at the first moment and fails to apply, determines the ninth thread that blocks the eighth thread, and updates the blocking relationship between the eighth thread and the ninth thread in the bitmap.
[0182] In the bitmap, the eighth thread corresponds to the first target row and the first target column in the bitmap, and the ninth thread corresponds to the second target row and the second target column in the bitmap. Among them, the element value in the first target row refers to the state in which the eighth thread is blocked by other threads, and the element value in the first target column refers to the state in which the eighth thread blocks other threads. Similarly, the element value in the second target row refers to the state in which the ninth thread is blocked by other threads, and the element value in the second target column refers to the state in which the ninth thread blocks other threads. Then, in the first target row, the element value corresponding to the second target column can refer to the state in which the eighth thread is blocked by the ninth thread.
[0183] It should be noted that the first target row does not refer to the first row in the bitmap, the first target column does not refer to the first column in the bitmap, the second target row does not refer to the second row in the bitmap, and the second target column does not refer to the second column in the bitmap. There may be multiple ninth threads, and there may also be multiple corresponding second target rows and second target columns.
[0184] In one example, the eighth thread is thread 0. At the first moment, the third thread lock applied for by thread 0 is thread lock A, which is currently held by thread 1, and the waiting queue of thread lock A also includes threads 2 to 7. The thread lock holding thread (i.e., thread 1) and the prior application threads (i.e., threads 2 to 7) of thread lock A block thread 0.
[0185] That is, at the first moment, thread 0 applies for thread lock A, and the application fails. Thread 0 finds out the current thread holding thread lock A, namely thread 1, and finds threads 2-7 that previously applied for thread lock A and are still waiting to obtain thread lock A in the waiting queue of thread lock A. Thread 0 determines that threads 1-7 block thread 0 and are the ninth threads blocking thread 0.
[0186] The first target row corresponding to thread 0 in the bitmap is row 0, and the second target columns corresponding to threads 1 to 7 in the bitmap are columns 1 to 7. Thread 0 modifies the element values of row 0 and columns 1 to 7 in the bitmap of Table 3 from 0 to 1, and the following Table 4 is obtained.
[0187] Table 4
[0188]
[0189] For the second case, the eighth thread successfully applies for the third thread lock at the first moment, usually because the third thread lock is not currently held by any other thread, or there are no other threads waiting to obtain the third thread lock in the waiting queue of the eighth thread. Then, at the first moment, the eighth thread applies for the third thread lock, and there is no ninth thread blocking the third thread lock. In other words, the blocking relationship of the eighth thread has not changed, and the eighth thread may not modify the element value of the corresponding area in the bitmap, and the bitmap may still be as shown in Table 3.
[0190] For the second scenario above, the thread acquires the thread lock, adds a blocking relationship, and executes the solution of S21c, which can mainly include the following steps:
[0191] S215: When the eighth thread acquires the third thread lock at the second moment, the eighth thread modifies the element value of each second target column in the first target row in the bitmap to the first value.
[0192] S216: the eighth thread searches for the tenth thread; wherein the tenth thread is all threads in the waiting queue locked by the third thread at the second moment;
[0193] S217: The eighth thread modifies the element value of the third target row corresponding to each tenth thread in the first target column of the bitmap to the second value.
[0194] The eighth thread applies for the third thread lock at the first moment and enters the waiting queue for the third thread lock. At a certain moment after the first moment, the eighth thread obtains the third thread lock, and the moment when the eighth thread obtains the third thread lock is recorded as the second moment, which is after the first moment. There are also two situations when the eighth thread obtains the third thread lock: in the first situation, the third thread lock is obtained in the order of applying for the third thread lock; in the second situation, the eighth thread obtains the third thread lock first.
[0195] For the first case, all ninth threads acquire the third thread lock and release the third thread lock, and the eighth thread acquires the third thread lock at the second moment. At the second moment, all ninth threads no longer block the eighth thread. The eighth thread can modify the element value of the second target column corresponding to each ninth thread in the first target row of the bitmap to the first value, indicating that each ninth thread no longer blocks the eighth thread.
[0196] For the second case, the eighth thread may obtain the third thread lock first. Because the eighth thread has a higher priority than some ninth threads, or the eighth thread is scheduled by the operating system first, the eighth thread obtains the third thread lock before some ninth threads. In this case, some ninth threads that have obtained and released the third thread lock no longer block the eighth thread, and some ninth threads that are still waiting to obtain the third thread lock change from blocking the eighth thread to being blocked by the eighth thread. All ninth threads no longer block the eighth thread, and the eighth thread can modify the element value of the second target column corresponding to each ninth thread in the first target row of the first bitmap to the first value, indicating that each ninth thread no longer blocks the eighth thread.
[0197] When the eighth thread acquires the third thread lock at the second moment, it can determine all threads currently waiting to acquire the third thread lock, that is, the tenth thread currently blocked by the eighth thread. If the eighth thread acquires the third thread lock in the first case, then all the tenth threads apply for the third thread lock after the first moment. If the eighth thread acquires the third thread lock in the second case, then some of the tenth threads apply for the third thread lock before the first moment, that is, the tenth thread includes some of the previous ninth thread.
[0198] The eighth thread determines all the tenth threads blocked by the eighth thread by querying the waiting queue of the third thread lock, and modifies the element value of the third target row corresponding to each tenth thread in the first target column of the bitmap to the second value.
[0199] In one example, continuing with the bitmap shown in Table 4 above, at the second moment, thread 0 acquires thread lock A.
[0200] If it corresponds to the first case, threads 1 to 7 no longer block thread 0. As shown in Table 5, thread 0 modifies the element values of columns 1 to 7 in the corresponding row 0 of the bitmap to 0, as shown in Table 5. If thread 0 determines that thread 8 and thread 9 are the tenth threads currently waiting to acquire thread lock A, as shown in Table 5, thread 0 can modify the element values of row 8 corresponding to thread 8 and row 9 corresponding to thread 9 in column 0 of the bitmap to 1.
[0201] Table 5
[0202]
[0203] In the second case, threads 1 to 7 no longer block thread 0. As shown in Table 6, thread 0 first modifies the element values of the corresponding row 0, columns 1 to 7 in the bitmap to 0. Threads 4 to 9 are blocked by thread 0, and thread 0 can also modify the element values of the corresponding rows 4 to 9 in column 0 to 1. In addition, thread 0 also determines that thread 8 and thread 9 are also the tenth threads currently waiting to acquire thread lock A. Thread 0 can modify the element values of the corresponding rows 8 to thread 8 and the corresponding rows 9 to thread 9 in column 0 of the bitmap to 1.
[0204] Table 6
[0205]
[0206]
[0207] In other cases, when the eighth thread acquires the third thread lock, the waiting queue of the third thread lock does not contain the tenth thread currently waiting to acquire the third thread lock, and the eighth thread does not query the tenth thread and does not need to modify the element value in the bitmap.
[0208] For the third scenario above, the thread releases the thread lock, reduces the blocking relationship, and executes the solution S21d, which mainly includes the following steps:
[0209] S218: When the eighth thread releases the third thread lock at the third moment, the eighth thread modifies the element value of each third target row in the first target column in the bitmap to the first value.
[0210] The eighth thread acquires the third thread lock at the second moment, and releases the third thread lock at a moment after the second moment, and the moment when the eighth thread releases the third thread lock is recorded as the third moment, and the third moment is after the second moment. The eighth thread releases the third thread lock, and the eighth thread no longer blocks any tenth thread. The eighth thread can modify the element value of each third target row in the first target column of the bitmap to the first value, indicating that the eighth thread no longer blocks the tenth thread.
[0211] When the eighth thread releases the third thread lock, one or more threads in the tenth thread will acquire the third thread lock. For ease of description, the thread currently acquiring the third thread lock among all the tenth threads is recorded as the eleventh thread, and the thread currently waiting to acquire the third thread lock is recorded as the twelfth thread.
[0212] The eleventh thread acquires the third thread lock and blocks the twelfth thread waiting to acquire the third thread lock. The eleventh thread may modify the element value in the fourth target column corresponding to the eleventh thread and the fifth target row corresponding to the twelfth thread in the bitmap to the second value.
[0213] In one example, as shown in Table 6 above, thread 0 releases thread lock A, and thread 4 acquires thread lock A. As shown in Table 7, thread 0 modifies the element values of rows 4 to 9 in column 0 of the bitmap to 0. In addition, thread 4 modifies the element values of rows 5 to 9 in column 4 of the bitmap to 1.
[0214] Table 7
[0215]
[0216]
[0217] In other cases, when the eighth thread releases the third thread lock, there may be no eleventh thread and twelfth thread currently waiting to acquire the third thread lock in the waiting queue of the third thread lock. In this case, only the eighth thread needs to modify the element value in the first target column.
[0218] The electronic device adopts a solution of recording the blocking relationship between threads using a bitmap, and the recorded blocking relationship between threads is clearer. The adopted bitmap data structure is independent of the computing task and thread lock of the thread, and there is no need to add additional nodes in the task structure task_struct and the lock data structure. In addition, the blocking relationship between threads that can be recorded using the bitmap does not need to distinguish thread locks, and is suitable for blocking relationships generated based on multiple thread locks in electronic devices. The electronic device updates the bitmap when the blocking relationship between threads is updated, without the need to periodically query and judge the blocking relationship between threads.
[0219] Secondly, the operating system executes S22 to optimize the thread scheduling scheme according to the blocking relationship between threads.
[0220] The electronic device can execute a thread scheduling scheme according to the blocking relationship between threads to speed up the thread running. The thread scheduling schemes that the electronic device can execute mainly include two kinds: the first scheme is that the operating system controls the thread currently waiting to obtain the thread lock to execute S22a and obtain the thread lock first; the second scheme is that the operating system executes S22b and schedules the thread first, especially the thread that has obtained the thread lock and is waiting to be scheduled.
[0221] For the first solution, the operating system enables the first thread waiting to acquire the first thread lock to acquire the first thread lock preferentially according to the blocking relationship between threads.
[0222] In the electronic device, it is assumed that the first thread lock is currently held by the second thread, and there are one or more threads in the waiting queue of the first thread lock waiting to obtain the first thread lock. Among all the second threads in the waiting queue of the first thread lock, there is a thread that needs to obtain the first thread lock first, which is recorded as the first thread.
[0223] The electronic device determines the first thread that needs to be locked first from all threads in the waiting queue of the first thread lock, and allows the first thread to obtain the first thread lock first. For example, the electronic device can determine the thread with the highest priority as the first thread.
[0224] In one example, if Figure 4 As shown, S22a may mainly include the following steps:
[0225] S221: The operating system searches for the first thread in the waiting queue of the first thread lock; wherein the first thread refers to a thread that is blocked by other threads and needs to be locked first among all threads in the waiting queue of the first thread lock, and the current locking thread of the first thread lock is the second thread.
[0226] S222: The operating system adds a first tag to the first thread.
[0227] S223: The second thread releases the first thread lock, and the first thread added with the first mark acquires the first thread lock.
[0228] Electronic devices create multiple threads, and different threads perform different computing tasks, have different importance or urgency, and have different priorities. For some threads that are more important, more urgent, or have higher priorities, waiting for a long time may affect the overall process. When creating threads or when threads are running, electronic devices can mark threads that are more important, more urgent, or have higher priorities as threads that need to be scheduled first, based on the importance, urgency, or priority of each thread. Priority-scheduled threads must be locked first before they can be scheduled first. In specific implementation, the operating system searches for threads that need to be locked first from the threads in the waiting queue of the thread lock.
[0229] In one example, the electronic device may set a priority lock pointer, where the priority lock pointer points to a thread that needs to be locked with priority.
[0230] In a specific implementation, the operating system points the priority lock pointer to the first thread in the waiting queue of the first thread lock, wherein the priority lock pointer points to the first thread, indicating that the first thread has added a first mark. In this way, when the second thread releases the first thread lock, the second thread can wake up the first thread pointed to by the priority lock pointer, so that the first thread acquires the first thread lock.
[0231] Specifically, the electronic device may also create a pointer blocker of a task_struct on the processor's run queue Run Queue as a priority lock pointer to point to a thread that needs to be locked with priority in the waiting queue of the first thread lock.
[0232] The electronic device can determine whether there is a thread that needs to be locked with priority by querying whether the priority lock pointer is empty. If the electronic device queries that the priority lock pointer is empty, it means that there is no thread that needs to be locked with priority. If the electronic device queries that the priority lock pointer is not empty, it means that there is a thread that needs to be locked with priority.
[0233] The electronic device queries the priority lock pointer to determine whether there is a thread that needs to be locked with priority. If the electronic device determines that there is a thread that needs to be locked with priority, the thread that needs to be locked with priority can be determined as the first thread. When the current lock holding thread of the first thread lock, that is, the second thread, releases the first thread lock, the first thread can be awakened to obtain the first thread lock with priority.
[0234] like Figure 5 As shown in (a), thread lock 1 is locked by thread 1, and the waiting queue of thread lock 1 includes threads 2, 3, 4, and 5. The priority lock pointer points to thread 4, so thread 4 is determined to be the thread that needs to be locked first.
[0235] like Figure 5 As shown in (b), thread lock 2 is locked by thread 7, and the waiting queue of thread lock 2 includes threads 10, 13, 14, and 15. The priority pointer is empty, indicating that there is no pointer that needs to be locked first.
[0236] When thread 1 releases thread lock 1, thread 4 acquires thread lock 1. Compared with threads 2 and 3 that are ahead of thread 4 in the waiting queue for thread lock 1, thread 4 has priority in acquiring thread lock 1. This can speed up the progress of thread 4 and optimize the overall application process.
[0237] Continue as Figure 4 As shown, S22a may also include a thread scheduling scheme for a thread that has not found a priority lock holding thread, specifically including:
[0238] S224: If the first thread is not found, the operating system searches for a third thread in the waiting queue of the first thread lock; wherein the third thread refers to a thread with the longest waiting time among all threads in the waiting queue of the first thread lock;
[0239] S225: The operating system adds a second mark to the third thread;
[0240] S226: The second thread releases the first thread lock, and the third thread with the second mark added acquires the first thread lock.
[0241] The electronic device queries the priority scheduling pointer, and if it is determined that there is no first thread that needs priority scheduling, it can search for the third thread with the longest waiting time and add a second mark to the third thread. When the third thread releases the first thread lock, the third thread acquires the first thread lock.
[0242] Continue as Figure 5 As shown in (b), thread 7 acquires thread lock 2. There is no thread that needs to be scheduled first in the waiting queue of thread lock 2, so thread 10 with the longest waiting time can be further determined. When thread 7 releases thread lock 2, thread 10 with the longest waiting time acquires thread lock 2.
[0243] In a specific implementation, the first thread and the execution subject that wakes up the first thread can be the second thread that releases the lock of the first thread. When the second thread releases the lock of the first thread, it queries the waiting queue of the first thread lock to determine whether the priority lock pointer is empty to determine the first thread. The second thread wakes up the first thread, and after the first thread is woken up, it acquires the lock of the first thread.
[0244] It should be noted that the thread that enters the waiting queue of the first thread lock is usually in a dormant state, and the running state parameter corresponds to the dormant state. The second thread wakes up the first thread, which means that the second thread modifies the running state parameter of the first thread to the parameter corresponding to the running state. The acquisition and release of the thread lock by the thread need to follow the agreed path and last for a certain period of time to complete.
[0245] When the second thread starts to release the first thread lock, the first thread is awakened and the first thread starts to pre-hold the lock. After the second thread completely releases the first thread lock, the first target thread acquires the thread lock.
[0246] In specific implementation, the operating system may also insert a hook point in the pick_next_task_fire function of the CFS scheduler (Completely Failing Scheduler) scheduling class to select the path of the next task, and check whether the blocker pointer of the current queue is empty. If it is not empty, the blocker recorded by the pointer is preferentially selected for scheduling.
[0247] The thread scheduling scheme provided in this implementation can determine the first thread that needs to hold the lock first according to the blocking relationship between threads, so that the first thread obtains the first thread lock first, thereby speeding up the overall process.
[0248] For the second solution, the electronic device configures an automatic wake-up function for the fourth thread. While the fourth thread is waiting to acquire the second thread lock, the fifth thread that blocks the fourth thread can be preferentially scheduled so that the fourth thread can acquire the second thread lock as soon as possible.
[0249] In a specific embodiment, Figure 6 As shown, the electronic device executing S22b may include the following steps:
[0250] S227: The fourth thread applies for the second thread lock and enters the waiting queue of the second thread lock; the fourth thread has the function of automatically waking up at multiple automatic wake-up times.
[0251] Among the multiple threads created by the electronic device, some threads have higher priorities, such as the main thread. If a thread with a higher priority is stuck in a blocked state for a long time, it will affect the overall application process. Based on this, the electronic device configures an automatic wake-up function for some threads with higher priority, and the automatic wake-up function can be configured by a timer. For ease of description, the thread configured with the self-wake-up function is recorded as the fourth thread.
[0252] In a specific implementation, if the fourth thread is the main thread, the main thread priority is relatively high, and the main thread blocking has a greater impact on the overall application process. In the case where the fourth thread is the main thread, the operating system can configure a timer for the fourth thread to implement an automatic wake-up function. The timer sends a timed wake-up message to the fourth thread at each automatic wake-up time, and the timed wake-up message is used to wake up the fourth thread at each automatic wake-up time.
[0253] The fourth thread is equipped with a timer, which mainly has two wake-up functions, namely, self-wake-up and wake-up of other threads. The self-wake-up function means that the fourth thread can automatically wake up at a fixed time after entering the dormant state. The function of waking up other threads means that the fourth thread can also wake up other threads in the dormant state every time it automatically wakes up.
[0254] For example, if the timer setting period is 10 microseconds, then after the fourth thread enters the sleep state, it can wake itself up every 10 microseconds and wake up other threads in the sleep state.
[0255] The fourth thread with automatic wake-up function applies for the second thread lock. If the second thread lock is currently acquired by other threads, the fourth thread will enter the waiting queue of the second thread lock. Threads entering the waiting queue of the second thread lock are usually in a dormant state and can only be awakened by other threads. The fourth thread entering the waiting queue of the second thread lock will be in an Uninterruptible Sleep state and can receive a wake-up message sent by a timer and automatically wake up.
[0256] In actual operation, according to the interval n microseconds set by the timer, the timer will send a wake-up message to the fourth thread every n microseconds, and the fourth thread will wake itself up according to the wake-up message and then enter the sleep state. The fourth thread will continue to alternate between self-wake-up and sleep state until the fourth thread acquires the second thread lock and ends the sleep state, and the timer stops sending wake-up messages to the fourth thread. For ease of description, the moment when the timer wakes up the fourth thread each time is defined as the automatic wake-up moment.
[0257] S228: At each automatic wake-up moment, the fourth thread searches for the fifth thread; wherein the fifth thread is the current thread holding the lock of the second thread, and the fifth thread directly or indirectly blocks the fourth thread.
[0258] S229: If the fifth thread has not been scheduled, the fourth thread adds a third mark to the fifth thread.
[0259] S2210: The operating system preferentially schedules the fifth thread to which the third mark is added.
[0260] At each automatic wake-up moment, the second target thread and the fourth thread wake themselves up and search the bitmap for the thread that currently acquires the second thread lock, which is recorded as the third target thread and the fifth thread. When the third target thread and the fifth thread acquire the second thread lock, there will be two states, namely being scheduled and not being scheduled. If the fifth thread acquires computing resources, it can be scheduled to perform computing tasks after acquiring the second thread lock, and release the second thread lock after completing the computing tasks. If the fifth thread cannot acquire computing resources for a long time due to reasons such as low priority, the fifth thread may be in an unscheduled state even if it acquires the second thread lock.
[0261] The fourth thread searches the bitmap and finds that the fifth thread that acquires the second thread lock is being scheduled, and the fourth thread can continue to wait to acquire the second thread lock.
[0262] The fourth thread searches the bitmap to find the fifth thread that has acquired the second thread lock, and the fifth thread has acquired the second thread lock but has not been scheduled. The fourth thread can add a third mark to the fifth thread, and the third mark indicates that the fifth thread is a thread that needs to be scheduled first. The mark added by the fourth thread to the fifth thread can enable the operating system to allocate computing resources to the fifth thread and schedule the fifth thread first. After acquiring the computing resources allocated by the operating system, the fifth thread can hold the second thread lock to execute the computing task as soon as possible, and release the second thread lock as soon as possible after completing the computing task. In this way, the fourth thread can acquire the second thread lock as soon as possible, reduce the time the fourth thread waits to acquire the second thread lock, and speed up the overall application process.
[0263] In a specific implementation, the scheme for the fourth thread to search for the fifth thread that obtains the second thread lock in the bitmap may specifically include: the fourth thread searches for the sixth thread that directly blocks the fourth thread; if the sixth thread that directly blocks the fourth thread obtains the second thread lock, the sixth thread is used as the fifth thread; if the sixth thread that directly blocks the fourth thread does not obtain the second thread lock, searches for the seventh thread that directly blocks the sixth thread; if the seventh thread obtains the second thread lock, the seventh thread is used as the fifth thread.
[0264] It should be noted that the sixth thread that directly blocks the fourth thread can be understood as, in the waiting queue of the second thread lock, the sixth thread is adjacent to the fourth thread and the sixth thread is ranked before the fourth thread. The seventh thread that directly blocks the sixth thread means that in the waiting queue of the second thread lock, the seventh thread is adjacent to the sixth thread and the seventh thread is ranked before the sixth thread. The seventh thread is ranked before the fourth thread, but the seventh thread is not adjacent to the fourth thread, and the seventh thread can be considered to indirectly block the fourth thread.
[0265] In other embodiments, the fifth thread searched by the fourth thread at different automatic wake-up moments may be the same or different. Specifically, at the first automatic wake-up moment, the fourth thread searches for the locked thread locked by the second thread at the first automatic wake-up moment as the fifth thread corresponding to the first automatic wake-up moment. At the second automatic wake-up moment, the fourth thread searches for the locked thread locked by the second thread at the second automatic wake-up moment as the fifth thread corresponding to the second automatic wake-up moment. The fifth thread corresponding to the first automatic wake-up moment and the fifth thread corresponding to the second automatic wake-up moment may be the same thread or different threads.
[0266] For example, the fourth thread is thread 0, thread 0 fails to apply for thread lock 1, and enters the waiting queue of thread lock 1.
[0267] At the first automatic wakeup time, thread 0 is woken up by the timer. For example Figure 7 As shown in (a), the thread currently blocking thread 0 is found from the bitmap to be thread 1, which currently acquires thread lock 1 and is not scheduled. Thread 0 wakes up thread 1 and marks thread 1 as a priority thread, so that the operating system preferentially allocates computing resources to thread 1 marked as a priority thread, and thread 1 is preferentially scheduled. After waking up thread 1, thread 0 continues to enter the sleep state.
[0268] At the second automatic wake-up time, thread 0 is woken up again by the timer. Figure 7 As shown in (b), the thread currently blocking thread 0 is found from the bitmap as thread 4. Thread 4 is blocked by the previous thread, thread 3. Thread 3 is blocked by the previous thread, thread 2. Thread 2 obtains the thread lock and is not scheduled.
[0269] Thread 0 can then wake up thread 2 and allow thread 2 to run first. Specifically, thread 0 can mark thread 2 as a priority scheduling thread so that the operating system allocates computing resources to thread 2 to achieve priority scheduling of thread 2. In addition, after thread 0 wakes up thread 2, it will continue to enter the sleep state.
[0270] Thread 0 continues the aforementioned operation until thread 0 acquires the second thread, ends the sleep state and enters the running state.
[0271] In a specific embodiment, Figure 8 As shown in FIG. 1 , the thread scheduling method provided for the electronic device involves a flowchart of a specific scheduling process. Figure 8 As shown, the provided thread scheduling method mainly includes the following steps:
[0272] S801: The first thread applies for a first thread lock.
[0273] The first thread may be any thread created by the operating system. When the first thread needs to access a certain memory resource, it applies for obtaining the corresponding first thread lock. For details, please refer to the specific implementation process of the above S213 and S214, which will not be repeated here.
[0274] S802: The first thread attempts to hold the first thread lock.
[0275] The first thread applies for the first thread lock and takes the fast lock holding path first, that is, the first thread first tries to hold the first thread lock. If the first thread lock is not currently held by other threads, the first thread can successfully hold the lock and obtain the first thread lock. If the first thread lock is currently held by other threads, the first thread fails to hold the lock and enters the slow lock holding path.
[0276] If the first thread attempts to hold the first thread lock and succeeds, S803 may be executed. If the first thread attempts to hold the first thread lock and fails to hold the first thread lock, the thread enters the slow path and executes S804.
[0277] S803: If the first thread successfully holds the lock, the lock holding thread identifier of the first thread lock is updated to the first thread.
[0278] The first thread successfully acquires the first thread lock, and updates the lock thread identifier of the first thread lock to the first thread. In this way, other threads can query the lock thread identifier of the first thread lock and know that the current lock thread holding the first thread lock is the first thread lock.
[0279] The specific implementation process of the first thread acquiring the first thread lock can refer to the specific implementation process of S215-S217 mentioned above, which will not be repeated here.
[0280] S804: The first thread holds the first thread lock and executes the computing task.
[0281] The first thread holds the first thread lock and executes the corresponding computing task. It should be noted that the computing task executed by the first thread while holding the first thread lock is not limited to all computing tasks executed during the operation of the first thread, and can refer to computing tasks that need to be executed under the premise that the first thread holds the first thread lock.
[0282] S805: The first thread clears the lock holding thread identifier of the first thread lock.
[0283] During the period of holding the first thread lock, the first thread completes the corresponding computing task and begins to enter the process of releasing the first thread lock. The first thread can first clear the lock thread identifier of the first thread lock to indicate that the current lock thread of the first thread lock is no longer the first thread.
[0284] S806: The first thread releases the first thread lock.
[0285] The first thread starts to release the first thread lock. For the specific implementation process of S218 mentioned above, details will not be repeated here.
[0286] S807: The first thread searches for the next second thread that applies to hold the first thread lock.
[0287] The first thread releases the first thread lock and searches for the next thread waiting to acquire the first thread lock, which is recorded as the second thread. The solution of the first thread searching for the second thread can refer to the specific implementation process of S221-S223 above, which will not be repeated here.
[0288] S808: The second thread holds the lock of the first thread.
[0289] S809: The first thread updates the lock holding thread identifier of the first thread lock to the second thread.
[0290] S810: The first thread wakes up the second thread, and the second thread holds the lock of the first thread and executes the computing task.
[0291] The second thread holds the lock of the first thread in advance, and updates the lock-holding thread identifier of the first thread lock to the second thread. After the second thread holds the lock in advance, it enters the process of acquiring the lock of the first thread. The first thread wakes up the second thread, and the second thread holds the lock of the first thread and starts to execute the corresponding computing task.
[0292] For the specific implementation process that the second thread attempts to hold the first thread lock and starts to execute the computing task after successfully holding the lock, please refer to the specific implementation process that the first thread attempts to hold the first thread lock, which will not be repeated here.
[0293] S811: The first thread updates the element value of the corresponding area in the bitmap.
[0294] In the aforementioned S801, the step of the first thread applying for the first thread lock, the step of the first thread acquiring the first thread lock in S803, the step of the first thread releasing the first thread lock in S806, and the step of the second thread pre-holding the first thread lock in S808 all involve updating the element value of the corresponding area in the bitmap. For details, please refer to the specific implementation process of the aforementioned S212-S218, which will not be repeated here.
[0295] S812: If the first thread fails to hold the lock, it enters a slow lock waiting path and enters a waiting queue for the first thread lock.
[0296] S813: The first thread enters a dormant state.
[0297] The first thread attempts to hold the lock when applying for the first thread lock. If the lock holding fails, the first thread enters the slow lock waiting path, enters the waiting queue of the first thread lock, and enters a dormant state.
[0298] S814: The first thread holds the first thread lock in the dormant state.
[0299] The first thread waits to acquire the first thread lock in the dormant state. After the thread before the first thread acquires the first thread lock and releases the first thread lock, the first thread is awakened to pre-hold the first thread lock. The specific implementation process of the first thread pre-holding the first thread lock in the dormant state can refer to the aforementioned S807, and the specific implementation process of S221-S223, which will not be repeated here.
[0300] S815: The lock holding thread identifier of the first thread lock is updated to the identifier of the first thread.
[0301] S816: The first thread is awakened.
[0302] S817: The first thread attempts to hold the first thread lock.
[0303] The predecessor thread of the first thread wakes up the first thread and pre-holds the first thread lock, updates the lock-holding thread identifier of the first thread lock to the identifier of the first thread, the predecessor thread of the first thread wakes up the first thread, and the first thread attempts to hold the first thread lock.
[0304] If the first thread successfully holds the lock, it jumps to execute S803.
[0305] If the first thread fails to hold the lock, the first thread executes S818: interrupt wakeup.
[0306] S819: The first thread is killed / exited.
[0307] As in the above step S802, the first thread attempts to hold the first thread lock. If the first thread succeeds in holding the lock, the process jumps to S803, where the first thread acquires the first thread lock and executes the computing task, and releases the first thread lock when the computing task is completed.
[0308] In some cases, for example, when the first thread is pre-locking the write lock, the write lock is preempted by other threads that are applying for locks (read locks or write locks), and the first thread will fail to hold the lock. If the first thread fails to hold the lock, it will not enter the waiting queue of the first thread lock again. The first thread is preempted to hold the write lock, and fails to hold the lock and enters the sleep state. If the signal kill command generated by the operating system is received, the first thread will be interrupted and awakened, and the first thread will be killed or exited.
[0309] like Fig. 9 As shown in FIG. 1 , it is a schematic diagram comparing the effects of the thread scheduling method provided in the embodiment of the present application. Fig. 9 (a) is a schematic diagram of frequent blocking of the main thread of an application caused by not using the thread scheduling method provided in the embodiment of the present application. After being blocked, the main thread can only passively wait for the lock-holding thread to release the lock. The lock-holding thread may not be scheduled for a long time due to its low priority, and the main thread is blocked for a long time. Fig. 9 (b) is a schematic diagram of the effect of using the thread scheduling method provided by the embodiment of the present application. After the main thread is blocked, it will actively search for the blocker according to the bitmap at regular intervals, and mark the blocker as a task that needs to be scheduled first, so that the system will select the blocker when selecting a task to execute next time. The total blocking time of the main thread is shortened.
[0310] The thread scheduling method provided in the above embodiment quickly records the blocking relationship between threads through a bitmap, does not need to add additional members to the task structure, is applicable to multiple types of thread locks, and does not need to periodically determine the blocking state. The operating system can effectively identify the blocked thread, quickly schedule it, and reduce the time of application freeze.
[0311] In addition, an embodiment of the present application further provides an electronic device, including a memory and a processor, wherein the processor runs an operating system;
[0312] Memory stores computer-executable instructions;
[0313] The processor executes the computer execution instructions stored in the memory, so that the operating system executes the thread scheduling method provided by the above embodiment. In addition to these main components, the electronic device also includes components for realizing basic functions. Fig.10 Provide specific instructions.
[0314] like Fig.10The figure shows a schematic diagram of the structure of an electronic device 1000 provided in an embodiment of the present application. The electronic device 1000 may include a processor 1010, a memory 1020, a Universal Serial Bus (USB) interface 1030, a charging management module 1040, a power management module 1041, a battery chip 1042, an antenna, a communication module 1050, a display screen 1060, an audio module 1070, a sensor module 1080, a camera 1090, and the like.
[0315] The structure shown in the embodiment of the present invention does not limit the electronic device 1000. It 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.
[0316] The processor 1010 may include one or more processing units. For example, the processor 1010 may include an application processor (AP), a modem processor, a graphics processor (GPU), an image signal processor (ISP), a controller, a memory, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural network processor (NPU). Different processing units may be independent devices or integrated into one or more processors.
[0317] The processor 1010 can be a decision maker that directs the various components of the electronic device 1000 to work in coordination according to instructions. It is the nerve center and command center of the electronic device 1000. The processor 1010 generates an operation control signal according to the instruction operation code and timing signal to complete the control of fetching and executing instructions.
[0318] The processor 1010 may also be provided with a memory for storing instructions and data. In some embodiments, the memory in the processor 1010 is a high-speed cache memory, which can store instructions or data that the processor 1010 has just used or cyclically used. If the processor 1010 needs to use the instruction or data again, it can be directly called from the memory. This avoids repeated access, reduces the waiting time of the processor 1010, and thus improves the efficiency of the system.
[0319] In some embodiments, the processor 1010 may include an interface. 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) interface, a General-Purpose Input / Output (GPIO) interface, a SIM interface, and / or a USB interface, etc.
[0320] The interface connection relationship between the modules shown in the embodiment of the present invention is only for illustrative purposes and does not constitute a structural limitation on the electronic device 1000. The electronic device 1000 may adopt different interface connection methods in the embodiment of the present invention, or a combination of multiple interface connection methods.
[0321] The charging management module 1040 can be a rechargeable battery or a disposable battery. When it is a rechargeable battery, it can receive charging input through a charger. The charger can be a wireless charger or a wired charger. In some wired charging embodiments, the charging management module 1040 can receive charging input from a wired charger through the USB interface 1030. In some wireless charging embodiments, the charging management module 1040 can receive wireless charging input through a wireless charging coil of the electronic device 1000. While the charging management module 1040 is charging the battery chip 1042, it can also power the electronic device 1000 through the power management module 1041.
[0322] The power management module 1041 is used to connect the battery chip 1042, the charging management module 1040 and the processor 1010. The power management module 1041 receives input from the battery chip 1042 and / or the charging management module 1040, and supplies power to the processor 1010, the memory 1021, the external memory interface 1020, the display screen 1094, the camera 1093, and the communication module 1060. The power management module 1041 can also be used to monitor parameters such as the capacity of the charging management module, the number of cycles of the charging management module, and the health status (leakage, impedance) of the charging management module. In some embodiments, the power management module 1041 can also be set in the processor 1010. In some embodiments, the power management module 1041 and the battery chip 1042 can also be set in the same device.
[0323] The wireless communication function of the electronic device 1000 can be implemented through an antenna, a communication module 1050, a modem, a baseband processor, and the like.
[0324] The electronic device 1000 implements a display function through a GPU, a display screen 1060, and an application processor. The GPU is a microprocessor for image processing, which is connected to the display screen 1094 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 1010 may include one or more GPUs that execute program instructions to generate or change display information. The display screen 1094 is used to display images, videos, etc. The display screen 1094 includes a display panel. The electronic device 1000 can implement a shooting function through an ISP, a camera 1090, a video codec, a GPU, a display 1060, and an application processor.
[0325] The electronic device 1000 can implement audio functions such as music playing and recording through the speaker, receiver, microphone, headphone interface, and application processor of the audio module 1070.
[0326] The thread scheduling methods in the aforementioned embodiments can all be implemented in the electronic device 1000 having the aforementioned hardware structure.
[0327] On the basis of the above embodiments, an embodiment of the present application further provides a thread scheduling device, the thread scheduling device includes a processor, and the processor is used to execute the thread scheduling method provided by the above embodiments.
[0328] The embodiment of the present application also provides a computer-readable storage medium, in which a computer program is stored. When the computer-readable storage medium is run on a computer, the computer executes the thread scheduling method provided in the above embodiment.
[0329] An embodiment of the present application also provides a computer program product including instructions, which, when executed on a computer, enables the computer to execute the thread scheduling method provided in the above embodiment.
[0330] The specific implementation methods of the electronic device, computer-readable storage medium, and computer program product containing instructions provided in the embodiments of the present application and the technical effects brought about by them can be found in the specific implementation process of the thread scheduling method provided in the aforementioned embodiments and the technical effects brought about by them, which will not be repeated here.
[0331] In some embodiments, through the description of the above implementation methods, technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above functional modules is used as an example. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device is divided into different functional modules to complete all or part of the functions described above. The specific working process of the system, device and unit described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0332] Each functional unit in each embodiment of the present application can be integrated into a processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The above integrated unit can be implemented in the form of hardware or in the form of software functional units.
[0333] 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 technical solution of the embodiment of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to perform all or part of the steps of the various embodiments of the present application. The aforementioned storage medium includes: various media that can store program codes, such as flash memory, mobile hard disk, read-only memory, random access memory, disk or optical disk.
[0334] The above is only a specific implementation of the embodiment of the present application, but the protection scope of the embodiment of the present application is not limited thereto, and any changes or replacements within the technical scope disclosed in the embodiment of the present application should be included in the protection scope of the embodiment of the present application. Therefore, the protection scope of the embodiment of the present application should be based on the protection scope of the claims.
Claims
1. A thread scheduling method, characterized in that: Applied to an electronic device, the electronic device includes an operating system, and the thread scheduling method includes: The operating system searches for a first thread in a waiting queue of a first thread lock; wherein the first thread refers to a thread that is blocked by other threads and needs to be locked first among all threads in the waiting queue of the first thread lock, and the current locked thread of the first thread lock is a second thread; The operating system adds a first mark to the first thread; The second thread releases the first thread lock, and the first thread added with the first mark acquires the first thread lock.
2. The thread scheduling method according to claim 1, characterized in that: After the operating system searches for the first thread in the waiting queue of the first thread lock, the thread scheduling method further includes: If the first thread is not found, the operating system searches for a third thread in the waiting queue of the first thread lock; wherein the third thread refers to a thread with the longest waiting time among all threads in the waiting queue of the first thread lock; The operating system adds a second mark to the third thread; The second thread releases the first thread lock, and the third thread added with the second mark acquires the first thread lock.
3. The thread scheduling method according to claim 1 or 2, characterized in that: The operating system adds a first mark to the first thread, the second thread releases the first thread lock, and the first thread with the first mark added acquires the first thread lock, including: The operating system points the priority lock pointer to the first thread in the waiting queue of the first thread lock; wherein the priority lock pointer points to the first thread indicating that the first thread has added the first mark; When the second thread releases the first thread lock, the second thread wakes up the first thread pointed to by the priority lock pointer, so that the first thread acquires the first thread lock.
4. The thread scheduling method according to any one of claims 1 to 3, characterized in that: The thread scheduling method also includes: The fourth thread applies for the second thread lock and enters the waiting queue of the second thread lock; the fourth thread has the function of automatically waking up at multiple automatic wake-up times; At each automatic wake-up moment, the fourth thread searches for the fifth thread; wherein the fifth thread is the current lock-holding thread of the second thread lock, and the fifth thread directly or indirectly blocks the fourth thread; If the fifth thread is not scheduled, the fourth thread adds a third mark to the fifth thread; The operating system preferentially schedules the fifth thread to which the third tag is added.
5. The thread scheduling method according to claim 4, characterized in that: At each automatic wake-up moment, the fourth thread searches for the fifth thread according to the step of: At the first automatic wake-up moment, the fourth thread searches for the locked thread locked by the second thread at the first automatic wake-up moment as the fifth thread corresponding to the first automatic wake-up moment; At the second automatic wake-up moment, the fourth thread searches for the locked thread locked by the second thread at the second automatic wake-up moment as the fifth thread corresponding to the second automatic wake-up moment; The fifth thread corresponding to the first automatic awakening moment and the fifth thread corresponding to the second automatic awakening moment are the same thread or different threads.
6. The thread scheduling method according to claim 4 or 5, characterized in that: The step of the fourth thread searching for the fifth thread comprises: The fourth thread searches for a sixth thread that directly blocks the fourth thread; If the sixth thread that directly blocks the fourth thread acquires the second thread lock, the sixth thread is used as the fifth thread; If the sixth thread that directly blocks the fourth thread does not acquire the second thread lock, searching for the seventh thread that directly blocks the sixth thread; If the seventh thread acquires the second thread lock, the seventh thread is used as the fifth thread.
7. The thread scheduling method according to any one of claims 4 to 6, characterized in that: Before the fourth thread applies for the second thread lock and enters the waiting queue of the second thread lock, the thread scheduling method further includes: If the fourth thread is the main thread, the operating system configures a timer corresponding to the automatic wake-up function for the fourth thread; The timer sends a timed wake-up message to the fourth thread at each automatic wake-up moment; the timed wake-up message is used to wake up the fourth thread at each automatic wake-up moment.
8. The thread scheduling method according to any one of claims 1 to 7, characterized in that: The electronic device stores a bitmap; wherein the bitmap records the blocking relationship between all threads in the electronic device, and the bitmap includes a row-column matrix, the element value of the mth row and the nth column in the row-column matrix indicates the blocking relationship between thread m and thread n, the element value being a first value indicates that the threads are not blocked, and the element value being a second value indicates that the threads are blocked, and m and n are both thread numbers; The thread scheduling method also includes: The operating system queries the element value in the first target row corresponding to the first thread in the bitmap to obtain the blocking relationship between threads in the electronic device.
9. The thread scheduling method according to claim 8, characterized in that: The thread scheduling method also includes: The operating system creates the bitmap and sets the element value in the bitmap to the first value; The eighth thread modifies the element value in the bitmap according to the change of the blocking relationship between threads; wherein the eighth thread is the thread whose blocking relationship between threads changes.
10. The thread scheduling method according to claim 9, characterized in that: The step of the eighth thread modifying the element value in the bitmap according to the change of the blocking relationship between threads includes: When the eighth thread applies for the third thread lock at the first moment, it searches for the ninth thread; wherein the ninth thread is all threads that block the eighth thread at the first moment, and the ninth thread includes the lock holding thread of the third thread lock at the first moment and all threads in the waiting queue of the third thread lock; The eighth thread modifies the element value of the second target column corresponding to each of the ninth threads in the first target row corresponding to the eighth thread in the bitmap to the second value.
11. The thread scheduling method according to claim 10, characterized in that: After the step of the eighth thread modifying the element value of the second target column corresponding to each of the ninth threads in the first target row corresponding to the eighth thread in the bitmap to the second value, the thread scheduling method further includes: When the eighth thread acquires the third thread lock at the second moment, the eighth thread modifies the element value of each second target column in the first target row in the bitmap to the first value; The eighth thread searches for the tenth thread; wherein the tenth thread is all threads in the waiting queue locked by the third thread at the second moment; The eighth thread modifies the element value of the third target row corresponding to each of the tenth threads in the first target column in the bitmap to the second value.
12. The thread scheduling method according to claim 11, characterized in that: After the eighth thread modifies the element value of the third target row corresponding to each of the tenth threads in the first target column of the bitmap to the second value, the thread scheduling method further includes: When the eighth thread releases the first thread lock at the third moment, the eighth thread modifies the element value of each third target row in the first target column in the bitmap to the first value.
13. The thread scheduling method according to any one of claims 8 to 12, characterized in that: The first value is 0, and the second value is 1.
14. An electronic device, characterized in that: The electronic device comprises a memory and a processor, an operating system of the electronic device runs on the processor, and the memory is coupled to the processor; The memory stores computer-executable instructions; The operating system schedules the processor to execute the computer-executable instructions stored in the memory, so that the electronic device executes the thread scheduling method according to any one of claims 1 to 13.
15. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, which, when executed on a computer, enables the computer to execute the thread scheduling method according to any one of claims 1 to 13.
16. A computer program product, characterized in that It comprises a computer program, and when the computer program is executed by a processor, it implements the thread scheduling method according to any one of claims 1 to 13.
Citation Information
Patent Citations
Method and system for reducing multithread program power consumption
CN103324269A
Database lock waiting processing method and device
CN112100192A
Service processing method, electronic equipment and computer readable storage medium
CN112835722A
Thread running method and device, computer equipment and storage medium
CN113742091A
Thread execution method and device, electronic equipment, computer readable medium and product
CN115658288A