Skip to content

Feature/general threading improvements#18

Open
khauersp wants to merge 11 commits into
masterfrom
feature/general-threading-improvements
Open

Feature/general threading improvements#18
khauersp wants to merge 11 commits into
masterfrom
feature/general-threading-improvements

Conversation

@khauersp

@khauersp khauersp commented Jun 4, 2026

Copy link
Copy Markdown
Owner

This pull request introduces significant improvements to the threading, timer management, and lifecycle handling in the J1939 ECU implementation. The main changes include splitting protocol and timer handling into separate threads for better isolation, introducing thread-safe mechanisms for timer events and subscriber management, and adding a robust dependent lifecycle registry to ensure proper shutdown of helper objects. Additionally, all timeouts and deadlines now use time.monotonic() for improved reliability, and critical shared resources are protected with appropriate locks.

Threading and Timer Management Improvements:

  • Split the original job thread into two dedicated threads: a protocol thread for TP/BAM timeout management and a timer thread for application cyclic callbacks, ensuring that slow user callbacks do not interfere with protocol deadlines. Timer events are now managed using a heap-based priority queue (heapq) for efficient scheduling. [1] [2] [3]
  • All timeouts, deadlines, and timer events now use time.monotonic() instead of time.time() to avoid issues with system clock changes. [1] [2] [3] [4] [5]

Thread Safety and Synchronization:

  • Introduced thread locks (threading.RLock and threading.Lock) to protect access to timer events, subscribers, and protocol buffers, ensuring safe concurrent access from multiple threads. [1] [2] [3] [4] [5] [6] [7]

Lifecycle and Shutdown Handling:

  • Added a dependent lifecycle registry to the ECU, allowing helper objects to register for shutdown callbacks. Registered dependents are stopped in LIFO order before the ECU's own threads are shut down, with robust error handling during shutdown. [1] [2]
  • Added register_dependent and unregister_dependent methods to both the ECU and ControllerApplication classes for managing dependent helpers. [1] [2]

General Codebase Improvements:

  • Improved documentation and comments throughout the codebase to clarify thread responsibilities, locking, and shutdown procedures. [1] [2]

These changes collectively enhance the robustness, correctness, and maintainability of the J1939 ECU implementation, especially in multi-threaded and complex application scenarios.

@khauersp khauersp closed this Jun 10, 2026
@khauersp khauersp reopened this Jun 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant