2 * Copyright 2017-present Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #include <folly/detail/AtFork.h>
21 #include <folly/Exception.h>
31 folly::Function<void()> prepare;
32 folly::Function<void()> parent;
33 folly::Function<void()> child;
38 static AtForkList& instance() {
39 static auto instance = new AtForkList();
43 static void prepare() noexcept {
44 instance().tasksLock.lock();
45 auto& tasks = instance().tasks;
46 for (auto task = tasks.rbegin(); task != tasks.rend(); ++task) {
51 static void parent() noexcept {
52 auto& tasks = instance().tasks;
53 for (auto& task : tasks) {
56 instance().tasksLock.unlock();
59 static void child() noexcept {
60 auto& tasks = instance().tasks;
61 for (auto& task : tasks) {
64 instance().tasksLock.unlock();
68 std::list<AtForkTask> tasks;
72 #if FOLLY_HAVE_PTHREAD_ATFORK
73 int ret = pthread_atfork(
74 &AtForkList::prepare, &AtForkList::parent, &AtForkList::child);
75 checkPosixError(ret, "pthread_atfork failed");
76 #elif !__ANDROID__ && !defined(_MSC_VER)
77 // pthread_atfork is not part of the Android NDK at least as of n9d. If
78 // something is trying to call native fork() directly at all with Android's
79 // process management model, this is probably the least of the problems.
81 // But otherwise, this is a problem.
82 #warning pthread_atfork unavailable
89 AtForkList::instance();
92 void AtFork::registerHandler(
94 folly::Function<void()> prepare,
95 folly::Function<void()> parent,
96 folly::Function<void()> child) {
97 std::lock_guard<std::mutex> lg(AtForkList::instance().tasksLock);
98 AtForkList::instance().tasks.push_back(
99 {object, std::move(prepare), std::move(parent), std::move(child)});
102 void AtFork::unregisterHandler(void* object) {
103 auto& list = AtForkList::instance();
104 std::lock_guard<std::mutex> lg(list.tasksLock);
105 for (auto it = list.tasks.begin(); it != list.tasks.end(); ++it) {
106 if (it->object == object) {
107 list.tasks.erase(it);
113 } // namespace detail