use libc-internal malloc for pthread_atfork
[musl] / src / thread / pthread_atfork.c
index 0773dc8..26d3254 100644 (file)
@@ -1,5 +1,12 @@
 #include <pthread.h>
+#include <errno.h>
 #include "libc.h"
+#include "lock.h"
+
+#define malloc __libc_malloc
+#define calloc undef
+#define realloc undef
+#define free undef
 
 static struct atfork_funcs {
        void (*prepare)(void);
@@ -8,13 +15,14 @@ static struct atfork_funcs {
        struct atfork_funcs *prev, *next;
 } *funcs;
 
-static int lock;
+static volatile int lock[1];
 
-static void fork_handler(int who)
+void __fork_handler(int who)
 {
        struct atfork_funcs *p;
+       if (!funcs) return;
        if (who < 0) {
-               LOCK(&lock);
+               LOCK(lock);
                for (p=funcs; p; p = p->next) {
                        if (p->prepare) p->prepare();
                        funcs = p;
@@ -25,17 +33,16 @@ static void fork_handler(int who)
                        else if (who && p->child) p->child();
                        funcs = p;
                }
-               UNLOCK(&lock);
+               UNLOCK(lock);
        }
 }
 
 int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
 {
        struct atfork_funcs *new = malloc(sizeof *new);
-       if (!new) return -1;
+       if (!new) return ENOMEM;
 
-       LOCK(&lock);
-       libc.fork_handler = fork_handler;
+       LOCK(lock);
        new->next = funcs;
        new->prev = 0;
        new->prepare = prepare;
@@ -43,6 +50,6 @@ int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(vo
        new->child = child;
        if (funcs) funcs->prev = new;
        funcs = new;
-       UNLOCK(&lock);
+       UNLOCK(lock);
        return 0;
 }