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 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/iomgr/port.h"
23 #ifdef GRPC_CFSTREAM_IOMGR
25 #include "src/core/lib/debug/trace.h"
26 #include "src/core/lib/iomgr/ev_posix.h"
27 #include "src/core/lib/iomgr/iomgr_internal.h"
28 #include "src/core/lib/iomgr/iomgr_posix.h"
29 #include "src/core/lib/iomgr/resolve_address.h"
30 #include "src/core/lib/iomgr/tcp_client.h"
31 #include "src/core/lib/iomgr/tcp_posix.h"
32 #include "src/core/lib/iomgr/tcp_server.h"
33 #include "src/core/lib/iomgr/timer.h"
35 static const char* grpc_cfstream_env_var = "grpc_cfstream";
37 extern grpc_tcp_server_vtable grpc_posix_tcp_server_vtable;
38 extern grpc_tcp_client_vtable grpc_posix_tcp_client_vtable;
39 extern grpc_tcp_client_vtable grpc_cfstream_client_vtable;
40 extern grpc_timer_vtable grpc_generic_timer_vtable;
41 extern grpc_pollset_vtable grpc_posix_pollset_vtable;
42 extern grpc_pollset_set_vtable grpc_posix_pollset_set_vtable;
43 extern grpc_address_resolver_vtable grpc_posix_resolver_vtable;
45 static void iomgr_platform_init(void) {
46 grpc_wakeup_fd_global_init();
47 grpc_event_engine_init();
50 static void iomgr_platform_flush(void) {}
52 static void iomgr_platform_shutdown(void) {
53 grpc_event_engine_shutdown();
54 grpc_wakeup_fd_global_destroy();
57 static void iomgr_platform_shutdown_background_closure(void) {
58 grpc_shutdown_background_closure();
61 static bool iomgr_platform_is_any_background_poller_thread(void) {
62 return grpc_is_any_background_poller_thread();
65 static bool iomgr_platform_add_closure_to_background_poller(
66 grpc_closure* closure, grpc_error* error) {
67 return grpc_add_closure_to_background_poller(closure, error);
70 static grpc_iomgr_platform_vtable vtable = {
73 iomgr_platform_shutdown,
74 iomgr_platform_shutdown_background_closure,
75 iomgr_platform_is_any_background_poller_thread,
76 iomgr_platform_add_closure_to_background_poller};
78 void grpc_set_default_iomgr_platform() {
79 char* enable_cfstream = getenv(grpc_cfstream_env_var);
80 grpc_tcp_client_vtable* client_vtable = &grpc_posix_tcp_client_vtable;
81 // CFStream is enabled by default on iOS, and disabled by default on other
82 // platforms. Defaults can be overriden by setting the grpc_cfstream
83 // environment variable.
85 if (enable_cfstream == nullptr || enable_cfstream[0] == '1') {
86 client_vtable = &grpc_cfstream_client_vtable;
89 if (enable_cfstream != nullptr && enable_cfstream[0] == '1') {
90 client_vtable = &grpc_cfstream_client_vtable;
94 grpc_set_tcp_client_impl(client_vtable);
95 grpc_set_tcp_server_impl(&grpc_posix_tcp_server_vtable);
96 grpc_set_timer_impl(&grpc_generic_timer_vtable);
97 grpc_set_pollset_vtable(&grpc_posix_pollset_vtable);
98 grpc_set_pollset_set_vtable(&grpc_posix_pollset_set_vtable);
99 grpc_set_resolver_impl(&grpc_posix_resolver_vtable);
100 grpc_set_iomgr_platform_vtable(&vtable);
103 bool grpc_iomgr_run_in_background() {
104 return grpc_event_engine_run_in_background();
107 #endif /* GRPC_CFSTREAM_IOMGR */