Thread reset method, device and equipment of RTOS and medium

CN122924864APending Publication Date: 2026-10-16ZHEJIANG GEELY HLDG GRP CO LTD +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611195357.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-08-07
Publication Date
2026-10-16

AI Technical Summary

Technical Problem

然而,这些方案依赖于对资源的重新分配,不仅需要较长的操作时间,还存在内存资源碎片化风险

Benefits of technology

[0064] The thread reset method, apparatus, device, and medium for RTOS provided in this application involve a first thread obtaining the thread identifier of a second thread to be reset during RTOS runtime, and then calling a pre-encapsulated thread reset interface based on the thread identifier to perform resource reset processing on the second thread to restore it to its initial state when the thread instance was created. On the one hand, since this method directly uses the existing content of the thread to be reset to complete the reset, it can reduce the risk of memory resource fragmentation caused by resource reallocation, thereby improving the security of thread reset and ensuring the security of system operation. On the other hand, compared with reset methods that require two steps, such as deletion and reconstruction, thread reset can be achieved with only a single interface call operation, which can reduce the reset operation time and allow the thread to recover to the schedulable state more quickly, thereby improving the efficiency of thread reset. Correspondingly, since the thread performing the reset operation can recover to the schedulable state more quickly, reducing the thread response time, this method can further improve the overall scheduling efficiency of the system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122924864A_ABST
    Figure CN122924864A_ABST
Patent Text Reader

Abstract

Embodiments of the present application provide a thread reset method, device, equipment and medium of an RTOS. The method comprises: when the RTOS is running, a first thread in the RTOS acquires a thread identifier of a second thread to be reset based on internal business logic; a pre-encapsulated thread reset interface is called based on the thread identifier; resource reset processing is performed on the second thread through the thread reset interface to restore the second thread to an initial state when the thread instance is created; wherein the second thread is the same thread as the first thread or the second thread is another thread in the RTOS. The method is used to achieve the effect of safely and efficiently resetting the thread.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of thread management in real-time operating systems (RTOS), and more particularly to a thread reset method, apparatus, device, and medium for RTOS. Background Technology

[0002] RTOS is widely used in automotive electronics, industrial control, aerospace, medical equipment, and other fields. As the basic unit of system scheduling, the running state of a thread directly affects the stability and response speed of the system.

[0003] Currently, a common requirement in real-world RTOS applications is to restore a thread that has already run, may be in a running, suspended, or abnormal state, to its initial state and re-enable it. This operation is conceptually known as thread "reset".

[0004] Existing solutions typically involve deleting and rebuilding threads or terminating and restarting them. However, these solutions rely on resource reallocation, which not only requires a long operation time but also carries the risk of memory fragmentation. Therefore, how to efficiently and safely perform thread reset is a pressing technical problem that needs to be solved. Summary of the Invention

[0005] This application provides a thread reset method, apparatus, device, and medium for an RTOS, which achieves a safe and efficient thread reset effect.

[0006] In a first aspect, embodiments of this application provide a thread reset method for an RTOS, comprising:

[0007] During RTOS runtime, the first thread in the RTOS obtains the thread identifier of the second thread to be reset based on internal business logic;

[0008] The pre-encapsulated thread reset interface is invoked based on the thread identifier;

[0009] The resource reset process of the second thread is performed through the thread reset interface to restore the second thread to its initial state when the thread instance was created.

[0010] The second thread is either the same thread as the first thread or it is another thread in the RTOS.

[0011] In one possible implementation, performing resource reset processing on the second thread through the thread reset interface to restore the second thread to its initial state when the thread instance was created includes:

[0012] Release the system resources occupied by the second thread;

[0013] Restore the state fields in the control block of the second thread to their initial configuration when the thread instance was created;

[0014] Reset the execution context of the second thread.

[0015] In one possible implementation, the method further includes:

[0016] If the second thread is currently in a ready or running state, then change the second thread to a suspended state.

[0017] In one possible implementation, resetting the execution context of the second thread includes:

[0018] The RTOS's SYSCALL hardware instruction is triggered, and the system call handler is entered.

[0019] In the kernel privileged runtime environment of the system call handler, the execution context of the second thread is reset.

[0020] In one possible implementation, resetting the execution context of the second thread includes:

[0021] After performing the global interrupt disable operation, the execution context of the second thread is reset;

[0022] The global interrupt disable operation is used to mask all interrupt requests from the RTOS.

[0023] In one possible implementation, before resetting the execution context of the second thread, the method further includes:

[0024] Create a target stack space; the target stack space is configured to be used exclusively for thread reset;

[0025] Accordingly, resetting the execution context of the second thread includes:

[0026] If the second thread and the first thread are the same thread, then the current execution stack of the first thread is switched from the original stack space to the target stack space;

[0027] Using the target stack space as the runtime environment, the execution context of the second thread is reset;

[0028] Switch the current execution stack of the first thread from the target stack space back to the reset original stack space.

[0029] In one possible implementation, after performing resource reset processing on the second thread through the thread reset interface to restore the second thread to its initial state when the thread instance was created, the method further includes:

