Thread timeout detection and handling method, apparatus, device, and storage medium

By monitoring the remaining time of threads in the PLC device and suspending timed-out threads, the system anomalies caused by high-priority thread blocking or timeout were resolved, enabling continuous device operation and anomaly information feedback.

CN119917366BActive Publication Date: 2026-01-27GUANGZHOU ZHIYUAN ELECTRONICS CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411867832.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-12-18
Publication Date
2026-01-27
Estimated Expiration
2044-12-18

AI Technical Summary

Technical Problem

In PLC devices, when a high-priority thread is blocked or times out, a low-priority thread cannot execute, causing the system to restart frequently, fail to connect to the host computer, and lose abnormal data.

Method used

By setting up a list object in the PLC device to record thread information, monitoring the remaining time of the semaphore waiting thread, suspending the target thread when a timeout occurs, releasing CPU resources, and reporting the timeout information to the host computer via a communication thread.

Benefits of technology

It enables the system to maintain normal operation without restarting or resetting the device, freeing up CPU resources for communication, timely feedback of abnormal information, and reducing frequent system restarts.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119917366B_ABST
    Figure CN119917366B_ABST
Patent Text Reader

Abstract

The application provides a thread timeout detection and processing method and device, equipment and a storage medium, relates to the computer technical field, solves the problem that other low-priority threads cannot be executed when the PLC device is in a thread timeout in the related art, determines whether the application thread is blocked or runs overtime by monitoring the application thread, triggers the application thread to be suspended when the application thread is blocked or runs overtime, detects the thread timeout condition in time and suspends the thread to release resources, so that the system can continue to run without crashing under the condition that the device does not need to be restarted or reset, and the released CPU resources are used to complete communication with the upper computer, feed back the abnormal condition of the current system, and upload the suspended thread timeout information to the upper computer to assist the user in troubleshooting the exception.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular to a method, apparatus, device and storage medium for thread timeout detection and processing. Background Technology

[0002] In PLC (Programmable Automation Controller) devices, users can freely define the priorities of application threads. In real-time systems, higher-priority threads execute first, and lower-priority threads execute later. If a user sets a high-priority thread and a low-priority thread, and the high-priority thread becomes blocked (or times out), the low-priority thread will not be able to execute.

[0003] In related technologies, watchdog timers are typically used to handle thread blocking or timeouts. When these issues occur, watchdog timers can only reset or restart the system. However, in PLC devices, directly restarting the system can easily lead to frequent restarts and inability to connect to the host computer. Summary of the Invention

[0004] This application provides a thread timeout detection and handling method, apparatus, device and storage medium, which solves the problem in related technologies that when a thread times out, other low-priority threads in PLC devices are prone to fail to execute. This solution can detect thread timeout in a timely manner and suspend the thread to release resources, so that the device can continue to run without restarting or resetting, which is beneficial to the continuous operation of the device.

[0005] Firstly, this application provides a thread timeout detection and handling method applied to a PLC device. The PLC device has a list object configured to record thread information of application threads programmed into the PLC device. The thread timeout detection and handling method includes:

[0006] If at least one application thread is running, update 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 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 it finishes running.

[0008] If the monitoring semaphore timeout occurs, trigger and wait for the target thread to execute the suspension operation during the current instruction termination phase;

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

[0010] Secondly, this application also provides a thread timeout detection and processing device, applied to a PLC device. The PLC device is equipped with a list object, which is used to record thread information of the application threads programmed into the PLC device. The thread timeout processing device includes:

[0011] The information update module is configured to update the thread information recorded in the list object when at least one application thread is running.

[0012] The thread lookup module is configured to search for the thread with the shortest remaining time in the application threads based on the updated thread information in the list object, 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 finish running and trigger the monitoring semaphore.

[0013] The timeout detection module is configured to trigger and wait for the target thread to perform a suspension operation during the current instruction termination phase when the monitored semaphore times out.

[0014] The timeout alarm module is configured to respond to the target thread's suspension operation by outputting thread timeout information to the host computer through a communication thread based on the released CPU resources occupied by the target thread.

