From 7cb1a017ec58faab02a36a459814d21f34dbb093 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Wed, 8 Jul 2015 17:54:16 -0700 Subject: [PATCH] ThreadedExecutor. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 | 2 + folly/futures/ThreadedExecutor.cpp | 39 ++++++++++++++++++++ folly/futures/ThreadedExecutor.h | 41 +++++++++++++++++++++ folly/futures/test/ThreadedExecutorTest.cpp | 37 +++++++++++++++++++ folly/test/Makefile.am | 1 + 5 files changed, 120 insertions(+) create mode 100644 folly/futures/ThreadedExecutor.cpp create mode 100644 folly/futures/ThreadedExecutor.h create mode 100644 folly/futures/test/ThreadedExecutorTest.cpp diff --git a/folly/Makefile.am b/folly/Makefile.am index 0401ce6b..ea5cac71 100644 --- a/folly/Makefile.am +++ b/folly/Makefile.am @@ -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 index 00000000..bfa0db61 --- /dev/null +++ b/folly/futures/ThreadedExecutor.cpp @@ -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 + +#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 new file mode 100644 index 00000000..f538c419 --- /dev/null +++ b/folly/futures/ThreadedExecutor.h @@ -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 +#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 new file mode 100644 index 00000000..bfc9ec7f --- /dev/null +++ b/folly/futures/test/ThreadedExecutorTest.cpp @@ -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 +#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()); +} diff --git a/folly/test/Makefile.am b/folly/test/Makefile.am index bbe732e1..c3a94ba7 100644 --- a/folly/test/Makefile.am +++ b/folly/test/Makefile.am @@ -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 \ -- 2.34.1