blob: 17043c1ea1ada25f21946d249f1721bd26a1cb41 [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 Tiller71a0f9d2015-09-28 17:22:01 -0700101 pollset->root_worker.links[GRPC_POLLSET_WORKER_LINK_POLLSET].next =
102 pollset->root_worker.links[GRPC_POLLSET_WORKER_LINK_POLLSET].prev =
103 &pollset->root_worker;
Nicolas Noblea14ebbb2015-06-22 14:17:34 -0700104}
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +0100105
Craig Tiller82f9bd82015-09-23 09:31:51 -0700106void grpc_pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
107 grpc_closure *closure) {
Nicolas "Pixel" Noble857d2502015-06-25 21:58:18 +0200108 pollset->shutting_down = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700109 grpc_pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST);
Craig Tiller3dedb702015-09-24 10:31:26 -0700110 if (!pollset->is_iocp_worker) {
Craig Tiller91031da2016-12-28 15:44:25 -0800111 grpc_closure_sched(exec_ctx, closure, GRPC_ERROR_NONE);
Craig Tiller3dedb702015-09-24 10:31:26 -0700112 } else {
113 pollset->on_shutdown = closure;
114 }
David Klempnerb5056612015-02-24 14:22:50 -0800115}
116
Craig Tillerb7a59772015-10-01 08:01:58 -0700117void grpc_pollset_destroy(grpc_pollset *pollset) {}
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +0100118
Craig Tillera41ac572016-05-17 16:08:17 -0700119grpc_error *grpc_pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
Craig Tiller3e149f32016-05-17 16:11:04 -0700120 grpc_pollset_worker **worker_hdl,
121 gpr_timespec now, gpr_timespec deadline) {
Craig Tillerbd479282016-02-18 08:07:09 -0800122 grpc_pollset_worker worker;
123 *worker_hdl = &worker;
124
Craig Tillerd1885e02015-07-30 14:32:31 -0700125 int added_worker = 0;
Craig Tillerbd479282016-02-18 08:07:09 -0800126 worker.links[GRPC_POLLSET_WORKER_LINK_POLLSET].next =
127 worker.links[GRPC_POLLSET_WORKER_LINK_POLLSET].prev =
128 worker.links[GRPC_POLLSET_WORKER_LINK_GLOBAL].next =
129 worker.links[GRPC_POLLSET_WORKER_LINK_GLOBAL].prev = NULL;
130 worker.kicked = 0;
131 worker.pollset = pollset;
132 gpr_cv_init(&worker.cv);
Craig Tillera82950e2015-09-22 12:33:20 -0700133 if (!pollset->kicked_without_pollers && !pollset->shutting_down) {
Craig Tiller3dedb702015-09-24 10:31:26 -0700134 if (g_active_poller == NULL) {
135 grpc_pollset_worker *next_worker;
136 /* become poller */
137 pollset->is_iocp_worker = 1;
Craig Tillerbd479282016-02-18 08:07:09 -0800138 g_active_poller = &worker;
Craig Tiller084c8082015-09-29 11:19:06 -0700139 gpr_mu_unlock(&grpc_polling_mu);
Craig Tiller3dedb702015-09-24 10:31:26 -0700140 grpc_iocp_work(exec_ctx, deadline);
Craig Tiller0b6312e2015-09-30 08:36:27 -0700141 grpc_exec_ctx_flush(exec_ctx);
Craig Tiller084c8082015-09-29 11:19:06 -0700142 gpr_mu_lock(&grpc_polling_mu);
Craig Tiller3dedb702015-09-24 10:31:26 -0700143 pollset->is_iocp_worker = 0;
144 g_active_poller = NULL;
Craig Tiller084c8082015-09-29 11:19:06 -0700145 /* try to get a worker from this pollsets worker list */
Craig Tillerb7a59772015-10-01 08:01:58 -0700146 next_worker = pop_front_worker(&pollset->root_worker,
147 GRPC_POLLSET_WORKER_LINK_POLLSET);
148 if (next_worker == NULL) {
149 /* try to get a worker from the global list */
150 next_worker = pop_front_worker(&g_global_root_worker,
151 GRPC_POLLSET_WORKER_LINK_GLOBAL);
152 }
Craig Tiller3dedb702015-09-24 10:31:26 -0700153 if (next_worker != NULL) {
Craig Tiller084c8082015-09-29 11:19:06 -0700154 next_worker->kicked = 1;
Craig Tiller3dedb702015-09-24 10:31:26 -0700155 gpr_cv_signal(&next_worker->cv);
156 }
Craig Tiller3dedb702015-09-24 10:31:26 -0700157
158 if (pollset->shutting_down && pollset->on_shutdown != NULL) {
Craig Tiller460da032017-01-03 08:45:29 -0800159 grpc_closure_sched(exec_ctx, pollset->on_shutdown, GRPC_ERROR_NONE);
Craig Tiller3dedb702015-09-24 10:31:26 -0700160 pollset->on_shutdown = NULL;
161 }
162 goto done;
163 }
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700164 push_front_worker(&g_global_root_worker, GRPC_POLLSET_WORKER_LINK_GLOBAL,
Craig Tillerbd479282016-02-18 08:07:09 -0800165 &worker);
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700166 push_front_worker(&pollset->root_worker, GRPC_POLLSET_WORKER_LINK_POLLSET,
Craig Tillerbd479282016-02-18 08:07:09 -0800167 &worker);
Craig Tillera82950e2015-09-22 12:33:20 -0700168 added_worker = 1;
Craig Tillerbd479282016-02-18 08:07:09 -0800169 while (!worker.kicked) {
170 if (gpr_cv_wait(&worker.cv, &grpc_polling_mu, deadline)) {
Craig Tiller084c8082015-09-29 11:19:06 -0700171 break;
172 }
173 }
Craig Tillera82950e2015-09-22 12:33:20 -0700174 } else {
175 pollset->kicked_without_pollers = 0;
176 }
Craig Tillerd1885e02015-07-30 14:32:31 -0700177done:
Craig Tiller258f8de2015-09-23 12:32:40 -0700178 if (!grpc_closure_list_empty(exec_ctx->closure_list)) {
Craig Tiller084c8082015-09-29 11:19:06 -0700179 gpr_mu_unlock(&grpc_polling_mu);
Craig Tiller258f8de2015-09-23 12:32:40 -0700180 grpc_exec_ctx_flush(exec_ctx);
Craig Tiller084c8082015-09-29 11:19:06 -0700181 gpr_mu_lock(&grpc_polling_mu);
Craig Tiller258f8de2015-09-23 12:32:40 -0700182 }
Craig Tillera82950e2015-09-22 12:33:20 -0700183 if (added_worker) {
Craig Tillerbd479282016-02-18 08:07:09 -0800184 remove_worker(&worker, GRPC_POLLSET_WORKER_LINK_GLOBAL);
185 remove_worker(&worker, GRPC_POLLSET_WORKER_LINK_POLLSET);
Craig Tillera82950e2015-09-22 12:33:20 -0700186 }
Craig Tiller9114a142016-02-24 08:40:30 -0800187 gpr_cv_destroy(&worker.cv);
Craig Tillerbd479282016-02-18 08:07:09 -0800188 *worker_hdl = NULL;
Craig Tillera41ac572016-05-17 16:08:17 -0700189 return GRPC_ERROR_NONE;
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +0100190}
191
Craig Tiller3e149f32016-05-17 16:11:04 -0700192grpc_error *grpc_pollset_kick(grpc_pollset *p,
193 grpc_pollset_worker *specific_worker) {
Craig Tillera82950e2015-09-22 12:33:20 -0700194 if (specific_worker != NULL) {
195 if (specific_worker == GRPC_POLLSET_KICK_BROADCAST) {
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700196 for (specific_worker =
197 p->root_worker.links[GRPC_POLLSET_WORKER_LINK_POLLSET].next;
Craig Tillera82950e2015-09-22 12:33:20 -0700198 specific_worker != &p->root_worker;
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700199 specific_worker =
200 specific_worker->links[GRPC_POLLSET_WORKER_LINK_POLLSET].next) {
Craig Tiller084c8082015-09-29 11:19:06 -0700201 specific_worker->kicked = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700202 gpr_cv_signal(&specific_worker->cv);
203 }
204 p->kicked_without_pollers = 1;
Craig Tiller3dedb702015-09-24 10:31:26 -0700205 if (p->is_iocp_worker) {
206 grpc_iocp_kick();
207 }
Craig Tillera82950e2015-09-22 12:33:20 -0700208 } else {
Craig Tiller10d3e592016-02-10 10:32:04 -0800209 if (p->is_iocp_worker && g_active_poller == specific_worker) {
210 grpc_iocp_kick();
Craig Tiller3dedb702015-09-24 10:31:26 -0700211 } else {
Craig Tiller084c8082015-09-29 11:19:06 -0700212 specific_worker->kicked = 1;
Craig Tiller3dedb702015-09-24 10:31:26 -0700213 gpr_cv_signal(&specific_worker->cv);
214 }
Craig Tillerd1885e02015-07-30 14:32:31 -0700215 }
Craig Tillera82950e2015-09-22 12:33:20 -0700216 } else {
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700217 specific_worker =
218 pop_front_worker(&p->root_worker, GRPC_POLLSET_WORKER_LINK_POLLSET);
Craig Tillera82950e2015-09-22 12:33:20 -0700219 if (specific_worker != NULL) {
Craig Tiller3dedb702015-09-24 10:31:26 -0700220 grpc_pollset_kick(p, specific_worker);
221 } else if (p->is_iocp_worker) {
222 grpc_iocp_kick();
Craig Tillera82950e2015-09-22 12:33:20 -0700223 } else {
224 p->kicked_without_pollers = 1;
Craig Tillerd1885e02015-07-30 14:32:31 -0700225 }
Craig Tillera82950e2015-09-22 12:33:20 -0700226 }
Craig Tillera41ac572016-05-17 16:08:17 -0700227 return GRPC_ERROR_NONE;
Craig Tillerd1885e02015-07-30 14:32:31 -0700228}
Nicolas "Pixel" Noble21f627a2015-02-04 01:31:14 +0100229
Craig Tiller71a0f9d2015-09-28 17:22:01 -0700230void grpc_kick_poller(void) { grpc_iocp_kick(); }
Craig Tiller26465e82015-09-28 17:08:30 -0700231
murgatroid99623dd4f2016-08-08 17:31:27 -0700232#endif /* GRPC_WINSOCK_SOCKET */