blob: 0378cf0c7655a293a93dfd96f4e72d08b5b8e5a3 [file] [log] [blame]
Nicolas Noble45e67a32015-02-09 16:20:49 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, 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
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
50static ULONG g_iocp_kick_token;
51static OVERLAPPED g_iocp_custom_overlap;
52
53static gpr_event g_shutdown_iocp;
54static gpr_event g_iocp_done;
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +020055static gpr_atm g_custom_events = 0;
Nicolas Noble45e67a32015-02-09 16:20:49 -080056
57static HANDLE g_iocp;
58
Craig Tiller82f9bd82015-09-23 09:31:51 -070059static void do_iocp_work(grpc_exec_ctx *exec_ctx) {
Nicolas Noble45e67a32015-02-09 16:20:49 -080060 BOOL success;
61 DWORD bytes = 0;
62 DWORD flags = 0;
63 ULONG_PTR completion_key;
64 LPOVERLAPPED overlapped;
Nicolas Noble45e67a32015-02-09 16:20:49 -080065 grpc_winsocket *socket;
66 grpc_winsocket_callback_info *info;
Craig Tillera82950e2015-09-22 12:33:20 -070067 success = GetQueuedCompletionStatus(g_iocp, &bytes, &completion_key,
68 &overlapped, INFINITE);
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +020069 /* success = 0 and overlapped = NULL means the deadline got attained.
70 Which is impossible. since our wait time is +inf */
Craig Tillera82950e2015-09-22 12:33:20 -070071 GPR_ASSERT(success || overlapped);
72 GPR_ASSERT(completion_key && overlapped);
73 if (overlapped == &g_iocp_custom_overlap) {
74 gpr_atm_full_fetch_add(&g_custom_events, -1);
75 if (completion_key == (ULONG_PTR)&g_iocp_kick_token) {
76 /* We were awoken from a kick. */
77 return;
Nicolas Noble45e67a32015-02-09 16:20:49 -080078 }
Craig Tillera82950e2015-09-22 12:33:20 -070079 gpr_log(GPR_ERROR, "Unknown custom completion key.");
80 abort();
81 }
Nicolas Noble45e67a32015-02-09 16:20:49 -080082
Craig Tillera82950e2015-09-22 12:33:20 -070083 socket = (grpc_winsocket *)completion_key;
84 if (overlapped == &socket->write_info.overlapped) {
85 info = &socket->write_info;
86 } else if (overlapped == &socket->read_info.overlapped) {
87 info = &socket->read_info;
88 } else {
89 gpr_log(GPR_ERROR, "Unknown IOCP operation");
90 abort();
91 }
92 success = WSAGetOverlappedResult(socket->socket, &info->overlapped, &bytes,
93 FALSE, &flags);
Nicolas Noble45e67a32015-02-09 16:20:49 -080094 info->bytes_transfered = bytes;
Craig Tillera82950e2015-09-22 12:33:20 -070095 info->wsa_error = success ? 0 : WSAGetLastError();
96 GPR_ASSERT(overlapped == &info->overlapped);
97 GPR_ASSERT(!info->has_pending_iocp);
98 gpr_mu_lock(&socket->state_mu);
Craig Tiller82f9bd82015-09-23 09:31:51 -070099 if (info->closure) {
100 grpc_exec_ctx_enqueue(exec_ctx, info->closure, 1);
101 info->closure = NULL;
Craig Tillera82950e2015-09-22 12:33:20 -0700102 } else {
103 info->has_pending_iocp = 1;
104 }
105 gpr_mu_unlock(&socket->state_mu);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800106}
107
Craig Tillera82950e2015-09-22 12:33:20 -0700108static void iocp_loop(void *p) {
Craig Tiller82f9bd82015-09-23 09:31:51 -0700109 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
110
Craig Tillera82950e2015-09-22 12:33:20 -0700111 while (gpr_atm_acq_load(&g_custom_events) ||
112 !gpr_event_get(&g_shutdown_iocp)) {
Craig Tiller82f9bd82015-09-23 09:31:51 -0700113 do_iocp_work(&exec_ctx);
114 grpc_exec_ctx_flush(&exec_ctx);
Craig Tillera82950e2015-09-22 12:33:20 -0700115 }
Craig Tiller82f9bd82015-09-23 09:31:51 -0700116 grpc_exec_ctx_finish(&exec_ctx);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800117
Craig Tillera82950e2015-09-22 12:33:20 -0700118 gpr_event_set(&g_iocp_done, (void *)1);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800119}
120
Craig Tillera82950e2015-09-22 12:33:20 -0700121void grpc_iocp_init(void) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800122 gpr_thd_id id;
123
Craig Tillera82950e2015-09-22 12:33:20 -0700124 g_iocp =
125 CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)NULL, 0);
126 GPR_ASSERT(g_iocp);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800127
Craig Tillera82950e2015-09-22 12:33:20 -0700128 gpr_event_init(&g_iocp_done);
129 gpr_event_init(&g_shutdown_iocp);
130 gpr_thd_new(&id, iocp_loop, NULL, NULL);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800131}
132
Craig Tillera82950e2015-09-22 12:33:20 -0700133void grpc_iocp_kick(void) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800134 BOOL success;
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700135
Craig Tillera82950e2015-09-22 12:33:20 -0700136 gpr_atm_full_fetch_add(&g_custom_events, 1);
137 success = PostQueuedCompletionStatus(g_iocp, 0, (ULONG_PTR)&g_iocp_kick_token,
138 &g_iocp_custom_overlap);
139 GPR_ASSERT(success);
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700140}
141
Craig Tillera82950e2015-09-22 12:33:20 -0700142void grpc_iocp_shutdown(void) {
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700143 BOOL success;
Craig Tillera82950e2015-09-22 12:33:20 -0700144 gpr_event_set(&g_shutdown_iocp, (void *)1);
145 grpc_iocp_kick();
146 gpr_event_wait(&g_iocp_done, gpr_inf_future(GPR_CLOCK_REALTIME));
147 success = CloseHandle(g_iocp);
148 GPR_ASSERT(success);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800149}
150
Craig Tillera82950e2015-09-22 12:33:20 -0700151void grpc_iocp_add_socket(grpc_winsocket *socket) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800152 HANDLE ret;
Craig Tillera82950e2015-09-22 12:33:20 -0700153 if (socket->added_to_iocp) return;
154 ret = CreateIoCompletionPort((HANDLE)socket->socket, g_iocp,
155 (gpr_uintptr)socket, 0);
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 }
Nicolas Noble45e67a32015-02-09 16:20:49 -0800163 socket->added_to_iocp = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700164 GPR_ASSERT(ret == g_iocp);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800165}
166
Nicolas "Pixel" Noble0f981e92015-05-03 10:40:56 +0200167/* 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. */
Craig Tiller82f9bd82015-09-23 09:31:51 -0700171static void socket_notify_on_iocp(grpc_exec_ctx *exec_ctx,
172 grpc_winsocket *socket,
173 grpc_closure *closure,
Craig Tillera82950e2015-09-22 12:33:20 -0700174 grpc_winsocket_callback_info *info) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800175 int run_now = 0;
Craig Tiller82f9bd82015-09-23 09:31:51 -0700176 GPR_ASSERT(info->closure == NULL);
Craig Tillera82950e2015-09-22 12:33:20 -0700177 gpr_mu_lock(&socket->state_mu);
178 if (info->has_pending_iocp) {
179 run_now = 1;
180 info->has_pending_iocp = 0;
Craig Tiller82f9bd82015-09-23 09:31:51 -0700181 grpc_exec_ctx_enqueue(exec_ctx, closure, 1);
Craig Tillera82950e2015-09-22 12:33:20 -0700182 } else {
Craig Tiller82f9bd82015-09-23 09:31:51 -0700183 info->closure = closure;
Craig Tillera82950e2015-09-22 12:33:20 -0700184 }
185 gpr_mu_unlock(&socket->state_mu);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800186}
187
Craig Tiller82f9bd82015-09-23 09:31:51 -0700188void grpc_socket_notify_on_write(grpc_exec_ctx *exec_ctx,
189 grpc_winsocket *socket,
190 grpc_closure *closure) {
191 socket_notify_on_iocp(exec_ctx, socket, closure, &socket->write_info);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800192}
193
Craig Tiller82f9bd82015-09-23 09:31:51 -0700194void grpc_socket_notify_on_read(grpc_exec_ctx *exec_ctx,
195 grpc_winsocket *socket,
196 grpc_closure *closure) {
197 socket_notify_on_iocp(exec_ctx, socket, closure, &socket->read_info);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800198}
199
Craig Tillerd6c98df2015-08-18 09:33:44 -0700200#endif /* GPR_WINSOCK_SOCKET */