Commit 7cb1a017 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Sara Golemon

ThreadedExecutor.

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
parent 58059b6e
...@@ -128,6 +128,7 @@ nobase_follyinclude_HEADERS = \ ...@@ -128,6 +128,7 @@ nobase_follyinclude_HEADERS = \
Format.h \ Format.h \
Format-inl.h \ Format-inl.h \
futures/Deprecated.h \ futures/Deprecated.h \
futures/ThreadedExecutor.h \
futures/DrivableExecutor.h \ futures/DrivableExecutor.h \
futures/Future-pre.h \ futures/Future-pre.h \
futures/helpers.h \ futures/helpers.h \
...@@ -306,6 +307,7 @@ libfolly_la_SOURCES = \ ...@@ -306,6 +307,7 @@ libfolly_la_SOURCES = \
FileUtil.cpp \ FileUtil.cpp \
FingerprintTables.cpp \ FingerprintTables.cpp \
futures/detail/ThreadWheelTimekeeper.cpp \ futures/detail/ThreadWheelTimekeeper.cpp \
futures/ThreadedExecutor.cpp \
futures/Future.cpp \ futures/Future.cpp \
futures/InlineExecutor.cpp \ futures/InlineExecutor.cpp \
futures/ManualExecutor.cpp \ futures/ManualExecutor.cpp \
......
/*
* 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));
}
}}
/*
* 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;
};
}}
/*
* 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());
}
...@@ -186,6 +186,7 @@ futures_test_SOURCES = \ ...@@ -186,6 +186,7 @@ futures_test_SOURCES = \
../futures/test/CollectTest.cpp \ ../futures/test/CollectTest.cpp \
../futures/test/ContextTest.cpp \ ../futures/test/ContextTest.cpp \
../futures/test/CoreTest.cpp \ ../futures/test/CoreTest.cpp \
../futures/test/ThreadedExecutorTest.cpp \
../futures/test/EnsureTest.cpp \ ../futures/test/EnsureTest.cpp \
../futures/test/ExecutorTest.cpp \ ../futures/test/ExecutorTest.cpp \
../futures/test/FSMTest.cpp \ ../futures/test/FSMTest.cpp \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment