issue: 4432879 Refactor event_handler_manager
Description
Avoid start_thread with each reg action. Refactor destructor.
What
Avoid start_thread with each reg action. Refactor destructor.
Why ?
Potential CPU improvement for CPS and code cleanup.
Change type
What kind of change does this PR introduce?
- [ ] Bugfix
- [ ] Feature
- [ ] Code style update
- [x] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] CI related changes
- [ ] Documentation content changes
- [ ] Tests
- [ ] Other
Check list
- [ ] Code follows the style de facto guidelines of this project
- [ ] Comments have been inserted in hard to understand places
- [ ] Documentation has been updated (if necessary)
- [ ] Test has been added (if possible)
@pasis, can we keep this in your privare fork?
bot:retest
bot:retest
bot:retest
/review
/improve
PR Reviewer Guide 🔍
Here are some key observations to aid the review process:
| ⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪ |
| 🧪 No relevant tests |
| 🔒 No security concerns identified |
⚡ Recommended focus areas for reviewResource Cleanup
m_epfd resource is properly closed and reset in the destructor to avoid resource leaks or undefined behavior. |
PR Code Suggestions ✨
| Category | Suggestion | Impact |
| Possible issue |
Add error handling for thread initializationAdd error handling for the src/core/event/event_handler_manager.cpp [347-348]
Suggestion importance[1-10]: 8__ Why: The suggestion improves robustness by adding error handling for | Medium |
Add null check for pointer accessAdd a null check for src/core/event/event_handler_manager.cpp [446]
Suggestion importance[1-10]: 8__ Why: Adding a null check for | Medium | |
Prevent resource leaks in thread attributesEnsure that the src/core/event/event_handler_manager.cpp [341-349]
Suggestion importance[1-10]: 7__ Why: The suggestion ensures that | Medium | |
| General |
Log error code before exceptionLog the error code before throwing the exception to provide more context for
Suggestion importance[1-10]: 6__ Why: Logging the error code before throwing an exception provides valuable debugging information. While not critical, it enhances the ability to diagnose issues when the thread fails to start. | Low |
Hi @pasis , @AlexanderGrissik , please check pr agent review comments.
bot:retest
Greptile Overview
Greptile Summary
Refactors event handler manager to start the internal thread once during global initialization instead of on each registration action, improving CPS performance.
Key Changes:
- Moved
start_thread()call frompost_new_reg_action()to global initialization inmain.cpp:1084 - Simplified destructor by removing
free_evh_resources()and moving cleanup logic directly into destructor - Improved error handling in
start_thread()with proper return values and cleanup - Moved
m_b_continue_runninginitialization outside internal thread mode check for consistency
Issues:
- None identified; the refactoring is clean and improves the design
Confidence Score: 4/5
- This PR is safe to merge with minimal risk - it's a clean refactoring that improves performance
- Score reflects well-structured refactoring with proper error handling. The change moves thread initialization from lazy (on first registration) to eager (during global init), which is cleaner and improves CPS performance. Error handling is improved with explicit return codes. The only minor concern is the errno usage pattern, but this is consistent with the existing codebase
- No files require special attention - all changes are straightforward refactoring
Important Files Changed
File Analysis
| Filename | Score | Overview |
|---|---|---|
| src/core/event/event_handler_manager.cpp | 4/5 | Refactored thread initialization to occur during global construction instead of on each registration action; improved error handling and removed free_evh_resources() method |
| src/core/event/event_handler_manager.h | 5/5 | Moved start_thread() to public interface and removed free_evh_resources() declaration |
| src/core/main.cpp | 4/5 | Added explicit event handler thread startup during global initialization with error checking |
Sequence Diagram
sequenceDiagram
participant Main as main.cpp
participant EHM as event_handler_manager
participant Thread as event_handler_thread
Note over Main: Global Initialization
Main->>EHM: NEW_CTOR(g_p_event_handler_manager)
activate EHM
EHM->>EHM: Constructor (internal_thread_mode=true)
EHM->>EHM: m_epfd = epoll_create()
EHM->>EHM: m_b_continue_running = true
EHM-->>Main: event_handler_manager instance
deactivate EHM
Main->>EHM: start_thread()
activate EHM
EHM->>EHM: pthread_attr_init(&tattr)
alt affinity configured
EHM->>EHM: pthread_attr_setaffinity_np()
end
EHM->>Thread: pthread_create()
activate Thread
Thread->>Thread: thread_loop()
EHM-->>Main: 0 (success)
deactivate EHM
Note over Main: Registration Actions
Main->>EHM: register_timer_event() / register_ibverbs_event()
activate EHM
EHM->>EHM: post_new_reg_action()
Note over EHM: Thread already running<br/>No start_thread() call
EHM->>Thread: do_wakeup()
EHM-->>Main:
deactivate EHM
Thread->>Thread: process registration actions
Note over Main: Shutdown
Main->>EHM: ~event_handler_manager()
activate EHM
EHM->>EHM: stop_thread()
EHM->>Thread: m_b_continue_running = false
EHM->>Thread: do_wakeup()
EHM->>Thread: pthread_join()
Thread-->>EHM: thread exits
deactivate Thread
EHM->>EHM: close(m_epfd)
deactivate EHM