Method and device for detecting and processing overtime of thread, equipment and storage medium

By monitoring the remaining time of the thread in the PLC device and suspending the timeout thread, the problem of high-priority thread blockage or timeout caused low-priority threads to be unable to execute, and the continuous operation of the device and abnormal troubleshooting are achieved.

CN119917366AActive Publication Date: 2025-05-02GUANGZHOU ZHIYUAN ELECTRONICS CO LTD
View PDF 8 Cites 0 Cited by

Patent Information

Application Number
CN202411867832.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-18
Publication Date
2025-05-02
Estimated Expiration
2044-12-18

AI Technical Summary

Technical Problem

In PLC devices, when high-priority threads are blocked or run timeout, low-priority threads cannot execute, resulting in frequent restarts of the system or inability to connect to the host computer.

Method used

By setting a list object in the PLC device to record the thread information of the application thread, monitor the remaining time of the thread in real time, and suspend the target thread when the timeout, free up CPU resources, and avoid system restart.

Benefits of technology

It realizes that without restarting or resetting the system, timely detecting and handling thread timeouts, ensuring that the device continues to work, and completing communication with the host computer by releasing CPU resources, assisting users in troubleshooting abnormalities.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119917366A_ABST
    Figure CN119917366A_ABST
Patent Text Reader

Abstract

The invention provides a thread timeout detection and processing method and device, equipment and a storage medium, relates to the technical field of computers, and solves the problem that other threads with low priorities cannot be executed easily when a PLC (Programmable Logic Controller) device overtimes a thread in the related technology. According to the method and the device, whether the application thread is blocked or overtime in operation or not is determined, so that the application thread is triggered to be suspended when the application thread is blocked or overtime in operation, the thread overtime condition is detected in time, and the thread is suspended to release resources, so that the resource release efficiency is improved under the condition that equipment does not need to be restarted or reset. And the system can be kept to continuously run under the condition that the system is not crashed, meanwhile, the released CPU resources are utilized to complete communication with the upper computer, the abnormal condition of the current system is fed back, and overtime information of the suspended thread is uploaded to the upper computer to assist a user in troubleshooting the abnormality.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a thread timeout detection and processing method, device, equipment and storage medium. Background Art

[0002] In PLC (Programmable Automation Controller) devices, users can define the priority of application threads at will. In real-time system devices, threads with high priority are executed first, and threads with low priority are executed later. If a user sets a high-priority thread and a low-priority thread respectively, when the high-priority thread is blocked (or runs out of time), the low-priority thread cannot be executed.

[0003] In the related technology, watchdog technology is usually used to solve the problem of thread blockage or thread timeout. After the above-mentioned thread blockage or thread timeout occurs, the watchdog technology can only reset the system or restart the system. However, in PLC equipment, directly restarting the system is likely to cause problems such as frequent system restarts and failure to connect to the host computer. Summary of the invention

[0004] The present application provides a thread timeout detection and processing method, apparatus, device and storage medium, which solves the problem in the related art that other low-priority threads cannot be executed when a thread of a PLC device times out. The present solution can detect thread timeouts in a timely manner and suspend threads to release resources, so that the system can continue to run without restarting or resetting the device, which is beneficial to the continuous operation of the device.

[0005] In a first aspect, the present application provides a thread timeout detection and processing method, which is applied to a PLC device, wherein the PLC device is provided with a list object, and the list object is used to record thread information of application threads burned into the PLC device. The thread timeout detection and processing method includes:

[0006] When there is at least one application thread in a running state, updating the thread information recorded in the list object;

[0007] Based on the updated thread information in the list object, find the thread with the shortest remaining time in the application threads as the target thread, and set the monitoring semaphore associated with the target thread. The waiting time of the monitoring semaphore is the remaining time of the target thread, so as to wait for the target thread to trigger the monitoring semaphore after finishing running.

[0008] When the waiting time for the monitored semaphore expires, trigger and wait for the target thread to perform a suspend operation at the end of the current instruction;

[0009] In response to the target thread executing the suspend operation, based on the released CPU resources occupied by the target thread, thread timeout information is output to the host computer through the communication thread.

[0010] In a second aspect, the present application further provides a thread timeout detection and processing device, which is applied to a PLC device, wherein the PLC device is provided with a list object, and the list object is used to record thread information of application threads burned into the PLC device, and the thread timeout processing device comprises:

[0011] An information updating module, configured to update the thread information recorded in the list object when at least one application thread is in a running state;

[0012] The thread search module is configured to search for a thread with the shortest remaining time in the application threads based on the updated thread information in the list object as a target thread, and set a monitoring semaphore associated with the target thread, the waiting time of the monitoring semaphore is the remaining time of the target thread, so as to trigger an operation on the monitoring semaphore after waiting for the target thread to finish running;

[0013] A timeout detection module is configured to trigger and wait for the target thread to perform a suspend operation at the end stage of the current instruction when the waiting time of the monitoring semaphore is exceeded;

