From 5df653118302eb4a816b0750c149e39d62cb8103 Mon Sep 17 00:00:00 2001
From: David Gobbi <david.gobbi@gmail.com>
Date: Sun, 4 Dec 2022 19:28:02 -0700
Subject: [PATCH] Add VTK_PYTHON_FULL_THREADSAFE as option

The VTK_PYTHON_FULL_THREADSAFE variable has existed since 2015, but
so far has only been a cmake option in Paraview, never in VTK itself.
Since this option adds the GIL locks that are required for any
Python concurrency via PyEval_SaveThread()/PyEval_RestoreThread(),
it should be useful within a broad range of VTK projects.

The VTK_NO_PYTHON_THREADS option is an older option that was available
in VTK 5 but disappeared in VTK 6, though it still appeared in the C++
source files and in Paraview's CMakeLists.txt.
---
 CMakeLists.txt                                          | 8 ++++++++
 Documentation/docs/build_instructions/build_settings.md | 4 ++++
 Documentation/release/dev/python-thread-options.md      | 8 ++++++++
 Utilities/Python/vtkPython.h                            | 2 +-
 Utilities/Python/vtkPythonConfigure.h.in                | 4 +++-
 5 files changed, 24 insertions(+), 2 deletions(-)
 create mode 100644 Documentation/release/dev/python-thread-options.md

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 16df55a4590..1b317e421e6 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -154,6 +154,14 @@ option(VTK_USE_MPI "Support MPI" OFF)
 option(VTK_SERIAL_TESTS_USE_MPIEXEC "Used on HPC to run serial tests on compute nodes" OFF)
 mark_as_advanced(VTK_SERIAL_TESTS_USE_MPIEXEC)
 
+# For platforms that don't support Python threads (do any exist?)
+option(VTK_NO_PYTHON_THREADS "Disable Python Threads support" OFF)
+mark_as_advanced(VTK_NO_PYTHON_THREADS)
+# This setting must be ON for Python concurrency
+cmake_dependent_option(VTK_PYTHON_FULL_THREADSAFE "Wrap all Python calls with the GIL" ON
+  "NOT VTK_NO_PYTHON_THREADS" OFF)
+mark_as_advanced(VTK_PYTHON_FULL_THREADSAFE)
+
 #-----------------------------------------------------------------------------
 # Add an option to enable memkind
 if (UNIX AND NOT APPLE)
diff --git a/Documentation/docs/build_instructions/build_settings.md b/Documentation/docs/build_instructions/build_settings.md
index 485cbfb2e1f..c14810a5180 100644
--- a/Documentation/docs/build_instructions/build_settings.md
+++ b/Documentation/docs/build_instructions/build_settings.md
@@ -176,6 +176,10 @@ More advanced options:
     enable Tkinter support for VTK widgets.
   * `VTK_BUILD_COMPILE_TOOLS_ONLY` (default `OFF`): If set, VTK will compile
     just its compile tools for use in a cross-compile build.
+  * `VTK_NO_PYTHON_THREADS` (default `OFF`): If set, then all Python threading
+    in VTK will be disabled.
+  * `VTK_PYTHON_FULL_THREADSAFE` (default `ON`): If set, lock the Python GIL
+    for Python C API calls, to make it safe to allow Python thread concurrency.
   * `VTK_SERIAL_TESTS_USE_MPIEXEC` (default `OFF`): Used on HPC to run
     serial tests on compute nodes. If set, it prefixes serial tests with
     "${MPIEXEC_EXECUTABLE}" "${MPIEXEC_NUMPROC_FLAG}" "1" ${MPIEXEC_PREFLAGS}
diff --git a/Documentation/release/dev/python-thread-options.md b/Documentation/release/dev/python-thread-options.md
new file mode 100644
index 00000000000..45d2d692e0f
--- /dev/null
+++ b/Documentation/release/dev/python-thread-options.md
@@ -0,0 +1,8 @@
+## Add Python Thread Options to CMake Config
+
+Two options that have previously only been available in ParaView are
+now available in VTK. `VTK_PYTHON_FULL_THREADSAFE` locks the GIL around
+Python C API calls in VTK C++ methods, allowing these methods to safely be
+called concurrently from multiply Python threads. `VTK_NO_PYTHON_THREADS`
+disables all Python threading to allow VTK to run on platforms that do
+not support Python threading.
diff --git a/Utilities/Python/vtkPython.h b/Utilities/Python/vtkPython.h
index 2fe0ac96a94..8213c4c935c 100644
--- a/Utilities/Python/vtkPython.h
+++ b/Utilities/Python/vtkPython.h
@@ -90,7 +90,7 @@ they are system headers.  Do NOT add any #undef lines here.  */
 #undef toupper
 #endif
 
-/* This logic is borrowed from mpi4py/vtkmpi4py/src/atimport.h */
+/* This logic is borrowed from mpi4py/vtkmpi4py/src/pycompat.h */
 #ifdef VTK_NO_PYTHON_THREADS
 #undef PyGILState_Ensure
 #define PyGILState_Ensure() ((PyGILState_STATE)0)
diff --git a/Utilities/Python/vtkPythonConfigure.h.in b/Utilities/Python/vtkPythonConfigure.h.in
index fc4743a07bc..6de3a1415e6 100644
--- a/Utilities/Python/vtkPythonConfigure.h.in
+++ b/Utilities/Python/vtkPythonConfigure.h.in
@@ -5,8 +5,10 @@
 
 /* This header is configured by VTK's build process.  */
 
-/* E.g. on BlueGene and Cray there is no multithreading */
+/* For platforms that don't support Python threads */
 #cmakedefine VTK_NO_PYTHON_THREADS
+
+/* This adds the locks that allow Python thread concurrency */
 #cmakedefine VTK_PYTHON_FULL_THREADSAFE
 
 /* Whether the real python debug library has been provided.  */
-- 
GitLab