Implement interruptible wait in Modbus tasks #3261
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This update introduces an interruptible wait mechanism to the Modbus bridge's WaitTask and CycleTasksManager classes, allowing for more responsive task switching and reducing unnecessary delays in task execution. Specifically:
Added a Mutex-based interrupt mechanism in WaitTask to replace the fixed Thread.sleep() delay, enabling the task to be interrupted and proceed immediately with the next operation. This change allows for more dynamic task management and reduces latency in processing Modbus operations.
Modified CycleTasksManager to interrupt the current WaitDelayTask upon executing a write operation, ensuring that write tasks are executed immediately without waiting for the delay to complete. This enhancement addresses a specific issue where write tasks were delayed by up to the Cycle Time due to unexpired wait times, thereby improving the system's responsiveness and throughput.
Extended CycleTasksManagerTest to include a test case verifying the immediate execution of write tasks following an onExecuteWrite() call, demonstrating the elimination of the previously observed delay and validating the effectiveness of the interrupt mechanism.
Discussed here
=== TEST: Write task executes immediately after onExecuteWrite ===
Starting cycle with onBeforeProcessImage()
Got WaitDelayTask with delay: 0ms
Delay task thread started, beginning wait...
Delay task completed normally
Calling onExecuteWrite() to interrupt the delay
Got write task immediately: DummyWriteTask [name=WT_1, delay=90]
Test passed: Write task was available immediately without waiting for delay!
=== END TEST ===
The test confirms that: