mirror of
https://github.com/nginx/nginx.git
synced 2024-11-21 16:28:40 +00:00
Win32: added WSAPoll() support.
WSAPoll() is only available with Windows Vista and newer (and only available during compilation if _WIN32_WINNT >= 0x0600). To make sure the code works with Windows XP, we do not redefine _WIN32_WINNT, but instead load WSAPoll() dynamically if it is not available during compilation. Also, sockets are not guaranteed to be small integers on Windows. So an index array is used instead of NGX_USE_FD_EVENT to map events to connections.
This commit is contained in:
parent
c6a0003af1
commit
40b74475d0
@ -11,6 +11,7 @@ CORE_SRCS="$WIN32_SRCS $IOCP_SRCS"
|
||||
OS_CONFIG="$WIN32_CONFIG"
|
||||
NGX_ICONS="$NGX_WIN32_ICONS"
|
||||
SELECT_SRCS=$WIN32_SELECT_SRCS
|
||||
POLL_SRCS=$WIN32_POLL_SRCS
|
||||
|
||||
ngx_pic_opt=
|
||||
ngx_binext=".exe"
|
||||
|
@ -106,6 +106,7 @@ WIN32_SELECT_SRCS=src/event/modules/ngx_win32_select_module.c
|
||||
|
||||
POLL_MODULE=ngx_poll_module
|
||||
POLL_SRCS=src/event/modules/ngx_poll_module.c
|
||||
WIN32_POLL_SRCS=src/event/modules/ngx_win32_poll_module.c
|
||||
|
||||
KQUEUE_MODULE=ngx_kqueue_module
|
||||
KQUEUE_SRCS=src/event/modules/ngx_kqueue_module.c
|
||||
|
435
src/event/modules/ngx_win32_poll_module.c
Normal file
435
src/event/modules/ngx_win32_poll_module.c
Normal file
@ -0,0 +1,435 @@
|
||||
|
||||
/*
|
||||
* Copyright (C) Igor Sysoev
|
||||
* Copyright (C) Maxim Dounin
|
||||
* Copyright (C) Nginx, Inc.
|
||||
*/
|
||||
|
||||
|
||||
#include <ngx_config.h>
|
||||
#include <ngx_core.h>
|
||||
#include <ngx_event.h>
|
||||
|
||||
|
||||
static ngx_int_t ngx_poll_init(ngx_cycle_t *cycle, ngx_msec_t timer);
|
||||
static void ngx_poll_done(ngx_cycle_t *cycle);
|
||||
static ngx_int_t ngx_poll_add_event(ngx_event_t *ev, ngx_int_t event,
|
||||
ngx_uint_t flags);
|
||||
static ngx_int_t ngx_poll_del_event(ngx_event_t *ev, ngx_int_t event,
|
||||
ngx_uint_t flags);
|
||||
static ngx_int_t ngx_poll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer,
|
||||
ngx_uint_t flags);
|
||||
static char *ngx_poll_init_conf(ngx_cycle_t *cycle, void *conf);
|
||||
|
||||
|
||||
static struct pollfd *event_list;
|
||||
static ngx_connection_t **event_index;
|
||||
static ngx_uint_t nevents;
|
||||
|
||||
|
||||
static ngx_str_t poll_name = ngx_string("poll");
|
||||
|
||||
static ngx_event_module_t ngx_poll_module_ctx = {
|
||||
&poll_name,
|
||||
NULL, /* create configuration */
|
||||
ngx_poll_init_conf, /* init configuration */
|
||||
|
||||
{
|
||||
ngx_poll_add_event, /* add an event */
|
||||
ngx_poll_del_event, /* delete an event */
|
||||
ngx_poll_add_event, /* enable an event */
|
||||
ngx_poll_del_event, /* disable an event */
|
||||
NULL, /* add an connection */
|
||||
NULL, /* delete an connection */
|
||||
NULL, /* trigger a notify */
|
||||
ngx_poll_process_events, /* process the events */
|
||||
ngx_poll_init, /* init the events */
|
||||
ngx_poll_done /* done the events */
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
ngx_module_t ngx_poll_module = {
|
||||
NGX_MODULE_V1,
|
||||
&ngx_poll_module_ctx, /* module context */
|
||||
NULL, /* module directives */
|
||||
NGX_EVENT_MODULE, /* module type */
|
||||
NULL, /* init master */
|
||||
NULL, /* init module */
|
||||
NULL, /* init process */
|
||||
NULL, /* init thread */
|
||||
NULL, /* exit thread */
|
||||
NULL, /* exit process */
|
||||
NULL, /* exit master */
|
||||
NGX_MODULE_V1_PADDING
|
||||
};
|
||||
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_poll_init(ngx_cycle_t *cycle, ngx_msec_t timer)
|
||||
{
|
||||
struct pollfd *list;
|
||||
ngx_connection_t **index;
|
||||
|
||||
if (event_list == NULL) {
|
||||
nevents = 0;
|
||||
}
|
||||
|
||||
if (ngx_process >= NGX_PROCESS_WORKER
|
||||
|| cycle->old_cycle == NULL
|
||||
|| cycle->old_cycle->connection_n < cycle->connection_n)
|
||||
{
|
||||
list = ngx_alloc(sizeof(struct pollfd) * cycle->connection_n,
|
||||
cycle->log);
|
||||
if (list == NULL) {
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
if (event_list) {
|
||||
ngx_memcpy(list, event_list, sizeof(struct pollfd) * nevents);
|
||||
ngx_free(event_list);
|
||||
}
|
||||
|
||||
event_list = list;
|
||||
|
||||
index = ngx_alloc(sizeof(ngx_connection_t *) * cycle->connection_n,
|
||||
cycle->log);
|
||||
if (index == NULL) {
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
if (event_index) {
|
||||
ngx_memcpy(index, event_index,
|
||||
sizeof(ngx_connection_t *) * nevents);
|
||||
ngx_free(event_index);
|
||||
}
|
||||
|
||||
event_index = index;
|
||||
}
|
||||
|
||||
ngx_io = ngx_os_io;
|
||||
|
||||
ngx_event_actions = ngx_poll_module_ctx.actions;
|
||||
|
||||
ngx_event_flags = NGX_USE_LEVEL_EVENT;
|
||||
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
ngx_poll_done(ngx_cycle_t *cycle)
|
||||
{
|
||||
ngx_free(event_list);
|
||||
ngx_free(event_index);
|
||||
|
||||
event_list = NULL;
|
||||
event_index = NULL;
|
||||
}
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_poll_add_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags)
|
||||
{
|
||||
ngx_event_t *e;
|
||||
ngx_connection_t *c;
|
||||
|
||||
c = ev->data;
|
||||
|
||||
ev->active = 1;
|
||||
|
||||
if (ev->index != NGX_INVALID_INDEX) {
|
||||
ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
|
||||
"poll event fd:%d ev:%i is already set", c->fd, event);
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
if (event == NGX_READ_EVENT) {
|
||||
e = c->write;
|
||||
#if (NGX_READ_EVENT != POLLIN)
|
||||
event = POLLIN;
|
||||
#endif
|
||||
|
||||
} else {
|
||||
e = c->read;
|
||||
#if (NGX_WRITE_EVENT != POLLOUT)
|
||||
event = POLLOUT;
|
||||
#endif
|
||||
}
|
||||
|
||||
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
|
||||
"poll add event: fd:%d ev:%i", c->fd, event);
|
||||
|
||||
if (e == NULL || e->index == NGX_INVALID_INDEX) {
|
||||
|
||||
event_list[nevents].fd = c->fd;
|
||||
event_list[nevents].events = (short) event;
|
||||
event_list[nevents].revents = 0;
|
||||
|
||||
event_index[nevents] = c;
|
||||
|
||||
ev->index = nevents;
|
||||
nevents++;
|
||||
|
||||
} else {
|
||||
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0,
|
||||
"poll add index: %i", e->index);
|
||||
|
||||
event_list[e->index].events |= (short) event;
|
||||
ev->index = e->index;
|
||||
}
|
||||
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_poll_del_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags)
|
||||
{
|
||||
ngx_event_t *e;
|
||||
ngx_connection_t *c;
|
||||
|
||||
c = ev->data;
|
||||
|
||||
ev->active = 0;
|
||||
|
||||
if (ev->index == NGX_INVALID_INDEX) {
|
||||
ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
|
||||
"poll event fd:%d ev:%i is already deleted",
|
||||
c->fd, event);
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
if (event == NGX_READ_EVENT) {
|
||||
e = c->write;
|
||||
#if (NGX_READ_EVENT != POLLIN)
|
||||
event = POLLIN;
|
||||
#endif
|
||||
|
||||
} else {
|
||||
e = c->read;
|
||||
#if (NGX_WRITE_EVENT != POLLOUT)
|
||||
event = POLLOUT;
|
||||
#endif
|
||||
}
|
||||
|
||||
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
|
||||
"poll del event: fd:%d ev:%i", c->fd, event);
|
||||
|
||||
if (e == NULL || e->index == NGX_INVALID_INDEX) {
|
||||
nevents--;
|
||||
|
||||
if (ev->index < nevents) {
|
||||
|
||||
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
|
||||
"index: copy event %ui to %i", nevents, ev->index);
|
||||
|
||||
event_list[ev->index] = event_list[nevents];
|
||||
event_index[ev->index] = event_index[nevents];
|
||||
|
||||
c = event_index[ev->index];
|
||||
|
||||
if (c->fd == (ngx_socket_t) -1) {
|
||||
ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
|
||||
"unexpected last event");
|
||||
|
||||
} else {
|
||||
if (c->read->index == nevents) {
|
||||
c->read->index = ev->index;
|
||||
}
|
||||
|
||||
if (c->write->index == nevents) {
|
||||
c->write->index = ev->index;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, ev->log, 0,
|
||||
"poll del index: %i", e->index);
|
||||
|
||||
event_list[e->index].events &= (short) ~event;
|
||||
}
|
||||
|
||||
ev->index = NGX_INVALID_INDEX;
|
||||
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
|
||||
static ngx_int_t
|
||||
ngx_poll_process_events(ngx_cycle_t *cycle, ngx_msec_t timer, ngx_uint_t flags)
|
||||
{
|
||||
int ready, revents;
|
||||
ngx_err_t err;
|
||||
ngx_uint_t i, found;
|
||||
ngx_event_t *ev;
|
||||
ngx_queue_t *queue;
|
||||
ngx_connection_t *c;
|
||||
|
||||
/* NGX_TIMER_INFINITE == INFTIM */
|
||||
|
||||
#if (NGX_DEBUG0)
|
||||
if (cycle->log->log_level & NGX_LOG_DEBUG_ALL) {
|
||||
for (i = 0; i < nevents; i++) {
|
||||
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
||||
"poll: %ui: fd:%d ev:%04Xd",
|
||||
i, event_list[i].fd, event_list[i].events);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "poll timer: %M", timer);
|
||||
|
||||
ready = WSAPoll(event_list, (u_int) nevents, (int) timer);
|
||||
|
||||
err = (ready == -1) ? ngx_errno : 0;
|
||||
|
||||
if (flags & NGX_UPDATE_TIME || ngx_event_timer_alarm) {
|
||||
ngx_time_update();
|
||||
}
|
||||
|
||||
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
||||
"poll ready %d of %ui", ready, nevents);
|
||||
|
||||
if (err) {
|
||||
ngx_log_error(NGX_LOG_ALERT, cycle->log, err, "WSAPoll() failed");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
if (ready == 0) {
|
||||
if (timer != NGX_TIMER_INFINITE) {
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
||||
"WSAPoll() returned no events without timeout");
|
||||
return NGX_ERROR;
|
||||
}
|
||||
|
||||
for (i = 0; i < nevents && ready; i++) {
|
||||
|
||||
revents = event_list[i].revents;
|
||||
|
||||
#if 1
|
||||
ngx_log_debug4(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
||||
"poll: %ui: fd:%d ev:%04Xd rev:%04Xd",
|
||||
i, event_list[i].fd, event_list[i].events, revents);
|
||||
#else
|
||||
if (revents) {
|
||||
ngx_log_debug4(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
||||
"poll: %ui: fd:%d ev:%04Xd rev:%04Xd",
|
||||
i, event_list[i].fd, event_list[i].events, revents);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (revents & POLLNVAL) {
|
||||
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
||||
"poll() error fd:%d ev:%04Xd rev:%04Xd",
|
||||
event_list[i].fd, event_list[i].events, revents);
|
||||
}
|
||||
|
||||
if (revents & ~(POLLIN|POLLOUT|POLLERR|POLLHUP|POLLNVAL)) {
|
||||
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
||||
"strange poll() events fd:%d ev:%04Xd rev:%04Xd",
|
||||
event_list[i].fd, event_list[i].events, revents);
|
||||
}
|
||||
|
||||
if (event_list[i].fd == (ngx_socket_t) -1) {
|
||||
/*
|
||||
* the disabled event, a workaround for our possible bug,
|
||||
* see the comment below
|
||||
*/
|
||||
continue;
|
||||
}
|
||||
|
||||
c = event_index[i];
|
||||
|
||||
if (c->fd == (ngx_socket_t) -1) {
|
||||
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "unexpected event");
|
||||
|
||||
/*
|
||||
* it is certainly our fault and it should be investigated,
|
||||
* in the meantime we disable this event to avoid a CPU spinning
|
||||
*/
|
||||
|
||||
if (i == nevents - 1) {
|
||||
nevents--;
|
||||
} else {
|
||||
event_list[i].fd = (ngx_socket_t) -1;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (revents & (POLLERR|POLLHUP|POLLNVAL)) {
|
||||
|
||||
/*
|
||||
* if the error events were returned, add POLLIN and POLLOUT
|
||||
* to handle the events at least in one active handler
|
||||
*/
|
||||
|
||||
revents |= POLLIN|POLLOUT;
|
||||
}
|
||||
|
||||
found = 0;
|
||||
|
||||
if ((revents & POLLIN) && c->read->active) {
|
||||
found = 1;
|
||||
|
||||
ev = c->read;
|
||||
ev->ready = 1;
|
||||
|
||||
queue = ev->accept ? &ngx_posted_accept_events
|
||||
: &ngx_posted_events;
|
||||
|
||||
ngx_post_event(ev, queue);
|
||||
}
|
||||
|
||||
if ((revents & POLLOUT) && c->write->active) {
|
||||
found = 1;
|
||||
|
||||
ev = c->write;
|
||||
ev->ready = 1;
|
||||
|
||||
ngx_post_event(ev, &ngx_posted_events);
|
||||
}
|
||||
|
||||
if (found) {
|
||||
ready--;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (ready != 0) {
|
||||
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "poll ready != events");
|
||||
}
|
||||
|
||||
return NGX_OK;
|
||||
}
|
||||
|
||||
|
||||
static char *
|
||||
ngx_poll_init_conf(ngx_cycle_t *cycle, void *conf)
|
||||
{
|
||||
ngx_event_conf_t *ecf;
|
||||
|
||||
ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
|
||||
|
||||
if (ecf->use != ngx_poll_module.ctx_index) {
|
||||
return NGX_CONF_OK;
|
||||
}
|
||||
|
||||
#if (NGX_LOAD_WSAPOLL)
|
||||
|
||||
if (!ngx_have_wsapoll) {
|
||||
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
|
||||
"poll is not available on this platform");
|
||||
return NGX_CONF_ERROR;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
return NGX_CONF_OK;
|
||||
}
|
@ -200,6 +200,49 @@ extern LPFN_CONNECTEX ngx_connectex;
|
||||
extern LPFN_DISCONNECTEX ngx_disconnectex;
|
||||
|
||||
|
||||
#if (NGX_HAVE_POLL && !defined POLLIN)
|
||||
|
||||
/*
|
||||
* WSAPoll() is only available if _WIN32_WINNT >= 0x0600.
|
||||
* If it is not available during compilation, we try to
|
||||
* load it dynamically at runtime.
|
||||
*/
|
||||
|
||||
#define NGX_LOAD_WSAPOLL 1
|
||||
|
||||
#define POLLRDNORM 0x0100
|
||||
#define POLLRDBAND 0x0200
|
||||
#define POLLIN (POLLRDNORM | POLLRDBAND)
|
||||
#define POLLPRI 0x0400
|
||||
|
||||
#define POLLWRNORM 0x0010
|
||||
#define POLLOUT (POLLWRNORM)
|
||||
#define POLLWRBAND 0x0020
|
||||
|
||||
#define POLLERR 0x0001
|
||||
#define POLLHUP 0x0002
|
||||
#define POLLNVAL 0x0004
|
||||
|
||||
typedef struct pollfd {
|
||||
|
||||
SOCKET fd;
|
||||
SHORT events;
|
||||
SHORT revents;
|
||||
|
||||
} WSAPOLLFD, *PWSAPOLLFD, FAR *LPWSAPOLLFD;
|
||||
|
||||
typedef int (WSAAPI *ngx_wsapoll_pt)(
|
||||
LPWSAPOLLFD fdArray,
|
||||
ULONG fds,
|
||||
INT timeout
|
||||
);
|
||||
|
||||
extern ngx_wsapoll_pt WSAPoll;
|
||||
extern ngx_uint_t ngx_have_wsapoll;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
int ngx_tcp_push(ngx_socket_t s);
|
||||
#define ngx_tcp_push_n "tcp_push()"
|
||||
|
||||
|
@ -58,6 +58,12 @@ static GUID cx_guid = WSAID_CONNECTEX;
|
||||
static GUID dx_guid = WSAID_DISCONNECTEX;
|
||||
|
||||
|
||||
#if (NGX_LOAD_WSAPOLL)
|
||||
ngx_wsapoll_pt WSAPoll;
|
||||
ngx_uint_t ngx_have_wsapoll;
|
||||
#endif
|
||||
|
||||
|
||||
ngx_int_t
|
||||
ngx_os_init(ngx_log_t *log)
|
||||
{
|
||||
@ -223,6 +229,32 @@ ngx_os_init(ngx_log_t *log)
|
||||
ngx_close_socket_n " failed");
|
||||
}
|
||||
|
||||
#if (NGX_LOAD_WSAPOLL)
|
||||
{
|
||||
HMODULE hmod;
|
||||
|
||||
hmod = GetModuleHandle("ws2_32.dll");
|
||||
if (hmod == NULL) {
|
||||
ngx_log_error(NGX_LOG_NOTICE, log, ngx_errno,
|
||||
"GetModuleHandle(\"ws2_32.dll\") failed");
|
||||
goto nopoll;
|
||||
}
|
||||
|
||||
WSAPoll = (ngx_wsapoll_pt) GetProcAddress(hmod, "WSAPoll");
|
||||
if (WSAPoll == NULL) {
|
||||
ngx_log_error(NGX_LOG_NOTICE, log, ngx_errno,
|
||||
"GetProcAddress(\"WSAPoll\") failed");
|
||||
goto nopoll;
|
||||
}
|
||||
|
||||
ngx_have_wsapoll = 1;
|
||||
|
||||
}
|
||||
|
||||
nopoll:
|
||||
|
||||
#endif
|
||||
|
||||
if (GetEnvironmentVariable("ngx_unique", ngx_unique, NGX_INT32_LEN + 1)
|
||||
!= 0)
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user