[Bf-blender-cvs] [5036ac6] master: Partial fix for T37604: Deadlock when stopping rendered viewport (Blender Internal)

IRIE Shinsuke noreply at git.blender.org
Tue Dec 17 08:48:39 CET 2013


Commit: 5036ac6903da222c68aac76bee49006d7e6724c1
Author: IRIE Shinsuke
Date:   Tue Dec 17 18:44:56 2013 +1100
http://developer.blender.org/rB5036ac6903da222c68aac76bee49006d7e6724c1

Partial fix for T37604: Deadlock when stopping rendered viewport (Blender Internal)

- Py_BEGIN_ALLOW_THREADS and Py_END_ALLOW_THREADS macros cannot be used here, because the Py_BEGIN_ALLOW_THREADS causes a crash when quitting Blender.
- The low level function PyEval_ReleaseLock() is used assuming the Python library was built with multi-threads support.

===================================================================

M	source/blender/editors/space_view3d/CMakeLists.txt
M	source/blender/editors/space_view3d/SConscript
M	source/blender/editors/space_view3d/space_view3d.c
M	source/blender/python/BPY_extern.h
M	source/blender/python/intern/bpy_interface.c

===================================================================

diff --git a/source/blender/editors/space_view3d/CMakeLists.txt b/source/blender/editors/space_view3d/CMakeLists.txt
index 51477ec..97c328d 100644
--- a/source/blender/editors/space_view3d/CMakeLists.txt
+++ b/source/blender/editors/space_view3d/CMakeLists.txt
@@ -64,6 +64,11 @@ set(SRC
 	view3d_intern.h
 )
 
+if(WITH_PYTHON)
+	blender_include_dirs(../../python)
+	add_definitions(-DWITH_PYTHON)
+endif()
+
 if(WITH_GAMEENGINE)
 	list(APPEND INC
 		../../../gameengine/BlenderRoutines
diff --git a/source/blender/editors/space_view3d/SConscript b/source/blender/editors/space_view3d/SConscript
index e73aa2d..e6658ab 100644
--- a/source/blender/editors/space_view3d/SConscript
+++ b/source/blender/editors/space_view3d/SConscript
@@ -48,6 +48,10 @@ incs = [
     '../../windowmanager',
     ]
 
+if env['WITH_BF_PYTHON']:
+    incs.append('../../python')
+    defs.append('WITH_PYTHON')
+
 if env['WITH_BF_GAMEENGINE']:
     defs.append('WITH_GAMEENGINE')
 if env['OURPLATFORM'] in ('win32-vc', 'win32-mingw', 'linuxcross', 'win64-vc', 'win64-mingw'):
diff --git a/source/blender/editors/space_view3d/space_view3d.c b/source/blender/editors/space_view3d/space_view3d.c
index 3f7d599..03b8997 100644
--- a/source/blender/editors/space_view3d/space_view3d.c
+++ b/source/blender/editors/space_view3d/space_view3d.c
@@ -69,6 +69,10 @@
 
 #include "UI_resources.h"
 
+#ifdef WITH_PYTHON
+#  include "BPY_extern.h"
+#endif
+
 #include "view3d_intern.h"  /* own include */
 
 /* ******************** manage regions ********************* */
@@ -266,7 +270,16 @@ static void view3d_stop_render_preview(wmWindowManager *wm, ARegion *ar)
 	RegionView3D *rv3d = ar->regiondata;
 
 	if (rv3d->render_engine) {
+#ifdef WITH_PYTHON
+		BPy_BEGIN_ALLOW_THREADS;
+#endif
+
 		WM_jobs_kill_type(wm, ar, WM_JOB_TYPE_RENDER_PREVIEW);
+
+#ifdef WITH_PYTHON
+		BPy_END_ALLOW_THREADS;
+#endif
+
 		if (rv3d->render_engine->re)
 			RE_Database_Free(rv3d->render_engine->re);
 		RE_engine_free(rv3d->render_engine);
diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h
index acb1ab2..697fa91 100644
--- a/source/blender/python/BPY_extern.h
+++ b/source/blender/python/BPY_extern.h
@@ -60,6 +60,19 @@ void BPY_python_start(int argc, const char **argv);
 void BPY_python_end(void);
 void BPY_python_reset(struct bContext *C);
 
+
+/* global interpreter lock */
+
+typedef void *BPy_ThreadStatePtr;
+
+BPy_ThreadStatePtr BPY_thread_save(void);
+void BPY_thread_restore(BPy_ThreadStatePtr tstate);
+
+/* our own wrappers to Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS */
+#define BPy_BEGIN_ALLOW_THREADS { BPy_ThreadStatePtr _bpy_saved_tstate = BPY_thread_save(); (void)0
+#define BPy_END_ALLOW_THREADS BPY_thread_restore(_bpy_saved_tstate); } (void)0
+
+
 /* 2.5 UI Scripts */
 int		BPY_filepath_exec(struct bContext *C, const char *filepath, struct ReportList *reports);
 int		BPY_text_exec(struct bContext *C, struct Text *text, struct ReportList *reports, const bool do_jump);
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c
index 7a3d56d..9fbad95 100644
--- a/source/blender/python/intern/bpy_interface.c
+++ b/source/blender/python/intern/bpy_interface.c
@@ -408,6 +408,31 @@ void BPY_python_reset(bContext *C)
 	BPY_modules_load_user(C);
 }
 
+/* wrapper functions related to global interpreter lock. these functions
+ * are slightly different from the original Python API, don't throw
+ * SIGABRT even if the thread state is NULL. */
+
+/* analogue of PyEval_SaveThread() */
+BPy_ThreadStatePtr BPY_thread_save(void)
+{
+	PyThreadState *tstate = PyThreadState_Swap(NULL);
+	/* note: tstate can be NULL when quitting Blender */
+
+	if (tstate && PyEval_ThreadsInitialized()) {
+		PyEval_ReleaseLock();
+	}
+
+	return (BPy_ThreadStatePtr)tstate;
+}
+
+/* analogue of PyEval_RestoreThread() */
+void BPY_thread_restore(BPy_ThreadStatePtr tstate)
+{
+	if (tstate) {
+		PyEval_RestoreThread((PyThreadState *)tstate);
+	}
+}
+
 static void python_script_error_jump_text(struct Text *text)
 {
 	int lineno;




More information about the Bf-blender-cvs mailing list