1 /*
2 ** GNU Pth - The GNU Portable Threads
3 ** Copyright (c) 1999-2006 Ralf S. Engelschall <rse@engelschall.com>
4 **
5 ** This file is part of GNU Pth, a non-preemptive thread scheduling
6 ** library which can be found at http://www.gnu.org/software/pth/.
7 **
8 ** This library is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU Lesser General Public
10 ** License as published by the Free Software Foundation; either
11 ** version 2.1 of the License, or (at your option) any later version.
12 **
13 ** This library is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** Lesser General Public License for more details.
17 **
18 ** You should have received a copy of the GNU Lesser General Public
19 ** License along with this library; if not, write to the Free Software
20 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 ** USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
22 **
23 ** pth_fork.c: Pth process forking support
24 */
25 /* ``Every day of my life
26 I am forced to add another
27 name to the list of people
28 who piss me off!''
29 -- Calvin */
30 #include "pth_p.h"
31
32 struct pth_atfork_st {
33 void (*prepare)(void *);
34 void (*parent)(void *);
35 void (*child)(void *);
36 void *arg;
37 };
38
39 static struct pth_atfork_st pth_atfork_list[PTH_ATFORK_MAX];
40 static int pth_atfork_idx = 0;
41
pth_atfork_push(void (* prepare)(void *),void (* parent)(void *),void (* child)(void *),void * arg)42 int pth_atfork_push(void (*prepare)(void *), void (*parent)(void *),
43 void (*child)(void *), void *arg)
44 {
45 if (pth_atfork_idx > PTH_ATFORK_MAX-1)
46 return pth_error(FALSE, ENOMEM);
47 pth_atfork_list[pth_atfork_idx].prepare = prepare;
48 pth_atfork_list[pth_atfork_idx].parent = parent;
49 pth_atfork_list[pth_atfork_idx].child = child;
50 pth_atfork_list[pth_atfork_idx].arg = arg;
51 pth_atfork_idx++;
52 return TRUE;
53 }
54
pth_atfork_pop(void)55 int pth_atfork_pop(void)
56 {
57 if (pth_atfork_idx <= 0)
58 return FALSE;
59 pth_atfork_idx--;
60 return TRUE;
61 }
62
pth_fork(void)63 pid_t pth_fork(void)
64 {
65 pid_t pid;
66 int i;
67
68 /* run preparation handlers in LIFO order */
69 for (i = pth_atfork_idx-1; i >= 0; i--)
70 if (pth_atfork_list[i].prepare != NULL)
71 pth_atfork_list[i].prepare(pth_atfork_list[i].arg);
72
73 /* fork the process */
74 if ((pid = pth_sc(fork)()) == -1)
75 return FALSE;
76
77 /* handle parent and child contexts */
78 if (pid != 0) {
79 /* Parent: */
80
81 /* run parent handlers in FIFO order */
82 for (i = 0; i <= pth_atfork_idx-1; i++)
83 if (pth_atfork_list[i].parent != NULL)
84 pth_atfork_list[i].parent(pth_atfork_list[i].arg);
85 }
86 else {
87 /* Child: */
88
89 /* kick out all threads except for the current one and the scheduler */
90 pth_scheduler_drop();
91
92 /* run child handlers in FIFO order */
93 for (i = 0; i <= pth_atfork_idx-1; i++)
94 if (pth_atfork_list[i].child != NULL)
95 pth_atfork_list[i].child(pth_atfork_list[i].arg);
96 }
97 return pid;
98 }
99
100