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 #ifndef _MSC_VER
37   using VoidTy = void;
38   using TaskTy = std::function<void()>;
39   using PackagedTaskTy = std::packaged_task<void()>;
40 #else
41   // MSVC 2013 has a bug and can't use std::packaged_task<void()>;
42   // We force it to use bool(bool) instead.
43   using VoidTy = bool;
44   using TaskTy = std::function<bool(bool)>;
45   using PackagedTaskTy = std::packaged_task<bool(bool)>;
46 #endif
47
48   /// Construct a pool with the number of core available on the system (or
49   /// whatever the value returned by std::thread::hardware_concurrency() is).
50   ThreadPool();
51
52   /// Construct a pool of \p ThreadCount threads
53   ThreadPool(unsigned ThreadCount);
54
55   /// Blocking destructor: the pool will wait for all the threads to complete.
56   ~ThreadPool();
57
58   /// Asynchronous submission of a task to the pool. The returned future can be
59   /// used to wait for the task to finish and is *non-blocking* on destruction.
60   template <typename Function, typename... Args>
61   inline std::shared_future<VoidTy> async(Function &&F, Args &&... ArgList) {
62     auto Task =
63         std::bind(std::forward<Function>(F), std::forward<Args...>(ArgList...));
64 #ifndef _MSC_VER
65     return asyncImpl(std::move(Task));
66 #else
67     return asyncImpl([Task] (VoidTy) -> VoidTy { Task(); return VoidTy(); });
68 #endif
69   }
70
71   /// Asynchronous submission of a task to the pool. The returned future can be
72   /// used to wait for the task to finish and is *non-blocking* on destruction.
73   template <typename Function>
74   inline std::shared_future<VoidTy> async(Function &&F) {
75 #ifndef _MSC_VER
76     return asyncImpl(std::forward<Function>(F));
77 #else
78     return asyncImpl([F] (VoidTy) -> VoidTy { F(); return VoidTy(); });
79 #endif
80   }
81
82   /// Blocking wait for all the threads to complete and the queue to be empty.
83   /// It is an error to try to add new tasks while blocking on this call.
84   void wait();
85
86 private:
87   /// Asynchronous submission of a task to the pool. The returned future can be
88   /// used to wait for the task to finish and is *non-blocking* on destruction.
89   std::shared_future<VoidTy> asyncImpl(TaskTy F);
90
91   /// Threads in flight
92   std::vector<llvm::thread> Threads;
93
94   /// Tasks waiting for execution in the pool.
95   std::queue<PackagedTaskTy> Tasks;
96
97   /// Locking and signaling for accessing the Tasks queue.
98   std::mutex QueueLock;
99   std::condition_variable QueueCondition;
100
101   /// Locking and signaling for job completion
102   std::mutex CompletionLock;
103   std::condition_variable CompletionCondition;
104
105   /// Keep track of the number of thread actually busy
106   std::atomic<unsigned> ActiveThreads;
107
108 #if LLVM_ENABLE_THREADS // avoids warning for unused variable
109   /// Signal for the destruction of the pool, asking thread to exit.
110   bool EnableFlag;
111 #endif
112 };
113 }
114
115 #endif // LLVM_SUPPORT_THREAD_POOL_H