[0015] Thirdly, this application also provides a PLC device, which includes:

[0016] One or more processors;

[0017] 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 handling method of this application.

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

[0020] This application solution monitors application threads to determine if they are blocked or timed out. If such blocking or timeout occurs, the application thread is suspended, allowing the system to continue running without crashing without requiring a device restart or reset. Simultaneously, the released CPU resources are used to communicate with the host computer, reporting any system anomalies and uploading timeout information of suspended threads to the host computer to assist the user in troubleshooting. Attached Figure Description

[0021] Figure 1 This is a schematic diagram illustrating the steps of a thread timeout detection and handling method provided in an embodiment of this application;

[0022] Figure 2 This is a schematic diagram illustrating the steps of selecting a target thread and configuring a monitoring semaphore according to an embodiment of this application;

[0023] Figure 3 This is a schematic diagram illustrating the steps for triggering the suspension of a target thread according to an embodiment of this application;

[0024] Figure 4 This is a schematic diagram of thread interaction in the event of a thread timeout, provided as an embodiment of this application.

[0025] Figure 5 This is a schematic diagram of thread interaction in the case where the thread has not timed out, provided as an embodiment of this application.

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

[0027] Figure 7 This is a schematic diagram of the structure of a PLC device provided in an embodiment of this application. Detailed Implementation

[0028] The embodiments of this application will be further described in detail below with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are merely illustrative of the embodiments of this application and are not intended to limit the scope of this application. Furthermore, it should be noted that, for ease of description, only the parts related to the embodiments of this application are shown in the accompanying drawings, not all structures. Those skilled in the art, after reading this specification, should be able to conceive that any combination of technical features can constitute an optional implementation method, provided that the technical features do not contradict each other.

[0029] The terms "first," "second," etc., used in the specification and claims of this application are used to distinguish similar objects, not to describe a specific order or sequence. It should be understood that such use of data can be interchanged where appropriate so that embodiments of this application can be implemented in orders other than those illustrated or described herein, and the objects distinguished by "first," "second," etc., are generally of the same class, not limited in number; for example, a first object can be one or more. Furthermore, in the specification and claims, "and / or" indicates at least one of the connected objects, and the character " / " generally indicates that the preceding and following objects are in an "or" relationship. In the description of this application, "multiple" means two or more, and "several" means one or more.

[0030] In embedded real-time systems, PLC devices allow users to define the priorities of application threads. Higher-priority threads execute first, and lower-priority threads execute later. Furthermore, the device can perform multi-threaded operations. If a user sets both a high-priority thread and a low-priority thread, and the high-priority thread becomes blocked (or times out), the low-priority thread will not be able to execute.

[0031] The proposed solution is to add a watchdog timer to each thread, automatically restarting or resetting the system when congestion or timeouts are detected. However, if the code of a high-priority thread itself has problems, congestion or timeouts may continue after restarting, leading to repeated system restarts and system malfunctions. Furthermore, a system restart or reset disconnects the device from the host computer, preventing the host computer from controlling the device and monitoring the PLC's status. Additionally, a system restart or reset erases previously useful error data, making troubleshooting difficult for the user.

[0032] In response, this application provides a thread timeout detection and handling method, which can be applied to a PLC device based on WASM (WebAssembly) technology, so that the PLC device can be user-defined in terms of the priority of application threads, with high-priority threads executing first and low-priority threads executing later. Figure 1 This is a schematic diagram illustrating the steps of a thread timeout detection and handling method provided in an embodiment of this application. A PLC device can execute this method to detect and handle threads that have timed out or are blocked. Furthermore, the PLC device also has a list object as a global object, which records thread information for all application threads programmed into the PLC device. For example... Figure 1As shown, the specific steps of this method include steps S110-S140. It can be conceivable that in practical applications, a highest-priority monitoring thread can be set in the PLC device to implement the timeout detection and handling method for this thread. The specific steps are as follows:

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

[0034] Understandably, for each application thread programmed 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 based on the thread information. It's conceivable that whenever a user creates an application thread within the PLC device, the list object can receive and record the thread information for that application thread.

