Method and system for static task to flat dynamic thread
By creating unregistered tasks in the AUTOSAR CP system and dynamically registering callback functions, the problem that static tasks in the system cannot be dynamically adjusted is solved, the system's scalability and flexibility is improved, and efficient porting and resource optimization of multi-threaded applications are realized.
Patent Information
- Application Number
- CN202411082072.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-08
- Publication Date
- 2024-11-22
AI Technical Summary
Static tasks cannot be created and destroyed dynamically in the AUTOSAR CP system, resulting in the system being unable to adjust when load changes, lacking scalability and flexibility, making it difficult to achieve efficient porting of multi-threaded applications.
Dynamic execution of static tasks is achieved by creating unregistered tasks in the AUTOSAR CP system and registering and releasing callback functions at runtime, allowing tasks to be executed in parallel during system scheduling, and restoring the unregistered state when no longer needed. Reuse of resources.
It improves the scalability and flexibility of the system, balances the system load, reduces the amount of code modifications for multi-threaded applications, improves the porting efficiency, and realizes the dynamic execution ability of static tasks.
Smart Images

Figure CN119003119A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of automotive electronics, and more specifically, to a method and system for replacing dynamic threads with static tasks. Background Art
[0002] AUTOSAR CP (Classic Platform) is an open software architecture for automotive electronic systems. It uses the concept of static tasks to organize the system and implements concurrent processing through multi-tasking and priority scheduling mechanisms in a single-core real-time operating system. Static tasks are tasks that have been defined and configured when the system starts. They cannot be dynamically created and destroyed at runtime like dynamic threads. The number and size of static tasks are determined at compile time and cannot be adjusted according to the system load.
[0003] Therefore, a solution is needed that can use static tasks to replace dynamic threads in a single-core multi-tasking system to increase the scalability and flexibility of the system, improve performance, and facilitate the porting of multi-threaded applications. Summary of the invention
[0004] The present invention aims at the technical problem that the dynamic execution of static tasks cannot be realized in the prior art, and provides a method and system for replacing dynamic threads with static tasks.
[0005] According to a first aspect of the present invention, a method for replacing dynamic threads with static tasks based on AUTOSAR CP is provided, comprising:
[0006] Before the AUTOSAR CP system runs, multiple unregistered tasks are created;
[0007] An interface is defined in an AUTOSAR CP system, and a plurality of callback functions are defined in the interface;
[0008] When the AUTOSAR CP system is running, the interface is called to select an unregistered task, a callback function is registered to the selected unregistered task, and the state of the task is set to be registered;
[0009] When the AUTOSAR CP system schedules the registered task, it executes the callback function of the registered task;
[0010] When the callback function of the registered task is no longer needed to be executed, the interface is called to release the callback function of the registered task and restore the task to an unregistered state, wherein the task restored to an unregistered state can register and execute other callback functions.
[0011] Based on the above technical solution, the present invention can also make the following improvements.
[0012] Optionally, before the AUTOSAR CP system runs, multiple unregistered tasks are created, including:
[0013] Before the AUTOSAR CP system runs, determine the number of static tasks that need to be configured, configure the static tasks, set each static task to an unregistered state, and set the function pointer of each static task to null.
[0014] Optionally, the number of defined callback functions is greater than the number of configured static tasks.
[0015] Optionally, when the AUTOSAR CP system is running, calling the interface to select an unregistered task, registering a callback function to the selected unregistered task, and setting the state of the task to registered includes:
[0016] Initially, according to the configured number n of the static tasks, n callback functions are selected from the defined m callback functions, n<m, and n and m are both positive integers, and the n callback functions are registered to the corresponding n static tasks, wherein one callback function is registered in each static task, and the status of the n tasks is modified to the registered status;
[0017] Add mn callback functions that are not registered in the task to the queue to be allocated.
[0018] Optionally, when the callback function of the registered task is no longer required to be executed, calling the interface to release the callback function of the registered task and restore the task to an unregistered state includes:
[0019] When the callback function of the registered task is no longer needed, the interface is called to unregister the specified task. If the currently executed task is not the specified task, the memory used by the specified task is released, the function pointer is set to null, and the status of the specified task is restored to the unregistered state.
[0020] If the currently executing task is the specified task, mark the currently executing task as a pending deregistration state; when the callback function of the task in the pending deregistration state is executed, the memory used by the specified task is released, the function pointer is set to null, and the state of the specified task is restored to the unregistered state.
[0021] Optionally, when there are tasks restored to the unregistered state, a corresponding number of callback functions in the queue to be allocated are registered with the unregistered tasks according to the number of tasks restored to the unregistered state, until all callback functions in the queue to be allocated are registered with the tasks and executed.
[0022] According to a second aspect of the present invention, a system for replacing dynamic threads with static tasks based on AUTOSAR CP is provided, comprising:
[0023] Create a module to create multiple unregistered tasks before the AUTOSAR CP system runs;
[0024] A definition module, used for defining an interface in an AUTOSAR CP system, and defining a plurality of callback functions in the interface;
[0025] A registration module, used for calling the interface to select an unregistered task when the AUTOSAR CP system is running, registering a callback function to the selected unregistered task, and setting the state of the task to be registered;
[0026] The execution module is used for the AUTOSAR CP system to execute the callback function of the registered task when scheduling the registered task;
[0027] The release module is used to call the interface to release the callback function of the registered task when the callback function of the registered task is no longer needed, and restore the task to an unregistered state, wherein the task restored to the unregistered state can register and execute other callback functions.
[0028] According to a third aspect of the present invention, an electronic device is provided, comprising a memory and a processor, wherein the processor is used to implement the steps of a method for replacing dynamic threads with static tasks based on AUTOSAR CP when executing a computer management program stored in the memory.
[0029] According to a fourth aspect of the present invention, a computer-readable storage medium is provided, on which a computer management program is stored, and when the computer management program is executed by a processor, the steps of a method for replacing dynamic threads with static tasks based on AUTOSAR CP are implemented.
[0030] The present invention provides a method and system for replacing dynamic threads with static tasks based on AUTOSAR CP. Interfaces and callback functions are defined in the system. Initially, a part of the callback functions are registered with multiple unregistered tasks to realize parallel execution of multiple tasks. When the tasks are executed, the callback functions registered in the tasks are released to restore the unregistered state. The tasks restored to the unregistered state can register and execute the remaining callback functions, thereby realizing dynamic execution of static tasks. The method and system are improved and optimized for the practice that the single-core multi-task real-time system based on the existing AUTOSAR CP cannot dynamically create and destroy tasks during operation, thereby improving the scalability and flexibility of the system, rationally planning system resources, balancing system loads, and improving the transplantation efficiency of multi-threaded applications. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] Figure 1A flow chart of a method for replacing dynamic threads with static tasks based on AUTOSAR CP provided by the present invention;
[0032] Figure 2 The overall flow chart of the method of replacing dynamic threads with static tasks based on AUTOSAR CP;
[0033] Figure 3 A structural block diagram of a system for replacing dynamic threads with static tasks based on AUTOSAR CP provided by the present invention;
[0034] Figure 4 A schematic diagram of the hardware structure of a possible electronic device provided by the present invention;
[0035] Figure 5 A schematic diagram of the hardware structure of a possible computer-readable storage medium provided by the present invention. DETAILED DESCRIPTION
[0036] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention. In addition, the technical features in the various embodiments or single embodiments provided by the present invention can be arbitrarily combined with each other to form a feasible technical solution. This combination is not subject to the constraints of the sequence of steps and / or the structural composition mode, but must be based on the ability of ordinary technicians in this field to achieve. When the combination of technical solutions is contradictory or cannot be achieved, it should be considered that such a combination of technical solutions does not exist and is not within the scope of protection required by the present invention.
[0037] Figure 1 The present invention provides a method flow chart of replacing dynamic threads with static tasks based on AUTOSAR CP, such as Figure 1 and Figure 2 As shown, the method includes:
[0038] Step 1: Before the AUTOSAR CP system runs, create multiple unregistered tasks.
[0039] It is understandable that before the AUTOSAR CP system is run, the number n of static tasks to be configured is determined, and the static tasks are configured, each static task is set to an unregistered state, and the function pointer of each static task is empty.
[0040] Step 2: define an interface in the AUTOSAR CP system, and define multiple callback functions in the interface.
[0041] It is understandable that when a task needs to be executed, an interface is defined in the AUTOSAR CP system, and multiple callback functions m are defined in the interface, wherein the number m of defined callback functions is greater than the number n of configured static tasks, and both m and n are positive integers.
[0042] Step 3: When the AUTOSAR CP system is running, the interface is called to select an unregistered task, a callback function is registered to the selected unregistered task, and the state of the task is set to be registered.
[0043] It is understandable that when registering a callback function in a configured static task, initially, according to the number n of configured static tasks, n callback functions are selected from the defined m callback functions, n<m, and n and m are both positive integers, and the n callback functions are registered in the corresponding n static tasks, wherein one callback function is registered in each static task, and the status of the n tasks is modified to the registered status. The mn callback functions that are not registered in the task are added to the queue to be allocated, and after waiting for the memory of the subsequent task to be released, the callback functions in the queue to be allocated are registered with the task that releases the memory.
[0044] Step 4: When the AUTOSAR CP system schedules the registered task, it executes the callback function of the registered task.
[0045] It is understandable that, for tasks with registered callback functions, the AUTOSAR CP system performs parallel scheduling execution, that is, executes the callback functions in these tasks.
[0046] Step 5, when the callback function of the registered task is no longer needed to be executed, the interface is called to release the callback function of the registered task and restore the task to an unregistered state, wherein the task restored to an unregistered state can register and execute other callback functions.
[0047] Among them, when the callback function of the registered task is no longer needed and the interface is called to unregister the specified task, if the currently executed task is not the specified task, the memory used by the specified task is released, the function pointer is set to null, and the state of the specified task is restored to the unregistered state;
[0048] If the currently executing task is the specified task, mark the currently executing task as a pending deregistration state; when the callback function of the task in the pending deregistration state is executed, the memory used by the specified task is released, the function pointer is set to null, and the state of the specified task is restored to the unregistered state.
[0049] It is understandable that when the callback function of a task does not need to be executed, when the interface is called to unregister the task, if the specified task is not being executed, the memory used by the specified task is directly released, the function pointer is set to null, and the status of the specified task is restored to the unregistered state. If the specified task is being executed, the memory of the specified task needs to be released after the callback function of the specified task is executed, and the status of the task after the memory is released is restored to the unregistered state.
[0050] When there are tasks that are restored to the unregistered state, the corresponding number of callback functions in the queue to be allocated are registered with the unregistered tasks according to the number of tasks restored to the unregistered state, until all callback functions in the queue to be allocated are registered with the tasks and executed.
[0051] See also Figure 3 , a system for replacing dynamic threads with static tasks based on AUTOSAR CP provided by the present invention, the system comprising:
[0052] A creation module 301 is used to create a plurality of unregistered tasks before the AUTOSAR CP system runs;
[0053] A definition module 302, used to define an interface in the AUTOSAR CP system, and define a plurality of callback functions in the interface;
[0054] The registration module 303 is used to call the interface to select an unregistered task when the AUTOSAR CP system is running, register a callback function to the selected unregistered task, and set the state of the task to be registered;
[0055] An execution module 304 is used for the AUTOSAR CP system to execute the callback function of the registered task when the registered task is scheduled;
[0056] The release module 305 is used to call the interface to release the callback function of the registered task when it is no longer necessary to execute the callback function of the registered task, and restore the task to an unregistered state, wherein the task restored to the unregistered state can register and execute other callback functions.
[0057] It can be understood that the system for replacing dynamic threads with static tasks based on AUTOSAR CP provided by the present invention corresponds to the method for replacing dynamic threads with static tasks based on AUTOSAR CP provided by the aforementioned embodiments. The relevant technical features of the system for replacing dynamic threads with static tasks based on AUTOSAR CP can refer to the relevant technical features of the method for replacing dynamic threads with static tasks based on AUTOSAR CP, which will not be described in detail herein.
[0058] See also Figure 4 , Figure 4 Schematic diagram of an electronic device provided by an embodiment of the present invention. Figure 4 As shown, an embodiment of the present invention provides an electronic device 400, including a memory 410, a processor 420, and a computer program 411 stored in the memory 410 and executable on the processor 420. When the processor 420 executes the computer program 411, the following steps are implemented: before the AUTOSAR CP system runs, a plurality of unregistered tasks are created; an interface is defined in the AUTOSAR CP system, and a plurality of callback functions are defined in the interface; when the AUTOSAR CP system runs, the interface is called to select an unregistered task, a callback function is registered to the selected unregistered task, and the state of the task is set to be registered; when the AUTOSAR CP system schedules the registered task, the callback function of the registered task is executed; when the callback function of the registered task is no longer required to be executed, the interface is called to release the callback function of the registered task, and the task is restored to the unregistered state, wherein the task restored to the unregistered state can register and execute other callback functions.
[0059] See also Figure 5 , Figure 5 Schematic diagram of an embodiment of a computer-readable storage medium provided by the present invention. Figure 5 As shown, this embodiment provides a computer-readable storage medium 500, on which a computer program 511 is stored. When the computer program 511 is executed by a processor, the following steps are implemented: before the AUTOSAR CP system runs, multiple unregistered tasks are created; an interface is defined in the AUTOSAR CP system, and multiple callback functions are defined in the interface; when the AUTOSAR CP system runs, the interface is called to select an unregistered task, a callback function is registered to the selected unregistered task, and the state of the task is set to registered; when the AUTOSAR CP system schedules the registered task, the callback function of the registered task is executed; when the callback function of the registered task is no longer needed to be executed, the interface is called to release the callback function of the registered task, and the task is restored to the unregistered state, wherein the task restored to the unregistered state can register and execute other callback functions.
[0060] The present invention provides a method and system for replacing dynamic threads with static tasks. For a single-core multi-task real-time system of AUTOSAR CP, several unregistered tasks are created before running. An unregistered task is selected using an interface during running, and a callback function is registered in the task. When the system schedules the task, the callback function is executed. When the callback function is no longer needed, the interface is called to release the callback function of the task and restore the task to an unregistered state. In the process of porting an application using multi-threading characteristics to a single-core multi-task real-time system of AUTOSAR CP, the scalability and flexibility of the system are improved, the execution process of multiple tasks is dynamically controlled, and waste of system resources is avoided; at the same time, the porting efficiency is also effectively improved, and the amount of code modification when changing multi-threading to multi-task is greatly reduced.
[0061] It should be noted that in the above embodiments, the description of each embodiment has its own emphasis, and for parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0062] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0063] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded computer, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0064] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0065] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0066] Although the preferred embodiments of the present invention have been described, those skilled in the art may make other changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention.
[0067] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.
Claims
1. A method for replacing dynamic threads with static tasks based on AUTOSAR CP, characterized in that: include: Before the AUTOSAR CP system runs, multiple unregistered tasks are created; An interface is defined in an AUTOSAR CP system, and a plurality of callback functions are defined in the interface; When the AUTOSAR CP system is running, the interface is called to select an unregistered task, a callback function is registered to the selected unregistered task, and the state of the task is set to be registered; When the AUTOSAR CP system schedules the registered task, it executes the callback function of the registered task; When the callback function of the registered task is no longer needed to be executed, the interface is called to release the callback function of the registered task and restore the task to an unregistered state, wherein the task restored to an unregistered state can register and execute other callback functions.
2. The method for replacing dynamic threads with static tasks according to claim 1, characterized in that: Before the AUTOSARCP system runs, multiple unregistered tasks are created, including: Before the AUTOSAR CP system runs, determine the number of static tasks that need to be configured, configure the static tasks, set each static task to an unregistered state, and set the function pointer of each static task to null.
3. The method for replacing dynamic threads with static tasks according to claim 1, characterized in that: The number of the defined callback functions is greater than the number of the configured static tasks.
4. The method for replacing dynamic threads with static tasks according to claim 1 or 3, characterized in that: When the AUTOSAR CP system is running, calling the interface to select an unregistered task, registering a callback function to the selected unregistered task, and setting the state of the task to be registered includes: Initially, according to the configured number n of the static tasks, n callback functions are selected from the defined m callback functions, n<m, and n and m are both positive integers, and the n callback functions are registered to the corresponding n static tasks, wherein one callback function is registered in each static task, and the status of the n tasks is modified to the registered status; Add mn callback functions that are not registered in the task to the queue to be allocated.
5. The method for replacing dynamic threads with static tasks according to claim 4, characterized in that: When the callback function of the registered task is no longer needed to be executed, calling the interface to release the callback function of the registered task and restore the task to an unregistered state includes: When the callback function of the registered task is no longer needed, the interface is called to unregister the specified task. If the currently executed task is not the specified task, the memory used by the specified task is released, the function pointer is set to null, and the status of the specified task is restored to the unregistered state. If the currently executing task is the specified task, mark the currently executing task as a pending deregistration state; when the callback function of the task in the pending deregistration state is executed, the memory used by the specified task is released, the function pointer is set to null, and the state of the specified task is restored to the unregistered state.
6. The method for replacing dynamic threads with static tasks according to claim 5, characterized in that: When there are tasks that are restored to the unregistered state, the corresponding number of callback functions in the queue to be allocated are registered with the unregistered tasks according to the number of tasks restored to the unregistered state, until all callback functions in the queue to be allocated are registered with the tasks and executed.
7. A system for replacing dynamic threads with static tasks based on AUTOSAR CP, characterized in that: include: Create a module to create multiple unregistered tasks before the AUTOSAR CP system runs; A definition module, used for defining an interface in an AUTOSAR CP system, and defining a plurality of callback functions in the interface; A registration module, used for calling the interface to select an unregistered task when the AUTOSAR CP system is running, registering a callback function to the selected unregistered task, and setting the state of the task to be registered; The execution module is used for the AUTOSAR CP system to execute the callback function of the registered task when scheduling the registered task; The release module is used to call the interface to release the callback function of the registered task when the callback function of the registered task is no longer needed, and restore the task to an unregistered state, wherein the task restored to the unregistered state can register and execute other callback functions.
8. An electronic device, characterized in that: The method comprises a memory and a processor, wherein the processor is used to implement the steps of the method for replacing dynamic threads with static tasks based on AUTOSAR CP as claimed in any one of claims 1 to 6 when executing a computer management program stored in the memory.
9. A computer-readable storage medium, characterized in that: A computer management program is stored thereon, and when the computer management program is executed by the processor, the steps of the method for replacing dynamic threads with static tasks based on AUTOSAR CP as claimed in any one of claims 1 to 6 are implemented.
Citation Information
Cited By
Service callback method and device
CN119743515A
Business callback method and apparatus
CN119743515B
