blob: f0f4a6ff39096b7dcf19fea7d12010a166fa5537 [file] [log] [blame]
Nicolas Noble45e67a32015-02-09 16:20:49 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * 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
murgatroid9954070892016-08-08 17:01:18 -070034#include "src/core/lib/iomgr/port.h"
Nicolas Noble45e67a32015-02-09 16:20:49 -080035
murgatroid99623dd4f2016-08-08 17:31:27 -070036#ifdef GRPC_WINSOCK_SOCKET
Nicolas Noble45e67a32015-02-09 16:20:49 -080037
38#include <winsock2.h>
39
Craig Tillerf40df232016-03-25 13:38:14 -070040#include <grpc/support/alloc.h>
Nicolas Noble45e67a32015-02-09 16:20:49 -080041#include <grpc/support/log.h>
Yuchen Zeng12dfdc32016-04-26 22:05:41 -070042#include <grpc/support/log_windows.h>
Nicolas Noble45e67a32015-02-09 16:20:49 -080043#include <grpc/support/thd.h>
44
Craig Tiller9533d042016-03-25 17:11:06 -070045#include "src/core/lib/iomgr/iocp_windows.h"
46#include "src/core/lib/iomgr/iomgr_internal.h"
47#include "src/core/lib/iomgr/socket_windows.h"
48#include "src/core/lib/iomgr/timer.h"
Nicolas Noble45e67a32015-02-09 16:20:49 -080049
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
yang-g9aa4f1b2016-03-02 16:39:13 -080074grpc_iocp_work_status grpc_iocp_work(grpc_exec_ctx *exec_ctx,
75 gpr_timespec deadline) {
Nicolas Noble45e67a32015-02-09 16:20:49 -080076 BOOL success;
77 DWORD bytes = 0;
78 DWORD flags = 0;
79 ULONG_PTR completion_key;
80 LPOVERLAPPED overlapped;
Nicolas Noble45e67a32015-02-09 16:20:49 -080081 grpc_winsocket *socket;
82 grpc_winsocket_callback_info *info;
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) {
yang-g9aa4f1b2016-03-02 16:39:13 -080087 return GRPC_IOCP_WORK_TIMEOUT;
Craig Tiller3dedb702015-09-24 10:31:26 -070088 }
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. */
yang-g9aa4f1b2016-03-02 16:39:13 -080094 return GRPC_IOCP_WORK_KICK;
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 {
Craig Tillera82950e2015-09-22 12:33:20 -0700106 abort();
107 }
108 success = WSAGetOverlappedResult(socket->socket, &info->overlapped, &bytes,
109 FALSE, &flags);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800110 info->bytes_transfered = bytes;
Craig Tillera82950e2015-09-22 12:33:20 -0700111 info->wsa_error = success ? 0 : WSAGetLastError();
112 GPR_ASSERT(overlapped == &info->overlapped);
Craig Tiller5352ff12016-05-18 10:44:38 -0700113 grpc_socket_become_ready(exec_ctx, socket, info);
yang-g9aa4f1b2016-03-02 16:39:13 -0800114 return GRPC_IOCP_WORK_WORK;
Nicolas Noble45e67a32015-02-09 16:20:49 -0800115}
116
Craig Tillera82950e2015-09-22 12:33:20 -0700117void grpc_iocp_init(void) {
Craig Tillera82950e2015-09-22 12:33:20 -0700118 g_iocp =
119 CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)NULL, 0);
120 GPR_ASSERT(g_iocp);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800121}
122
Craig Tillera82950e2015-09-22 12:33:20 -0700123void grpc_iocp_kick(void) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800124 BOOL success;
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700125
Craig Tillera82950e2015-09-22 12:33:20 -0700126 gpr_atm_full_fetch_add(&g_custom_events, 1);
127 success = PostQueuedCompletionStatus(g_iocp, 0, (ULONG_PTR)&g_iocp_kick_token,
128 &g_iocp_custom_overlap);
129 GPR_ASSERT(success);
Nicolas Noble8703f4d2015-03-23 13:52:18 -0700130}
131
Craig Tiller01be53d2015-09-30 08:36:03 -0700132void grpc_iocp_flush(void) {
133 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
yang-g9aa4f1b2016-03-02 16:39:13 -0800134 grpc_iocp_work_status work_status;
Craig Tillerb7a59772015-10-01 08:01:58 -0700135
Craig Tiller01be53d2015-09-30 08:36:03 -0700136 do {
yang-g9aa4f1b2016-03-02 16:39:13 -0800137 work_status = grpc_iocp_work(&exec_ctx, gpr_inf_past(GPR_CLOCK_MONOTONIC));
138 } while (work_status == GRPC_IOCP_WORK_KICK ||
139 grpc_exec_ctx_flush(&exec_ctx));
Craig Tiller01be53d2015-09-30 08:36:03 -0700140}
141
Craig Tillera82950e2015-09-22 12:33:20 -0700142void grpc_iocp_shutdown(void) {
Craig Tiller3dedb702015-09-24 10:31:26 -0700143 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
144 while (gpr_atm_acq_load(&g_custom_events)) {
145 grpc_iocp_work(&exec_ctx, gpr_inf_future(GPR_CLOCK_MONOTONIC));
146 grpc_exec_ctx_flush(&exec_ctx);
147 }
148 grpc_exec_ctx_finish(&exec_ctx);
149 GPR_ASSERT(CloseHandle(g_iocp));
Nicolas Noble45e67a32015-02-09 16:20:49 -0800150}
151
Craig Tillera82950e2015-09-22 12:33:20 -0700152void grpc_iocp_add_socket(grpc_winsocket *socket) {
Nicolas Noble45e67a32015-02-09 16:20:49 -0800153 HANDLE ret;
Craig Tillera82950e2015-09-22 12:33:20 -0700154 if (socket->added_to_iocp) return;
155 ret = CreateIoCompletionPort((HANDLE)socket->socket, g_iocp,
Craig Tiller7536af02015-12-22 13:49:30 -0800156 (uintptr_t)socket, 0);
Craig Tillera82950e2015-09-22 12:33:20 -0700157 if (!ret) {
158 char *utf8_message = gpr_format_message(WSAGetLastError());
159 gpr_log(GPR_ERROR, "Unable to add socket to iocp: %s", utf8_message);
160 gpr_free(utf8_message);
161 __debugbreak();
162 abort();
163 }
Nicolas Noble45e67a32015-02-09 16:20:49 -0800164 socket->added_to_iocp = 1;
Craig Tillera82950e2015-09-22 12:33:20 -0700165 GPR_ASSERT(ret == g_iocp);
Nicolas Noble45e67a32015-02-09 16:20:49 -0800166}
167
murgatroid99623dd4f2016-08-08 17:31:27 -0700168#endif /* GRPC_WINSOCK_SOCKET */