Skip to content

[lldb][progress] Add progress manager class #81319

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

Merged
merged 4 commits into from
Feb 15, 2024
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
21 changes: 21 additions & 0 deletions lldb/include/lldb/Core/Progress.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include "lldb/Utility/ConstString.h"
#include "lldb/lldb-types.h"
#include "llvm/ADT/StringMap.h"
#include <atomic>
#include <mutex>
#include <optional>
Expand Down Expand Up @@ -119,6 +120,26 @@ class Progress {
bool m_complete = false;
};

/// A class used to group progress reports by category. This is done by using a
/// map that maintains a refcount of each category of progress reports that have
/// come in. Keeping track of progress reports this way will be done if a
/// debugger is listening to the eBroadcastBitProgressByCategory broadcast bit.
class ProgressManager {
public:
ProgressManager();
~ProgressManager();

/// Control the refcount of the progress report category as needed.
void Increment(std::string category);
void Decrement(std::string category);

static ProgressManager &Instance();

private:
llvm::StringMap<uint64_t> m_progress_category_map;
std::mutex m_progress_map_mutex;
};

} // namespace lldb_private

#endif // LLDB_CORE_PROGRESS_H
33 changes: 33 additions & 0 deletions lldb/source/Core/Progress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "lldb/Core/Debugger.h"
#include "lldb/Utility/StreamString.h"

#include <mutex>
#include <optional>

using namespace lldb;
Expand Down Expand Up @@ -66,3 +67,35 @@ void Progress::ReportProgress() {
m_debugger_id);
}
}

ProgressManager::ProgressManager() : m_progress_category_map() {}

ProgressManager::~ProgressManager() {}

ProgressManager &ProgressManager::Instance() {
static std::once_flag g_once_flag;
static ProgressManager *g_progress_manager = nullptr;
std::call_once(g_once_flag, []() {
// NOTE: known leak to avoid global destructor chain issues.
g_progress_manager = new ProgressManager();
});
return *g_progress_manager;
}

void ProgressManager::Increment(std::string title) {
std::lock_guard<std::mutex> lock(m_progress_map_mutex);
m_progress_category_map[title]++;
}

void ProgressManager::Decrement(std::string title) {
std::lock_guard<std::mutex> lock(m_progress_map_mutex);
auto pos = m_progress_category_map.find(title);

if (pos == m_progress_category_map.end())
return;

if (pos->second <= 1)
m_progress_category_map.erase(title);
else
--pos->second;
}