blob: 759340e00ef9365f4a3893a50ad7a0bb7e8b25c3 [file] [log] [blame]
Nicolas Noble45e67a32015-02-09 16:20:49 -08001/*
2 *
Nicolas "Pixel" Noble929523a2016-01-28 09:55:14 +01003 * Copyright 2015-2016, Google Inc.
Nicolas Noble45e67a32015-02-09 16:20:49 -08004 * 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 Quintasf747bbc2015-10-04 23:09:47 -070045#include "src/core/iomgr/timer_internal.h"
Nicolas Noble45e67a32015-02-09 16:20:49 -080046#include "src/core/iomgr/iocp_windows.h"
47#include "src/core/iomgr/iomgr_internal.h"
48#include "src/core/iomgr/socket_windows.h"
49
50static ULONG g_iocp_kick_token;
51static OVERLAPPED g_iocp_custom_overlap;
52
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +020053static gpr_atm g_custom_events = 0;
Nicolas Noble45e67a32015-02-09 16:20:49 -080054
55static HANDLE g_iocp;
56
Craig Tiller3dedb702015-09-24 10:31:26 -070057static DWORD deadline_to_millis_timeout(gpr_timespec deadline,
58 gpr_timespec now) {
59 gpr_timespec timeout;
murgatroid99309830f2016-02-05 11:30:00 -080060 static const int64_t max_spin_polling_us = 10;
Craig Tiller3dedb702015-09-24 10:31:26 -070061 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 Tiller71a0f9d2015-09-28 17:22:01 -070065 max_spin_polling_us,
66 GPR_TIMESPAN))) <= 0) {
Craig Tiller3dedb702015-09-24 10:31:26 -070067 return 0;
68 }
69 timeout = gpr_time_sub(deadline, now);
Nicolas "Pixel" Noblebd76a632016-01-28 09:33:08 +010070 return (DWORD)gpr_time_to_millis(gpr_time_add(
Craig Tillerb7a59772015-10-01 08:01:58 -070071 timeout, gpr_time_from_nanos(GPR_NS_PER_MS - 1, GPR_TIMESPAN)));
Craig Tiller3dedb702015-09-24 10:31:26 -070072}
73
74void grpc_iocp_work(grpc_exec_ctx *exec_ctx, gpr_timespec deadline) {
Nicolas Noble45e67a32015-02-09 16:20:49 -080075 BOOL success;
76 DWORD bytes = 0;
77 DWORD flags = 0;
78 ULONG_PTR completion_key;
79 LPOVERLAPPED overlapped;
Nicolas Noble45e67a32015-02-09 16:20:49 -080080 grpc_winsocket *socket;
81 grpc_winsocket_callback_info *info;
Craig Tiller258f8de2015-09-23 12:32:40 -070082 grpc_closure *closure = NULL;
Craig Tiller71a0f9d2015-09-28 17:22:01 -070083 success = GetQueuedCompletionStatus(
84 g_iocp, &bytes, &completion_key, &overlapped,
85 deadline_to_millis_timeout(deadline, gpr_now(deadline.clock_type)));
Craig Tiller3dedb702015-09-24 10:31:26 -070086 if (success == 0 && overlapped == NULL) {
87 return;
88 }
Craig Tillera82950e2015-09-22 12:33:20 -070089 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 Noble45e67a32015-02-09 16:20:49 -080095 }
Craig Tillera82950e2015-09-22 12:33:20 -070096 gpr_log(GPR_ERROR, "Unknown custom completion key.");
97 abort();
98 }
Nicolas Noble45e67a32015-02-09 16:20:49 -080099
Craig Tillera82950e2015-09-22 12:33:20 -0700100 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 Noble45e67a32015-02-09 16:20:49 -0800111 info->bytes_transfered = bytes;
Craig Tillera82950e2015-09-22 12:33:20 -0700112 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 Tiller82f9bd82015-09-23 09:31:51 -0700116 if (info->closure) {
Craig Tiller258f8de2015-09-23 12:32:40 -0700117 closure = info->closure;
Craig Tiller82f9bd82015-09-23 09:31:51 -0700118 info->closure = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700119 } else {
120 info->has_pending_iocp = 1;
121 }
122 gpr_mu_unlock(&socket->state_mu);
Craig Tillereced8ae2016-01-28 14:13:20 -0800123 grpc_exec_ctx_enqueue(exec_ctx, closure, true, NULL);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800124}
125
Craig Tillera82950e2015-09-22 12:33:20 -0700126void grpc_iocp_init(void) {
Craig Tillera82950e2015-09-22 12:33:20 -0700127 g_iocp =
128 CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)NULL, 0);
129 GPR_ASSERT(g_iocp);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800130}
131
Craig Tillera82950e2015-09-22 12:33:20 -0700132void grpc_iocp_kick(void) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800133 BOOL success;
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700134
Craig Tillera82950e2015-09-22 12:33:20 -0700135 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 Noble8703f4d2015-03-23 13:52:18 -0700139}
140
Craig Tiller01be53d2015-09-30 08:36:03 -0700141void grpc_iocp_flush(void) {
142 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Craig Tillerb7a59772015-10-01 08:01:58 -0700143
Craig Tiller01be53d2015-09-30 08:36:03 -0700144 do {
Craig Tiller14337912015-09-30 08:41:51 -0700145 grpc_iocp_work(&exec_ctx, gpr_inf_past(GPR_CLOCK_MONOTONIC));
Craig Tiller01be53d2015-09-30 08:36:03 -0700146 } while (grpc_exec_ctx_flush(&exec_ctx));
147}
148
Craig Tillera82950e2015-09-22 12:33:20 -0700149void grpc_iocp_shutdown(void) {
Craig Tiller3dedb702015-09-24 10:31:26 -0700150 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 Noble45e67a32015-02-09 16:20:49 -0800157}
158
Craig Tillera82950e2015-09-22 12:33:20 -0700159void grpc_iocp_add_socket(grpc_winsocket *socket) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800160 HANDLE ret;
Craig Tillera82950e2015-09-22 12:33:20 -0700161 if (socket->added_to_iocp) return;
162 ret = CreateIoCompletionPort((HANDLE)socket->socket, g_iocp,
Craig Tiller7536af02015-12-22 13:49:30 -0800163 (uintptr_t)socket, 0);
Craig Tillera82950e2015-09-22 12:33:20 -0700164 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 Noble45e67a32015-02-09 16:20:49 -0800171 socket->added_to_iocp = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700172 GPR_ASSERT(ret == g_iocp);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800173}
174
Nicolas "Pixel" Noble0f981e92015-05-03 10:40:56 +0200175/* 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 Tiller82f9bd82015-09-23 09:31:51 -0700179static void socket_notify_on_iocp(grpc_exec_ctx *exec_ctx,
Craig Tiller565b18b2015-09-23 10:09:42 -0700180 grpc_winsocket *socket, grpc_closure *closure,
Craig Tillera82950e2015-09-22 12:33:20 -0700181 grpc_winsocket_callback_info *info) {
Craig Tiller82f9bd82015-09-23 09:31:51 -0700182 GPR_ASSERT(info->closure == NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700183 gpr_mu_lock(&socket->state_mu);
184 if (info->has_pending_iocp) {
Craig Tillera82950e2015-09-22 12:33:20 -0700185 info->has_pending_iocp = 0;
Craig Tillereced8ae2016-01-28 14:13:20 -0800186 grpc_exec_ctx_enqueue(exec_ctx, closure, true, NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700187 } else {
Craig Tiller82f9bd82015-09-23 09:31:51 -0700188 info->closure = closure;
Craig Tillera82950e2015-09-22 12:33:20 -0700189 }
190 gpr_mu_unlock(&socket->state_mu);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800191}
192
Craig Tiller82f9bd82015-09-23 09:31:51 -0700193void 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 Noble45e67a32015-02-09 16:20:49 -0800197}
198
Craig Tiller565b18b2015-09-23 10:09:42 -0700199void grpc_socket_notify_on_read(grpc_exec_ctx *exec_ctx, grpc_winsocket *socket,
Craig Tiller82f9bd82015-09-23 09:31:51 -0700200 grpc_closure *closure) {
201 socket_notify_on_iocp(exec_ctx, socket, closure, &socket->read_info);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800202}
203
Craig Tillerd6c98df2015-08-18 09:33:44 -0700204#endif /* GPR_WINSOCK_SOCKET */