1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
/*
*
* Copyright 2014, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <grpc/support/port_platform.h>
#ifdef GPR_WINSOCK_SOCKET
#include <winsock2.h>
#include <grpc/support/log.h>
#include <grpc/support/log_win32.h>
#include <grpc/support/alloc.h>
#include <grpc/support/thd.h>
#include "src/core/iomgr/alarm_internal.h"
#include "src/core/iomgr/socket_windows.h"
#include "src/core/iomgr/iomgr_internal.h"
#include "src/core/iomgr/pollset_windows.h"
static grpc_pollset g_global_pollset;
static ULONG g_pollset_kick_token;
static OVERLAPPED g_pollset_custom_overlap;
static gpr_event g_shutdown_global_poller;
static gpr_event g_global_poller_done;
void grpc_pollset_init(grpc_pollset *pollset) {
gpr_mu_init(&pollset->mu);
gpr_cv_init(&pollset->cv);
pollset->iocp = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL,
(ULONG_PTR)NULL, 0);
GPR_ASSERT(pollset->iocp);
}
void grpc_pollset_destroy(grpc_pollset *pollset) {
BOOL status;
gpr_mu_destroy(&pollset->mu);
gpr_cv_destroy(&pollset->cv);
status = CloseHandle(pollset->iocp);
GPR_ASSERT(status);
}
static int pollset_poll(grpc_pollset *pollset,
gpr_timespec deadline, gpr_timespec now) {
BOOL success;
DWORD bytes = 0;
DWORD flags = 0;
ULONG_PTR completion_key;
LPOVERLAPPED overlapped;
gpr_timespec wait_time = gpr_time_sub(deadline, now);
grpc_winsocket *socket;
grpc_winsocket_callback_info *info;
void(*f)(void *, int) = NULL;
void *opaque = NULL;
gpr_mu_unlock(&pollset->mu);
success = GetQueuedCompletionStatus(pollset->iocp, &bytes,
&completion_key, &overlapped,
gpr_time_to_millis(wait_time));
gpr_mu_lock(&pollset->mu);
if (!success && !overlapped) {
/* The deadline got attained. */
return 0;
}
GPR_ASSERT(completion_key && overlapped);
if (overlapped == &g_pollset_custom_overlap) {
if (completion_key == (ULONG_PTR) &g_pollset_kick_token) {
/* We were awoken from a kick. */
gpr_log(GPR_DEBUG, "pollset_poll - got a kick");
return 1;
}
gpr_log(GPR_ERROR, "Unknown custom completion key.");
abort();
}
GPR_ASSERT(pollset == &g_global_pollset);
socket = (grpc_winsocket*) completion_key;
if (overlapped == &socket->write_info.overlapped) {
gpr_log(GPR_DEBUG, "pollset_poll - got write packet");
info = &socket->write_info;
} else if (overlapped == &socket->read_info.overlapped) {
gpr_log(GPR_DEBUG, "pollset_poll - got read packet");
info = &socket->read_info;
} else {
gpr_log(GPR_ERROR, "Unknown IOCP operation");
abort();
}
success = WSAGetOverlappedResult(socket->socket, &info->overlapped, &bytes,
FALSE, &flags);
gpr_log(GPR_DEBUG, "bytes: %u, flags: %u - op %s", bytes, flags,
success ? "succeeded" : "failed");
info->bytes_transfered = bytes;
info->wsa_error = success ? 0 : WSAGetLastError();
GPR_ASSERT(overlapped == &info->overlapped);
gpr_mu_lock(&socket->state_mu);
GPR_ASSERT(!info->has_pending_iocp);
if (info->cb) {
f = info->cb;
opaque = info->opaque;
info->cb = NULL;
} else {
info->has_pending_iocp = 1;
}
gpr_mu_unlock(&socket->state_mu);
if (f) f(opaque, 1);
return 1;
}
int grpc_pollset_work(grpc_pollset *pollset, gpr_timespec deadline) {
gpr_timespec now;
now = gpr_now();
if (gpr_time_cmp(now, deadline) > 0) {
return 0;
}
if (grpc_maybe_call_delayed_callbacks(NULL, 1)) {
return 1;
}
if (grpc_alarm_check(NULL, now, &deadline)) {
return 1;
}
return pollset_poll(pollset, deadline, now);
}
void grpc_pollset_kick(grpc_pollset *pollset) {
BOOL status;
status = PostQueuedCompletionStatus(pollset->iocp, 0,
(ULONG_PTR) &g_pollset_kick_token,
&g_pollset_custom_overlap);
GPR_ASSERT(status);
}
static void global_poller(void *p) {
while (!gpr_event_get(&g_shutdown_global_poller)) {
gpr_mu_lock(&g_global_pollset.mu);
grpc_pollset_work(&g_global_pollset, gpr_inf_future);
gpr_mu_unlock(&g_global_pollset.mu);
}
gpr_event_set(&g_global_poller_done, (void *) 1);
}
void grpc_pollset_global_init(void) {
gpr_thd_id id;
grpc_pollset_init(&g_global_pollset);
gpr_event_init(&g_global_poller_done);
gpr_event_init(&g_shutdown_global_poller);
gpr_thd_new(&id, global_poller, NULL, NULL);
}
void grpc_pollset_global_shutdown(void) {
gpr_event_set(&g_shutdown_global_poller, (void *) 1);
grpc_pollset_kick(&g_global_pollset);
gpr_event_wait(&g_global_poller_done, gpr_inf_future);
grpc_pollset_destroy(&g_global_pollset);
}
void grpc_pollset_add_handle(grpc_pollset *pollset, grpc_winsocket *socket) {
HANDLE ret;
if (socket->added_to_iocp) return;
ret = CreateIoCompletionPort((HANDLE)socket->socket,
g_global_pollset.iocp,
(gpr_uintptr) socket, 0);
if (!ret) {
char *utf8_message = gpr_format_message(WSAGetLastError());
gpr_log(GPR_ERROR, "Unable to add socket to iocp: %s", utf8_message);
gpr_free(utf8_message);
__debugbreak();
abort();
}
socket->added_to_iocp = 1;
GPR_ASSERT(ret == g_global_pollset.iocp);
}
static void handle_notify_on_iocp(grpc_winsocket *socket,
void(*cb)(void *, int), void *opaque,
grpc_winsocket_callback_info *info) {
int run_now = 0;
GPR_ASSERT(!info->cb);
gpr_mu_lock(&socket->state_mu);
if (info->has_pending_iocp) {
run_now = 1;
info->has_pending_iocp = 0;
gpr_log(GPR_DEBUG, "handle_notify_on_iocp - runs now");
} else {
info->cb = cb;
info->opaque = opaque;
gpr_log(GPR_DEBUG, "handle_notify_on_iocp - queued");
}
gpr_mu_unlock(&socket->state_mu);
if (run_now) cb(opaque, 1);
}
void grpc_handle_notify_on_write(grpc_winsocket *socket,
void(*cb)(void *, int), void *opaque) {
gpr_log(GPR_DEBUG, "grpc_handle_notify_on_write");
handle_notify_on_iocp(socket, cb, opaque, &socket->write_info);
}
void grpc_handle_notify_on_read(grpc_winsocket *socket,
void(*cb)(void *, int), void *opaque) {
gpr_log(GPR_DEBUG, "grpc_handle_notify_on_read");
handle_notify_on_iocp(socket, cb, opaque, &socket->read_info);
}
grpc_pollset *grpc_global_pollset(void) {
return &g_global_pollset;
}
#endif /* GPR_WINSOCK_SOCKET */
|