1 // Copyright 2023 The gRPC Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 #include <grpc/support/port_platform.h> 15 #include <stddef.h> 16 17 #include <memory> 18 19 #include "src/core/lib/event_engine/forkable.h" 20 #include "src/core/lib/event_engine/thread_pool/thread_pool.h" 21 #include "src/core/lib/event_engine/thread_pool/work_stealing_thread_pool.h" 22 #include "src/core/util/no_destruct.h" 23 24 namespace grpc_event_engine { 25 namespace experimental { 26 27 namespace { 28 grpc_core::NoDestruct<ObjectGroupForkHandler> g_thread_pool_fork_manager; 29 30 class ThreadPoolForkCallbackMethods { 31 public: Prefork()32 static void Prefork() { g_thread_pool_fork_manager->Prefork(); } PostforkParent()33 static void PostforkParent() { g_thread_pool_fork_manager->PostforkParent(); } PostforkChild()34 static void PostforkChild() { g_thread_pool_fork_manager->PostforkChild(); } 35 }; 36 } // namespace 37 MakeThreadPool(size_t reserve_threads)38std::shared_ptr<ThreadPool> MakeThreadPool(size_t reserve_threads) { 39 auto thread_pool = std::make_shared<WorkStealingThreadPool>(reserve_threads); 40 g_thread_pool_fork_manager->RegisterForkable( 41 thread_pool, ThreadPoolForkCallbackMethods::Prefork, 42 ThreadPoolForkCallbackMethods::PostforkParent, 43 ThreadPoolForkCallbackMethods::PostforkChild); 44 return thread_pool; 45 } 46 47 } // namespace experimental 48 } // namespace grpc_event_engine 49