[0030] A new thread to be executed can be obtained through the thread reset interface;

[0031] The scheduling variables of the RTOS are updated to the new thread to be executed, and the scheduler is triggered to switch the currently running thread to the new thread to be executed.

[0032] Secondly, embodiments of this application provide a thread reset device for an RTOS, comprising:

[0033] The acquisition module is used to acquire the thread identifier of the second thread to be reset based on internal business logic during RTOS runtime, whereby the first thread in the RTOS acquires the thread identifier of the second thread to be reset.

[0034] The calling module is used to call a pre-encapsulated thread reset interface based on the thread identifier;

[0035] The reset module is used to perform resource reset processing on the second thread through the thread reset interface, so as to restore the second thread to the initial state when the thread instance was created;

[0036] The second thread is either the same thread as the first thread or it is another thread in the RTOS.

[0037] In one possible implementation, the reset module includes:

[0038] A resource release unit is used to release the system resources occupied by the second thread.

[0039] A state recovery unit is used to restore the state fields in the control block of the second thread to the initial configuration when the thread instance was created.

[0040] The reset unit is used to reset the execution context of the second thread.

[0041] In one possible implementation, the device further includes:

[0042] The state adjustment module is used to change the second thread to a suspended state if the second thread is currently in a ready or running state.

[0043] In one possible implementation, the reset unit is specifically used for:

[0044] The RTOS's SYSCALL hardware instruction is triggered, and the system call handler is entered.

[0045] In the kernel privileged runtime environment of the system call handler, the execution context of the second thread is reset.

[0046] In one possible implementation, the reset unit is specifically used for:

[0047] After performing the global interrupt disable operation, the execution context of the second thread is reset;

[0048] The global interrupt disable operation is used to mask all interrupt requests from the RTOS.

[0049] In one possible implementation, the device further includes:

[0050] A module is created to create a target stack space; the target stack space is configured to be used exclusively for thread reset.

[0051] Accordingly, the reset unit is specifically used for:

[0052] If the second thread and the first thread are the same thread, then the current execution stack of the first thread is switched from the original stack space to the target stack space;

[0053] Using the target stack space as the runtime environment, the execution context of the second thread is reset;

[0054] Switch the current execution stack of the first thread from the target stack space back to the reset original stack space.

[0055] In one possible implementation, the device further includes:

[0056] The runtime recovery module is used to obtain a new thread to be executed through the thread reset interface;

[0057] The scheduling variables of the RTOS are updated to the new thread to be executed, and the scheduler is triggered to switch the currently running thread to the new thread to be executed.

[0058] Thirdly, embodiments of this application provide an electronic device, including: a memory and a processor;

[0059] The memory stores computer-executed instructions;

[0060] The processor executes computer execution instructions stored in the memory, causing the processor to perform the first aspect and / or various possible implementations of the first aspect as described above.

[0061] Fourthly, embodiments of this application provide a vehicle, including a vehicle body and an electronic device from a third aspect.

[0062] Fifthly, embodiments of this application provide a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, are used to implement the first aspect and / or various possible implementations of the first aspect.

[0063] Sixthly, embodiments of this application provide a computer program product, including a computer program that, when executed by a processor, implements the first aspect and / or various possible implementations of the first aspect.

[0064] The thread reset method, apparatus, device, and medium for RTOS provided in this application involve a first thread obtaining the thread identifier of a second thread to be reset during RTOS runtime, and then calling a pre-encapsulated thread reset interface based on the thread identifier to perform resource reset processing on the second thread to restore it to its initial state when the thread instance was created. On the one hand, since this method directly uses the existing content of the thread to be reset to complete the reset, it can reduce the risk of memory resource fragmentation caused by resource reallocation, thereby improving the security of thread reset and ensuring the security of system operation. On the other hand, compared with reset methods that require two steps, such as deletion and reconstruction, thread reset can be achieved with only a single interface call operation, which can reduce the reset operation time and allow the thread to recover to the schedulable state more quickly, thereby improving the efficiency of thread reset. Correspondingly, since the thread performing the reset operation can recover to the schedulable state more quickly, reducing the thread response time, this method can further improve the overall scheduling efficiency of the system. Attached Figure Description

[0065] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.

[0066] Figure 1 A flowchart illustrating a thread reset method for an RTOS provided in Embodiment 1 of this application;

[0067] Figure 2 This is a flowchart illustrating a thread reset method for an RTOS provided in Embodiment 2 of this application;

[0068] Figure 3This is a flowchart illustrating a thread reset method for an RTOS provided in Embodiment 3 of this application;

[0069] Figure 4 This is a schematic diagram of the structure of a thread reset device for an RTOS provided in Embodiment 4 of this application;

[0070] Figure 5 This is a schematic diagram of the structure of a thread reset device for an RTOS provided in Embodiment 5 of this application;

[0071] Figure 6 This is a schematic diagram of the structure of an electronic device provided in Embodiment Six of this application.

[0072] The accompanying drawings illustrate specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to particular embodiments. Detailed Implementation

[0073] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.

[0074] To facilitate understanding of the technical content of this application, the background technology is described in detail below:

