Skip to content

Commit 4cab9be

Browse files
committed
Encapsulate SerialBackgroundQueue. Remove DEBUG output.
1 parent ed9bdb7 commit 4cab9be

File tree

1 file changed

+40
-44
lines changed

1 file changed

+40
-44
lines changed

lld/MachO/Driver.cpp

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -296,19 +296,50 @@ class DeferredFile {
296296
};
297297
using DeferredFiles = std::vector<DeferredFile>;
298298

299-
class BackgroundQueue {
299+
class SerialBackgroundQueue {
300300
std::deque<std::function<void()>> queue;
301301
std::thread *running;
302302
std::mutex mutex;
303303

304304
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+
}
306337
};
307338

308-
// #ifndef NDEBUG
339+
#ifndef NDEBUG
309340
#include <iomanip>
310341
#include <iostream>
311-
// #endif
342+
#endif
312343

313344
// Most input files have been mapped but not yet paged in.
314345
// This code forces the page-ins on multiple threads so
@@ -341,50 +372,15 @@ void multiThreadedPageInBackground(DeferredFiles &deferred) {
341372
});
342373

343374
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");
383379
}
384380

385381
static void multiThreadedPageIn(const DeferredFiles &deferred,
386382
bool reap = false) {
387-
static BackgroundQueue pageInQueue;
383+
static SerialBackgroundQueue pageInQueue;
388384
pageInQueue.queueWork(
389385
[=]() {
390386
DeferredFiles files = deferred;

0 commit comments

Comments
 (0)