|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "arrow/util/atfork_internal.h" |
| 19 | + |
| 20 | +#include <algorithm> |
| 21 | +#include <atomic> |
| 22 | +#include <mutex> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +#ifndef _WIN32 |
| 26 | +#include <pthread.h> |
| 27 | +#endif |
| 28 | + |
| 29 | +#include "arrow/util/io_util.h" |
| 30 | +#include "arrow/util/logging.h" |
| 31 | + |
| 32 | +namespace arrow { |
| 33 | +namespace internal { |
| 34 | + |
| 35 | +namespace { |
| 36 | + |
| 37 | +struct RunningHandler { |
| 38 | + // A temporary owning copy of a handler, to make sure that a handler |
| 39 | + // that runs before fork can still run after fork. |
| 40 | + std::shared_ptr<AtForkHandler> handler; |
| 41 | + // The token returned by the before-fork handler, to pass to after-fork handlers. |
| 42 | + std::any token; |
| 43 | + |
| 44 | + explicit RunningHandler(std::shared_ptr<AtForkHandler> handler) |
| 45 | + : handler(std::move(handler)) {} |
| 46 | +}; |
| 47 | + |
| 48 | +std::mutex g_mutex; |
| 49 | +std::vector<std::weak_ptr<AtForkHandler>> g_handlers; |
| 50 | +std::vector<RunningHandler> g_handlers_while_forking; |
| 51 | + |
| 52 | +void MaintainHandlersUnlocked() { |
| 53 | + auto it = std::remove_if( |
| 54 | + g_handlers.begin(), g_handlers.end(), |
| 55 | + [](const std::weak_ptr<AtForkHandler>& ptr) { return ptr.expired(); }); |
| 56 | + g_handlers.erase(it, g_handlers.end()); |
| 57 | +} |
| 58 | + |
| 59 | +void BeforeFork() { |
| 60 | + // Lock the mutex and keep it locked until the end of AfterForkParent(), |
| 61 | + // to avoid multiple concurrent forks and atforks. |
| 62 | + g_mutex.lock(); |
| 63 | + |
| 64 | + DCHECK(g_handlers_while_forking.empty()); // AfterForkParent clears it |
| 65 | + |
| 66 | + for (const auto& weak_handler : g_handlers) { |
| 67 | + if (auto handler = weak_handler.lock()) { |
| 68 | + g_handlers_while_forking.emplace_back(std::move(handler)); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // XXX can the handler call RegisterAtFork()? |
| 73 | + for (auto&& handler : g_handlers_while_forking) { |
| 74 | + if (handler.handler->before) { |
| 75 | + handler.token = handler.handler->before(); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +void AfterForkParent() { |
| 81 | + // The mutex was locked by BeforeFork() |
| 82 | + |
| 83 | + auto handlers = std::move(g_handlers_while_forking); |
| 84 | + g_handlers_while_forking.clear(); |
| 85 | + // Execute handlers in reverse order |
| 86 | + for (auto it = handlers.rbegin(); it != handlers.rend(); ++it) { |
| 87 | + auto&& handler = *it; |
| 88 | + if (handler.handler->parent_after) { |
| 89 | + handler.handler->parent_after(std::move(handler.token)); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + g_mutex.unlock(); |
| 94 | + // handlers will be destroyed here without the mutex locked, so that |
| 95 | + // any action taken by destructors might call RegisterAtFork |
| 96 | +} |
| 97 | + |
| 98 | +void AfterForkChild() { |
| 99 | + // Need to reinitialize the mutex as it is probably invalid. Also, the |
| 100 | + // old mutex destructor may fail. |
| 101 | + // Fortunately, we are a single thread in the child process by now, so no |
| 102 | + // additional synchronization is needed. |
| 103 | + new (&g_mutex) std::mutex; |
| 104 | + |
| 105 | + auto handlers = std::move(g_handlers_while_forking); |
| 106 | + g_handlers_while_forking.clear(); |
| 107 | + // Execute handlers in reverse order |
| 108 | + for (auto it = handlers.rbegin(); it != handlers.rend(); ++it) { |
| 109 | + auto&& handler = *it; |
| 110 | + if (handler.handler->child_after) { |
| 111 | + handler.handler->child_after(std::move(handler.token)); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +struct AtForkInitializer { |
| 117 | + AtForkInitializer() { |
| 118 | +#ifndef _WIN32 |
| 119 | + int r = pthread_atfork(&BeforeFork, &AfterForkParent, &AfterForkChild); |
| 120 | + if (r != 0) { |
| 121 | + IOErrorFromErrno(r, "Error when calling pthread_atfork: ").Abort(); |
| 122 | + } |
| 123 | +#endif |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +}; // namespace |
| 128 | + |
| 129 | +void RegisterAtFork(std::weak_ptr<AtForkHandler> weak_handler) { |
| 130 | + static AtForkInitializer initializer; |
| 131 | + |
| 132 | + std::lock_guard<std::mutex> lock(g_mutex); |
| 133 | + MaintainHandlersUnlocked(); |
| 134 | + g_handlers.push_back(std::move(weak_handler)); |
| 135 | +} |
| 136 | + |
| 137 | +} // namespace internal |
| 138 | +} // namespace arrow |
0 commit comments