fix missing synchronization of pthread TSD keys with MT-fork

commit 167390f055 seems to have
overlooked the presence of a lock here, probably because it was one of
the exceptions not using LOCK() but a rwlock.

as such, it can't be added to the generic table of locks to take, so
add an explicit atfork function for the pthread keys table. the order
it is called does not particularly matter since nothing else in libc
but pthread_exit interacts with keys.
This commit is contained in:
Rich Felker committed 2022-10-19 14:01:32 -04:00
1 parent 5ff3eea91f
commit cf76df0e1f
3 files changed
+12

No files matched your search

+8
View File
@@ -1,4 +1,5 @@
#include "pthread_impl.h"
#include "fork_impl.h"
volatile size_t __pthread_tsd_size = sizeof(void *) * PTHREAD_KEYS_MAX;
void *__pthread_tsd_main[PTHREAD_KEYS_MAX] = { 0 };
@@ -20,6 +21,13 @@ static void dummy_0(void)
weak_alias(dummy_0, __tl_lock);
weak_alias(dummy_0, __tl_unlock);
void __pthread_key_atfork(int who)
{
if (who<0) __pthread_rwlock_rdlock(&key_lock);
else if (!who) __pthread_rwlock_unlock(&key_lock);
else key_lock = (pthread_rwlock_t)PTHREAD_RWLOCK_INITIALIZER;
}
int __pthread_key_create(pthread_key_t *k, void (*dtor)(void *))
{
pthread_t self = __pthread_self();