[0035] PLC devices support multi-threaded operation, and each application thread in a PLC device runs according to a cycle. It's conceivable that the cycle for each application thread can be different. Within each cycle, the application thread runs for a corresponding period of time, while the remaining time is spent in a sleep state. For example, if a thread's cycle is 50ms and its execution time is 2ms, then the remaining 48ms are sleep time.

[0036] To address this, when at least one application thread is running, the thread information recorded in the list object is updated when any application thread starts working. This allows the system to determine if the application thread is running by querying the list object, which helps in determining if the application thread has timed out. Optionally, in one embodiment, the thread information includes the start time. It is conceivable that although each application thread in a PLC device runs cyclically, the complexity of the operating conditions within the PLC device may cause the start time of each application thread to change within the cycle. Therefore, when any application thread starts running, the device responds by recording the start time of that application thread as the start time and further updates the corresponding start time in the list object.

[0037] Step S120: Based on the updated thread information in the list object, find the thread with the shortest remaining duration in the application threads as the target thread, and set a monitoring semaphore associated with the target thread. The waiting time of the monitoring semaphore is the remaining duration of the target thread, so as to wait for the target thread to trigger the monitoring semaphore after it finishes running.

[0038] It's conceivable that the application thread being searched is a currently running thread, from which the target thread is identified—the one with the shortest remaining time. In other words, the thread information records relevant time information to determine the remaining time of each application thread; this remaining time is the time elapsed between the current running time of the application thread and the start of the next cycle. For example, in a PLC device where a highest-priority monitoring thread is set up to implement timeout detection and handling, the monitoring thread can use a list object to find the thread with the shortest remaining time among the currently running application threads and use it as the target thread. Alternatively, if no application threads are running, a check can be performed at preset intervals to wait for an application thread that can be monitored.

[0039] In addition, a monitoring semaphore is set up. It can be understood that this monitoring semaphore is a flag associated with the target thread. The waiting time of the monitoring semaphore is the remaining time of the target thread, and then it waits for the target thread to finish running before triggering the monitoring semaphore.

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

[0041] It's understandable that a monitoring semaphore, through its corresponding value, can determine whether a trigger operation from a target thread exists. After the target thread finishes running, it can trigger the monitoring semaphore. If the remaining time of the target thread is used as the waiting time for the monitoring semaphore, then when the target thread can execute the trigger operation, the monitoring semaphore will be normally awakened. However, if the target thread is blocked or times out, it cannot execute the trigger operation within the remaining time, meaning the monitoring semaphore cannot be triggered and the wait times out. This can be addressed programmatically by configuring corresponding values ​​for the two states of normal wake-up and timeout exit of the monitoring semaphore, allowing the value of the monitoring semaphore to be changed through trigger operations. Therefore, by monitoring the semaphore, the device can determine whether the current target thread is blocked or times out; when the target thread is blocked or times out, it can be determined that the monitoring semaphore has timed out.

[0042] Step S130: If the monitoring semaphore timeout occurs, trigger and wait for the target thread to perform a suspension operation during the current instruction termination phase.

[0043] In the WASM runtime mechanism, each line of code is translated into instructions (one line of code may translate into many instructions). All instructions can be called OPs (operations). The process of executing an instruction (OP) is called OP_CALL, which is the instruction execution phase; the process after executing an instruction (OP) is called OP_END, which is the instruction termination phase. Therefore, when running an application thread, when the device compiles the corresponding code, each line of code in the thread's corresponding program code is transformed into a corresponding instruction (OP). Thus, during the execution process, after each instruction (OP) is executed, the thread enters the instruction termination phase.

[0044] If the monitoring semaphore times out, the device can trigger the target thread to suspend during the current instruction completion phase. This means that when the target thread is currently executing an instruction, if the monitoring semaphore times out, the target thread will suspend during the instruction completion phase after the instruction finishes, thus releasing the corresponding CPU (Central Processing Unit) resources. These CPU resources include processing speed and the number of cores, which determine the computer's processing power and multitasking capabilities.

