Skip to content

Commit 9ebf836

Browse files
committed
GIL
1 parent c76995b commit 9ebf836

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

unity/native/papi-python/include/PapiData.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,20 @@ struct pesapi_scope__
9494
PyObject* values[SCOPE_FIX_SIZE_VALUES_SIZE];
9595
uint32_t values_used;
9696
eastl::vector<PyObject*, eastl::allocator_malloc>* dynamic_alloc_values = nullptr;
97+
PyThreadState* tstate = nullptr;
9798
PyThreadState *prevThreadState = nullptr;
9899

99100
explicit pesapi_scope__(CppObjectMapper* mapper)
100101
{
101-
prevThreadState = PyThreadState_Swap(mapper->threadState);
102+
printf("ctor pesapi_scope__, p %p\n", this);
102103
this->mapper = mapper;
103104
prev_scope = (pesapi_scope__*)(mapper->getCurrentScope());
105+
if (prev_scope == nullptr)
106+
{
107+
printf("GIL Acquire in pesapi_scope__ ctor\n");
108+
tstate = PyThreadState_New(mapper->threadState->interp);
109+
prevThreadState = PyThreadState_Swap(tstate);
110+
}
104111
mapper->setCurrentScope(this);
105112
values_used = 0;
106113
caught = nullptr;
@@ -154,6 +161,14 @@ struct pesapi_scope__
154161

155162
~pesapi_scope__()
156163
{
164+
printf("dtor pesapi_scope__, p %p\n", this);
165+
if (prev_scope == nullptr)
166+
{
167+
printf("GIL Release in pesapi_scope__ dtor\n");
168+
PyThreadState_Clear(tstate);
169+
PyThreadState_Swap(prevThreadState);
170+
PyThreadState_Delete(tstate);
171+
}
157172
if (caught)
158173
{
159174
caught->~caught_exception_info();
@@ -177,7 +192,6 @@ struct pesapi_scope__
177192
dynamic_alloc_values = nullptr;
178193
}
179194
mapper->setCurrentScope(prev_scope);
180-
PyThreadState_Swap(prevThreadState);
181195
}
182196
};
183197

unity/native/papi-python/source/CppObjectMapperPython.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -970,9 +970,7 @@ void CppObjectMapper::Initialize(PyThreadState *InThreadState)
970970
if (!threadState) {
971971
return;
972972
}
973-
974-
auto prevThreadState = PyThreadState_Swap(threadState);
975-
973+
976974
// Ensure __main__ module exists
977975
PyObject* main_module = PyImport_AddModule("__main__");
978976
if (!main_module) {
@@ -991,10 +989,9 @@ void CppObjectMapper::Initialize(PyThreadState *InThreadState)
991989
PyErr_Clear();
992990
return;
993991
}
994-
992+
995993
PtrClassDef.TypeId = &PtrClassDef;
996994
PtrClassDef.ScriptName = "__Pointer";
997-
PyThreadState_Swap(prevThreadState);
998995
}
999996

1000997
void CppObjectMapper::Cleanup()
@@ -1041,10 +1038,11 @@ pesapi_env_ref create_py_env()
10411038
free(mapper);
10421039
return nullptr;
10431040
}
1044-
1041+
PyThreadState_Swap(threadState);
10451042
new (mapper) pesapi::pythonimpl::CppObjectMapper();
1043+
10461044
mapper->Initialize(threadState);
1047-
1045+
10481046
return pesapi::pythonimpl::g_pesapi_ffi.create_env_ref(reinterpret_cast<pesapi_env>(mapper));
10491047
}
10501048

unity/native/papi-python/source/PapiPythonImpl.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -575,10 +575,6 @@ void pesapi_close_scope(pesapi_scope pscope)
575575
if (!scope)
576576
{
577577
return;
578-
}
579-
if (scope->prev_scope == nullptr)
580-
{
581-
582578
}
583579
scope->~pesapi_scope__();
584580
free(scope);

0 commit comments

Comments
 (0)