blob: 0c62bfccd51542b6a7deeb3d68e7b830635c6c60 [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 }
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +0200130 gpr_log(GPR_DEBUG, "iocp_loop is done");
Nicolas Noble45e67a32015-02-09 16:20:49 -0800131
132 gpr_event_set(&g_iocp_done, (void *)1);
133}
134
135void grpc_iocp_init(void) {
136 gpr_thd_id id;
137
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +0200138 g_iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE,
139 NULL, (ULONG_PTR)NULL, 0);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800140 GPR_ASSERT(g_iocp);
141
142 gpr_event_init(&g_iocp_done);
143 gpr_event_init(&g_shutdown_iocp);
144 gpr_thd_new(&id, iocp_loop, NULL, NULL);
145}
146
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700147void grpc_iocp_kick(void) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800148 BOOL success;
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700149
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +0200150 gpr_atm_full_fetch_add(&g_custom_events, 1);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800151 success = PostQueuedCompletionStatus(g_iocp, 0,
152 (ULONG_PTR) &g_iocp_kick_token,
153 &g_iocp_custom_overlap);
154 GPR_ASSERT(success);
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700155}
156
157void grpc_iocp_shutdown(void) {
158 BOOL success;
159 gpr_event_set(&g_shutdown_iocp, (void *)1);
160 grpc_iocp_kick();
Nicolas Noble45e67a32015-02-09 16:20:49 -0800161 gpr_event_wait(&g_iocp_done, gpr_inf_future);
162 success = CloseHandle(g_iocp);
163 GPR_ASSERT(success);
164}
165
166void grpc_iocp_add_socket(grpc_winsocket *socket) {
167 HANDLE ret;
168 if (socket->added_to_iocp) return;
169 ret = CreateIoCompletionPort((HANDLE)socket->socket,
170 g_iocp, (gpr_uintptr) socket, 0);
171 if (!ret) {
172 char *utf8_message = gpr_format_message(WSAGetLastError());
173 gpr_log(GPR_ERROR, "Unable to add socket to iocp: %s", utf8_message);
174 gpr_free(utf8_message);
175 __debugbreak();
176 abort();
177 }
178 socket->added_to_iocp = 1;
179 GPR_ASSERT(ret == g_iocp);
180}
181
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700182void grpc_iocp_socket_orphan(grpc_winsocket *socket) {
Nicolas "Pixel" Noble8e1a55d2015-05-02 15:21:25 -0700183 GPR_ASSERT(!socket->orphan);
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700184 gpr_atm_full_fetch_add(&g_orphans, 1);
Nicolas "Pixel" Noble8e1a55d2015-05-02 15:21:25 -0700185 socket->orphan = 1;
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700186}
187
Nicolas "Pixel" Noble0f981e92015-05-03 10:40:56 +0200188/* Calling notify_on_read or write means either of two things:
189 -) The IOCP already completed in the background, and we need to call
190 the callback now.
191 -) The IOCP hasn't completed yet, and we're queuing it for later. */
Nicolas Noble45e67a32015-02-09 16:20:49 -0800192static void socket_notify_on_iocp(grpc_winsocket *socket,
193 void(*cb)(void *, int), void *opaque,
194 grpc_winsocket_callback_info *info) {
195 int run_now = 0;
196 GPR_ASSERT(!info->cb);
197 gpr_mu_lock(&socket->state_mu);
198 if (info->has_pending_iocp) {
199 run_now = 1;
200 info->has_pending_iocp = 0;
Nicolas Noble45e67a32015-02-09 16:20:49 -0800201 } else {
202 info->cb = cb;
203 info->opaque = opaque;
Nicolas Noble45e67a32015-02-09 16:20:49 -0800204 }
205 gpr_mu_unlock(&socket->state_mu);
206 if (run_now) cb(opaque, 1);
207}
208
209void grpc_socket_notify_on_write(grpc_winsocket *socket,
210 void(*cb)(void *, int), void *opaque) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800211 socket_notify_on_iocp(socket, cb, opaque, &socket->write_info);
212}
213
214void grpc_socket_notify_on_read(grpc_winsocket *socket,
215 void(*cb)(void *, int), void *opaque) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800216 socket_notify_on_iocp(socket, cb, opaque, &socket->read_info);
217}
218
Craig Tiller190d3602015-02-18 09:23:38 -0800219#endif /* GPR_WINSOCK_SOCKET */