3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
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.
33 #include <grpc/support/port_platform.h>
35 #include "src/core/lib/iomgr/port.h"
37 #ifdef GRPC_CFSTREAM_IOMGR
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/tcp_client.h"
45 #include "src/core/lib/iomgr/tcp_posix.h"
46 #include "src/core/lib/iomgr/tcp_server.h"
47 #include "src/core/lib/iomgr/timer.h"
49 static const char* grpc_cfstream_env_var = "grpc_cfstream";
50 static const char* grpc_cfstream_run_loop_env_var = "GRPC_CFSTREAM_RUN_LOOP";
52 extern grpc_tcp_server_vtable grpc_posix_tcp_server_vtable;
53 extern grpc_tcp_client_vtable grpc_posix_tcp_client_vtable;
54 extern grpc_tcp_client_vtable grpc_cfstream_client_vtable;
55 extern grpc_timer_vtable grpc_generic_timer_vtable;
56 extern grpc_pollset_vtable grpc_posix_pollset_vtable;
57 extern grpc_pollset_set_vtable grpc_posix_pollset_set_vtable;
58 extern grpc_address_resolver_vtable grpc_posix_resolver_vtable;
60 static void apple_iomgr_platform_init(void) { grpc_pollset_global_init(); }
62 static void apple_iomgr_platform_flush(void) {}
64 static void apple_iomgr_platform_shutdown(void) {
65 grpc_pollset_global_shutdown();
68 static void apple_iomgr_platform_shutdown_background_closure(void) {}
70 static bool apple_iomgr_platform_is_any_background_poller_thread(void) {
74 static bool apple_iomgr_platform_add_closure_to_background_poller(
75 grpc_closure* closure, grpc_error* error) {
79 static grpc_iomgr_platform_vtable apple_vtable = {
80 apple_iomgr_platform_init,
81 apple_iomgr_platform_flush,
82 apple_iomgr_platform_shutdown,
83 apple_iomgr_platform_shutdown_background_closure,
84 apple_iomgr_platform_is_any_background_poller_thread,
85 apple_iomgr_platform_add_closure_to_background_poller};
87 static void iomgr_platform_init(void) {
88 grpc_wakeup_fd_global_init();
89 grpc_event_engine_init();
92 static void iomgr_platform_flush(void) {}
94 static void iomgr_platform_shutdown(void) {
95 grpc_event_engine_shutdown();
96 grpc_wakeup_fd_global_destroy();
99 static void iomgr_platform_shutdown_background_closure(void) {
100 grpc_shutdown_background_closure();
103 static bool iomgr_platform_is_any_background_poller_thread(void) {
104 return grpc_is_any_background_poller_thread();
107 static bool iomgr_platform_add_closure_to_background_poller(
108 grpc_closure* closure, grpc_error* error) {
109 return grpc_add_closure_to_background_poller(closure, error);
112 static grpc_iomgr_platform_vtable vtable = {
114 iomgr_platform_flush,
115 iomgr_platform_shutdown,
116 iomgr_platform_shutdown_background_closure,
117 iomgr_platform_is_any_background_poller_thread,
118 iomgr_platform_add_closure_to_background_poller};
120 void grpc_set_default_iomgr_platform() {
121 char* enable_cfstream_str = getenv(grpc_cfstream_env_var);
122 bool enable_cfstream =
123 enable_cfstream_str == nullptr || enable_cfstream_str[0] != '0';
124 char* enable_cfstream_run_loop_str = getenv(grpc_cfstream_run_loop_env_var);
125 // CFStream run-loop is disabled by default. The user has to enable it
126 // explicitly with environment variable.
127 bool enable_cfstream_run_loop = enable_cfstream_run_loop_str != nullptr &&
128 enable_cfstream_run_loop_str[0] == '1';
129 if (!enable_cfstream) {
130 // Use POSIX sockets for both client and server
131 grpc_set_tcp_client_impl(&grpc_posix_tcp_client_vtable);
132 grpc_set_tcp_server_impl(&grpc_posix_tcp_server_vtable);
133 grpc_set_pollset_vtable(&grpc_posix_pollset_vtable);
134 grpc_set_pollset_set_vtable(&grpc_posix_pollset_set_vtable);
135 grpc_set_iomgr_platform_vtable(&vtable);
136 } else if (enable_cfstream && !enable_cfstream_run_loop) {
137 // Use CFStream with dispatch queue for client; use POSIX sockets for server
138 grpc_set_tcp_client_impl(&grpc_cfstream_client_vtable);
139 grpc_set_tcp_server_impl(&grpc_posix_tcp_server_vtable);
140 grpc_set_pollset_vtable(&grpc_posix_pollset_vtable);
141 grpc_set_pollset_set_vtable(&grpc_posix_pollset_set_vtable);
142 grpc_set_iomgr_platform_vtable(&vtable);
144 // Use CFStream with CFRunLoop for client; server not supported
145 grpc_set_tcp_client_impl(&grpc_cfstream_client_vtable);
146 grpc_set_pollset_vtable(&grpc_apple_pollset_vtable);
147 grpc_set_pollset_set_vtable(&grpc_apple_pollset_set_vtable);
148 grpc_set_iomgr_platform_vtable(&apple_vtable);
150 grpc_set_timer_impl(&grpc_generic_timer_vtable);
151 grpc_set_resolver_impl(&grpc_posix_resolver_vtable);
154 bool grpc_iomgr_run_in_background() {
155 char* enable_cfstream_str = getenv(grpc_cfstream_env_var);
156 bool enable_cfstream =
157 enable_cfstream_str == nullptr || enable_cfstream_str[0] != '0';
158 char* enable_cfstream_run_loop_str = getenv(grpc_cfstream_run_loop_env_var);
159 // CFStream run-loop is disabled by default. The user has to enable it
160 // explicitly with environment variable.
161 bool enable_cfstream_run_loop = enable_cfstream_run_loop_str != nullptr &&
162 enable_cfstream_run_loop_str[0] == '1';
163 if (enable_cfstream && enable_cfstream_run_loop) {
166 return grpc_event_engine_run_in_background();
170 #endif /* GRPC_CFSTREAM_IOMGR */