[0045] Optionally, in one embodiment, if the monitoring semaphore has not timed out, the device searches for a new target thread, as described above based on the remaining time.

[0046] Step S140: In response to the target thread performing a suspension operation, based on the CPU resources occupied by the released target thread, output thread timeout information to the host computer through the communication thread.

[0047] Furthermore, when the target thread is suspended, the CPU resources it occupied are released. The device can then use a low-priority communication thread to communicate with the host computer. Understandably, the PLC device has a dedicated thread (the aforementioned communication thread) that communicates with the host computer to report the current status of the PLC device and update firmware. This communication thread does not need to communicate with the host computer in real time; it can utilize idle CPU resources to complete the communication. Therefore, the communication thread has a low priority. When the target thread becomes blocked or times out, the device uses the released CPU resources to run the communication thread, thereby transmitting the thread timeout information to the host computer. This allows the user to identify the blocked or timed-out thread through the host computer, facilitating troubleshooting.

[0048] It should be noted that in some embodiments, if the monitoring semaphore has not timed out or the target thread has performed a suspension operation, a new target thread is searched among the currently running application threads, and the monitoring semaphore is reset. For example, even if 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 among the currently running application threads. It is conceivable that the determination of a new target thread can be based on the remaining duration in the thread information.

[0049] As can be seen from the above solution, this application monitors the application thread to determine whether the application thread is blocked or timed out. When there is a blockage or timeout, the application thread is suspended, so that the device does not need to be restarted or reset, which effectively ensures the normal operation of the device. It can also release the occupied CPU resources to complete the communication with the host computer, which helps to eliminate abnormal threads.

[0050] In some embodiments, thread information includes start time and maximum periodicity, where start time corresponds to the time when the thread begins running, and maximum periodicity corresponds to the end time of the current period. Figure 2 This is a schematic diagram illustrating the steps of selecting a target thread and configuring a monitoring semaphore according to an embodiment of this application. The corresponding steps are as follows:

[0051] Step S210: Determine the remaining duration of the application thread based on the application thread's start time, maximum periodic runtime, and current time.

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

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

[0054] Understandably, the remaining time of an application thread represents the time elapsed between the current running time of the application thread and the start of the next cycle. The device can calculate how much time a thread has left to complete its work based on its start time, maximum cycle time, and the current time. It's conceivable that the device can calculate the remaining time of application threads in real-time, or it can recalculate the remaining time for each application thread each time a target thread is searched. Therefore, determining the remaining time still requires consideration of the current time.

[0055] Therefore, the thread with the shortest remaining time is selected as the target thread from the application threads. Based on the remaining time of this target thread, a monitoring semaphore is associated. It's understood that the monitoring semaphore needs to be triggered within a certain time to confirm that the target thread is not blocked or has timed out. To this end, the device can wait for the target thread's remaining time to trigger the monitoring semaphore; that is, the remaining time of the target thread is used as the waiting time for the monitoring semaphore. Within this time, the device waits for the target thread to trigger the monitoring semaphore. It's conceivable that if no trigger operation is received after the remaining time expires, the monitoring semaphore wait timeout is determined.

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

[0057] In one embodiment, when there are multiple application threads with the shortest remaining time, the device still needs to determine the target thread. For example, any one of the application threads with the shortest remaining time can be selected as the target thread. It's understood that the remaining time represents the remaining working time within the current period. Within this remaining time, an application thread may complete execution or fail to complete. Furthermore, although the remaining time is the same, the actual end times of each application thread differ. It's conceivable that if another application thread completes execution within its remaining time while the target thread is being monitored, the device does not need to monitor that application thread. Therefore, once the determined target thread finishes running within its remaining time, i.e., the target thread has completed execution, a new application thread is selected from the remaining application threads as the new target thread. It should be noted that selecting a new target thread can also be done according to the remaining time-based selection scheme described in the above embodiment.

