From: Alexander Mordvintsev Date: Wed, 7 Mar 2012 12:46:21 +0000 (+0000) Subject: using RAII to manage GIL -- still fails to handle exceptions correctly X-Git-Tag: accepted/tizen/6.0/unified/20201030.111113~5410 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0e27c772aa88e84979ad2f7686bb7d21b035eb91;p=platform%2Fupstream%2Fopencv.git using RAII to manage GIL -- still fails to handle exceptions correctly --- diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index b4e4611..8255265 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -105,6 +105,22 @@ static inline int* refcountFromPyObject(const PyObject* obj) return (int*)((size_t)obj + REFCOUNT_OFFSET); } +class PyAllowThreads +{ +public: + PyAllowThreads() : _state(PyEval_SaveThread()) + { + //printf("+GIL\n"); + } + ~PyAllowThreads() + { + PyEval_RestoreThread(_state); + //printf("-GIL\n"); + } +private: + PyThreadState* _state; +}; + class NumpyAllocator : public MatAllocator { public: diff --git a/modules/python/src2/gen2.py b/modules/python/src2/gen2.py index a76c519..5eeaaa0 100644 --- a/modules/python/src2/gen2.py +++ b/modules/python/src2/gen2.py @@ -19,9 +19,10 @@ gen_template_parse_args = Template("""const char* keywords[] = { $kw_list, NULL gen_template_func_body = Template("""$code_decl $code_parse { - Py_BEGIN_ALLOW_THREADS - $code_fcall; - Py_END_ALLOW_THREADS + { + PyAllowThreads allow; + $code_fcall; + } $code_ret; } """)