Skip to content

Commit

Permalink
use is_alive() for device::is_valid(); remove ctor arg
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/device.cpp
#	src/device.h
#	src/ds/d400/d400-factory.cpp
#	src/ds/d500/d500-factory.cpp
  • Loading branch information
maloel committed Oct 1, 2023
1 parent b32d97a commit 6072ad8
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 139 deletions.
40 changes: 1 addition & 39 deletions src/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,52 +31,14 @@ librealsense::find_profile( rs2_stream stream, int index, std::vector< stream_in
}


device::device( std::shared_ptr< const device_info > const & dev_info,
bool device_changed_notifications )
device::device( std::shared_ptr< const device_info > const & dev_info )
: _dev_info( dev_info )
, _is_valid( true )
, _is_alive( std::make_shared< bool >( true ) )
, _profiles_tags( [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<std::mutex> lock(_device_changed_mtx);
for (auto& dev_info : removed->list)
{
if( dev_info.info->is_same_as( _dev_info ) )
{
_is_valid = false;
return;
}
}
});

_device_changed_callback_id
= get_context()->register_internal_device_callback( { cb,
[]( rs2_devices_changed_callback * p )
{
p->release();
} } );
}
}

device::~device()
{
*_is_alive = false;

if( _device_changed_callback_id )
get_context()->unregister_internal_device_callback( _device_changed_callback_id );

_sensors.clear();
}

Expand Down
11 changes: 2 additions & 9 deletions src/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ class device

bool is_valid() const override
{
std::lock_guard<std::mutex> lock(_device_changed_mtx);
return _is_valid;
return _dev_info->is_alive();
}

void tag_profiles(stream_profiles profiles) const override;
Expand All @@ -66,8 +65,6 @@ class device

virtual void stop_activity() const;

bool device_changed_notifications_on() const { return _device_changed_callback_id; }

format_conversion get_format_conversion() const;

protected:
Expand All @@ -76,19 +73,15 @@ class device
void register_stream_to_extrinsic_group(const stream_interface& stream, uint32_t groupd_index);
std::vector<rs2_format> map_supported_color_formats(rs2_format source_format);

explicit device( std::shared_ptr< const device_info > const &, bool device_changed_notifications = true );
explicit device( std::shared_ptr< const device_info > const & );

std::map<int, std::pair<uint32_t, std::shared_ptr<const stream_interface>>> _extrinsics;

private:
std::vector<std::shared_ptr<sensor_interface>> _sensors;
std::shared_ptr< const device_info > _dev_info;
bool _is_valid;
mutable std::mutex _device_changed_mtx;
uint64_t _device_changed_callback_id = 0;
rsutils::lazy< std::vector< tagged_profile > > _profiles_tags;

std::shared_ptr< bool > _is_alive; // Ensures object can be accessed
};


Expand Down
Loading

0 comments on commit 6072ad8

Please sign in to comment.