summaryrefslogtreecommitdiffstats
path: root/lld/Common/Threads.cpp
blob: 86c8f43f5fc6ec9c8af95c53c5e3af6dc790f224 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//===- Threads.cpp --------------------------------------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lld/Common/Threads.h"
#include <thread>

static std::vector<std::thread> Threads;

bool lld::ThreadsEnabled = true;

// Runs a given function in a new thread.
void lld::runBackground(std::function<void()> Fn) {
  Threads.emplace_back(Fn);
}

// Wait for all threads spawned for runBackground() to finish.
//
// You need to call this function from the main thread before exiting
// because it is not defined what will happen to non-main threads when
// the main thread exits.
void lld::waitForBackgroundThreads() {
  for (std::thread &T : Threads)
    if (T.joinable())
      T.join();
}
OpenPOWER on IntegriCloud