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

Fix dead-lock on multi-threaded log invocations #6427

Merged
merged 1 commit into from
May 19, 2020
Merged
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
23 changes: 16 additions & 7 deletions common/notifications.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,11 +683,22 @@ namespace rs2
void notifications_model::foreach_log(std::function<void(const std::string& line)> action)
{
std::lock_guard<std::recursive_mutex> lock(m);
for (auto&& l : log)

// Process only the messages that are available upon invocation
std::string log_entry;
for (size_t len = 0; len < incoming_log_queue.size(); len++)
{
action(l);
if (incoming_log_queue.try_dequeue(&log_entry))
notification_logs.push_back(log_entry);
}

// Limit the notification window
while (notification_logs.size() > 200)
notification_logs.pop_front();

for (auto&& l : notification_logs)
action(l);

auto rc = ImGui::GetCursorPos();
ImGui::SetCursorPos({ rc.x, rc.y + 5 });

Expand All @@ -698,15 +709,13 @@ namespace rs2
}
}

// Callback function must not include mutex
void notifications_model::add_log(std::string line)
{
std::lock_guard<std::recursive_mutex> lock(m);
if (!line.size()) return;
// Limit the notification window
while (log.size() > 200)
log.pop_front();

if (line[line.size() - 1] != '\n') line += "\n";
log.push_back(line);
incoming_log_queue.enqueue(std::move(line));
new_log = true;
}

Expand Down
3 changes: 2 additions & 1 deletion common/notifications.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ namespace rs2
std::recursive_mutex m;
bool new_log = false;

std::deque<std::string> log;
single_consumer_queue<std::string> incoming_log_queue;
std::deque<std::string> notification_logs;
std::shared_ptr<notification_model> selected;
std::chrono::system_clock::time_point last_snoozed;
};
Expand Down