OLD | NEW |
1 #include <pthread.h> | 1 #include <pthread.h> |
2 #include "libc.h" | 2 #include "libc.h" |
3 | 3 |
4 static struct atfork_funcs { | 4 static struct atfork_funcs { |
5 » void (*prepare)(void); | 5 void (*prepare)(void); |
6 » void (*parent)(void); | 6 void (*parent)(void); |
7 » void (*child)(void); | 7 void (*child)(void); |
8 » struct atfork_funcs *prev, *next; | 8 struct atfork_funcs *prev, *next; |
9 } *funcs; | 9 } * funcs; |
10 | 10 |
11 static volatile int lock[2]; | 11 static volatile int lock[2]; |
12 | 12 |
13 void __fork_handler(int who) | 13 void __fork_handler(int who) { |
14 { | 14 struct atfork_funcs* p; |
15 » struct atfork_funcs *p; | 15 if (!funcs) |
16 » if (!funcs) return; | 16 return; |
17 » if (who < 0) { | 17 if (who < 0) { |
18 » » LOCK(lock); | 18 LOCK(lock); |
19 » » for (p=funcs; p; p = p->next) { | 19 for (p = funcs; p; p = p->next) { |
20 » » » if (p->prepare) p->prepare(); | 20 if (p->prepare) |
21 » » » funcs = p; | 21 p->prepare(); |
22 » » } | 22 funcs = p; |
23 » } else { | 23 } |
24 » » for (p=funcs; p; p = p->prev) { | 24 } else { |
25 » » » if (!who && p->parent) p->parent(); | 25 for (p = funcs; p; p = p->prev) { |
26 » » » else if (who && p->child) p->child(); | 26 if (!who && p->parent) |
27 » » » funcs = p; | 27 p->parent(); |
28 » » } | 28 else if (who && p->child) |
29 » » UNLOCK(lock); | 29 p->child(); |
30 » } | 30 funcs = p; |
| 31 } |
| 32 UNLOCK(lock); |
| 33 } |
31 } | 34 } |
32 | 35 |
33 int pthread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(vo
id)) | 36 int pthread_atfork(void (*prepare)(void), |
34 { | 37 void (*parent)(void), |
35 » struct atfork_funcs *new = malloc(sizeof *new); | 38 void (*child)(void)) { |
36 » if (!new) return -1; | 39 struct atfork_funcs* new = malloc(sizeof *new); |
| 40 if (!new) |
| 41 return -1; |
37 | 42 |
38 » LOCK(lock); | 43 LOCK(lock); |
39 » new->next = funcs; | 44 new->next = funcs; |
40 » new->prev = 0; | 45 new->prev = 0; |
41 » new->prepare = prepare; | 46 new->prepare = prepare; |
42 » new->parent = parent; | 47 new->parent = parent; |
43 » new->child = child; | 48 new->child = child; |
44 » if (funcs) funcs->prev = new; | 49 if (funcs) |
45 » funcs = new; | 50 funcs->prev = new; |
46 » UNLOCK(lock); | 51 funcs = new; |
47 » return 0; | 52 UNLOCK(lock); |
| 53 return 0; |
48 } | 54 } |
OLD | NEW |