Release GIL in function generator

Signed-off-by: Oleksii Moskalenko <moskalenko.alexey@gmail.com>
This commit is contained in:
Oleksii Moskalenko 2022-06-27 18:32:39 -07:00
parent 0b356cbc43
commit d8c8eee594
1 changed files with 11 additions and 0 deletions

View File

@ -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 { if isMethod {
g.gofile.Printf( g.gofile.Printf(
`vifc, __err := gopyh.VarFromHandleTry((gopyh.CGoHandle)(_handle), "%s") `vifc, __err := gopyh.VarFromHandleTry((gopyh.CGoHandle)(_handle), "%s")
@ -407,6 +415,9 @@ if __err != nil {
if rvIsErr || nres == 2 { if rvIsErr || nres == 2 {
g.gofile.Printf("\n") g.gofile.Printf("\n")
// reacquire GIL
g.gofile.Printf("C.PyEval_RestoreThread(_saved_thread)\n")
g.gofile.Printf("if __err != nil {\n") g.gofile.Printf("if __err != nil {\n")
g.gofile.Indent() g.gofile.Indent()
g.gofile.Printf("estr := C.CString(__err.Error())\n") g.gofile.Printf("estr := C.CString(__err.Error())\n")