From 8882934f4dd994239085d009605d9852d195bf1d Mon Sep 17 00:00:00 2001 From: Eran Date: Wed, 11 Oct 2023 08:58:55 +0300 Subject: [PATCH] try to remove deadlock --- src/device.cpp | 45 +++++++++++++++++++++------------------------ src/device.h | 8 +++----- 2 files changed, 24 insertions(+), 29 deletions(-) diff --git a/src/device.cpp b/src/device.cpp index 6cc93aea004..d43cdfce45f 100644 --- a/src/device.cpp +++ b/src/device.cpp @@ -169,34 +169,34 @@ matcher_factory::create_timestamp_composite_matcher( std::vector< std::shared_pt device::device(std::shared_ptr ctx, const platform::backend_device_group group, bool device_changed_notifications) - : _context(ctx), _group(group), _is_valid(true), - _device_changed_notifications(device_changed_notifications), - _is_alive( std::make_shared< bool >( true ) ) + : _context( ctx ) + , _group( group ) + , _device_changed_notifications( device_changed_notifications ) + , _is_alive( std::make_shared< std::atomic< bool > >( true ) ) { _profiles_tags = lazy>([this]() { return get_profiles_tags(); }); if (_device_changed_notifications) { - std::weak_ptr< bool > weak = _is_alive; - auto cb = new devices_changed_callback_internal([this, weak](rs2_device_list* removed, rs2_device_list* added) - { - // The callback can be called from one thread while the object is being destroyed by another. - // Check if members can still be accessed. - auto alive = weak.lock(); - if( ! alive || ! *alive ) - return; - - // Update is_valid variable when device is invalid - std::lock_guard lock(_device_changed_mtx); - for (auto& dev_info : removed->list) + std::weak_ptr< std::atomic< bool > > weak_alive = _is_alive; + auto cb = new devices_changed_callback_internal( + [weak_alive, group]( rs2_device_list * removed, rs2_device_list * added ) { - if (dev_info.info->get_device_data() == _group) - { - _is_valid = false; + // The callback can be called from one thread while the object is being destroyed by another. + // Check if members can still be accessed. + auto alive = weak_alive.lock(); + if( ! alive || ! *alive ) return; + + for( auto & dev_info : removed->list ) + { + if( dev_info.info->get_device_data() == group ) + { + *alive = false; + return; + } } - } - }); + } ); _callback_id = _context->register_internal_device_callback({ cb, [](rs2_devices_changed_callback* p) { p->release(); } }); } @@ -204,12 +204,9 @@ device::device(std::shared_ptr ctx, device::~device() { - *_is_alive = false; - if (_device_changed_notifications) - { _context->unregister_internal_device_callback(_callback_id); - } + _sensors.clear(); } diff --git a/src/device.h b/src/device.h index 3f7096bc387..eab39ec333a 100644 --- a/src/device.h +++ b/src/device.h @@ -73,8 +73,7 @@ class device : public virtual device_interface, public info_container bool is_valid() const override { - std::lock_guard lock(_device_changed_mtx); - return _is_valid; + return *_is_alive; } void tag_profiles(stream_profiles profiles) const override; @@ -104,12 +103,11 @@ class device : public virtual device_interface, public info_container std::vector> _sensors; std::shared_ptr _context; const platform::backend_device_group _group; - bool _is_valid, _device_changed_notifications; - mutable std::mutex _device_changed_mtx; + bool _device_changed_notifications; uint64_t _callback_id; lazy> _profiles_tags; - std::shared_ptr< bool > _is_alive; // Ensures object can be accessed + std::shared_ptr< std::atomic< bool > > _is_alive; }; // Helper function that should be used when multiple FW calls needs to be made.