Windows: implement uv_poll
This commit is contained in:
parent
e1154d70ce
commit
d7a71761c4
@ -146,6 +146,8 @@ typedef struct _AFD_POLL_INFO {
|
||||
AFD_POLL_HANDLE_INFO Handles[1];
|
||||
} AFD_POLL_INFO, *PAFD_POLL_INFO;
|
||||
|
||||
#define UV_MSAFD_PROVIDER_COUNT 3
|
||||
|
||||
|
||||
/**
|
||||
* It should be possible to cast uv_buf_t[] to WSABUF[]
|
||||
@ -158,6 +160,8 @@ typedef struct uv_buf_t {
|
||||
|
||||
typedef int uv_file;
|
||||
|
||||
typedef SOCKET uv_platform_socket_t;
|
||||
|
||||
typedef HANDLE uv_thread_t;
|
||||
|
||||
typedef CRITICAL_SECTION uv_mutex_t;
|
||||
@ -219,6 +223,9 @@ RB_HEAD(uv_timer_tree_s, uv_timer_s);
|
||||
uv_prepare_t* next_prepare_handle; \
|
||||
uv_check_t* next_check_handle; \
|
||||
uv_idle_t* next_idle_handle; \
|
||||
/* This handle holds the peer sockets for the fast variant of uv_poll_t */ \
|
||||
SOCKET poll_peer_sockets[UV_MSAFD_PROVIDER_COUNT]; \
|
||||
/* State used by uv_ares. */ \
|
||||
ares_channel ares_chan; \
|
||||
int ares_active_sockets; \
|
||||
uv_timer_t ares_polling_timer; \
|
||||
@ -237,6 +244,7 @@ RB_HEAD(uv_timer_tree_s, uv_timer_s);
|
||||
UV_ARES_CLEANUP_REQ, \
|
||||
UV_FS_EVENT_REQ, \
|
||||
UV_GETADDRINFO_REQ, \
|
||||
UV_POLL_REQ, \
|
||||
UV_PROCESS_EXIT, \
|
||||
UV_PROCESS_CLOSE, \
|
||||
UV_READ, \
|
||||
@ -386,6 +394,21 @@ RB_HEAD(uv_timer_tree_s, uv_timer_s);
|
||||
COORD saved_position; \
|
||||
WORD saved_attributes;
|
||||
|
||||
#define UV_POLL_PRIVATE_FIELDS \
|
||||
SOCKET socket; \
|
||||
/* Used in fast mode */ \
|
||||
SOCKET peer_socket; \
|
||||
AFD_POLL_INFO afd_poll_info_1; \
|
||||
AFD_POLL_INFO afd_poll_info_2; \
|
||||
/* Used in fast and slow mode. */ \
|
||||
uv_req_t poll_req_1; \
|
||||
uv_req_t poll_req_2; \
|
||||
unsigned char submitted_events_1; \
|
||||
unsigned char submitted_events_2; \
|
||||
unsigned char mask_events_1; \
|
||||
unsigned char mask_events_2; \
|
||||
unsigned char events;
|
||||
|
||||
#define UV_TIMER_PRIVATE_FIELDS \
|
||||
RB_ENTRY(uv_timer_s) tree_entry; \
|
||||
int64_t due; \
|
||||
|
||||
@ -84,6 +84,8 @@ static void uv_loop_init(uv_loop_t* loop) {
|
||||
loop->next_check_handle = NULL;
|
||||
loop->next_idle_handle = NULL;
|
||||
|
||||
memset(&loop->poll_peer_sockets, 0, sizeof loop->poll_peer_sockets);
|
||||
|
||||
loop->ares_active_sockets = 0;
|
||||
loop->ares_chan = NULL;
|
||||
|
||||
@ -130,6 +132,14 @@ uv_loop_t* uv_loop_new(void) {
|
||||
|
||||
void uv_loop_delete(uv_loop_t* loop) {
|
||||
if (loop != &uv_default_loop_) {
|
||||
int i;
|
||||
for (i = 0; i < ARRAY_SIZE(loop->poll_peer_sockets); i++) {
|
||||
SOCKET sock = loop->poll_peer_sockets[i];
|
||||
if (sock != 0 && sock != INVALID_SOCKET) {
|
||||
closesocket(sock);
|
||||
}
|
||||
}
|
||||
|
||||
free(loop);
|
||||
}
|
||||
}
|
||||
|
||||
@ -64,6 +64,9 @@ int uv_is_active(const uv_handle_t* handle) {
|
||||
case UV_CHECK:
|
||||
return (handle->flags & UV_HANDLE_ACTIVE) ? 1 : 0;
|
||||
|
||||
case UV_POLL:
|
||||
return ((uv_poll_t*) handle)->events != 0;
|
||||
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
@ -112,6 +115,10 @@ void uv_close(uv_handle_t* handle, uv_close_cb cb) {
|
||||
}
|
||||
return;
|
||||
|
||||
case UV_POLL:
|
||||
uv_poll_close(handle->loop, (uv_poll_t*) handle);
|
||||
return;
|
||||
|
||||
case UV_TIMER:
|
||||
uv_timer_stop((uv_timer_t*)handle);
|
||||
uv_want_endgame(loop, handle);
|
||||
@ -195,6 +202,10 @@ void uv_process_endgames(uv_loop_t* loop) {
|
||||
uv_udp_endgame(loop, (uv_udp_t*) handle);
|
||||
break;
|
||||
|
||||
case UV_POLL:
|
||||
uv_poll_endgame(loop, (uv_poll_t*) handle);
|
||||
break;
|
||||
|
||||
case UV_TIMER:
|
||||
uv_timer_endgame(loop, (uv_timer_t*) handle);
|
||||
break;
|
||||
|
||||
@ -84,6 +84,9 @@ void uv_process_timers(uv_loop_t* loop);
|
||||
#define UV_HANDLE_TTY_SAVED_POSITION 0x02000000
|
||||
#define UV_HANDLE_TTY_SAVED_ATTRIBUTES 0x04000000
|
||||
|
||||
/* Only used by uv_poll_t handles. */
|
||||
#define UV_HANDLE_POLL_SLOW 0x02000000
|
||||
|
||||
|
||||
void uv_want_endgame(uv_loop_t* loop, uv_handle_t* handle);
|
||||
void uv_process_endgames(uv_loop_t* loop);
|
||||
@ -230,6 +233,16 @@ void uv_process_tty_connect_req(uv_loop_t* loop, uv_tty_t* handle,
|
||||
void uv_tty_endgame(uv_loop_t* loop, uv_tty_t* handle);
|
||||
|
||||
|
||||
/*
|
||||
* Poll watchers
|
||||
*/
|
||||
void uv_process_poll_req(uv_loop_t* loop, uv_poll_t* handle,
|
||||
uv_req_t* req);
|
||||
|
||||
void uv_poll_close(uv_loop_t* loop, uv_poll_t* handle);
|
||||
void uv_poll_endgame(uv_loop_t* loop, uv_poll_t* handle);
|
||||
|
||||
|
||||
/*
|
||||
* Loop watchers
|
||||
*/
|
||||
|
||||
609
src/win/poll.c
Normal file
609
src/win/poll.c
Normal file
@ -0,0 +1,609 @@
|
||||
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
#include <assert.h>
|
||||
#include <io.h>
|
||||
|
||||
#include "uv.h"
|
||||
#include "../uv-common.h"
|
||||
#include "internal.h"
|
||||
|
||||
|
||||
static const GUID uv_msafd_provider_ids[UV_MSAFD_PROVIDER_COUNT] = {
|
||||
{0xe70f1aa0, 0xab8b, 0x11cf,
|
||||
{0x8c, 0xa3, 0x00, 0x80, 0x5f, 0x48, 0xa1, 0x92}},
|
||||
{0xf9eab0c0, 0x26d4, 0x11d0,
|
||||
{0xbb, 0xbf, 0x00, 0xaa, 0x00, 0x6c, 0x34, 0xe4}},
|
||||
{0x9fc48064, 0x7298, 0x43e4,
|
||||
{0xb7, 0xbd, 0x18, 0x1f, 0x20, 0x89, 0x79, 0x2a}}
|
||||
};
|
||||
|
||||
typedef struct uv_single_fd_set_s {
|
||||
unsigned int fd_count;
|
||||
SOCKET fd_array[1];
|
||||
} uv_single_fd_set_t;
|
||||
|
||||
|
||||
static void uv__fast_poll_submit_poll_req(uv_loop_t* loop, uv_poll_t* handle) {
|
||||
uv_req_t* req;
|
||||
AFD_POLL_INFO* afd_poll_info;
|
||||
DWORD result;
|
||||
|
||||
/* Find a yet unsubmitted req to submit. */
|
||||
if (handle->submitted_events_1 == 0) {
|
||||
req = &handle->poll_req_1;
|
||||
afd_poll_info = &handle->afd_poll_info_1;
|
||||
handle->submitted_events_1 = handle->events;
|
||||
handle->mask_events_1 = 0;
|
||||
handle->mask_events_2 = handle->events;
|
||||
} else if (handle->submitted_events_2 == 0) {
|
||||
req = &handle->poll_req_2;
|
||||
afd_poll_info = &handle->afd_poll_info_2;
|
||||
handle->submitted_events_2 = handle->events;
|
||||
handle->mask_events_1 = handle->events;
|
||||
handle->mask_events_2 = 0;
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
|
||||
/* Setting Exclusive to TRUE makes the other poll request return if there */
|
||||
/* is any. */
|
||||
afd_poll_info->Exclusive = TRUE;
|
||||
afd_poll_info->NumberOfHandles = 1;
|
||||
afd_poll_info->Timeout.QuadPart = INT64_MAX;
|
||||
afd_poll_info->Handles[0].Handle = (HANDLE) handle->socket;
|
||||
afd_poll_info->Handles[0].Status = 0;
|
||||
afd_poll_info->Handles[0].Events = 0;
|
||||
|
||||
if (handle->events & UV_READABLE) {
|
||||
afd_poll_info->Handles[0].Events |= AFD_POLL_RECEIVE |
|
||||
AFD_POLL_DISCONNECT | AFD_POLL_ACCEPT | AFD_POLL_ABORT;
|
||||
}
|
||||
if (handle->events & UV_WRITABLE) {
|
||||
afd_poll_info->Handles[0].Events |= AFD_POLL_SEND | AFD_POLL_CONNECT_FAIL;
|
||||
}
|
||||
|
||||
memset(&req->overlapped, 0, sizeof req->overlapped);
|
||||
|
||||
result = uv_msafd_poll((SOCKET) handle->peer_socket,
|
||||
afd_poll_info,
|
||||
&req->overlapped);
|
||||
if (result != 0 && WSAGetLastError() != WSA_IO_PENDING) {
|
||||
/* Queue this req, reporting an error. */
|
||||
SET_REQ_ERROR(req, WSAGetLastError());
|
||||
uv_insert_pending_req(loop, req);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int uv__fast_poll_cancel_poll_reqs(uv_loop_t* loop, uv_poll_t* handle) {
|
||||
AFD_POLL_INFO afd_poll_info;
|
||||
DWORD result;
|
||||
HANDLE event;
|
||||
OVERLAPPED overlapped;
|
||||
|
||||
event = CreateEvent(NULL, TRUE, FALSE, NULL);
|
||||
if (event == NULL) {
|
||||
uv__set_sys_error(loop, GetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
afd_poll_info.Exclusive = TRUE;
|
||||
afd_poll_info.NumberOfHandles = 1;
|
||||
afd_poll_info.Timeout.QuadPart = INT64_MAX;
|
||||
afd_poll_info.Handles[0].Handle = (HANDLE) handle->socket;
|
||||
afd_poll_info.Handles[0].Status = 0;
|
||||
afd_poll_info.Handles[0].Events = AFD_POLL_ALL;
|
||||
|
||||
memset(&overlapped, 0, sizeof overlapped);
|
||||
overlapped.hEvent = (HANDLE) ((uintptr_t) event & 1);
|
||||
|
||||
result = uv_msafd_poll(handle->socket,
|
||||
&afd_poll_info,
|
||||
&overlapped);
|
||||
|
||||
if (result == SOCKET_ERROR) {
|
||||
DWORD error = WSAGetLastError();
|
||||
if (error != WSA_IO_PENDING) {
|
||||
uv__set_sys_error(loop, WSAGetLastError());
|
||||
CloseHandle(event);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
CloseHandle(event);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void uv__fast_poll_process_poll_req(uv_loop_t* loop, uv_poll_t* handle,
|
||||
uv_req_t* req) {
|
||||
unsigned char mask_events;
|
||||
AFD_POLL_INFO* afd_poll_info;
|
||||
|
||||
if (req == &handle->poll_req_1) {
|
||||
afd_poll_info = &handle->afd_poll_info_1;
|
||||
handle->submitted_events_1 = 0;
|
||||
mask_events = handle->mask_events_1;
|
||||
} else if (req == &handle->poll_req_2) {
|
||||
afd_poll_info = &handle->afd_poll_info_2;
|
||||
handle->submitted_events_2 = 0;
|
||||
mask_events = handle->mask_events_2;
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
|
||||
/* Report an error unless the select was just interrupted. */
|
||||
if (!REQ_SUCCESS(req)) {
|
||||
DWORD error = GET_REQ_SOCK_ERROR(req);
|
||||
if (error != WSAEINTR && handle->events != 0) {
|
||||
handle->events = 0; /* Stop the watcher */
|
||||
uv__set_sys_error(loop, error);
|
||||
handle->poll_cb(handle, -1, 0);
|
||||
}
|
||||
|
||||
} else if (afd_poll_info->NumberOfHandles >= 1) {
|
||||
unsigned char events = 0;
|
||||
|
||||
if ((afd_poll_info->Handles[0].Events & (AFD_POLL_RECEIVE |
|
||||
AFD_POLL_DISCONNECT | AFD_POLL_ACCEPT | AFD_POLL_ABORT)) != 0) {
|
||||
events |= UV_READABLE;
|
||||
}
|
||||
if ((afd_poll_info->Handles[0].Events & (AFD_POLL_SEND |
|
||||
AFD_POLL_CONNECT_FAIL)) != 0) {
|
||||
events |= UV_WRITABLE;
|
||||
}
|
||||
|
||||
events &= handle->events & ~mask_events;
|
||||
|
||||
if (afd_poll_info->Handles[0].Events & AFD_POLL_LOCAL_CLOSE) {
|
||||
/* Stop polling. */
|
||||
handle->events = 0;
|
||||
}
|
||||
|
||||
if (events != 0) {
|
||||
handle->poll_cb(handle, 0, events);
|
||||
}
|
||||
}
|
||||
|
||||
if ((handle->events & ~(handle->submitted_events_1 |
|
||||
handle->submitted_events_2)) != 0) {
|
||||
uv__fast_poll_submit_poll_req(loop, handle);
|
||||
} else if ((handle->flags & UV_HANDLE_CLOSING) &&
|
||||
handle->submitted_events_1 == 0 &&
|
||||
handle->submitted_events_2 == 0) {
|
||||
uv_want_endgame(loop, (uv_handle_t*) handle);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int uv__fast_poll_set(uv_loop_t* loop, uv_poll_t* handle, int events) {
|
||||
assert(handle->type == UV_POLL);
|
||||
assert(!(handle->flags & UV_HANDLE_CLOSING));
|
||||
assert((events & ~(UV_READABLE | UV_WRITABLE)) == 0);
|
||||
|
||||
handle->events = events;
|
||||
if ((handle->events & ~(handle->submitted_events_1 |
|
||||
handle->submitted_events_2)) != 0) {
|
||||
uv__fast_poll_submit_poll_req(handle->loop, handle);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void uv__fast_poll_close(uv_loop_t* loop, uv_poll_t* handle) {
|
||||
handle->events = 0;
|
||||
|
||||
if (handle->submitted_events_1 == 0 &&
|
||||
handle->submitted_events_2 == 0) {
|
||||
uv_want_endgame(loop, (uv_handle_t*) handle);
|
||||
} else {
|
||||
/* Try to cancel outstanding poll requests. */
|
||||
if (pCancelIoEx) {
|
||||
/* Use CancelIoEx to cancel poll requests if available. */
|
||||
if (handle->submitted_events_1)
|
||||
pCancelIoEx((HANDLE) handle->socket, &handle->poll_req_1.overlapped);
|
||||
if (handle->submitted_events_2)
|
||||
pCancelIoEx((HANDLE) handle->socket, &handle->poll_req_2.overlapped);
|
||||
} else if (handle->submitted_events_1 | handle->submitted_events_2) {
|
||||
/* Execute another unique poll to force the others to return. */
|
||||
uv__fast_poll_cancel_poll_reqs(loop, handle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static SOCKET uv__fast_poll_create_peer_socket(HANDLE iocp,
|
||||
WSAPROTOCOL_INFOW* protocol_info) {
|
||||
SOCKET sock = 0;
|
||||
|
||||
sock = WSASocketW(protocol_info->iAddressFamily,
|
||||
protocol_info->iSocketType,
|
||||
protocol_info->iProtocol,
|
||||
protocol_info,
|
||||
0,
|
||||
WSA_FLAG_OVERLAPPED);
|
||||
if (sock == INVALID_SOCKET) {
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
if (!SetHandleInformation((HANDLE) sock, HANDLE_FLAG_INHERIT, 0)) {
|
||||
goto error;
|
||||
};
|
||||
|
||||
if (CreateIoCompletionPort((HANDLE) sock,
|
||||
iocp,
|
||||
(ULONG_PTR) sock,
|
||||
0) == NULL) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
return sock;
|
||||
|
||||
error:
|
||||
closesocket(sock);
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
|
||||
static SOCKET uv__fast_poll_get_peer_socket(uv_loop_t* loop,
|
||||
WSAPROTOCOL_INFOW* protocol_info) {
|
||||
int index, i;
|
||||
SOCKET peer_socket;
|
||||
|
||||
index = -1;
|
||||
for (i = 0; i < ARRAY_SIZE(uv_msafd_provider_ids); i++) {
|
||||
if (memcmp((void*) &protocol_info->ProviderId,
|
||||
(void*) &uv_msafd_provider_ids[i],
|
||||
sizeof protocol_info->ProviderId) == 0) {
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check if the protocol uses an msafd socket. */
|
||||
if (index < 0) {
|
||||
return INVALID_SOCKET;
|
||||
}
|
||||
|
||||
/* If we didn't (try) to create a peer socket yet, try to make one. Don't */
|
||||
/* try again if the peer socket creation failed earlier for the same */
|
||||
/* protocol. */
|
||||
peer_socket = loop->poll_peer_sockets[index];
|
||||
if (peer_socket == 0) {
|
||||
peer_socket = uv__fast_poll_create_peer_socket(loop->iocp, protocol_info);
|
||||
loop->poll_peer_sockets[index] = peer_socket;
|
||||
}
|
||||
|
||||
return peer_socket;
|
||||
}
|
||||
|
||||
|
||||
static DWORD WINAPI uv__slow_poll_thread_proc(void* arg) {
|
||||
uv_req_t* req = (uv_req_t*) arg;
|
||||
uv_poll_t* handle = (uv_poll_t*) req->data;
|
||||
unsigned char events, reported_events;
|
||||
int r;
|
||||
uv_single_fd_set_t rfds, wfds, efds;
|
||||
struct timeval timeout;
|
||||
|
||||
assert(handle->type == UV_POLL);
|
||||
assert(req->type == UV_POLL_REQ);
|
||||
|
||||
if (req == &handle->poll_req_1) {
|
||||
events = handle->submitted_events_1;
|
||||
} else if (req == &handle->poll_req_2) {
|
||||
events = handle->submitted_events_2;
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if (handle->events & UV_READABLE) {
|
||||
rfds.fd_count = 1;
|
||||
rfds.fd_array[0] = handle->socket;
|
||||
} else {
|
||||
rfds.fd_count = 0;
|
||||
}
|
||||
|
||||
if (handle->events & UV_WRITABLE) {
|
||||
wfds.fd_count = 1;
|
||||
wfds.fd_array[0] = handle->socket;
|
||||
efds.fd_count = 1;
|
||||
efds.fd_array[0] = handle->socket;
|
||||
} else {
|
||||
wfds.fd_count = 0;
|
||||
efds.fd_count = 0;
|
||||
}
|
||||
|
||||
/* Make the select() time out after 3 minutes. If select() hangs because */
|
||||
/* the user closed the socket, we will at least not hang indefinitely. */
|
||||
timeout.tv_sec = 3 * 60;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
r = select(1, (fd_set*) &rfds, (fd_set*) &wfds, (fd_set*) &efds, &timeout);
|
||||
if (r == SOCKET_ERROR) {
|
||||
/* Queue this req, reporting an error. */
|
||||
SET_REQ_ERROR(&handle->poll_req_1, WSAGetLastError());
|
||||
POST_COMPLETION_FOR_REQ(handle->loop, req);
|
||||
return 0;
|
||||
}
|
||||
|
||||
reported_events = 0;
|
||||
|
||||
if (r > 0) {
|
||||
if (rfds.fd_count > 0) {
|
||||
assert(rfds.fd_count == 1);
|
||||
assert(rfds.fd_array[0] == handle->socket);
|
||||
reported_events |= UV_READABLE;
|
||||
}
|
||||
|
||||
if (wfds.fd_count > 0) {
|
||||
assert(wfds.fd_count == 1);
|
||||
assert(wfds.fd_array[0] == handle->socket);
|
||||
reported_events |= UV_WRITABLE;
|
||||
} else if (efds.fd_count > 0) {
|
||||
assert(efds.fd_count == 1);
|
||||
assert(efds.fd_array[0] == handle->socket);
|
||||
reported_events |= UV_WRITABLE;
|
||||
}
|
||||
}
|
||||
|
||||
SET_REQ_SUCCESS(req);
|
||||
req->overlapped.InternalHigh = (DWORD) reported_events;
|
||||
POST_COMPLETION_FOR_REQ(handle->loop, req);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void uv__slow_poll_submit_poll_req(uv_loop_t* loop, uv_poll_t* handle) {
|
||||
uv_req_t* req;
|
||||
|
||||
/* Find a yet unsubmitted req to submit. */
|
||||
if (handle->submitted_events_1 == 0) {
|
||||
req = &handle->poll_req_1;
|
||||
handle->submitted_events_1 = handle->events;
|
||||
handle->mask_events_1 = 0;
|
||||
handle->mask_events_2 = handle->events;
|
||||
} else if (handle->submitted_events_2 == 0) {
|
||||
req = &handle->poll_req_2;
|
||||
handle->submitted_events_2 = handle->events;
|
||||
handle->mask_events_1 = handle->events;
|
||||
handle->mask_events_2 = 0;
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if (!QueueUserWorkItem(uv__slow_poll_thread_proc,
|
||||
(void*) req,
|
||||
WT_EXECUTELONGFUNCTION)) {
|
||||
/* Make this req pending, reporting an error. */
|
||||
SET_REQ_ERROR(req, GetLastError());
|
||||
uv_insert_pending_req(loop, req);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
static void uv__slow_poll_process_poll_req(uv_loop_t* loop, uv_poll_t* handle,
|
||||
uv_req_t* req) {
|
||||
unsigned char mask_events;
|
||||
if (req == &handle->poll_req_1) {
|
||||
handle->submitted_events_1 = 0;
|
||||
mask_events = handle->mask_events_1;
|
||||
} else if (req == &handle->poll_req_2) {
|
||||
handle->submitted_events_2 = 0;
|
||||
mask_events = handle->mask_events_2;
|
||||
} else {
|
||||
assert(0);
|
||||
}
|
||||
|
||||
if (!REQ_SUCCESS(req)) {
|
||||
/* Error. */
|
||||
if (handle->events != 0) {
|
||||
handle->events = 0; /* Stop the watcher */
|
||||
uv__set_sys_error(loop, GET_REQ_ERROR(req));
|
||||
handle->poll_cb(handle, -1, 0);
|
||||
}
|
||||
} else {
|
||||
/* Got some events. */
|
||||
int events = req->overlapped.InternalHigh & handle->events & ~mask_events;
|
||||
if (events != 0) {
|
||||
handle->poll_cb(handle, 0, events);
|
||||
}
|
||||
}
|
||||
|
||||
if ((handle->events & ~(handle->submitted_events_1 |
|
||||
handle->submitted_events_2)) != 0) {
|
||||
uv__slow_poll_submit_poll_req(loop, handle);
|
||||
} else if ((handle->flags & UV_HANDLE_CLOSING) &&
|
||||
handle->submitted_events_1 == 0 &&
|
||||
handle->submitted_events_2 == 0) {
|
||||
uv_want_endgame(loop, (uv_handle_t*) handle);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int uv__slow_poll_set(uv_loop_t* loop, uv_poll_t* handle, int events) {
|
||||
assert(handle->type == UV_POLL);
|
||||
assert(!(handle->flags & UV_HANDLE_CLOSING));
|
||||
assert((events & ~(UV_READABLE | UV_WRITABLE)) == 0);
|
||||
|
||||
handle->events = events;
|
||||
if ((handle->events &
|
||||
~(handle->submitted_events_1 | handle->submitted_events_2)) != 0) {
|
||||
uv__slow_poll_submit_poll_req(handle->loop, handle);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static void uv__slow_poll_close(uv_loop_t* loop, uv_poll_t* handle) {
|
||||
handle->events = 0;
|
||||
|
||||
if (handle->submitted_events_1 == 0 &&
|
||||
handle->submitted_events_2 == 0) {
|
||||
uv_want_endgame(loop, (uv_handle_t*) handle);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd) {
|
||||
return uv_poll_init_socket(loop, handle, (SOCKET) _get_osfhandle(fd));
|
||||
}
|
||||
|
||||
|
||||
int uv_poll_init_socket(uv_loop_t* loop, uv_poll_t* handle,
|
||||
uv_platform_socket_t socket) {
|
||||
WSAPROTOCOL_INFOW protocol_info;
|
||||
int len;
|
||||
SOCKET peer_socket, base_socket;
|
||||
DWORD bytes;
|
||||
|
||||
/* Try to obtain a base handle for the socket. This increases this chances */
|
||||
/* that we find an AFD handle and are able to use the fast poll mechanism. */
|
||||
/* This will always fail on windows XP/2k3, since they don't support the */
|
||||
/* SIO_BASE_HANDLE ioctl. */
|
||||
#ifndef NDEBUG
|
||||
base_socket = INVALID_SOCKET;
|
||||
#endif
|
||||
|
||||
if (WSAIoctl(socket,
|
||||
SIO_BASE_HANDLE,
|
||||
NULL,
|
||||
0,
|
||||
&base_socket,
|
||||
sizeof base_socket,
|
||||
&bytes,
|
||||
NULL,
|
||||
NULL) == 0) {
|
||||
assert(base_socket != 0 && base_socket != INVALID_SOCKET);
|
||||
socket = base_socket;
|
||||
}
|
||||
|
||||
handle->type = UV_POLL;
|
||||
handle->socket = socket;
|
||||
handle->loop = loop;
|
||||
handle->flags = 0;
|
||||
handle->events = 0;
|
||||
|
||||
/* Obtain protocol information about the socket. */
|
||||
len = sizeof protocol_info;
|
||||
if (getsockopt(socket,
|
||||
SOL_SOCKET,
|
||||
SO_PROTOCOL_INFOW,
|
||||
(char*) &protocol_info,
|
||||
&len) != 0) {
|
||||
uv__set_sys_error(loop, WSAGetLastError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Get the peer socket that is needed to enable fast poll. If the returned */
|
||||
/* value is NULL, the protocol is not implemented by MSAFD and we'll have */
|
||||
/* to use slow mode. */
|
||||
peer_socket = uv__fast_poll_get_peer_socket(loop, &protocol_info);
|
||||
|
||||
if (peer_socket != INVALID_SOCKET) {
|
||||
/* Initialize fast poll specific fields. */
|
||||
handle->peer_socket = peer_socket;
|
||||
} else {
|
||||
/* Initialize slow poll specific fields. */
|
||||
handle->flags |= UV_HANDLE_POLL_SLOW;
|
||||
}
|
||||
|
||||
/* Intialize 2 poll reqs. */
|
||||
handle->submitted_events_1 = 0;
|
||||
uv_req_init(loop, (uv_req_t*) &(handle->poll_req_1));
|
||||
handle->poll_req_1.type = UV_POLL_REQ;
|
||||
handle->poll_req_1.data = handle;
|
||||
|
||||
handle->submitted_events_2 = 0;
|
||||
uv_req_init(loop, (uv_req_t*) &(handle->poll_req_2));
|
||||
handle->poll_req_2.type = UV_POLL_REQ;
|
||||
handle->poll_req_2.data = handle;
|
||||
|
||||
uv_ref(loop);
|
||||
|
||||
loop->counters.handle_init++;
|
||||
loop->counters.poll_init++;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int uv_poll_start(uv_poll_t* handle, int events, uv_poll_cb cb) {
|
||||
if (!(handle->flags & UV_HANDLE_POLL_SLOW)) {
|
||||
if (uv__fast_poll_set(handle->loop, handle, events) < 0)
|
||||
return -1;
|
||||
} else {
|
||||
if (uv__slow_poll_set(handle->loop, handle, events) < 0)
|
||||
return -1;
|
||||
}
|
||||
|
||||
handle->poll_cb = cb;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int uv_poll_stop(uv_poll_t* handle) {
|
||||
if (!(handle->flags & UV_HANDLE_POLL_SLOW)) {
|
||||
return uv__fast_poll_set(handle->loop, handle, 0);
|
||||
} else {
|
||||
return uv__slow_poll_set(handle->loop, handle, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void uv_process_poll_req(uv_loop_t* loop, uv_poll_t* handle, uv_req_t* req) {
|
||||
if (!(handle->flags & UV_HANDLE_POLL_SLOW)) {
|
||||
uv__fast_poll_process_poll_req(loop, handle, req);
|
||||
} else {
|
||||
uv__slow_poll_process_poll_req(loop, handle, req);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void uv_poll_close(uv_loop_t* loop, uv_poll_t* handle) {
|
||||
if (!(handle->flags & UV_HANDLE_POLL_SLOW)) {
|
||||
uv__fast_poll_close(loop, handle);
|
||||
} else {
|
||||
uv__slow_poll_close(loop, handle);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void uv_poll_endgame(uv_loop_t* loop, uv_poll_t* handle) {
|
||||
assert(handle->flags & UV_HANDLE_CLOSING);
|
||||
assert(!(handle->flags & UV_HANDLE_CLOSED));
|
||||
|
||||
assert(handle->submitted_events_1 == 0);
|
||||
assert(handle->submitted_events_2 == 0);
|
||||
|
||||
handle->flags |= UV_HANDLE_CLOSED;
|
||||
|
||||
if (handle->close_cb) {
|
||||
handle->close_cb((uv_handle_t*)handle);
|
||||
}
|
||||
|
||||
uv_unref(loop);
|
||||
}
|
||||
@ -135,6 +135,10 @@ void uv_process_reqs(uv_loop_t* loop) {
|
||||
uv_process_async_wakeup_req(loop, (uv_async_t*) req->data, req);
|
||||
break;
|
||||
|
||||
case UV_POLL_REQ:
|
||||
uv_process_poll_req(loop, (uv_poll_t*) req->data, req);
|
||||
break;
|
||||
|
||||
case UV_ARES_EVENT_REQ:
|
||||
uv_process_ares_event_req(loop, (uv_ares_action_t*) req->data, req);
|
||||
break;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user