3 * Copyright 2018 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"
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/sync.h>
30 #include "src/core/lib/iomgr/closure.h"
31 #include "src/core/lib/iomgr/iomgr_custom.h"
32 #include "src/core/lib/iomgr/pollset.h"
33 #include "src/core/lib/iomgr/pollset_custom.h"
34 #include "src/core/lib/iomgr/timer.h"
36 #include "src/core/lib/debug/trace.h"
38 static grpc_custom_poller_vtable* poller_vtable;
44 static size_t pollset_size() { return sizeof(grpc_pollset); }
46 static void pollset_global_init() { poller_vtable->init(); }
48 static void pollset_global_shutdown() { poller_vtable->shutdown(); }
50 static void pollset_init(grpc_pollset* pollset, gpr_mu** mu) {
51 GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
52 gpr_mu_init(&pollset->mu);
56 static void pollset_shutdown(grpc_pollset* pollset, grpc_closure* closure) {
57 GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
58 GRPC_CLOSURE_SCHED(closure, GRPC_ERROR_NONE);
61 static void pollset_destroy(grpc_pollset* pollset) {
62 GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
63 gpr_mu_destroy(&pollset->mu);
66 static grpc_error* pollset_work(grpc_pollset* pollset,
67 grpc_pollset_worker** worker_hdl,
68 grpc_millis deadline) {
69 GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
70 gpr_mu_unlock(&pollset->mu);
71 grpc_millis now = grpc_core::ExecCtx::Get()->Now();
72 grpc_millis timeout = 0;
74 timeout = deadline - now;
76 // We yield here because the poll() call might yield
77 // control back to the application
78 grpc_core::ExecCtx* curr = grpc_core::ExecCtx::Get();
79 grpc_core::ExecCtx::Set(nullptr);
80 poller_vtable->poll(static_cast<size_t>(timeout));
81 grpc_core::ExecCtx::Set(curr);
82 grpc_core::ExecCtx::Get()->InvalidateNow();
83 if (grpc_core::ExecCtx::Get()->HasWork()) {
84 grpc_core::ExecCtx::Get()->Flush();
86 gpr_mu_lock(&pollset->mu);
87 return GRPC_ERROR_NONE;
90 static grpc_error* pollset_kick(grpc_pollset* pollset,
91 grpc_pollset_worker* specific_worker) {
92 GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
93 poller_vtable->kick();
94 return GRPC_ERROR_NONE;
97 grpc_pollset_vtable custom_pollset_vtable = {
98 pollset_global_init, pollset_global_shutdown,
99 pollset_init, pollset_shutdown,
100 pollset_destroy, pollset_work,
101 pollset_kick, pollset_size};
103 void grpc_custom_pollset_init(grpc_custom_poller_vtable* vtable) {
104 poller_vtable = vtable;
105 grpc_set_pollset_vtable(&custom_pollset_vtable);