mirror of
https://github.com/EttusResearch/uhd.git
synced 2026-10-25 17:36:10 +00:00
tests: Ensure ctrlport responses come in expected order
Due to thread scheduling on the test OS, requests made in close succession could be responded to out of order, which does not correctly model the hardware. This change ensures that responses come in the same order they were requested.
This commit is contained in:
parent
3d0afa2c8d
commit
4e87ee8cdf
@ -93,6 +93,8 @@ public:
|
||||
std::mutex response_threads_mutex{};
|
||||
std::vector<std::thread> response_threads{};
|
||||
std::atomic<size_t> pending_responses{0};
|
||||
std::atomic<size_t> response_sequence{0};
|
||||
std::atomic<size_t> responses_delivered{0};
|
||||
|
||||
ctrlport_endpoint::send_fn_t send_fn;
|
||||
mock_clock_iface client_clk;
|
||||
@ -219,12 +221,18 @@ private:
|
||||
void schedule_async_response(const ctrl_payload& response, int64_t delay_ms = 1)
|
||||
{
|
||||
pending_responses++;
|
||||
// Ensure responses are delivered in order by waiting on the previous one
|
||||
size_t my_seq = response_sequence++;
|
||||
|
||||
std::lock_guard<std::mutex> lock(response_threads_mutex);
|
||||
response_threads.emplace_back([this, response, delay_ms]() {
|
||||
response_threads.emplace_back([this, response, delay_ms, my_seq]() {
|
||||
// Small delay to simulate hardware response time
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(delay_ms));
|
||||
while (responses_delivered.load() < my_seq) {
|
||||
std::this_thread::yield();
|
||||
}
|
||||
endpoint->handle_recv(response);
|
||||
responses_delivered++;
|
||||
pending_responses--;
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user