[0075] In the thread management model of an RTOS, the thread is the basic unit of system scheduling. Each thread has an independent thread control block, which stores the thread's running state, priority, stack pointer, program counter, time slice, and state information such as suspended / ready / running. During its lifecycle, a thread enters the ready state after being created by the thread creation function, is scheduled for execution by the scheduler, and is deleted after the thread entry function returns or the thread deletion function is called, thus completing the reclamation of thread instance resources.

[0076] However, in real-world RTOS applications, especially in scenarios with high functional safety requirements (such as ISO 26262 ASIL level requirements), there is a common need: to restore a thread that has already run, and may be in a running, suspended, or abnormal state, to its initial state and re-enable it. This operation is conceptually called thread "reset".

[0077] Currently, some solutions use suspension and then resumption or manual context reset to reset the thread; however, these two solutions are not effective in resetting the thread.

[0078] Suspension and resumption mainly works by changing the thread scheduling state to pause and resume the thread, in order to maintain the apparent continuity of the system. However, suspension and resumption do not actually change the thread’s existing execution context. The program counter, register state, and stack contents remain in the state when the thread was suspended. After resumption, the thread often continues execution from the original interruption point, and cannot achieve true “starting from the beginning” execution.

[0079] Manual context reset allows developers to manually manipulate the thread control block and stack, resetting the program counter to the thread entry address and cleaning up the stack contents. However, this method heavily relies on the non-public implementation details of the system's internal data structures. The internal structure may change between different versions of the system, resulting in poor code portability. Furthermore, directly manipulating kernel data structures poses security risks and may lead to system crashes or undefined behavior.

[0080] Furthermore, some existing solutions employ methods such as deletion followed by reconstruction or termination followed by restart to reset threads. The basic idea behind these methods is to first exit the original thread and then recreate the corresponding thread instance to regain the opportunity to execute. These solutions achieve thread reset through function calls, offering easy portability and a relatively safe way to truly "start from scratch." However, these methods require reallocation of thread stacks and control block memory, resulting in longer operation times and the risk of memory fragmentation, impacting system security and scheduling efficiency. Moreover, deletion and creation operations have significant time uncertainties, making them particularly unsuitable for hard real-time systems.

[0081] Therefore, how to safely and efficiently complete thread reset is an urgent technical problem to be solved. To solve the above problem, this application provides a thread reset method for RTOS. During RTOS runtime, the first thread obtains the thread identifier of the second thread to be reset based on its internal business logic, and calls a pre-encapsulated thread reset interface based on the thread identifier. Through the thread reset interface, resource reset processing is performed on the second thread to restore it to the initial state when the thread instance was created. The second thread can be either the first thread itself or other threads in the RTOS.

[0082] Specifically, this method introduces a standardized, dedicated reset interface into the RTOS that enables thread reset without recreating the thread. This not only allows for a true "start from scratch" but also offers portability due to the interface call mechanism. Furthermore, compared to methods like deletion and reconstruction, this method directly utilizes the existing content of the thread to be reset, reducing the risk of memory fragmentation caused by resource reallocation and thus improving the safety of thread reset and ensuring system security. Moreover, compared to two-step reset methods like deletion and reconstruction, this method only requires a single interface call, reducing reset time and allowing the thread to recover to a schedulable state more quickly, thereby improving efficiency. Consequently, the faster recovery of the thread to a schedulable state and reduced response time further enhances the overall system scheduling efficiency.

[0083] The technical solution of this application and how the technical solution of this application solves the above-mentioned technical problems are described in detail below with specific embodiments. These specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments. The embodiments of this application will now be described with reference to the accompanying drawings.

[0084] Figure 1 This is a flowchart illustrating a thread reset method for an RTOS provided in Embodiment 1 of this application, as shown below. Figure 1 As shown, the method includes:

[0085] S101: During RTOS runtime, the first thread in the RTOS obtains the thread identifier of the second thread to be reset based on internal business logic.

[0086] It should be noted that the method proposed in this application is applicable to RTOS architectures that include an operating system kernel, a thread scheduling module, and multiple threads; this application does not limit the specific RTOS products to which this method is adapted.

[0087] In this application, both the first thread and the second thread are threads configured in the RTOS. The first thread is the execution entity that initiates the reset operation; the second thread is the target thread to be reset. The second thread can be the same thread as the first thread or it can be another thread in the RTOS.

[0088] The thread identifier is used to locate the second thread. In specific implementations, it can be a thread handle, thread number, thread control block pointer, or an index value maintained by the RTOS, as long as it can be recognized by the thread reset interface and used to locate the corresponding thread instance.

[0089] In the specific implementation, during the RTOS runtime phase, the first thread in the running state executes its own business logic; if the business logic determines that a reset operation needs to be initiated, the thread identifier corresponding to the object to be reset is read from the business logic, and the thread pointed to by the thread identifier is identified as the second thread to be reset, thereby carrying out the subsequent reset process.

[0090] Understandably, developers can pre-embed thread reset interface call code in the business logic of each thread, taking into account the reset requirements of various threads under different working conditions. Accordingly, during system runtime, each running thread can complete condition judgment based on its own business logic, and once the preset reset trigger condition is met, it can automatically initiate a reset operation for the second thread.

