blob: 006f8b2abff8f42e8214107a6f363dadc06d426b [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
Nicolas Noble8703f4d2015-03-23 13:52:18 -070059static void do_iocp_work() {
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 Tillerd6c98df2015-08-18 09:33:44 -070067 void (*f)(void *, int) = NULL;
Nicolas Noble45e67a32015-02-09 16:20:49 -080068 void *opaque = NULL;
Craig Tillerd6c98df2015-08-18 09:33:44 -070069 success = GetQueuedCompletionStatus(g_iocp, &bytes, &completion_key,
70 &overlapped, INFINITE);
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +020071 /* success = 0 and overlapped = NULL means the deadline got attained.
72 Which is impossible. since our wait time is +inf */
73 GPR_ASSERT(success || overlapped);
Nicolas Noble45e67a32015-02-09 16:20:49 -080074 GPR_ASSERT(completion_key && overlapped);
75 if (overlapped == &g_iocp_custom_overlap) {
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +020076 gpr_atm_full_fetch_add(&g_custom_events, -1);
Craig Tillerd6c98df2015-08-18 09:33:44 -070077 if (completion_key == (ULONG_PTR)&g_iocp_kick_token) {
Nicolas Noble45e67a32015-02-09 16:20:49 -080078 /* We were awoken from a kick. */
Nicolas Noble8703f4d2015-03-23 13:52:18 -070079 return;
Nicolas Noble45e67a32015-02-09 16:20:49 -080080 }
81 gpr_log(GPR_ERROR, "Unknown custom completion key.");
82 abort();
83 }
84
Craig Tillerd6c98df2015-08-18 09:33:44 -070085 socket = (grpc_winsocket *)completion_key;
Nicolas Noble45e67a32015-02-09 16:20:49 -080086 if (overlapped == &socket->write_info.overlapped) {
Nicolas Noble45e67a32015-02-09 16:20:49 -080087 info = &socket->write_info;
88 } else if (overlapped == &socket->read_info.overlapped) {
Nicolas Noble45e67a32015-02-09 16:20:49 -080089 info = &socket->read_info;
90 } else {
91 gpr_log(GPR_ERROR, "Unknown IOCP operation");
92 abort();
93 }
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +020094 success = WSAGetOverlappedResult(socket->socket, &info->overlapped, &bytes,
95 FALSE, &flags);
Nicolas Noble45e67a32015-02-09 16:20:49 -080096 info->bytes_transfered = bytes;
97 info->wsa_error = success ? 0 : WSAGetLastError();
98 GPR_ASSERT(overlapped == &info->overlapped);
Nicolas Noble45e67a32015-02-09 16:20:49 -080099 GPR_ASSERT(!info->has_pending_iocp);
Craig Tiller9f80fcf2015-08-28 08:22:48 -0700100 gpr_mu_lock(&socket->state_mu);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800101 if (info->cb) {
102 f = info->cb;
103 opaque = info->opaque;
104 info->cb = NULL;
105 } else {
106 info->has_pending_iocp = 1;
107 }
108 gpr_mu_unlock(&socket->state_mu);
109 if (f) f(opaque, 1);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800110}
111
112static void iocp_loop(void *p) {
Craig Tiller9f80fcf2015-08-28 08:22:48 -0700113 while (gpr_atm_acq_load(&g_custom_events) ||
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +0200114 !gpr_event_get(&g_shutdown_iocp)) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800115 do_iocp_work();
116 }
117
118 gpr_event_set(&g_iocp_done, (void *)1);
119}
120
121void grpc_iocp_init(void) {
122 gpr_thd_id id;
123
Craig Tillerd6c98df2015-08-18 09:33:44 -0700124 g_iocp =
125 CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)NULL, 0);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800126 GPR_ASSERT(g_iocp);
127
128 gpr_event_init(&g_iocp_done);
129 gpr_event_init(&g_shutdown_iocp);
130 gpr_thd_new(&id, iocp_loop, NULL, NULL);
131}
132
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700133void grpc_iocp_kick(void) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800134 BOOL success;
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700135
Nicolas "Pixel" Noble7f2e98c2015-05-08 01:41:21 +0200136 gpr_atm_full_fetch_add(&g_custom_events, 1);
Craig Tillerd6c98df2015-08-18 09:33:44 -0700137 success = PostQueuedCompletionStatus(g_iocp, 0, (ULONG_PTR)&g_iocp_kick_token,
Nicolas Noble45e67a32015-02-09 16:20:49 -0800138 &g_iocp_custom_overlap);
139 GPR_ASSERT(success);
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700140}
141
142void grpc_iocp_shutdown(void) {
143 BOOL success;
144 gpr_event_set(&g_shutdown_iocp, (void *)1);
145 grpc_iocp_kick();
Craig Tiller143e7bf2015-07-13 08:41:49 -0700146 gpr_event_wait(&g_iocp_done, gpr_inf_future(GPR_CLOCK_REALTIME));
Nicolas Noble45e67a32015-02-09 16:20:49 -0800147 success = CloseHandle(g_iocp);
148 GPR_ASSERT(success);
149}
150
151void grpc_iocp_add_socket(grpc_winsocket *socket) {
152 HANDLE ret;
153 if (socket->added_to_iocp) return;
Craig Tillerd6c98df2015-08-18 09:33:44 -0700154 ret = CreateIoCompletionPort((HANDLE)socket->socket, g_iocp,
155 (gpr_uintptr)socket, 0);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800156 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 }
163 socket->added_to_iocp = 1;
164 GPR_ASSERT(ret == g_iocp);
165}
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. */
Nicolas Noble45e67a32015-02-09 16:20:49 -0800171static void socket_notify_on_iocp(grpc_winsocket *socket,
Craig Tillerd6c98df2015-08-18 09:33:44 -0700172 void (*cb)(void *, int), void *opaque,
Nicolas Noble45e67a32015-02-09 16:20:49 -0800173 grpc_winsocket_callback_info *info) {
174 int run_now = 0;
175 GPR_ASSERT(!info->cb);
176 gpr_mu_lock(&socket->state_mu);
177 if (info->has_pending_iocp) {
178 run_now = 1;
179 info->has_pending_iocp = 0;
Nicolas Noble45e67a32015-02-09 16:20:49 -0800180 } else {
181 info->cb = cb;
182 info->opaque = opaque;
Nicolas Noble45e67a32015-02-09 16:20:49 -0800183 }
184 gpr_mu_unlock(&socket->state_mu);
185 if (run_now) cb(opaque, 1);
186}
187
188void grpc_socket_notify_on_write(grpc_winsocket *socket,
Craig Tillerd6c98df2015-08-18 09:33:44 -0700189 void (*cb)(void *, int), void *opaque) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800190 socket_notify_on_iocp(socket, cb, opaque, &socket->write_info);
191}
192
Craig Tillerd6c98df2015-08-18 09:33:44 -0700193void grpc_socket_notify_on_read(grpc_winsocket *socket, void (*cb)(void *, int),
194 void *opaque) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800195 socket_notify_on_iocp(socket, cb, opaque, &socket->read_info);
196}
197
Craig Tillerd6c98df2015-08-18 09:33:44 -0700198#endif /* GPR_WINSOCK_SOCKET */