A thread-per-task executor
authorYedidya Feldblum <yfeldblum@fb.com>
Sat, 14 May 2016 11:29:37 +0000 (04:29 -0700)
committerFacebook Github Bot 2 <facebook-github-bot-2-bot@fb.com>
Sat, 14 May 2016 11:38:25 +0000 (04:38 -0700)
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

folly/Makefile.am
folly/futures/ThreadedExecutor.cpp [deleted file]
folly/futures/ThreadedExecutor.h [deleted file]
folly/futures/test/ThreadedExecutorTest.cpp [deleted file]

index 178d49fefc37912784e50a48b094389703c8e2b2..050588071b777ef6a20a83994f986fc3a5ea1bf8 100644 (file)
@@ -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 (file)
index 3451700..0000000
+++ /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 <folly/futures/ThreadedExecutor.h>
-
-#include <glog/logging.h>
-
-using namespace std;
-
-namespace folly { namespace futures {
-
-ThreadedExecutor::~ThreadedExecutor() {
-  lock_guard<mutex> lock(mutex_);
-  destructing_ = true;
-  for (auto& th : threads_) {
-    th.join();
-  }
-}
-
-void ThreadedExecutor::add(Func f) {
-  lock_guard<mutex> 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 (file)
index 8971124..0000000
+++ /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 <list>
-#include <mutex>
-#include <thread>
-#include <folly/Executor.h>
-
-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<std::thread> threads_;
-  bool destructing_ = false;
-};
-
-}}
diff --git a/folly/futures/test/ThreadedExecutorTest.cpp b/folly/futures/test/ThreadedExecutorTest.cpp
deleted file mode 100644 (file)
index a4b639a..0000000
+++ /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 <folly/futures/ThreadedExecutor.h>
-#include <folly/futures/Future.h>
-#include <folly/Conv.h>
-
-#include <gtest/gtest.h>
-
-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<string>(x); })
-    .wait()
-    .getTry();
-  EXPECT_EQ("17", ret.value());
-}