[0091] S102: Call the pre-encapsulated thread reset interface based on the thread identifier.

[0092] In this step, the thread reset interface is a pre-encapsulated reset call entry point. Its function is to uniformly accept thread reset operations and encapsulate subsequent processing within the RTOS thread management mechanism. Calling this interface based on the thread identifier means that the first thread only needs to submit the thread identifier corresponding to the second thread to the interface, and the interface can complete the location of the target thread and subsequent processing.

[0093] The second thread can be either the first thread or another thread in the RTOS. In other words, a thread can be reset by itself or by another thread.

[0094] In practice, after obtaining the thread identifier of the second thread, the first thread passes that thread identifier as an input parameter to the thread reset interface. This interface can be implemented as a system call entry point in the RTOS kernel, or as a function encapsulation layer exposed by the thread management module. Understandably, with the thread reset interface implemented in a pre-encapsulated form, the first thread does not need to write separate processing logic for different thread states; instead, it can uniformly complete the call through the thread identifier.

[0095] Based on the above analysis, this step unifies the thread reset entry point into a pre-encapsulated interface, decoupling the calling side from the underlying thread processing details. The interface then locates the target thread based on the thread identifier and takes over the subsequent reset process, thus giving the reset operation a clear calling boundary and a consistent execution path, thereby reducing the implementation complexity of thread reset in a multi-threaded system.

[0096] S103: Perform resource reset processing based on the thread identifier through the thread reset interface to restore the first thread to the initial state when the thread instance was created.

[0097] In this step, the resource reset process is a thread reset process, the goal of which is to restore the second thread to the initial state after the thread instance is created. The initial state after the thread instance is created refers to the state when the thread has been created and has the initial running conditions.

[0098] In its implementation, after locating the target thread, the thread reset interface performs resource reset processing on the second thread, restoring it to its initial state when the thread instance was created. Whether the second thread is the same as the first thread or another thread, the thread reset interface can perform the reset processing in a unified manner. Through this processing, the second thread no longer continues its pre-reset running state after reset, but instead returns to its initial state corresponding to when the thread instance was created, thus completing the reset of the second thread. Once the resource reset processing is complete, the second thread can re-enter the runtime system managed by the RTOS.

[0099] The thread reset method for RTOS provided in this application involves a first thread obtaining the thread identifier of a second thread to be reset during RTOS runtime, and then calling a pre-encapsulated thread reset interface based on the thread identifier to perform resource reset processing on the second thread, restoring it to its initial state when the thread instance was created. On the one hand, since this method directly uses the existing content of the thread to be reset to complete the reset, it can reduce the risk of memory resource fragmentation caused by resource reallocation, thereby improving the security of thread reset and ensuring the security of system operation. On the other hand, compared with reset methods that require two steps, such as deletion and reconstruction, thread reset can be achieved with only a single interface call operation, which can reduce the reset operation time and allow the thread to recover to the schedulable state more quickly, thereby improving the efficiency of thread reset. Correspondingly, since the thread performing the reset operation can recover to the schedulable state more quickly, reducing the thread response time, this method can further improve the overall scheduling efficiency of the system.

[0100] Furthermore, Figure 2 This is a flowchart illustrating a thread reset method for an RTOS provided in Embodiment 2 of this application. Based on the above embodiments, the specific implementation of step S103 in the aforementioned embodiments of this application is as follows: Figure 2 As shown, this implementation method can include the following:

[0101] S1031: Release the system resources occupied by the second thread.

[0102] In this step, the resource list of the reset second thread can be traversed through the thread reset interface, and various system resources occupied by the second thread in the example table can be released. System resources are kernel resources actively requested by the thread at runtime, such as mutexes, message buffers, semaphores, event groups, and dynamic heap caches.

[0103] Understandably, this step not only restores the system resource state of the thread to its initial state, but also prevents other threads from deadlocking while waiting for the system resources they occupy during the reset process.

[0104] In one possible implementation, to further ensure the reliable execution of thread reset, the method provided in this embodiment may further include: if the second thread is currently in a ready state or a running state, then change the second thread to a suspended state.

[0105] In the specific implementation, after the first thread obtains the thread identifier of the second thread and calls the thread reset interface, the thread reset interface first queries the current thread state of the second thread and matches the query result with the ready state and the running state.

[0106] When the second thread is determined to be in the ready state, the thread reset interface removes it from the ready queue and marks it as suspended through the thread state management interface provided by the RTOS. When the second thread is determined to be in the running state, the thread reset interface first requests the scheduler to perform preemption or switchout processing on the second thread, and then updates its status field to suspended. If the second thread is currently in a suspended, blocked, or other unschedulable state, its original state remains unchanged, and subsequent resource reset processing continues.

[0107] The method provided in this implementation changes the second thread, which is in a ready or running state, to a suspended state before the resource reset process. This isolates the second thread from the scheduling system during the reset period, allowing the thread reset interface to complete resource reset and execution context restoration even when the thread is no longer involved in execution. Using this method, the reset process of the second thread will not be interrupted by other scheduled tasks, thus improving the stability and controllability of the thread reset process.

