1#include <c10/util/DeadlockDetection.h>
2
3#include <cstdlib>
4
5namespace c10 {
6namespace impl {
7
8namespace {
9PythonGILHooks* python_gil_hooks = nullptr;
10
11bool disable_detection() {
12 return std::getenv("TORCH_DISABLE_DEADLOCK_DETECTION") != nullptr;
13}
14} // namespace
15
16bool check_python_gil() {
17 if (!python_gil_hooks) {
18 return false;
19 }
20 return python_gil_hooks->check_python_gil();
21}
22
23void SetPythonGILHooks(PythonGILHooks* hooks) {
24 if (disable_detection()) {
25 return;
26 }
27 TORCH_INTERNAL_ASSERT(!hooks || !python_gil_hooks);
28 python_gil_hooks = hooks;
29}
30
31} // namespace impl
32} // namespace c10
33