1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/iomgr/port.h"
22
23 #ifdef GRPC_WINSOCK_SOCKET
24
25 #include <winsock2.h>
26
27 #include <limits>
28
29 #include <grpc/support/alloc.h>
30 #include <grpc/support/log.h>
31 #include <grpc/support/log_windows.h>
32
33 #include "src/core/lib/debug/stats.h"
34 #include "src/core/lib/gprpp/thd.h"
35 #include "src/core/lib/iomgr/iocp_windows.h"
36 #include "src/core/lib/iomgr/iomgr_internal.h"
37 #include "src/core/lib/iomgr/socket_windows.h"
38 #include "src/core/lib/iomgr/timer.h"
39
40 static ULONG g_iocp_kick_token;
41 static OVERLAPPED g_iocp_custom_overlap;
42
43 static gpr_atm g_custom_events = 0;
44
45 static HANDLE g_iocp;
46
deadline_to_millis_timeout(grpc_millis deadline)47 static DWORD deadline_to_millis_timeout(grpc_millis deadline) {
48 if (deadline == GRPC_MILLIS_INF_FUTURE) {
49 return INFINITE;
50 }
51 grpc_millis now = grpc_core::ExecCtx::Get()->Now();
52 if (deadline < now) return 0;
53 grpc_millis timeout = deadline - now;
54 if (timeout > std::numeric_limits<DWORD>::max()) return INFINITE;
55 return static_cast<DWORD>(deadline - now);
56 }
57
grpc_iocp_work(grpc_millis deadline)58 grpc_iocp_work_status grpc_iocp_work(grpc_millis deadline) {
59 BOOL success;
60 DWORD bytes = 0;
61 DWORD flags = 0;
62 ULONG_PTR completion_key;
63 LPOVERLAPPED overlapped;
64 grpc_winsocket* socket;
65 grpc_winsocket_callback_info* info;
66 GRPC_STATS_INC_SYSCALL_POLL();
67 success =
68 GetQueuedCompletionStatus(g_iocp, &bytes, &completion_key, &overlapped,
69 deadline_to_millis_timeout(deadline));
70 grpc_core::ExecCtx::Get()->InvalidateNow();
71 if (success == 0 && overlapped == NULL) {
72 return GRPC_IOCP_WORK_TIMEOUT;
73 }
74 GPR_ASSERT(completion_key && overlapped);
75 if (overlapped == &g_iocp_custom_overlap) {
76 gpr_atm_full_fetch_add(&g_custom_events, -1);
77 if (completion_key == (ULONG_PTR)&g_iocp_kick_token) {
78 /* We were awoken from a kick. */
79 return GRPC_IOCP_WORK_KICK;
80 }
81 gpr_log(GPR_ERROR, "Unknown custom completion key.");
82 abort();
83 }
84
85 socket = (grpc_winsocket*)completion_key;
86 if (overlapped == &socket->write_info.overlapped) {
87 info = &socket->write_info;
88 } else if (overlapped == &socket->read_info.overlapped) {
89 info = &socket->read_info;
90 } else {
91 abort();
92 }
93 if (socket->shutdown_called) {
94 info->bytes_transferred = 0;
95 info->wsa_error = WSA_OPERATION_ABORTED;
96 } else {
97 success = WSAGetOverlappedResult(socket->socket, &info->overlapped, &bytes,
98 FALSE, &flags);
99 info->bytes_transferred = bytes;
100 info->wsa_error = success ? 0 : WSAGetLastError();
101 }
102 GPR_ASSERT(overlapped == &info->overlapped);
103 grpc_socket_become_ready(socket, info);
104 return GRPC_IOCP_WORK_WORK;
105 }
106
grpc_iocp_init(void)107 void grpc_iocp_init(void) {
108 g_iocp =
109 CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)NULL, 0);
110 GPR_ASSERT(g_iocp);
111 }
112
grpc_iocp_kick(void)113 void grpc_iocp_kick(void) {
114 BOOL success;
115
116 gpr_atm_full_fetch_add(&g_custom_events, 1);
117 success = PostQueuedCompletionStatus(g_iocp, 0, (ULONG_PTR)&g_iocp_kick_token,
118 &g_iocp_custom_overlap);
119 GPR_ASSERT(success);
120 }
121
grpc_iocp_flush(void)122 void grpc_iocp_flush(void) {
123 grpc_core::ExecCtx exec_ctx;
124 grpc_iocp_work_status work_status;
125
126 do {
127 work_status = grpc_iocp_work(GRPC_MILLIS_INF_PAST);
128 } while (work_status == GRPC_IOCP_WORK_KICK ||
129 grpc_core::ExecCtx::Get()->Flush());
130 }
131
grpc_iocp_shutdown(void)132 void grpc_iocp_shutdown(void) {
133 grpc_core::ExecCtx exec_ctx;
134 while (gpr_atm_acq_load(&g_custom_events)) {
135 grpc_iocp_work(GRPC_MILLIS_INF_FUTURE);
136 grpc_core::ExecCtx::Get()->Flush();
137 }
138
139 GPR_ASSERT(CloseHandle(g_iocp));
140 }
141
grpc_iocp_add_socket(grpc_winsocket * socket)142 void grpc_iocp_add_socket(grpc_winsocket* socket) {
143 HANDLE ret;
144 if (socket->added_to_iocp) return;
145 ret = CreateIoCompletionPort((HANDLE)socket->socket, g_iocp,
146 (uintptr_t)socket, 0);
147 if (!ret) {
148 char* utf8_message = gpr_format_message(WSAGetLastError());
149 gpr_log(GPR_ERROR, "Unable to add socket to iocp: %s", utf8_message);
150 gpr_free(utf8_message);
151 __debugbreak();
152 abort();
153 }
154 socket->added_to_iocp = 1;
155 GPR_ASSERT(ret == g_iocp);
156 }
157
158 #endif /* GRPC_WINSOCK_SOCKET */
159