[0014] The timeout alarm module is configured to respond to the target thread executing the suspension operation and output the thread timeout information to the upper computer through the communication thread based on the CPU resources occupied by the released target thread.

[0015] In a third aspect, the present application further provides a PLC device, comprising:

[0016] one or more processors;

[0017] a storage device for storing one or more programs,

[0018] When one or more programs are executed by one or more processors, the one or more processors implement the thread timeout detection and processing method of the present application.

[0019] In a fourth aspect, the present application also provides a storage medium storing computer executable instructions, which, when executed by a processor, are used to execute the thread timeout detection and processing method of the present application.

[0020] The present application solution monitors the application thread to determine whether the application thread is blocked or has timed out, and triggers the application thread to suspend when there is a blockage or timeout, so that the system can continue to run without crashing without restarting or resetting the device. At the same time, the released CPU resources are used to complete communication with the host computer, feedback the abnormal situation of the current system and upload the suspended thread timeout information to the host computer to assist users in troubleshooting abnormalities. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] Figure 1 A schematic diagram of the steps of a thread timeout detection and processing method provided in an embodiment of the present application;

[0022] Figure 2 A schematic diagram of the steps of selecting a target thread and configuring a monitoring semaphore provided in an embodiment of the present application;

[0023] Figure 3 A schematic diagram of steps for triggering the target thread to suspend provided in an embodiment of the present application;

[0024] Figure 4 A schematic diagram of thread interaction in the case of thread timeout provided by an embodiment of the present application;

[0025] Figure 5 A schematic diagram of thread interaction provided in an embodiment of the present application when the thread has not timed out;

[0026] Figure 6 A schematic diagram of the structure of a thread timeout detection and processing device provided in an embodiment of the present application;

[0027] Figure 7 A schematic diagram of the structure of a PLC device provided in one embodiment of the present application. DETAILED DESCRIPTION

[0028] The embodiments of the present application are further described in detail below in conjunction with the accompanying drawings and examples. It is to be understood that the specific embodiments described herein are only used to explain the embodiments of the present application, rather than to limit the embodiments of the present application. It should also be noted that, for ease of description, only the parts related to the embodiments of the present application rather than all structures are shown in the accompanying drawings, and those skilled in the art should be able to think of it after reading the specification of this application that as long as the technical features do not contradict each other, any combination of the technical features can constitute an optional implementation method.

[0029] The terms "first", "second", etc. in the specification and claims of the present application are used to distinguish similar objects, and are not used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable when appropriate, so that the embodiments of the present application can be implemented in an order other than those illustrated or described here, and the objects distinguished by "first", "second", etc. are generally a class, and the number of objects is not limited. For example, the first object can be one or more. In addition, "and / or" in the specification and claims represents at least one of the connected objects, and the character " / " generally represents that the objects associated before and after are in an "or" relationship. In the description of the present application, "multiple" means two or more, and "several" means one or more.

[0030] In embedded real-time systems, PLC devices can be customized by users to set the priority of application threads. Threads with high priority are executed first, and threads with low priority are executed later. The device can also perform multi-threaded operations. If a user sets a high-priority thread and a low-priority thread respectively, when the high-priority thread is blocked (or runs out of time), the low-priority thread cannot be executed.

[0031] In this regard, the solution provided in the related art is to add a watchdog to each thread, and then through the monitoring of the watchdog, automatically restart or reset the system when a blockage or operation timeout is found. However, if there is a problem with the high-priority thread code itself, blockage or operation timeout will continue to occur after restarting, and the system will restart continuously, causing system abnormalities. Moreover, after the system is restarted or reset, the device and the host computer will be disconnected, making it impossible for the host computer to control the device or know the status of the PLC device. In addition, after the system is restarted or reset, the previous useful abnormal data will be cleared, and it is difficult for users to troubleshoot the problem.

[0032] In this regard, an embodiment of the present application provides a thread timeout detection and processing method, which can be applied to a PLC device. The PLC device is implemented based on WASM (WebAssembly) technology, so that the PLC device can allow the user to customize the priority of the application thread, and the thread with a high priority is executed first, and the thread with a low priority is executed later. Figure 1 This is a schematic diagram of the steps of the thread timeout detection and processing method provided in an embodiment of the present application. The PLC device can detect and process the threads that have timed out or are blocked by executing this method. The PLC device is also provided with a list object as a global object, which is used to record the thread information of all application threads burned into the PLC device. Figure 1As shown, the specific steps of the method include steps S110-S140. It can be imagined that in practical applications, a monitoring thread with the highest priority can be set in the PLC device to implement the thread timeout detection and processing method. The specific steps are as follows:

[0033] Step S110: When there is at least one application thread in a running state, update the thread information recorded in the list object.

