[Support] Use unique_function rather than std::function for ThreadPool TaskTy.
authorLang Hames <lhames@gmail.com>
Thu, 10 Sep 2020 17:05:46 +0000 (10:05 -0700)
committerLang Hames <lhames@gmail.com>
Thu, 10 Sep 2020 17:46:46 +0000 (10:46 -0700)
This will allow non-copyable function objects (e.g. lambdas that capture
unique_ptrs) to be used with ThreadPool.

Differential Revision: https://reviews.llvm.org/D87467

llvm/include/llvm/Support/ThreadPool.h
llvm/unittests/Support/ThreadPool.cpp

index 528fb32..3d24fb0 100644 (file)
@@ -13,6 +13,7 @@
 #ifndef LLVM_SUPPORT_THREAD_POOL_H
 #define LLVM_SUPPORT_THREAD_POOL_H
 
+#include "llvm/ADT/FunctionExtras.h"
 #include "llvm/Config/llvm-config.h"
 #include "llvm/Support/Threading.h"
 #include "llvm/Support/thread.h"
@@ -36,7 +37,7 @@ namespace llvm {
 /// for some work to become available.
 class ThreadPool {
 public:
-  using TaskTy = std::function<void()>;
+  using TaskTy = unique_function<void()>;
   using PackagedTaskTy = std::packaged_task<void()>;
 
   /// Construct a pool using the hardware strategy \p S for mapping hardware
index 43882d0..b374737 100644 (file)
@@ -133,6 +133,13 @@ TEST_F(ThreadPoolTest, Async) {
   ASSERT_EQ(2, i.load());
 }
 
+TEST_F(ThreadPoolTest, NonCopyableTask) {
+  CHECK_UNSUPPORTED();
+  ThreadPool Pool;
+  Pool.async([P = std::make_unique<int>()] {});
+  Pool.wait();
+};
+
 TEST_F(ThreadPoolTest, GetFuture) {
   CHECK_UNSUPPORTED();
   ThreadPool Pool(hardware_concurrency(2));