[Lldb-commits] [lldb] [lldb][progress] Add progress manager class (PR #81319)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Wed Feb 14 19:31:37 PST 2024


================
@@ -66,3 +67,41 @@ void Progress::ReportProgress() {
                              m_debugger_id);
   }
 }
+
+ProgressManager &ProgressManager::InstanceImpl() {
+  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;
+}
+
+ProgressManager::ProgressManager() : m_progress_category_map() {}
+
+ProgressManager::~ProgressManager() {}
+
+ProgressManager &ProgressManager::Instance() { return InstanceImpl(); }
+
+void ProgressManager::Increment(std::string title) {
+  std::lock_guard<std::mutex> lock(m_progress_map_mutex);
+  auto pair = m_progress_category_map.insert(std::pair(title, 1));
+
+  // If pair.first is not empty after insertion it means that that
+  // category was entered for the first time and should not be incremented.
+  if (!pair.second)
+    ++pair.first->second;
----------------
JDevlieghere wrote:

Any reason this should be a pre-increment? `pair.first` returns an iterator so without checking the C++ reference, I don't know if this is incrementing the or the value. Thinking about it some more, you can simplify this a lot:

```
std::lock_guard<std::mutex> lock(m_progress_map_mutex);
m_progress_category_map[title]++;
```

`operator[]` will return a default constructed value (i.e. 0) and then the increment will set it to one. 

https://github.com/llvm/llvm-project/pull/81319


More information about the lldb-commits mailing list