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 "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 55 | static gpr_atm g_custom_events = 0; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 56 | |
| 57 | static HANDLE g_iocp; |
| 58 | |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 59 | static void do_iocp_work() { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 60 | BOOL success; |
| 61 | DWORD bytes = 0; |
| 62 | DWORD flags = 0; |
| 63 | ULONG_PTR completion_key; |
| 64 | LPOVERLAPPED overlapped; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 65 | grpc_winsocket *socket; |
| 66 | grpc_winsocket_callback_info *info; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 67 | void (*f)(void *, int) = NULL; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 68 | void *opaque = NULL; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 69 | success = GetQueuedCompletionStatus(g_iocp, &bytes, &completion_key, |
| 70 | &overlapped, INFINITE); |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 71 | /* success = 0 and overlapped = NULL means the deadline got attained. |
| 72 | Which is impossible. since our wait time is +inf */ |
| 73 | GPR_ASSERT(success || overlapped); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 74 | GPR_ASSERT(completion_key && overlapped); |
| 75 | if (overlapped == &g_iocp_custom_overlap) { |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 76 | gpr_atm_full_fetch_add(&g_custom_events, -1); |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 77 | if (completion_key == (ULONG_PTR)&g_iocp_kick_token) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 78 | /* We were awoken from a kick. */ |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 79 | return; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 80 | } |
| 81 | gpr_log(GPR_ERROR, "Unknown custom completion key."); |
| 82 | abort(); |
| 83 | } |
| 84 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 85 | socket = (grpc_winsocket *)completion_key; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 86 | if (overlapped == &socket->write_info.overlapped) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 87 | info = &socket->write_info; |
| 88 | } else if (overlapped == &socket->read_info.overlapped) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 89 | info = &socket->read_info; |
| 90 | } else { |
| 91 | gpr_log(GPR_ERROR, "Unknown IOCP operation"); |
| 92 | abort(); |
| 93 | } |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 94 | success = WSAGetOverlappedResult(socket->socket, &info->overlapped, &bytes, |
| 95 | FALSE, &flags); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 96 | info->bytes_transfered = bytes; |
| 97 | info->wsa_error = success ? 0 : WSAGetLastError(); |
| 98 | GPR_ASSERT(overlapped == &info->overlapped); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 99 | GPR_ASSERT(!info->has_pending_iocp); |
Craig Tiller | 9f80fcf | 2015-08-28 08:22:48 -0700 | [diff] [blame] | 100 | gpr_mu_lock(&socket->state_mu); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 101 | if (info->cb) { |
| 102 | f = info->cb; |
| 103 | opaque = info->opaque; |
| 104 | info->cb = NULL; |
| 105 | } else { |
| 106 | info->has_pending_iocp = 1; |
| 107 | } |
| 108 | gpr_mu_unlock(&socket->state_mu); |
| 109 | if (f) f(opaque, 1); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static void iocp_loop(void *p) { |
Craig Tiller | 9f80fcf | 2015-08-28 08:22:48 -0700 | [diff] [blame] | 113 | while (gpr_atm_acq_load(&g_custom_events) || |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 114 | !gpr_event_get(&g_shutdown_iocp)) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 115 | do_iocp_work(); |
| 116 | } |
| 117 | |
| 118 | gpr_event_set(&g_iocp_done, (void *)1); |
| 119 | } |
| 120 | |
| 121 | void grpc_iocp_init(void) { |
| 122 | gpr_thd_id id; |
| 123 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 124 | g_iocp = |
| 125 | CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)NULL, 0); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 126 | GPR_ASSERT(g_iocp); |
| 127 | |
| 128 | gpr_event_init(&g_iocp_done); |
| 129 | gpr_event_init(&g_shutdown_iocp); |
| 130 | gpr_thd_new(&id, iocp_loop, NULL, NULL); |
| 131 | } |
| 132 | |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 133 | void grpc_iocp_kick(void) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 134 | BOOL success; |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 135 | |
Nicolas "Pixel" Noble | 7f2e98c | 2015-05-08 01:41:21 +0200 | [diff] [blame] | 136 | gpr_atm_full_fetch_add(&g_custom_events, 1); |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 137 | success = PostQueuedCompletionStatus(g_iocp, 0, (ULONG_PTR)&g_iocp_kick_token, |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 138 | &g_iocp_custom_overlap); |
| 139 | GPR_ASSERT(success); |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void grpc_iocp_shutdown(void) { |
| 143 | BOOL success; |
| 144 | gpr_event_set(&g_shutdown_iocp, (void *)1); |
| 145 | grpc_iocp_kick(); |
Craig Tiller | 143e7bf | 2015-07-13 08:41:49 -0700 | [diff] [blame] | 146 | gpr_event_wait(&g_iocp_done, gpr_inf_future(GPR_CLOCK_REALTIME)); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 147 | success = CloseHandle(g_iocp); |
| 148 | GPR_ASSERT(success); |
| 149 | } |
| 150 | |
| 151 | void grpc_iocp_add_socket(grpc_winsocket *socket) { |
| 152 | HANDLE ret; |
| 153 | if (socket->added_to_iocp) return; |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 154 | ret = CreateIoCompletionPort((HANDLE)socket->socket, g_iocp, |
| 155 | (gpr_uintptr)socket, 0); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 156 | if (!ret) { |
| 157 | char *utf8_message = gpr_format_message(WSAGetLastError()); |
| 158 | gpr_log(GPR_ERROR, "Unable to add socket to iocp: %s", utf8_message); |
| 159 | gpr_free(utf8_message); |
| 160 | __debugbreak(); |
| 161 | abort(); |
| 162 | } |
| 163 | socket->added_to_iocp = 1; |
| 164 | GPR_ASSERT(ret == g_iocp); |
| 165 | } |
| 166 | |
Nicolas "Pixel" Noble | 0f981e9 | 2015-05-03 10:40:56 +0200 | [diff] [blame] | 167 | /* Calling notify_on_read or write means either of two things: |
| 168 | -) The IOCP already completed in the background, and we need to call |
| 169 | the callback now. |
| 170 | -) 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] | 171 | static void socket_notify_on_iocp(grpc_winsocket *socket, |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 172 | void (*cb)(void *, int), void *opaque, |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 173 | grpc_winsocket_callback_info *info) { |
| 174 | int run_now = 0; |
| 175 | GPR_ASSERT(!info->cb); |
| 176 | gpr_mu_lock(&socket->state_mu); |
| 177 | if (info->has_pending_iocp) { |
| 178 | run_now = 1; |
| 179 | info->has_pending_iocp = 0; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 180 | } else { |
| 181 | info->cb = cb; |
| 182 | info->opaque = opaque; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 183 | } |
| 184 | gpr_mu_unlock(&socket->state_mu); |
| 185 | if (run_now) cb(opaque, 1); |
| 186 | } |
| 187 | |
| 188 | void grpc_socket_notify_on_write(grpc_winsocket *socket, |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 189 | void (*cb)(void *, int), void *opaque) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 190 | socket_notify_on_iocp(socket, cb, opaque, &socket->write_info); |
| 191 | } |
| 192 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 193 | void grpc_socket_notify_on_read(grpc_winsocket *socket, void (*cb)(void *, int), |
| 194 | void *opaque) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 195 | socket_notify_on_iocp(socket, cb, opaque, &socket->read_info); |
| 196 | } |
| 197 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 198 | #endif /* GPR_WINSOCK_SOCKET */ |