blob: 3d3a193d00d9abd4a83edf8c764b15f30fe57c52 [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 Noble8703f4d2015-03-23 13:52:18 -070055static gpr_atm g_orphans = 0;
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +020056static gpr_atm g_custom_events = 0;
Nicolas Noble45e67a32015-02-09 16:20:49 -080057
58static HANDLE g_iocp;
59
Nicolas Noble8703f4d2015-03-23 13:52:18 -070060static void do_iocp_work() {
Nicolas Noble45e67a32015-02-09 16:20:49 -080061 BOOL success;
62 DWORD bytes = 0;
63 DWORD flags = 0;
64 ULONG_PTR completion_key;
65 LPOVERLAPPED overlapped;
Nicolas Noble45e67a32015-02-09 16:20:49 -080066 grpc_winsocket *socket;
67 grpc_winsocket_callback_info *info;
68 void(*f)(void *, int) = NULL;
69 void *opaque = NULL;
70 success = GetQueuedCompletionStatus(g_iocp, &bytes,
71 &completion_key, &overlapped,
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +020072 INFINITE);
73 /* success = 0 and overlapped = NULL means the deadline got attained.
74 Which is impossible. since our wait time is +inf */
75 GPR_ASSERT(success || overlapped);
Nicolas Noble45e67a32015-02-09 16:20:49 -080076 GPR_ASSERT(completion_key && overlapped);
77 if (overlapped == &g_iocp_custom_overlap) {
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +020078 gpr_atm_full_fetch_add(&g_custom_events, -1);
Nicolas Noble45e67a32015-02-09 16:20:49 -080079 if (completion_key == (ULONG_PTR) &g_iocp_kick_token) {
80 /* We were awoken from a kick. */
Nicolas Noble8703f4d2015-03-23 13:52:18 -070081 return;
Nicolas Noble45e67a32015-02-09 16:20:49 -080082 }
83 gpr_log(GPR_ERROR, "Unknown custom completion key.");
84 abort();
85 }
86
87 socket = (grpc_winsocket*) completion_key;
88 if (overlapped == &socket->write_info.overlapped) {
Nicolas Noble45e67a32015-02-09 16:20:49 -080089 info = &socket->write_info;
90 } else if (overlapped == &socket->read_info.overlapped) {
Nicolas Noble45e67a32015-02-09 16:20:49 -080091 info = &socket->read_info;
92 } else {
93 gpr_log(GPR_ERROR, "Unknown IOCP operation");
94 abort();
95 }
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +020096 GPR_ASSERT(info->outstanding);
Nicolas Noble8703f4d2015-03-23 13:52:18 -070097 if (socket->orphan) {
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +020098 info->outstanding = 0;
99 if (!socket->read_info.outstanding && !socket->write_info.outstanding) {
100 grpc_winsocket_destroy(socket);
101 gpr_atm_full_fetch_add(&g_orphans, -1);
102 }
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700103 return;
104 }
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +0200105 success = WSAGetOverlappedResult(socket->socket, &info->overlapped, &bytes,
106 FALSE, &flags);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800107 info->bytes_transfered = bytes;
108 info->wsa_error = success ? 0 : WSAGetLastError();
109 GPR_ASSERT(overlapped == &info->overlapped);
110 gpr_mu_lock(&socket->state_mu);
111 GPR_ASSERT(!info->has_pending_iocp);
112 if (info->cb) {
113 f = info->cb;
114 opaque = info->opaque;
115 info->cb = NULL;
116 } else {
117 info->has_pending_iocp = 1;
118 }
119 gpr_mu_unlock(&socket->state_mu);
120 if (f) f(opaque, 1);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800121}
122
123static void iocp_loop(void *p) {
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +0200124 while (gpr_atm_acq_load(&g_orphans) ||
125 gpr_atm_acq_load(&g_custom_events) ||
126 !gpr_event_get(&g_shutdown_iocp)) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800127 grpc_maybe_call_delayed_callbacks(NULL, 1);
128 do_iocp_work();
129 }
130
131 gpr_event_set(&g_iocp_done, (void *)1);
132}
133
134void grpc_iocp_init(void) {
135 gpr_thd_id id;
136
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +0200137 g_iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE,
138 NULL, (ULONG_PTR)NULL, 0);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800139 GPR_ASSERT(g_iocp);
140
141 gpr_event_init(&g_iocp_done);
142 gpr_event_init(&g_shutdown_iocp);
143 gpr_thd_new(&id, iocp_loop, NULL, NULL);
144}
145
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700146void grpc_iocp_kick(void) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800147 BOOL success;
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700148
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +0200149 gpr_atm_full_fetch_add(&g_custom_events, 1);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800150 success = PostQueuedCompletionStatus(g_iocp, 0,
151 (ULONG_PTR) &g_iocp_kick_token,
152 &g_iocp_custom_overlap);
153 GPR_ASSERT(success);
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700154}
155
156void grpc_iocp_shutdown(void) {
157 BOOL success;
158 gpr_event_set(&g_shutdown_iocp, (void *)1);
159 grpc_iocp_kick();
Nicolas Noble45e67a32015-02-09 16:20:49 -0800160 gpr_event_wait(&g_iocp_done, gpr_inf_future);
161 success = CloseHandle(g_iocp);
162 GPR_ASSERT(success);
163}
164
165void grpc_iocp_add_socket(grpc_winsocket *socket) {
166 HANDLE ret;
167 if (socket->added_to_iocp) return;
168 ret = CreateIoCompletionPort((HANDLE)socket->socket,
169 g_iocp, (gpr_uintptr) socket, 0);
170 if (!ret) {
171 char *utf8_message = gpr_format_message(WSAGetLastError());
172 gpr_log(GPR_ERROR, "Unable to add socket to iocp: %s", utf8_message);
173 gpr_free(utf8_message);
174 __debugbreak();
175 abort();
176 }
177 socket->added_to_iocp = 1;
178 GPR_ASSERT(ret == g_iocp);
179}
180
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700181void grpc_iocp_socket_orphan(grpc_winsocket *socket) {
Nicolas "Pixel" Noble8e1a55d2015-05-02 15:21:25 -0700182 GPR_ASSERT(!socket->orphan);
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700183 gpr_atm_full_fetch_add(&g_orphans, 1);
Nicolas "Pixel" Noble8e1a55d2015-05-02 15:21:25 -0700184 socket->orphan = 1;
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700185}
186
Nicolas "Pixel" Noble0f981e92015-05-03 10:40:56 +0200187/* Calling notify_on_read or write means either of two things:
188 -) The IOCP already completed in the background, and we need to call
189 the callback now.
190 -) The IOCP hasn't completed yet, and we're queuing it for later. */
Nicolas Noble45e67a32015-02-09 16:20:49 -0800191static void socket_notify_on_iocp(grpc_winsocket *socket,
192 void(*cb)(void *, int), void *opaque,
193 grpc_winsocket_callback_info *info) {
194 int run_now = 0;
195 GPR_ASSERT(!info->cb);
196 gpr_mu_lock(&socket->state_mu);
197 if (info->has_pending_iocp) {
198 run_now = 1;
199 info->has_pending_iocp = 0;
Nicolas Noble45e67a32015-02-09 16:20:49 -0800200 } else {
201 info->cb = cb;
202 info->opaque = opaque;
Nicolas Noble45e67a32015-02-09 16:20:49 -0800203 }
204 gpr_mu_unlock(&socket->state_mu);
205 if (run_now) cb(opaque, 1);
206}
207
208void grpc_socket_notify_on_write(grpc_winsocket *socket,
209 void(*cb)(void *, int), void *opaque) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800210 socket_notify_on_iocp(socket, cb, opaque, &socket->write_info);
211}
212
213void grpc_socket_notify_on_read(grpc_winsocket *socket,
214 void(*cb)(void *, int), void *opaque) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800215 socket_notify_on_iocp(socket, cb, opaque, &socket->read_info);
216}
217
Craig Tiller190d3602015-02-18 09:23:38 -0800218#endif /* GPR_WINSOCK_SOCKET */