Some time ago, I've posted some mails about thread issues in Python
2.3. Unfortunately, those post did not receive any attention.
Currently, mpi4py on Python 2.3 makes the interpreter segfault. I
could easily fix this issue myself for mpi4py, but perhaps it do make
sense to alleviate other Py2.3 users of this really low-level issue.
The problem: If at the point PyGILState_Release() is called, the GIL
whas not yet created, then the Python (2.3) interpreter segfautls.
The solution: Call PyEval_InitThread() in the module init function.
But this could have performance impacts for non threaded applications
using a Cython-generated extension module, so this thread
initialization should be done only if needed. A simple approach is to
call PyEval_InitThread() only if the Cython module ever
released/aquired the GIL
The attached patch implement all the ideas discussed above. Any chance
to this being accepted? Any better idea? Note that PyEval_InitThread()
is a no-op if the GIL is already created.
--
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594
diff -r 64bf6d68a46b Cython/Compiler/ModuleNode.py
--- a/Cython/Compiler/ModuleNode.py Wed Oct 29 12:40:31 2008 -0300
+++ b/Cython/Compiler/ModuleNode.py Wed Oct 29 12:42:45 2008 -0300
@@ -1565,6 +1565,13 @@ class ModuleNode(Nodes.Node, Nodes.Block
env.generate_library_function_declarations(code)
self.generate_filename_init_call(code)
+ code.putln("/*--- Threads initialization code ---*/")
+ code.putln("#if defined(__PYX_FORCE_INIT_THREADS) && __PYX_FORCE_INIT_THREADS")
+ code.putln("#ifdef WITH_THREAD")
+ code.putln("PyEval_InitThreads();")
+ code.putln("#endif")
+ code.putln("#endif")
+
code.putln("/*--- Initialize various global constants etc. ---*/")
code.putln(code.error_goto_if_neg("__Pyx_InitGlobals()", self.pos))
diff -r 64bf6d68a46b Cython/Compiler/Nodes.py
--- a/Cython/Compiler/Nodes.py Wed Oct 29 12:40:31 2008 -0300
+++ b/Cython/Compiler/Nodes.py Wed Oct 29 12:43:15 2008 -0300
@@ -985,6 +985,7 @@ class FuncDefNode(StatNode, BlockNode):
# ----- GIL acquisition
acquire_gil = self.need_gil_acquisition(lenv)
if acquire_gil:
+ env.use_utility_code(py23_init_threads_utility_code)
code.putln("PyGILState_STATE _save = PyGILState_Ensure();")
# ----- Automatic lead-ins for certain special functions
if is_getbuffer_slot:
@@ -4222,6 +4223,7 @@ class GILStatNode(TryFinallyStatNode):
finally_clause = GILExitNode(pos, state = state))
def analyse_expressions(self, env):
+ env.use_utility_code(py23_init_threads_utility_code)
was_nogil = env.nogil
env.nogil = 1
TryFinallyStatNode.analyse_expressions(self, env)
@@ -5215,3 +5217,16 @@ static void __Pyx_ExceptionReset(PyObjec
""")
#------------------------------------------------------------------------------------
+
+py23_init_threads_utility_code = UtilityCode(
+proto="""
+#ifndef __PYX_FORCE_INIT_THREADS
+#define __PYX_FORCE_INIT_THREADS 0
+#if PY_VERSION_HEX < 0x02040000
+#undef __PYX_FORCE_INIT_THREADS
+#define __PYX_FORCE_INIT_THREADS 1
+#endif
+#endif
+""")
+
+#------------------------------------------------------------------------------------
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev