Debian Codegraph @

src/core/lib/iomgr/iomgr_posix_cfstream.cc

raw
1 /*
2 *
3 * Copyright 2015 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 /// CFStream is build-enabled on iOS by default and disabled by default on other
20 /// platforms (see port_platform.h). To enable CFStream build on another
21 /// platform, the users need to define macro "GRPC_CFSTREAM=1" when building
22 /// gRPC.
23 ///
24 /// When CFStream is to be built (either by default on iOS or by macro on other
25 /// platforms), the users can disable CFStream with environment variable
26 /// "grpc_cfstream=0". This will let gRPC to fallback to use POSIX sockets. In
27 /// addition, the users may choose to use an alternative CFRunLoop based pollset
28 /// "ev_apple" by setting environment variable "GRPC_CFSTREAM_RUN_LOOP=1". This
29 /// pollset resolves a bug from Apple when CFStream streams dispatch events to
30 /// dispatch queues. The caveat of this pollset is that users may not be able to
31 /// run a gRPC server in the same process.
32
33 #include <grpc/support/port_platform.h>
34
35 #include "src/core/lib/iomgr/port.h"
36
37 #ifdef GRPC_CFSTREAM_IOMGR
38
39 #include "src/core/lib/debug/trace.h"
40 #include "src/core/lib/iomgr/ev_apple.h"
41 #include "src/core/lib/iomgr/ev_posix.h"
42 #include "src/core/lib/iomgr/iomgr_internal.h"
43 #include "src/core/lib/iomgr/resolve_address.h"
44 #include "src/core/lib/iomgr/resolve_address_posix.h"
45 #include "src/core/lib/iomgr/tcp_client.h"
46 #include "src/core/lib/iomgr/tcp_posix.h"
47 #include "src/core/lib/iomgr/tcp_server.h"
48 #include "src/core/lib/iomgr/timer.h"
49
50 static const char* grpc_cfstream_env_var = "grpc_cfstream";
51 static const char* grpc_cfstream_run_loop_env_var = "GRPC_CFSTREAM_RUN_LOOP";
52
53 extern grpc_tcp_server_vtable grpc_posix_tcp_server_vtable;
54 extern grpc_tcp_client_vtable grpc_posix_tcp_client_vtable;
55 extern grpc_tcp_client_vtable grpc_cfstream_client_vtable;
56 extern grpc_timer_vtable grpc_generic_timer_vtable;
57 extern grpc_pollset_vtable grpc_posix_pollset_vtable;
58 extern grpc_pollset_set_vtable grpc_posix_pollset_set_vtable;
59
60 static void apple_iomgr_platform_init(void) { grpc_pollset_global_init(); }
61
62 static void apple_iomgr_platform_flush(void) {}
63
64 static void apple_iomgr_platform_shutdown(void) {
65 grpc_pollset_global_shutdown();
66 grpc_core::ResetDNSResolver(nullptr); // delete the resolver
67 }
68
69 static void apple_iomgr_platform_shutdown_background_closure(void) {}
70
71 static bool apple_iomgr_platform_is_any_background_poller_thread(void) {
72 return false;
73 }
74
75 static bool apple_iomgr_platform_add_closure_to_background_poller(
76 grpc_closure* closure, grpc_error_handle error) {
77 return false;
78 }
79
80 static grpc_iomgr_platform_vtable apple_vtable = {
81 apple_iomgr_platform_init,
82 apple_iomgr_platform_flush,
83 apple_iomgr_platform_shutdown,
84 apple_iomgr_platform_shutdown_background_closure,
85 apple_iomgr_platform_is_any_background_poller_thread,
86 apple_iomgr_platform_add_closure_to_background_poller};
87
88 namespace {
89 struct CFStreamEnv {
90 bool enable_cfstream;
91 bool enable_cfstream_run_loop;
92 };
93
94 // Parses environment variables for CFStream specific settings
95 CFStreamEnv ParseEnvForCFStream() {
96 CFStreamEnv env;
97 char* enable_cfstream_str = getenv(grpc_cfstream_env_var);
98 env.enable_cfstream =
99 enable_cfstream_str == nullptr || enable_cfstream_str[0] != '0';
100 char* enable_cfstream_run_loop_str = getenv(grpc_cfstream_run_loop_env_var);
101 // CFStream run-loop is disabled by default. The user has to enable it
102 // explicitly with environment variable.
103 env.enable_cfstream_run_loop = enable_cfstream_run_loop_str != nullptr &&
104 enable_cfstream_run_loop_str[0] == '1';
105 return env;
106 }
107
108 void MaybeInitializeTcpPosix(void) {
109 CFStreamEnv env = ParseEnvForCFStream();
110 if (!env.enable_cfstream || !env.enable_cfstream_run_loop) {
111 grpc_tcp_posix_init();
112 }
113 }
114
115 void MaybeShutdownTcpPosix(void) {
116 CFStreamEnv env = ParseEnvForCFStream();
117 if (!env.enable_cfstream || !env.enable_cfstream_run_loop) {
118 grpc_tcp_posix_shutdown();
119 }
120 }
121 } // namespace
122
123 static void iomgr_platform_init(void) {
124 MaybeInitializeTcpPosix();
125 grpc_wakeup_fd_global_init();
126 grpc_event_engine_init();
127 }
128
129 static void iomgr_platform_flush(void) {}
130
131 static void iomgr_platform_shutdown(void) {
132 grpc_event_engine_shutdown();
133 grpc_wakeup_fd_global_destroy();
134 MaybeShutdownTcpPosix();
135 }
136
137 static void iomgr_platform_shutdown_background_closure(void) {
138 grpc_shutdown_background_closure();
139 }
140
141 static bool iomgr_platform_is_any_background_poller_thread(void) {
142 return grpc_is_any_background_poller_thread();
143 }
144
145 static bool iomgr_platform_add_closure_to_background_poller(
146 grpc_closure* closure, grpc_error_handle error) {
147 return grpc_add_closure_to_background_poller(closure, error);
148 }
149
150 static grpc_iomgr_platform_vtable vtable = {
151 iomgr_platform_init,
152 iomgr_platform_flush,
153 iomgr_platform_shutdown,
154 iomgr_platform_shutdown_background_closure,
155 iomgr_platform_is_any_background_poller_thread,
156 iomgr_platform_add_closure_to_background_poller};
157
158 void grpc_set_default_iomgr_platform() {
159 CFStreamEnv env = ParseEnvForCFStream();
160 if (!env.enable_cfstream) {
161 // Use POSIX sockets for both client and server
162 grpc_set_tcp_client_impl(&grpc_posix_tcp_client_vtable);
163 grpc_set_tcp_server_impl(&grpc_posix_tcp_server_vtable);
164 grpc_set_pollset_vtable(&grpc_posix_pollset_vtable);
165 grpc_set_pollset_set_vtable(&grpc_posix_pollset_set_vtable);
166 grpc_set_iomgr_platform_vtable(&vtable);
167 } else if (env.enable_cfstream && !env.enable_cfstream_run_loop) {
168 // Use CFStream with dispatch queue for client; use POSIX sockets for server
169 grpc_set_tcp_client_impl(&grpc_cfstream_client_vtable);
170 grpc_set_tcp_server_impl(&grpc_posix_tcp_server_vtable);
171 grpc_set_pollset_vtable(&grpc_posix_pollset_vtable);
172 grpc_set_pollset_set_vtable(&grpc_posix_pollset_set_vtable);
173 grpc_set_iomgr_platform_vtable(&vtable);
174 } else {
175 // Use CFStream with CFRunLoop for client; server not supported
176 grpc_set_tcp_client_impl(&grpc_cfstream_client_vtable);
177 grpc_set_pollset_vtable(&grpc_apple_pollset_vtable);
178 grpc_set_pollset_set_vtable(&grpc_apple_pollset_set_vtable);
179 grpc_set_iomgr_platform_vtable(&apple_vtable);
180 }
181 grpc_tcp_client_global_init();
182 grpc_set_timer_impl(&grpc_generic_timer_vtable);
183 grpc_core::ResetDNSResolver(std::make_unique<grpc_core::NativeDNSResolver>());
184 }
185
186 bool grpc_iomgr_run_in_background() {
187 char* enable_cfstream_str = getenv(grpc_cfstream_env_var);
188 bool enable_cfstream =
189 enable_cfstream_str == nullptr || enable_cfstream_str[0] != '0';
190 char* enable_cfstream_run_loop_str = getenv(grpc_cfstream_run_loop_env_var);
191 // CFStream run-loop is disabled by default. The user has to enable it
192 // explicitly with environment variable.
193 bool enable_cfstream_run_loop = enable_cfstream_run_loop_str != nullptr &&
194 enable_cfstream_run_loop_str[0] == '1';
195 if (enable_cfstream && enable_cfstream_run_loop) {
196 return false;
197 } else {
198 return grpc_event_engine_run_in_background();
199 }
200 }
201
202 #endif /* GRPC_CFSTREAM_IOMGR */