[0034] It is understandable that for each application thread burned into the PLC device, the list object records the thread information corresponding to each application thread, so that the device can determine whether the corresponding application thread is running according to the thread information. It is conceivable that every time a user creates an application thread in the PLC device, the list object can receive the thread information of the application thread and record it.

[0035] PLC devices support multi-threaded operation, and each application thread in the PLC device runs according to a cycle. It is conceivable that the cycle corresponding to each application thread can also be different. In each cycle, the application thread runs in the corresponding time, and the application thread is in a dormant state during the rest of the time. For example, if the cycle of a thread is 50ms, the running time of the thread is 2ms, and the remaining 48ms is the dormant time.

[0036] In this regard, when there is at least one application thread in a running state, such as when any application thread starts working, the thread information recorded in the list object will be updated, so that the application thread can be determined to be in a running state by querying the list object, which helps to determine whether the application thread has timed out. Optionally, in one embodiment, the thread information includes the start-up running time. It is conceivable that although each application thread in the PLC device runs according to a cycle, due to the complexity of the running conditions in the PLC device, the application thread may change the time when it starts running within the cycle. Furthermore, when any application thread starts running, the device responds to the running of the application thread and records the time when the application thread starts running as the start-up running time, and further updates the start-up running time corresponding to the application thread in the list object.

[0037] Step S120: Based on the updated thread information in the list object, search the application thread for the thread with the shortest remaining time as the target thread, and set the monitoring semaphore associated with the target thread. The waiting time of the monitoring semaphore is the remaining time of the target thread, so as to wait for the target thread to trigger the monitoring semaphore after the end of the operation.

[0038] It is conceivable that the application thread being searched is the running thread, and then the target thread is determined therefrom, and the target thread is the thread with the shortest remaining time. That is, it is understandable that the corresponding time information is recorded in the thread information to facilitate the determination of the remaining time of each application thread, and the remaining time is the time from the current running time of the application thread in the current cycle to the start time of the next cycle. For example, in the case of setting the highest priority monitoring thread in the PLC device to implement the thread timeout detection and processing method, the monitoring thread can find the thread with the shortest remaining time from the currently running application threads through the list object, and use it as the target thread. Of course, it is also conceivable that in the case of no running application thread, it can be checked once after each preset time interval to wait for the application thread that can be monitored.

[0039] In addition, a monitoring semaphore is also set. It can be understood that the monitoring semaphore is used as a flag bit, which is associated with the target thread. The waiting time of the monitoring semaphore is the remaining time of the target thread, and then the triggering operation of the monitoring semaphore is performed after the target thread finishes running.

[0040] The remaining time of the target thread is used as the waiting time of the monitoring semaphore. If the target thread executes the trigger operation, the monitoring semaphore will wake up normally. If the target thread runs out of time, the monitoring semaphore will wait for the timeout. The program can judge the state of the semaphore based on whether the semaphore wakes up normally or times out.

[0041] It can be understood that the monitoring semaphore can determine whether there is a trigger operation of the target thread through the corresponding value. For this, after the target thread ends its operation, the target thread can trigger the monitoring semaphore. For example, the remaining time of the target thread is used as the waiting time of the monitoring semaphore, and then when the target thread can perform the trigger operation, the monitoring semaphore will be awakened normally; and when the target thread is blocked or the operation is timed out, the trigger operation cannot be performed within the remaining time, that is, the monitoring semaphore cannot be triggered, and the waiting timeout. For this, the program can configure the corresponding values ​​for the two states of normal awakening and timeout exit of the monitoring semaphore, so as to change the value of the monitoring semaphore through the trigger operation. Therefore, by monitoring the semaphore, the device can determine whether the current target thread is blocked or timed out. When the target thread is blocked or timed out, it can be determined that the monitoring semaphore has timed out.

[0042] Step S130: When the waiting time for the monitored semaphore has timed out, trigger and wait for the target thread to perform a suspend operation at the end stage of the current instruction.

[0043] In the WASM operation mechanism, each line of code will be converted into instructions one by one (one line of code may be converted into many instructions), all instructions can be called OP, the process of executing instructions (OP) is called OP_CALL, that is, the instruction execution stage; when the instruction (OP) is executed, the process is called OP_END, that is, the instruction end stage. In this regard, when running an application thread, when the device compiles the corresponding code, each sentence of code in the thread corresponding program code will become a corresponding instruction (OP), and then during the running process, after each instruction (OP) is executed, it will enter the instruction end stage accordingly.

[0044] In the case of a timeout in the waiting time for the monitoring semaphore, the device can trigger the target thread to perform a suspension operation in the current instruction end phase. It is understandable that when the target thread is currently executing an instruction, the monitoring semaphore waiting timeout occurs. Accordingly, after the instruction is executed, the target thread enters the instruction end phase and performs a suspension operation, thereby releasing the corresponding CPU (Central Processing Unit) resources. It is conceivable that CPU resources include processing speed, number of cores, etc., which are used to determine the processing power and multi-tasking processing capability of the computer.

