Skip to content

Commit 73e4ea5

Browse files
author
Roberto De Ioris
committed
more threading fixes
1 parent c3fa588 commit 73e4ea5

File tree

6 files changed

+253
-174
lines changed

6 files changed

+253
-174
lines changed

Source/UnrealEnginePython/Private/Slate/UEPySWindow.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ static PyObject *py_ue_swindow_add_modal(ue_PySWindow *self, PyObject * args)
107107
{
108108
parent_window = FModuleManager::LoadModuleChecked<IMainFrameModule>("MainFrame").GetParentWindow();
109109
}
110+
Py_BEGIN_ALLOW_THREADS;
110111
FSlateApplication::Get().AddModalWindow(StaticCastSharedRef<SWindow>(py_SWindow->AsShared()), parent_window, false);
112+
Py_END_ALLOW_THREADS;
111113
Py_RETURN_NONE;
112114
}
113115
#endif

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
780780
{ "get_instanced_foliage_actor_for_current_level", (PyCFunction)py_ue_get_instanced_foliage_actor_for_current_level, METH_VARARGS, "" },
781781
{ "get_instanced_foliage_actor_for_level", (PyCFunction)py_ue_get_instanced_foliage_actor_for_level, METH_VARARGS, "" },
782782
{ "get_foliage_types", (PyCFunction)py_ue_get_foliage_types, METH_VARARGS, "" },
783-
783+
784784