[0108] S1032: Restore the state fields in the control block of the second thread to their initial configuration when the thread instance was created.

[0109] In this step, it is necessary to read the initial state data saved when the thread is created and overwrite the state fields of the thread control block to complete the initialization of the relevant states in the control block.

[0110] S1033: Reset the execution context of the second thread.

[0111] In this step, the execution context of the second thread needs to be reset to restore it to its initial state at creation. Specifically, the content to be reset may include the thread's general-purpose registers, program counter, stack space, context-related registers, interrupt-related registers, etc.

[0112] For example, this step requires resetting the program counter to the thread entry address, clearing the general-purpose registers, pointing the stack pointer to the initial position, and restoring the interrupt register to its default value.

[0113] Understandably, this step, by resetting the execution context, such as critical registers and stack contents, ensures that the thread restarts execution from the entry function, achieving a "start from scratch" reset effect.

[0114] Furthermore, depending on whether the CPU architecture of the RTOS deployed by the method supports system calls, the following two methods can be provided to implement this step:

[0115] In one possible implementation, if the RTOS's CPU architecture supports system calls, this step can be implemented using steps A1 and B1 as follows:

[0116] Step A1: Trigger the RTOS's SYSCALL hardware instruction to jump into the system call handler.

[0117] Step B1: In the kernel privileged runtime environment of the system call handler, reset the execution context of the second thread.

[0118] Understandably, in an RTOS architecture, if the CPU architecture supports system calls, the CPU will be configured with two execution environments with different access levels: a user runtime environment (also known as user mode) and a kernel privileged runtime environment (also known as kernel mode). Only in the kernel privileged runtime environment can the CPU access and modify kernel private resources. Furthermore, in this type of RTOS architecture, before making hardware instruction calls, the CPU will run in the user runtime environment, and the first thread cannot directly access or modify the kernel private resources such as the execution context of the second thread. Therefore, to achieve a secure reset of kernel core data such as thread contexts, it is necessary to trigger a runtime environment switch using the SYSCALL hardware instruction, switching to the kernel privileged runtime environment to complete the relevant operations.

[0119] In the specific implementation, when the first thread triggers a reset request for the second thread based on its internal business logic, the reset interface encapsulates the context reset request for the second thread into a system call request and writes it into a kernel-recognizable parameter area. Subsequently, it enters the system call handler by executing the SYSCALL hardware instruction. The system call handler parses the target thread identifier in the kernel privileged runtime environment, locates the thread control block and its execution context storage area corresponding to the second thread, and then the kernel directly resets its execution context data, so that the second thread can restart execution from the preset initial state when it is subsequently rescheduled.

[0120] This implementation establishes a controlled jump path from user mode to kernel mode using the SYSCALL hardware instruction when the RTOS CPU architecture supports system calls. This ensures that the execution context reset is confined to the kernel privileged runtime environment, thereby enabling thread context reset under system call-supported architectures. Furthermore, because this architecture relies on hardware permission isolation mechanisms to constrain access behavior, it avoids the risk of user threads unauthorizedly modifying scheduling-related data, effectively ensuring high execution reliability during the reset process.

[0121] In another possible implementation, if the RTOS's CPU architecture does not support system calls, this step can be taken as step A2:

[0122] Step A2: After performing the global interrupt disable operation, reset the execution context of the second thread. The global interrupt disable operation is used to mask all interrupt requests from the RTOS.

[0123] In practical applications, when executing S1033, it is necessary to first perform a global interrupt disable operation. Specifically, this can be done by executing an interrupt disable instruction that matches the RTOS, so that the reset action is completed within a critical section without interruption. The subsequent "reset the execution context of the second thread" will continue to be executed within the thread reset interface.

[0124] By first disabling global interrupt operations and then resetting the execution context, the second thread can avoid receiving switching requests or preemption requests from other threads during the data reset period. This allows the register values, stack frame contents, and status fields in the execution context to be written once and kept consistent.

[0125] Understandably, after completing the thread reset, the RTOS needs to perform an interrupt enable operation again to restore the response to all interrupt requests.

[0126] This implementation first disables global interrupts when the CPU architecture of the RTOS does not support system calls. This keeps the execution context of the second thread stable when it is reset, reducing context disturbances and state residues during the reset process, thereby ensuring high execution reliability during the reset process.

[0127] Furthermore, in one possible implementation, the "resetting the execution context of the second thread" in the aforementioned implementation can specifically include the following steps a1 to c1:

[0128] Step a1: Determine if the second thread and the first thread are the same thread.

[0129] Understandably, in this implementation, two types of execution context reset logic are specifically distinguished based on whether the second thread is performing a reset on itself. In this step, it is necessary to first determine whether the second thread and the first thread are the same thread. For example, the thread identifier of the second thread can be compared with the thread identifier of the first thread.

[0130] Step b1: If yes, then perform the following steps:

[0131] 1) Create a target stack space, which is configured to be used exclusively for thread reset.