[0058] For example, if both thread a and thread b are application threads with the shortest remaining time, and thread a is the target thread, if thread a terminates within the remaining time while thread b continues running before termination, thread b will still be the application thread with the shortest remaining time, and will be selected as the new target thread. As another example, consider a first thread a, a second thread b, and a third thread c, where both thread a and thread b are application threads with the shortest remaining time. If thread a is the target thread, and both thread a and thread b terminate within the remaining time, but thread b terminates earlier than thread a, the only application thread still running is thread c, and will be selected as the new target thread.

[0059] Figure 3 This diagram illustrates the steps for triggering target thread suspension according to an embodiment of this application. In one embodiment, each application thread within the PLC device has a thread object, which records a suspension signal. By configuring the suspension signal, the application thread can be suspended. Therefore, when an application thread becomes blocked or times out, CPU resources can be released by suspending the application thread. The specific steps are as follows:

[0060] Step S310: If no trigger operation is received before the remaining time expires, determine that the monitoring semaphore has timed out.

[0061] Step S320: In response to the timeout of the monitoring semaphore, configure a suspension signal in the thread object corresponding to the target thread. The suspension signal is used to trigger the target thread to perform a suspension operation when the target thread enters the instruction end stage after completing any instruction.

[0062] Understandably, the device can monitor whether the monitored semaphore has timed out based on the remaining time. That is, it times the event based on the remaining time; if no trigger operation is received by the end of the timeout, it can be determined that the monitored semaphore has timed out. For cases where the monitored semaphore has timed out, the target thread's thread object can be configured, such as by configuring a suspend signal within that thread object.

[0063] For example, when the suspend signal is TRUE, during the execution of the target thread, after completing the current instruction, it will enter the corresponding instruction termination stage and perform a suspension operation. Therefore, when the monitoring semaphore timeout occurs, the suspend signal in the target thread's thread object is configured to TRUE to trigger the target thread to perform a suspension operation. During the execution of the target thread, after each instruction is completed, the device enters the instruction termination stage corresponding to that instruction. In this stage, the thread object is queried, and if it is determined that the suspend signal is configured to TRUE, the target thread is suspended, causing the target thread to enter a sleep state to release the CPU resources it occupies.

[0064] Therefore, by suspending threads that are blocked or have timed out, 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 and the host computer can maintain a communication connection.

[0065] In one embodiment, thread timeout information includes thread context information. This context information includes the thread's execution state at a given moment, such as register states, program counter (PC), stack pointer and stack contents, and related kernel resource information (e.g., file descriptors, memory mappings). In a multi-threaded PLC device environment, each thread has its own independent context information. The register states are the intermediate data, addresses, and instruction counts stored in the CPU's registers during thread execution. The program counter indicates the location of the next instruction to be executed by the current thread. The stack pointer and stack contents include the function call states stored in the thread's call stack, including local variables and return addresses.

[0066] Furthermore, when the target thread is suspended, the device acquires the target thread's context information and adds it to the thread timeout information. After the target thread is suspended and enters a sleep state, the CPU resources it occupied are released. The device then uses these CPU resources to start and run the communication thread, and provides the communication thread with the thread timeout information, enabling the communication thread to output this timeout information to the host computer. The user can then obtain information about currently blocked or timed-out threads through the host computer for further processing. Therefore, by acquiring the thread's context information and utilizing the released CPU resources, the device can complete the task of reporting abnormal threads to the host computer, which is beneficial for subsequent troubleshooting of abnormal threads.

[0067] For example, in one application scenario, a PLC device has an application thread for detecting switch presses. This application thread has a high priority to meet the high detection speed requirement of the switch. If the user adds or writes problematic code in this application thread (such as continuously looping and waiting at a certain time), causing the runtime to exceed the entire thread's cycle time, i.e., a timeout occurs, then lower-priority threads cannot obtain CPU resources and thus cannot execute. For example, the communication thread for communicating with the host computer to report the current PLC device status and update the PLC firmware will disconnect due to access timeout. Moreover, the watchdog circuit will cause the system to restart continuously. After each restart, the device will prioritize executing the high-priority thread, i.e., the aforementioned application thread, causing the communication thread to be unable to connect and complete communication such as device status reporting and firmware updates, thus preventing the lower-priority communication thread from executing.

