Skip to content

Commit 92cf66b

Browse files
author
Roberto De Ioris
committed
smarter gil manager, 20tab#68
1 parent 01e05cb commit 92cf66b

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

‎Source/UnrealEnginePython/Private/UnrealEnginePython.cpp‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,21 @@ bool PyUnicodeOrString_Check(PyObject *py_obj) {
3939

4040
void FUnrealEnginePythonModule::PythonGILRelease() {
4141
#if UEPY_THREADING
42-
if (PyGILState_Check()) {
42+
if (PyGILState_Check() == 1) {
4343
ue_python_gil = PyEval_SaveThread();
4444
}
4545
#endif
4646
}
4747

48-
void FUnrealEnginePythonModule::PythonGILAcquire() {
48+
bool FUnrealEnginePythonModule::PythonGILAcquire() {
4949
#if UEPY_THREADING
50-
if (!PyGILState_Check()) {
50+
if (PyGILState_Check() == 0) {
5151
PyEval_RestoreThread((PyThreadState *)ue_python_gil);
52+
return true;
5253
}
54+
return false;
5355
#endif
56+
return true;
5457
}
5558

5659
void FUnrealEnginePythonModule::StartupModule()

‎Source/UnrealEnginePython/Public/UnrealEnginePython.h‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class UNREALENGINEPYTHON_API FUnrealEnginePythonModule : public IModuleInterface
1111
{
1212
public:
1313

14-
void PythonGILAcquire();
14+
bool PythonGILAcquire();
1515
void PythonGILRelease();
1616

1717
virtual void StartupModule() override;
@@ -32,18 +32,21 @@ struct FScopePythonGIL {
3232
{
3333
#if UEPY_THREADING
3434
UnrealEnginePythonModule = FModuleManager::LoadModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
35-
UnrealEnginePythonModule.PythonGILAcquire();
35+
safeForRelease = UnrealEnginePythonModule.PythonGILAcquire();
3636
#endif
3737
}
3838

3939
~FScopePythonGIL()
4040
{
4141
#if UEPY_THREADING
42-
UnrealEnginePythonModule.PythonGILRelease();
42+
if (safeForRelease) {
43+
UnrealEnginePythonModule.PythonGILRelease();
44+
}
4345
#endif
4446
}
4547

4648
FUnrealEnginePythonModule UnrealEnginePythonModule;
49+
bool safeForRelease;
4750
};
4851

4952

0 commit comments

Comments
 (0)