Control device and processing method
The control device and method address the issue of incorrect variable initialization in coroutine functions by saving and restoring variables in global memory, ensuring accurate resumption of interrupted processes.
Patent Information
- Application Number
- JP2020194423
- Authority / Receiving Office
- JP · JP
- Patent Type
- Patents
- Current Assignee / Owner
- Filing Date
- 2020-11-24
- Publication Date
- 2024-11-19
- Estimated Expiration
- 2040-11-24
AI Technical Summary
Existing implementations of coroutine functions in C language using switch-case statements result in undefined values or incorrect initialization of automatic variables when resuming interrupted processes, leading to abnormal restarts.
A control device and method that saves automatic variables of coroutine functions to a global memory area before interruption, restoring them to the stack area before resuming processing, ensuring accurate variable values are used for normal restarts.
Enables normal resumption of interrupted coroutine functions by initializing automatic variables correctly, allowing reliable and efficient execution of multiple coroutine functions.
Smart Images

Figure 0007585738000001 
Figure 0007585738000002 
Figure 0007585738000003
Abstract
Description
[Technical field]
[0001] The present invention relates to a control device and a processing method, and more particularly to a control device and a processing method for executing a coroutine function. [Background technology]
[0002] 2. Description of the Related Art Conventionally, a processing method for executing a coroutine function is known (see, for example, Patent Document 1).
[0003] The above-mentioned Patent Document 1 discloses a process of a coroutine function. In a normal function (such as a subroutine function, assumed to be function B), when function B is called from a caller function (assumed to be function A), the process returns to the caller function A after the process of the called function B is completed. On the other hand, in a coroutine function, when a coroutine function (assumed to be coroutine function C) is called from the caller function A, coroutine function C suspends the process after executing the process. As a result, the process returns to the caller function A. Then, when the caller function A calls coroutine function C again, the called coroutine function C resumes the process from the point where it was suspended.
[0004] Here, conventionally, there are languages such as C language that do not provide coroutine functions as standard libraries. In this case, for example, in C language, a method is known to implement coroutine functions pseudo-wise using switch-case statements. In this method, multiple processes in a coroutine function are divided into case clauses. Then, the process in one case clause is executed, and the label of the case clause to be processed next is saved in memory, and then the coroutine function is terminated. This realizes pseudo-interruption of the process of the coroutine function. Note that automatic variables of the coroutine function are saved in the stack area of memory. Then, when the process is resumed, the process is executed from the beginning of the coroutine function. Then, the process of the case clause with the label saved before the process was interrupted is executed according to the condition judgment of the switch-case statement. [Prior art documents] [Patent documents]
[0005] [Patent Document 1] Patent No. 5421914 Summary of the Invention [Problem to be solved by the invention]
[0006] However, when a pseudo coroutine function is implemented using a switch-case statement in C language, when the coroutine function is called to resume an interrupted process, the stack area of memory where the automatic variables of the coroutine function are saved is initialized or an undefined value is saved in the stack area. This causes a problem that when the interrupted process is resumed, the values of the automatic variables of the coroutine function are different from the values before the process was interrupted, and the interrupted process may not be resumed normally.
[0007] The present invention has been made to solve the above-mentioned problems, and one object of the present invention is to provide a control device and a processing method that are capable of normally resuming interrupted processing when executing a coroutine function. [Means for solving the problem]
[0008] In order to achieve the above object, a control device according to a first aspect of the present invention is a control device that executes a coroutine function that suspends processing and then resumes the processing from a continuation of the suspended processing, the control device comprising: a memory that stores a program including the coroutine function; and a control unit that executes the program; The process performed by the control unit executing the program is as follows:a step of allocating a save area in a global area of memory for saving information about the coroutine function after the coroutine function is called; a step of saving automatic variables included in the information about the coroutine function stored in a stack area of memory to the save area after the coroutine function is called and before processing of the coroutine function is interrupted; and a step of restoring the automatic variables saved in the save area to the stack area of memory before resuming the interrupted processing. creating a list of addresses of automatic variables in the stack area of memory in a global area of memory after calling the coroutine function; The term "coroutine function" includes not only the coroutine functions provided as standard libraries, but also pseudo-coroutine functions implemented using switch-case statements in the C language.
[0009] In the control device according to the first aspect of the present invention, as described above, the program includes a step of saving the automatic variables included in the information of the coroutine function saved in the stack area of the memory to a save area after the coroutine function is called and before the processing of the coroutine function is suspended, and a step of restoring the automatic variables saved in the save area to the stack area of the memory before resuming the suspended processing. As a result, the automatic variables saved in the stack area of the memory are saved to the save area of the global area of the memory, so that when the coroutine function is called to resume the suspended processing, even if the stack area of the memory is initialized or an indefinite value is saved in the stack area, the automatic variables saved in the save area of the global area of the memory are restored to the stack area of the memory. As a result, when the suspended processing of the coroutine function is resumed, the processing can be resumed based on the accurate automatic variables. As a result, the suspended processing can be resumed normally when executing the coroutine function.
[0010] In the control device according to the first aspect, the information of the coroutine function preferably further includes pointer information of the coroutine function, the interruption position of the coroutine function, arguments, and a return value. With this configuration, the pointer information of the coroutine function, the interruption position of the coroutine function, automatic variables, arguments, and a return value required for resuming the interrupted process are saved in a save area in the global area of the memory, so that the interrupted process can be resumed normally and reliably.
[0011] In this case, the information on each of the automatic variables, arguments, and return values is preferably memory area information or pointer information. Here, the global memory area is freely writable, unlike the stack memory area, so the information on each of the automatic variables, arguments, and return values can be written as either memory area information or pointer information.
[0012] In the control device according to the first aspect, the step of reserving a save area preferably includes a step of reserving a save area according to the size of the information of the coroutine function. Here, when the information of the coroutine function is saved in a stack area of a caller function that calls the coroutine function, the area for the information of the coroutine function may be reserved with a fixed size. In this case, depending on the configuration of the coroutine function (the number of arguments, automatic variables, and return value), the area may not be appropriately reserved (excess or shortage) for the information of the coroutine function. Therefore, by configuring as described above, the save area can be reserved with a variable length in the global area of the memory (it is easier to specify the size of the global area compared to the stack area), so that the save area can be appropriately reserved.
[0013] In the control device according to the first aspect, preferably, Processing performed by the control unit executing a programfurther includes a step of freeing the area in which the automatic variables were stored after restoring the automatic variables to the stack area of memory. With this configuration, the area in which the automatic variables were stored is freed after the automatic variables are restored to the stack area of memory, so that when multiple coroutine functions are provided, the global area of memory after the freeing can be used as an area for saving the automatic variables of other coroutine functions. In other words, even when there are a relatively large number of coroutine functions, the coroutine functions can be executed appropriately.
[0014] In the control device according to the first aspect, preferably , own The step of saving the automatic variables to the save area includes a step of saving the automatic variables to the save area based on the created address list, and the step of restoring the automatic variables to the stack area includes a step of restoring the automatic variables to the stack area of memory based on the created address list. With this configuration, the automatic variables are saved and restored based on the address list, so that the automatic variables can be saved to the save area and restored to the stack area without any mistakes.
[0015] In this case, preferably, Processing performed by the control unit executing a program further includes a step of freeing the area in which the address list of the automatic variables has been stored after the automatic variables have been saved to the save area. With this configuration, the area in which the address list of the automatic variables has been saved is freed after the automatic variables have been saved to the save area, so that when multiple coroutine functions are provided, the global area of the memory after the freeing can be used as an area for saving address lists of other coroutine functions, etc. In other words, even when there are a relatively large number of coroutine functions, the coroutine functions can be executed appropriately.
[0016] A processing method according to a second aspect of the present invention is a processing method for executing a coroutine function that suspends processing and then resumes processing from where the suspended processing continues, the method comprising the steps of: calling the coroutine function; after the coroutine function is called, allocating a save area in a global area of memory for saving information about the coroutine function; after the coroutine function is called and before the processing of the coroutine function is suspended, saving to the save area automatic variables included in the information about the coroutine function that are saved in the stack area of memory; and before resuming the suspended processing, restoring the automatic variables saved in the save area to the stack area of memory; creating a list of addresses of automatic variables in the stack area of memory in a global area of memory after calling the coroutine function; Includes.
[0017] As described above, the processing method according to the second aspect of the present invention includes the steps of: after the coroutine function is called, before the processing of the coroutine function is suspended, saving the automatic variables included in the information of the coroutine function saved in the stack area of the memory to a save area; and before resuming the suspended processing, restoring the automatic variables saved in the save area to the stack area of the memory. As a result, the automatic variables saved in the stack area of the memory are saved to the save area of the global area of the memory, so that when the coroutine function is called to resume the suspended processing, even if the stack area of the memory is initialized or an indefinite value is saved in the stack area, the automatic variables saved in the save area of the global area of the memory are restored to the stack area of the memory. As a result, when the suspended processing of the coroutine function is resumed, the processing can be resumed based on the accurate automatic variables. This makes it possible to provide a processing method that allows the suspended processing to be resumed normally when executing a coroutine function. Effect of the Invention
[0018] According to the present invention, as described above, when executing a coroutine function, the interrupted process can be normally resumed. [Brief description of the drawings]
[0019] [Figure 1] FIG. 2 is a block diagram of a control device according to the present embodiment. [Diagram 2] FIG. 2 is a diagram showing a configuration of a memory of the control device according to the present embodiment. [Diagram 3] FIG. 11 is a sequence diagram for explaining processing of a general subroutine function. [Figure 4] FIG. 11 is a sequence diagram for explaining a process of a general coroutine function. [Diagram 5] 1 is a diagram for explaining the relationship between processing of a coroutine function and memory according to the present embodiment. FIG. [Figure 6] FIG. 2 is a diagram for explaining memory blocks of the control device according to the present embodiment. [Figure 7] FIG. 2 is a diagram illustrating processing of a coroutine function according to the present embodiment. [Figure 8] 11 is a diagram for explaining an address list (stack frame definition) of automatic variables according to the present embodiment. FIG. [Figure 9] FIG. 11 is a flow diagram for explaining processing of a coroutine function according to the present embodiment. [Figure 10] 1A and 1B are diagrams for explaining an example of a description of a coroutine function and an example of actual code according to the present embodiment. [Figure 11] FIG. 11 is a sequence diagram for explaining processing of a coroutine function according to the present embodiment. [Figure 12] FIG. 2 is a memory transition diagram for explaining processing of a coroutine function according to the present embodiment. [Figure 13] FIG. 11 is a sequence diagram for explaining the processing of two coroutine functions according to the present embodiment. [Figure 14] FIG. 2 is a memory transition diagram for explaining the processing of two coroutine functions according to the present embodiment. [Figure 15] FIG. 2 is a sequence diagram for explaining processing of a coroutine function and a sub-coroutine function according to the present embodiment. [Figure 16] FIG. 2 is a memory transition diagram for explaining processing of a coroutine function and a sub-coroutine function according to the present embodiment. DETAILED DESCRIPTION OF THE PREFERRED EMBODIMENTS
[0020] DETAILED DESCRIPTION OF THE PREFERRED EMBODIMENTS Hereinafter, preferred embodiments of the present invention will be described with reference to the accompanying drawings.
[0021] The configuration of a control device 100 according to this embodiment will be described with reference to Figures 1 to 16. The control device 100 is, for example, a control device 100 used in an embedded device.
[0022] As shown in FIG. 1, the control device 100 includes a microcomputer 2 and a memory 3 provided on a printed circuit board 1. The memory 3 is composed of a ROM (Read Only Memory) and a RAM (Random Access Memory), etc. A program 4 including a coroutine function is stored in the memory 3. The microcomputer 2 executes the program 4 stored in the memory 3. The program 4 is written in the C language. The microcomputer 2 is an example of a "control unit" in the claims.
[0023] As shown in Fig. 2, the memory 3 handled by the C language includes a text area 3a, a global area 3b (static area), a heap area 3c, and a stack area 3d. The text area 3a stores a program 4 translated into machine language. The global area 3b stores static variables such as global variables. The heap area 3c is used for managing the memory 3. The stack area 3d stores automatic variables of the C language (such as local variables). The sizes of the heap area 3c and stack area 3d change during the program 4.
[0024] In this embodiment, the control device 100 is configured to execute a pseudo-co-routine function implemented in C language that suspends processing and then resumes the processing from where the processing was suspended.
[0025] The difference between a subroutine function and a coroutine function will be described with reference to FIGS.
[0026] As shown in Figure 3, when a subroutine function func2 is called from func1 (call), func2 executes processing up to the return statement included in func2. On the other hand, as shown in Figure 4, when a coroutine function func2 is called from func1 (resume), func2 executes processing halfway through and then suspends (yield). Then, when func2 is called from func1 again (resume), processing resumes from where it was suspended in the previous processing.
[0027] In the C language, a coroutine function is not provided as a standard library. In this embodiment, a pseudo coroutine function is implemented using a Switch-case statement in the C language (see FIG. 10). Specifically, in the pseudo coroutine function implemented by the Switch-case statement, the processing of the coroutine function is divided into case clauses. Then, the processing of one case clause is executed, and the label (interruption position) of the case clause to be processed next is saved in the memory 3, and then the processing returns (leaves) from the coroutine function. In this way, a pseudo coroutine function is implemented using the Switch-case statement in the C language. When the processing of the coroutine function is resumed, the coroutine function is called, and processing is started from the beginning of the coroutine function. Then, based on the condition judgment of the Switch statement, processing is executed from the case clause corresponding to the label (interruption position) of the case clause saved when the previous processing was interrupted.
[0028] (Program Structure) Next, the configuration of a coroutine function pseudo-implemented in C language according to this embodiment (the configuration of program 4) will be described.
[0029] In this embodiment, as shown in FIG. 5, the processing of a coroutine function pseudo-implemented in C language (program 4) includes a step of allocating a save area 3e in the global area 3b of memory 3 to save information about the coroutine function after the coroutine function is called. The save area 3e is managed in units of variable-length blocks. As shown in FIG. 6, block information including a usage information flag and a block size is stored at the beginning of the block. The usage information flag indicates whether the block is unused or in use. The block size includes the block information and information about the size of the allocated memory area.
[0030] In the process of reserving the save area 3e, the block information is referenced from the top of the save area 3e, and a block whose usage status flag is unused is searched for. If a block whose usage status flag is unused is found, the usage status flag of this block is set to "in use" and the top address of the area (block) of the allocated memory 3 is returned. If a block whose usage status flag is unused is not found, the block information is searched again (sequentially searched) for a block whose usage status flag is unused.
[0031] In this embodiment, the information on the coroutine function further includes, in addition to the automatic variables, pointer information on the coroutine function, the interruption position of the coroutine function, arguments, and a return value. Each piece of information on the automatic variables, arguments, and return value is area information or pointer information on the memory 3. The size of the area information on the automatic variables, area information on arguments, and area information on the return value can be changed within the range of the save area 3e. The reference destinations of the pointer information on the automatic variables, pointer information on arguments, and pointer information on the return value are areas secured in the save area 3e and have the required size for each.
[0032] In addition, in this embodiment, as shown in FIG. 5, the step of reserving the save area 3e includes a step of reserving the save area 3e in the global area 3b of the memory 3 according to the size of the information of the coroutine function. Here, when the information of the coroutine function is reserved in the stack area 3d of the calling function that calls the coroutine function, the size (maximum value) of the stack area 3d is determined when the program 4 is compiled. Therefore, the size of the stack area 3d for saving the information of the coroutine function is less than the maximum value of the stack area 3d determined at the time of compilation, so that the number of coroutine functions that can be executed simultaneously is limited. On the other hand, in this embodiment, the information of the coroutine function is saved in the save area 3e of the global area 3b. Since it is easier to specify the size of the global area 3b than the stack area 3d, in this embodiment, it is possible to suppress the limit on the number of coroutine functions that can be executed simultaneously.
[0033] Furthermore, in this embodiment, the program 4 includes a step of saving, after the coroutine function is called and before the processing of the coroutine function is interrupted, automatic variables included in the information of the coroutine function stored in the stack area 3d of the memory 3 to the save area 3e. Note that the automatic variables may be saved to an automatic variable area inside the information of the coroutine function (hereinafter sometimes referred to as Co information), or may be saved to an area secured separately from the Co information.
[0034] Furthermore, in this embodiment, the program 4 includes a step of restoring the automatic variables saved in the save area 3e to the stack area 3d of the memory 3 before resuming the interrupted process.
[0035] In the example shown in FIG. 7, coroutine function 1 and coroutine function 2 are executed. When coroutine function 1 is executed, automatic variables 1-1 and 1-2 of coroutine function 1 are stored in stack area 3d of memory 3. Before the processing of coroutine function 1 is interrupted, automatic variables 1-1 and 1-2 stored in stack area 3d are saved to save area 3e. Note that in the example of FIG. 7, automatic variables 1-1 and 1-2 are saved in an area different from an area in which Co information 1 (pointer information, interruption position of coroutine function, arguments, return value, etc.), which is information of coroutine function 1, is saved.
[0036] Next, when the coroutine function 2 is executed, the automatic variables 2-1 and 2-2 of the coroutine function 2 are stored in the stack area 3d of the memory 3. Before the processing of the coroutine function 2 is interrupted, the automatic variables 2-1 and 2-2 stored in the stack area 3d are saved to the save area 3e.
[0037] When the coroutine function 1 is resumed, the automatic variables 1-1 and 1-2 stored in the stack area 3d are restored to the stack area 3d in the memory 3. The same is true when the coroutine function 2 is resumed.
[0038] Moreover, in this embodiment, the program 4 further includes a step of freeing the area (save area 3e) in which the automatic variables have been saved, after restoring the automatic variables to the stack area 3d of the memory 3. Specifically, as shown in Fig. 6, the program 4 refers to the block information of the block including the area to be freed from the top address of the save area 3e, and sets the usage status flag of this block to unused.
[0039] 8, in this embodiment, the program 4 further includes a step of creating an address list AL (stack frame definition) of automatic variables in the stack area 3d of the memory 3 after calling the coroutine function. Specifically, a definition area 3f required for saving information of the address list AL is secured in the global area 3b of the memory 3. Then, the address value (pointer) on the stack area 3d of the automatic variable to be saved is saved in the global area 3b as the address list AL (stack frame definition).
[0040] For example, in the example shown in Figure 8(a), automatic variables A, B, and C (denoted as A, B, and C, respectively, in Figure 8) are stored in stack area 3d. Then, a pointer to automatic variable A, a pointer to automatic variable B, and a pointer to automatic variable C are stored as address list AL in global area 3b. Note that the pointers to automatic variables A, B, and C are stored in any order, not limited to the order shown in Figure 8.
[0041] In this embodiment, as shown in Fig. 8(c), the step of saving the automatic variables in the global area 3b includes a step of saving the automatic variables in the save area 3e based on the created address list AL. Specifically, the save area 3e for saving the information (automatic variables) of the coroutine function is secured in the global area 3b of the memory 3. Then, the values of the automatic variables referenced by the pointer are saved in the save area 3e in the order defined in the address list AL.
[0042] For example, in the example shown in Figure 8(c), automatic variables A, B, and C are stored in stack area 3d. Then, the value of automatic variable A referenced by the pointer is saved in save area 3e. As a result, the value of automatic variable A is saved in global area 3b. The same is true for automatic variables B and C.
[0043] In this embodiment, the program 4 further includes, after the step of saving (the values of) the automatic variables in the save area 3e, a step of releasing the area in which the address list AL has been saved.
[0044] In this embodiment, as shown in Fig. 8(b), the step of restoring the automatic variables to the stack area 3d includes a step of restoring the automatic variables to the stack area 3d of the memory 3 based on the created address list AL. Specifically, the values of the automatic variables saved before the interruption of the process are obtained from the save area 3e. Then, the automatic variables are saved in the part of the stack area 3d pointed to by the pointer in the order defined in the address list AL.
[0045] For instance, in the example shown in FIG. 8(b), the values of automatic variables A, B, and C before the interruption are saved in save area 3e. Then, the value of automatic variable A is saved in the portion of stack area 3d pointed to by the pointer to automatic variable A in address list AL. This causes the value of automatic variable A to be restored to stack area 3d. The same is true for automatic variables B and C. After automatic variables A to C are restored to stack area 3d in memory 3, the area where automatic variables A to C were saved (part of global area 3b) is released.
[0046] Next, a method for processing a coroutine function according to this embodiment will be described. Note that the following process is performed by the microcomputer 2.
[0047] First, as shown in FIGS. 9, 11 and 12, in step S1 (co_begin in FIG. 10), a coroutine function is called.
[0048] Next, in step S2 (define_frame in FIG. 10), after the coroutine function is called, an area for saving an address list AL of automatic variables in the stack area 3d of the memory 3 is secured in the global area 3b of the memory 3. Then, after the coroutine function is called, the address list AL is created in this area.
[0049] Next, in step S3 (Switch in FIG. 10), a Switch statement determines the interruption position of the processing of the coroutine function.
[0050] If it is determined in step S3 that the coroutine function is executed for the first time (case 0 in FIG. 10), the process of the case() clause in FIG. 10 is executed in step S4. That is, "hello world!!" is displayed on a display (not shown).
[0051] Next, as shown in Fig. 10 (an example of a description of a coroutine function), a command to interrupt the processing of the coroutine function is issued by "co_yield()". Specifically, in step S5 (save_value in Fig. 10), the automatic variables stored in the stack area 3d for the coroutine function are saved to the save area 3e of the global area 3b. In detail, after the save area 3e for saving the information of the coroutine function (automatic variables, pointer information of the coroutine function, interruption position of the coroutine function, arguments, and return value) is secured in the global area 3b of the memory 3, the automatic variables in the stack area 3d are saved to the save area 3e of the global area 3b.
[0052] Next, in step S6 (_state=_LINE_ in FIG. 10), the interruption position (the position where the coroutine function is interrupted) is saved (stored) in the save area 3e of the memory 3.
[0053] Next, in step S7 (return in FIG. 10), the coroutine function is interrupted (ended).
[0054] If it is determined in step S3 that the coroutine function has been resumed (first resume) (case_LINE_ in FIG. 10), the automatic variables saved in the global area 3b of the memory 3 are restored to the stack area 3d in step S8 (load_value() in FIG. 10). After that, the area in the global area 3b of the memory 3 where the automatic variables were saved is released.
[0055] Next, in step S9, the process of the case_LINE_ clause in Fig. 10 is executed. That is, "hi world!" is displayed on a display (not shown).
[0056] Next, as shown in Fig. 10, a command to interrupt the processing of the coroutine function is issued by "co_yield()". Specifically, in step S10 (save_value in Fig. 10), the automatic variables stored in the stack area 3d for the coroutine function are saved to the save area 3e of the global area 3b. In detail, after the save area 3e for saving the information of the coroutine function (automatic variables, pointer information of the coroutine function, interruption position of the coroutine function, arguments, and return value) is secured in the global area 3b of the memory 3, the automatic variables in the stack area 3d are saved to the save area 3e of the global area 3b.
[0057] Next, in step S11 (_state=_LINE_ in FIG. 10), the interruption position (the position where the coroutine function is interrupted) is saved in the save area 3e of the memory 3.
[0058] Next, in step S12 (return in FIG. 10), the coroutine function is interrupted (ended).
[0059] If it is determined in step S3 that the coroutine function has been resumed (second resumed) (case_LINE_ in FIG. 10), the automatic variables saved in the global area 3b of the memory 3 are restored to the stack area 3d in step S13 (load_value() in FIG. 10). After that, the area in the global area 3b of the memory 3 where the automatic variables were saved is released.
[0060] Next, in step S14, the process of the case_LINE_ clause in Fig. 10 is executed. That is, ":)" is displayed on a display (not shown).
[0061] Next, as shown in Fig. 10, the end of the processing of the coroutine function is instructed by "co_end". Specifically, in step S15 (_state=_LINE_ in Fig. 10), the interruption position (the position where the coroutine function ends) is saved to the save area 3e of the memory 3.
[0062] Next, in step S16 (return in FIG. 10), the coroutine function is terminated, and the area in which the automatic variables were saved (part of the global area 3b) is released.
[0063] In addition, after the coroutine function is terminated in step S16, if it is determined in step S3 that the coroutine function has been resumed, the position where the coroutine function ended in step S16 has been saved in the save area 3e of the memory 3, so the coroutine function is terminated without being processed.
[0064] (Multiple coroutine functions) Next, an operation when a plurality of coroutine functions are executed will be described with reference to Fig. 13 and Fig. 14. Below, a case where coroutine function 1 and coroutine function 2 are executed will be described.
[0065] First, the coroutine function 1 is called (step S21). Then, after the coroutine function 1 is called, an area for storing the address list AL is secured in the global area 3b of the memory 3, and the address list AL (Co information 1) is created in this area.
[0066] Next, the processing of the coroutine function 1 is executed (step S22). During the processing of the coroutine function 1, the automatic variable 1-1 is stored in the stack area 3d. Note that, although FIG. 14 shows a diagram in which the automatic variable 1-1 is stored in the global area 3b (save area 3e) and restored to the stack area 3d, when the processing of the coroutine function 1 is executed for the first time, it is not necessary to restore the automatic variable 1-1 or to release the area in which the automatic variable 1-1 was stored.
[0067] Next, the processing of the coroutine function 1 is interrupted (step S23). As a result, the automatic variable 1-1 stored in the stack area 3d is saved to the save area 3e of the global area 3b. In addition, the interruption position (the position where the coroutine function is interrupted) is saved to the save area 3e of the memory 3 (Co information 1).
[0068] Next, the coroutine function 2 is called (step S24). After the coroutine function 2 is called, an area for saving the address list AL is secured in the global area 3b of the memory 3, and the address list AL (Co information 2) is created in this area.
[0069] Next, the processing of the coroutine function 2 is executed (step S25). During the processing of the coroutine function 2, the automatic variable 2-1 is stored in the stack area 3d. Note that, although FIG. 14 shows a diagram in which the automatic variable 2-1 is stored in the global area 3b (save area 3e) and restored to the stack area 3d, when the processing of the coroutine function 2 is executed for the first time, it is not necessary to restore the automatic variable 2-1 or to release the area in which the automatic variable 2-1 was stored.
[0070] Next, the processing of the coroutine function 2 is interrupted (step S26). As a result, the automatic variable 2-1 stored in the stack area 3d is saved to the save area 3e of the global area 3b. In addition, the interruption position (the position where the coroutine function is interrupted) is saved to the save area 3e of the memory 3 (Co information 2).
[0071] Next, the coroutine function 1 is called again, and the processing of the coroutine function 1 is resumed (step S27). As a result, the automatic variable 1-1 saved in the global area 3b of the memory 3 is restored to the stack area 3d. After that, the area in the global area 3b of the memory 3 where the automatic variable 1-1 was saved is released. Note that the processing of interrupting the coroutine function 1 thereafter is the same as that described above.
[0072] Next, the coroutine function 2 is called again, and the processing of the coroutine function 2 is resumed (step S28). As a result, the automatic variable 2-1 saved in the global area 3b of the memory 3 is restored to the stack area 3d. After that, the area in the global area 3b of the memory 3 where the automatic variable 2-1 was saved is released. Note that the processing of interrupting the coroutine function 2 thereafter is the same as that described above.
[0073] (Subcoroutine functions) Next, an operation when a sub-coroutine function is called from a coroutine function will be described with reference to Figures 15 and 16. Below, a case where a coroutine function 1 and a coroutine function 1-1 which is a sub-coroutine function will be executed will be described.
[0074] First, the coroutine function 1 is called (step S31). Then, after the coroutine function 1 is called, an area for storing the address list AL is secured in the global area 3b of the memory 3, and the address list AL (Co information 1) is created in this area.
[0075] Next, the coroutine function 1-1 is called (step S32).
[0076] Next, the co-routine function 1-1 is started (step S33), and an area for storing the address list AL is secured in the global area 3b of the memory 3, and the address list AL (Co information 1-1) is created in this area.
[0077] Next, the processing of the coroutine function 1-1 is executed (step S34). During the processing of the coroutine function 1-1, the automatic variable 1-1-1 is stored in the stack area 3d. Note that, although FIG. 16 shows a diagram in which the automatic variable 1-1-1 is stored in the save area 3e and restored to the stack area 3d, when the processing of the coroutine function 1-1-1 is executed for the first time, it is not necessary to restore the automatic variable 1-1-1 or to release the area in which the automatic variable 1-1-1 was stored.
[0078] Next, the processing of the coroutine function 1-1 is interrupted (step S35). As a result, the automatic variable 1-1 stored in the stack area 3d is saved to the save area 3e of the global area 3b. In addition, the interruption position (the position where the coroutine function is interrupted) is saved to the save area 3e of the memory 3 (Co information 1-1).
[0079] Next, the processing of the coroutine function 1 is resumed (step S36). Specifically, the automatic variable 1-1 saved in the save area 3e of the global area 3b is restored (saved) in the stack area 3d, and the processing is executed using the automatic variable 1-1 saved in the stack area 3d. Also, the save area 3e in which the automatic variable 1-1 was saved is released. Then, when the processing of the coroutine function 1 is interrupted, the automatic variable 1-1 saved in the save area 3e of the global area 3b is saved. Also, the interruption position (the position where the coroutine function was interrupted) is saved in the save area 3e of the memory 3 (Co information 1).
[0080] [Effects of this embodiment] In this embodiment, the following effects can be obtained.
[0081] In this embodiment, as described above, the program 4 includes a step of saving the automatic variables included in the information of the coroutine function saved in the stack area 3d of the memory 3 to the save area 3e after the coroutine function is called and before the processing of the coroutine function is suspended, and a step of restoring the automatic variables saved in the save area 3e to the stack area 3d of the memory 3 before resuming the suspended processing. As a result, the automatic variables saved in the stack area 3d of the memory 3 are saved to the save area 3e of the global area 3b of the memory 3, so that when the coroutine function is called to resume the suspended processing, even if the stack area 3d of the memory 3 is initialized or an indefinite value is saved in the stack area 3d, the automatic variables saved in the save area 3e of the global area 3b of the memory 3 are restored to the stack area 3d of the memory 3. As a result, when the suspended processing of the coroutine function is resumed, the processing can be resumed based on the accurate automatic variables. As a result, the suspended processing can be resumed normally when executing the coroutine function.
[0082] In this embodiment, as described above, the information of the coroutine function further includes the pointer information of the coroutine function, the interruption position of the coroutine function, the arguments, and the return value. As a result, the pointer information of the coroutine function, the interruption position of the coroutine function, the automatic variables, the arguments, and the return value, which are necessary for resuming the interrupted process, are saved in the save area 3e of the global area 3b of the memory 3, so that the interrupted process can be resumed normally and reliably.
[0083] In this embodiment, as described above, the information on each of the automatic variables, arguments, and return values is area information or pointer information on the memory 3. Here, the global area 3b of the memory 3 is freely writable, unlike the stack area 3d of the memory 3, so that the information on each of the automatic variables, arguments, and return values can be written as either area information or pointer information on the memory 3.
[0084] In addition, in this embodiment, as described above, the step of reserving the save area 3e includes a step of reserving the save area 3e according to the size of the information of the coroutine function. Here, when the information of the coroutine function is saved in the stack area 3d of the calling function that calls the coroutine function, the area for the information of the coroutine function may be reserved with a fixed size. In this case, depending on the configuration of the coroutine function (the number of arguments, automatic variables, and return values), the area may not be appropriately reserved (excess or shortage) for the information of the coroutine function. Therefore, by configuring as described above, the save area 3e can be reserved with a variable length in the global area 3b of the memory 3 (the size of the global area 3b is easier to specify than the stack area 3d), so the save area 3e can be appropriately reserved.
[0085] Also, in this embodiment, as described above, the program 4 further includes a step of freeing the area in which the automatic variables were saved after restoring the automatic variables to the stack area 3d of the memory 3. As a result, the area in the memory 3 in which the automatic variables were saved is freed after the automatic variables are restored to the stack area 3d of the memory 3, so that when multiple coroutine functions are provided, the global area 3b of the memory 3 after the freeing can be used as an area for saving the automatic variables of other coroutine functions. In other words, even when the number of coroutine functions is relatively large, the coroutine functions can be executed appropriately.
[0086] Also, in this embodiment, as described above, the program 4 further includes a step of creating an address list AL of the automatic variables in the stack area 3d of the memory 3 in the global area 3b of the memory 3 after calling the coroutine function. The step of saving the automatic variables in the save area 3e includes a step of saving the automatic variables in the save area 3e based on the created address list AL. The step of restoring the automatic variables to the stack area 3d includes a step of restoring the automatic variables to the stack area 3d of the memory 3 based on the created address list AL. As a result, the automatic variables are saved and restored based on the address list AL, so that the automatic variables can be saved to the save area 3e and restored to the stack area 3d without any mistakes.
[0087] In this embodiment, as described above, the program 4 further includes a step of freeing the area in which the address list AL of the automatic variables is stored after the automatic variables are saved in the save area 3e. As a result, the area in which the address list AL of the automatic variables is saved is freed after the automatic variables are saved in the save area 3e, so that when multiple coroutine functions are provided, the global area 3b of the memory 3 after the freeing can be used as an area for saving the address lists AL of other coroutine functions. In other words, even when the number of coroutine functions is relatively large, the coroutine functions can be executed appropriately.
[0088] [Variations] The embodiments disclosed herein should be considered to be illustrative and not restrictive in all respects. The scope of the present invention is indicated by the claims, not by the description of the embodiments above, and further includes all modifications (variations) within the meaning and scope of the claims.
[0089] For example, in the above embodiment, an example was shown in which the coroutine function was pseudo-implemented in the C language, but the present invention is not limited to this. For example, the coroutine function may be implemented in a language other than the C language.
[0090] In the above embodiment, the coroutine function is implemented in a pseudo manner using a Switch-case statement in the C language, but the present invention is not limited to this. For example, the coroutine function may be implemented using a statement (command statement) other than the Switch-case statement in the C language.
[0091] In the above embodiment, the information of the coroutine function includes the automatic variables, the pointer information of the coroutine function, the interruption position of the coroutine function, the arguments, and the return value, but the present invention is not limited to this. In the present invention, the information of the coroutine function may include information other than the above information.
[0092] 12 and the like in the above embodiment, an example is shown in which the automatic variables are stored in an area other than the area in which the Co information is stored in the global area 3b of the memory 3, but the present invention is not limited to this. For example, the automatic variables may be stored in an area for automatic variables in the Co information. [Explanation of symbols]
[0093] 2 Microcomputer (control unit) 3. Memory 3b Global Area 3d stack area 3e Evacuation area 4. Program 100 Control device AL Address List
Claims
1. A control device that executes a coroutine function that resumes a process from a continuation of the interrupted process after the process is interrupted, A memory in which a program including the coroutine function is stored; A control unit that executes the program, The process performed by the control unit executing the program is as follows: After the coroutine function is called, allocating a save area in a global area of the memory for saving information of the coroutine function; After the coroutine function is called, before processing of the coroutine function is suspended, automatic variables included in information of the coroutine function stored in a stack area of the memory are saved to the save area; before resuming the interrupted process, restoring the automatic variables saved in the save area to the stack area of the memory; creating, in the global area of the memory, a list of addresses of the automatic variables in the stack area of the memory after calling the coroutine function.
2. The control device according to claim 1 , wherein the information of the coroutine function further includes pointer information of the coroutine function, a suspension position of the coroutine function, arguments, and a return value.
3. 3. The control device according to claim 2, wherein the information of each of the automatic variables, the arguments, and the return values is area information or pointer information in the memory.
4. 4. The control device according to claim 1, wherein the step of reserving the save area includes a step of reserving the save area depending on a size of the information of the coroutine function.
5. A control device as described in any one of claims 1 to 4, wherein the processing performed by the control unit executing the program further includes a step of freeing the area in which the automatic variables were stored after restoring the automatic variables to the stack area of the memory.
6. the step of saving the automatic variables to the save area includes a step of saving the automatic variables to the save area based on the created address list, A control device as described in any one of claims 1 to 5, wherein the step of restoring the automatic variable to the stack area includes a step of restoring the automatic variable to the stack area of the memory based on the created address list.
7. The control device of claim 6, wherein the processing performed by the control unit executing the program further includes a step of freeing the area in which the address list of the automatic variables is stored after the automatic variables are saved in the save area.
8. A processing method for executing a coroutine function that resumes a process from a point after the process is suspended, comprising the steps of: calling the coroutine function; After the coroutine function is called, allocating a save area in a global area of memory for saving information of the coroutine function; After the coroutine function is called, before processing of the coroutine function is suspended, automatic variables included in information of the coroutine function stored in a stack area of the memory are saved to the save area; before resuming the interrupted process, restoring the automatic variables saved in the save area to the stack area of the memory; creating, in the global area of the memory, a list of addresses of the automatic variables in the stack area of the memory after calling the coroutine function.
Citation Information
Patent Citations
Desulfurizing agnet for molten pig iron
JP1979021914A
Efficient resumption of co-routines on a linear stack
US20110167248A1
Differential stack-based symmetric co-routines
US20130227254A1
Compiler optimization of coroutines
US20160321045A1
Method and coroutine framework for processing parallel tasks, device, medium, and unmanned vehicle
US20200073711A1
