Skip to content

Commit ad45284

Browse files
[lldb][progress] Add progress manager class (llvm#81319) (#8218)
Per discussions from llvm#81026, it was decided that having a class that manages a map of progress reports would be beneficial in order to categorize them. This class is a part of the overall `Progress` class and utilizes a map that keeps a count of how many times a progress report category has been sent. This would be used with the new debugger broadcast bit added in llvm#81169 so that a user listening with that bit will receive grouped progress reports. (cherry picked from commit 327d2f8)
1 parent 4fa1a34 commit ad45284

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

lldb/include/lldb/Core/Progress.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "lldb/Utility/ConstString.h"
1313
#include "lldb/lldb-types.h"
14+
#include "llvm/ADT/StringMap.h"
1415
#include <atomic>
1516
#include <mutex>
1617
#include <optional>
@@ -119,6 +120,26 @@ class Progress {
119120
bool m_complete = false;
120121
};
121122

123+
/// A class used to group progress reports by category. This is done by using a
124+
/// map that maintains a refcount of each category of progress reports that have
125+
/// come in. Keeping track of progress reports this way will be done if a
126+
/// debugger is listening to the eBroadcastBitProgressByCategory broadcast bit.
127+
class ProgressManager {
128+
public:
129+
ProgressManager();
130+
~ProgressManager();
131+
132+
/// Control the refcount of the progress report category as needed.
133+
void Increment(std::string category);
134+
void Decrement(std::string category);
135+
136+
static ProgressManager &Instance();
137+
138+
private:
139+
llvm::StringMap<uint64_t> m_progress_category_map;
140+
std::mutex m_progress_map_mutex;
141+
};
142+
122143
} // namespace lldb_private
123144

124145
#endif // LLDB_CORE_PROGRESS_H

lldb/source/Core/Progress.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "lldb/Core/Debugger.h"
1212
#include "lldb/Utility/StreamString.h"
1313

14+
#include <mutex>
1415
#include <optional>
1516

1617
using namespace lldb;
@@ -66,3 +67,35 @@ void Progress::ReportProgress() {
6667
m_debugger_id);
6768
}
6869
}
70+
71+
ProgressManager::ProgressManager() : m_progress_category_map() {}
72+
73+
ProgressManager::~ProgressManager() {}
74+
75+
ProgressManager &ProgressManager::Instance() {
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+
85+
void ProgressManager::Increment(std::string title) {
86+
std::lock_guard<std::mutex> lock(m_progress_map_mutex);
87+
m_progress_category_map[title]++;
88+
}
89+
90+
void 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+
}

0 commit comments

Comments
 (0)