Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Nicolas "Pixel" Noble | 929523a | 2016-01-28 09:55:14 +0100 | [diff] [blame] | 3 | * Copyright 2015-2016, 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 | |
David Garcia Quintas | f747bbc | 2015-10-04 23:09:47 -0700 | [diff] [blame] | 45 | #include "src/core/iomgr/timer_internal.h" |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 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 | |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 53 | static gpr_atm g_custom_events = 0; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 54 | |
| 55 | static HANDLE g_iocp; |
| 56 | |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 57 | static DWORD deadline_to_millis_timeout(gpr_timespec deadline, |
| 58 | gpr_timespec now) { |
| 59 | gpr_timespec timeout; |
murgatroid99 | 309830f | 2016-02-05 11:30:00 -0800 | [diff] [blame^] | 60 | static const int64_t max_spin_polling_us = 10; |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 61 | if (gpr_time_cmp(deadline, gpr_inf_future(deadline.clock_type)) == 0) { |
| 62 | return INFINITE; |
| 63 | } |
| 64 | if (gpr_time_cmp(deadline, gpr_time_add(now, gpr_time_from_micros( |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 65 | max_spin_polling_us, |
| 66 | GPR_TIMESPAN))) <= 0) { |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | timeout = gpr_time_sub(deadline, now); |
Nicolas "Pixel" Noble | bd76a63 | 2016-01-28 09:33:08 +0100 | [diff] [blame] | 70 | return (DWORD)gpr_time_to_millis(gpr_time_add( |
Craig Tiller | b7a5977 | 2015-10-01 08:01:58 -0700 | [diff] [blame] | 71 | timeout, gpr_time_from_nanos(GPR_NS_PER_MS - 1, GPR_TIMESPAN))); |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void grpc_iocp_work(grpc_exec_ctx *exec_ctx, gpr_timespec deadline) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 75 | BOOL success; |
| 76 | DWORD bytes = 0; |
| 77 | DWORD flags = 0; |
| 78 | ULONG_PTR completion_key; |
| 79 | LPOVERLAPPED overlapped; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 80 | grpc_winsocket *socket; |
| 81 | grpc_winsocket_callback_info *info; |
Craig Tiller | 258f8de | 2015-09-23 12:32:40 -0700 | [diff] [blame] | 82 | grpc_closure *closure = NULL; |
Craig Tiller | 71a0f9d | 2015-09-28 17:22:01 -0700 | [diff] [blame] | 83 | success = GetQueuedCompletionStatus( |
| 84 | g_iocp, &bytes, &completion_key, &overlapped, |
| 85 | deadline_to_millis_timeout(deadline, gpr_now(deadline.clock_type))); |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 86 | if (success == 0 && overlapped == NULL) { |
| 87 | return; |
| 88 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 89 | GPR_ASSERT(completion_key && overlapped); |
| 90 | if (overlapped == &g_iocp_custom_overlap) { |
| 91 | gpr_atm_full_fetch_add(&g_custom_events, -1); |
| 92 | if (completion_key == (ULONG_PTR)&g_iocp_kick_token) { |
| 93 | /* We were awoken from a kick. */ |
| 94 | return; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 95 | } |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 96 | gpr_log(GPR_ERROR, "Unknown custom completion key."); |
| 97 | abort(); |
| 98 | } |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 99 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 100 | socket = (grpc_winsocket *)completion_key; |
| 101 | if (overlapped == &socket->write_info.overlapped) { |
| 102 | info = &socket->write_info; |
| 103 | } else if (overlapped == &socket->read_info.overlapped) { |
| 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); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 111 | info->bytes_transfered = bytes; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 112 | info->wsa_error = success ? 0 : WSAGetLastError(); |
| 113 | GPR_ASSERT(overlapped == &info->overlapped); |
| 114 | GPR_ASSERT(!info->has_pending_iocp); |
| 115 | gpr_mu_lock(&socket->state_mu); |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 116 | if (info->closure) { |
Craig Tiller | 258f8de | 2015-09-23 12:32:40 -0700 | [diff] [blame] | 117 | closure = info->closure; |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 118 | info->closure = NULL; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 119 | } else { |
| 120 | info->has_pending_iocp = 1; |
| 121 | } |
| 122 | gpr_mu_unlock(&socket->state_mu); |
Craig Tiller | eced8ae | 2016-01-28 14:13:20 -0800 | [diff] [blame] | 123 | grpc_exec_ctx_enqueue(exec_ctx, closure, true, NULL); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 126 | void grpc_iocp_init(void) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 127 | g_iocp = |
| 128 | CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)NULL, 0); |
| 129 | GPR_ASSERT(g_iocp); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 132 | void grpc_iocp_kick(void) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 133 | BOOL success; |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 134 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 135 | gpr_atm_full_fetch_add(&g_custom_events, 1); |
| 136 | success = PostQueuedCompletionStatus(g_iocp, 0, (ULONG_PTR)&g_iocp_kick_token, |
| 137 | &g_iocp_custom_overlap); |
| 138 | GPR_ASSERT(success); |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Craig Tiller | 01be53d | 2015-09-30 08:36:03 -0700 | [diff] [blame] | 141 | void grpc_iocp_flush(void) { |
| 142 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Craig Tiller | b7a5977 | 2015-10-01 08:01:58 -0700 | [diff] [blame] | 143 | |
Craig Tiller | 01be53d | 2015-09-30 08:36:03 -0700 | [diff] [blame] | 144 | do { |
Craig Tiller | 1433791 | 2015-09-30 08:41:51 -0700 | [diff] [blame] | 145 | grpc_iocp_work(&exec_ctx, gpr_inf_past(GPR_CLOCK_MONOTONIC)); |
Craig Tiller | 01be53d | 2015-09-30 08:36:03 -0700 | [diff] [blame] | 146 | } while (grpc_exec_ctx_flush(&exec_ctx)); |
| 147 | } |
| 148 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 149 | void grpc_iocp_shutdown(void) { |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 150 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 151 | while (gpr_atm_acq_load(&g_custom_events)) { |
| 152 | grpc_iocp_work(&exec_ctx, gpr_inf_future(GPR_CLOCK_MONOTONIC)); |
| 153 | grpc_exec_ctx_flush(&exec_ctx); |
| 154 | } |
| 155 | grpc_exec_ctx_finish(&exec_ctx); |
| 156 | GPR_ASSERT(CloseHandle(g_iocp)); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 159 | void grpc_iocp_add_socket(grpc_winsocket *socket) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 160 | HANDLE ret; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 161 | if (socket->added_to_iocp) return; |
| 162 | ret = CreateIoCompletionPort((HANDLE)socket->socket, g_iocp, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 163 | (uintptr_t)socket, 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 164 | if (!ret) { |
| 165 | char *utf8_message = gpr_format_message(WSAGetLastError()); |
| 166 | gpr_log(GPR_ERROR, "Unable to add socket to iocp: %s", utf8_message); |
| 167 | gpr_free(utf8_message); |
| 168 | __debugbreak(); |
| 169 | abort(); |
| 170 | } |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 171 | socket->added_to_iocp = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 172 | GPR_ASSERT(ret == g_iocp); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 175 | /* Calling notify_on_read or write means either of two things: |
| 176 | -) The IOCP already completed in the background, and we need to call |
| 177 | the callback now. |
| 178 | -) The IOCP hasn't completed yet, and we're queuing it for later. */ |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 179 | static void socket_notify_on_iocp(grpc_exec_ctx *exec_ctx, |
Craig Tiller | 565b18b | 2015-09-23 10:09:42 -0700 | [diff] [blame] | 180 | grpc_winsocket *socket, grpc_closure *closure, |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 181 | grpc_winsocket_callback_info *info) { |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 182 | GPR_ASSERT(info->closure == NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 183 | gpr_mu_lock(&socket->state_mu); |
| 184 | if (info->has_pending_iocp) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 185 | info->has_pending_iocp = 0; |
Craig Tiller | eced8ae | 2016-01-28 14:13:20 -0800 | [diff] [blame] | 186 | grpc_exec_ctx_enqueue(exec_ctx, closure, true, NULL); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 187 | } else { |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 188 | info->closure = closure; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 189 | } |
| 190 | gpr_mu_unlock(&socket->state_mu); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 193 | void grpc_socket_notify_on_write(grpc_exec_ctx *exec_ctx, |
| 194 | grpc_winsocket *socket, |
| 195 | grpc_closure *closure) { |
| 196 | socket_notify_on_iocp(exec_ctx, socket, closure, &socket->write_info); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Craig Tiller | 565b18b | 2015-09-23 10:09:42 -0700 | [diff] [blame] | 199 | void grpc_socket_notify_on_read(grpc_exec_ctx *exec_ctx, grpc_winsocket *socket, |
Craig Tiller | 82f9bd8 | 2015-09-23 09:31:51 -0700 | [diff] [blame] | 200 | grpc_closure *closure) { |
| 201 | socket_notify_on_iocp(exec_ctx, socket, closure, &socket->read_info); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 204 | #endif /* GPR_WINSOCK_SOCKET */ |