avoid deadlocks after forking threaded processes
parent
daa44905ee
commit
4aa0fab4f4
27
malloc.c
27
malloc.c
|
@ -525,6 +525,31 @@ static void regions_delete(struct region_info *region) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void pre_fork(void) {
|
||||||
|
pthread_mutex_lock(®ions_lock);
|
||||||
|
for (unsigned i = 0; i < N_SIZE_CLASSES; i++) {
|
||||||
|
pthread_mutex_lock(&size_class_metadata[i].mutex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void post_fork_parent(void) {
|
||||||
|
pthread_mutex_unlock(®ions_lock);
|
||||||
|
for (unsigned i = 0; i < N_SIZE_CLASSES; i++) {
|
||||||
|
pthread_mutex_unlock(&size_class_metadata[i].mutex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void post_fork_child(void) {
|
||||||
|
if (pthread_mutex_init(®ions_lock, NULL)) {
|
||||||
|
fatal_error("mutex initialization failed");
|
||||||
|
}
|
||||||
|
for (unsigned i = 0; i < N_SIZE_CLASSES; i++) {
|
||||||
|
if (pthread_mutex_init(&size_class_metadata[i].mutex, NULL)) {
|
||||||
|
fatal_error("mutex initialization failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
COLD static void init_slow_path(void) {
|
COLD static void init_slow_path(void) {
|
||||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||||
|
|
||||||
|
@ -535,6 +560,8 @@ COLD static void init_slow_path(void) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pthread_atfork(pre_fork, post_fork_parent, post_fork_child);
|
||||||
|
|
||||||
struct random_state rng;
|
struct random_state rng;
|
||||||
random_state_init(&rng);
|
random_state_init(&rng);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue