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