Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Side by Side Diff: third_party/grpc/src/core/iomgr/iocp_windows.c

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/grpc/src/core/iomgr/iocp_windows.h ('k') | third_party/grpc/src/core/iomgr/iomgr.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-2016, Google Inc.
4 * 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/timer.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
50 static ULONG g_iocp_kick_token;
51 static OVERLAPPED g_iocp_custom_overlap;
52
53 static gpr_atm g_custom_events = 0;
54
55 static HANDLE g_iocp;
56
57 static DWORD deadline_to_millis_timeout(gpr_timespec deadline,
58 gpr_timespec now) {
59 gpr_timespec timeout;
60 static const int64_t max_spin_polling_us = 10;
61 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(
65 max_spin_polling_us,
66 GPR_TIMESPAN))) <= 0) {
67 return 0;
68 }
69 timeout = gpr_time_sub(deadline, now);
70 return (DWORD)gpr_time_to_millis(gpr_time_add(
71 timeout, gpr_time_from_nanos(GPR_NS_PER_MS - 1, GPR_TIMESPAN)));
72 }
73
74 grpc_iocp_work_status grpc_iocp_work(grpc_exec_ctx *exec_ctx,
75 gpr_timespec deadline) {
76 BOOL success;
77 DWORD bytes = 0;
78 DWORD flags = 0;
79 ULONG_PTR completion_key;
80 LPOVERLAPPED overlapped;
81 grpc_winsocket *socket;
82 grpc_winsocket_callback_info *info;
83 grpc_closure *closure = NULL;
84 success = GetQueuedCompletionStatus(
85 g_iocp, &bytes, &completion_key, &overlapped,
86 deadline_to_millis_timeout(deadline, gpr_now(deadline.clock_type)));
87 if (success == 0 && overlapped == NULL) {
88 return GRPC_IOCP_WORK_TIMEOUT;
89 }
90 GPR_ASSERT(completion_key && overlapped);
91 if (overlapped == &g_iocp_custom_overlap) {
92 gpr_atm_full_fetch_add(&g_custom_events, -1);
93 if (completion_key == (ULONG_PTR)&g_iocp_kick_token) {
94 /* We were awoken from a kick. */
95 return GRPC_IOCP_WORK_KICK;
96 }
97 gpr_log(GPR_ERROR, "Unknown custom completion key.");
98 abort();
99 }
100
101 socket = (grpc_winsocket *)completion_key;
102 if (overlapped == &socket->write_info.overlapped) {
103 info = &socket->write_info;
104 } else if (overlapped == &socket->read_info.overlapped) {
105 info = &socket->read_info;
106 } else {
107 gpr_log(GPR_ERROR, "Unknown IOCP operation");
108 abort();
109 }
110 success = WSAGetOverlappedResult(socket->socket, &info->overlapped, &bytes,
111 FALSE, &flags);
112 info->bytes_transfered = bytes;
113 info->wsa_error = success ? 0 : WSAGetLastError();
114 GPR_ASSERT(overlapped == &info->overlapped);
115 GPR_ASSERT(!info->has_pending_iocp);
116 gpr_mu_lock(&socket->state_mu);
117 if (info->closure) {
118 closure = info->closure;
119 info->closure = NULL;
120 } else {
121 info->has_pending_iocp = 1;
122 }
123 gpr_mu_unlock(&socket->state_mu);
124 grpc_exec_ctx_enqueue(exec_ctx, closure, true, NULL);
125 return GRPC_IOCP_WORK_WORK;
126 }
127
128 void grpc_iocp_init(void) {
129 g_iocp =
130 CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, (ULONG_PTR)NULL, 0);
131 GPR_ASSERT(g_iocp);
132 }
133
134 void grpc_iocp_kick(void) {
135 BOOL success;
136
137 gpr_atm_full_fetch_add(&g_custom_events, 1);
138 success = PostQueuedCompletionStatus(g_iocp, 0, (ULONG_PTR)&g_iocp_kick_token,
139 &g_iocp_custom_overlap);
140 GPR_ASSERT(success);
141 }
142
143 void grpc_iocp_flush(void) {
144 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
145 grpc_iocp_work_status work_status;
146
147 do {
148 work_status = grpc_iocp_work(&exec_ctx, gpr_inf_past(GPR_CLOCK_MONOTONIC));
149 } while (work_status == GRPC_IOCP_WORK_KICK ||
150 grpc_exec_ctx_flush(&exec_ctx));
151 }
152
153 void grpc_iocp_shutdown(void) {
154 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
155 while (gpr_atm_acq_load(&g_custom_events)) {
156 grpc_iocp_work(&exec_ctx, gpr_inf_future(GPR_CLOCK_MONOTONIC));
157 grpc_exec_ctx_flush(&exec_ctx);
158 }
159 grpc_exec_ctx_finish(&exec_ctx);
160 GPR_ASSERT(CloseHandle(g_iocp));
161 }
162
163 void grpc_iocp_add_socket(grpc_winsocket *socket) {
164 HANDLE ret;
165 if (socket->added_to_iocp) return;
166 ret = CreateIoCompletionPort((HANDLE)socket->socket, g_iocp,
167 (uintptr_t)socket, 0);
168 if (!ret) {
169 char *utf8_message = gpr_format_message(WSAGetLastError());
170 gpr_log(GPR_ERROR, "Unable to add socket to iocp: %s", utf8_message);
171 gpr_free(utf8_message);
172 __debugbreak();
173 abort();
174 }
175 socket->added_to_iocp = 1;
176 GPR_ASSERT(ret == g_iocp);
177 }
178
179 /* Calling notify_on_read or write means either of two things:
180 -) The IOCP already completed in the background, and we need to call
181 the callback now.
182 -) The IOCP hasn't completed yet, and we're queuing it for later. */
183 static void socket_notify_on_iocp(grpc_exec_ctx *exec_ctx,
184 grpc_winsocket *socket, grpc_closure *closure,
185 grpc_winsocket_callback_info *info) {
186 GPR_ASSERT(info->closure == NULL);
187 gpr_mu_lock(&socket->state_mu);
188 if (info->has_pending_iocp) {
189 info->has_pending_iocp = 0;
190 grpc_exec_ctx_enqueue(exec_ctx, closure, true, NULL);
191 } else {
192 info->closure = closure;
193 }
194 gpr_mu_unlock(&socket->state_mu);
195 }
196
197 void grpc_socket_notify_on_write(grpc_exec_ctx *exec_ctx,
198 grpc_winsocket *socket,
199 grpc_closure *closure) {
200 socket_notify_on_iocp(exec_ctx, socket, closure, &socket->write_info);
201 }
202
203 void grpc_socket_notify_on_read(grpc_exec_ctx *exec_ctx, grpc_winsocket *socket,
204 grpc_closure *closure) {
205 socket_notify_on_iocp(exec_ctx, socket, closure, &socket->read_info);
206 }
207
208 #endif /* GPR_WINSOCK_SOCKET */
OLDNEW
« no previous file with comments | « third_party/grpc/src/core/iomgr/iocp_windows.h ('k') | third_party/grpc/src/core/iomgr/iomgr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698