LLDB mainline
Progress.cpp
Go to the documentation of this file.
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
10
11#include "lldb/Core/Debugger.h"
13
14#include <mutex>
15#include <optional>
16
17using namespace lldb;
18using namespace lldb_private;
19
20std::atomic<uint64_t> Progress::g_id(0);
21
22Progress::Progress(std::string title, std::string details,
23 std::optional<uint64_t> total,
24 lldb_private::Debugger *debugger)
25 : m_title(title), m_details(details), m_id(++g_id), m_completed(0),
26 m_total(Progress::kNonDeterministicTotal) {
27 if (total)
28 m_total = *total;
29
30 if (debugger)
31 m_debugger_id = debugger->GetID();
32 std::lock_guard<std::mutex> guard(m_mutex);
34}
35
37 // Make sure to always report progress completed when this object is
38 // destructed so it indicates the progress dialog/activity should go away.
39 std::lock_guard<std::mutex> guard(m_mutex);
40 if (!m_completed)
43}
44
45void Progress::Increment(uint64_t amount,
46 std::optional<std::string> updated_detail) {
47 if (amount > 0) {
48 std::lock_guard<std::mutex> guard(m_mutex);
49 if (updated_detail)
50 m_details = std::move(updated_detail.value());
51 // Watch out for unsigned overflow and make sure we don't increment too
52 // much and exceed m_total.
53 if (m_total && (amount > (m_total - m_completed)))
55 else
56 m_completed += amount;
58 }
59}
60
62 if (!m_complete) {
63 // Make sure we only send one notification that indicates the progress is
64 // complete
68 }
69}
70
71ProgressManager::ProgressManager() : m_progress_category_map() {}
72
74
76 static std::once_flag g_once_flag;
77 static ProgressManager *g_progress_manager = nullptr;
78 std::call_once(g_once_flag, []() {
79 // NOTE: known leak to avoid global destructor chain issues.
80 g_progress_manager = new ProgressManager();
81 });
82 return *g_progress_manager;
83}
84
85void ProgressManager::Increment(std::string title) {
86 std::lock_guard<std::mutex> lock(m_progress_map_mutex);
88}
89
90void ProgressManager::Decrement(std::string title) {
91 std::lock_guard<std::mutex> lock(m_progress_map_mutex);
92 auto pos = m_progress_category_map.find(title);
93
94 if (pos == m_progress_category_map.end())
95 return;
96
97 if (pos->second <= 1)
98 m_progress_category_map.erase(title);
99 else
100 --pos->second;
101}
A class to manage flag bits.
Definition: Debugger.h:79
static void ReportProgress(uint64_t progress_id, std::string title, std::string details, uint64_t completed, uint64_t total, std::optional< lldb::user_id_t > debugger_id)
Report progress events.
Definition: Debugger.cpp:1448
A class used to group progress reports by category.
Definition: Progress.h:127
void Increment(std::string category)
Control the refcount of the progress report category as needed.
Definition: Progress.cpp:85
static ProgressManager & Instance()
Definition: Progress.cpp:75
llvm::StringMap< uint64_t > m_progress_category_map
Definition: Progress.h:139
void Decrement(std::string category)
Definition: Progress.cpp:90
A Progress indicator helper class.
Definition: Progress.h:57
bool m_complete
Set to true when progress has been reported where m_completed == m_total to ensure that we don't send...
Definition: Progress.h:120
void Increment(uint64_t amount=1, std::optional< std::string > updated_detail={})
Increment the progress and send a notification to the intalled callback.
Definition: Progress.cpp:45
uint64_t m_completed
How much work ([0...m_total]) that has been completed.
Definition: Progress.h:110
static std::atomic< uint64_t > g_id
Definition: Progress.h:102
std::optional< lldb::user_id_t > m_debugger_id
The optional debugger ID to report progress to.
Definition: Progress.h:116
std::string m_details
Definition: Progress.h:105
std::string m_title
The title of the progress activity.
Definition: Progress.h:104
std::mutex m_mutex
Definition: Progress.h:106
uint64_t m_total
Total amount of work, use a std::nullopt in the constructor for non deterministic progress.
Definition: Progress.h:113
~Progress()
Destroy the progress object.
Definition: Progress.cpp:36
Progress(std::string title, std::string details={}, std::optional< uint64_t > total=std::nullopt, lldb_private::Debugger *debugger=nullptr)
Construct a progress object that will report information.
Definition: Progress.cpp:22
const uint64_t m_id
A unique integer identifier for progress reporting.
Definition: Progress.h:108
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
lldb::user_id_t GetID() const
Get accessor for the user ID.
Definition: UserID.h:47