thread initialisation when using with gil/nogil (by Lisandro)
authorStefan Behnel <scoder@users.berlios.de>
Thu, 19 Mar 2009 09:50:40 +0000 (10:50 +0100)
committerStefan Behnel <scoder@users.berlios.de>
Thu, 19 Mar 2009 09:50:40 +0000 (10:50 +0100)
Cython/Compiler/ModuleNode.py
Cython/Compiler/Nodes.py

index 1d5192280a161327091594bc4f5c3799c5219e8b..ddb9b6b399444a4f5d042db07755e5a5ffe94700 100644 (file)
@@ -1604,6 +1604,13 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
         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))
 
index 0cd1e18aebdb957ccd9923c31dd6889b95e591e3..9634b9144017873458d95b40b39ba00b20343b1f 100644 (file)
@@ -1075,6 +1075,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 not lenv.nogil:
@@ -4528,6 +4529,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)
@@ -5621,3 +5623,16 @@ static void __Pyx_ExceptionReset(PyObject *type, PyObject *value, PyObject *tb)
 """)
 
 #------------------------------------------------------------------------------------
+
+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
+""")
+
+#------------------------------------------------------------------------------------