Thread scheduling method and device, electronic equipment and storage medium
By working together with the kernel scheduler and the user thread scheduler, an independent thread environment is created and resources are managed, which solves the inefficiency problem caused by improper scheduling of user threads and kernel threads and achieves efficient and stable thread scheduling.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-28
- Publication Date
- 2026-03-10
AI Technical Summary
In existing technologies, the scheduling of user threads and kernel threads cannot meet actual needs, resulting in problems such as blocking system calls or triggering page missing, which leads to thread scheduling failing to meet actual needs and low execution efficiency.
The kernel scheduler responds to the application's thread request instructions, creating multiple thread environments corresponding to the thread requirements and allocating kernel resources to each thread environment. The user thread scheduler, in response to the kernel scheduler's resource readiness information, selects the corresponding user thread from the user thread library for execution. The kernel scheduler can also respond to thread exceptions and make adjustments to ensure the independence and stability of the thread environment.
It achieves flexibility and stability in thread scheduling, improves the efficiency of thread execution, reduces the risk of deadlock, and ensures full utilization of system resources and application responsiveness.
Smart Images

Figure CN121636065A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of operating system technology, and in particular to a thread scheduling method, a thread scheduling device, an electronic device, and a computer-readable storage medium. Background Technology
[0002] User threads and kernel threads are two different thread management methods in operating systems, differing in implementation, performance, and flexibility. User threads are managed in user space by user-level thread libraries, while kernel threads are directly managed by the operating system kernel, which is responsible for their creation, scheduling, and management. In practice, user threads and kernel threads can be used together to form so-called "lightweight processes." However, during application execution, issues such as blocking system calls or triggering page misses can arise, causing thread scheduling to fail to meet actual needs and resulting in low thread execution efficiency. Summary of the Invention
[0003] The present invention provides a thread scheduling method, apparatus, electronic device, and computer-readable storage medium to solve or partially solve the problems of thread scheduling failing to meet actual scheduling requirements and low execution efficiency.
[0004] This invention discloses a thread scheduling method, involving a user thread scheduler and a kernel scheduler, the method comprising:
[0005] The kernel scheduler responds to the application's thread request instruction by creating at least two first thread environments corresponding to the thread request instruction, and allocates corresponding first kernel resources to each first thread environment;
[0006] In response to receiving resource readiness information sent by the kernel scheduler, the user thread scheduler selects a first user thread corresponding to each of the first kernel resources from the user thread library and executes the corresponding first user thread in each of the first thread environments. The resource readiness information indicates that the system resources corresponding to the kernel resources are ready.
[0007] In some feasible embodiments, it also includes:
[0008] In response to a thread exception occurring in the first user thread, the kernel scheduler designates the first user thread that experienced the exception as the first target user thread and creates a corresponding second thread environment for the first target kernel resource corresponding to the first target user thread.
[0009] The user thread scheduler selects a second user thread from the user thread library that corresponds to the first target kernel resource, and executes the second user thread in the second thread environment.
[0010] In some feasible embodiments, the step of using the kernel scheduler to respond to a thread exception occurring in the first user thread and designating the first user thread that experienced the exception as the first target user thread includes:
[0011] In response to a blocking event or a preemption event occurring in the first user thread, the kernel scheduler designates the first user thread that experienced either the blocking event or the preemption event as the first target user thread.
[0012] In some feasible embodiments, it also includes:
[0013] In response to the end of blocking or preemption of the first target user thread, the kernel scheduler selects any second target kernel resource from the first kernel resources other than the first target kernel resource.
[0014] The kernel scheduler creates a corresponding third thread environment for the second target kernel resource, and sends kernel processing events for the first target user thread to the user thread scheduler through the third thread environment.
[0015] In response to receiving the kernel processing event, the user thread scheduler puts the first target user thread and the second target user thread corresponding to the second target kernel resource back into the user thread library.
[0016] The user thread scheduler stores the first user-mode context and the first kernel resource context corresponding to the first target user thread, as well as the second user-mode context and the second kernel resource context corresponding to the second target user thread.
[0017] In some feasible embodiments, it also includes:
[0018] The user thread scheduler selects a third user thread from the user thread library and executes the third user thread in the third thread environment.
[0019] In some feasible embodiments, it also includes:
[0020] The user thread scheduler obtains the quantity relationship between the first user thread and the first kernel resource, and generates a resource processing notification for the quantity relationship.
[0021] In response to receiving the resource processing notification, the kernel scheduler executes the resource configuration corresponding to the resource processing notification.
[0022] In some feasible embodiments, the execution of the resource configuration corresponding to the resource processing notification includes:
[0023] If the resource processing notification indicates that the number of the first user threads is greater than the number of the first kernel resources, then a third target kernel resource that is idle is searched from the system scope, a fourth thread environment is created for the third target kernel resource, and the first user thread is executed on the fourth thread environment.
[0024] In some feasible embodiments, the execution of the resource configuration corresponding to the resource processing notification includes:
[0025] If the resource processing notification indicates that the number of the first user threads is less than the number of the first kernel resources, then a fourth target kernel resource that is in an idle state is selected from the first kernel resources and the fourth target kernel resource is reclaimed.
[0026] In some feasible embodiments, the kernel scheduler includes at least a kernel interface, and the step of executing resource configuration corresponding to the resource processing notification in response to receiving the resource processing notification includes:
[0027] The kernel scheduler responds to the resource processing notification received through the kernel interface by executing the resource configuration corresponding to the resource processing notification.
[0028] In some feasible embodiments, the kernel scheduler includes a kernel callback interface, and the step of selecting a first user thread corresponding to each of the first kernel resources from the user thread library in response to receiving resource readiness information sent by the kernel scheduler includes:
[0029] In response to receiving resource readiness information sent by the kernel scheduler through the kernel callback interface, the user thread scheduler selects a first user thread from the user thread library corresponding to each of the first kernel resources.
[0030] This invention also discloses a thread scheduling device, which includes at least a user thread scheduler and a kernel scheduler; wherein,
[0031] The kernel scheduler is used to respond to the thread request instruction of the application, create at least two first thread environments corresponding to the thread request instruction, and allocate corresponding first kernel resources to each first thread environment;
[0032] The user thread scheduler is used to respond to receiving resource readiness information sent by the kernel scheduler, select a first user thread corresponding to each of the first kernel resources from the user thread library, and execute the corresponding first user thread in each of the first thread environments. The resource readiness information is information indicating that the system resources corresponding to the kernel resources are ready.
[0033] In some feasible embodiments, the kernel scheduler is used to respond to the occurrence of a thread exception in the first user thread, to take the first user thread that has the exception as the first target user thread, and to create a corresponding second thread environment for the first target kernel resource corresponding to the first target user thread;
[0034] The user thread scheduler is used to select a second user thread from the user thread library that corresponds to the first target kernel resource, and execute the second user thread in the second thread environment.
[0035] In some feasible embodiments,
[0036] The kernel scheduler is configured to, in response to a blocking event or a preemption event occurring in the first user thread, designate the first user thread that experienced the blocking event or the preemption event as the first target user thread.
[0037] In some feasible embodiments, the kernel scheduler is configured to select any second target kernel resource from the first kernel resources other than the first target kernel resource in response to the end of blocking or preemption of the first target user thread;
[0038] The kernel scheduler is used to create a corresponding third thread environment for the second target kernel resource, and send kernel processing events for the first target user thread to the user thread scheduler through the third thread environment;
[0039] The user thread scheduler is used to respond to receiving the kernel processing event by putting the first target user thread and the second target user thread corresponding to the second target kernel resource back into the user thread library;
[0040] The user thread scheduler is used to store the first user-mode context and the first kernel resource context corresponding to the first target user thread, and the second user-mode context and the second kernel resource context corresponding to the second target user thread.
[0041] In some feasible embodiments, the user thread scheduler is used to select a third user thread from the user thread library and execute the third user thread in the third thread environment.
[0042] In some feasible embodiments,
[0043] The user thread scheduler is used to obtain the quantity relationship between the first user thread and the first kernel resource, and generate a resource processing notification for the quantity relationship;
[0044] The kernel scheduler is used to execute the resource configuration corresponding to the resource processing notification in response to receiving the resource processing notification.
[0045] In some feasible embodiments,
[0046] If the resource processing notification indicates that the number of the first user threads is greater than the number of the first kernel resources, the kernel scheduler is used to search for a third target kernel resource that is idle in the system, create a fourth thread environment for the third target kernel resource, and enable the first user threads to execute in the fourth thread environment.
[0047] In some feasible embodiments,
[0048] The kernel scheduler is configured to, if the resource processing notification indicates that the number of the first user threads is less than the number of the first kernel resources, select a fourth target kernel resource that is in an idle state from the first kernel resources and reclaim the fourth target kernel resource.
[0049] In some feasible embodiments, the kernel scheduler includes at least a kernel interface; wherein,
[0050] The kernel scheduler is used to execute resource configuration corresponding to the resource processing notification received through the kernel interface.
[0051] In some feasible embodiments, the kernel scheduler includes a kernel callback interface; wherein,
[0052] The user thread scheduler is used to select a first user thread corresponding to each of the first kernel resources from the user thread library in response to receiving resource ready information sent by the kernel scheduler through the kernel callback interface.
[0053] This invention also discloses an electronic device, including a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus;
[0054] The memory is used to store computer programs;
[0055] When the processor executes a program stored in the memory, it implements the method described in the embodiments of the present invention.
[0056] This invention also discloses a computer-readable storage medium storing instructions that, when executed by one or more processors, cause the processors to perform the methods described in this invention.
[0057] This invention also discloses a computer program product, including a computer program / instruction, wherein when the computer program / instruction is executed, it implements the method described in this invention.
[0058] The embodiments of the present invention have the following advantages:
[0059] In this embodiment of the invention, during thread scheduling, the kernel scheduler can respond to the application's thread request instruction, create at least two first thread environments corresponding to the thread request instruction, and allocate corresponding first kernel resources to each first thread environment. Then, the user thread scheduler can respond to the resource readiness information sent by the kernel scheduler, select the first user thread corresponding to each first kernel resource from the user thread library, and execute the corresponding first user thread in each first thread environment. The resource readiness information indicates that the kernel resources are ready. Thus, during thread scheduling, the kernel scheduler can create corresponding thread environments according to the application's needs, and each thread environment executes its corresponding kernel thread and user thread independently. This ensures both the flexibility of thread scheduling and the stability and efficiency of each thread's execution process. Attached Figure Description
[0060] To more clearly illustrate the technical solution of the present invention, the accompanying drawings used in the description of the present invention will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0061] Figure 1 This is a flowchart of the steps of a thread scheduling method provided in an embodiment of the present invention;
[0062] Figure 2 This is a schematic diagram of the thread scheduling architecture provided in an embodiment of the present invention;
[0063] Figure 3 This is a schematic diagram of the thread structure provided in the embodiments of the present invention;
[0064] Figure 4 This is a schematic diagram of a thread management scenario provided in an embodiment of the present invention;
[0065] Figure 5This is a structural block diagram of a thread scheduling device provided in an embodiment of the present invention;
[0066] Figure 6 This is a block diagram of an electronic device provided in an embodiment of the present invention. Detailed Implementation
[0067] To make the above-mentioned objects, features and advantages of the present invention more apparent and understandable, the present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments.
[0068] As an example, during application runtime, corresponding threads can be scheduled to implement specific application functions. In concurrent programming, thread scheduling can be performed by invoking the appropriate threads through either the user thread scheduler or the kernel thread scheduler.
[0069] For user thread schedulers, users can define scheduling rules based on actual business requirements. During thread switching, there's no need to maintain thread contexts across user and kernel layers, resulting in higher execution efficiency. However, a drawback is that if a user thread executes a blocking system call or triggers a page fault, the user thread scheduler cannot detect such events and schedule other available user threads, wasting CPU (Central Processing Unit) time. Conversely, for kernel thread schedulers, the advantage is that if a user thread built on kernel threads executes a blocking system call or triggers a page fault, the kernel scheduler can detect such events and schedule other available kernel threads. However, a corresponding disadvantage is that user thread scheduling requires cross-user / kernel processing. Furthermore, the scheduler implicitly determines which user thread to execute based on which kernel thread, and kernel threads lack awareness of user business information. Therefore, for specific applications, obtaining the scheduled user thread is not the optimal result, leading to low execution efficiency.
[0070] In this invention, during thread scheduling, the kernel scheduler responds to the application's thread request instructions, creating at least two first thread environments corresponding to the instructions and allocating corresponding first kernel resources to each first thread environment. Then, the user thread scheduler, responding to resource readiness information received from the kernel scheduler, selects first user threads from the user thread library corresponding to each first kernel resource and executes the corresponding first user threads in each first thread environment. The resource readiness information indicates that kernel resources are ready. Thus, during thread scheduling, the kernel scheduler can create corresponding thread environments according to the application's needs, and each thread environment independently executes its corresponding kernel and user threads. This ensures both the flexibility of thread scheduling and the stability and efficiency of each thread's execution.
[0071] To enable those skilled in the art to better understand the technical solutions in the embodiments of the present invention, some technical features involved in the embodiments of the present invention are explained and described below:
[0072] Scheduler Actctrl: The activity control scheduler, which is equivalent to the communication mechanism between the user scheduler and the CPU allocator in the kernel, and also refers to a thread environment similar to kernel threads.
[0073] Page fault: In this embodiment of the invention, a page fault can refer to the process of the system waiting for I / O complete.
[0074] Upcall: A mechanism in which kernel space calls code at a fixed location in user space to send a notification to user space.
[0075] CPU Context: Processor context, including general-purpose registers and system registers used during thread execution.
[0076] Ready list: A collection of executable user threads within an application. Each thread in the ready list has a complete execution environment, including user-mode context and kernel-mode context (CPU Context).
[0077] Reference Figure 1 This diagram illustrates a flowchart of a thread scheduling method provided in an embodiment of the present invention, involving a user thread scheduler and a kernel scheduler, and specifically includes the following steps:
[0078] Step 101: In response to the application's thread request instruction, the kernel scheduler creates at least two first thread environments corresponding to the thread request instruction and allocates corresponding first kernel resources to each first thread environment.
[0079] In this embodiment of the invention, during the operation of an application, the kernel scheduler can create multiple thread environments according to the application's operational needs, especially concurrency requirements, and allocate corresponding kernel resources in one thread environment. It can also select user threads from the user thread library through the user thread scheduler. Thus, through the cooperation between user threads and kernel resources in one thread environment, the parallel processing capabilities of multi-core processors can be fully utilized, and the execution efficiency of thread scheduling can be improved.
[0080] In practical implementation, the thread environment can serve as an execution container for user threads. On one hand, the kernel scheduler can send kernel events to the user thread scheduler within the thread environment; on the other hand, a space can be allocated in the kernel to store the processor context (general-purpose registers, system registers, etc.) associated with the current user thread. Furthermore, the thread environment's data structure can be divided into two parts: the User Stack and the Kernel Stack. The Kernel Stack maps to kernel space and is used by the thread environment during kernel-mode execution. The User Stack maps to user space and is used by the thread environment during user-mode execution.
[0081] Each user thread has its own stack. The user thread scheduler can use the user stack of the thread environment, while the kernel stack is used when a user thread calls the kernel. The kernel also allocates a Statemem (memory region) controlled by the thread environment, which can be used to record the processor context of the thread executing in the thread environment when a thread is blocked or preempted. Furthermore, the user thread scheduler can record which user thread is currently executing within the thread environment.
[0082] It should be noted that user threads and kernel threads are two different levels of thread concepts in the operating system, and their relationship with CPU resources also differs. The relationship between user threads and kernel threads can be described as follows:
[0083] User threads are threads created and managed in user space, typically supported by user-level thread libraries. Operations such as creation, scheduling, and synchronization of user threads do not require kernel intervention, resulting in lower overhead and faster creation and switching speeds. The disadvantage of user threads is that they cannot directly utilize the parallel processing capabilities of multi-core processors, as they need to be mapped to kernel threads to execute on the CPU.
[0084] Kernel threads are threads created and managed directly by the operating system kernel. They run in kernel space and have direct access to kernel resources. Kernel threads can fully utilize the parallel processing capabilities of multi-core processors because they can be independently scheduled to execute on different CPU cores. The creation and management of kernel threads require kernel intervention, therefore their overhead is relatively high.
[0085] The mapping relationship between user threads and kernel threads includes, but is not limited to, the following examples:
[0086] One-to-one model: Each user thread is mapped to a kernel thread. This model can fully utilize the parallel processing capabilities of multi-core processors, but the overhead of creating and managing kernel threads is relatively large.
[0087] Many-to-one model: Multiple user threads are mapped to a single kernel thread. This model allows for the rapid creation and switching of user threads, but it cannot take advantage of the parallel processing capabilities of multi-core processors.
[0088] Many-to-many model: Multiple user threads can be mapped to multiple kernel threads. This model combines the advantages of the previous two, and can balance performance and overhead to a certain extent.
[0089] Furthermore, regarding the relationship between kernel threads and CPU resources: ① Scheduling: Kernel threads are scheduled by the operating system kernel's scheduler. The scheduler decides which kernel thread should execute on the CPU. The scheduler typically allocates CPU time slices based on specific scheduling algorithms (such as round-robin scheduling, priority scheduling, etc.) to ensure that all kernel threads fairly obtain CPU resources. ② Parallel Processing: In multi-core processors or multi-processor systems, kernel threads can execute simultaneously on multiple CPU cores, thus achieving parallel processing. The parallel execution capability of kernel threads can significantly improve the overall system performance and throughput. ③ Resource Management: During execution, kernel threads need to access CPU resources, such as registers, caches, and instruction pipelines. The operating system kernel is responsible for managing these resources, ensuring that kernel threads can efficiently utilize CPU resources while avoiding resource conflicts and contention. ④ Load Balancing: The kernel thread scheduler is also responsible for load balancing, ensuring that the workload is evenly distributed across all CPU cores, preventing some cores from being overloaded while others are idle.
[0090] In other words, user threads and kernel threads are two different levels of thread management in the operating system, and they interact with CPU resources through different mapping relationships. Kernel threads are directly associated with CPU resources and can fully utilize the parallel processing capabilities of multi-core processors, while user threads indirectly utilize CPU resources by mapping to kernel threads.
[0091] In some feasible implementations, the user thread scheduler can reside in user space, while the kernel scheduler can reside in kernel space. During thread scheduling, the kernel scheduler can design corresponding application thread scheduling strategies based on the application's business characteristics. Specifically, the user thread scheduler can record context switches for user threads, request more kernel resources from the kernel scheduler, inform the kernel scheduler of available kernel resources, and receive kernel event notifications from the kernel scheduler. The kernel scheduler can create thread environments, send kernel events to the user thread scheduler based on the corresponding interfaces within the thread environment, receive CPU information from the user thread scheduler, and allocate appropriate CPU resources to the application within the kernel through the CPU allocator. Thus, through the user thread scheduler and application thread scheduling strategies, the execution of user threads on the CPU resources available to the application can be effectively controlled without kernel intervention, effectively ensuring the efficiency and stability of thread execution.
[0092] Furthermore, kernel resources can be virtual CPU resources provided by the kernel scheduler to applications, or they can be real physical CPU resources. The appropriate strategy can be determined based on the specific scenario, and this invention does not impose any restrictions on this.
[0093] In practical implementation, when an application needs to execute multiple tasks simultaneously, or when a task takes a long time to complete, or when a large application task is broken down into multiple smaller tasks, the application can generate corresponding thread request instructions. After detecting the thread request instructions, the kernel scheduler can create corresponding first thread environments and allocate corresponding first kernel resources to each first thread environment. Each first thread environment is independent of the others, and the first kernel resources corresponding to each first thread environment are also independent, thus maintaining a high degree of independence between different thread environments and effectively improving the execution efficiency and stability during thread execution.
[0094] For example, if an application breaks down an application task into n subtasks, the kernel scheduler can create n thread environments that correspond one-to-one with the n subtasks and allocate n kernel resources to the n thread environments. This allows different thread environments to maintain a high degree of independence, effectively improving the execution efficiency and stability of the threads during operation.
[0095] Step 102: In response to receiving resource ready information sent by the kernel scheduler, the user thread scheduler selects a first user thread corresponding to each of the first kernel resources from the user thread library and executes the corresponding first user thread in each of the first thread environments. The resource ready information is information indicating that the kernel resources are ready.
[0096] After the kernel scheduler creates the corresponding thread environment, it can send resource readiness information to the user thread scheduler to inform it that the kernel resources corresponding to the thread environment are ready and the user thread can be selected for execution. In a feasible implementation, the user thread scheduler, upon receiving the resource readiness information from the kernel scheduler, selects the first user thread corresponding to each first kernel resource from the user thread library and executes the corresponding first user thread in each first thread environment. The resource readiness information indicates that the kernel resources are ready. Therefore, during thread scheduling, the kernel scheduler can create corresponding thread environments according to the application's needs. Each thread environment executes its corresponding kernel thread and user thread independently, ensuring both flexibility in thread scheduling and stability and efficiency in the execution of each thread.
[0097] In some feasible implementations, the kernel scheduler can detect the thread state of user threads. When an exception is detected in a user thread, the kernel scheduler can adjust the thread scheduling in a timely manner. In this embodiment of the invention, in response to a thread exception in a first user thread, the kernel scheduler can designate the exception-prone first user thread as the first target user thread, create a corresponding second thread environment for the first target kernel resource corresponding to the first target user thread, and then select the second user thread corresponding to the first target kernel resource from the user thread library through the user thread scheduler. The second user thread is then executed in the second thread environment. Thus, in the event of a user thread exception, the kernel scheduler can create a new thread environment, allowing the user thread scheduler to select a new user thread based on the new thread environment and execute the new user thread in the new thread environment. This improves concurrency, ensures that system resources are fully utilized, guarantees the responsiveness and stability of the application, reduces inter-thread dependencies, and lowers the risk of deadlock.
[0098] User thread exceptions can include blocking events and preemption events. Blocking refers to a thread being blocked while waiting for an event (such as I / O operation completion, acquiring a lock, waiting for a semaphore, etc.). In a blocked state, the thread pauses execution and does not occupy CPU resources until the event it is waiting for occurs. Preemption refers to the operating system forcibly interrupting the currently executing thread and allocating CPU resources to other threads. Optionally, the kernel scheduler can, in response to a blocking event or a preemption event occurring in the first user thread, designate the first user thread that experienced the blocking or preemption event as the first target user thread, so as to create a corresponding second thread environment for the first target kernel resource corresponding to the first target user thread.
[0099] In this embodiment of the invention, when the kernel scheduler detects the end of blocking or preemption, it can, in response to the end of blocking or preemption of the first target user thread, select any second target kernel resource from the first kernel resources other than the first target kernel resource, and create a corresponding third thread environment for the second target kernel resource. The third thread environment then sends a kernel processing event for the first target user thread to the user thread scheduler. Upon receiving the kernel processing event, the user thread scheduler returns the first target user thread and the second target user thread corresponding to the second target kernel resource to the user thread library, and saves the first user-mode context and first kernel resource context corresponding to the first target user thread, and the second user-mode context and second kernel resource context corresponding to the second target user thread. Furthermore, after recording the corresponding contexts, since the third thread environment currently does not have a corresponding user thread, the user thread scheduler can select a third user thread from the user thread library and execute the third user thread in the third thread environment.
[0100] Kernel processing events, also known as kernel notification events, are notifications sent by the operating system kernel to user-space programs when it detects changes in specific conditions or states. These events are typically related to system resource usage, hardware state changes, and file system operations. The purpose of kernel notification events is to enable user-space programs to respond to these changes in a timely manner, thereby achieving more efficient and flexible system management. Furthermore, user-mode context refers to the environment and state in which a user process or thread executes in its user space within the operating system. This can include register states, memory mappings, open file descriptors, environment variables, signal masks, thread-local storage, and security context. Kernel resource context, also known as processor context, refers to the state and environment in which the CPU executes tasks. This can include the contents of internal CPU registers, execution mode (user mode or kernel mode), and the location of the currently executing instruction. Kernel resource context is a crucial factor that the operating system must consider when performing process and thread scheduling, interrupt handling, and exception handling. It can include register states, execution mode, memory management unit state, interrupt and exception handling state, and multi-core / multi-thread state.
[0101] Furthermore, when the operating system switches between processes or threads, it must save the current processor context and load the context of the new process or thread. This process is called context switching. Context switching is a critical operation in the operating system because it directly affects system performance and responsiveness. Proper management and switching of processor context are essential for ensuring stable system operation, preventing resource conflicts, and improving concurrent processing capabilities. The operating system kernel must ensure that processor context can be switched safely and efficiently between different execution environments and tasks.
[0102] Optionally, common types of kernel notification events include: file system events, such as the creation, deletion, and modification of files or directories; device events, such as the insertion or removal of USB devices and changes in hard disk status; process events, such as the creation, termination, and status changes of processes; network events, such as the establishment and disconnection of network connections and data transmission status; and system status events, such as system startup, shutdown, hibernation, and wake-up. This invention does not limit these events.
[0103] In some feasible implementations, the kernel scheduler may include a kernel callback interface. The kernel scheduler can send corresponding kernel notification events to user space through the kernel callback interface, so that the user thread scheduler can perform corresponding operations after detecting the kernel notification event, such as selecting a user thread.
[0104] Correspondingly, in the process of processing the above-mentioned resource readiness information, the kernel scheduler can also send resource readiness information to user space through the kernel callback interface. Then, in response to receiving the resource readiness information sent by the kernel scheduler through the kernel callback interface, the user thread scheduler can select the first user thread corresponding to each first kernel resource from the user thread library. The subsequent processing can be referred to the aforementioned embodiments, and will not be repeated here.
[0105] Furthermore, during thread execution, the user thread scheduler can detect the quantitative relationship between kernel resources and user threads. In this embodiment of the invention, during normal thread scheduling, one user thread corresponds to one kernel resource. By detecting the quantitative relationship between user threads and kernel resources, it can be determined whether kernel resources are redundant or insufficient. In a specific implementation, the user thread scheduler can obtain the quantitative relationship between the first user thread and the first kernel resource, generate a resource processing notification based on the quantitative relationship, and send the resource processing notification to the kernel space. The kernel scheduler can respond to receiving the resource processing notification by executing the resource configuration corresponding to the resource processing notification.
[0106] In the specific implementation, if the resource processing notification indicates that the number of first user threads is greater than the number of first kernel resources, then a third target kernel resource in an idle state is searched from the system scope, a fourth thread environment is created for the third target kernel resource, and the first user thread executes on the fourth thread environment. If the resource processing notification indicates that the number of first user threads is less than the number of first kernel resources, then a fourth target kernel resource in an idle state is selected from the first kernel resources and reclaimed.
[0107] It should be noted that the kernel scheduler may also include a kernel interface. The kernel scheduler can respond to resource processing notifications received through the kernel interface, execute the resource configuration corresponding to the resource processing notification, and thus communicate through the kernel interface, enabling information exchange between the kernel space and the user space, effectively ensuring the stability of thread execution.
[0108] It should be noted that the embodiments of the present invention include, but are not limited to, the examples described above. It is understood that those skilled in the art can make further settings according to actual needs under the guidance of the ideas in the embodiments of the present invention, and the present invention does not limit such settings.
[0109] In this embodiment of the invention, during thread scheduling, the kernel scheduler can respond to the application's thread request instruction, create at least two first thread environments corresponding to the thread request instruction, and allocate corresponding first kernel resources to each first thread environment. Then, the user thread scheduler can respond to the resource readiness information sent by the kernel scheduler, select the first user thread corresponding to each first kernel resource from the user thread library, and execute the corresponding first user thread in each first thread environment. The resource readiness information indicates that the kernel resources are ready. Thus, during thread scheduling, the kernel scheduler can create corresponding thread environments according to the application's needs, and each thread environment executes its corresponding kernel thread and user thread independently. This ensures both the flexibility of thread scheduling and the stability and efficiency of each thread's execution process.
[0110] To enable those skilled in the art to better understand the technical solutions in the embodiments of the present invention, the following examples are provided for illustrative purposes:
[0111] Reference Figure 2This diagram illustrates the thread scheduling architecture provided in this embodiment of the invention. It demonstrates a scheduling mechanism that combines the functionality of the Kernel scheduler with the flexibility and high performance of the User scheduler through a User scheduler in conjunction with a newly added kernel interface. The functions of each component in the management and control architecture (Scheduler Actctrl) are as follows: Application scheduling strategy refers to the application thread scheduling strategy designed according to the characteristics of the application business; the User scheduler (also known as the user thread scheduler) can be used to implement user-mode context switching, request more CPU resources from the Kernel (kernel scheduler) or inform the Kernel that there is CPU available, and receive Kernel event notifications; the Kernel can include a kernel callback interface (Kernel upcaller), a kernel interface (Kernel interface), and a CPU allocator. The kernel callback interface can be used to send Kernel events to the User scheduler, the kernel interface can be used to receive CPU information issued by the User scheduler, and the CPU allocator can be used to allocate CPU resources to the App in the kernel. Thus, through the cooperation between the User scheduler and the application scheduling strategy, it is possible to control which CPU the User thread executes on without Kernel intervention. Correspondingly, the kernel can report relevant kernel events to user space through the kernel callback interface.
[0112] It should be noted that the aforementioned management and control architecture can serve as an execution container for user threads, similar to a traditional Kernel Thread. It also sends Kernel events to the User Scheduler and allocates space within the kernel to store the processor context (general-purpose registers, system registers, etc.) associated with the current user thread. Furthermore, refer to... Figure 3This diagram illustrates the thread structure provided in this embodiment of the invention. The Scheduler Actctrl data structure is similar to the traditional Kernel Thread, divided into two parts: User Stack and Kernel Stack. The Kernel Stack is mapped to kernel space and is used by Actctrl during kernel execution. The User Stack is mapped to user space and is used by Actctrl during user execution. Each user thread has its own Stack. The User scheduler uses the User Stack of Actctrl, and when a user thread calls the kernel, it uses the ActctrlKernel Stack. The kernel also allocates a memory region called State mem controlled by Actctrl. When the Actctrl thread is blocked or preempted, the processor context of the thread executing on Actctrl is recorded. In addition, the User scheduler records which user thread is currently executing on the Actctrl thread.
[0113] Based on the aforementioned management and control architecture, when an application starts, the kernel can create a corresponding Actctrl thread environment and allocate an execution CPU (i.e., kernel resource) to it. Then, through the kernel callback interface in the Actctrl thread environment, it calls the User scheduler initialization code in user space. After the User scheduler's initialization code completes its own initialization, it calls the application's entry point function. Next, after the application has finished starting, if it needs to create more threads and require more CPU resources during runtime, the kernel can create new Actctrl thread environments, allocate corresponding execution CPUs to each Actctrl thread environment, and then notify the User scheduler through the kernel callback interface that the new CPU resources are ready, allowing other user threads to execute in the new Actctrl thread environments. Correspondingly, when the kernel needs to send a notification to the User scheduler, it can also create a corresponding Actctrl thread environment and then inform the User scheduler of the specific kernel notification event through the kernel interface.
[0114] The kernel notification events reported by the Kernel to the User scheduler may include at least the following:
[0115] ① Add CPU resources to the App
[0116] ②App CPU resources are being preempted
[0117] ③Actctrl thread block (due to blocking system calls or page faults, etc.)
[0118] ④Actctrl thread unblock
[0119] For the kernel notification events mentioned above, the Kernel can report them to the User scheduler through the following interface:
[0120] add_current_CPU(CPU_id):
[0121] After receiving the kernel notification event, the User scheduler executes a user thread on the Actctrl thread.
[0122] CPU_be_preempted(preempt Actctrl_id,CPU Context):
[0123] Upon receiving this kernel notification event, the user thread executing on the preempted Actctrl thread will be returned to the readylist (user thread library).
[0124] Actctrl_Block(Blocked Actctrl_id):
[0125] Upon receiving the kernel notification event, the User scheduler selects to execute other user threads on the readylist.
[0126] Actctrl_unBlock(unBlockedActctrl_id, CPU Context):
[0127] Upon receiving the kernel notification event, the user thread is returned to the readylist, and one of the threads on the readylist is selected for execution.
[0128] In one example, refer to Figure 4 This diagram illustrates a scenario of thread management provided in an embodiment of the present invention. The straight arrows represent the Actctrl thread environment, the S-shaped arrows represent user threads, and the set of S-shaped arrows on the right side of the diagram represents the readylist. Furthermore, it involves both user-level and kernel-level runtime systems; user threads run in the user-level runtime system, and kernel threads run in the kernel-level runtime system.
[0129] At time T1, the Kernel allocates two CPUs to the App. On each CPU, the Kernel uses an upcall to instruct the User scheduler to select two user threads from the readylist and execute them in the Actctrl thread environment.
[0130] At time T2, User Thread1 is blocked within the Kernel. The Kernel creates a new Actctrl thread environment on the CPU executing User Thread1 and notifies the User scheduler of User Thread1's block via an upcall. Upon receiving the notification, the User scheduler will select a user thread from the readylist to execute on the new Actctrl thread.
[0131] At time T3, I / O is complete, and the kernel needs to notify the User scheduler User Thread1 to unblock.
[0132] The kernel creates a new Actctrl thread environment and preempts a CPU under the App to report events.
[0133] In the new Actctrl thread, the User scheduler is simultaneously notified of the following two kernel notification events via a single upcall, and the CPU Context of the user thread stored in the Statemem is returned:
[0134] ①User Thread1 unblock
[0135] ②The CPU used by Actctrl B is preempted.
[0136] After receiving the notification, the User scheduler performs two actions:
[0137] 1. Save the user-mode context and CPU context of User Thread1, and put them back into readylist;
[0138] 2. Save the user-mode context and CPU context of the preempted User Thread and put them back into readylist. At this point, the original A and B Actctrl thread environments can be destroyed.
[0139] At time T4, the User scheduler selects a user thread from the readylist to execute in the Actctrl D thread environment created at time T3.
[0140] The above mechanism solves the problem of user threads not being able to be blocked and ensures that the number of Actctrl threads in the App is consistent with the number of CPU resources allocated to the App.
[0141] Furthermore, during thread execution, the User scheduler can request CPU resources from the Kernel. CPU resource allocation is handled by the CPU allocator within the kernel, and users can influence the allocator's outcome by calling a newly added Kernel interface. When the number of user threads in an app is less than its available CPU resources, or when the app's available CPU resources exceed the number of user threads, the User scheduler sends a notification to the Kernel through the Kernel interface. The interfaces provided by the Kernel to the User scheduler are as follows:
[0142] need_more_CPUs(CPU_num)
[0143] When the number of executable threads in an app exceeds the CPU resources held by the app, it requests CPU from the kernel.
[0144] current_CPU_idle()
[0145] When the CPU resources held by the app are greater than the number of executable threads, the kernel is notified that there is idle CPU.
[0146] When the kernel receives a CPU idle notification, it will destroy the Actctrl thread belonging to the app that is running on the idle CPU and reclaim the CPU resources.
[0147] When the kernel receives a CPU resource request, it searches for an idle CPU system-wide. If an idle CPU is found, a new Actctrl thread is created, and the User scheduler is notified via upcall. If no idle CPU is found, it waits until an available idle CPU becomes available.
[0148] It should be noted that, for the sake of simplicity, the method embodiments are all described as a series of actions. However, those skilled in the art should understand that the embodiments of the present invention are not limited to the described order of actions, because according to the embodiments of the present invention, some steps can be performed in other orders or simultaneously. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are preferred embodiments, and the actions involved are not necessarily essential to the embodiments of the present invention.
[0149] Reference Figure 5 The diagram illustrates a structural block diagram of a thread scheduling device provided in an embodiment of the present invention. The scheduling device includes at least a kernel scheduler and a user thread scheduler, and may specifically include the following modules:
[0150] The kernel scheduler is used to respond to the thread request instruction of the application, create at least two first thread environments corresponding to the thread request instruction, and allocate corresponding first kernel resources to each first thread environment;
[0151] The user thread scheduler is used to respond to receiving resource readiness information sent by the kernel scheduler, select a first user thread corresponding to each of the first kernel resources from the user thread library, and execute the corresponding first user thread in each of the first thread environments. The resource readiness information is information indicating that the system resources corresponding to the kernel resources are ready.
[0152] In some feasible embodiments, the kernel scheduler is used to respond to the occurrence of a thread exception in the first user thread, to take the first user thread that has the exception as the first target user thread, and to create a corresponding second thread environment for the first target kernel resource corresponding to the first target user thread;
[0153] The user thread scheduler is used to select a second user thread from the user thread library that corresponds to the first target kernel resource, and execute the second user thread in the second thread environment.
[0154] In some feasible embodiments,
[0155] The kernel scheduler is configured to, in response to a blocking event or a preemption event occurring in the first user thread, designate the first user thread that experienced the blocking event or the preemption event as the first target user thread.
[0156] In some feasible embodiments, the kernel scheduler is configured to select any second target kernel resource from the first kernel resources other than the first target kernel resource in response to the end of blocking or preemption of the first target user thread;
[0157] The kernel scheduler is used to create a corresponding third thread environment for the second target kernel resource, and send kernel processing events for the first target user thread to the user thread scheduler through the third thread environment;
[0158] The user thread scheduler is used to respond to receiving the kernel processing event by putting the first target user thread and the second target user thread corresponding to the second target kernel resource back into the user thread library;
[0159] The user thread scheduler is used to store the first user-mode context and the first kernel resource context corresponding to the first target user thread, and the second user-mode context and the second kernel resource context corresponding to the second target user thread.
[0160] In some feasible embodiments, the user thread scheduler is used to select a third user thread from the user thread library and execute the third user thread in the third thread environment.
[0161] In some feasible embodiments,
[0162] The user thread scheduler is used to obtain the quantity relationship between the first user thread and the first kernel resource, and generate a resource processing notification for the quantity relationship;
[0163] The kernel scheduler is used to execute the resource configuration corresponding to the resource processing notification in response to receiving the resource processing notification.
[0164] In some feasible embodiments,
[0165] If the resource processing notification indicates that the number of the first user threads is greater than the number of the first kernel resources, the kernel scheduler is used to search for a third target kernel resource that is idle in the system, create a fourth thread environment for the third target kernel resource, and enable the first user threads to execute in the fourth thread environment.
[0166] In some feasible embodiments,
[0167] The kernel scheduler is configured to, if the resource processing notification indicates that the number of the first user threads is less than the number of the first kernel resources, select a fourth target kernel resource that is in an idle state from the first kernel resources and reclaim the fourth target kernel resource.
[0168] In some feasible embodiments, the kernel scheduler includes at least a kernel interface; wherein,
[0169] The kernel scheduler is used to execute resource configuration corresponding to the resource processing notification received through the kernel interface.
[0170] In some feasible embodiments, the kernel scheduler includes a kernel callback interface; wherein,
[0171] The user thread scheduler is used to select a first user thread corresponding to each of the first kernel resources from the user thread library in response to receiving resource ready information sent by the kernel scheduler through the kernel callback interface.
[0172] As the device embodiment is basically similar to the method embodiment, the description is relatively simple, and relevant parts can be found in the description of the method embodiment.
[0173] In addition, this invention also provides an electronic device, including: a processor, a memory, and a computer program stored in the memory and executable on the processor. When the computer program is executed by the processor, it implements the various processes of the above-described thread scheduling method embodiments and achieves the same technical effect. To avoid repetition, it will not be described again here.
[0174] This invention also discloses a computer program product, including a computer program / instruction. When the computer program / instruction is executed, it implements the various processes of the above-described thread scheduling method embodiment and achieves the same technical effect. To avoid repetition, it will not be described again here.
[0175] This invention also provides a computer-readable storage medium storing a computer program. When the computer program is executed by a processor, it implements the various processes of the thread scheduling method embodiments described above and achieves the same technical effect. To avoid repetition, it will not be described again here. The computer-readable storage medium may be a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk, etc.
[0176] Figure 6 A schematic diagram of the hardware structure of an electronic device for implementing various embodiments of the present invention.
[0177] The electronic device 600 includes, but is not limited to, components such as: a radio frequency unit 601, a network module 602, an audio output unit 603, an input unit 604, a sensor 605, a display unit 606, a user input unit 607, an interface unit 608, a memory 609, a processor 610, and a power supply 611. Those skilled in the art will understand that the electronic device structure involved in the embodiments of the present invention does not constitute a limitation on the electronic device. The electronic device may include more or fewer components than illustrated, or combine certain components, or have different component arrangements. In the embodiments of the present invention, the electronic device includes, but is not limited to, mobile phones, tablet computers, laptop computers, PDAs, in-vehicle terminals, wearable devices, and pedometers.
[0178] It should be understood that, in this embodiment of the invention, the radio frequency unit 601 can be used for receiving and transmitting signals during information transmission or calls. Specifically, it receives downlink data from the base station and processes it with the processor 610; additionally, it transmits uplink data to the base station. Typically, the radio frequency unit 601 includes, but is not limited to, an antenna, at least one amplifier, a transceiver, a coupler, a low-noise amplifier, a duplexer, etc. Furthermore, the radio frequency unit 601 can also communicate with networks and other devices through a wireless communication system.
[0179] The electronic device provides users with wireless broadband internet access through the network module 602, such as helping users send and receive emails, browse web pages, and access streaming media.
[0180] The audio output unit 603 can convert audio data received by the radio frequency unit 601 or the network module 602 or stored in the memory 609 into audio signals and output them as sound. Furthermore, the audio output unit 603 can also provide audio output related to specific functions performed by the electronic device 600 (e.g., call signal reception sound, message reception sound, etc.). The audio output unit 603 includes a speaker, a buzzer, and a receiver, etc.
[0181] Input unit 604 is used to receive audio or video signals. Input unit 604 may include a graphics processing unit (GPU) 6041 and a microphone 6042. GPU 6041 processes image data of still images or videos acquired by an image capture device (such as a camera) in video capture mode or image capture mode. The processed image frames can be displayed on display unit 606. The image frames processed by GPU 6041 can be stored in memory 609 (or other storage medium) or transmitted via radio frequency unit 601 or network module 602. Microphone 6042 can receive sound and process such sound into audio data. The processed audio data can be converted into a format that can be transmitted to a mobile communication base station via radio frequency unit 601 in telephone call mode.
[0182] The electronic device 600 also includes at least one sensor 605, such as a light sensor, a motion sensor, and other sensors. Specifically, the light sensor includes an ambient light sensor and a proximity sensor. The ambient light sensor can adjust the brightness of the display panel 6061 according to the ambient light level, and the proximity sensor can turn off the display panel 6061 and / or backlight when the electronic device 600 is moved to the ear. As a type of motion sensor, an accelerometer sensor can detect the magnitude of acceleration in various directions (generally three axes). When stationary, it can detect the magnitude and direction of gravity and can be used to identify the posture of the electronic device (such as landscape / portrait switching, related games, magnetometer posture calibration), vibration recognition related functions (such as pedometer, tapping), etc. The sensor 605 may also include a fingerprint sensor, pressure sensor, iris sensor, molecular sensor, gyroscope, barometer, hygrometer, thermometer, infrared sensor, etc., which will not be described in detail here.
[0183] The display unit 606 is used to display information input by the user or information provided to the user. The display unit 606 may include a display panel 6061, which may be configured in the form of a liquid crystal display (LCD), an organic light-emitting diode (OLED), or the like.
[0184] User input unit 607 can be used to receive input numerical or character information, and to generate key signal inputs related to user settings and function control of electronic devices. Specifically, user input unit 607 includes a touch panel 6071 and other input devices 6072. Touch panel 6071, also known as a touch screen, can collect touch operations performed by the user on or near it (such as operations performed by the user using a finger, stylus, or any suitable object or accessory on or near touch panel 6071). Touch panel 6071 may include two parts: a touch detection device and a touch controller. The touch detection device detects the user's touch position and the signal generated by the touch operation, and transmits the signal to the touch controller; the touch controller receives touch information from the touch detection device, converts it into touch point coordinates, and sends it to the processor 610, which receives and executes commands from the processor 610. In addition, touch panel 6071 can be implemented using various types such as resistive, capacitive, infrared, and surface acoustic wave. Besides touch panel 6071, user input unit 607 may also include other input devices 6072. Specifically, other input devices 6072 may include, but are not limited to, physical keyboards, function keys (such as volume control buttons, power buttons, etc.), trackballs, mice, joysticks, etc., which will not be described in detail here.
[0185] Furthermore, the touch panel 6071 can cover the display panel 6061. When the touch panel 6071 detects a touch operation on or near it, it transmits the information to the processor 610 to determine the type of touch event. Subsequently, the processor 610 provides corresponding visual output on the display panel 6061 according to the type of touch event. It is understood that in one embodiment, the touch panel 6071 and the display panel 6061 are implemented as two independent components to realize the input and output functions of the electronic device. However, in some embodiments, the touch panel 6071 and the display panel 6061 can be integrated to realize the input and output functions of the electronic device. The specific implementation is not limited here.
[0186] Interface unit 608 serves as an interface for connecting external devices to electronic device 600. For example, external devices may include a wired or wireless headphone port, an external power supply (or battery charger) port, a wired or wireless data port, a memory card port, a port for connecting a device with an identification module, an audio input / output (I / O) port, a video I / O port, a headphone port, and so on. Interface unit 608 can be used to receive input from external devices (e.g., data, power, etc.) and transmit the received input to one or more components within electronic device 600, or it can be used to transmit data between electronic device 600 and external devices.
[0187] The memory 609 can be used to store software programs and various data. The memory 609 may primarily include a program storage area and a data storage area. The program storage area may store the operating system, applications required for at least one function (such as sound playback, image playback, etc.), etc.; the data storage area may store data created based on the use of the mobile phone (such as audio data, phonebook, etc.). Furthermore, the memory 609 may include high-speed random access memory, and may also include non-volatile memory, such as at least one disk storage device, flash memory device, or other volatile solid-state storage device.
[0188] The processor 610 is the control center of the electronic device. It connects various parts of the electronic device via various interfaces and lines. By running or executing software programs and / or modules stored in the memory 609, and by calling data stored in the memory 609, it performs various functions and processes data, thereby providing overall monitoring of the electronic device. The processor 610 may include one or more processing units; preferably, the processor 610 may integrate an application processor and a modem processor. The application processor mainly handles the operating system, user interface, and applications, while the modem processor mainly handles wireless communication. It is understood that the modem processor may not be integrated into the processor 610.
[0189] The electronic device 600 may also include a power supply 611 (such as a battery) for supplying power to various components. Preferably, the power supply 611 is logically connected to the processor 610 through a power management system, thereby enabling functions such as managing charging, discharging, and power consumption through the power management system.
[0190] In addition, the electronic device 600 includes some functional modules not shown, which will not be described in detail here.
[0191] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.
[0192] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods of the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes several instructions to cause a terminal (which may be a mobile phone, computer, server, air conditioner, or network device, etc.) to execute the methods described in the various embodiments of the present invention.
[0193] The embodiments of the present invention have been described above with reference to the accompanying drawings. However, the present invention is not limited to the specific embodiments described above. The specific embodiments described above are merely illustrative and not restrictive. Those skilled in the art can make many other forms under the guidance of the present invention without departing from the spirit and scope of the claims, and all of these forms are within the protection scope of the present invention.
[0194] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed in this invention can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementations should not be considered beyond the scope of this invention.
[0195] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0196] In the embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative. For instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.
[0197] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0198] In addition, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0199] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, essentially, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, ROM, RAM, magnetic disks, or optical disks.
[0200] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A method of scheduling threads, characterized by, The method relates to a user thread scheduler and a kernel scheduler, and comprises the following steps: creating, by the kernel scheduler, at least two first thread environments corresponding to thread demand instructions of an application program, and allocating corresponding first kernel resources to each of the first thread environments in response to the thread demand instructions of the application program; selecting, by the user thread scheduler, a first user thread corresponding to each of the first kernel resources from a user thread library, and executing the corresponding first user thread in each of the first thread environments in response to receiving resource ready information sent by the kernel scheduler, the resource ready information being information indicating that a system resource corresponding to a kernel resource is ready.
2. The method of claim 1, wherein, Further comprising: creating, by the kernel scheduler, a corresponding second thread environment for a first target kernel resource corresponding to a first target user thread in response to a thread exception of the first user thread; selecting, by the user thread scheduler, a second user thread corresponding to the first target kernel resource from the user thread library, and executing the second user thread in the second thread environment.
3. The method of claim 2, wherein, The step of creating, by the kernel scheduler, a corresponding second thread environment for a first target kernel resource corresponding to a first target user thread in response to a thread exception of the first user thread comprises the following steps: selecting, by the kernel scheduler, a first target user thread in response to a blocking event of the first user thread or a preemption event of the first user thread.
4. The method of claim 3, wherein, Further comprising: selecting, by the kernel scheduler, any second target kernel resource from the first kernel resources except the first target kernel resource in response to an end of the blocking of the first target user thread or an end of the preemption of the first target user thread; creating, by the kernel scheduler, a corresponding third thread environment for the second target kernel resource, and sending, by the third thread environment, a kernel processing event for the first target user thread to the user thread scheduler; returning, by the user thread scheduler, the first target user thread and a second target user thread corresponding to the second target kernel resource to the user thread library in response to receiving the kernel processing event; saving, by the user thread scheduler, a first user state context and a first kernel resource context corresponding to the first target user thread, and a second user state context and a second kernel resource context corresponding to the second target user thread.
5. The method of claim 4, wherein, Further comprising: selecting, by the user thread scheduler, a third user thread from the user thread library, and executing the third user thread in the third thread environment.
6. The method according to any one of claims 1 to 5, characterized in that, Further comprising: acquiring, by the user thread scheduler, a quantity relationship between the first user thread and the first kernel resource, and generating a resource processing notification for the quantity relationship; executing, by the kernel scheduler, a resource configuration corresponding to the resource processing notification in response to receiving the resource processing notification.
7. The method of claim 6, wherein, The step of executing, by the kernel scheduler, a resource configuration corresponding to the resource processing notification comprises the following steps: If the resource processing notification represents that the number of the first user threads is greater than the number of the first kernel resources, a third target kernel resource in an idle state is searched from a system range, a fourth thread environment is created for the third target kernel resource, and the first user threads are executed on the fourth thread environment.
8. The method of claim 6, wherein, The execution of the resource configuration corresponding to the resource processing notification comprises: If the resource processing notification represents that the number of the first user threads is less than the number of the first kernel resources, a fourth target kernel resource in an idle state is selected from the first kernel resources, and the fourth target kernel resource is recycled.
9. The method of claim 6, wherein, The kernel scheduler at least comprises a kernel interface, and the execution of the resource configuration corresponding to the resource processing notification by the kernel scheduler in response to the reception of the resource processing notification comprises: The execution of the resource configuration corresponding to the resource processing notification by the kernel scheduler in response to the reception of the resource processing notification by the kernel interface.
10. The method of claim 1, wherein, The kernel scheduler comprises a kernel callback interface, and the selection of the first user thread corresponding to each of the first kernel resources from a user thread library by the user thread scheduler in response to the reception of the resource ready information sent by the kernel scheduler comprises: The selection of the first user thread corresponding to each of the first kernel resources from a user thread library by the user thread scheduler in response to the reception of the resource ready information sent by the kernel scheduler through the kernel callback interface.
11. A thread scheduling apparatus, characterized by comprising: The scheduling apparatus at least comprises a user thread scheduler and a kernel scheduler; wherein, The kernel scheduler is configured to create at least two first thread environments corresponding to thread demand instructions of an application program in response to the thread demand instructions, and allocate corresponding first kernel resources to each of the first thread environments; The user thread scheduler is configured to select first user threads corresponding to each of the first kernel resources from a user thread library in response to the reception of resource ready information sent by the kernel scheduler, and execute the corresponding first user threads in each of the first thread environments, the resource ready information being information representing that system resources corresponding to kernel resources are ready.
12. An electronic device, comprising: The apparatus comprises a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory complete mutual communication through the communication bus; The memory is configured to store a computer program; The processor is configured to execute the program stored on the memory, and implement the method of any one of claims 1-10.
13. A computer readable storage medium having instructions stored thereon, which when executed by one or more processors, cause the processors to perform the method of any one of claims 1-10.
14. A computer program product, characterised in that, The apparatus comprises a computer program / instruction, which when executed, implements the method of any one of claims 1-10. The apparatus comprises a computer program / instruction, which when executed, implements the method of any one of claims 1-10.