1 #include <pthread.h>
2 #include <errno.h>
3 #include "libc.h"
4 #include "lock.h"
5
6 #define malloc __libc_malloc
7 #define calloc undef
8 #define realloc undef
9 #define free undef
10
11 static struct atfork_funcs {
12 void (*prepare)(void);
13 void (*parent)(void);
14 void (*child)(void);
15 struct atfork_funcs *prev, *next;
16 } *funcs, *gwpfuncs;
17
18 static volatile int lock[1];
19
__fork_handler(int who)20 void __fork_handler(int who)
21 {
22 struct atfork_funcs *p;
23 if (!funcs) return;
24 if (who < 0) {
25 LOCK(lock);
26 for (p=funcs; p; p = p->next) {
27 if (p->prepare) p->prepare();
28 funcs = p;
29 }
30 // The gwpasan prepare must be executed last,and the gwpasan handler must be executed first.
31 if (gwpfuncs && gwpfuncs->prepare) gwpfuncs->prepare();
32 } else {
33 // The gwpasan child will unlock malloc mutex, so execute it first to avoid deadlocks.
34 if (gwpfuncs && !who && gwpfuncs->parent) gwpfuncs->parent();
35 if (gwpfuncs && who && gwpfuncs->child) gwpfuncs->child();
36 for (p=funcs; p; p = p->prev) {
37 if (!who && p->parent) p->parent();
38 else if (who && p->child) p->child();
39 funcs = p;
40 }
41 UNLOCK(lock);
42 }
43 }
44
pthread_atfork_for_gwpasan(void (*prepare)(void), void (*parent)(void), void (*child)(void))45 int pthread_atfork_for_gwpasan(void (*prepare)(void), void (*parent)(void), void (*child)(void))
46 {
47 struct atfork_funcs *new = malloc(sizeof *new);
48 if (!new) return ENOMEM;
49
50 LOCK(lock);
51 new->prepare = prepare;
52 new->parent = parent;
53 new->child = child;
54 gwpfuncs = new;
55 UNLOCK(lock);
56 return 0;
57 }
58
pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))59 int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
60 {
61 struct atfork_funcs *new = malloc(sizeof *new);
62 if (!new) return ENOMEM;
63
64 LOCK(lock);
65 new->next = funcs;
66 new->prev = 0;
67 new->prepare = prepare;
68 new->parent = parent;
69 new->child = child;
70 if (funcs) funcs->prev = new;
71 funcs = new;
72 UNLOCK(lock);
73 return 0;
74 }
75