[0068] To address this, the PLC device can implement the thread timeout detection and handling method described in this application by setting a monitoring thread. It's understood that the monitoring thread spends most of its time waiting for semaphores, and this waiting process releases CPU resources. Therefore, the monitoring thread is set to the highest priority. Additionally, the device also sets up a global object, namely the aforementioned list object, which records thread information for each thread, such as start time, maximum periodic runtime, and thread object.

[0069] A PLC device contains a monitoring thread, a communication thread, and several application threads, such as... Figure 4 As shown, Figure 4 This is a schematic diagram of thread interaction in the case of thread timeout provided in an embodiment of this 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 suspend the application thread in time when the application thread times out, and call the corresponding CPU resources so that the communication thread can run, and output the thread timeout information to the host computer.

[0070] Specifically, when an application thread starts running, the list object updates the thread information corresponding to that application thread, such as updating the application thread's start time. This allows the monitoring thread to determine the remaining time of the application thread based on its start time, maximum periodic runtime, 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 the target thread. The monitoring thread sets up a corresponding monitoring semaphore and counts the remaining time, waiting for the application thread to trigger an operation on the monitoring thread.

[0071] Furthermore, if the monitoring signal times out, the monitoring thread configures the suspension signal in the thread object corresponding to the application thread. For example, if the suspension signal is configured as TRUE, the application thread will execute a suspension operation when it detects a TRUE-configured suspension signal in OP_END, thus placing the application thread in a suspended state. Additionally, the monitoring thread triggers the communication thread to start running. The communication thread operates based on the CPU resources released by the application thread. The monitoring thread can send the application thread's context information to the communication thread, enabling the communication thread to provide thread timeout information to the host computer, notifying the host computer of relevant information about currently timed-out threads. Therefore, in the application scenario described above, the communication thread can communicate with the host computer in a timely manner, maintaining the communication connection between the two, and the device will not frequently restart, thus preventing device malfunctions.

[0072] This solution monitors application threads and suspends them when blockages or timeouts occur. This allows for timely detection of thread timeouts and suspension of threads to release resources, eliminating the need for device restarts or resets. This reduces the frequency of system restarts or resets, effectively ensuring the normal operation of the device. Furthermore, it releases occupied CPU resources to facilitate communication with the host computer, which helps in the elimination of abnormal threads.

[0073] Figure 5 The figure shows a schematic diagram of thread interaction in the case where the thread has not timed out, provided as an embodiment of this application. As shown, the application thread is able to finish running within the remaining time and thus execute the trigger operation. In this case, the monitoring semaphore in the monitoring thread does not wait for the timeout, and the monitoring thread searches for a new target thread. Meanwhile, the application thread enters a sleep state to wait for the next cycle.

[0074] It's conceivable that inter-thread communication methods include shared memory and message passing. Shared memory communication involves threads reading and writing data in shared memory. For example, multiple threads can operate on the same shared variable; when one thread modifies the variable's value, other threads can detect the change and react accordingly. This method doesn't require explicit message sending but achieves coordination and communication through shared resources. Message passing, on the other hand, requires threads to communicate by sending and receiving messages. Examples include wait / notify mechanisms, join methods, and piped input / output streams. This method requires threads to explicitly send messages to notify other threads of certain events. It should be noted that any of these methods can be used for communication between threads in this solution.

[0075] Figure 6 This is a schematic diagram of the structure of a thread timeout detection and processing device provided in an embodiment of this application. The device is used to execute the thread timeout detection and processing method provided in the above embodiment, and has corresponding functional modules and beneficial effects for executing the method. The thread timeout detection and processing device is applied to a PLC device. The PLC device has a list object, which is used to record thread information of application threads 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 update module 601 is configured to update the thread information recorded in the list object when at least one application thread is running.

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

[0078] The timeout detection module 603 is configured to trigger and wait for the target thread to perform a suspension operation during the current instruction termination phase when the monitoring semaphore times out.

