Imported Upstream version 1.36.0
[platform/upstream/grpc.git] / src / core / lib / iomgr / iomgr_posix_cfstream.cc
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/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"
48
49 static const char* grpc_cfstream_env_var = "grpc_cfstream";
50 static const char* grpc_cfstream_run_loop_env_var = "GRPC_CFSTREAM_RUN_LOOP";
51
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;
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 }
67
68 static void apple_iomgr_platform_shutdown_background_closure(void) {}
69
70 static bool apple_iomgr_platform_is_any_background_poller_thread(void) {
71   return false;
72 }
73
74 static bool apple_iomgr_platform_add_closure_to_background_poller(
75     grpc_closure* closure, grpc_error* error) {
76   return false;
77 }
78
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};
86
87 static void iomgr_platform_init(void) {
88   grpc_wakeup_fd_global_init();
89   grpc_event_engine_init();
90 }
91
92 static void iomgr_platform_flush(void) {}
93
94 static void iomgr_platform_shutdown(void) {
95   grpc_event_engine_shutdown();
96   grpc_wakeup_fd_global_destroy();
97 }
98
99 static void iomgr_platform_shutdown_background_closure(void) {
100   grpc_shutdown_background_closure();
101 }
102
103 static bool iomgr_platform_is_any_background_poller_thread(void) {
104   return grpc_is_any_background_poller_thread();
105 }
106
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);
110 }
111
112 static grpc_iomgr_platform_vtable vtable = {
113     iomgr_platform_init,
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};
119
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);
143   } else {
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);
149   }
150   grpc_set_timer_impl(&grpc_generic_timer_vtable);
151   grpc_set_resolver_impl(&grpc_posix_resolver_vtable);
152 }
153
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) {
164     return false;
165   } else {
166     return grpc_event_engine_run_in_background();
167   }
168 }
169
170 #endif /* GRPC_CFSTREAM_IOMGR */