Main Page
Related Pages
Modules
Namespaces
Namespace List
Namespace Members
All
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Functions
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Variables
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Typedefs
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
r
s
t
u
v
w
y
z
Enumerations
a
b
c
d
e
f
g
h
i
j
l
m
n
o
p
r
s
t
u
v
w
Enumerator
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
r
s
t
u
z
Classes
Class List
Class Hierarchy
Class Members
All
:
[
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
Functions
[
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
~
Variables
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Typedefs
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
r
s
t
u
v
w
y
Enumerations
a
b
c
d
e
f
h
i
k
l
m
n
o
p
r
s
t
u
v
w
Enumerator
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
z
Properties
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
r
s
t
u
v
w
Related Functions
:
a
b
c
d
e
f
g
h
i
j
l
m
n
o
p
q
r
s
t
u
v
w
z
Files
File List
File Members
All
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Functions
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
z
Variables
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
Typedefs
_
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
w
x
z
Enumerations
_
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
r
s
t
u
v
w
x
Enumerator
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
r
s
t
u
v
w
x
Macros
_
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
grpc
src
core
lib
iomgr
fork_posix.cc
Go to the documentation of this file.
1
/*
2
*
3
* Copyright 2017 gRPC authors.
4
*
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
8
*
9
* http://www.apache.org/licenses/LICENSE-2.0
10
*
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
16
*
17
*/
18
19
#include <
grpc/support/port_platform.h
>
20
21
#include "
src/core/lib/iomgr/port.h
"
22
23
#ifdef GRPC_POSIX_FORK
24
25
#ifdef GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK
26
#include <pthread.h>
27
#endif
28
29
#include <
string.h
>
30
31
#include <
grpc/fork.h
>
32
#include <
grpc/grpc.h
>
33
#include <
grpc/support/log.h
>
34
35
#include "
src/core/lib/gprpp/fork.h
"
36
#include "
src/core/lib/gprpp/thd.h
"
37
#include "
src/core/lib/iomgr/ev_posix.h
"
38
#include "
src/core/lib/iomgr/executor.h
"
39
#include "
src/core/lib/iomgr/timer_manager.h
"
40
#include "
src/core/lib/iomgr/wakeup_fd_posix.h
"
41
42
/*
43
* NOTE: FORKING IS NOT GENERALLY SUPPORTED, THIS IS ONLY INTENDED TO WORK
44
* AROUND VERY SPECIFIC USE CASES.
45
*/
46
47
namespace
{
48
bool
skipped_handler =
true
;
49
bool
registered_handlers =
false
;
50
}
// namespace
51
52
void
grpc_prefork
() {
53
skipped_handler =
true
;
54
// This may be called after core shuts down, so verify initialized before
55
// instantiating an ExecCtx.
56
if
(!
grpc_is_initialized
()) {
57
return
;
58
}
59
grpc_core::ExecCtx
exec_ctx
;
60
if
(!
grpc_core::Fork::Enabled
()) {
61
gpr_log
(
GPR_ERROR
,
62
"Fork support not enabled; try running with the "
63
"environment variable GRPC_ENABLE_FORK_SUPPORT=1"
);
64
return
;
65
}
66
const
char
* poll_strategy_name =
grpc_get_poll_strategy_name
();
67
if
(poll_strategy_name ==
nullptr
||
68
(strcmp(poll_strategy_name,
"epoll1"
) != 0 &&
69
strcmp(poll_strategy_name,
"poll"
) != 0)) {
70
gpr_log
(
GPR_INFO
,
71
"Fork support is only compatible with the epoll1 and poll polling "
72
"strategies"
);
73
return
;
74
}
75
if
(!
grpc_core::Fork::BlockExecCtx
()) {
76
gpr_log
(
GPR_INFO
,
77
"Other threads are currently calling into gRPC, skipping fork() "
78
"handlers"
);
79
return
;
80
}
81
grpc_timer_manager_set_threading
(
false
);
82
grpc_core::Executor::SetThreadingAll
(
false
);
83
grpc_core::ExecCtx::Get
()->
Flush
();
84
grpc_core::Fork::AwaitThreads
();
85
skipped_handler =
false
;
86
}
87
88
void
grpc_postfork_parent
() {
89
if
(!skipped_handler) {
90
grpc_core::Fork::AllowExecCtx
();
91
grpc_core::ExecCtx
exec_ctx
;
92
grpc_timer_manager_set_threading
(
true
);
93
grpc_core::Executor::SetThreadingAll
(
true
);
94
}
95
}
96
97
void
grpc_postfork_child
() {
98
if
(!skipped_handler) {
99
grpc_core::Fork::AllowExecCtx
();
100
grpc_core::ExecCtx
exec_ctx
;
101
grpc_core::Fork::child_postfork_func
reset_polling_engine =
102
grpc_core::Fork::GetResetChildPollingEngineFunc
();
103
if
(reset_polling_engine !=
nullptr
) {
104
reset_polling_engine();
105
}
106
grpc_timer_manager_set_threading
(
true
);
107
grpc_core::Executor::SetThreadingAll
(
true
);
108
}
109
}
110
111
void
grpc_fork_handlers_auto_register
() {
112
if
(
grpc_core::Fork::Enabled
() & !registered_handlers) {
113
#ifdef GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK
114
pthread_atfork(
grpc_prefork
,
grpc_postfork_parent
,
grpc_postfork_child
);
115
registered_handlers =
true
;
116
#endif // GRPC_POSIX_FORK_ALLOW_PTHREAD_ATFORK
117
}
118
}
119
120
#endif // GRPC_POSIX_FORK
grpc_core::Fork::GetResetChildPollingEngineFunc
static child_postfork_func GetResetChildPollingEngineFunc()
Definition:
fork.cc:202
fork.h
GPR_INFO
#define GPR_INFO
Definition:
include/grpc/impl/codegen/log.h:56
log.h
wakeup_fd_posix.h
grpc_timer_manager_set_threading
void grpc_timer_manager_set_threading(bool enabled)
Definition:
iomgr/timer_manager.cc:346
timer_manager.h
grpc_postfork_child
void grpc_postfork_child(void)
Definition:
fork_windows.cc:37
string.h
grpc_core::Fork::AllowExecCtx
static void AllowExecCtx()
Definition:
fork.cc:213
grpc_get_poll_strategy_name
const char * grpc_get_poll_strategy_name()
grpc_core::Executor::SetThreadingAll
static void SetThreadingAll(bool enable)
Definition:
executor.cc:446
grpc_postfork_parent
void grpc_postfork_parent(void)
Definition:
fork_windows.cc:35
ev_posix.h
grpc_core::ExecCtx::Flush
bool Flush()
Definition:
exec_ctx.cc:69
grpc_core::Fork::AwaitThreads
static void AwaitThreads()
Definition:
fork.cc:230
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc.h
GPR_ERROR
#define GPR_ERROR
Definition:
include/grpc/impl/codegen/log.h:57
grpc_core::Fork::child_postfork_func
void(* child_postfork_func)(void)
Definition:
src/core/lib/gprpp/fork.h:40
grpc_core::ExecCtx
Definition:
exec_ctx.h:97
executor.h
grpc_is_initialized
GRPCAPI int grpc_is_initialized(void)
Definition:
init.cc:247
port.h
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition:
end2end_binder_transport_test.cc:75
thd.h
grpc_fork_handlers_auto_register
void grpc_fork_handlers_auto_register(void)
Definition:
fork_windows.cc:39
fork.h
grpc_prefork
void grpc_prefork(void)
Definition:
fork_windows.cc:33
grpc_core::Fork::BlockExecCtx
static bool BlockExecCtx()
Definition:
fork.cc:206
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition:
exec_ctx.h:205
grpc_core::Fork::Enabled
static bool Enabled()
Definition:
fork.cc:184
port_platform.h
grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:22