[0045] Optionally, in one embodiment, if the monitoring semaphore does not wait for a timeout, the device searches for a new target thread again, such as determining a new target thread based on the remaining time as described above.

[0046] Step S140: In response to the target thread executing the suspend operation, based on the CPU resources occupied by the target thread being released, outputting thread timeout information to the host computer through the communication thread.

[0047] Furthermore, when the target thread is suspended, the CPU resources occupied by it are released. In this regard, the device can call a low-priority communication thread to communicate with the host computer. It is understandable that a thread (i.e., the above-mentioned communication thread) will be provided in the PLC device to communicate with the host computer, so as to report the current situation of the PLC device to the host computer and update the firmware. The communication thread does not need to communicate with the host computer in real time, and it can use the idle CPU resources to complete the communication of the host computer, so the priority of the communication thread is low. When the target thread is blocked or the operation is timed out, the device uses the CPU resources occupied by the released target thread to complete the operation of the communication thread, thereby transmitting the thread timeout information to the host computer, so that the user can determine the blocked or timed-out thread through the host computer, so that the user can eliminate the problems caused by the thread.

[0048] It should be noted that, in some embodiments, when the monitoring semaphore does not wait for a timeout or the target thread performs a suspension operation, a new target thread is searched in the currently running application thread and the monitoring semaphore is reset. For example, after the current target thread is already in a suspended state, the device will continue to monitor other application threads, that is, determine a new target thread in the currently running application thread. It is conceivable that the new target thread can be determined based on the remaining time in the thread information.

[0049] It can be seen from the above scheme that the present application monitors the application thread to determine whether the application thread is blocked or has timed out, and triggers the application thread to suspend when there is a blockage or timeout, so that the device does not need to be restarted or reset, effectively ensuring the normal operation of the device, and can also release the occupied CPU resources to use the CPU resources to complete the communication with the host computer, which helps to eliminate abnormal threads.

[0050] In some embodiments, the thread information includes a start-up time and a maximum cycle time, wherein the start-up time corresponds to the time when the thread starts running, and the maximum cycle time corresponds to the end time of the current cycle. Figure 2 A schematic diagram of the steps of selecting a target thread and configuring a monitoring semaphore provided in an embodiment of the present application, wherein the corresponding steps are as follows:

[0051] Step S210: Determine the remaining time of the application thread according to the start-up running time of the application thread, the maximum running time of the cycle, and the current time.

[0052] Step S220: Based on the remaining time, select a thread with the shortest remaining time from the application threads as the target thread.

[0053] Step S230: Wait for a trigger operation on the monitoring semaphore according to the remaining duration of the target thread.

[0054] It is understandable that the remaining time of an application thread is used to indicate the time from the current running time of the application thread in the current cycle to the start time of the next cycle. The device can calculate how much time is left for the thread to complete the work based on the start running time of each application thread, the maximum running time of the cycle, and the current time. It is conceivable that the device can calculate the remaining time of the application thread in real time, or recalculate the corresponding remaining time of each application thread each time the target thread is searched. Therefore, when determining the remaining time, it is still necessary to combine the current time for calculation.

[0055] Then, the thread with the shortest remaining time is selected as the target thread in the application thread. And based on the remaining time corresponding to the target thread, the monitoring semaphore is associated. It can be understood that the monitoring semaphore needs to be triggered within the corresponding time to determine that the target thread is not blocked or the operation is not timed out. In this regard, the device can wait for the triggering operation of the monitoring semaphore according to the remaining time of the target thread, that is, the remaining time of the target thread is used as the waiting time of the monitoring semaphore, and wait for the triggering operation of the monitoring semaphore by the target thread during this time. It can be imagined that if the triggering operation is not received after the timing of the remaining time ends, it is determined that the monitoring semaphore waiting timeout; if the triggering operation is not received after the timing of the remaining time ends, it is determined that the monitoring semaphore waiting timeout.

[0056] Therefore, by monitoring the semaphore, the device can easily and accurately determine whether the application thread is blocked or has timed out, so as to complete the suspension of the application thread in a timely manner, which helps to reduce the occurrence of frequent device restarts.

[0057] In one embodiment, in the case where there are multiple application threads with the shortest remaining time, the device still needs to determine the target thread from them, such as using any application thread among the multiple application threads with the shortest remaining time as the target thread. It can be understood that the remaining time is used to represent the remaining working time in the current cycle. During the remaining time, the application thread may be completed or not completed. Although the remaining time is the same, the actual end time of each application thread is sequential. It can be imagined that if another application thread is completed within the remaining time during the process of monitoring the target thread, the device does not need to monitor the application thread. Furthermore, when the determined target thread ends running within the remaining time, that is, the target thread is completed, correspondingly, a new application thread is selected from the application threads that have not yet ended running as the new target thread. It should be noted that the selection of a new target thread can also be carried out according to the scheme based on the remaining time in the above embodiment.

