Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions lang_lock.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,40 @@
#ifdef MULTI_PERL
perl_key last_py_tstate;

#ifndef WIN32
#include <pthread.h>
#include <unistd.h>
/* Thread-local GIL state across prepare → parent/child (same thread). */
static __thread PyGILState_STATE atfork_gstate;

static void
_atfork_prepare(void)
{
#if PY_VERSION_HEX >= 0x03070000
atfork_gstate = PyGILState_Ensure();
PyOS_BeforeFork();
#endif
}

static void
_atfork_parent(void)
{
#if PY_VERSION_HEX >= 0x03070000
PyOS_AfterFork_Parent();
PyGILState_Release(atfork_gstate);
#endif
}

static void
_atfork_child(void)
{
#if PY_VERSION_HEX >= 0x03070000
PyOS_AfterFork_Child();
PyGILState_Release(atfork_gstate);
#endif
}
#endif

void
lang_lock_init()
{
Expand All @@ -23,6 +57,13 @@ lang_lock_init()
#endif
Py_FatalError("Can't create TSD key for py_tstate");
}
#ifndef WIN32
static bool atfork_registered = false;
if (!atfork_registered) {
(void) pthread_atfork(_atfork_prepare, _atfork_parent, _atfork_child);
atfork_registered = true;
}
#endif
}

#else /* MULTI_PERL */
Expand Down