pytorch/c10/util/DeadlockDetection.cpp
Tristan Rice 2881e0ea17 torch/deadlockdetection: add TORCH_DISABLE_DEADLOCK_DETECTION env for use with torch deploy (#77270)
Summary:
Currently there's an #ifdef USE_DEPLOY to disable deadlock detection in torch for torch deploy. We want to be able to link against binary distributions of PyTorch so we need to have a way to disable deadlock detection at runtime.

55f55a4cf6/torch/csrc/autograd/python_variable.cpp (L1017)

Test Plan: buck test //caffe2/c10/test:util_base_test

Differential Revision: D36303256

Pull Request resolved: https://github.com/pytorch/pytorch/pull/77270
Approved by: https://github.com/PaliC
2022-05-12 00:10:59 +00:00

33 lines
608 B
C++

#include <c10/util/DeadlockDetection.h>
#include <cstdlib>
namespace c10 {
namespace impl {
namespace {
PythonGILHooks* python_gil_hooks = nullptr;
bool disable_detection() {
return std::getenv("TORCH_DISABLE_DEADLOCK_DETECTION") != nullptr;
}
} // namespace
bool check_python_gil() {
if (!python_gil_hooks) {
return false;
}
return python_gil_hooks->check_python_gil();
}
void SetPythonGILHooks(PythonGILHooks* hooks) {
if (disable_detection()) {
return;
}
TORCH_INTERNAL_ASSERT(!hooks || !python_gil_hooks);
python_gil_hooks = hooks;
}
} // namespace impl
} // namespace c10