[0058] Exemplarily, the first thread a and the second thread b are both application threads with the shortest remaining duration. When the first thread a is used as the target thread, when the first thread a ends within the remaining duration, but the second thread b does not end within the remaining duration, the second thread b is still the application thread with the shortest remaining duration, and the second thread b is then used as the reselected target thread. For another example, there are the first thread a, the second thread b, and the third thread c, where the first thread a and the second thread b are both application threads with the shortest remaining duration. If the first thread a is used as the target thread, when the first thread a and the second thread b both end within the remaining duration, and the second thread b ends earlier than the first thread a, the application threads that have not yet ended only include the third thread c, and the reselected target thread is the third thread c.

[0059] Figure 3 A schematic diagram of the steps for triggering the target thread to suspend is provided in an embodiment of the present application. In one embodiment, each application thread in the PLC device is provided with a thread object, and a suspend signal is recorded in the thread object. By configuring the suspend signal, the application thread can be triggered to suspend. In this case, when the application thread is blocked or timed out, the application thread can be suspended to release CPU resources. The specific steps are as follows:

[0060] Step S310: When no trigger operation is received after the remaining time is counted down, it is determined that the monitoring signal quantity waiting timeout has occurred.

[0061] Step S320, in response to a timeout in waiting for the monitored semaphore, a suspend signal is configured in the thread object corresponding to the target thread, and the suspend signal is used to trigger the target thread to perform a suspend operation when the target thread enters the instruction end phase after completing any instruction.

[0062] It is understandable that the device can monitor whether the monitoring semaphore has timed out according to the remaining time, that is, it can count according to the remaining time, and if no trigger operation is received at the end of the count, it can be determined that the monitoring semaphore has timed out. In the case of a monitoring semaphore waiting timeout, the thread object of the target thread is configured, such as configuring a suspend signal in the thread object.

[0063] Exemplarily, when the suspend signal is TRUE, during the running of the target thread, after executing the current instruction, the corresponding instruction end phase will be entered and the suspend operation will be performed. Therefore, when the monitoring semaphore wait times out, the suspend signal in the thread object of the target thread is correspondingly configured to TRUE to trigger the target thread to perform the suspend operation. During the running of the target thread, the device enters the instruction end phase corresponding to the instruction after each instruction is executed, queries the thread object in this phase, and then suspends the target thread when it is determined that the suspend signal is configured to TRUE, so that the target thread enters a sleep state to release the CPU resources it occupies.

[0064] Therefore, by suspending the blocked or timed-out threads, the device can release the corresponding CPU resources without restarting, and use the released CPU resources to execute low-priority threads, so that the device can maintain a communication connection with the host computer.

[0065] In one embodiment, the thread timeout information includes the context information of the thread. It is understandable that the context information of the thread includes the execution state of the thread at a certain moment, such as register state, program counter (PC), stack pointer and stack content and related kernel resource information (such as file descriptor, memory map, etc.). In the multi-threaded environment of the PLC device, each thread has its own independent context information. Among them, the register state is the intermediate data, address, instruction count, etc. of the thread execution saved by the register of the CPU. The program counter is used to indicate the location of the next instruction to be executed by the current thread. The stack pointer and stack content include the state of the function call saved by the call stack of the thread, which includes local variables and return addresses.

[0066] Furthermore, when the target thread performs a suspend operation, the device obtains the context information of the target thread and adds it to the thread timeout information. Moreover, after the target thread is suspended and is in a sleep state, the CPU resources occupied by it are released, and the device calls the CPU resources to start running the communication thread, and provides the communication thread with thread timeout information, so that the communication thread outputs the thread timeout information to the host computer. In this regard, the user can obtain the information of the currently blocked or timed-out thread through the host computer for easy processing. Therefore, by obtaining the context information of the thread and using the released CPU resources, the device can complete the work of feeding back abnormal threads to the host computer, which is conducive to the subsequent elimination of abnormal threads.

[0067] For example, in an application scenario, the PLC device has an application thread for detecting the switch pressing situation, and the priority of the application thread is high to meet the requirement of fast detection speed required by the switch. If the user adds or writes a code with logic problems in the application thread (such as waiting in a loop at a certain moment), and the running time exceeds the cycle of the entire thread, that is, the running timeout occurs, then the low-priority thread cannot obtain CPU resources and cannot be executed, such as the communication thread that communicates with the host computer to report the device status of the current PLC device and update the PLC firmware, which disconnects the communication connection due to access timeout. In addition, the watchdog circuit will cause the system to restart continuously. After each restart, the device will give priority to executing the high-priority thread, that is, the above-mentioned application thread, resulting in the communication thread being unable to connect all the time, unable to complete the communication such as device status reporting and firmware update, resulting in the low-priority communication thread being unable to execute.

