ThreadedExecutor.
authorYedidya Feldblum <yfeldblum@fb.com>
Thu, 9 Jul 2015 00:54:16 +0000 (17:54 -0700)
committerSara Golemon <sgolemon@fb.com>
Thu, 9 Jul 2015 22:32:55 +0000 (15:32 -0700)
Summary: [Folly] ThreadedExecutor.

It's an Executor that runs functions each in its own thread.

Kind of simple. Suitable for a few types of strange cases.

Reviewed By: @​hannesr

Differential Revision: D2226211

folly/Makefile.am
folly/futures/ThreadedExecutor.cpp [new file with mode: 0644]
folly/futures/ThreadedExecutor.h [new file with mode: 0644]
folly/futures/test/ThreadedExecutorTest.cpp [new file with mode: 0644]
folly/test/Makefile.am

index 0401ce6b1fb3c1c71de8fdba55447621f558b568..ea5cac71965ab028a37a0058df950b5d8ce07b70 100644 (file)
@@ -128,6 +128,7 @@ nobase_follyinclude_HEADERS = \
        Format.h \
        Format-inl.h \
        futures/Deprecated.h \
+       futures/ThreadedExecutor.h \
        futures/DrivableExecutor.h \
        futures/Future-pre.h \
        futures/helpers.h \
@@ -306,6 +307,7 @@ libfolly_la_SOURCES = \
        FileUtil.cpp \
        FingerprintTables.cpp \
        futures/detail/ThreadWheelTimekeeper.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
new file mode 100644 (file)
index 0000000..bfa0db6
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2015 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
new file mode 100644 (file)
index 0000000..f538c41
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright 2015 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
new file mode 100644 (file)
index 0000000..bfc9ec7
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2015 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());
+}
index bbe732e1809e85c48e9226721beda18a9fa29a57..c3a94ba76eb1e27539afaf2311f99fc67dd4c2a3 100644 (file)
@@ -186,6 +186,7 @@ futures_test_SOURCES = \
     ../futures/test/CollectTest.cpp \
     ../futures/test/ContextTest.cpp \
     ../futures/test/CoreTest.cpp \
+    ../futures/test/ThreadedExecutorTest.cpp \
     ../futures/test/EnsureTest.cpp \
     ../futures/test/ExecutorTest.cpp \
     ../futures/test/FSMTest.cpp \