| 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 |
|
| 15 |
#include <grpc/support/port_platform.h> |
| 16 |
|
| 17 |
#include "src/core/lib/event_engine/forkable.h" |
| 18 |
|
| 19 |
#ifdef GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK |
| 20 |
|
| 21 |
#include <pthread.h> |
| 22 |
|
| 23 |
#include "absl/container/flat_hash_set.h" |
| 24 |
|
| 25 |
#include "src/core/lib/gprpp/no_destruct.h" |
| 26 |
#include "src/core/lib/gprpp/sync.h" |
| 27 |
|
| 28 |
namespace grpc_event_engine { |
| 29 |
namespace experimental { |
| 30 |
|
| 31 |
namespace { |
| 32 |
grpc_core::NoDestruct<grpc_core::Mutex> g_mu; |
| 33 |
bool g_registered ABSL_GUARDED_BY(g_mu){false}; |
| 34 |
grpc_core::NoDestruct<absl::flat_hash_set<Forkable*>> g_forkables |
| 35 |
ABSL_GUARDED_BY(g_mu); |
| 36 |
} // namespace |
| 37 |
|
| 38 |
Forkable::Forkable() { ManageForkable(this); } |
| 39 |
|
| 40 |
Forkable::~Forkable() { StopManagingForkable(this); } |
| 41 |
|
| 42 |
void RegisterForkHandlers() { |
| 43 |
grpc_core::MutexLock lock(g_mu.get()); |
| 44 |
if (!std::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 |
|
| 87 |
Forkable::Forkable() {} |
| 88 |
Forkable::~Forkable() {} |
| 89 |
|
| 90 |
void RegisterForkHandlers() {} |
| 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 |