785785
{ "add_actor_component", (PyCFunction)py_ue_add_actor_component, METH_VARARGS, "" },
786786
{ "add_instance_component", (PyCFunction)py_ue_add_instance_component, METH_VARARGS, "" },
@@ -2052,11 +2052,15 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
20522052
return false;
20532053
if (PyObject_IsTrue(py_obj))
20542054
{
2055+
Py_BEGIN_ALLOW_THREADS;
20552056
casted_prop->SetPropertyValue_InContainer(buffer, true, index);
2057+
Py_END_ALLOW_THREADS;
20562058
}
20572059
else
20582060
{
2061+
Py_BEGIN_ALLOW_THREADS;
20592062
casted_prop->SetPropertyValue_InContainer(buffer, false, index);
2063+
Py_END_ALLOW_THREADS;
20602064
}
20612065
return true;
20622066
}
@@ -2066,42 +2070,54 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
20662070
if (auto casted_prop = Cast<UIntProperty>(prop))
20672071
{
20682072
PyObject *py_long = PyNumber_Long(py_obj);
2073+
Py_BEGIN_ALLOW_THREADS;
20692074
casted_prop->SetPropertyValue_InContainer(buffer, PyLong_AsLong(py_long), index);
2075+
Py_END_ALLOW_THREADS;
20702076
Py_DECREF(py_long);
20712077
return true;
20722078
}
20732079
if (auto casted_prop = Cast<UUInt32Property>(prop))
20742080
{
20752081
PyObject *py_long = PyNumber_Long(py_obj);
2082+
Py_BEGIN_ALLOW_THREADS;
20762083
casted_prop->SetPropertyValue_InContainer(buffer, PyLong_AsUnsignedLong(py_long), index);
2084+
Py_END_ALLOW_THREADS;
20772085
Py_DECREF(py_long);
20782086
return true;
20792087
}
20802088
if (auto casted_prop = Cast<UInt64Property>(prop))
20812089
{
20822090
PyObject *py_long = PyNumber_Long(py_obj);
2091+
Py_BEGIN_ALLOW_THREADS;
20832092
casted_prop->SetPropertyValue_InContainer(buffer, PyLong_AsLongLong(py_long), index);
2093+
Py_END_ALLOW_THREADS;
20842094
Py_DECREF(py_long);
20852095
return true;
20862096
}
20872097
if (auto casted_prop = Cast<UUInt64Property>(prop))
20882098
{
20892099
PyObject *py_long = PyNumber_Long(py_obj);
2100+
Py_BEGIN_ALLOW_THREADS;
20902101
casted_prop->SetPropertyValue_InContainer(buffer, PyLong_AsUnsignedLongLong(py_long), index);
2102+
Py_END_ALLOW_THREADS;
20912103
Py_DECREF(py_long);
20922104
return true;
20932105
}
20942106
if (auto casted_prop = Cast<UFloatProperty>(prop))
20952107
{
20962108
PyObject *py_float = PyNumber_Float(py_obj);
2109+
Py_BEGIN_ALLOW_THREADS;
20972110
casted_prop->SetPropertyValue_InContainer(buffer, PyFloat_AsDouble(py_float), index);
2111+
Py_END_ALLOW_THREADS;
20982112
Py_DECREF(py_float);
20992113
return true;
21002114
}
21012115
if (auto casted_prop = Cast<UByteProperty>(prop))
21022116
{
21032117
PyObject *py_long = PyNumber_Long(py_obj);
2118+
Py_BEGIN_ALLOW_THREADS;
21042119
casted_prop->SetPropertyValue_InContainer(buffer, PyLong_AsUnsignedLong(py_long), index);
2120+
Py_END_ALLOW_THREADS;
21052121
Py_DECREF(py_long);
21062122
return true;
21072123
}
@@ -2110,7 +2126,9 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
21102126
{
21112127
PyObject *py_long = PyNumber_Long(py_obj);
21122128
void *prop_addr = casted_prop->ContainerPtrToValuePtr<void>(buffer, index);
2129+
Py_BEGIN_ALLOW_THREADS;
21132130
casted_prop->GetUnderlyingProperty()->SetIntPropertyValue(prop_addr, (uint64)PyLong_AsUnsignedLong(py_long));
2131+
Py_END_ALLOW_THREADS;
21142132
Py_DECREF(py_long);
21152133
return true;
21162134
}
@@ -2124,17 +2142,23 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
21242142
{
21252143
if (auto casted_prop = Cast<UStrProperty>(prop))
21262144
{
2145+
Py_BEGIN_ALLOW_THREADS;
21272146
casted_prop->SetPropertyValue_InContainer(buffer, UTF8_TO_TCHAR(PyUnicode_AsUTF8(py_obj)), index);
2147+
Py_END_ALLOW_THREADS;
21282148
return true;
21292149
}
21302150
if (auto casted_prop = Cast<UNameProperty>(prop))
21312151
{
2152+
Py_BEGIN_ALLOW_THREADS;
21322153
casted_prop->SetPropertyValue_InContainer(buffer, UTF8_TO_TCHAR(PyUnicode_AsUTF8(py_obj)), index);
2154+
Py_END_ALLOW_THREADS;
21332155
return true;
21342156
}
21352157
if (auto casted_prop = Cast<UTextProperty>(prop))
21362158
{
2159+
Py_BEGIN_ALLOW_THREADS;
21372160
casted_prop->SetPropertyValue_InContainer(buffer, FText::FromString(UTF8_TO_TCHAR(PyUnicode_AsUTF8(py_obj))), index);
2161+
Py_END_ALLOW_THREADS;
21382162
return true;
21392163
}
21402164
return false;
@@ -2150,7 +2174,9 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
21502174
{
21512175

21522176
Py_ssize_t pybytes_len = PyBytes_Size(py_obj);
2177+
uint8 *buf = (uint8 *)PyBytes_AsString(py_obj);
21532178

2179+
Py_BEGIN_ALLOW_THREADS;
21542180
// fix array helper size
21552181
if (helper.Num() < pybytes_len)
21562182
{
@@ -2161,8 +2187,9 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
21612187
helper.RemoveValues(pybytes_len, helper.Num() - pybytes_len);
21622188
}
21632189

2164-
uint8 *buf = (uint8 *)PyBytes_AsString(py_obj);
2190+
21652191
FMemory::Memcpy(helper.GetRawPtr(), buf, pybytes_len);
2192+
Py_END_ALLOW_THREADS;
21662193
return true;
21672194
}
21682195
}
@@ -2180,7 +2207,9 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
21802207
{
21812208

21822209
Py_ssize_t pybytes_len = PyByteArray_Size(py_obj);
2210+
uint8 *buf = (uint8 *)PyByteArray_AsString(py_obj);
21832211

2212+
Py_BEGIN_ALLOW_THREADS;
21842213
// fix array helper size
21852214
if (helper.Num() < pybytes_len)
21862215
{
@@ -2191,8 +2220,9 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
21912220
helper.RemoveValues(pybytes_len, helper.Num() - pybytes_len);
21922221
}
21932222

2194-
uint8 *buf = (uint8 *)PyByteArray_AsString(py_obj);
2223+
21952224
FMemory::Memcpy(helper.GetRawPtr(), buf, pybytes_len);
2225+
Py_END_ALLOW_THREADS;
21962226
return true;
21972227
}
21982228
}
@@ -2311,7 +2341,9 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
23112341
{
23122342
if (casted_prop->Struct == TBaseStructure<FVector>::Get())
23132343
{
2344+
Py_BEGIN_ALLOW_THREADS;
23142345
*casted_prop->ContainerPtrToValuePtr<FVector>(buffer, index) = py_vec->vec;
2346+
Py_END_ALLOW_THREADS;
23152347
return true;
23162348
}
23172349
}
@@ -2324,7 +2356,9 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
23242356
{
23252357
if (casted_prop->Struct == TBaseStructure<FRotator>::Get())
23262358
{
2359+
Py_BEGIN_ALLOW_THREADS;
23272360
*casted_prop->ContainerPtrToValuePtr<FRotator>(buffer, index) = py_rot->rot;
2361+
Py_END_ALLOW_THREADS;
23282362
return true;
23292363
}
23302364
}
@@ -2337,7 +2371,9 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
23372371
{
23382372
if (casted_prop->Struct == TBaseStructure<FTransform>::Get())
23392373
{
2374+
Py_BEGIN_ALLOW_THREADS;
23402375
*casted_prop->ContainerPtrToValuePtr<FTransform>(buffer, index) = py_transform->transform;
2376+
Py_END_ALLOW_THREADS;
23412377
return true;
23422378
}
23432379
}
@@ -2350,7 +2386,9 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
23502386
{
23512387
if (casted_prop->Struct == TBaseStructure<FColor>::Get())
23522388
{
2389+
Py_BEGIN_ALLOW_THREADS;
23532390
*casted_prop->ContainerPtrToValuePtr<FColor>(buffer, index) = py_color->color;
2391+
Py_END_ALLOW_THREADS;
23542392
return true;
23552393
}
23562394
}
@@ -2363,7 +2401,9 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
23632401
{
23642402
if (casted_prop->Struct == TBaseStructure<FLinearColor>::Get())
23652403
{
2404+
Py_BEGIN_ALLOW_THREADS;
23662405
*casted_prop->ContainerPtrToValuePtr<FLinearColor>(buffer, index) = py_color->color;
2406+
Py_END_ALLOW_THREADS;
23672407
return true;
23682408
}
23692409
}
@@ -2376,7 +2416,9 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
23762416
{
23772417
if (casted_prop->Struct == FHitResult::StaticStruct())
23782418
{
2419+
Py_BEGIN_ALLOW_THREADS;
23792420
*casted_prop->ContainerPtrToValuePtr<FHitResult>(buffer, index) = py_hit->hit;
2421+
Py_END_ALLOW_THREADS;
23802422
return true;
23812423
}
23822424
}
@@ -2391,8 +2433,10 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
23912433
{
23922434
if (casted_prop->Struct == py_u_struct->u_struct)
23932435
{
2436+
Py_BEGIN_ALLOW_THREADS;
23942437
uint8 *dest = casted_prop->ContainerPtrToValuePtr<uint8>(buffer, index);
23952438
FMemory::Memcpy(dest, py_u_struct->data, py_u_struct->u_struct->GetStructureSize());
2439+
Py_END_ALLOW_THREADS;
23962440
return true;
23972441
}
23982442
}
@@ -2406,30 +2450,40 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
24062450
{
24072451
if (auto casted_prop = Cast<UClassProperty>(prop))
24082452
{
2453+
Py_BEGIN_ALLOW_THREADS;
24092454
casted_prop->SetPropertyValue_InContainer(buffer, ue_obj->ue_object, index);
2455+
Py_END_ALLOW_THREADS;
24102456
return true;
24112457
}
24122458
else if (auto casted_prop_soft_class = Cast<USoftClassProperty>(prop))
24132459
{
2460+
Py_BEGIN_ALLOW_THREADS;
24142461
casted_prop_soft_class->SetPropertyValue_InContainer(buffer, FSoftObjectPtr(ue_obj->ue_object), index);
2462+
Py_END_ALLOW_THREADS;
24152463
return true;
24162464
}
24172465
else if (auto casted_prop_soft_object = Cast<USoftObjectProperty>(prop))
24182466
{
2467+
Py_BEGIN_ALLOW_THREADS;
24192468
casted_prop_soft_object->SetPropertyValue_InContainer(buffer, FSoftObjectPtr(ue_obj->ue_object), index);
2469+
Py_END_ALLOW_THREADS;
24202470
return true;
24212471
}
24222472
else if (auto casted_prop_weak_object = Cast<UWeakObjectProperty>(prop))
24232473
{
2474+
Py_BEGIN_ALLOW_THREADS;
24242475
casted_prop_weak_object->SetPropertyValue_InContainer(buffer, FWeakObjectPtr(ue_obj->ue_object), index);
2476+
Py_END_ALLOW_THREADS;
24252477
return true;
24262478
}
24272479
else if (auto casted_prop_base = Cast<UObjectPropertyBase>(prop))
24282480
{
24292481
// ensure the object type is correct, otherwise crash could happen (soon or later)
24302482
if (!ue_obj->ue_object->IsA(casted_prop_base->PropertyClass))
24312483
return false;
2484+
Py_BEGIN_ALLOW_THREADS;
24322485
casted_prop_base->SetObjectPropertyValue_InContainer(buffer, ue_obj->ue_object, index);
2486+
Py_END_ALLOW_THREADS;
24332487
return true;
24342488
}
24352489

@@ -2444,22 +2498,28 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
24442498
// ensure the object type is correct, otherwise crash could happen (soon or later)
24452499
if (!ue_obj->ue_object->IsA(casted_prop->PropertyClass))
24462500
return false;
2501+
Py_BEGIN_ALLOW_THREADS;
24472502
casted_prop->SetObjectPropertyValue_InContainer(buffer, ue_obj->ue_object, index);
2503+
Py_END_ALLOW_THREADS;
24482504
return true;
24492505
}
24502506
else if (auto casted_prop_soft_object = Cast<USoftObjectProperty>(prop))
24512507
{
24522508
if (!ue_obj->ue_object->IsA(casted_prop_soft_object->PropertyClass))
24532509
return false;
2510+
Py_BEGIN_ALLOW_THREADS;
24542511
casted_prop_soft_object->SetPropertyValue_InContainer(buffer, FSoftObjectPtr(ue_obj->ue_object), index);
2512+
Py_END_ALLOW_THREADS;
24552513
return true;
24562514
}
24572515
else if (auto casted_prop_interface = Cast<UInterfaceProperty>(prop))
24582516
{
24592517
// ensure the object type is correct, otherwise crash could happen (soon or later)
24602518
if (!ue_obj->ue_object->GetClass()->ImplementsInterface(casted_prop_interface->InterfaceClass))
24612519
return false;
2520+
Py_BEGIN_ALLOW_THREADS;
24622521
casted_prop_interface->SetPropertyValue_InContainer(buffer, FScriptInterface(ue_obj->ue_object), index);
2522+
Py_END_ALLOW_THREADS;
24632523
return true;
24642524
}
24652525
}
@@ -2471,13 +2531,17 @@ bool ue_py_convert_pyobject(PyObject *py_obj, UProperty *prop, uint8 *buffer, in
24712531
auto casted_prop_class = Cast<UClassProperty>(prop);
24722532
if (casted_prop_class)
24732533
{
2534+
Py_BEGIN_ALLOW_THREADS;
24742535
casted_prop_class->SetPropertyValue_InContainer(buffer, nullptr, index);
2536+
Py_END_ALLOW_THREADS;
24752537
return true;
24762538
}
24772539
auto casted_prop = Cast<UObjectPropertyBase>(prop);
24782540
if (casted_prop)
24792541
{
2542+
Py_BEGIN_ALLOW_THREADS;
24802543
casted_prop->SetObjectPropertyValue_InContainer(buffer, nullptr, index);
2544+
Py_END_ALLOW_THREADS;
24812545
return true;
24822546
}
24832547
return false;
@@ -2649,8 +2713,8 @@ PyObject *py_ue_ufunction_call(UFunction *u_function, UObject *u_obj, PyObject *
26492713
return PyErr_Format(PyExc_Exception, "UFunction has no SuperFunction");
26502714
}
26512715
u_function = u_function->GetSuperFunction();
2716+
}
26522717
}
2653-
}
26542718

