Debian Codegraph @

src/core/lib/iomgr/iocp_windows.cc

raw
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/debug/stats_data.h"
35 #include "src/core/lib/gprpp/thd.h"
36 #include "src/core/lib/iomgr/iocp_windows.h"
37 #include "src/core/lib/iomgr/iomgr_internal.h"
38 #include "src/core/lib/iomgr/socket_windows.h"
39 #include "src/core/lib/iomgr/timer.h"
40
41 static ULONG g_iocp_kick_token;
42 static OVERLAPPED g_iocp_custom_overlap;
43
44 static gpr_atm g_custom_events = 0;
45
46 static HANDLE g_iocp;
47
48 static DWORD deadline_to_millis_timeout(grpc_core::Timestamp deadline) {
49 if (deadline == grpc_core::Timestamp::InfFuture()) {
50 return INFINITE;
51 }
52 grpc_core::Timestamp now = grpc_core::Timestamp::Now();
53 if (deadline < now) return 0;
54 grpc_core::Duration timeout = deadline - now;
55 if (timeout.millis() > std::numeric_limits<DWORD>::max()) return INFINITE;
56 return static_cast<DWORD>(timeout.millis());
57 }
58
59 grpc_iocp_work_status grpc_iocp_work(grpc_core::Timestamp deadline) {
60 BOOL success;
61 DWORD bytes = 0;
62 DWORD flags = 0;
63 ULONG_PTR completion_key;
64 LPOVERLAPPED overlapped;
65 grpc_winsocket* socket;
66 grpc_winsocket_callback_info* info;
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
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
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
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_core::Timestamp::InfPast());
128 } while (work_status == GRPC_IOCP_WORK_KICK ||
129 grpc_core::ExecCtx::Get()->Flush());
130 }
131
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_core::Timestamp::InfFuture());
136 grpc_core::ExecCtx::Get()->Flush();
137 }
138
139 GPR_ASSERT(CloseHandle(g_iocp));
140 }
141
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 */