5648db0642afb1abe8cf5fca7dd21ff65518bc10
[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 #ifdef _MSC_VER
20 // concrt.h depends on eh.h for __uncaught_exception declaration
21 // even if we disable exceptions.
22 #include <eh.h>
23 #endif
24
25 #include <condition_variable>
26 #include <functional>
27 #include <future>
28 #include <memory>
29 #include <mutex>
30 #include <queue>
31 #include <utility>
32
33 namespace llvm {
34
35 /// A ThreadPool for asynchronous parallel execution on a defined number of
36 /// threads.
37 ///
38 /// The pool keeps a vector of threads alive, waiting on a condition variable
39 /// for some work to become available.
40 class ThreadPool {
41 public:
42 #ifndef _MSC_VER
43   using VoidTy = void;
44   using TaskTy = std::function<void()>;
45   using PackagedTaskTy = std::packaged_task<void()>;
46 #else
47   // MSVC 2013 has a bug and can't use std::packaged_task<void()>;
48   // We force it to use bool(bool) instead.
49   using VoidTy = bool;
50   using TaskTy = std::function<bool(bool)>;
51   using PackagedTaskTy = std::packaged_task<bool(bool)>;
52 #endif
53
54   /// Construct a pool with the number of core available on the system (or
55   /// whatever the value returned by std::thread::hardware_concurrency() is).
56   ThreadPool();
57
58   /// Construct a pool of \p ThreadCount threads
59   ThreadPool(unsigned ThreadCount);
60
61   /// Blocking destructor: the pool will wait for all the threads to complete.
62   ~ThreadPool();
63
64   /// Asynchronous submission of a task to the pool. The returned future can be
65   /// used to wait for the task to finish and is *non-blocking* on destruction.
66   template <typename Function, typename... Args>
67   inline std::shared_future<VoidTy> async(Function &&F, Args &&... ArgList) {
68     auto Task =
69         std::bind(std::forward<Function>(F), std::forward<Args>(ArgList)...);
70 #ifndef _MSC_VER
71     return asyncImpl(std::move(Task));
72 #else
73     // This lambda has to be marked mutable because MSVC 2013's std::bind call
74     // operator isn't const qualified.
75     return asyncImpl([Task](VoidTy) mutable -> VoidTy {
76       Task();
77       return VoidTy();
78     });
79 #endif
80   }
81
82   /// Asynchronous submission of a task to the pool. The returned future can be
83   /// used to wait for the task to finish and is *non-blocking* on destruction.
84   template <typename Function>
85   inline std::shared_future<VoidTy> async(Function &&F) {
86 #ifndef _MSC_VER
87     return asyncImpl(std::forward<Function>(F));
88 #else
89     return asyncImpl([F] (VoidTy) -> VoidTy { F(); return VoidTy(); });
90 #endif
91   }
92
93   /// Blocking wait for all the threads to complete and the queue to be empty.
94   /// It is an error to try to add new tasks while blocking on this call.
95   void wait();
96
97 private:
98   /// Asynchronous submission of a task to the pool. The returned future can be
99   /// used to wait for the task to finish and is *non-blocking* on destruction.
100   std::shared_future<VoidTy> asyncImpl(TaskTy F);
101
102   /// Threads in flight
103   std::vector<llvm::thread> Threads;
104
105   /// Tasks waiting for execution in the pool.
106   std::queue<PackagedTaskTy> Tasks;
107
108   /// Locking and signaling for accessing the Tasks queue.
109   std::mutex QueueLock;
110   std::condition_variable QueueCondition;
111
112   /// Locking and signaling for job completion
113   std::mutex CompletionLock;
114   std::condition_variable CompletionCondition;
115
116   /// Keep track of the number of thread actually busy
117   std::atomic<unsigned> ActiveThreads;
118
119 #if LLVM_ENABLE_THREADS // avoids warning for unused variable
120   /// Signal for the destruction of the pool, asking thread to exit.
121   bool EnableFlag;
122 #endif
123 };
124 }
125
126 #endif // LLVM_SUPPORT_THREAD_POOL_H