Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(326)

Side by Side Diff: third_party/grpc/src/core/iomgr/pollset_windows.c

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 #include <grpc/support/port_platform.h>
35
36 #ifdef GPR_WINSOCK_SOCKET
37
38 #include <grpc/support/log.h>
39 #include <grpc/support/thd.h>
40
41 #include "src/core/iomgr/iomgr_internal.h"
42 #include "src/core/iomgr/iocp_windows.h"
43 #include "src/core/iomgr/pollset.h"
44 #include "src/core/iomgr/pollset_windows.h"
45
46 gpr_mu grpc_polling_mu;
47 static grpc_pollset_worker *g_active_poller;
48 static grpc_pollset_worker g_global_root_worker;
49
50 void grpc_pollset_global_init() {
51 gpr_mu_init(&grpc_polling_mu);
52 g_active_poller = NULL;
53 g_global_root_worker.links[GRPC_POLLSET_WORKER_LINK_GLOBAL].next =
54 g_global_root_worker.links[GRPC_POLLSET_WORKER_LINK_GLOBAL].prev =
55 &g_global_root_worker;
56 }
57
58 void grpc_pollset_global_shutdown() { gpr_mu_destroy(&grpc_polling_mu); }
59
60 static void remove_worker(grpc_pollset_worker *worker,
61 grpc_pollset_worker_link_type type) {
62 worker->links[type].prev->links[type].next = worker->links[type].next;
63 worker->links[type].next->links[type].prev = worker->links[type].prev;
64 worker->links[type].next = worker->links[type].prev = worker;
65 }
66
67 static int has_workers(grpc_pollset_worker *root,
68 grpc_pollset_worker_link_type type) {
69 return root->links[type].next != root;
70 }
71
72 static grpc_pollset_worker *pop_front_worker(
73 grpc_pollset_worker *root, grpc_pollset_worker_link_type type) {
74 if (has_workers(root, type)) {
75 grpc_pollset_worker *w = root->links[type].next;
76 remove_worker(w, type);
77 return w;
78 } else {
79 return NULL;
80 }
81 }
82
83 static void push_front_worker(grpc_pollset_worker *root,
84 grpc_pollset_worker_link_type type,
85 grpc_pollset_worker *worker) {
86 worker->links[type].prev = root;
87 worker->links[type].next = worker->links[type].prev->links[type].next;
88 worker->links[type].prev->links[type].next =
89 worker->links[type].next->links[type].prev = worker;
90 }
91
92 size_t grpc_pollset_size(void) { return sizeof(grpc_pollset); }
93
94 /* There isn't really any such thing as a pollset under Windows, due to the
95 nature of the IO completion ports. We're still going to provide a minimal
96 set of features for the sake of the rest of grpc. But grpc_pollset_work
97 won't actually do any polling, and return as quickly as possible. */
98
99 void grpc_pollset_init(grpc_pollset *pollset, gpr_mu **mu) {
100 *mu = &grpc_polling_mu;
101 memset(pollset, 0, sizeof(*pollset));
102 pollset->root_worker.links[GRPC_POLLSET_WORKER_LINK_POLLSET].next =
103 pollset->root_worker.links[GRPC_POLLSET_WORKER_LINK_POLLSET].prev =
104 &pollset->root_worker;
105 }
106
107 void grpc_pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
108 grpc_closure *closure) {
109 pollset->shutting_down = 1;
110 grpc_pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST);
111 if (!pollset->is_iocp_worker) {
112 grpc_exec_ctx_enqueue(exec_ctx, closure, true, NULL);
113 } else {
114 pollset->on_shutdown = closure;
115 }
116 }
117
118 void grpc_pollset_destroy(grpc_pollset *pollset) {}
119
120 void grpc_pollset_reset(grpc_pollset *pollset) {
121 GPR_ASSERT(pollset->shutting_down);
122 GPR_ASSERT(
123 !has_workers(&pollset->root_worker, GRPC_POLLSET_WORKER_LINK_POLLSET));
124 pollset->shutting_down = 0;
125 pollset->is_iocp_worker = 0;
126 pollset->kicked_without_pollers = 0;
127 pollset->on_shutdown = NULL;
128 }
129
130 void grpc_pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
131 grpc_pollset_worker **worker_hdl, gpr_timespec now,
132 gpr_timespec deadline) {
133 grpc_pollset_worker worker;
134 *worker_hdl = &worker;
135
136 int added_worker = 0;
137 worker.links[GRPC_POLLSET_WORKER_LINK_POLLSET].next =
138 worker.links[GRPC_POLLSET_WORKER_LINK_POLLSET].prev =
139 worker.links[GRPC_POLLSET_WORKER_LINK_GLOBAL].next =
140 worker.links[GRPC_POLLSET_WORKER_LINK_GLOBAL].prev = NULL;
141 worker.kicked = 0;
142 worker.pollset = pollset;
143 gpr_cv_init(&worker.cv);
144 if (!pollset->kicked_without_pollers && !pollset->shutting_down) {
145 if (g_active_poller == NULL) {
146 grpc_pollset_worker *next_worker;
147 /* become poller */
148 pollset->is_iocp_worker = 1;
149 g_active_poller = &worker;
150 gpr_mu_unlock(&grpc_polling_mu);
151 grpc_iocp_work(exec_ctx, deadline);
152 grpc_exec_ctx_flush(exec_ctx);
153 gpr_mu_lock(&grpc_polling_mu);
154 pollset->is_iocp_worker = 0;
155 g_active_poller = NULL;
156 /* try to get a worker from this pollsets worker list */
157 next_worker = pop_front_worker(&pollset->root_worker,
158 GRPC_POLLSET_WORKER_LINK_POLLSET);
159 if (next_worker == NULL) {
160 /* try to get a worker from the global list */
161 next_worker = pop_front_worker(&g_global_root_worker,
162 GRPC_POLLSET_WORKER_LINK_GLOBAL);
163 }
164 if (next_worker != NULL) {
165 next_worker->kicked = 1;
166 gpr_cv_signal(&next_worker->cv);
167 }
168
169 if (pollset->shutting_down && pollset->on_shutdown != NULL) {
170 grpc_exec_ctx_enqueue(exec_ctx, pollset->on_shutdown, true, NULL);
171 pollset->on_shutdown = NULL;
172 }
173 goto done;
174 }
175 push_front_worker(&g_global_root_worker, GRPC_POLLSET_WORKER_LINK_GLOBAL,
176 &worker);
177 push_front_worker(&pollset->root_worker, GRPC_POLLSET_WORKER_LINK_POLLSET,
178 &worker);
179 added_worker = 1;
180 while (!worker.kicked) {
181 if (gpr_cv_wait(&worker.cv, &grpc_polling_mu, deadline)) {
182 break;
183 }
184 }
185 } else {
186 pollset->kicked_without_pollers = 0;
187 }
188 done:
189 if (!grpc_closure_list_empty(exec_ctx->closure_list)) {
190 gpr_mu_unlock(&grpc_polling_mu);
191 grpc_exec_ctx_flush(exec_ctx);
192 gpr_mu_lock(&grpc_polling_mu);
193 }
194 if (added_worker) {
195 remove_worker(&worker, GRPC_POLLSET_WORKER_LINK_GLOBAL);
196 remove_worker(&worker, GRPC_POLLSET_WORKER_LINK_POLLSET);
197 }
198 gpr_cv_destroy(&worker.cv);
199 *worker_hdl = NULL;
200 }
201
202 void grpc_pollset_kick(grpc_pollset *p, grpc_pollset_worker *specific_worker) {
203 if (specific_worker != NULL) {
204 if (specific_worker == GRPC_POLLSET_KICK_BROADCAST) {
205 for (specific_worker =
206 p->root_worker.links[GRPC_POLLSET_WORKER_LINK_POLLSET].next;
207 specific_worker != &p->root_worker;
208 specific_worker =
209 specific_worker->links[GRPC_POLLSET_WORKER_LINK_POLLSET].next) {
210 specific_worker->kicked = 1;
211 gpr_cv_signal(&specific_worker->cv);
212 }
213 p->kicked_without_pollers = 1;
214 if (p->is_iocp_worker) {
215 grpc_iocp_kick();
216 }
217 } else {
218 if (p->is_iocp_worker && g_active_poller == specific_worker) {
219 grpc_iocp_kick();
220 } else {
221 specific_worker->kicked = 1;
222 gpr_cv_signal(&specific_worker->cv);
223 }
224 }
225 } else {
226 specific_worker =
227 pop_front_worker(&p->root_worker, GRPC_POLLSET_WORKER_LINK_POLLSET);
228 if (specific_worker != NULL) {
229 grpc_pollset_kick(p, specific_worker);
230 } else if (p->is_iocp_worker) {
231 grpc_iocp_kick();
232 } else {
233 p->kicked_without_pollers = 1;
234 }
235 }
236 }
237
238 void grpc_kick_poller(void) { grpc_iocp_kick(); }
239
240 #endif /* GPR_WINSOCK_SOCKET */
OLDNEW
« no previous file with comments | « third_party/grpc/src/core/iomgr/pollset_windows.h ('k') | third_party/grpc/src/core/iomgr/resolve_address.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698