You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add tests for multiple runs and cross-thread scheduling
Diagram Walkthrough
flowchart LR
A["Runtime::run()"] --> B["Check run_thread_id_"]
B -- "First call" --> C["Enable ring & flush queue"]
B -- "Same thread" --> D["Flush ring & loop"]
B -- "Different thread" --> E["Throw runtime_error"]
C --> D
D --> F["Reset tick_count_ & exit_allowed_"]
Loading
File Walkthrough
Relevant files
Enhancement
runtime.hpp
Refactor runtime state for multiple run calls
include/condy/runtime.hpp
Replaced state machine with atomic bool ring_enabled_ and thread ID tracking
Added thread binding check in run() to allow multiple calls from same thread
Reset tick_count_ and exit_allowed_ after run() completes
Removed State enum and related atomic state variable
The run() function checks run_thread_id_ without synchronization. If two threads call run() concurrently, both could see run_thread_id_ as default and proceed to enable the ring, leading to undefined behavior. The documentation states this is not allowed, but the code does not enforce it with a mutex or atomic check, making it a potential race condition.
if (run_thread_id_ == std::thread::id()) {
int r = io_uring_enable_rings(ring_.ring());
if (r < 0) {
throwdetail::make_system_error("io_uring_enable_rings", -r);
}
run_thread_id_ = std::this_thread::get_id();
{
std::lock_guard<std::mutex> lock(mutex_);
flush_global_queue_();
ring_enabled_.store(true, std::memory_order_relaxed);
}
if (!disable_register_ring_fd_) {
io_uring_register_ring_fd(ring_.ring());
}
} elseif (run_thread_id_ == std::this_thread::get_id()) {
flush_ring_();
} else {
throwstd::runtime_error(
"Runtime::run() can only be called from the same thread");
}
In the first-run path of run(), io_uring_enable_rings is called without checking if the ring is already enabled. If run() is called multiple times from the same thread, the second call skips the first-run block but does not re-enable the ring. However, if the ring was disabled between runs (not shown in diff), calling io_uring_enable_rings again could fail or cause undefined behavior. The code assumes the ring stays enabled, but the reset logic does not disable it.
int r = io_uring_enable_rings(ring_.ring());
if (r < 0) {
throwdetail::make_system_error("io_uring_enable_rings", -r);
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
PR Type
Enhancement, Tests
Description
Support multiple Runtime::run() calls
Add thread binding and state reset
Add tests for multiple runs and cross-thread scheduling
Diagram Walkthrough
File Walkthrough
runtime.hpp
Refactor runtime state for multiple run callsinclude/condy/runtime.hpp
tracking
thread
test_runtime.cpp
Add tests for multiple run scenariostests/test_runtime.cpp
test_sync_wait.cpp
Add sync_wait multiple run testtests/test_sync_wait.cpp