11
11
#include " lldb/Core/Debugger.h"
12
12
#include " lldb/Utility/StreamString.h"
13
13
14
+ #include < mutex>
14
15
#include < optional>
15
16
16
17
using namespace lldb ;
@@ -67,34 +68,28 @@ void Progress::ReportProgress() {
67
68
}
68
69
}
69
70
70
- void ProgressManager::Initialize () {
71
- lldbassert (!InstanceImpl () && " A progress report manager already exists." );
72
- InstanceImpl ().emplace ();
73
- }
74
-
75
- void ProgressManager::Terminate () {
76
- lldbassert (InstanceImpl () &&
77
- " A progress report manager has already been terminated." );
78
- InstanceImpl ().reset ();
79
- }
80
-
81
- std::optional<ProgressManager> &ProgressManager::InstanceImpl () {
82
- static std::optional<ProgressManager> g_progress_manager;
83
- return g_progress_manager;
71
+ ProgressManager &ProgressManager::InstanceImpl () {
72
+ static std::once_flag g_once_flag;
73
+ static ProgressManager *g_progress_manager = nullptr ;
74
+ std::call_once (g_once_flag, []() {
75
+ // NOTE: known leak to avoid global destructor chain issues.
76
+ g_progress_manager = new ProgressManager ();
77
+ });
78
+ return *g_progress_manager;
84
79
}
85
80
86
81
ProgressManager::ProgressManager () : m_progress_category_map() {}
87
82
88
83
ProgressManager::~ProgressManager () {}
89
84
90
- ProgressManager &ProgressManager::Instance () { return * InstanceImpl (); }
85
+ ProgressManager &ProgressManager::Instance () { return InstanceImpl (); }
91
86
92
87
void ProgressManager::Increment (std::string title) {
93
88
std::lock_guard<std::mutex> lock (m_progress_map_mutex);
94
89
auto pair = m_progress_category_map.insert (std::pair (title, 1 ));
95
90
96
91
// If pair.first is not empty after insertion it means that that
97
- // category was entered for the first time and should not be incremented
92
+ // category was entered for the first time and should not be incremented.
98
93
if (!pair.second )
99
94
++pair.first ->second ;
100
95
}
@@ -103,10 +98,10 @@ void ProgressManager::Decrement(std::string title) {
103
98
std::lock_guard<std::mutex> lock (m_progress_map_mutex);
104
99
auto pos = m_progress_category_map.find (title);
105
100
106
- if (pos == m_progress_category_map.end ())
101
+ if (pos == m_progress_category_map.end () || pos-> second == 0 )
107
102
return ;
108
103
109
- // Remove the category from the map if the refcount reaches 0
104
+ // Remove the category from the map if the refcount reaches 0.
110
105
if (--pos->second == 0 )
111
106
m_progress_category_map.erase (title);
112
107
}
0 commit comments