From 06f3f2eebe21ce9129b2b5c0ed784399e5599e39 Mon Sep 17 00:00:00 2001 From: Hayodea Hekol Date: Tue, 30 Sep 2025 21:24:51 -0400 Subject: [PATCH] DevMgr:attachAllUnattachedDevicesFromKnownListReq: acquire qutex We should acquire the qutex here before iterating through the list of DA specs. --- smocore/deviceManager/deviceManager.cpp | 107 +++++++++++++----- smocore/include/deviceManager/deviceManager.h | 1 + 2 files changed, 82 insertions(+), 26 deletions(-) diff --git a/smocore/deviceManager/deviceManager.cpp b/smocore/deviceManager/deviceManager.cpp index 4ab4d36..4f0b223 100644 --- a/smocore/deviceManager/deviceManager.cpp +++ b/smocore/deviceManager/deviceManager.cpp @@ -623,37 +623,92 @@ void DeviceManager::attachAllUnattachedDevicesFromCmdlineReq( attachAllUnattachedDevicesFromReq(specs, std::move(cb)); } +class DeviceManager::AttachAllUnattachedDevicesFromKnownListReq +: public SerializedAsynchronousContinuation< + attachAllUnattachedDevicesFromReqCbFn> +{ +public: + AttachAllUnattachedDevicesFromKnownListReq( + const std::shared_ptr &caller, + Callback cb, + std::vector> requiredLocks) + : SerializedAsynchronousContinuation< + attachAllUnattachedDevicesFromReqCbFn>( + caller, cb, requiredLocks) + {} + +public: + void attachAllUnattachedDevicesFromKnownListReq1_posted( + [[maybe_unused]] + std::shared_ptr context + ) + { + // Create a vector to hold unattached device specs + auto unattachedSpecs = std::make_shared< + std::vector>(); + + // Cycle through all DA specs in deviceAttachmentSpecs + for (const auto& spec : DeviceManager::deviceAttachmentSpecs) + { + bool isAttached = false; + + // Cross reference with attachedDeviceRoles + for (const auto& role : DeviceManager::attachedDeviceRoles) + { + if (*role->deviceAttachmentSpec == *spec) + { + isAttached = true; + break; + } + } + + // If spec doesn't appear in attachedDeviceRoles, add it to vector + if (!isAttached) { + unattachedSpecs->push_back(*spec); + } + } + + // Release the DeviceManager qutex early before calling the inner method + DeviceManager::getInstance().qutex.release(); + + // Pass the vector to the existing function + DeviceManager::getInstance().attachAllUnattachedDevicesFromReq( + unattachedSpecs, + {context, std::bind( + &AttachAllUnattachedDevicesFromKnownListReq + ::attachAllUnattachedDevicesFromKnownListReq2, + context.get(), context, + std::placeholders::_1)}); + } + + void attachAllUnattachedDevicesFromKnownListReq2( + [[maybe_unused]] + std::shared_ptr context, + AsynchronousLoop loop + ) + { + callOriginalCb(loop); + } +}; + void DeviceManager::attachAllUnattachedDevicesFromKnownListReq( Callback cb ) { - // Create a vector to hold unattached device specs - auto unattachedSpecs = std::make_shared< - std::vector>(); + const auto& caller = ComponentThread::getSelf(); + + auto request = std::make_shared( + caller, cb, + LockSet::Set{ + std::ref(DeviceManager::getInstance().qutex) + }); - // Cycle through all DA specs in deviceAttachmentSpecs - for (const auto& spec : deviceAttachmentSpecs) - { - bool isAttached = false; - - // Cross reference with attachedDeviceRoles - for (const auto& role : attachedDeviceRoles) - { - if (*role->deviceAttachmentSpec == *spec) - { - isAttached = true; - break; - } - } - - // If spec doesn't appear in attachedDeviceRoles, add it to the vector - if (!isAttached) { - unattachedSpecs->push_back(*spec); - } - } - - // Pass the vector to the existing function - attachAllUnattachedDevicesFromReq(unattachedSpecs, std::move(cb)); + AttachAllUnattachedDevicesFromKnownListReq::LockerAndInvoker lockvoker( + *request, mrntt::mrntt.thread, + std::bind( + &AttachAllUnattachedDevicesFromKnownListReq + ::attachAllUnattachedDevicesFromKnownListReq1_posted, + request.get(), request)); } class DeviceManager::DetachAllAttachedDeviceRoles diff --git a/smocore/include/deviceManager/deviceManager.h b/smocore/include/deviceManager/deviceManager.h index 3aa95ec..e2490e4 100644 --- a/smocore/include/deviceManager/deviceManager.h +++ b/smocore/include/deviceManager/deviceManager.h @@ -111,6 +111,7 @@ private: class AttachSenseDeviceReq; typedef AttachSenseDeviceReq DetachSenseDeviceReq; class AttachAllUnattachedDevicesFromReq; + class AttachAllUnattachedDevicesFromKnownListReq; class DetachAllAttachedDeviceRoles; };