Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent duplicate xlink stream names #811

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 22 additions & 18 deletions src/device/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,10 @@ bool Device::startPipelineImpl(const Pipeline& pipeline) {
}

// Create DataInputQueue's
auto streamName = xlinkIn->getStreamName();
if(inputQueueMap.count(streamName) != 0) throw std::invalid_argument(fmt::format("Streams have duplicate name '{}'", streamName));
// set max data size, for more verbosity
inputQueueMap[xlinkIn->getStreamName()] = std::make_shared<DataInputQueue>(connection, xlinkIn->getStreamName(), 16, true, xlinkIn->getMaxDataSize());
inputQueueMap[std::move(streamName)] = std::make_shared<DataInputQueue>(connection, xlinkIn->getStreamName(), 16, true, xlinkIn->getMaxDataSize());
}
for(const auto& kv : pipeline.getNodeMap()) {
const auto& node = kv.second;
Expand All @@ -275,29 +277,31 @@ bool Device::startPipelineImpl(const Pipeline& pipeline) {
continue;
}

auto streamName = xlinkOut->getStreamName();
// Create DataOutputQueue's
auto streamName = xlinkOut->getStreamName();
if(outputQueueMap.count(streamName) != 0) throw std::invalid_argument(fmt::format("Streams have duplicate name '{}'", streamName));
outputQueueMap[streamName] = std::make_shared<DataOutputQueue>(connection, streamName);

// Add callback for events
callbackIdMap[streamName] = outputQueueMap[streamName]->addCallback([this](std::string queueName, std::shared_ptr<ADatatype>) {
{
// Lock first
std::unique_lock<std::mutex> lock(eventMtx);

// Check if size is equal or greater than EVENT_QUEUE_MAXIMUM_SIZE
if(eventQueue.size() >= EVENT_QUEUE_MAXIMUM_SIZE) {
auto numToRemove = eventQueue.size() - EVENT_QUEUE_MAXIMUM_SIZE + 1;
eventQueue.erase(eventQueue.begin(), eventQueue.begin() + numToRemove);
}
callbackIdMap[std::move(streamName)] =
outputQueueMap[xlinkOut->getStreamName()]->addCallback([this](std::string queueName, std::shared_ptr<ADatatype>) {
{
// Lock first
std::unique_lock<std::mutex> lock(eventMtx);

// Check if size is equal or greater than EVENT_QUEUE_MAXIMUM_SIZE
if(eventQueue.size() >= EVENT_QUEUE_MAXIMUM_SIZE) {
auto numToRemove = eventQueue.size() - EVENT_QUEUE_MAXIMUM_SIZE + 1;
eventQueue.erase(eventQueue.begin(), eventQueue.begin() + numToRemove);
}

// Add to the end of event queue
eventQueue.push_back(std::move(queueName));
}
// Add to the end of event queue
eventQueue.push_back(std::move(queueName));
}

// notify the rest
eventCv.notify_all();
});
// notify the rest
eventCv.notify_all();
});
}
return DeviceBase::startPipelineImpl(pipeline);
}
Expand Down
24 changes: 24 additions & 0 deletions tests/src/pipeline_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,27 @@ TEST_CASE("Cross pipeline link with Input and Output") {
// Then check that actually linking throws
REQUIRE_THROWS(xin->out.link(xout->input));
}

TEST_CASE("Duplicate xlink stream names") {
dai::Pipeline p;
auto sysInfo1 = p.create<dai::node::SystemLogger>();
auto sysInfo2 = p.create<dai::node::SystemLogger>();
auto xout1 = p.create<dai::node::XLinkOut>();
auto xout2 = p.create<dai::node::XLinkOut>();
sysInfo1->out.link(xout1->input);
sysInfo2->out.link(xout2->input);
xout1->setStreamName("test1");
xout2->setStreamName("test1");
REQUIRE_THROWS_AS(dai::Device{p}, std::invalid_argument);

p = {};
auto script1 = p.create<dai::node::Script>();
auto script2 = p.create<dai::node::Script>();
auto xin1 = p.create<dai::node::XLinkIn>();
auto xin2 = p.create<dai::node::XLinkIn>();
xin1->out.link(script1->inputs["in"]);
xin2->out.link(script2->inputs["in"]);
xin1->setStreamName("test2");
xin1->setStreamName("test2");
REQUIRE_THROWS_AS(dai::Device{p}, std::invalid_argument);
}