| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2015 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef GRPC_CORE_LIB_EVENT_ENGINE_THREAD_POOL_H |
| 20 |
#define GRPC_CORE_LIB_EVENT_ENGINE_THREAD_POOL_H |
| 21 |
|
| 22 |
#include <grpc/support/port_platform.h> |
| 23 |
|
| 24 |
#include <stdint.h> |
| 25 |
|
| 26 |
#include <atomic> |
| 27 |
#include <memory> |
| 28 |
#include <queue> |
| 29 |
|
| 30 |
#include "absl/base/thread_annotations.h" |
| 31 |
#include "absl/functional/any_invocable.h" |
| 32 |
|
| 33 |
#include <grpc/event_engine/event_engine.h> |
| 34 |
#include <grpc/support/cpu.h> |
| 35 |
|
| 36 |
#include "src/core/lib/event_engine/executor/executor.h" |
| 37 |
#include "src/core/lib/event_engine/forkable.h" |
| 38 |
#include "src/core/lib/gpr/useful.h" |
| 39 |
#include "src/core/lib/gprpp/sync.h" |
| 40 |
|
| 41 |
namespace grpc_event_engine { |
| 42 |
namespace experimental { |
| 43 |
|
| 44 |
class ThreadPool final : public Forkable, public Executor { |
| 45 |
public: |
| 46 |
ThreadPool(); |
| 47 |
// Asserts Quiesce was called. |
| 48 |
~ThreadPool() override; |
| 49 |
|
| 50 |
void Quiesce(); |
| 51 |
|
| 52 |
// Run must not be called after Quiesce completes |
| 53 |
void Run(absl::AnyInvocable<void()> callback) override; |
| 54 |
void Run(EventEngine::Closure* closure) override; |
| 55 |
|
| 56 |
// Forkable |
| 57 |
// Ensures that the thread pool is empty before forking. |
| 58 |
void PrepareFork() override; |
| 59 |
void PostforkParent() override; |
| 60 |
void PostforkChild() override; |
| 61 |
|
| 62 |
private: |
| 63 |
class Queue { |
| 64 |
public: |
| 65 |
explicit Queue(unsigned reserve_threads) |
| 66 |
: reserve_threads_(reserve_threads) {} |
| 67 |
bool Step(); |
| 68 |
void SetShutdown() { SetState(State::kShutdown); } |
| 69 |
void SetForking() { SetState(State::kForking); } |
| 70 |
// Add a callback to the queue. |
| 71 |
// Return true if we should also spin up a new thread. |
| 72 |
bool Add(absl::AnyInvocable<void()> callback); |
| 73 |
void Reset() { SetState(State::kRunning); } |
| 74 |
bool IsBacklogged(); |
| 75 |
void SleepIfRunning(); |
| 76 |
|
| 77 |
private: |
| 78 |
enum class State { kRunning, kShutdown, kForking }; |
| 79 |
|
| 80 |
void SetState(State state); |
| 81 |
|
| 82 |
grpc_core::Mutex mu_; |
| 83 |
grpc_core::CondVar cv_; |
| 84 |
std::queue<absl::AnyInvocable<void()>> callbacks_ ABSL_GUARDED_BY(mu_); |
| 85 |
unsigned threads_waiting_ ABSL_GUARDED_BY(mu_) = 0; |
| 86 |
const unsigned reserve_threads_; |
| 87 |
State state_ ABSL_GUARDED_BY(mu_) = State::kRunning; |
| 88 |
}; |
| 89 |
|
| 90 |
class ThreadCount { |
| 91 |
public: |
| 92 |
void Add(); |
| 93 |
void Remove(); |
| 94 |
void BlockUntilThreadCount(int threads, const char* why); |
| 95 |
|
| 96 |
private: |
| 97 |
grpc_core::Mutex mu_; |
| 98 |
grpc_core::CondVar cv_; |
| 99 |
int threads_ ABSL_GUARDED_BY(mu_) = 0; |
| 100 |
}; |
| 101 |
|
| 102 |
struct State { |
| 103 |
explicit State(int reserve_threads) : queue(reserve_threads) {} |
| 104 |
Queue queue; |
| 105 |
ThreadCount thread_count; |
| 106 |
// After pool creation we use this to rate limit creation of threads to one |
| 107 |
// at a time. |
| 108 |
std::atomic<bool> currently_starting_one_thread{false}; |
| 109 |
std::atomic<uint64_t> last_started_thread{0}; |
| 110 |
}; |
| 111 |
|
| 112 |
using StatePtr = std::shared_ptr<State>; |
| 113 |
|
| 114 |
enum class StartThreadReason { |
| 115 |
kInitialPool, |
| 116 |
kNoWaitersWhenScheduling, |
| 117 |
kNoWaitersWhenFinishedStarting, |
| 118 |
}; |
| 119 |
|
| 120 |
static void ThreadFunc(StatePtr state); |
| 121 |
// Start a new thread; throttled indicates whether the State::starting_thread |
| 122 |
// variable is being used to throttle this threads creation against others or |
| 123 |
// not: at thread pool startup we start several threads concurrently, but |
| 124 |
// after that we only start one at a time. |
| 125 |
static void StartThread(StatePtr state, StartThreadReason reason); |
| 126 |
void Postfork(); |
| 127 |
|
| 128 |
const unsigned reserve_threads_ = |
| 129 |
grpc_core::Clamp(gpr_cpu_num_cores(), 2u, 32u); |
| 130 |
const StatePtr state_ = std::make_shared<State>(reserve_threads_); |
| 131 |
std::atomic<bool> quiesced_{false}; |
| 132 |
}; |
| 133 |
|
| 134 |
} // namespace experimental |
| 135 |
} // namespace grpc_event_engine |
| 136 |
|
| 137 |
#endif // GRPC_CORE_LIB_EVENT_ENGINE_THREAD_POOL_H |