Add a C++11 ThreadPool implementation in LLVM
[oota-llvm.git] / include / llvm / Support / ThreadPool.h
1 //===-- llvm/Support/ThreadPool.h - A ThreadPool implementation -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a crude C++11 based thread pool.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_SUPPORT_THREAD_POOL_H
15 #define LLVM_SUPPORT_THREAD_POOL_H
16
17 #include "llvm/Support/thread.h"
18
19 #include <condition_variable>
20 #include <functional>
21 #include <future>
22 #include <memory>
23 #include <mutex>
24 #include <queue>
25 #include <utility>
26
27 namespace llvm {
28
29 /// A ThreadPool for asynchronous parallel execution on a defined number of
30 /// threads.
31 ///
32 /// The pool keeps a vector of threads alive, waiting on a condition variable
33 /// for some work to become available.
34 class ThreadPool {
35 public:
36   using TaskTy = std::function<void()>;
37
38   /// Construct a pool with the number of core available on the system (or
39   /// whatever the value returned by std::thread::hardware_concurrency() is).
40   ThreadPool();
41
42   /// Construct a pool of \p ThreadCount threads
43   ThreadPool(unsigned ThreadCount);
44
45   /// Blocking destructor: the pool will wait for all the threads to complete.
46   ~ThreadPool();
47
48   /// Asynchronous submission of a task to the pool. The returned future can be
49   /// used to wait for the task to finish and is *non-blocking* on destruction.
50   template <typename Function, typename... Args>
51   inline std::shared_future<void> async(Function &&F, Args &&... ArgList) {
52     auto Task =
53         std::bind(std::forward<Function>(F), std::forward<Args...>(ArgList...));
54     return asyncImpl(Task);
55   }
56
57   /// Asynchronous submission of a task to the pool. The returned future can be
58   /// used to wait for the task to finish and is *non-blocking* on destruction.
59   template <typename Function>
60   inline std::shared_future<void> async(Function &&F) {
61     return asyncImpl(F);
62   }
63
64   /// Blocking wait for all the threads to complete and the queue to be empty.
65   /// It is an error to try to add new tasks while blocking on this call.
66   void wait();
67
68 private:
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   std::shared_future<void> asyncImpl(TaskTy f);
72
73   /// Threads in flight
74   std::vector<llvm::thread> Threads;
75
76   /// Tasks waiting for execution in the pool.
77   std::queue<std::packaged_task<void()>> Tasks;
78
79   /// Locking and signaling for accessing the Tasks queue.
80   std::mutex QueueLock;
81   std::condition_variable QueueCondition;
82
83   /// Locking and signaling for job completion
84   std::mutex CompletionLock;
85   std::condition_variable CompletionCondition;
86
87   /// Keep track of the number of thread actually busy
88   std::atomic<unsigned> ActiveThreads;
89
90   /// Signal for the destruction of the pool, asking thread to exit.
91   bool EnableFlag;
92 };
93 }
94
95 #endif // LLVM_SUPPORT_THREAD_POOL_H