blob: 2a45e708df6a499c3b4fa9560cbff19b88e2df38 [file] [log] [blame]
Craig Tillerf2d39b72015-01-21 15:06:08 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tillerf2d39b72015-01-21 15:06:08 -08004 * 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
murgatroid9954070892016-08-08 17:01:18 -070034#include "src/core/lib/iomgr/port.h"
Craig Tillerf2d39b72015-01-21 15:06:08 -080035
murgatroid99623dd4f2016-08-08 17:31:27 -070036#ifdef GRPC_WINSOCK_SOCKET
Craig Tillerf2d39b72015-01-21 15:06:08 -080037
Craig Tiller46714f92015-11-03 11:29:36 -080038#include <grpc/support/log.h>
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010039#include <grpc/support/thd.h>
40
Craig Tiller9533d042016-03-25 17:11:06 -070041#include "src/core/lib/iomgr/iocp_windows.h"
42#include "src/core/lib/iomgr/iomgr_internal.h"
43#include "src/core/lib/iomgr/pollset.h"
44#include "src/core/lib/iomgr/pollset_windows.h"
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +010045
Craig Tiller084c8082015-09-29 11:19:06 -070046gpr_mu grpc_polling_mu;
Craig Tiller3dedb702015-09-24 10:31:26 -070047static grpc_pollset_worker *g_active_poller;
48static grpc_pollset_worker g_global_root_worker;
49
Craig Tiller4c33dba2016-03-31 10:35:00 -070050void grpc_pollset_global_init(void) {
Craig Tiller084c8082015-09-29 11:19:06 -070051 gpr_mu_init(&grpc_polling_mu);
Craig Tiller3dedb702015-09-24 10:31:26 -070052 g_active_poller = NULL;
53 g_global_root_worker.links[GRPC_POLLSET_WORKER_LINK_GLOBAL].next =
Craig Tiller71a0f9d2015-09-28 17:22:01 -070054 g_global_root_worker.links[GRPC_POLLSET_WORKER_LINK_GLOBAL].prev =
55 &g_global_root_worker;
Craig Tillerd1885e02015-07-30 14:32:31 -070056}
57
Craig Tiller4c33dba2016-03-31 10:35:00 -070058void grpc_pollset_global_shutdown(void) { gpr_mu_destroy(&grpc_polling_mu); }
Craig Tillerd1885e02015-07-30 14:32:31 -070059
Craig Tiller71a0f9d2015-09-28 17:22:01 -070060static void remove_worker(grpc_pollset_worker *worker,
Craig Tiller3dedb702015-09-24 10:31:26 -070061 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
Craig Tiller71a0f9d2015-09-28 17:22:01 -070067static int has_workers(grpc_pollset_worker *root,
68 grpc_pollset_worker_link_type type) {
Craig Tiller3dedb702015-09-24 10:31:26 -070069 return root->links[type].next != root;
70}
71
72static 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);
Craig Tillera82950e2015-09-22 12:33:20 -070077 return w;
78 } else {
79 return NULL;
80 }
Craig Tillerd1885e02015-07-30 14:32:31 -070081}
82
Craig Tiller71a0f9d2015-09-28 17:22:01 -070083static void push_front_worker(grpc_pollset_worker *root,
84 grpc_pollset_worker_link_type type,
Craig Tiller3dedb702015-09-24 10:31:26 -070085 grpc_pollset_worker *worker) {
86 worker->links[type].prev = root;
87 worker->links[type].next = worker->links[type].prev->links[type].next;
Craig Tiller71a0f9d2015-09-28 17:22:01 -070088 worker->links[type].prev->links[type].next =
89 worker->links[type].next->links[type].prev = worker;
Craig Tillerd1885e02015-07-30 14:32:31 -070090}
91
Craig Tiller81416962016-02-25 20:22:11 -080092size_t grpc_pollset_size(void) { return sizeof(grpc_pollset); }
Craig Tiller69b093b2016-02-25 19:04:07 -080093
Nicolas "Pixel" Noble0f981e92015-05-03 10:40:56 +020094/* 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
Craig Tiller69b093b2016-02-25 19:04:07 -080099void grpc_pollset_init(grpc_pollset *pollset, gpr_mu **mu) {
100 *mu = &grpc_polling_mu;
Craig Tillera82950e2015-09-22 12:33:20 -0700101 memset(pollset, 0, sizeof(*pollset));
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700102 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;
Nicolas Noblea14ebbb2015-06-22 14:17:34 -0700105}
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +0100106
Craig Tiller82f9bd82015-09-23 09:31:51 -0700107void grpc_pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
108 grpc_closure *closure) {
Nicolas "Pixel" Noble857d2502015-06-25 21:58:18 +0200109 pollset->shutting_down = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700110 grpc_pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST);
Craig Tiller3dedb702015-09-24 10:31:26 -0700111 if (!pollset->is_iocp_worker) {
Craig Tiller91031da2016-12-28 15:44:25 -0800112 grpc_closure_sched(exec_ctx, closure, GRPC_ERROR_NONE);
Craig Tiller3dedb702015-09-24 10:31:26 -0700113 } else {
114 pollset->on_shutdown = closure;
115 }
David Klempnerb5056612015-02-24 14:22:50 -0800116}
117
Craig Tillerb7a59772015-10-01 08:01:58 -0700118void grpc_pollset_destroy(grpc_pollset *pollset) {}
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +0100119
Craig Tiller46714f92015-11-03 11:29:36 -0800120void grpc_pollset_reset(grpc_pollset *pollset) {
121 GPR_ASSERT(pollset->shutting_down);
Craig Tiller3c53bb22015-11-10 14:24:36 +0000122 GPR_ASSERT(
123 !has_workers(&pollset->root_worker, GRPC_POLLSET_WORKER_LINK_POLLSET));
Craig Tiller46714f92015-11-03 11:29:36 -0800124 pollset->shutting_down = 0;
125 pollset->is_iocp_worker = 0;
126 pollset->kicked_without_pollers = 0;
127 pollset->on_shutdown = NULL;
128}
Craig Tillere8b5f622015-11-02 14:15:03 -0800129
Craig Tillera41ac572016-05-17 16:08:17 -0700130grpc_error *grpc_pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
Craig Tiller3e149f32016-05-17 16:11:04 -0700131 grpc_pollset_worker **worker_hdl,
132 gpr_timespec now, gpr_timespec deadline) {
Craig Tillerbd479282016-02-18 08:07:09 -0800133 grpc_pollset_worker worker;
134 *worker_hdl = &worker;
135
Craig Tillerd1885e02015-07-30 14:32:31 -0700136 int added_worker = 0;
Craig Tillerbd479282016-02-18 08:07:09 -0800137 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);
Craig Tillera82950e2015-09-22 12:33:20 -0700144 if (!pollset->kicked_without_pollers && !pollset->shutting_down) {
Craig Tiller3dedb702015-09-24 10:31:26 -0700145 if (g_active_poller == NULL) {
146 grpc_pollset_worker *next_worker;
147 /* become poller */
148 pollset->is_iocp_worker = 1;
Craig Tillerbd479282016-02-18 08:07:09 -0800149 g_active_poller = &worker;
Craig Tiller084c8082015-09-29 11:19:06 -0700150 gpr_mu_unlock(&grpc_polling_mu);
Craig Tiller3dedb702015-09-24 10:31:26 -0700151 grpc_iocp_work(exec_ctx, deadline);
Craig Tiller0b6312e2015-09-30 08:36:27 -0700152 grpc_exec_ctx_flush(exec_ctx);
Craig Tiller084c8082015-09-29 11:19:06 -0700153 gpr_mu_lock(&grpc_polling_mu);
Craig Tiller3dedb702015-09-24 10:31:26 -0700154 pollset->is_iocp_worker = 0;
155 g_active_poller = NULL;
Craig Tiller084c8082015-09-29 11:19:06 -0700156 /* try to get a worker from this pollsets worker list */
Craig Tillerb7a59772015-10-01 08:01:58 -0700157 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 }
Craig Tiller3dedb702015-09-24 10:31:26 -0700164 if (next_worker != NULL) {
Craig Tiller084c8082015-09-29 11:19:06 -0700165 next_worker->kicked = 1;
Craig Tiller3dedb702015-09-24 10:31:26 -0700166 gpr_cv_signal(&next_worker->cv);
167 }
Craig Tiller3dedb702015-09-24 10:31:26 -0700168
169 if (pollset->shutting_down && pollset->on_shutdown != NULL) {
Craig Tiller460da032017-01-03 08:45:29 -0800170 grpc_closure_sched(exec_ctx, pollset->on_shutdown, GRPC_ERROR_NONE);
Craig Tiller3dedb702015-09-24 10:31:26 -0700171 pollset->on_shutdown = NULL;
172 }
173 goto done;
174 }
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700175 push_front_worker(&g_global_root_worker, GRPC_POLLSET_WORKER_LINK_GLOBAL,
Craig Tillerbd479282016-02-18 08:07:09 -0800176 &worker);
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700177 push_front_worker(&pollset->root_worker, GRPC_POLLSET_WORKER_LINK_POLLSET,
Craig Tillerbd479282016-02-18 08:07:09 -0800178 &worker);
Craig Tillera82950e2015-09-22 12:33:20 -0700179 added_worker = 1;
Craig Tillerbd479282016-02-18 08:07:09 -0800180 while (!worker.kicked) {
181 if (gpr_cv_wait(&worker.cv, &grpc_polling_mu, deadline)) {
Craig Tiller084c8082015-09-29 11:19:06 -0700182 break;
183 }
184 }
Craig Tillera82950e2015-09-22 12:33:20 -0700185 } else {
186 pollset->kicked_without_pollers = 0;
187 }
Craig Tillerd1885e02015-07-30 14:32:31 -0700188done:
Craig Tiller258f8de2015-09-23 12:32:40 -0700189 if (!grpc_closure_list_empty(exec_ctx->closure_list)) {
Craig Tiller084c8082015-09-29 11:19:06 -0700190 gpr_mu_unlock(&grpc_polling_mu);
Craig Tiller258f8de2015-09-23 12:32:40 -0700191 grpc_exec_ctx_flush(exec_ctx);
Craig Tiller084c8082015-09-29 11:19:06 -0700192 gpr_mu_lock(&grpc_polling_mu);
Craig Tiller258f8de2015-09-23 12:32:40 -0700193 }
Craig Tillera82950e2015-09-22 12:33:20 -0700194 if (added_worker) {
Craig Tillerbd479282016-02-18 08:07:09 -0800195 remove_worker(&worker, GRPC_POLLSET_WORKER_LINK_GLOBAL);
196 remove_worker(&worker, GRPC_POLLSET_WORKER_LINK_POLLSET);
Craig Tillera82950e2015-09-22 12:33:20 -0700197 }
Craig Tiller9114a142016-02-24 08:40:30 -0800198 gpr_cv_destroy(&worker.cv);
Craig Tillerbd479282016-02-18 08:07:09 -0800199 *worker_hdl = NULL;
Craig Tillera41ac572016-05-17 16:08:17 -0700200 return GRPC_ERROR_NONE;
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +0100201}
202
Craig Tiller3e149f32016-05-17 16:11:04 -0700203grpc_error *grpc_pollset_kick(grpc_pollset *p,
204 grpc_pollset_worker *specific_worker) {
Craig Tillera82950e2015-09-22 12:33:20 -0700205 if (specific_worker != NULL) {
206 if (specific_worker == GRPC_POLLSET_KICK_BROADCAST) {
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700207 for (specific_worker =
208 p->root_worker.links[GRPC_POLLSET_WORKER_LINK_POLLSET].next;
Craig Tillera82950e2015-09-22 12:33:20 -0700209 specific_worker != &p->root_worker;
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700210 specific_worker =
211 specific_worker->links[GRPC_POLLSET_WORKER_LINK_POLLSET].next) {
Craig Tiller084c8082015-09-29 11:19:06 -0700212 specific_worker->kicked = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700213 gpr_cv_signal(&specific_worker->cv);
214 }
215 p->kicked_without_pollers = 1;
Craig Tiller3dedb702015-09-24 10:31:26 -0700216 if (p->is_iocp_worker) {
217 grpc_iocp_kick();
218 }
Craig Tillera82950e2015-09-22 12:33:20 -0700219 } else {
Craig Tiller10d3e592016-02-10 10:32:04 -0800220 if (p->is_iocp_worker && g_active_poller == specific_worker) {
221 grpc_iocp_kick();
Craig Tiller3dedb702015-09-24 10:31:26 -0700222 } else {
Craig Tiller084c8082015-09-29 11:19:06 -0700223 specific_worker->kicked = 1;
Craig Tiller3dedb702015-09-24 10:31:26 -0700224 gpr_cv_signal(&specific_worker->cv);
225 }
Craig Tillerd1885e02015-07-30 14:32:31 -0700226 }
Craig Tillera82950e2015-09-22 12:33:20 -0700227 } else {
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700228 specific_worker =
229 pop_front_worker(&p->root_worker, GRPC_POLLSET_WORKER_LINK_POLLSET);
Craig Tillera82950e2015-09-22 12:33:20 -0700230 if (specific_worker != NULL) {
Craig Tiller3dedb702015-09-24 10:31:26 -0700231 grpc_pollset_kick(p, specific_worker);
232 } else if (p->is_iocp_worker) {
233 grpc_iocp_kick();
Craig Tillera82950e2015-09-22 12:33:20 -0700234 } else {
235 p->kicked_without_pollers = 1;
Craig Tillerd1885e02015-07-30 14:32:31 -0700236 }
Craig Tillera82950e2015-09-22 12:33:20 -0700237 }
Craig Tillera41ac572016-05-17 16:08:17 -0700238 return GRPC_ERROR_NONE;
Craig Tillerd1885e02015-07-30 14:32:31 -0700239}
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +0100240
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700241void grpc_kick_poller(void) { grpc_iocp_kick(); }
Craig Tiller26465e82015-09-28 17:08:30 -0700242
murgatroid99623dd4f2016-08-08 17:31:27 -0700243#endif /* GRPC_WINSOCK_SOCKET */