Craig Tiller | f2d39b7 | 2015-01-21 15:06:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2014, 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 | |
Craig Tiller | d14a1a5 | 2015-01-21 15:26:29 -0800 | [diff] [blame] | 34 | #include <grpc/support/port_platform.h> |
Craig Tiller | f2d39b7 | 2015-01-21 15:06:08 -0800 | [diff] [blame] | 35 | |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame^] | 36 | #ifdef GPR_WINSOCK_SOCKET |
Craig Tiller | f2d39b7 | 2015-01-21 15:06:08 -0800 | [diff] [blame] | 37 | |
Nicolas "Pixel" Noble | 21f627a | 2015-02-04 01:31:14 +0100 | [diff] [blame^] | 38 | #include <winsock2.h> |
| 39 | |
| 40 | #include <grpc/support/log.h> |
| 41 | #include <grpc/support/thd.h> |
| 42 | |
| 43 | #include "src/core/iomgr/alarm_internal.h" |
| 44 | #include "src/core/iomgr/socket_windows.h" |
| 45 | #include "src/core/iomgr/iomgr_internal.h" |
| 46 | #include "src/core/iomgr/pollset_windows.h" |
| 47 | |
| 48 | static grpc_pollset g_global_pollset; |
| 49 | static ULONG g_pollset_kick_token; |
| 50 | static OVERLAPPED g_pollset_custom_overlap; |
| 51 | |
| 52 | static gpr_event g_shutdown_global_poller; |
| 53 | static gpr_event g_global_poller_done; |
| 54 | |
| 55 | void grpc_pollset_init(grpc_pollset *pollset) { |
| 56 | pollset->iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, |
| 57 | (ULONG_PTR)NULL, 0); |
| 58 | GPR_ASSERT(pollset->iocp); |
| 59 | } |
| 60 | |
| 61 | void grpc_pollset_destroy(grpc_pollset *pollset) { |
| 62 | BOOL status; |
| 63 | status = CloseHandle(pollset->iocp); |
| 64 | GPR_ASSERT(status); |
| 65 | } |
| 66 | |
| 67 | static int pollset_poll(grpc_pollset *pollset, |
| 68 | gpr_timespec deadline, gpr_timespec now) { |
| 69 | BOOL success; |
| 70 | DWORD bytes = 0; |
| 71 | DWORD flags = 0; |
| 72 | ULONG_PTR completion_key; |
| 73 | LPOVERLAPPED overlapped; |
| 74 | gpr_timespec wait_time = gpr_time_sub(deadline, now); |
| 75 | grpc_winsocket *socket; |
| 76 | grpc_winsocket_callback_info *info; |
| 77 | void(*f)(void *, int) = NULL; |
| 78 | void *opaque = NULL; |
| 79 | success = GetQueuedCompletionStatus(pollset->iocp, &bytes, |
| 80 | &completion_key, &overlapped, |
| 81 | gpr_time_to_millis(wait_time)); |
| 82 | |
| 83 | if (!success && !overlapped) { |
| 84 | /* The deadline got attained. */ |
| 85 | return 0; |
| 86 | } |
| 87 | GPR_ASSERT(completion_key && overlapped); |
| 88 | if (overlapped == &g_pollset_custom_overlap) { |
| 89 | if (completion_key == (ULONG_PTR) &g_pollset_kick_token) { |
| 90 | /* We were awoken from a kick. */ |
| 91 | gpr_log(GPR_DEBUG, "pollset_poll - got a kick"); |
| 92 | return 1; |
| 93 | } |
| 94 | gpr_log(GPR_ERROR, "Unknown custom completion key."); |
| 95 | abort(); |
| 96 | } |
| 97 | |
| 98 | socket = (grpc_winsocket*) completion_key; |
| 99 | if (overlapped == &socket->write_info.overlapped) { |
| 100 | gpr_log(GPR_DEBUG, "pollset_poll - got write packet"); |
| 101 | info = &socket->write_info; |
| 102 | } else if (overlapped == &socket->read_info.overlapped) { |
| 103 | gpr_log(GPR_DEBUG, "pollset_poll - got read packet"); |
| 104 | info = &socket->read_info; |
| 105 | } else { |
| 106 | gpr_log(GPR_ERROR, "Unknown IOCP operation"); |
| 107 | abort(); |
| 108 | } |
| 109 | success = WSAGetOverlappedResult(socket->socket, &info->overlapped, &bytes, |
| 110 | FALSE, &flags); |
| 111 | gpr_log(GPR_DEBUG, "bytes: %u, flags: %u - op %s", bytes, flags, |
| 112 | success ? "succeeded" : "failed"); |
| 113 | info->bytes_transfered = bytes; |
| 114 | info->wsa_error = success ? 0 : WSAGetLastError(); |
| 115 | GPR_ASSERT(overlapped == &info->overlapped); |
| 116 | gpr_mu_lock(&socket->state_mu); |
| 117 | GPR_ASSERT(!info->has_pending_iocp); |
| 118 | if (info->cb) { |
| 119 | f = info->cb; |
| 120 | opaque = info->opaque; |
| 121 | info->cb = NULL; |
| 122 | } else { |
| 123 | info->has_pending_iocp = 1; |
| 124 | } |
| 125 | gpr_mu_unlock(&socket->state_mu); |
| 126 | if (f) f(opaque, 1); |
| 127 | |
| 128 | return 1; |
| 129 | } |
| 130 | |
| 131 | int grpc_pollset_work(grpc_pollset *pollset, gpr_timespec deadline) { |
| 132 | gpr_timespec now; |
| 133 | now = gpr_now(); |
| 134 | if (gpr_time_cmp(now, deadline) > 0) { |
| 135 | return 0; |
| 136 | } |
| 137 | if (grpc_maybe_call_delayed_callbacks(NULL, 1)) { |
| 138 | return 1; |
| 139 | } |
| 140 | if (grpc_alarm_check(NULL, now, &deadline)) { |
| 141 | return 1; |
| 142 | } |
| 143 | return pollset_poll(pollset, deadline, now); |
| 144 | } |
| 145 | |
| 146 | void grpc_pollset_kick(grpc_pollset *pollset) { |
| 147 | BOOL status; |
| 148 | status = PostQueuedCompletionStatus(pollset->iocp, 0, |
| 149 | (ULONG_PTR) &g_pollset_kick_token, |
| 150 | &g_pollset_custom_overlap); |
| 151 | GPR_ASSERT(status); |
| 152 | } |
| 153 | |
| 154 | static void global_poller(void *p) { |
| 155 | while (!gpr_event_get(&g_shutdown_global_poller)) { |
| 156 | grpc_pollset_work(&g_global_pollset, gpr_inf_future); |
| 157 | } |
| 158 | |
| 159 | gpr_event_set(&g_global_poller_done, (void *) 1); |
| 160 | } |
| 161 | |
| 162 | void grpc_pollset_global_init(void) { |
| 163 | gpr_thd_id id; |
| 164 | |
| 165 | grpc_pollset_init(&g_global_pollset); |
| 166 | gpr_event_init(&g_global_poller_done); |
| 167 | gpr_event_init(&g_shutdown_global_poller); |
| 168 | gpr_thd_new(&id, global_poller, NULL, NULL); |
| 169 | } |
| 170 | |
| 171 | void grpc_pollset_global_shutdown(void) { |
| 172 | gpr_event_set(&g_shutdown_global_poller, (void *) 1); |
| 173 | grpc_pollset_kick(&g_global_pollset); |
| 174 | gpr_event_wait(&g_global_poller_done, gpr_inf_future); |
| 175 | grpc_pollset_destroy(&g_global_pollset); |
| 176 | } |
| 177 | |
| 178 | void grpc_pollset_add_handle(grpc_pollset *pollset, grpc_winsocket *socket) { |
| 179 | HANDLE ret = CreateIoCompletionPort((HANDLE) socket->socket, pollset->iocp, |
| 180 | (gpr_uintptr) socket, 0); |
| 181 | GPR_ASSERT(ret == pollset->iocp); |
| 182 | } |
| 183 | |
| 184 | static void handle_notify_on_iocp(grpc_winsocket *socket, |
| 185 | void(*cb)(void *, int), void *opaque, |
| 186 | grpc_winsocket_callback_info *info) { |
| 187 | int run_now = 0; |
| 188 | GPR_ASSERT(!info->cb); |
| 189 | gpr_mu_lock(&socket->state_mu); |
| 190 | if (info->has_pending_iocp) { |
| 191 | run_now = 1; |
| 192 | info->has_pending_iocp = 0; |
| 193 | gpr_log(GPR_DEBUG, "handle_notify_on_iocp - runs now"); |
| 194 | } else { |
| 195 | info->cb = cb; |
| 196 | info->opaque = opaque; |
| 197 | gpr_log(GPR_DEBUG, "handle_notify_on_iocp - queued"); |
| 198 | } |
| 199 | gpr_mu_unlock(&socket->state_mu); |
| 200 | if (run_now) cb(opaque, 1); |
| 201 | } |
| 202 | |
| 203 | void grpc_handle_notify_on_write(grpc_winsocket *socket, |
| 204 | void(*cb)(void *, int), void *opaque) { |
| 205 | gpr_log(GPR_DEBUG, "grpc_handle_notify_on_write"); |
| 206 | handle_notify_on_iocp(socket, cb, opaque, &socket->write_info); |
| 207 | } |
| 208 | |
| 209 | void grpc_handle_notify_on_read(grpc_winsocket *socket, |
| 210 | void(*cb)(void *, int), void *opaque) { |
| 211 | gpr_log(GPR_DEBUG, "grpc_handle_notify_on_read"); |
| 212 | handle_notify_on_iocp(socket, cb, opaque, &socket->read_info); |
| 213 | } |
| 214 | |
| 215 | grpc_pollset *grpc_global_pollset(void) { |
| 216 | return &g_global_pollset; |
| 217 | } |
| 218 | |
| 219 | #endif /* GPR_WINSOCK_SOCKET */ |