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