26552719
//NOTE: u_function->PropertiesSize maps to local variable uproperties + ufunction paramaters uproperties
26562720
uint8 *buffer = (uint8 *)FMemory_Alloca(u_function->ParmsSize);
@@ -2732,7 +2796,9 @@ PyObject *py_ue_ufunction_call(UFunction *u_function, UObject *u_obj, PyObject *
27322796
FScopeCycleCounterUObject ObjectScope(u_obj);
27332797
FScopeCycleCounterUObject FunctionScope(u_function);
27342798

2799+
Py_BEGIN_ALLOW_THREADS;
27352800
u_obj->ProcessEvent(u_function, buffer);
2801+
Py_END_ALLOW_THREADS;
27362802

27372803
PyObject *ret = nullptr;
27382804

@@ -2796,7 +2862,7 @@ PyObject *py_ue_ufunction_call(UFunction *u_function, UObject *u_obj, PyObject *
27962862

27972863
Py_INCREF(Py_None);
27982864
return Py_None;
2799-
}
2865+
}
28002866

28012867
PyObject *ue_bind_pyevent(ue_PyUObject *u_obj, FString event_name, PyObject *py_callable, bool fail_on_wrong_property)
28022868
{
@@ -3114,9 +3180,9 @@ UFunction *unreal_engine_add_function(UClass *u_class, char *name, PyObject *py_
31143180
if (p->HasAnyPropertyFlags(CPF_ReturnParm))
31153181
{
31163182
function->ReturnValueOffset = p->GetOffset_ForUFunction();
3117-
}
31183183
}
31193184
}
3185+
}
31203186

31213187
if (parent_function)
31223188
{

0 commit comments

Comments
 (0)