forkable.cc
Go to the documentation of this file.
1 // Copyright 2022 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 
16 
18 
19 #ifdef GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK
20 
21 #include <pthread.h>
22 
23 #include "absl/container/flat_hash_set.h"
24 
27 
28 namespace grpc_event_engine {
29 namespace experimental {
30 
31 namespace {
33 bool g_registered ABSL_GUARDED_BY(g_mu){false};
36 } // namespace
37 
39 
41 
42 void RegisterForkHandlers() {
43  grpc_core::MutexLock lock(g_mu.get());
44  if (!absl::exchange(g_registered, true)) {
45  pthread_atfork(PrepareFork, PostforkParent, PostforkChild);
46  }
47 };
48 
49 void PrepareFork() {
50  grpc_core::MutexLock lock(g_mu.get());
51  for (auto* forkable : *g_forkables) {
52  forkable->PrepareFork();
53  }
54 }
55 void PostforkParent() {
56  grpc_core::MutexLock lock(g_mu.get());
57  for (auto* forkable : *g_forkables) {
58  forkable->PostforkParent();
59  }
60 }
61 
62 void PostforkChild() {
63  grpc_core::MutexLock lock(g_mu.get());
64  for (auto* forkable : *g_forkables) {
65  forkable->PostforkChild();
66  }
67 }
68 
69 void ManageForkable(Forkable* forkable) {
70  grpc_core::MutexLock lock(g_mu.get());
71  g_forkables->insert(forkable);
72 }
73 
74 void StopManagingForkable(Forkable* forkable) {
75  grpc_core::MutexLock lock(g_mu.get());
76  g_forkables->erase(forkable);
77 }
78 
79 } // namespace experimental
80 } // namespace grpc_event_engine
81 
82 #else // GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK
83 
84 namespace grpc_event_engine {
85 namespace experimental {
86 
89 
91 void PrepareFork() {}
92 void PostforkParent() {}
93 void PostforkChild() {}
94 
95 void ManageForkable(Forkable* /* forkable */) {}
96 void StopManagingForkable(Forkable* /* forkable */) {}
97 
98 } // namespace experimental
99 } // namespace grpc_event_engine
100 
101 #endif // GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK
grpc_event_engine::experimental::RegisterForkHandlers
void RegisterForkHandlers()
Definition: forkable.cc:90
grpc_event_engine::experimental::Forkable::~Forkable
virtual ~Forkable()
Definition: forkable.cc:88
grpc_event_engine::experimental::PostforkChild
void PostforkChild()
Definition: forkable.cc:93
grpc_core::NoDestruct
Definition: no_destruct.h:50
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
ABSL_GUARDED_BY
#define ABSL_GUARDED_BY(x)
Definition: abseil-cpp/absl/base/thread_annotations.h:62
grpc_event_engine::experimental::PostforkParent
void PostforkParent()
Definition: forkable.cc:92
grpc_event_engine::experimental::StopManagingForkable
void StopManagingForkable(Forkable *)
Definition: forkable.cc:96
no_destruct.h
g_mu
static gpr_mu g_mu
Definition: iomgr.cc:55
grpc_event_engine::experimental::Forkable
Definition: forkable.h:40
grpc_event_engine
Definition: endpoint_config.h:24
grpc_event_engine::experimental::PrepareFork
void PrepareFork()
Definition: forkable.cc:91
grpc_event_engine::experimental::Forkable::Forkable
Forkable()
Definition: forkable.cc:87
grpc_event_engine::experimental::ManageForkable
void ManageForkable(Forkable *)
Definition: forkable.cc:95
sync.h
absl::exchange
T exchange(T &obj, U &&new_value)
Definition: abseil-cpp/absl/utility/utility.h:314
forkable.h
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:22