Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -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 | |
| 34 | #include <grpc/support/port_platform.h> |
| 35 | |
| 36 | #ifdef GPR_WINSOCK_SOCKET |
| 37 | |
| 38 | #include <winsock2.h> |
| 39 | |
| 40 | #include <grpc/support/log.h> |
| 41 | #include <grpc/support/log_win32.h> |
| 42 | #include <grpc/support/alloc.h> |
| 43 | #include <grpc/support/thd.h> |
| 44 | |
| 45 | #include "src/core/iomgr/alarm_internal.h" |
| 46 | #include "src/core/iomgr/iocp_windows.h" |
| 47 | #include "src/core/iomgr/iomgr_internal.h" |
| 48 | #include "src/core/iomgr/socket_windows.h" |
| 49 | |
| 50 | static ULONG g_iocp_kick_token; |
| 51 | static OVERLAPPED g_iocp_custom_overlap; |
| 52 | |
| 53 | static gpr_event g_shutdown_iocp; |
| 54 | static gpr_event g_iocp_done; |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 55 | static gpr_atm g_orphans = 0; |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 56 | static gpr_atm g_custom_events = 0; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 57 | |
| 58 | static HANDLE g_iocp; |
| 59 | |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 60 | static void do_iocp_work() { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 61 | BOOL success; |
| 62 | DWORD bytes = 0; |
| 63 | DWORD flags = 0; |
| 64 | ULONG_PTR completion_key; |
| 65 | LPOVERLAPPED overlapped; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 66 | grpc_winsocket *socket; |
| 67 | grpc_winsocket_callback_info *info; |
| 68 | void(*f)(void *, int) = NULL; |
| 69 | void *opaque = NULL; |
| 70 | success = GetQueuedCompletionStatus(g_iocp, &bytes, |
| 71 | &completion_key, &overlapped, |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 72 | INFINITE); |
| 73 | /* success = 0 and overlapped = NULL means the deadline got attained. |
| 74 | Which is impossible. since our wait time is +inf */ |
| 75 | GPR_ASSERT(success || overlapped); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 76 | GPR_ASSERT(completion_key && overlapped); |
| 77 | if (overlapped == &g_iocp_custom_overlap) { |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 78 | gpr_atm_full_fetch_add(&g_custom_events, -1); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 79 | if (completion_key == (ULONG_PTR) &g_iocp_kick_token) { |
| 80 | /* We were awoken from a kick. */ |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 81 | return; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 82 | } |
| 83 | gpr_log(GPR_ERROR, "Unknown custom completion key."); |
| 84 | abort(); |
| 85 | } |
| 86 | |
| 87 | socket = (grpc_winsocket*) completion_key; |
| 88 | if (overlapped == &socket->write_info.overlapped) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 89 | info = &socket->write_info; |
| 90 | } else if (overlapped == &socket->read_info.overlapped) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 91 | info = &socket->read_info; |
| 92 | } else { |
| 93 | gpr_log(GPR_ERROR, "Unknown IOCP operation"); |
| 94 | abort(); |
| 95 | } |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 96 | GPR_ASSERT(info->outstanding); |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 97 | if (socket->orphan) { |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 98 | info->outstanding = 0; |
| 99 | if (!socket->read_info.outstanding && !socket->write_info.outstanding) { |
| 100 | grpc_winsocket_destroy(socket); |
| 101 | gpr_atm_full_fetch_add(&g_orphans, -1); |
| 102 | } |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 103 | return; |
| 104 | } |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 105 | success = WSAGetOverlappedResult(socket->socket, &info->overlapped, &bytes, |
| 106 | FALSE, &flags); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 107 | info->bytes_transfered = bytes; |
| 108 | info->wsa_error = success ? 0 : WSAGetLastError(); |
| 109 | GPR_ASSERT(overlapped == &info->overlapped); |
| 110 | gpr_mu_lock(&socket->state_mu); |
| 111 | GPR_ASSERT(!info->has_pending_iocp); |
| 112 | if (info->cb) { |
| 113 | f = info->cb; |
| 114 | opaque = info->opaque; |
| 115 | info->cb = NULL; |
| 116 | } else { |
| 117 | info->has_pending_iocp = 1; |
| 118 | } |
| 119 | gpr_mu_unlock(&socket->state_mu); |
| 120 | if (f) f(opaque, 1); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static void iocp_loop(void *p) { |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 124 | while (gpr_atm_acq_load(&g_orphans) || |
| 125 | gpr_atm_acq_load(&g_custom_events) || |
| 126 | !gpr_event_get(&g_shutdown_iocp)) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 127 | grpc_maybe_call_delayed_callbacks(NULL, 1); |
| 128 | do_iocp_work(); |
| 129 | } |
| 130 | |
| 131 | gpr_event_set(&g_iocp_done, (void *)1); |
| 132 | } |
| 133 | |
| 134 | void grpc_iocp_init(void) { |
| 135 | gpr_thd_id id; |
| 136 | |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 137 | g_iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, |
| 138 | NULL, (ULONG_PTR)NULL, 0); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 139 | GPR_ASSERT(g_iocp); |
| 140 | |
| 141 | gpr_event_init(&g_iocp_done); |
| 142 | gpr_event_init(&g_shutdown_iocp); |
| 143 | gpr_thd_new(&id, iocp_loop, NULL, NULL); |
| 144 | } |
| 145 | |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 146 | void grpc_iocp_kick(void) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 147 | BOOL success; |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 148 | |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 149 | gpr_atm_full_fetch_add(&g_custom_events, 1); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 150 | success = PostQueuedCompletionStatus(g_iocp, 0, |
| 151 | (ULONG_PTR) &g_iocp_kick_token, |
| 152 | &g_iocp_custom_overlap); |
| 153 | GPR_ASSERT(success); |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | void grpc_iocp_shutdown(void) { |
| 157 | BOOL success; |
| 158 | gpr_event_set(&g_shutdown_iocp, (void *)1); |
| 159 | grpc_iocp_kick(); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 160 | gpr_event_wait(&g_iocp_done, gpr_inf_future); |
| 161 | success = CloseHandle(g_iocp); |
| 162 | GPR_ASSERT(success); |
| 163 | } |
| 164 | |
| 165 | void grpc_iocp_add_socket(grpc_winsocket *socket) { |
| 166 | HANDLE ret; |
| 167 | if (socket->added_to_iocp) return; |
| 168 | ret = CreateIoCompletionPort((HANDLE)socket->socket, |
| 169 | g_iocp, (gpr_uintptr) socket, 0); |
| 170 | if (!ret) { |
| 171 | char *utf8_message = gpr_format_message(WSAGetLastError()); |
| 172 | gpr_log(GPR_ERROR, "Unable to add socket to iocp: %s", utf8_message); |
| 173 | gpr_free(utf8_message); |
| 174 | __debugbreak(); |
| 175 | abort(); |
| 176 | } |
| 177 | socket->added_to_iocp = 1; |
| 178 | GPR_ASSERT(ret == g_iocp); |
| 179 | } |
| 180 | |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 181 | void grpc_iocp_socket_orphan(grpc_winsocket *socket) { |
Nicolas "Pixel" Noble | 8e1a55d | 2015-05-02 15:21:25 -0700 | [diff] [blame] | 182 | GPR_ASSERT(!socket->orphan); |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 183 | gpr_atm_full_fetch_add(&g_orphans, 1); |
Nicolas "Pixel" Noble | 8e1a55d | 2015-05-02 15:21:25 -0700 | [diff] [blame] | 184 | socket->orphan = 1; |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 187 | /* Calling notify_on_read or write means either of two things: |
| 188 | -) The IOCP already completed in the background, and we need to call |
| 189 | the callback now. |
| 190 | -) The IOCP hasn't completed yet, and we're queuing it for later. */ |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 191 | static void socket_notify_on_iocp(grpc_winsocket *socket, |
| 192 | void(*cb)(void *, int), void *opaque, |
| 193 | grpc_winsocket_callback_info *info) { |
| 194 | int run_now = 0; |
| 195 | GPR_ASSERT(!info->cb); |
| 196 | gpr_mu_lock(&socket->state_mu); |
| 197 | if (info->has_pending_iocp) { |
| 198 | run_now = 1; |
| 199 | info->has_pending_iocp = 0; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 200 | } else { |
| 201 | info->cb = cb; |
| 202 | info->opaque = opaque; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 203 | } |
| 204 | gpr_mu_unlock(&socket->state_mu); |
| 205 | if (run_now) cb(opaque, 1); |
| 206 | } |
| 207 | |
| 208 | void grpc_socket_notify_on_write(grpc_winsocket *socket, |
| 209 | void(*cb)(void *, int), void *opaque) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 210 | socket_notify_on_iocp(socket, cb, opaque, &socket->write_info); |
| 211 | } |
| 212 | |
| 213 | void grpc_socket_notify_on_read(grpc_winsocket *socket, |
| 214 | void(*cb)(void *, int), void *opaque) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 215 | socket_notify_on_iocp(socket, cb, opaque, &socket->read_info); |
| 216 | } |
| 217 | |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 218 | #endif /* GPR_WINSOCK_SOCKET */ |