[llvm] d9c8b02 - [Support] Use unique_function rather than std::function for ThreadPool TaskTy.
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 10 10:47:11 PDT 2020
Author: Lang Hames
Date: 2020-09-10T10:46:46-07:00
New Revision: d9c8b0256cfc673c2413b13993c9440be598818f
URL: https://github.com/llvm/llvm-project/commit/d9c8b0256cfc673c2413b13993c9440be598818f
DIFF: https://github.com/llvm/llvm-project/commit/d9c8b0256cfc673c2413b13993c9440be598818f.diff
LOG: [Support] Use unique_function rather than std::function for ThreadPool TaskTy.
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
Added:
Modified:
llvm/include/llvm/Support/ThreadPool.h
llvm/unittests/Support/ThreadPool.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/ThreadPool.h b/llvm/include/llvm/Support/ThreadPool.h
index 528fb32525eb..3d24fb099739 100644
--- a/llvm/include/llvm/Support/ThreadPool.h
+++ b/llvm/include/llvm/Support/ThreadPool.h
@@ -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
diff --git a/llvm/unittests/Support/ThreadPool.cpp b/llvm/unittests/Support/ThreadPool.cpp
index 43882d0f3cee..b3747376689a 100644
--- a/llvm/unittests/Support/ThreadPool.cpp
+++ b/llvm/unittests/Support/ThreadPool.cpp
@@ -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));
More information about the llvm-commits
mailing list