From 9687773f711a0b5c68e3ff9d60e976cfbf227e1c Mon Sep 17 00:00:00 2001 From: Stefan Behnel Date: Thu, 19 Mar 2009 10:50:40 +0100 Subject: [PATCH] thread initialisation when using with gil/nogil (by Lisandro) --- Cython/Compiler/ModuleNode.py | 7 +++++++ Cython/Compiler/Nodes.py | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index 1d519228..ddb9b6b3 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -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)) diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index 0cd1e18a..9634b914 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -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 +""") + +#------------------------------------------------------------------------------------ -- 2.26.2