musl: allocator: Avoid deadlock in pthread_atfork

Musl 1.2.3 and before call malloc() in pthread_atfork(),
which may result in a deadlock.
This commit is contained in:
klzgrad 2025-02-08 19:57:01 +08:00
parent c087e6f6d5
commit ce8c22e607
2 changed files with 7 additions and 1 deletions

View File

@ -59,7 +59,9 @@ thread_local bool g_is_main_thread = true;
class InitAtFork { class InitAtFork {
public: public:
InitAtFork() { InitAtFork() {
#if !defined(__MUSL__)
pthread_atfork(nullptr, nullptr, internal::InvalidateTidCache); pthread_atfork(nullptr, nullptr, internal::InvalidateTidCache);
#endif
} }
}; };

View File

@ -297,7 +297,11 @@ void PartitionAllocMallocInitOnce() {
return; return;
} }
#if PA_BUILDFLAG(IS_LINUX) || PA_BUILDFLAG(IS_CHROMEOS) #if defined(__MUSL__)
static_cast<void>(BeforeForkInParent);
static_cast<void>(AfterForkInParent);
static_cast<void>(AfterForkInChild);
#elif PA_BUILDFLAG(IS_LINUX) || PA_BUILDFLAG(IS_CHROMEOS)
// When fork() is called, only the current thread continues to execute in the // When fork() is called, only the current thread continues to execute in the
// child process. If the lock is held, but *not* by this thread when fork() is // child process. If the lock is held, but *not* by this thread when fork() is
// called, we have a deadlock. // called, we have a deadlock.