1 // Copyright 2021 The gRPC Authors
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include <grpc/support/port_platform.h>
16 #ifdef GRPC_USE_EVENT_ENGINE
17 #include <grpc/event_engine/event_engine.h>
19 #include "src/core/lib/debug/trace.h"
20 #include "src/core/lib/iomgr/closure.h"
21 #include "src/core/lib/iomgr/event_engine/iomgr.h"
22 #include "src/core/lib/iomgr/iomgr_internal.h"
23 #include "src/core/lib/iomgr/tcp_client.h"
24 #include "src/core/lib/iomgr/tcp_server.h"
25 #include "src/core/lib/iomgr/timer.h"
26 #include "src/core/lib/surface/init.h"
28 extern grpc_tcp_client_vtable grpc_event_engine_tcp_client_vtable;
29 extern grpc_tcp_server_vtable grpc_event_engine_tcp_server_vtable;
30 extern grpc_timer_vtable grpc_event_engine_timer_vtable;
31 extern grpc_pollset_vtable grpc_event_engine_pollset_vtable;
32 extern grpc_pollset_set_vtable grpc_event_engine_pollset_set_vtable;
33 extern grpc_address_resolver_vtable grpc_event_engine_resolver_vtable;
35 // Disabled by default. grpc_polling_trace must be defined in all iomgr
36 // implementations due to its usage in lockfree_event.
37 grpc_core::DebugOnlyTraceFlag grpc_polling_trace(false, "polling");
41 using ::grpc_event_engine::experimental::DefaultEventEngineFactory;
42 using ::grpc_event_engine::experimental::EventEngine;
44 EventEngine* g_event_engine = nullptr;
46 // TODO(nnoble): Instantiate the default EventEngine if none have been provided.
47 void iomgr_platform_init(void) { GPR_ASSERT(g_event_engine != nullptr); }
49 void iomgr_platform_flush(void) {}
51 void iomgr_platform_shutdown(void) {
52 delete g_event_engine;
53 g_event_engine = nullptr;
56 void iomgr_platform_shutdown_background_closure(void) {}
58 bool iomgr_platform_is_any_background_poller_thread(void) {
59 return g_event_engine->IsWorkerThread();
62 bool iomgr_platform_add_closure_to_background_poller(
63 grpc_closure* /* closure */, grpc_error* /* error */) {
67 grpc_iomgr_platform_vtable vtable = {
70 iomgr_platform_shutdown,
71 iomgr_platform_shutdown_background_closure,
72 iomgr_platform_is_any_background_poller_thread,
73 iomgr_platform_add_closure_to_background_poller};
77 void grpc_set_default_iomgr_platform() {
78 grpc_set_tcp_client_impl(&grpc_event_engine_tcp_client_vtable);
79 grpc_set_tcp_server_impl(&grpc_event_engine_tcp_server_vtable);
80 grpc_set_timer_impl(&grpc_event_engine_timer_vtable);
81 grpc_set_pollset_vtable(&grpc_event_engine_pollset_vtable);
82 grpc_set_pollset_set_vtable(&grpc_event_engine_pollset_set_vtable);
83 grpc_set_resolver_impl(&grpc_event_engine_resolver_vtable);
84 grpc_set_iomgr_platform_vtable(&vtable);
87 bool grpc_iomgr_run_in_background() { return false; }
89 grpc_event_engine::experimental::EventEngine* grpc_iomgr_event_engine() {
90 return g_event_engine;
95 void SetDefaultEventEngine(
96 std::unique_ptr<grpc_event_engine::experimental::EventEngine>
98 GPR_ASSERT(g_event_engine == nullptr);
99 g_event_engine = event_engine.release();
102 } // namespace grpc_core
104 #endif // GRPC_USE_EVENT_ENGINE