• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2018 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 #include <grpc/support/port_platform.h>
20 
21 #include "src/core/lib/iomgr/port.h"
22 
23 #include <stddef.h>
24 #include <string.h>
25 
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include <grpc/support/sync.h>
29 
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"
35 
36 #include "src/core/lib/debug/trace.h"
37 
38 static grpc_custom_poller_vtable* poller_vtable;
39 
40 struct grpc_pollset {
41   gpr_mu mu;
42 };
43 
pollset_size()44 static size_t pollset_size() { return sizeof(grpc_pollset); }
45 
pollset_global_init()46 static void pollset_global_init() { poller_vtable->init(); }
47 
pollset_global_shutdown()48 static void pollset_global_shutdown() { poller_vtable->shutdown(); }
49 
pollset_init(grpc_pollset * pollset,gpr_mu ** mu)50 static void pollset_init(grpc_pollset* pollset, gpr_mu** mu) {
51   GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
52   gpr_mu_init(&pollset->mu);
53   *mu = &pollset->mu;
54 }
55 
pollset_shutdown(grpc_pollset * pollset,grpc_closure * closure)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);
59 }
60 
pollset_destroy(grpc_pollset * pollset)61 static void pollset_destroy(grpc_pollset* pollset) {
62   GRPC_CUSTOM_IOMGR_ASSERT_SAME_THREAD();
63   gpr_mu_destroy(&pollset->mu);
64 }
65 
pollset_work(grpc_pollset * pollset,grpc_pollset_worker ** worker_hdl,grpc_millis deadline)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;
73   if (deadline > now) {
74     timeout = deadline - now;
75   }
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();
85   }
86   gpr_mu_lock(&pollset->mu);
87   return GRPC_ERROR_NONE;
88 }
89 
pollset_kick(grpc_pollset * pollset,grpc_pollset_worker * specific_worker)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;
95 }
96 
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};
102 
grpc_custom_pollset_init(grpc_custom_poller_vtable * vtable)103 void grpc_custom_pollset_init(grpc_custom_poller_vtable* vtable) {
104   poller_vtable = vtable;
105   grpc_set_pollset_vtable(&custom_pollset_vtable);
106 }
107