1 | //===-- Progress.cpp ------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "lldb/Core/Progress.h" |
10 | |
11 | #include "lldb/Core/Debugger.h" |
12 | #include "lldb/Utility/StreamString.h" |
13 | |
14 | #include <optional> |
15 | |
16 | using namespace lldb; |
17 | using namespace lldb_private; |
18 | |
19 | std::atomic<uint64_t> Progress::g_id(0); |
20 | |
21 | Progress::Progress(std::string title, std::string details, |
22 | std::optional<uint64_t> total, |
23 | lldb_private::Debugger *debugger) |
24 | : m_title(title), m_details(details), m_id(++g_id), m_completed(0), |
25 | m_total(Progress::kNonDeterministicTotal) { |
26 | if (total) |
27 | m_total = *total; |
28 | |
29 | if (debugger) |
30 | m_debugger_id = debugger->GetID(); |
31 | std::lock_guard<std::mutex> guard(m_mutex); |
32 | ReportProgress(); |
33 | } |
34 | |
35 | Progress::~Progress() { |
36 | // Make sure to always report progress completed when this object is |
37 | // destructed so it indicates the progress dialog/activity should go away. |
38 | std::lock_guard<std::mutex> guard(m_mutex); |
39 | if (!m_completed) |
40 | m_completed = m_total; |
41 | ReportProgress(); |
42 | } |
43 | |
44 | void Progress::Increment(uint64_t amount, |
45 | std::optional<std::string> updated_detail) { |
46 | if (amount > 0) { |
47 | std::lock_guard<std::mutex> guard(m_mutex); |
48 | if (updated_detail) |
49 | m_details = std::move(updated_detail.value()); |
50 | // Watch out for unsigned overflow and make sure we don't increment too |
51 | // much and exceed m_total. |
52 | if (m_total && (amount > (m_total - m_completed))) |
53 | m_completed = m_total; |
54 | else |
55 | m_completed += amount; |
56 | ReportProgress(); |
57 | } |
58 | } |
59 | |
60 | void Progress::ReportProgress() { |
61 | if (!m_complete) { |
62 | // Make sure we only send one notification that indicates the progress is |
63 | // complete |
64 | m_complete = m_completed == m_total; |
65 | Debugger::ReportProgress(progress_id: m_id, title: m_title, details: m_details, completed: m_completed, total: m_total, |
66 | debugger_id: m_debugger_id); |
67 | } |
68 | } |
69 | |