forked from theanarkh/read-linuxthreads-2.0.1-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptfork.c
115 lines (108 loc) · 4.34 KB
/
ptfork.c
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Linuxthreads - a simple clone()-based implementation of Posix */
/* threads for Linux. */
/* Copyright (C) 1996 Xavier Leroy ([email protected]) */
/* */
/* This program is free software; you can redistribute it and/or */
/* modify it under the terms of the GNU Library General Public License */
/* as published by the Free Software Foundation; either version 2 */
/* of the License, or (at your option) any later version. */
/* */
/* This program 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 Library General Public License for more details. */
/* The "atfork" stuff */
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include "pthread.h"
#include "internals.h"
struct handler_list {
void (*handler)(void);
struct handler_list * next;
};
// 用于互斥访问链表的互斥变量
static pthread_mutex_t pthread_atfork_lock = PTHREAD_MUTEX_INITIALIZER;
// 三个链表
static struct handler_list * pthread_atfork_prepare = NULL;
static struct handler_list * pthread_atfork_parent = NULL;
static struct handler_list * pthread_atfork_child = NULL;
// 生成一个新的handler_list节点插入到list中
static void pthread_insert_list(struct handler_list ** list,
void (*handler)(void),
struct handler_list * newlist,
int at_end)
{
if (handler == NULL) return;
// 插入到最后,则先把直接指向尾节点
if (at_end) {
while(*list != NULL) list = &((*list)->next);
}
// 保存数据到新节点
newlist->handler = handler;
// *list即第一个节点的地址
newlist->next = *list;
// *list的内容修改为新节点
*list = newlist;
}
struct handler_list_block {
struct handler_list prepare, parent, child;
};
int pthread_atfork(void (*prepare)(void),
void (*parent)(void),
void (*child)(void))
{
struct handler_list_block * block =
(struct handler_list_block *) malloc(sizeof(struct handler_list_block));
if (block == NULL) return ENOMEM;
pthread_mutex_lock(&pthread_atfork_lock);
/* "prepare" handlers are called in LIFO */
// 把三个函数保存到一个节点中,如果这个节点分别插入三个handle_list队列
pthread_insert_list(&pthread_atfork_prepare, prepare, &block->prepare, 0);
/* "parent" handlers are called in FIFO */
pthread_insert_list(&pthread_atfork_parent, parent, &block->parent, 1);
/* "child" handlers are called in FIFO */
pthread_insert_list(&pthread_atfork_child, child, &block->child, 1);
pthread_mutex_unlock(&pthread_atfork_lock);
return 0;
}
// handle_list链表中每个节点的函数
static inline void pthread_call_handlers(struct handler_list * list)
{
for (/*nothing*/; list != NULL; list = list->next) (list->handler)();
}
extern int __fork(void);
// http://man7.org/linux/man-pages/man3/pthread_atfork.3.html
/*
glibc中定义了fork和__fork的关系。
weak_alias (__fork, fork)
# define weak_alias(name, aliasname) _weak_alias (name, aliasname)
# define _weak_alias(name, aliasname) \
extern __typeof (name) aliasname __attribute__ ((weak, alias (#name)))
fork是弱符号,并且是__fork的的别名。即如果定义了fork,则会覆盖glibc中的fork。
这里就是覆盖glibc的fork,然后在调用glibc的__fork之前执行一些额外的操作。这样用户在执行fork的时候,
就会执行下面这个fork函数,从而执行glibc的__fork
*/
int fork(void)
{
int pid;
struct handler_list * prepare, * child, * parent;
pthread_mutex_lock(&pthread_atfork_lock);
prepare = pthread_atfork_prepare;
child = pthread_atfork_child;
parent = pthread_atfork_parent;
pthread_mutex_unlock(&pthread_atfork_lock);
// 调fork之前调用函数列表
pthread_call_handlers(prepare);
pid = __fork();
// 子进程
if (pid == 0) {
__pthread_reset_main_thread();
__fresetlockfiles();
pthread_call_handlers(child);
} else {
// 父进程
pthread_call_handlers(parent);
}
return pid;
}