From d8c8eee59492d14a080dd0d42b2a81b12f18baf2 Mon Sep 17 00:00:00 2001 From: Oleksii Moskalenko Date: Mon, 27 Jun 2022 18:32:39 -0700 Subject: [PATCH] Release GIL in function generator Signed-off-by: Oleksii Moskalenko --- bind/gen_func.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/bind/gen_func.go b/bind/gen_func.go index adcffe1..8f2e606 100644 --- a/bind/gen_func.go +++ b/bind/gen_func.go @@ -260,6 +260,14 @@ func (g *pyGen) genFuncBody(sym *symbol, fsym *Func) { } } } + + // release GIL + g.gofile.Printf("_saved_thread := C.PyEval_SaveThread()\n") + if !rvIsErr && nres != 2 { + // reacquire GIL after return + g.gofile.Printf("defer C.PyEval_RestoreThread(_saved_thread)\n") + } + if isMethod { g.gofile.Printf( `vifc, __err := gopyh.VarFromHandleTry((gopyh.CGoHandle)(_handle), "%s") @@ -407,6 +415,9 @@ if __err != nil { if rvIsErr || nres == 2 { g.gofile.Printf("\n") + // reacquire GIL + g.gofile.Printf("C.PyEval_RestoreThread(_saved_thread)\n") + g.gofile.Printf("if __err != nil {\n") g.gofile.Indent() g.gofile.Printf("estr := C.CString(__err.Error())\n")