forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ARROW-17975: [C++] Create at-fork facility (apache#14594)
Also migrate the `ThreadPool` class to use the new facility. The `util::GlobalForkSafeMutex` facility is now unused, we may want to remove it in a later PR. Authored-by: Antoine Pitrou <antoine@python.org> Signed-off-by: Antoine Pitrou <antoine@python.org>
- Loading branch information
Showing
10 changed files
with
586 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#include "arrow/util/atfork_internal.h" | ||
|
||
#include <algorithm> | ||
#include <atomic> | ||
#include <mutex> | ||
#include <vector> | ||
|
||
#ifndef _WIN32 | ||
#include <pthread.h> | ||
#endif | ||
|
||
#include "arrow/util/io_util.h" | ||
#include "arrow/util/logging.h" | ||
|
||
namespace arrow { | ||
namespace internal { | ||
|
||
namespace { | ||
|
||
struct RunningHandler { | ||
// A temporary owning copy of a handler, to make sure that a handler | ||
// that runs before fork can still run after fork. | ||
std::shared_ptr<AtForkHandler> handler; | ||
// The token returned by the before-fork handler, to pass to after-fork handlers. | ||
std::any token; | ||
|
||
explicit RunningHandler(std::shared_ptr<AtForkHandler> handler) | ||
: handler(std::move(handler)) {} | ||
}; | ||
|
||
std::mutex g_mutex; | ||
std::vector<std::weak_ptr<AtForkHandler>> g_handlers; | ||
std::vector<RunningHandler> g_handlers_while_forking; | ||
|
||
void MaintainHandlersUnlocked() { | ||
auto it = std::remove_if( | ||
g_handlers.begin(), g_handlers.end(), | ||
[](const std::weak_ptr<AtForkHandler>& ptr) { return ptr.expired(); }); | ||
g_handlers.erase(it, g_handlers.end()); | ||
} | ||
|
||
void BeforeFork() { | ||
// Lock the mutex and keep it locked until the end of AfterForkParent(), | ||
// to avoid multiple concurrent forks and atforks. | ||
g_mutex.lock(); | ||
|
||
DCHECK(g_handlers_while_forking.empty()); // AfterForkParent clears it | ||
|
||
for (const auto& weak_handler : g_handlers) { | ||
if (auto handler = weak_handler.lock()) { | ||
g_handlers_while_forking.emplace_back(std::move(handler)); | ||
} | ||
} | ||
|
||
// XXX can the handler call RegisterAtFork()? | ||
for (auto&& handler : g_handlers_while_forking) { | ||
if (handler.handler->before) { | ||
handler.token = handler.handler->before(); | ||
} | ||
} | ||
} | ||
|
||
void AfterForkParent() { | ||
// The mutex was locked by BeforeFork() | ||
|
||
auto handlers = std::move(g_handlers_while_forking); | ||
g_handlers_while_forking.clear(); | ||
// Execute handlers in reverse order | ||
for (auto it = handlers.rbegin(); it != handlers.rend(); ++it) { | ||
auto&& handler = *it; | ||
if (handler.handler->parent_after) { | ||
handler.handler->parent_after(std::move(handler.token)); | ||
} | ||
} | ||
|
||
g_mutex.unlock(); | ||
// handlers will be destroyed here without the mutex locked, so that | ||
// any action taken by destructors might call RegisterAtFork | ||
} | ||
|
||
void AfterForkChild() { | ||
// Need to reinitialize the mutex as it is probably invalid. Also, the | ||
// old mutex destructor may fail. | ||
// Fortunately, we are a single thread in the child process by now, so no | ||
// additional synchronization is needed. | ||
new (&g_mutex) std::mutex; | ||
|
||
auto handlers = std::move(g_handlers_while_forking); | ||
g_handlers_while_forking.clear(); | ||
// Execute handlers in reverse order | ||
for (auto it = handlers.rbegin(); it != handlers.rend(); ++it) { | ||
auto&& handler = *it; | ||
if (handler.handler->child_after) { | ||
handler.handler->child_after(std::move(handler.token)); | ||
} | ||
} | ||
} | ||
|
||
struct AtForkInitializer { | ||
AtForkInitializer() { | ||
#ifndef _WIN32 | ||
int r = pthread_atfork(&BeforeFork, &AfterForkParent, &AfterForkChild); | ||
if (r != 0) { | ||
IOErrorFromErrno(r, "Error when calling pthread_atfork: ").Abort(); | ||
} | ||
#endif | ||
} | ||
}; | ||
|
||
}; // namespace | ||
|
||
void RegisterAtFork(std::weak_ptr<AtForkHandler> weak_handler) { | ||
static AtForkInitializer initializer; | ||
|
||
std::lock_guard<std::mutex> lock(g_mutex); | ||
MaintainHandlersUnlocked(); | ||
g_handlers.push_back(std::move(weak_handler)); | ||
} | ||
|
||
} // namespace internal | ||
} // namespace arrow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
#pragma once | ||
|
||
#include <any> | ||
#include <functional> | ||
#include <memory> | ||
#include <utility> | ||
|
||
#include "arrow/util/visibility.h" | ||
|
||
namespace arrow { | ||
namespace internal { | ||
|
||
struct ARROW_EXPORT AtForkHandler { | ||
using CallbackBefore = std::function<std::any()>; | ||
using CallbackAfter = std::function<void(std::any)>; | ||
|
||
// The before-fork callback can return an arbitrary token (wrapped in std::any) | ||
// that will passed as-is to after-fork callbacks. This can ensure that any | ||
// resource necessary for after-fork handling is kept alive. | ||
CallbackBefore before; | ||
CallbackAfter parent_after; | ||
CallbackAfter child_after; | ||
|
||
AtForkHandler() = default; | ||
|
||
explicit AtForkHandler(CallbackAfter child_after) | ||
: child_after(std::move(child_after)) {} | ||
|
||
AtForkHandler(CallbackBefore before, CallbackAfter parent_after, | ||
CallbackAfter child_after) | ||
: before(std::move(before)), | ||
parent_after(std::move(parent_after)), | ||
child_after(std::move(child_after)) {} | ||
}; | ||
|
||
// Register the given at-fork handlers. Their intended lifetime should be tracked by | ||
// calling code using an owning shared_ptr. | ||
ARROW_EXPORT | ||
void RegisterAtFork(std::weak_ptr<AtForkHandler>); | ||
|
||
} // namespace internal | ||
} // namespace arrow |
Oops, something went wrong.