[0132] It should be noted that dynamically creating a new stack space every time a thread self-reset is triggered would result in frequent memory allocations, increasing system overhead and potentially causing memory fragmentation. Therefore, this step can be performed during the system initialization phase, i.e., pre-allocating a global dedicated stack memory as the target stack space during system initialization. Thus, during subsequent system operation, any thread executing a self-reset can reuse this memory area to handle its thread self-reset process, eliminating the need for repeated dynamic memory allocation.

[0133] 2) Switch the current running stack of the first thread from the original stack space to the target stack space, and use the target stack space as the running environment.

[0134] Understandably, the reset operation will clean up and overwrite the original stack data of the thread. If the reset logic is still running in the original thread stack, the reset program's read and write operations on the stack memory will concurrently access the uncleaned thread context data in the original stack, thus causing stack read and write contention. Therefore, it is necessary to switch the current running stack of the thread to be reset to another stack space to avoid stack read and write conflicts during the reset execution phase.

[0135] In view of this, in this implementation, an independent stack space will first be allocated for the reset operation. This stack space does not belong to any thread and is only used for context switching during the reset process. Then, the current running stack of the first thread will be switched from the original stack space to the target stack space to provide the memory resource foundation for subsequent reset operations.

[0136] Regarding the specific implementation of stack switching, for example, the original stack pointer and the execution context information related to the current stack can be saved first, and then the current stack pointer can be switched to the top of the target stack space so that subsequent execution can be completed in the target stack space.

[0137] 3) Using the target stack space as the runtime environment, reset the execution context of the second thread.

[0138] In this step, the first thread (which is also the second thread) will use the target stack space to reset its own execution context, including resetting the program counter to the thread entry address, clearing the general-purpose registers, pointing the stack pointer to the initial position, and restoring the interrupt register to its default value.

[0139] 4) Switch the current execution stack of the first thread from the target stack space back to the original stack space after the reset.

[0140] In this step, after the execution context is reset, the current stack pointer of the first thread needs to be restored from the target stack space to the original stack space, and the reset thread context in the original stack space is used as the official running environment for the second thread.

[0141] Step c1: If not, reset the execution context of the second thread using the original stack space of the first thread as the runtime environment.

[0142] In this step, if it is determined that the first thread and the second thread are not the same thread, then since there is no stack conflict, the stack switching operation can be skipped. That is, the first thread continues to execute in its original stack space while resetting the execution context of the second thread.

[0143] The specific means of "resetting the execution context of the second thread" provided in this implementation method is to first execute the scenario judgment logic through the second thread execution context reset scheme adopted in this implementation method. Only when the thread initiates self-reset will the context reset process be carried out based on the created dedicated stack space. This can avoid the reset failure problem caused by stack conflict in the self-reset scenario and ensure the reliable implementation of the thread self-reset function.

[0144] In another implementation, the "resetting the execution context of the second thread" mentioned above can specifically include the following steps a2 to c2:

[0145] Step a2: Create the target stack space.

[0146] Step b2: Switch the current execution stack of the second thread from the original stack space to the target stack space, and use the target stack space as the execution environment;

[0147] Step c2: Reset the execution context of the second thread;

[0148] Step d2: Switch the current execution stack of the second thread from the target stack space back to the original stack space after the reset.

[0149] This implementation pre-allocates target stack space and performs a stack switching operation on the first thread initiating the reset in all reset scenarios, enabling unified operation logic for reset processing across all scenarios. This eliminates the need for branch judgment processes for different reset scenarios and reduces the computational overhead caused by condition judgment, thereby effectively improving the overall processing efficiency of thread reset.