[0079] The timeout alarm module 604 is configured to output thread timeout information to the host computer via a communication thread in response to the target thread performing a suspension operation, based on the CPU resources occupied by the released target thread.

[0080] Based on the above embodiments, the thread information includes the start time, and the information update module 601 is specifically configured as follows:

[0081] In response to the execution of any application thread, record the start time of the currently running application thread and update the corresponding start time of the application thread in the list object.

[0082] Based on the above embodiments, the thread information includes the start time and maximum periodic runtime, and the thread lookup module 602 is specifically configured as follows:

[0083] Determine the remaining duration of the application thread based on its start time, maximum periodic runtime, and current time.

[0084] Based on the remaining time, the thread with the shortest remaining time among the application threads is selected as the target thread;

[0085] Wait for the target thread to trigger the monitoring semaphore based on the remaining time.

[0086] Based on the above embodiments, the thread lookup module 602 is further configured as follows:

[0087] If there are multiple application threads with the shortest remaining time, select one application thread as the target thread from among them.

[0088] If the target thread terminates within the remaining time, a new application thread will be selected from the application threads that have not yet terminated as the new target thread.

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

[0090] If no trigger operation is received before the remaining time expires, it is determined that the monitoring semaphore has timed out.

[0091] In response to a timeout in the monitoring semaphore, a suspension signal is configured in the thread object corresponding to the target thread. The suspension signal is used to trigger the target thread to perform a suspension operation when the target thread enters the instruction termination stage after completing any instruction.

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

[0093] If the target thread performs a suspension operation, obtain the target thread's context information and add it to the thread timeout information;

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

[0095] Based on the above embodiments, the device further includes a thread reselection module, which is specifically configured as follows:

[0096] If the monitoring semaphore has not timed out or the target thread has performed a suspension operation, find a new target thread in the currently running application threads and reset the monitoring semaphore.

[0097] It is worth noting that in the embodiments of the above-mentioned device, the modules are divided according to functional logic, but are not limited to the above division, as long as the corresponding functions can be achieved; in addition, the specific names of each module are only for easy differentiation and are not used to limit the scope of protection of this application.

[0098] Figure 7 This is a schematic diagram of a PLC device provided in an embodiment of this application. The device is used to execute the thread timeout detection and processing method provided in the above embodiment, and has corresponding functional modules and beneficial effects for executing the 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; one processor 701 is shown as an example in the figure. The processor 701, memory 702, input device 703, and output device 704 can be connected via a bus or other means; a bus connection is shown as an example in the figure. 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 embodiments of this application. The processor 701 executes various corresponding functional applications and data processing by running the software programs, instructions, and modules stored in the memory 702, thereby realizing the above-mentioned thread timeout detection and processing method.

[0099] The memory 702 may primarily include a program storage area and a data storage area. The program storage area may store the operating system and at least one application program required for a given function; the data storage area may store data recorded or created during use. Furthermore, the memory 702 may include high-speed random access memory and may also include non-volatile memory, such as at least one disk storage device, flash memory device, or other non-volatile solid-state storage device. In some embodiments, the memory 702 may further include memory remotely configured relative to the processor 701, which can be connected to a terminal device via a network. Examples of such networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, 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 inputs related to the user settings and function control of the device; the output device 704 can be used to send or display key signal outputs related to the user settings and function control of the device.

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

[0102] Computer-readable storage media include both permanent and non-permanent, removable and non-removable media, and information storage can be achieved by any method or technology. 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 technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, disk storage or other magnetic storage devices, or any other non-transfer medium that can be used to store information accessible by a computing device.

[0103] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.

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

Claims

