@@ -296,19 +296,50 @@ class DeferredFile {
296
296
};
297
297
using DeferredFiles = std::vector<DeferredFile>;
298
298
299
- class BackgroundQueue {
299
+ class SerialBackgroundQueue {
300
300
std::deque<std::function<void ()>> queue;
301
301
std::thread *running;
302
302
std::mutex mutex;
303
303
304
304
public:
305
- void queueWork (std::function<void ()> work, bool reap);
305
+ void queueWork (std::function<void ()> work, bool reap) {
306
+ mutex.lock ();
307
+ if (running && (queue.empty () || reap)) {
308
+ mutex.unlock ();
309
+ running->join ();
310
+ mutex.lock ();
311
+ delete running;
312
+ running = nullptr ;
313
+ }
314
+
315
+ if (!reap) {
316
+ queue.emplace_back (std::move (work));
317
+ if (!running)
318
+ running = new std::thread ([&]() {
319
+ bool shouldPop = false ;
320
+ while (true ) {
321
+ mutex.lock ();
322
+ if (shouldPop)
323
+ queue.pop_front ();
324
+ if (queue.empty ()) {
325
+ mutex.unlock ();
326
+ break ;
327
+ }
328
+ auto work = std::move (queue.front ());
329
+ shouldPop = true ;
330
+ mutex.unlock ();
331
+ work ();
332
+ }
333
+ });
334
+ }
335
+ mutex.unlock ();
336
+ }
306
337
};
307
338
308
- // #ifndef NDEBUG
339
+ #ifndef NDEBUG
309
340
#include < iomanip>
310
341
#include < iostream>
311
- // #endif
342
+ #endif
312
343
313
344
// Most input files have been mapped but not yet paged in.
314
345
// This code forces the page-ins on multiple threads so
@@ -341,50 +372,15 @@ void multiThreadedPageInBackground(DeferredFiles &deferred) {
341
372
});
342
373
343
374
LLVM_ATTRIBUTE_UNUSED auto dt = high_resolution_clock::now () - t0;
344
- // LLVM_DEBUG(;
345
- if (Process::GetEnv (" LLD_MULTI_THREAD_PAGE" ))
346
- std::cerr << " multiThreadedPageIn " << totalBytes << " /" << included << " /"
347
- << deferred.size () << " /" << std::setprecision (4 )
348
- << duration_cast<milliseconds>(dt).count () / 1000 . << " \n " ;
349
- // );
350
- }
351
-
352
- void BackgroundQueue::queueWork (std::function<void ()> work, bool reap) {
353
- mutex.lock ();
354
- if (running && (queue.empty () || reap)) {
355
- mutex.unlock ();
356
- running->join ();
357
- mutex.lock ();
358
- delete running;
359
- running = nullptr ;
360
- }
361
-
362
- if (!reap) {
363
- queue.emplace_back (work);
364
- if (!running)
365
- running = new std::thread ([&]() {
366
- bool shouldPop = false ;
367
- while (true ) {
368
- mutex.lock ();
369
- if (shouldPop)
370
- queue.pop_front ();
371
- if (queue.empty ()) {
372
- mutex.unlock ();
373
- break ;
374
- }
375
- auto work = std::move (queue.front ());
376
- shouldPop = true ;
377
- mutex.unlock ();
378
- work ();
379
- }
380
- });
381
- }
382
- mutex.unlock ();
375
+ LLVM_DEBUG (if (Process::GetEnv (" LLD_MULTI_THREAD_PAGE" )) std::cerr
376
+ << " multiThreadedPageIn " << totalBytes << " /" << included << " /"
377
+ << deferred.size () << " /" << std::setprecision (4 )
378
+ << duration_cast<milliseconds>(dt).count () / 1000 . << " \n " );
383
379
}
384
380
385
381
static void multiThreadedPageIn (const DeferredFiles &deferred,
386
382
bool reap = false ) {
387
- static BackgroundQueue pageInQueue;
383
+ static SerialBackgroundQueue pageInQueue;
388
384
pageInQueue.queueWork (
389
385
[=]() {
390
386
DeferredFiles files = deferred;
0 commit comments