[0150] In practical applications, based on the aforementioned implementation methods, if the reset operation requires stack switching, and the context reset operation is implemented based on the SYSCALL call hardware instruction, the target stack space to be switched can be the stack used by default in non-SYSCALL mode (i.e., pre-allocated stack space dedicated to thread reset), or it can be the stack used by default in SYSCALL mode (i.e., no additional stack space dedicated to thread reset is needed, and the hardware's default kernel stack is directly reused as the target space). Furthermore, the stack switching operation can be performed manually by the developers. Further, if the hardware system also supports automatic stack switching after a system call is initiated, the stack switching action can be performed automatically by the hardware. Additionally, if the reset operation requires station switching, and the system's CPU architecture does not support system calls, the stack switching operation can be performed manually by the developers.

[0151] The thread reset method for RTOS provided in this application, when performing thread reset through an interface, first releases the system resources occupied by the second thread, so that the system resource state of the thread is restored to its initial state, while avoiding deadlock of other threads waiting for the system resources occupied by the second thread during the reset. Next, by changing the state of the second thread from ready or running to suspended, the reset process can be prevented from being interrupted by other scheduled tasks. By restoring the state fields in the control block of the second thread to the initial configuration when the thread instance was created, the initialization of the relevant states in the control block of the second thread is effectively completed. Finally, by resetting the execution context of the second thread, a "start from scratch" reset effect can be achieved. The above processing method restores the thread resources, state fields, and execution context to be consistent with the initial state, effectively achieving the thread reset effect. Furthermore, this method also enables the system to have thread self-reset capability by pre-allocating an independent target stack space and using a stack switching mechanism. At the same time, this embodiment also provides context reset adapted to different system configurations, so that the thread reset function can be effectively implemented regardless of whether the system supports system calls.

[0152] Furthermore, Figure 3 This is a flowchart illustrating a thread reset method for an RTOS provided in Embodiment 3 of this application, as shown below. Figure 3 As shown, based on the above embodiment, in order to restore the system's scheduling operation, the following steps may be included after step S104:

[0153] S201: Obtain a new thread to be executed through the thread reset interface.

[0154] S202: Update the RTOS scheduling variables to the new thread to be executed, and trigger the scheduler to switch the currently running thread to the new thread to be executed.

[0155] In practical implementation, the RTOS scheduling variable can be set as a pointer variable pointing to the head of the current ready queue or the current highest priority thread. After acquiring a new thread to be executed, the thread reset interface updates this pointer to the address of the corresponding thread control block, enabling the scheduler to read the new execution target accordingly. Subsequently, the interface sends a switch trigger signal to the scheduler. After saving the context of the currently running thread, the scheduler loads the execution context of the new thread to be executed based on the updated scheduling variable and transfers processor control to that thread, thus switching the runtime slot originally occupied by the second thread to the new thread to be executed.

[0156] In the method provided in this application embodiment, thread reset and subsequent scheduling are completed within the same interface control link, so that after the thread reset is completed, the system can directly enter a new thread running state and restart normal running scheduling, effectively ensuring the continuity of RTOS running control.

[0157] Figure 4 This is a schematic diagram of the structure of a thread reset device for an RTOS provided in Embodiment 4 of this application, as shown below. Figure 4 As shown, the thread reset device 30 of the RTOS provided in this embodiment includes:

[0158] The acquisition module 301 is used to obtain the thread identifier of the second thread to be reset based on internal business logic during RTOS runtime.

[0159] Module 302 is invoked to call a pre-encapsulated thread reset interface based on the thread identifier;

[0160] The reset module 303 is used to perform resource reset processing on the second thread through the thread reset interface, so as to restore the second thread to the initial state when the thread instance was created;

[0161] In this case, the second thread is either the same thread as the first thread or the second thread is another thread in the RTOS.

[0162] The thread reset device 40 of the RTOS provided in this embodiment can execute the method provided in the above method embodiment. Its implementation principle and technical effect are similar, and will not be described in detail here.

[0163] Figure 5 This is a schematic diagram of the structure of a thread reset device for an RTOS provided in Embodiment 5 of this application, as shown below. Figure 5 As shown, based on the above embodiments, the RTOS thread reset device 30 provided in this embodiment further includes:

[0164] The state adjustment module 304 is used to change the second thread to a suspended state if the second thread is currently in a ready state or a running state.

[0165] Create module 305 to create the target stack space; the target stack space is configured to be used exclusively for thread reset.

[0166] The runtime recovery module 306 is used to obtain a new thread to be executed through the thread reset interface;

[0167] Update the RTOS scheduling variables to the new thread to be executed, and trigger the scheduler to switch the currently running thread to the new thread to be executed.

[0168] In one possible implementation, the reset module 303 includes:

[0169] The resource release unit is used to release system resources occupied by the second thread.

[0170] The state recovery unit is used to restore the state fields in the control block of the second thread to their initial configuration when the thread instance was created.

[0171] The reset unit is used to reset the execution context of the second thread.

[0172] In one possible implementation, the reset unit is specifically used for:

[0173] The RTOS's SYSCALL hardware instruction is triggered, and the system call handler is jumped to the system call process.

[0174] The execution context of the second thread is reset within the kernel privileged runtime environment of the system call handler.

[0175] In one possible implementation, the reset unit is specifically used for:

[0176] After performing the global interrupt disable operation, the execution context of the second thread is reset;

[0177] The global interrupt disable operation is used to mask all interrupt requests from the RTOS.

[0178] In one possible implementation, the reset unit is specifically used for:

[0179] If the second thread is the same as the first thread, then the current execution stack of the first thread is switched from the original stack space to the target stack space;

[0180] Using the target stack space as the runtime environment, reset the execution context of the second thread;

[0181] Switch the current execution stack of the first thread from the target stack space back to the original stack space after the reset.

[0182] The thread reset device 30 of the RTOS provided in this embodiment can execute the method provided in the above method embodiment. Its implementation principle and technical effect are similar, and will not be described in detail here.

[0183] Figure 6 This is a schematic diagram of the structure of an electronic device provided in Embodiment Six of this application. Figure 6 As shown, the electronic device 40 provided in this embodiment includes at least one processor 401 and a memory 402. Optionally, the electronic device 40 further includes a communication component 403. The processor 401, memory 402, and communication component 403 are connected via a bus 404.

[0184] In a specific implementation, at least one processor 401 executes computer execution instructions stored in memory 402, causing at least one processor 401 to perform the above-described method.

[0185] The specific implementation process of processor 401 can be found in the above method embodiments, and its implementation principle and technical effect are similar. It will not be repeated here.

[0186] In the above embodiments, it should be understood that the processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), etc. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the method disclosed in this invention can be directly implemented by a hardware processor, or implemented by a combination of hardware and software modules within the processor.