1. A method for detecting and handling thread timeouts, characterized in that, Applied to a PLC device, the PLC device is configured with a list object, which is used to record thread information of the application threads programmed into the PLC device. The thread timeout detection and handling method includes: If at least one of the application threads is running, update the thread information recorded in the list object; Based on the updated thread information in the list object, the thread with the shortest remaining duration is found in the application threads as the target thread, and a monitoring semaphore associated with the target thread is set. The waiting duration of the monitoring semaphore is the remaining duration of the target thread, so as to wait for the target thread to trigger the monitoring semaphore after it finishes running. The triggering operation is used to switch the monitoring semaphore to a value corresponding to different states. If the monitoring semaphore times out, the target thread is triggered to perform a suspension operation during the current instruction termination phase. The suspension operation is used to release the CPU resources occupied by the target thread. In response to the target thread performing a suspension operation, based on the CPU resources released from the target thread, thread timeout information is output to the host computer via a communication thread.

2. The thread timeout detection and processing method according to claim 1, characterized in that, The thread information includes the start time. Updating the thread information recorded in the list object when at least one of the application threads is running includes: In response to the execution of any application thread, the startup time of the currently running application thread is recorded, and the startup time of 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 time and maximum periodic runtime. Based on the updated thread information in the list object, the thread with the shortest remaining runtime is searched among the application threads as the target thread. A monitoring semaphore associated with the target thread is set, and the waiting time of the monitoring semaphore is the remaining runtime of the target thread, to wait for the target thread to trigger the monitoring semaphore after it finishes running. This includes: The remaining duration of the application thread is determined based on its start time, maximum periodic runtime, and current time. Based on the remaining time, the thread with the shortest remaining time among the application threads is selected as the target thread; The target thread waits for the remaining time to trigger the monitoring semaphore.

4. The thread timeout detection and processing method according to claim 3, characterized in that, The step of selecting the thread with the shortest remaining time from the application threads as the target thread based on the remaining time includes: If there are multiple application threads with the shortest remaining time, one application thread is selected as the target thread from among the multiple application threads; If the target thread terminates within the remaining time, then a new application thread is selected from the application threads that have not yet terminated as the new target thread.

5. The thread timeout detection and processing method according to any one of claims 1-4, characterized in that, The thread information includes a thread object. The step of triggering the target thread to perform a suspension operation during the current instruction termination phase when the monitoring semaphore timeout occurs includes: If no trigger operation is received before the remaining time expires, it is determined that the monitoring semaphore has timed out. In response to the timeout of the monitoring semaphore, a suspension signal is configured in the thread object corresponding to the target thread. The suspension signal is used to trigger the target thread to perform a suspension operation when the target thread enters the instruction termination stage after completing any instruction.

6. The thread timeout detection and processing method according to claim 1, characterized in that, In response to the target thread performing a suspension operation, based on the released CPU resources occupied by the target thread, thread timeout information is output to the host computer via a communication thread, including: If the target thread performs a suspension operation, obtain the context information of the target thread and add it to the thread timeout information; The system provides the communication thread with the thread timeout information and calls the CPU resources to start running the communication thread in order 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 includes: If the monitoring semaphore has not timed out or the target thread has performed a suspension operation, a new target thread is found 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 configured with a list object, which is used to record thread information of the application threads programmed into the PLC device. The thread timeout processing and detection device includes: The information update module is configured to update the thread information recorded in the list object when at least one of the application threads is running. The thread lookup module is configured to search for remaining threads in the application threads based on the updated thread information in the list object. The thread with the shortest remaining duration is selected as the target thread, and a monitoring semaphore associated with the target thread is set. The waiting time of the semaphore is the remaining time of the target thread, in order to wait for the target thread to trigger the monitoring semaphore after it finishes running. The triggering operation is used to switch the monitoring semaphore to a value corresponding to a different state. The timeout detection module is configured to trigger the target thread to perform a suspension operation during the current instruction termination phase when the monitored semaphore times out, and the suspension operation is used to release the CPU resources occupied by the target thread. The timeout alarm module is configured to, in response to the target thread performing a suspension operation, output thread timeout information to the host computer through a communication thread based on the released CPU resources occupied by the target thread.

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

10. A storage medium for storing computer-executable instructions, characterized in that, The computer-executable instructions, when executed by a processor, are used to perform the thread timeout detection and processing method as described in any one of claims 1-7.

Citation Information

Patent Citations

  • Multi-thread watch dog monitoring method

    CN101794244A

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

    CN116932248A