Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/plugins/ucx/ucx_thread_pool_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,9 @@ class nixlUcxDedicatedThread final : public nixlUcxThread {
task = queue_.tryPop();
} else {
NIXL_TRACE << "dedicated " << *this << " waiting for requests";
task = queue_.pop(token);
// We need to progress idle worker to terminate closed connections
getWorkers().front()->progressLoop();
Comment thread
iyastreb marked this conversation as resolved.
Resolved
Comment thread
iyastreb marked this conversation as resolved.
Resolved
task = queue_.pop(token, std::chrono::seconds(1));
}

if (task != nullptr) {
Expand Down
13 changes: 13 additions & 0 deletions src/utils/common/blocking_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef NIXL_SRC_UTILS_COMMON_BLOCKING_QUEUE_H
#define NIXL_SRC_UTILS_COMMON_BLOCKING_QUEUE_H

#include <chrono>
#include <condition_variable>
#include <mutex>
#include <stop_token>
Expand Down Expand Up @@ -87,6 +88,18 @@ template<typename T> class blockingQueue {
return popLocked();
}

/**
* @brief Pop the oldest item, waiting until a push, timeout, or stop request
* @return The item, or nullptr if the queue is empty after waiting
*/
template<typename repType, typename periodType>
[[nodiscard]] T *
pop(std::stop_token token, const std::chrono::duration<repType, periodType> &timeout) {
std::unique_lock<std::mutex> lock(mutex_);
cv_.wait_for(lock, std::move(token), timeout, [this] { return head_ != nullptr; });
return popLocked();
}

private:
[[nodiscard]] T *
popLocked() noexcept {
Expand Down
38 changes: 33 additions & 5 deletions test/gtest/blocking_queue_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ namespace nixl {
TEST(blockingQueueTest, FIFO) {
blockingQueue<item> queue;
EXPECT_EQ(queue.tryPop(), nullptr);
const auto timeout = std::chrono::milliseconds(1);
const auto start = std::chrono::steady_clock::now();
EXPECT_EQ(queue.pop({}, timeout), nullptr);
EXPECT_GE(std::chrono::steady_clock::now() - start, timeout);

std::array<item, 5> items;
for (auto &item : items) {
Expand Down Expand Up @@ -72,20 +76,44 @@ TEST(blockingQueueTest, Stop) {
EXPECT_EQ(queue.pop(stopped.get_token()), nullptr);
queue.push(&single);
EXPECT_EQ(queue.pop(stopped.get_token()), &single);
queue.push(&single);
EXPECT_EQ(queue.pop(stopped.get_token(), std::chrono::seconds(5)), &single);
EXPECT_EQ(queue.pop(stopped.get_token()), nullptr);

// A stop request wakes a blocked pop. The consumer announces that it is
// about to block; a stop landing just before the wait returns null as well,
// and a missed wake-up hangs the join below.
// The consumer announces that it is about to block; a stop arriving before
// the wait must also be observed.
const auto timeout = std::chrono::seconds(5);
for (bool timed : {false, true}) {
std::atomic<bool> popping{false};
std::jthread consumer([&](std::stop_token token) {
const auto start = std::chrono::steady_clock::now();
popping.store(true);
EXPECT_EQ(timed ? queue.pop(token, timeout) : queue.pop(token), nullptr);
EXPECT_LT(std::chrono::steady_clock::now() - start, timeout);
});
while (!popping.load()) {
std::this_thread::yield();
}
consumer.request_stop();
}
}

TEST(blockingQueueTest, TimedPush) {
blockingQueue<item> queue;
item single;
const auto timeout = std::chrono::seconds(5);
std::atomic<bool> popping{false};
std::jthread consumer([&](std::stop_token token) {
const auto start = std::chrono::steady_clock::now();
popping.store(true);
EXPECT_EQ(queue.pop(token), nullptr);
EXPECT_EQ(queue.pop(token, timeout), &single);
EXPECT_LT(std::chrono::steady_clock::now() - start, timeout);
});
while (!popping.load()) {
std::this_thread::yield();
}
consumer.request_stop();
queue.push(&single);
consumer.join();
}

TEST(blockingQueueTest, MPMC) {
Expand Down
Loading