[0187] The memory may include random access memory (RAM) in high-speed memory, and may also include non-volatile memory (NVM), such as at least one disk storage device.

[0188] The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. Buses can be categorized as address buses, data buses, control buses, etc. For ease of illustration, the buses shown in the accompanying drawings are not limited to a single bus or a single type of bus.

[0189] This application also provides a vehicle, including a vehicle body and the aforementioned electronic equipment. The electronic equipment is used to implement the above-described method.

[0190] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the above-described method.

[0191] This application also provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the above-described method.

[0192] The aforementioned readable storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as Static Random-Access Memory (SRAM), Electrically Erasable Programmable Read-Only Memory (EEPROM), Erasable Programmable Read-Only Memory (EPROM), Programmable Read-Only Memory (PROM), Read-Only Memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk. The readable storage medium can be any available medium accessible to a general-purpose or special-purpose computer.

[0193] An exemplary readable storage medium is coupled to a processor, enabling the processor to read information from and write information to the readable storage medium. Of course, the readable storage medium can also be a component of the processor. The processor and the readable storage medium can reside within an ASIC. Alternatively, the processor and the readable storage medium can exist as discrete components in a device.

[0194] The division of units is merely a logical functional division; 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 indirect coupling or communication connection through some interfaces, devices, or units, and may be electrical, mechanical, or other forms.

[0195] 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.

[0196] 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.

[0197] If a function is implemented as 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 this invention, or the part that contributes to the prior art, or a part 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 of 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.

[0198] Those skilled in the art will understand that all or part of the steps of the above-described method embodiments can be implemented by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When executed, the program performs the steps of the above-described method embodiments; and the aforementioned storage medium includes various media capable of storing program code, such as ROM, RAM, magnetic disks, or optical disks.

[0199] Finally, it should be noted that other embodiments of the invention will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This invention is intended to cover any variations, uses, or adaptations of the invention that follow the general principles of the invention and include common knowledge or customary techniques in the art not disclosed herein, and is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope.

Claims

1. A thread reset method for an RTOS, characterized in that, The method includes: During RTOS runtime, the first thread in the RTOS obtains the thread identifier of the second thread to be reset based on internal business logic; The pre-encapsulated thread reset interface is invoked based on the thread identifier; The resource reset process of the second thread is performed through the thread reset interface to restore the second thread to its initial state when the thread instance was created. The second thread is either the same thread as the first thread or it is another thread in the RTOS.

2. The method according to claim 1, characterized in that, The step of performing resource reset processing on the second thread through the thread reset interface to restore the second thread to its initial state when the thread instance was created includes: Release the system resources occupied by the second thread; Restore the state fields in the control block of the second thread to their initial configuration when the thread instance was created; Reset the execution context of the second thread.

3. The method according to claim 2, characterized in that, The method further includes: If the second thread is currently in a ready or running state, then change the second thread to a suspended state.

4. The method according to claim 2, characterized in that, The resetting of the execution context of the second thread includes: The RTOS's SYSCALL hardware instruction is triggered, and the system call handler is entered. In the kernel privileged runtime environment of the system call handler, the execution context of the second thread is reset.

5. The method according to claim 2, characterized in that, The resetting of the execution context of the second thread includes: After performing the global interrupt disable operation, the execution context of the second thread is reset; The global interrupt disable operation is used to mask all interrupt requests from the RTOS.

6. The method according to claim 4 or 5, characterized in that, Before resetting the execution context of the second thread, the method further includes: Create a target stack space; the target stack space is configured to be used exclusively for thread reset; Accordingly, resetting the execution context of the second thread includes: If the second thread and the first thread are the same thread, then the current execution stack of the first thread is switched from the original stack space to the target stack space; Using the target stack space as the runtime environment, the execution context of the second thread is reset; Switch the current execution stack of the first thread from the target stack space back to the reset original stack space.

7. The method according to any one of claims 1 to 5, characterized in that, After performing resource reset processing on the second thread through the thread reset interface to restore the second thread to its initial state when the thread instance was created, the method further includes: A new thread to be executed can be obtained through the thread reset interface; The scheduling variables of the RTOS are updated to the new thread to be executed, and the scheduler is triggered to switch the currently running thread to the new thread to be executed.

8. A thread reset device for an RTOS, characterized in that, The device includes: The acquisition module is used to acquire the thread identifier of the second thread to be reset based on internal business logic during RTOS runtime, whereby the first thread in the RTOS acquires the thread identifier of the second thread to be reset. The calling module is used to call a pre-encapsulated thread reset interface based on the thread identifier; The reset module is used to perform resource reset processing on the second thread through the thread reset interface, so as to restore the second thread to the initial state when the thread instance was created; The second thread is either the same thread as the first thread or it is another thread in the RTOS.

9. An electronic device, characterized in that, include: Memory, processor; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory, causing the processor to perform the method as described in any one of claims 1-7.

10. A vehicle, characterized in that, It includes the vehicle body and the electronic equipment as described in claim 9.

11. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the method as described in any one of claims 1-7.