From: Yedidya Feldblum Date: Sat, 14 May 2016 11:29:37 +0000 (-0700) Subject: A thread-per-task executor X-Git-Tag: 2016.07.26~233 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=0afc127248ddfd944ec11648ff066d783b87c078;p=folly.git A thread-per-task executor Summary: [Wangle] A thread-per-task executor. Moved from Folly into Wangle and fleshed out. * Starts task threads from a control thread, rather than starting task threads from the submitter thread. Because starting task threads is likely to be more expensive than moving a functor. * Joins task threads as they finish, rather than joining all task threads in the executor's destructor. Suitable for running tasks which spend most of their time sleeping. Such as blocking IO, blocking fork-exec-wait, etc., when it is inconvenient to use the nonblocking variants with an IO executor. Reviewed By: simpkins Differential Revision: D3286988 fbshipit-source-id: 4b91133a7d55332ebbae020c1515c60e816906b3 --- diff --git a/folly/Makefile.am b/folly/Makefile.am index 178d49fe..05058807 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -156,7 +156,6 @@ nobase_follyinclude_HEADERS = \ Format.h \ Format-inl.h \ futures/Barrier.h \ - futures/ThreadedExecutor.h \ futures/DrivableExecutor.h \ futures/Future-pre.h \ futures/helpers.h \ @@ -387,7 +386,6 @@ libfolly_la_SOURCES = \ FileUtil.cpp \ FingerprintTables.cpp \ futures/Barrier.cpp \ - futures/ThreadedExecutor.cpp \ futures/Future.cpp \ futures/InlineExecutor.cpp \ futures/ManualExecutor.cpp \ diff --git a/folly/futures/ThreadedExecutor.cpp b/folly/futures/ThreadedExecutor.cpp deleted file mode 100644 index 3451700c..00000000 --- a/folly/futures/ThreadedExecutor.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2016 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include - -using namespace std; - -namespace folly { namespace futures { - -ThreadedExecutor::~ThreadedExecutor() { - lock_guard lock(mutex_); - destructing_ = true; - for (auto& th : threads_) { - th.join(); - } -} - -void ThreadedExecutor::add(Func f) { - lock_guard lock(mutex_); - CHECK(!destructing_); - threads_.emplace_back(std::move(f)); -} - -}} diff --git a/folly/futures/ThreadedExecutor.h b/folly/futures/ThreadedExecutor.h deleted file mode 100644 index 8971124c..00000000 --- a/folly/futures/ThreadedExecutor.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2016 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include -#include -#include -#include - -namespace folly { namespace futures { - -/** - * Runs functions each in its own thread. - * - * Kind of simple. Suitable for a few types of strange cases. - */ -class ThreadedExecutor : public Executor { -public: - ~ThreadedExecutor(); - void add(Func f) override; -private: - std::mutex mutex_; - std::list threads_; - bool destructing_ = false; -}; - -}} diff --git a/folly/futures/test/ThreadedExecutorTest.cpp b/folly/futures/test/ThreadedExecutorTest.cpp deleted file mode 100644 index a4b639a3..00000000 --- a/folly/futures/test/ThreadedExecutorTest.cpp +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2016 Facebook, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include - -#include - -using namespace std; -using namespace folly; -using namespace folly::futures; - -class ThreadedExecutorTest : public testing::Test {}; - -TEST_F(ThreadedExecutorTest, example) { - ThreadedExecutor x; - auto ret = via(&x) - .then([&] { return 17; }) - .then([&](int x) { return to(x); }) - .wait() - .getTry(); - EXPECT_EQ("17", ret.value()); -}