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

Avoid device_watcher deadlock after reset #11933

Closed
Closed
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
19 changes: 15 additions & 4 deletions src/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,12 @@ namespace librealsense

std::map<uint64_t, devices_changed_callback_ptr> devices_changed_callbacks;
{
std::lock_guard<std::mutex> lock(_devices_changed_callbacks_mtx);
std::unique_lock<std::timed_mutex> lk(_devices_changed_callbacks_mtx, std::defer_lock);
if (!lk.try_lock_for(std::chrono::milliseconds(500)))
{
LOG_ERROR("Couldn't lock the _devices_changed_callbacks_mtx mutex. Exiting the callback");
return;
}
devices_changed_callbacks = _devices_changed_callbacks;
}

Expand Down Expand Up @@ -414,7 +419,9 @@ namespace librealsense

uint64_t context::register_internal_device_callback(devices_changed_callback_ptr callback)
{
std::lock_guard<std::mutex> lock(_devices_changed_callbacks_mtx);
std::unique_lock<std::timed_mutex> lk(_devices_changed_callbacks_mtx, std::defer_lock);
lk.lock();

auto callback_id = unique_id::generate_id();
_devices_changed_callbacks.insert(std::make_pair(callback_id, std::move(callback)));

Expand All @@ -428,7 +435,9 @@ namespace librealsense

void context::unregister_internal_device_callback(uint64_t cb_id)
{
std::lock_guard<std::mutex> lock(_devices_changed_callbacks_mtx);
std::unique_lock<std::timed_mutex> lk(_devices_changed_callbacks_mtx, std::defer_lock);
lk.lock();

_devices_changed_callbacks.erase(cb_id);

if (_devices_changed_callback == nullptr && _devices_changed_callbacks.size() == 0) // There are no register callbacks any more _device_watcher can be stopped
Expand All @@ -439,7 +448,9 @@ namespace librealsense

void context::set_devices_changed_callback(devices_changed_callback_ptr callback)
{
std::lock_guard<std::mutex> lock(_devices_changed_callbacks_mtx);
std::unique_lock<std::timed_mutex> lk(_devices_changed_callbacks_mtx, std::defer_lock);
lk.lock();

_devices_changed_callback = std::move(callback);

if (_device_watcher->is_stopped())
Expand Down
3 changes: 2 additions & 1 deletion src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ namespace librealsense
devices_changed_callback_ptr _devices_changed_callback;
std::map<int, std::weak_ptr<const stream_interface>> _streams;
std::map<int, std::map<int, std::weak_ptr<lazy<rs2_extrinsics>>>> _extrinsics;
std::mutex _streams_mutex, _devices_changed_callbacks_mtx;
std::mutex _streams_mutex;
std::timed_mutex _devices_changed_callbacks_mtx;
};

class readonly_device_info : public device_info
Expand Down