1 //===-- llvm/Support/ThreadPool.h - A ThreadPool implementation -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a crude C++11 based thread pool.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_SUPPORT_THREAD_POOL_H
15 #define LLVM_SUPPORT_THREAD_POOL_H
17 #include "llvm/Support/thread.h"
19 #include <condition_variable>
29 /// A ThreadPool for asynchronous parallel execution on a defined number of
32 /// The pool keeps a vector of threads alive, waiting on a condition variable
33 /// for some work to become available.
39 // MSVC 2013 has a bug and can't use std::packaged_task<void()>;
40 // We force it to use bool(bool) instead.
43 using TaskTy = std::function<VoidTy(VoidTy)>;
44 using PackagedTaskTy = std::packaged_task<VoidTy(VoidTy)>;
46 /// Construct a pool with the number of core available on the system (or
47 /// whatever the value returned by std::thread::hardware_concurrency() is).
50 /// Construct a pool of \p ThreadCount threads
51 ThreadPool(unsigned ThreadCount);
53 /// Blocking destructor: the pool will wait for all the threads to complete.
56 /// Asynchronous submission of a task to the pool. The returned future can be
57 /// used to wait for the task to finish and is *non-blocking* on destruction.
58 template <typename Function, typename... Args>
59 inline std::shared_future<VoidTy> async(Function &&F, Args &&... ArgList) {
61 std::bind(std::forward<Function>(F), std::forward<Args...>(ArgList...));
63 return asyncImpl(std::move(Task));
65 return asyncImpl([Task] (VoidTy) -> VoidTy { Task(); return VoidTy(); });
69 /// Asynchronous submission of a task to the pool. The returned future can be
70 /// used to wait for the task to finish and is *non-blocking* on destruction.
71 template <typename Function>
72 inline std::shared_future<VoidTy> async(Function &&F) {
74 return asyncImpl(std::forward<Function>(F));
76 return asyncImpl([F] (VoidTy) -> VoidTy { F(); return VoidTy(); });
80 /// Blocking wait for all the threads to complete and the queue to be empty.
81 /// It is an error to try to add new tasks while blocking on this call.
85 /// Asynchronous submission of a task to the pool. The returned future can be
86 /// used to wait for the task to finish and is *non-blocking* on destruction.
87 std::shared_future<VoidTy> asyncImpl(TaskTy F);
90 std::vector<llvm::thread> Threads;
92 /// Tasks waiting for execution in the pool.
93 std::queue<PackagedTaskTy> Tasks;
95 /// Locking and signaling for accessing the Tasks queue.
97 std::condition_variable QueueCondition;
99 /// Locking and signaling for job completion
100 std::mutex CompletionLock;
101 std::condition_variable CompletionCondition;
103 /// Keep track of the number of thread actually busy
104 std::atomic<unsigned> ActiveThreads;
106 #if LLVM_ENABLE_THREADS // avoids warning for unused variable
107 /// Signal for the destruction of the pool, asking thread to exit.
113 #endif // LLVM_SUPPORT_THREAD_POOL_H