Skip to content

Commit b2c2779

Browse files
committed
Trying to solve issues with threads python.
1 parent f6b7fba commit b2c2779

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

source/loaders/py_loader/include/py_loader/py_loader_threading.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
extern "C" {
3030
#endif
3131

32-
PY_LOADER_NO_EXPORT void py_loader_thread_initialize(const int host);
32+
PY_LOADER_NO_EXPORT int py_loader_thread_initialize(const int host);
3333

3434
PY_LOADER_NO_EXPORT int py_loader_thread_is_main(void);
3535

source/loaders/py_loader/source/py_loader_impl.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2651,6 +2651,7 @@ loader_impl_data py_loader_impl_initialize(loader_impl impl, configuration confi
26512651
#if DEBUG_ENABLED
26522652
int gc_initialized = 1;
26532653
#endif
2654+
int gil_release;
26542655

26552656
if (py_impl == NULL)
26562657
{
@@ -2682,7 +2683,7 @@ loader_impl_data py_loader_impl_initialize(loader_impl impl, configuration confi
26822683
}
26832684

26842685
/* Initialize threading */
2685-
py_loader_thread_initialize(host);
2686+
gil_release = py_loader_thread_initialize(host);
26862687

26872688
/* Initialize executable */
26882689
if (py_loader_impl_initialize_sys_executable(py_impl) != 0)
@@ -2780,6 +2781,11 @@ loader_impl_data py_loader_impl_initialize(loader_impl impl, configuration confi
27802781
PyType_Modified(PyCFunctionTypePtr());
27812782
#endif
27822783

2784+
if (gil_release)
2785+
{
2786+
py_loader_thread_release();
2787+
}
2788+
27832789
/* Register initialization */
27842790
loader_initialization_register(impl);
27852791

@@ -2823,6 +2829,10 @@ loader_impl_data py_loader_impl_initialize(loader_impl impl, configuration confi
28232829
#endif
28242830
error_after_argv:
28252831
error_after_sys_executable:
2832+
if (gil_release)
2833+
{
2834+
py_loader_thread_release();
2835+
}
28262836
(void)py_loader_impl_finalize(py_impl, host);
28272837
error_init_py:
28282838
free(py_impl);

source/loaders/py_loader/source/py_loader_threading.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct py_thread_state
3030
PyGILState_STATE gstate;
3131

3232
py_thread_state() :
33-
ref_count(0) {}
33+
ref_count(0), gstate(PyGILState_UNLOCKED) {}
3434

3535
void ensure()
3636
{
@@ -63,17 +63,30 @@ thread_local py_thread_state current_thread_state;
6363
thread_local uint64_t current_thread_id = thread_id_get_current();
6464
static std::vector<PyObject *> delayed_destructor;
6565

66-
void py_loader_thread_initialize(const int host)
66+
int py_loader_thread_initialize(const int host)
6767
{
6868
main_thread_id = thread_id_get_current();
6969

7070
if (host == 1)
7171
{
72+
int gil_status = PyGILState_Check();
73+
7274
PyGILState_STATE gstate = PyGILState_Ensure();
7375
main_thread_state = PyThreadState_Get();
74-
main_thread_ref_count++;
7576
PyGILState_Release(gstate);
77+
78+
if (gil_status == 0)
79+
{
80+
py_loader_thread_acquire();
81+
return 1;
82+
}
83+
else
84+
{
85+
main_thread_ref_count++;
86+
}
7687
}
88+
89+
return 0;
7790
}
7891

7992
int py_loader_thread_is_main()

0 commit comments

Comments
 (0)