@@ -48,7 +48,10 @@ PathMappingList::PathMappingList(const PathMappingList &rhs)
48
48
49
49
const PathMappingList &PathMappingList::operator =(const PathMappingList &rhs) {
50
50
if (this != &rhs) {
51
- std::scoped_lock<std::recursive_mutex, std::recursive_mutex> locks (m_mutex, rhs.m_mutex );
51
+ std::scoped_lock<std::mutex, std::mutex> callback_locks (
52
+ m_callback_mutex, rhs.m_callback_mutex );
53
+ std::scoped_lock<std::mutex, std::mutex> pairs_locks (m_pairs_mutex,
54
+ rhs.m_pairs_mutex );
52
55
m_pairs = rhs.m_pairs ;
53
56
m_callback = nullptr ;
54
57
m_callback_baton = nullptr ;
@@ -59,85 +62,105 @@ const PathMappingList &PathMappingList::operator=(const PathMappingList &rhs) {
59
62
60
63
PathMappingList::~PathMappingList () = default ;
61
64
62
- void PathMappingList::Append (llvm::StringRef path, llvm::StringRef replacement,
63
- bool notify) {
64
- std::lock_guard<std::recursive_mutex> lock (m_mutex);
65
+ void PathMappingList::AppendImpl (llvm::StringRef path,
66
+ llvm::StringRef replacement) {
65
67
++m_mod_id;
66
68
m_pairs.emplace_back (pair (NormalizePath (path), NormalizePath (replacement)));
69
+ }
70
+
71
+ void PathMappingList::Notify (bool notify) const {
72
+ std::lock_guard<std::mutex> lock (m_callback_mutex);
67
73
if (notify && m_callback)
68
74
m_callback (*this , m_callback_baton);
69
75
}
70
76
77
+ void PathMappingList::Append (llvm::StringRef path, llvm::StringRef replacement,
78
+ bool notify) {
79
+ {
80
+ std::lock_guard<std::mutex> lock (m_pairs_mutex);
81
+ AppendImpl (path, replacement);
82
+ }
83
+ Notify (notify);
84
+ }
85
+
71
86
void PathMappingList::Append (const PathMappingList &rhs, bool notify) {
72
- std::scoped_lock<std::recursive_mutex, std::recursive_mutex> locks (m_mutex, rhs.m_mutex );
73
- ++m_mod_id;
74
- if (!rhs.m_pairs .empty ()) {
87
+ {
88
+ std::scoped_lock<std::mutex, std::mutex> locks (m_pairs_mutex,
89
+ rhs.m_pairs_mutex );
90
+ ++m_mod_id;
91
+ if (rhs.m_pairs .empty ())
92
+ return ;
75
93
const_iterator pos, end = rhs.m_pairs .end ();
76
94
for (pos = rhs.m_pairs .begin (); pos != end; ++pos)
77
95
m_pairs.push_back (*pos);
78
- if (notify && m_callback)
79
- m_callback (*this , m_callback_baton);
80
96
}
97
+ Notify (notify);
81
98
}
82
99
83
100
bool PathMappingList::AppendUnique (llvm::StringRef path,
84
101
llvm::StringRef replacement, bool notify) {
85
102
auto normalized_path = NormalizePath (path);
86
103
auto normalized_replacement = NormalizePath (replacement);
87
- std::lock_guard<std::recursive_mutex> lock (m_mutex);
88
- for (const auto &pair : m_pairs) {
89
- if (pair.first .GetStringRef () == normalized_path &&
90
- pair.second .GetStringRef () == normalized_replacement)
91
- return false ;
104
+ {
105
+ std::lock_guard<std::mutex> lock (m_pairs_mutex);
106
+ for (const auto &pair : m_pairs) {
107
+ if (pair.first .GetStringRef () == normalized_path &&
108
+ pair.second .GetStringRef () == normalized_replacement)
109
+ return false ;
110
+ }
111
+ AppendImpl (path, replacement);
92
112
}
93
- Append (path, replacement, notify);
113
+ Notify ( notify);
94
114
return true ;
95
115
}
96
116
97
117
void PathMappingList::Insert (llvm::StringRef path, llvm::StringRef replacement,
98
118
uint32_t index, bool notify) {
99
- std::lock_guard<std::recursive_mutex> lock (m_mutex);
100
- ++m_mod_id;
101
- iterator insert_iter;
102
- if (index >= m_pairs.size ())
103
- insert_iter = m_pairs.end ();
104
- else
105
- insert_iter = m_pairs.begin () + index ;
106
- m_pairs.emplace (insert_iter, pair (NormalizePath (path),
107
- NormalizePath (replacement)));
108
- if (notify && m_callback)
109
- m_callback (*this , m_callback_baton);
119
+ {
120
+ std::lock_guard<std::mutex> lock (m_pairs_mutex);
121
+ ++m_mod_id;
122
+ iterator insert_iter;
123
+ if (index >= m_pairs.size ())
124
+ insert_iter = m_pairs.end ();
125
+ else
126
+ insert_iter = m_pairs.begin () + index ;
127
+ m_pairs.emplace (insert_iter,
128
+ pair (NormalizePath (path), NormalizePath (replacement)));
129
+ }
130
+ Notify (notify);
110
131
}
111
132
112
133
bool PathMappingList::Replace (llvm::StringRef path, llvm::StringRef replacement,
113
134
uint32_t index, bool notify) {
114
- std::lock_guard<std::recursive_mutex> lock (m_mutex);
115
- if (index >= m_pairs.size ())
116
- return false ;
117
- ++m_mod_id;
118
- m_pairs[index ] = pair (NormalizePath (path), NormalizePath (replacement));
119
- if (notify && m_callback)
120
- m_callback (*this , m_callback_baton);
135
+ {
136
+ std::lock_guard<std::mutex> lock (m_pairs_mutex);
137
+ if (index >= m_pairs.size ())
138
+ return false ;
139
+ ++m_mod_id;
140
+ m_pairs[index ] = pair (NormalizePath (path), NormalizePath (replacement));
141
+ }
142
+ Notify (notify);
121
143
return true ;
122
144
}
123
145
124
146
bool PathMappingList::Remove (size_t index, bool notify) {
125
- std::lock_guard<std::recursive_mutex> lock (m_mutex);
126
- if (index >= m_pairs.size ())
127
- return false ;
147
+ {
148
+ std::lock_guard<std::mutex> lock (m_pairs_mutex);
149
+ if (index >= m_pairs.size ())
150
+ return false ;
128
151
129
- ++m_mod_id;
130
- iterator iter = m_pairs.begin () + index ;
131
- m_pairs.erase (iter);
132
- if (notify && m_callback)
133
- m_callback (* this , m_callback_baton );
152
+ ++m_mod_id;
153
+ iterator iter = m_pairs.begin () + index ;
154
+ m_pairs.erase (iter);
155
+ }
156
+ Notify (notify );
134
157
return true ;
135
158
}
136
159
137
160
// For clients which do not need the pair index dumped, pass a pair_index >= 0
138
161
// to only dump the indicated pair.
139
162
void PathMappingList::Dump (Stream *s, int pair_index) {
140
- std::lock_guard<std::recursive_mutex > lock (m_mutex );
163
+ std::lock_guard<std::mutex > lock (m_pairs_mutex );
141
164
unsigned int numPairs = m_pairs.size ();
142
165
143
166
if (pair_index < 0 ) {
@@ -155,7 +178,7 @@ void PathMappingList::Dump(Stream *s, int pair_index) {
155
178
156
179
llvm::json::Value PathMappingList::ToJSON () {
157
180
llvm::json::Array entries;
158
- std::lock_guard<std::recursive_mutex > lock (m_mutex );
181
+ std::lock_guard<std::mutex > lock (m_pairs_mutex );
159
182
for (const auto &pair : m_pairs) {
160
183
llvm::json::Array entry{pair.first .GetStringRef ().str (),
161
184
pair.second .GetStringRef ().str ()};
@@ -165,12 +188,13 @@ llvm::json::Value PathMappingList::ToJSON() {
165
188
}
166
189
167
190
void PathMappingList::Clear (bool notify) {
168
- std::lock_guard<std::recursive_mutex> lock (m_mutex);
169
- if (!m_pairs.empty ())
170
- ++m_mod_id;
171
- m_pairs.clear ();
172
- if (notify && m_callback)
173
- m_callback (*this , m_callback_baton);
191
+ {
192
+ std::lock_guard<std::mutex> lock (m_pairs_mutex);
193
+ if (!m_pairs.empty ())
194
+ ++m_mod_id;
195
+ m_pairs.clear ();
196
+ }
197
+ Notify (notify);
174
198
}
175
199
176
200
bool PathMappingList::RemapPath (ConstString path,
@@ -196,7 +220,7 @@ static void AppendPathComponents(FileSpec &path, llvm::StringRef components,
196
220
197
221
std::optional<FileSpec> PathMappingList::RemapPath (llvm::StringRef mapping_path,
198
222
bool only_if_exists) const {
199
- std::lock_guard<std::recursive_mutex > lock (m_mutex );
223
+ std::lock_guard<std::mutex > lock (m_pairs_mutex );
200
224
if (m_pairs.empty () || mapping_path.empty ())
201
225
return {};
202
226
LazyBool path_is_relative = eLazyBoolCalculate;
@@ -235,7 +259,7 @@ std::optional<llvm::StringRef>
235
259
PathMappingList::ReverseRemapPath (const FileSpec &file, FileSpec &fixed) const {
236
260
std::string path = file.GetPath ();
237
261
llvm::StringRef path_ref (path);
238
- std::lock_guard<std::recursive_mutex > lock (m_mutex );
262
+ std::lock_guard<std::mutex > lock (m_pairs_mutex );
239
263
for (const auto &it : m_pairs) {
240
264
llvm::StringRef removed_prefix = it.second .GetStringRef ();
241
265
if (!path_ref.consume_front (it.second .GetStringRef ()))
@@ -264,34 +288,35 @@ PathMappingList::FindFile(const FileSpec &orig_spec) const {
264
288
265
289
bool PathMappingList::Replace (llvm::StringRef path, llvm::StringRef new_path,
266
290
bool notify) {
267
- std::lock_guard<std::recursive_mutex> lock (m_mutex);
268
- uint32_t idx = FindIndexForPath (path);
269
- if (idx < m_pairs.size ()) {
291
+ {
292
+ std::lock_guard<std::mutex> lock (m_pairs_mutex);
293
+ uint32_t idx = FindIndexForPath (path);
294
+ if (idx >= m_pairs.size ())
295
+ return false ;
270
296
++m_mod_id;
271
297
m_pairs[idx].second = ConstString (new_path);
272
- if (notify && m_callback)
273
- m_callback (*this , m_callback_baton);
274
- return true ;
275
298
}
276
- return false ;
299
+ Notify (notify);
300
+ return true ;
277
301
}
278
302
279
303
bool PathMappingList::Remove (ConstString path, bool notify) {
280
- std::lock_guard<std::recursive_mutex> lock (m_mutex);
281
- iterator pos = FindIteratorForPath (path);
282
- if (pos != m_pairs.end ()) {
304
+ {
305
+ std::lock_guard<std::mutex> lock (m_pairs_mutex);
306
+ iterator pos = FindIteratorForPath (path);
307
+ if (pos == m_pairs.end ())
308
+ return false ;
309
+
283
310
++m_mod_id;
284
311
m_pairs.erase (pos);
285
- if (notify && m_callback)
286
- m_callback (*this , m_callback_baton);
287
- return true ;
288
312
}
289
- return false ;
313
+ Notify (notify);
314
+ return true ;
290
315
}
291
316
292
317
PathMappingList::const_iterator
293
318
PathMappingList::FindIteratorForPath (ConstString path) const {
294
- std::lock_guard<std::recursive_mutex > lock (m_mutex );
319
+ std::lock_guard<std::mutex > lock (m_pairs_mutex );
295
320
const_iterator pos;
296
321
const_iterator begin = m_pairs.begin ();
297
322
const_iterator end = m_pairs.end ();
@@ -305,7 +330,7 @@ PathMappingList::FindIteratorForPath(ConstString path) const {
305
330
306
331
PathMappingList::iterator
307
332
PathMappingList::FindIteratorForPath (ConstString path) {
308
- std::lock_guard<std::recursive_mutex > lock (m_mutex );
333
+ std::lock_guard<std::mutex > lock (m_pairs_mutex );
309
334
iterator pos;
310
335
iterator begin = m_pairs.begin ();
311
336
iterator end = m_pairs.end ();
@@ -319,7 +344,7 @@ PathMappingList::FindIteratorForPath(ConstString path) {
319
344
320
345
bool PathMappingList::GetPathsAtIndex (uint32_t idx, ConstString &path,
321
346
ConstString &new_path) const {
322
- std::lock_guard<std::recursive_mutex > lock (m_mutex );
347
+ std::lock_guard<std::mutex > lock (m_pairs_mutex );
323
348
if (idx < m_pairs.size ()) {
324
349
path = m_pairs[idx].first ;
325
350
new_path = m_pairs[idx].second ;
@@ -330,7 +355,7 @@ bool PathMappingList::GetPathsAtIndex(uint32_t idx, ConstString &path,
330
355
331
356
uint32_t PathMappingList::FindIndexForPath (llvm::StringRef orig_path) const {
332
357
const ConstString path = ConstString (NormalizePath (orig_path));
333
- std::lock_guard<std::recursive_mutex > lock (m_mutex );
358
+ std::lock_guard<std::mutex > lock (m_pairs_mutex );
334
359
const_iterator pos;
335
360
const_iterator begin = m_pairs.begin ();
336
361
const_iterator end = m_pairs.end ();
0 commit comments