Craig Tiller | f2d39b7 | 2015-01-21 15:06:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | f2d39b7 | 2015-01-21 15:06:08 -0800 | [diff] [blame] | 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 | |
murgatroid99 | 5407089 | 2016-08-08 17:01:18 -0700 | [diff] [blame] | 34 | #include "src/core/lib/iomgr/port.h" |
Craig Tiller | f2d39b7 | 2015-01-21 15:06:08 -0800 | [diff] [blame] | 35 | |
murgatroid99 | 623dd4f | 2016-08-08 17:31:27 -0700 | [diff] [blame] | 36 | #ifdef GRPC_WINSOCK_SOCKET |
Craig Tiller | f2d39b7 | 2015-01-21 15:06:08 -0800 | [diff] [blame] | 37 | |
Craig Tiller | 46714f9 | 2015-11-03 11:29:36 -0800 | [diff] [blame] | 38 | #include <grpc/support/log.h> |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 39 | #include <grpc/support/thd.h> |
| 40 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 41 | #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" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 45 | |
Craig Tiller | 084c808 | 2015-09-29 11:19:06 -0700 | [diff] [blame] | 46 | gpr_mu grpc_polling_mu; |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 47 | static grpc_pollset_worker *g_active_poller; |
| 48 | static grpc_pollset_worker g_global_root_worker; |
| 49 | |
Craig Tiller | 4c33dba | 2016-03-31 10:35:00 -0700 | [diff] [blame] | 50 | void grpc_pollset_global_init(void) { |
Craig Tiller | 084c808 | 2015-09-29 11:19:06 -0700 | [diff] [blame] | 51 | gpr_mu_init(&grpc_polling_mu); |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 52 | g_active_poller = NULL; |
| 53 | g_global_root_worker.links[GRPC_POLLSET_WORKER_LINK_GLOBAL].next = |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 54 | g_global_root_worker.links[GRPC_POLLSET_WORKER_LINK_GLOBAL].prev = |
| 55 | &g_global_root_worker; |
Craig Tiller | d1885e0 | 2015-07-30 14:32:31 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Craig Tiller | 4c33dba | 2016-03-31 10:35:00 -0700 | [diff] [blame] | 58 | void grpc_pollset_global_shutdown(void) { gpr_mu_destroy(&grpc_polling_mu); } |
Craig Tiller | d1885e0 | 2015-07-30 14:32:31 -0700 | [diff] [blame] | 59 | |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 60 | static void remove_worker(grpc_pollset_worker *worker, |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 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 | |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 67 | static int has_workers(grpc_pollset_worker *root, |
| 68 | grpc_pollset_worker_link_type type) { |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 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); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 77 | return w; |
| 78 | } else { |
| 79 | return NULL; |
| 80 | } |
Craig Tiller | d1885e0 | 2015-07-30 14:32:31 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 83 | static void push_front_worker(grpc_pollset_worker *root, |
| 84 | grpc_pollset_worker_link_type type, |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 85 | grpc_pollset_worker *worker) { |
| 86 | worker->links[type].prev = root; |
| 87 | worker->links[type].next = worker->links[type].prev->links[type].next; |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 88 | worker->links[type].prev->links[type].next = |
| 89 | worker->links[type].next->links[type].prev = worker; |
Craig Tiller | d1885e0 | 2015-07-30 14:32:31 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Craig Tiller | 8141696 | 2016-02-25 20:22:11 -0800 | [diff] [blame] | 92 | size_t grpc_pollset_size(void) { return sizeof(grpc_pollset); } |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 93 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 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 | |
Craig Tiller | 69b093b | 2016-02-25 19:04:07 -0800 | [diff] [blame] | 99 | void grpc_pollset_init(grpc_pollset *pollset, gpr_mu **mu) { |
| 100 | *mu = &grpc_polling_mu; |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 101 | 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 Noble | a14ebbb | 2015-06-22 14:17:34 -0700 | [diff] [blame] | 104 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 105 | |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 106 | void grpc_pollset_shutdown(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
| 107 | grpc_closure *closure) { |
Nicolas "Pixel" Noble | 857d250 | 2015-06-25 21:58:18 +0200 | [diff] [blame] | 108 | pollset->shutting_down = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 109 | grpc_pollset_kick(pollset, GRPC_POLLSET_KICK_BROADCAST); |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 110 | if (!pollset->is_iocp_worker) { |
Craig Tiller | 91031da | 2016-12-28 15:44:25 -0800 | [diff] [blame] | 111 | grpc_closure_sched(exec_ctx, closure, GRPC_ERROR_NONE); |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 112 | } else { |
| 113 | pollset->on_shutdown = closure; |
| 114 | } |
David Klempner | b505661 | 2015-02-24 14:22:50 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Craig Tiller | b7a5977 | 2015-10-01 08:01:58 -0700 | [diff] [blame] | 117 | void grpc_pollset_destroy(grpc_pollset *pollset) {} |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 118 | |
Craig Tiller | a41ac57 | 2016-05-17 16:08:17 -0700 | [diff] [blame] | 119 | grpc_error *grpc_pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, |
Craig Tiller | 3e149f3 | 2016-05-17 16:11:04 -0700 | [diff] [blame] | 120 | grpc_pollset_worker **worker_hdl, |
| 121 | gpr_timespec now, gpr_timespec deadline) { |
Craig Tiller | bd47928 | 2016-02-18 08:07:09 -0800 | [diff] [blame] | 122 | grpc_pollset_worker worker; |
| 123 | *worker_hdl = &worker; |
| 124 | |
Craig Tiller | d1885e0 | 2015-07-30 14:32:31 -0700 | [diff] [blame] | 125 | int added_worker = 0; |
Craig Tiller | bd47928 | 2016-02-18 08:07:09 -0800 | [diff] [blame] | 126 | 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 Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 133 | if (!pollset->kicked_without_pollers && !pollset->shutting_down) { |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 134 | if (g_active_poller == NULL) { |
| 135 | grpc_pollset_worker *next_worker; |
| 136 | /* become poller */ |
| 137 | pollset->is_iocp_worker = 1; |
Craig Tiller | bd47928 | 2016-02-18 08:07:09 -0800 | [diff] [blame] | 138 | g_active_poller = &worker; |
Craig Tiller | 084c808 | 2015-09-29 11:19:06 -0700 | [diff] [blame] | 139 | gpr_mu_unlock(&grpc_polling_mu); |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 140 | grpc_iocp_work(exec_ctx, deadline); |
Craig Tiller | 0b6312e | 2015-09-30 08:36:27 -0700 | [diff] [blame] | 141 | grpc_exec_ctx_flush(exec_ctx); |
Craig Tiller | 084c808 | 2015-09-29 11:19:06 -0700 | [diff] [blame] | 142 | gpr_mu_lock(&grpc_polling_mu); |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 143 | pollset->is_iocp_worker = 0; |
| 144 | g_active_poller = NULL; |
Craig Tiller | 084c808 | 2015-09-29 11:19:06 -0700 | [diff] [blame] | 145 | /* try to get a worker from this pollsets worker list */ |
Craig Tiller | b7a5977 | 2015-10-01 08:01:58 -0700 | [diff] [blame] | 146 | 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 Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 153 | if (next_worker != NULL) { |
Craig Tiller | 084c808 | 2015-09-29 11:19:06 -0700 | [diff] [blame] | 154 | next_worker->kicked = 1; |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 155 | gpr_cv_signal(&next_worker->cv); |
| 156 | } |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 157 | |
| 158 | if (pollset->shutting_down && pollset->on_shutdown != NULL) { |
Craig Tiller | 460da03 | 2017-01-03 08:45:29 -0800 | [diff] [blame] | 159 | grpc_closure_sched(exec_ctx, pollset->on_shutdown, GRPC_ERROR_NONE); |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 160 | pollset->on_shutdown = NULL; |
| 161 | } |
| 162 | goto done; |
| 163 | } |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 164 | push_front_worker(&g_global_root_worker, GRPC_POLLSET_WORKER_LINK_GLOBAL, |
Craig Tiller | bd47928 | 2016-02-18 08:07:09 -0800 | [diff] [blame] | 165 | &worker); |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 166 | push_front_worker(&pollset->root_worker, GRPC_POLLSET_WORKER_LINK_POLLSET, |
Craig Tiller | bd47928 | 2016-02-18 08:07:09 -0800 | [diff] [blame] | 167 | &worker); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 168 | added_worker = 1; |
Craig Tiller | bd47928 | 2016-02-18 08:07:09 -0800 | [diff] [blame] | 169 | while (!worker.kicked) { |
| 170 | if (gpr_cv_wait(&worker.cv, &grpc_polling_mu, deadline)) { |
Craig Tiller | 084c808 | 2015-09-29 11:19:06 -0700 | [diff] [blame] | 171 | break; |
| 172 | } |
| 173 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 174 | } else { |
| 175 | pollset->kicked_without_pollers = 0; |
| 176 | } |
Craig Tiller | d1885e0 | 2015-07-30 14:32:31 -0700 | [diff] [blame] | 177 | done: |
Craig Tiller | 258f8de | 2015-09-23 12:32:40 -0700 | [diff] [blame] | 178 | if (!grpc_closure_list_empty(exec_ctx->closure_list)) { |
Craig Tiller | 084c808 | 2015-09-29 11:19:06 -0700 | [diff] [blame] | 179 | gpr_mu_unlock(&grpc_polling_mu); |
Craig Tiller | 258f8de | 2015-09-23 12:32:40 -0700 | [diff] [blame] | 180 | grpc_exec_ctx_flush(exec_ctx); |
Craig Tiller | 084c808 | 2015-09-29 11:19:06 -0700 | [diff] [blame] | 181 | gpr_mu_lock(&grpc_polling_mu); |
Craig Tiller | 258f8de | 2015-09-23 12:32:40 -0700 | [diff] [blame] | 182 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 183 | if (added_worker) { |
Craig Tiller | bd47928 | 2016-02-18 08:07:09 -0800 | [diff] [blame] | 184 | remove_worker(&worker, GRPC_POLLSET_WORKER_LINK_GLOBAL); |
| 185 | remove_worker(&worker, GRPC_POLLSET_WORKER_LINK_POLLSET); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 186 | } |
Craig Tiller | 9114a14 | 2016-02-24 08:40:30 -0800 | [diff] [blame] | 187 | gpr_cv_destroy(&worker.cv); |
Craig Tiller | bd47928 | 2016-02-18 08:07:09 -0800 | [diff] [blame] | 188 | *worker_hdl = NULL; |
Craig Tiller | a41ac57 | 2016-05-17 16:08:17 -0700 | [diff] [blame] | 189 | return GRPC_ERROR_NONE; |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Craig Tiller | 3e149f3 | 2016-05-17 16:11:04 -0700 | [diff] [blame] | 192 | grpc_error *grpc_pollset_kick(grpc_pollset *p, |
| 193 | grpc_pollset_worker *specific_worker) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 194 | if (specific_worker != NULL) { |
| 195 | if (specific_worker == GRPC_POLLSET_KICK_BROADCAST) { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 196 | for (specific_worker = |
| 197 | p->root_worker.links[GRPC_POLLSET_WORKER_LINK_POLLSET].next; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 198 | specific_worker != &p->root_worker; |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 199 | specific_worker = |
| 200 | specific_worker->links[GRPC_POLLSET_WORKER_LINK_POLLSET].next) { |
Craig Tiller | 084c808 | 2015-09-29 11:19:06 -0700 | [diff] [blame] | 201 | specific_worker->kicked = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 202 | gpr_cv_signal(&specific_worker->cv); |
| 203 | } |
| 204 | p->kicked_without_pollers = 1; |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 205 | if (p->is_iocp_worker) { |
| 206 | grpc_iocp_kick(); |
| 207 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 208 | } else { |
Craig Tiller | 10d3e59 | 2016-02-10 10:32:04 -0800 | [diff] [blame] | 209 | if (p->is_iocp_worker && g_active_poller == specific_worker) { |
| 210 | grpc_iocp_kick(); |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 211 | } else { |
Craig Tiller | 084c808 | 2015-09-29 11:19:06 -0700 | [diff] [blame] | 212 | specific_worker->kicked = 1; |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 213 | gpr_cv_signal(&specific_worker->cv); |
| 214 | } |
Craig Tiller | d1885e0 | 2015-07-30 14:32:31 -0700 | [diff] [blame] | 215 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 216 | } else { |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 217 | specific_worker = |
| 218 | pop_front_worker(&p->root_worker, GRPC_POLLSET_WORKER_LINK_POLLSET); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 219 | if (specific_worker != NULL) { |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 220 | grpc_pollset_kick(p, specific_worker); |
| 221 | } else if (p->is_iocp_worker) { |
| 222 | grpc_iocp_kick(); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 223 | } else { |
| 224 | p->kicked_without_pollers = 1; |
Craig Tiller | d1885e0 | 2015-07-30 14:32:31 -0700 | [diff] [blame] | 225 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 226 | } |
Craig Tiller | a41ac57 | 2016-05-17 16:08:17 -0700 | [diff] [blame] | 227 | return GRPC_ERROR_NONE; |
Craig Tiller | d1885e0 | 2015-07-30 14:32:31 -0700 | [diff] [blame] | 228 | } |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame] | 229 | |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 230 | void grpc_kick_poller(void) { grpc_iocp_kick(); } |
Craig Tiller | 26465e8 | 2015-09-28 17:08:30 -0700 | [diff] [blame] | 231 | |
murgatroid99 | 623dd4f | 2016-08-08 17:31:27 -0700 | [diff] [blame] | 232 | #endif /* GRPC_WINSOCK_SOCKET */ |