[0068] In this regard, the PLC device can implement the thread timeout detection and processing method of the present application by setting a monitoring thread. It can be understood that most of the working time of the monitoring thread is in the waiting of the monitoring semaphore, and the waiting process of the semaphore will release CPU resources. For this reason, the priority of the monitoring thread is set to the highest priority. In addition, the device is also provided with a global object, that is, the above-mentioned list object, which records the thread information corresponding to each thread, such as the start-up running time, the maximum running time of the cycle, the thread object, etc.

[0069] There are monitoring threads, communication threads and several application threads in the PLC device, such as Figure 4 As shown, Figure 4 A schematic diagram of thread interaction in the case of thread timeout is provided for an embodiment of the present application. Taking the interaction between an application thread, a monitoring thread and a communication thread as an example, in the application scenario of the above example, the monitoring thread can monitor the application thread, and when the application thread times out, it can suspend the application thread in time, call the corresponding CPU resources to enable the communication thread to run, and output the thread timeout information to the host computer.

[0070] Specifically, when the application thread starts running, the thread information corresponding to the application thread in the list object is updated, such as updating the start-up running time corresponding to the application thread, so that the monitoring thread can determine the remaining time of the application thread according to the start-up running time of the application thread, the maximum running time of the cycle, and the current time, and then determine whether the application thread is the target thread based on the remaining time. In this example, the application thread is taken as the target thread, and the monitoring thread is set with a corresponding monitoring semaphore, and counts according to the remaining time to wait for the application thread to trigger the monitoring thread.

[0071] Then, in the case of a timeout on the waiting for the monitoring signal, the monitoring thread correspondingly configures the suspend signal in the thread object corresponding to the application thread, such as configuring the suspend signal to TRUE. Accordingly, when the application thread detects the suspend signal configured to TRUE at OP_END, the application thread performs a suspend operation, and the application thread is in a suspended state. In addition, the monitoring thread also triggers the communication thread to start running. The communication thread runs based on the CPU resources released by the application thread. The monitoring thread can send the context information of the application thread to the communication thread, so that the communication thread can provide the host computer with thread timeout information and notify the host computer of the relevant information of the thread that is currently running and timed out. Therefore, in the application scenario of the above example, the communication thread can communicate with the host computer in a timely manner to maintain the communication connection between the two, and the device will not restart frequently to cause device failure.

[0072] This solution monitors the application thread and triggers the application thread to suspend when there is a blockage or operation timeout. It detects the thread timeout in time and suspends the thread to release resources, so that the device does not need to be restarted or reset, thereby reducing the occurrence of frequent restarts or resets of the system, effectively ensuring the normal operation of the device, and can also release the occupied CPU resources to use the CPU resources to complete the communication with the host computer, which helps to eliminate abnormal threads.

[0073] Figure 5 A thread interaction diagram is provided for an embodiment of the present application when the thread has not timed out. As shown in the figure, when the application thread is able to run to completion within the remaining time, thereby executing the trigger operation, in this regard, the monitoring semaphore in the monitoring thread does not wait for timeout, the monitoring thread re-searches for a new target thread, and the application thread enters a sleep state to wait for the next cycle.

[0074] It can be imagined that the communication methods between threads include shared memory and message passing. Among them, the shared memory method is that threads communicate by reading and writing data in shared memory. For example, multiple threads can operate the same shared variable. When one thread modifies the value of this variable, other threads can perceive this change and respond accordingly. This method does not require explicit message sending, but achieves coordination and communication between threads through shared resources. The message passing method is that threads need to communicate by sending and receiving messages. For example, it can be a wait / notify mechanism, a join method, and a pipeline input / output stream. This method requires threads to explicitly send messages to notify other threads of the occurrence of certain events. It should be noted that the communication between threads in this solution can adopt any of the above methods.

[0075] Figure 6 The present invention is a schematic diagram of the structure of a thread timeout detection and processing device provided in an embodiment of the present invention. The device is used to execute the thread timeout detection and processing method provided in the above embodiment, and has the corresponding functional modules and beneficial effects of the execution method. The thread timeout detection and processing device is applied to a PLC device, and the PLC device is provided with a list object, which is used to record the thread information of the application thread burned into the PLC device. The thread timeout processing device includes: an information update module 601, a thread search module 602, a timeout detection module 603 and a timeout alarm module 604.

[0076] The information updating module 601 is configured to update the thread information recorded in the list object when there is at least one application thread in a running state;

[0077] The thread search module 602 is configured to search for a thread with the shortest remaining time in the application threads based on the updated thread information in the list object as the target thread, and set a monitoring semaphore associated with the target thread, the waiting time of the monitoring semaphore is the remaining time of the target thread, so as to wait for the target thread to trigger the monitoring semaphore after the target thread ends its operation;

[0078] The timeout detection module 603 is configured to trigger and wait for the target thread to perform a suspend operation at the end stage of the current instruction when the waiting time for the monitoring semaphore expires;

[0079] The timeout alarm module 604 is configured to respond to the target thread executing the suspend operation and output thread timeout information to the upper computer through the communication thread based on the CPU resources occupied by the released target thread.

