diff --git a/smocore/deviceManager/deviceManager.cpp b/smocore/deviceManager/deviceManager.cpp index fa4967a..4bf8e4b 100644 --- a/smocore/deviceManager/deviceManager.cpp +++ b/smocore/deviceManager/deviceManager.cpp @@ -170,26 +170,48 @@ public: }; class DeviceManager::RemoveDeviceAttachmentSpecReq -: public PostedAsynchronousContinuation +: public SerializedAsynchronousContinuation { public: RemoveDeviceAttachmentSpecReq( - const std::shared_ptr &s, + const DeviceAttachmentSpec &spec, const std::shared_ptr &caller, - Callback cb) - : PostedAsynchronousContinuation( - caller, cb), - spec(s) + Callback cb, + std::vector> requiredLocks) + : SerializedAsynchronousContinuation( + caller, cb, requiredLocks), + spec(spec) {} +public: + DeviceAttachmentSpec spec; + std::shared_ptr specPtr; + public: void removeDeviceAttachmentSpecReq1_posted( [[maybe_unused]] std::shared_ptr context ) { + // Find the shared_ptr to the spec in the collection + for (const auto& existingSpec : DeviceManager::deviceAttachmentSpecs) + { + if (*existingSpec == spec) + { + specPtr = existingSpec; + break; + } + } + + if (!specPtr) + { + // Spec not found, callback with failure and return + callOriginalCb(false, nullptr); + return; + } + // Call detachSenseDeviceReq first - only clean up metadata if this succeeds DeviceManager::getInstance().detachSenseDeviceReq( - spec, + specPtr, {context, std::bind( &RemoveDeviceAttachmentSpecReq::removeDeviceAttachmentSpecReq2, context.get(), context, @@ -213,14 +235,14 @@ public: try { // Find the DeviceRole in attachedDeviceRoles auto deviceRoleIt = std::find_if( - attachedDeviceRoles.begin(), - attachedDeviceRoles.end(), - [&spec = spec](const std::shared_ptr &role) { - return *role->deviceAttachmentSpec == *spec; + DeviceManager::attachedDeviceRoles.begin(), + DeviceManager::attachedDeviceRoles.end(), + [&specPtr = specPtr](const std::shared_ptr &role) { + return *role->deviceAttachmentSpec == *specPtr; } ); - if (deviceRoleIt == attachedDeviceRoles.end()) + if (deviceRoleIt == DeviceManager::attachedDeviceRoles.end()) { // DeviceRole not found, callback with failure callOriginalCb(false, deviceSpec); @@ -231,7 +253,7 @@ public: auto& device = deviceRole->parentDevice; // Remove DeviceRole from DeviceManager's collection - attachedDeviceRoles.erase(deviceRoleIt); + DeviceManager::attachedDeviceRoles.erase(deviceRoleIt); // Remove DeviceRole from Device's collection auto deviceRoleIt2 = std::find( @@ -245,14 +267,18 @@ public: // Remove DeviceAttachmentSpec from deviceAttachmentSpecs collection auto specIt = std::find_if( - deviceAttachmentSpecs.begin(), - deviceAttachmentSpecs.end(), - [&spec = spec](const std::shared_ptr &existingSpec) { - return *existingSpec == *spec; - }); - if (specIt != deviceAttachmentSpecs.end()) + DeviceManager::deviceAttachmentSpecs.begin(), + DeviceManager::deviceAttachmentSpecs.end(), + [&specPtr = specPtr]( + const std::shared_ptr &existingSpec) + { + return *existingSpec == *specPtr; + } + ); + + if (specIt != DeviceManager::deviceAttachmentSpecs.end()) { - deviceAttachmentSpecs.erase(specIt); + DeviceManager::deviceAttachmentSpecs.erase(specIt); } // Callback with success @@ -262,9 +288,6 @@ public: callOriginalCb(false, deviceSpec); } } - -public: - std::shared_ptr spec; }; void DeviceManager::newDeviceAttachmentSpecInd( @@ -290,33 +313,19 @@ void DeviceManager::removeDeviceAttachmentSpecReq( const DeviceAttachmentSpec &spec, Callback callback) { - // Find the shared_ptr to the spec in the collection - std::shared_ptr specPtr = nullptr; - for (const auto& existingSpec : deviceAttachmentSpecs) - { - if (*existingSpec == spec) - { - specPtr = existingSpec; - break; - } - } - - if (!specPtr) - { - // Spec not found, callback with failure - callback.callbackFn(false, nullptr); - return; - } - - // Create async continuation const auto& caller = ComponentThread::getSelf(); - auto continuation = std::make_shared( - specPtr, caller, callback); - mrntt::mrntt.thread->getIoService().post( + auto request = std::make_shared( + spec, caller, callback, + LockSet::Set{ + std::ref(DeviceManager::getInstance().qutex) + }); + + RemoveDeviceAttachmentSpecReq::LockerAndInvoker lockvoker( + *request, mrntt::mrntt.thread, std::bind( &RemoveDeviceAttachmentSpecReq::removeDeviceAttachmentSpecReq1_posted, - continuation.get(), continuation)); + request.get(), request)); } class DeviceManager::AttachSenseDeviceReq