Commit d67aa207 authored by Yang Zhang's avatar Yang Zhang Committed by Facebook Github Bot

add folly/executors/thread_factory/InitThreadFactory

Summary: InitThreadFactory allows running initialization function on newly created threads. This is useful to do preconditioning work on the threads before putting them to use.

Differential Revision: D7947389

fbshipit-source-id: 6a161c887def3862b47473155516c03ba3f10e8b
parent 17f682e4
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <folly/executors/ThreadPoolExecutor.h> #include <folly/executors/ThreadPoolExecutor.h>
#include <folly/executors/task_queue/LifoSemMPMCQueue.h> #include <folly/executors/task_queue/LifoSemMPMCQueue.h>
#include <folly/executors/task_queue/UnboundedBlockingQueue.h> #include <folly/executors/task_queue/UnboundedBlockingQueue.h>
#include <folly/executors/thread_factory/InitThreadFactory.h>
#include <folly/executors/thread_factory/PriorityThreadFactory.h> #include <folly/executors/thread_factory/PriorityThreadFactory.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
...@@ -447,6 +448,18 @@ TEST(PriorityThreadFactoryTest, ThreadPriority) { ...@@ -447,6 +448,18 @@ TEST(PriorityThreadFactoryTest, ThreadPriority) {
EXPECT_EQ(desiredPriority, actualPriority); EXPECT_EQ(desiredPriority, actualPriority);
} }
TEST(InitThreadFactoryTest, InitializerCalled) {
int initializerCalledCount = 0;
InitThreadFactory factory(
std::make_shared<NamedThreadFactory>("test"),
[&initializerCalledCount] { initializerCalledCount++; });
factory
.newThread(
[&initializerCalledCount]() { EXPECT_EQ(initializerCalledCount, 1); })
.join();
EXPECT_EQ(initializerCalledCount, 1);
}
class TestData : public folly::RequestData { class TestData : public folly::RequestData {
public: public:
explicit TestData(int data) : data_(data) {} explicit TestData(int data) : data_(data) {}
......
/*
* Copyright 2018-present 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 <memory>
#include <thread>
#include <folly/executors/thread_factory/ThreadFactory.h>
namespace folly {
class InitThreadFactory : public ThreadFactory {
public:
explicit InitThreadFactory(
std::shared_ptr<ThreadFactory> threadFactory,
Func&& threadInitializer)
: threadFactory_(std::move(threadFactory)),
threadInitializer_(
std::make_shared<Func>(std::move(threadInitializer))) {}
std::thread newThread(Func&& func) override {
return threadFactory_->newThread(
[func = std::move(func), initializer = threadInitializer_]() mutable {
(*initializer)();
func();
});
}
private:
std::shared_ptr<ThreadFactory> threadFactory_;
std::shared_ptr<Func> threadInitializer_;
};
} // namespace folly
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