[0080] Based on the above embodiment, the thread information includes the start-up running time, and the information updating module 601 is specifically configured as follows:

[0081] In response to the running of any application thread, the start-up running time of the currently running application thread is recorded, and the start-up running time corresponding to the application thread is updated in the list object.

[0082] Based on the above embodiment, the thread information includes the start running time and the maximum running time of the cycle, and the thread search module 602 is specifically configured as follows:

[0083] Determine the remaining time of the application thread according to the start running time of the application thread, the maximum running time of the cycle and the current time;

[0084] Based on the remaining time, select the thread with the shortest remaining time from the application threads as the target thread;

[0085] Wait for the triggering operation of the monitoring semaphore according to the remaining time of the target thread.

[0086] Based on the above embodiment, the thread search module 602 is further specifically configured as follows:

[0087] When there are multiple application threads with the shortest remaining time, select one application thread from the multiple application threads as the target thread;

[0088] If the target thread finishes running within the remaining time, a new application thread is selected from the application threads that have not yet finished running as a new target thread.

[0089] Based on the above embodiment, the timeout detection module 603 is specifically configured as follows:

[0090] If no trigger operation is received after the remaining time is exceeded, it is determined that the monitoring signal quantity waiting timeout;

[0091] In response to a timeout on the waiting of the monitoring semaphore, a suspend signal is configured in the thread object corresponding to the target thread. The suspend signal is used to trigger the target thread to perform a suspend operation when the target thread enters the instruction end phase after completing any instruction.

[0092] Based on the above embodiment, the timeout alarm module 604 is specifically configured as follows:

[0093] When the target thread performs a suspend operation, the context information of the target thread is obtained and added to the thread timeout information;

[0094] Provide thread timeout information to the communication thread, and call CPU resources to start running the communication thread to output the thread timeout information to the host computer.

[0095] On the basis of the above embodiment, the device further includes a thread reselection module, and the thread reselection module is specifically configured as follows:

[0096] When the monitoring semaphore does not wait for a timeout or the target thread performs a suspend operation, a new target thread is searched in the currently running application threads, and the monitoring semaphore is reset.

[0097] It is worth noting that in the embodiment of the above-mentioned device, each module is divided only according to functional logic, but is not limited to the above-mentioned division, as long as the corresponding functions can be realized; in addition, the specific name of each module is only for the convenience of distinguishing each other, and is not used to limit the scope of protection of this application.

[0098] Figure 7 The structural diagram of the PLC device provided in an embodiment of the present application is used to execute the thread timeout detection and processing method provided in the above embodiment, and has the functional modules and beneficial effects corresponding to the execution method. As shown in the figure, it includes a processor 701, a memory 702, an input device 703 and an output device 704. The number of processors 701 can be one or more, and one processor 701 is taken as an example in the figure; the processor 701, the memory 702, the input device 703 and the output device 704 can be connected by a bus or other means, and the figure takes the connection through the bus as an example. The memory 702, as a computer-readable storage medium, can be used to store software programs, computer executable programs and modules, such as the program instructions / modules corresponding to the thread timeout detection and processing method in the embodiment of the present application. The processor 701 executes the corresponding various functional applications and data processing by running the software programs, instructions and modules stored in the memory 702, that is, the above-mentioned thread timeout detection and processing method is realized.

[0099] The memory 702 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system and at least one application required for a function; the data storage area may store data recorded or created during use, etc. In addition, the memory 702 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some embodiments, the memory 702 may further include a memory remotely arranged relative to the processor 701, and these remotely arranged memories may be connected to the terminal device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.

[0100] The input device 703 can be used to input corresponding digital or character information to the processor 701, and to generate key signal input related to the user settings and function control of the device; the output device 704 can be used to send or display key signal output related to the user settings and function control of the device.

[0101] An embodiment of the present application also provides a storage medium storing computer executable instructions, which, when executed by a processor, are used to perform relevant operations in the thread timeout detection and processing method provided in any embodiment of the present application.

[0102] Computer-readable storage media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer-readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device.

[0103] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.

[0104] Note that the above are only preferred embodiments of the present application and the technical principles used. Those skilled in the art will understand that the present application is not limited to the specific embodiments described herein, and that various obvious changes, readjustments and substitutions can be made by those skilled in the art without departing from the scope of protection of the present application. Therefore, although the present application is described in more detail through the above embodiments, the present application is not limited to the above embodiments, and may include more other equivalent embodiments without departing from the concept of the present application, and the scope of the present application is determined by the scope of the appended claims.

Claims

1. A thread timeout detection and processing method, characterized in that: Applied to a PLC device, the PLC device is provided with a list object, the list object is used to record thread information of application threads burned into the PLC device, and the thread timeout detection and processing method includes: In the case that there is at least one of the application threads in a running state, updating the thread information recorded in the list object; Based on the updated thread information in the list object, searching the application threads for a thread with the shortest remaining time as a target thread, and setting a monitoring semaphore associated with the target thread, wherein the waiting time of the monitoring semaphore is the remaining time of the target thread, so as to wait for the target thread to trigger the monitoring semaphore after finishing running; In the case where the monitoring semaphore wait times out, triggering and waiting for the target thread to perform a suspend operation at the end stage of the current instruction; In response to the target thread executing the suspend operation, based on the released CPU resources occupied by the target thread, thread timeout information is output to a host computer through a communication thread.

2. The thread timeout detection and processing method according to claim 1, characterized in that: The thread information includes the start-up running time, and when at least one of the application threads is in the running state, updating the thread information recorded in the list object includes: In response to the running of any application thread, the start-up running time of the currently running application thread is recorded, and the start-up running time corresponding to the application thread is updated in the list object.

3. The thread timeout detection and processing method according to claim 1, characterized in that: The thread information includes the start running time and the maximum running time of the cycle. Based on the updated thread information in the list object, searching the application thread for the thread with the shortest remaining time as the target thread, and setting the monitoring semaphore associated with the target thread, the waiting time of the monitoring semaphore is the remaining time of the target thread, so as to wait for the target thread to trigger the monitoring semaphore after the running ends, including: Determine the remaining time of the application thread according to the start running time of the application thread, the maximum running time of the cycle and the current time; Based on the remaining time, selecting a thread with the shortest remaining time from the application threads as the target thread; Wait for a triggering operation on the monitoring semaphore according to the remaining duration of the target thread.

4. The thread timeout detection and processing method according to claim 3, characterized in that: The selecting, based on the remaining time, a thread with the shortest remaining time from the application threads as the target thread comprises: When there are multiple application threads with the shortest remaining time, select one application thread from the multiple application threads as the target thread; If the target thread finishes running within the remaining time, a new application thread is selected from the application threads that have not yet finished running as a new target thread.

5. The thread timeout detection and processing method according to any one of claims 1 to 4, characterized in that: The thread information includes a thread object, and in the case where the monitoring semaphore wait times out, triggering and waiting for the target thread to perform a suspend operation at the end stage of the current instruction includes: If no trigger operation is received after the remaining time is counted down, determining that the monitoring signal quantity waiting timeout has occurred; In response to the monitoring semaphore waiting timeout, a suspend signal is configured in the thread object corresponding to the target thread, and the suspend signal is used to trigger the target thread to perform a suspend operation when the target thread enters the instruction end stage after completing any instruction.

6. The thread timeout detection and processing method according to claim 1, characterized in that: The step of outputting thread timeout information to a host computer through a communication thread based on releasing CPU resources occupied by the target thread in response to the target thread executing the suspend operation comprises: When the target thread performs a suspend operation, obtaining context information of the target thread and adding the context information to the thread timeout information; The thread timeout information is provided to the communication thread, and the CPU resource is called to start running the communication thread, so as to output the thread timeout information to the host computer.

7. The thread timeout detection and processing method according to claim 1 or 2, characterized in that: The method further comprises: In the case that the monitoring semaphore has not waited for a timeout or the target thread performs a suspend operation, a new target thread is searched in the currently running application threads, and the monitoring semaphore is reset.

8. A thread timeout detection and processing device, characterized in that: Applied to a PLC device, the PLC device is provided with a list object, the list object is used to record thread information of application threads burned into the PLC device, and the thread timeout processing and detection device includes: An information updating module, configured to update the thread information recorded in the list object when at least one of the application threads is in a running state; A thread search module is configured to search for a thread with the shortest remaining time in the application threads based on the updated thread information in the list object as a target thread, and set a monitoring semaphore associated with the target thread, wherein the waiting time of the monitoring semaphore is the remaining time of the target thread, so as to wait for the target thread to trigger the monitoring semaphore after the target thread ends its operation; A timeout detection module, configured to trigger and wait for the target thread to perform a suspend operation at the end stage of the current instruction when the monitoring semaphore wait times out; The timeout alarm module is configured to respond to the target thread executing the suspension operation and output thread timeout information to the upper computer through the communication thread based on the CPU resources occupied by the target thread being released.

9. A PLC device, characterized in that: include: one or more processors; A storage device for storing one or more programs, when the one or more programs are executed by the one or more processors, the one or more processors implement the thread timeout detection and processing method as described in any one of claims 1-7.

10. A storage medium storing computer executable instructions, characterized in that: When the computer executable instructions are executed by a processor, they are used to perform the thread timeout detection and processing method according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Multi-thread watch dog monitoring method

    CN101794244A

  • Thread detection method, thread detection terminal and computer readable storage medium

    CN107247630A

  • Terminal control method and device

    CN111666120A

  • Event processing method and device based on threads and coroutines and terminal equipment

    CN116932248A

  • Terminal control method and device

    TW202044023A