Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [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 | |
murgatroid99 | 5407089 | 2016-08-08 17:01:18 -0700 | [diff] [blame] | 34 | #include "src/core/lib/iomgr/port.h" |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 35 | |
murgatroid99 | 623dd4f | 2016-08-08 17:31:27 -0700 | [diff] [blame] | 36 | #ifdef GRPC_WINSOCK_SOCKET |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 37 | |
| 38 | #include <winsock2.h> |
| 39 | |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 40 | #include <grpc/support/alloc.h> |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 41 | #include <grpc/support/log.h> |
Yuchen Zeng | 12dfdc3 | 2016-04-26 22:05:41 -0700 | [diff] [blame] | 42 | #include <grpc/support/log_windows.h> |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 43 | #include <grpc/support/thd.h> |
| 44 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 45 | #include "src/core/lib/iomgr/iocp_windows.h" |
| 46 | #include "src/core/lib/iomgr/iomgr_internal.h" |
| 47 | #include "src/core/lib/iomgr/socket_windows.h" |
| 48 | #include "src/core/lib/iomgr/timer.h" |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 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 | |
yang-g | 9aa4f1b | 2016-03-02 16:39:13 -0800 | [diff] [blame] | 74 | grpc_iocp_work_status grpc_iocp_work(grpc_exec_ctx *exec_ctx, |
| 75 | gpr_timespec deadline) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 76 | BOOL success; |
| 77 | DWORD bytes = 0; |
| 78 | DWORD flags = 0; |
| 79 | ULONG_PTR completion_key; |
| 80 | LPOVERLAPPED overlapped; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 81 | grpc_winsocket *socket; |
| 82 | grpc_winsocket_callback_info *info; |
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) { |
yang-g | 9aa4f1b | 2016-03-02 16:39:13 -0800 | [diff] [blame] | 87 | return GRPC_IOCP_WORK_TIMEOUT; |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 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. */ |
yang-g | 9aa4f1b | 2016-03-02 16:39:13 -0800 | [diff] [blame] | 94 | return GRPC_IOCP_WORK_KICK; |
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 { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 106 | abort(); |
| 107 | } |
| 108 | success = WSAGetOverlappedResult(socket->socket, &info->overlapped, &bytes, |
| 109 | FALSE, &flags); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 110 | info->bytes_transfered = bytes; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 111 | info->wsa_error = success ? 0 : WSAGetLastError(); |
| 112 | GPR_ASSERT(overlapped == &info->overlapped); |
Craig Tiller | 5352ff1 | 2016-05-18 10:44:38 -0700 | [diff] [blame] | 113 | grpc_socket_become_ready(exec_ctx, socket, info); |
yang-g | 9aa4f1b | 2016-03-02 16:39:13 -0800 | [diff] [blame] | 114 | return GRPC_IOCP_WORK_WORK; |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 117 | void grpc_iocp_init(void) { |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 118 | g_iocp = |
| 119 | CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)NULL, 0); |
| 120 | GPR_ASSERT(g_iocp); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 123 | void grpc_iocp_kick(void) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 124 | BOOL success; |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 125 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 126 | gpr_atm_full_fetch_add(&g_custom_events, 1); |
| 127 | success = PostQueuedCompletionStatus(g_iocp, 0, (ULONG_PTR)&g_iocp_kick_token, |
| 128 | &g_iocp_custom_overlap); |
| 129 | GPR_ASSERT(success); |
Nicolas Noble | 8703f4d | 2015-03-23 13:52:18 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Craig Tiller | 01be53d | 2015-09-30 08:36:03 -0700 | [diff] [blame] | 132 | void grpc_iocp_flush(void) { |
| 133 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
yang-g | 9aa4f1b | 2016-03-02 16:39:13 -0800 | [diff] [blame] | 134 | grpc_iocp_work_status work_status; |
Craig Tiller | b7a5977 | 2015-10-01 08:01:58 -0700 | [diff] [blame] | 135 | |
Craig Tiller | 01be53d | 2015-09-30 08:36:03 -0700 | [diff] [blame] | 136 | do { |
yang-g | 9aa4f1b | 2016-03-02 16:39:13 -0800 | [diff] [blame] | 137 | work_status = grpc_iocp_work(&exec_ctx, gpr_inf_past(GPR_CLOCK_MONOTONIC)); |
| 138 | } while (work_status == GRPC_IOCP_WORK_KICK || |
| 139 | grpc_exec_ctx_flush(&exec_ctx)); |
Craig Tiller | 01be53d | 2015-09-30 08:36:03 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 142 | void grpc_iocp_shutdown(void) { |
Craig Tiller | 3dedb70 | 2015-09-24 10:31:26 -0700 | [diff] [blame] | 143 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
| 144 | while (gpr_atm_acq_load(&g_custom_events)) { |
| 145 | grpc_iocp_work(&exec_ctx, gpr_inf_future(GPR_CLOCK_MONOTONIC)); |
| 146 | grpc_exec_ctx_flush(&exec_ctx); |
| 147 | } |
| 148 | grpc_exec_ctx_finish(&exec_ctx); |
| 149 | GPR_ASSERT(CloseHandle(g_iocp)); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 152 | void grpc_iocp_add_socket(grpc_winsocket *socket) { |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 153 | HANDLE ret; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 154 | if (socket->added_to_iocp) return; |
| 155 | ret = CreateIoCompletionPort((HANDLE)socket->socket, g_iocp, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 156 | (uintptr_t)socket, 0); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 157 | if (!ret) { |
| 158 | char *utf8_message = gpr_format_message(WSAGetLastError()); |
| 159 | gpr_log(GPR_ERROR, "Unable to add socket to iocp: %s", utf8_message); |
| 160 | gpr_free(utf8_message); |
| 161 | __debugbreak(); |
| 162 | abort(); |
| 163 | } |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 164 | socket->added_to_iocp = 1; |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 165 | GPR_ASSERT(ret == g_iocp); |
Nicolas Noble | 45e67a3 | 2015-02-09 16:20:49 -0800 | [diff] [blame] | 166 | } |
| 167 | |
murgatroid99 | 623dd4f | 2016-08-08 17:31:27 -0700 | [diff] [blame] | 168 | #endif /* GRPC_WINSOCK_SOCKET */ |