2003-05-20 00:39:14 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
/*
|
2004-01-16 14:15:48 +08:00
|
|
|
* Copyright (C) 2002-2004 Igor Sysoev, http://sysoev.ru/en/
|
2002-08-07 00:39:45 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
2002-08-30 00:59:54 +08:00
|
|
|
#include <ngx_core.h>
|
2002-08-07 00:39:45 +08:00
|
|
|
#include <ngx_event.h>
|
|
|
|
#include <ngx_kqueue_module.h>
|
|
|
|
|
|
|
|
|
2003-06-06 22:59:20 +08:00
|
|
|
typedef struct {
|
2003-11-21 14:30:49 +08:00
|
|
|
int changes;
|
|
|
|
int events;
|
2003-06-06 22:59:20 +08:00
|
|
|
} ngx_kqueue_conf_t;
|
|
|
|
|
|
|
|
|
2004-06-16 23:32:11 +08:00
|
|
|
static ngx_int_t ngx_kqueue_init(ngx_cycle_t *cycle);
|
2003-07-04 23:10:33 +08:00
|
|
|
static void ngx_kqueue_done(ngx_cycle_t *cycle);
|
2004-06-16 23:32:11 +08:00
|
|
|
static ngx_int_t ngx_kqueue_add_event(ngx_event_t *ev, int event, u_int flags);
|
|
|
|
static ngx_int_t ngx_kqueue_del_event(ngx_event_t *ev, int event, u_int flags);
|
|
|
|
static ngx_int_t ngx_kqueue_set_event(ngx_event_t *ev, int filter, u_int flags);
|
|
|
|
static ngx_int_t ngx_kqueue_process_events(ngx_cycle_t *cycle);
|
2004-04-08 23:58:25 +08:00
|
|
|
static ngx_inline void ngx_kqueue_dump_event(ngx_log_t *log,
|
|
|
|
struct kevent *kev);
|
2002-12-25 01:30:59 +08:00
|
|
|
|
2003-07-07 14:11:50 +08:00
|
|
|
static void *ngx_kqueue_create_conf(ngx_cycle_t *cycle);
|
|
|
|
static char *ngx_kqueue_init_conf(ngx_cycle_t *cycle, void *conf);
|
2003-05-16 23:27:48 +08:00
|
|
|
|
|
|
|
|
2003-06-16 02:32:13 +08:00
|
|
|
int ngx_kqueue = -1;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
static struct kevent *change_list, *event_list;
|
2003-11-21 14:30:49 +08:00
|
|
|
static int max_changes, nchanges, nevents;
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
static ngx_str_t kqueue_name = ngx_string("kqueue");
|
2003-05-16 23:27:48 +08:00
|
|
|
|
|
|
|
static ngx_command_t ngx_kqueue_commands[] = {
|
|
|
|
|
|
|
|
{ngx_string("kqueue_changes"),
|
|
|
|
NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_num_slot,
|
|
|
|
0,
|
2003-05-20 00:39:14 +08:00
|
|
|
offsetof(ngx_kqueue_conf_t, changes),
|
2003-05-16 23:27:48 +08:00
|
|
|
NULL},
|
|
|
|
|
|
|
|
{ngx_string("kqueue_events"),
|
|
|
|
NGX_EVENT_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_num_slot,
|
|
|
|
0,
|
2003-05-20 00:39:14 +08:00
|
|
|
offsetof(ngx_kqueue_conf_t, events),
|
2003-05-16 23:27:48 +08:00
|
|
|
NULL},
|
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
ngx_null_command
|
2003-05-16 23:27:48 +08:00
|
|
|
};
|
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
|
|
|
|
ngx_event_module_t ngx_kqueue_module_ctx = {
|
|
|
|
&kqueue_name,
|
|
|
|
ngx_kqueue_create_conf, /* create configuration */
|
|
|
|
ngx_kqueue_init_conf, /* init configuration */
|
|
|
|
|
2003-05-23 19:53:01 +08:00
|
|
|
{
|
2003-05-20 00:39:14 +08:00
|
|
|
ngx_kqueue_add_event, /* add an event */
|
|
|
|
ngx_kqueue_del_event, /* delete an event */
|
|
|
|
ngx_kqueue_add_event, /* enable an event */
|
|
|
|
ngx_kqueue_del_event, /* disable an event */
|
|
|
|
NULL, /* add an connection */
|
|
|
|
NULL, /* delete an connection */
|
|
|
|
ngx_kqueue_process_events, /* process the events */
|
|
|
|
ngx_kqueue_init, /* init the events */
|
2003-05-21 21:28:21 +08:00
|
|
|
ngx_kqueue_done /* done the events */
|
2003-05-20 00:39:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2003-05-16 23:27:48 +08:00
|
|
|
ngx_module_t ngx_kqueue_module = {
|
2003-05-27 20:18:54 +08:00
|
|
|
NGX_MODULE,
|
2003-05-20 00:39:14 +08:00
|
|
|
&ngx_kqueue_module_ctx, /* module context */
|
2003-05-16 23:27:48 +08:00
|
|
|
ngx_kqueue_commands, /* module directives */
|
2003-05-27 20:18:54 +08:00
|
|
|
NGX_EVENT_MODULE, /* module type */
|
2003-07-04 14:03:52 +08:00
|
|
|
NULL, /* init module */
|
|
|
|
NULL /* init child */
|
2003-05-16 23:27:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2003-07-04 00:30:22 +08:00
|
|
|
|
2004-06-16 23:32:11 +08:00
|
|
|
static ngx_int_t ngx_kqueue_init(ngx_cycle_t *cycle)
|
2003-07-04 00:30:22 +08:00
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
ngx_kqueue_conf_t *kcf;
|
|
|
|
|
|
|
|
kcf = ngx_event_get_conf(cycle->conf_ctx, ngx_kqueue_module);
|
|
|
|
|
2003-06-16 02:32:13 +08:00
|
|
|
if (ngx_kqueue == -1) {
|
|
|
|
ngx_kqueue = kqueue();
|
|
|
|
|
|
|
|
if (ngx_kqueue == -1) {
|
2003-07-04 23:10:33 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
|
|
|
|
"kqueue() failed");
|
2003-06-16 02:32:13 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max_changes < kcf->changes) {
|
|
|
|
if (nchanges) {
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
|
|
|
|
if (kevent(ngx_kqueue, change_list, nchanges, NULL, 0, &ts) == -1) {
|
2003-07-04 23:10:33 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
|
|
"kevent() failed");
|
2003-06-16 02:32:13 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2003-07-04 23:10:33 +08:00
|
|
|
nchanges = 0;
|
2003-06-16 02:32:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (change_list) {
|
|
|
|
ngx_free(change_list);
|
|
|
|
}
|
|
|
|
|
2003-12-09 23:08:11 +08:00
|
|
|
change_list = ngx_alloc(kcf->changes * sizeof(struct kevent),
|
|
|
|
cycle->log);
|
|
|
|
if (change_list == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2003-06-16 02:32:13 +08:00
|
|
|
}
|
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
max_changes = kcf->changes;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-06-16 02:32:13 +08:00
|
|
|
if (nevents < kcf->events) {
|
|
|
|
if (event_list) {
|
|
|
|
ngx_free(event_list);
|
|
|
|
}
|
2002-09-13 22:47:42 +08:00
|
|
|
|
2003-12-09 23:08:11 +08:00
|
|
|
event_list = ngx_alloc(kcf->events * sizeof(struct kevent),
|
|
|
|
cycle->log);
|
|
|
|
if (event_list == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2002-08-26 23:18:19 +08:00
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-06-16 02:32:13 +08:00
|
|
|
nevents = kcf->events;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-07-04 23:10:33 +08:00
|
|
|
ngx_io = ngx_os_io;
|
2003-06-16 02:32:13 +08:00
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
ngx_event_actions = ngx_kqueue_module_ctx.actions;
|
2003-02-11 15:14:40 +08:00
|
|
|
|
2003-10-13 00:49:16 +08:00
|
|
|
ngx_event_flags = NGX_USE_ONESHOT_EVENT
|
2003-02-11 15:14:40 +08:00
|
|
|
#if (HAVE_CLEAR_EVENT)
|
2003-10-13 00:49:16 +08:00
|
|
|
|NGX_USE_CLEAR_EVENT
|
2003-03-12 04:38:13 +08:00
|
|
|
#else
|
|
|
|
|NGX_USE_LEVEL_EVENT
|
2003-03-04 14:33:48 +08:00
|
|
|
#endif
|
|
|
|
#if (HAVE_LOWAT_EVENT)
|
|
|
|
|NGX_HAVE_LOWAT_EVENT
|
2003-02-07 01:21:13 +08:00
|
|
|
#endif
|
2004-04-05 04:32:09 +08:00
|
|
|
|NGX_HAVE_INSTANCE_EVENT
|
2003-02-11 15:14:40 +08:00
|
|
|
|NGX_HAVE_KQUEUE_EVENT;
|
|
|
|
|
2002-09-27 23:05:29 +08:00
|
|
|
return NGX_OK;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2003-07-04 23:10:33 +08:00
|
|
|
static void ngx_kqueue_done(ngx_cycle_t *cycle)
|
2003-03-04 14:33:48 +08:00
|
|
|
{
|
2003-05-20 00:39:14 +08:00
|
|
|
if (close(ngx_kqueue) == -1) {
|
2003-07-04 23:10:33 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
|
|
"kqueue close() failed");
|
2003-03-04 14:33:48 +08:00
|
|
|
}
|
2003-05-20 00:39:14 +08:00
|
|
|
|
2003-06-16 02:32:13 +08:00
|
|
|
ngx_kqueue = -1;
|
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
ngx_free(change_list);
|
|
|
|
ngx_free(event_list);
|
2003-06-16 02:32:13 +08:00
|
|
|
|
|
|
|
change_list = NULL;
|
|
|
|
event_list = NULL;
|
|
|
|
max_changes = 0;
|
|
|
|
nchanges = 0;
|
|
|
|
nevents = 0;
|
2003-03-04 14:33:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-16 23:32:11 +08:00
|
|
|
static ngx_int_t ngx_kqueue_add_event(ngx_event_t *ev, int event, u_int flags)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2003-11-26 04:44:56 +08:00
|
|
|
ngx_event_t *e;
|
2003-05-20 00:39:14 +08:00
|
|
|
ngx_connection_t *c;
|
|
|
|
|
2002-12-25 01:30:59 +08:00
|
|
|
ev->active = 1;
|
2004-01-06 04:55:48 +08:00
|
|
|
ev->disabled = 0;
|
2003-05-20 23:37:55 +08:00
|
|
|
ev->oneshot = (flags & NGX_ONESHOT_EVENT) ? 1 : 0;
|
2002-09-12 22:42:29 +08:00
|
|
|
|
2004-06-29 05:03:14 +08:00
|
|
|
if (ngx_thread_main()
|
|
|
|
&& nchanges > 0
|
2003-11-21 14:30:49 +08:00
|
|
|
&& ev->index < (u_int) nchanges
|
|
|
|
&& ((uintptr_t) change_list[ev->index].udata & (uintptr_t) ~1)
|
|
|
|
== (uintptr_t) ev)
|
2002-12-23 14:29:22 +08:00
|
|
|
{
|
2003-11-26 04:44:56 +08:00
|
|
|
if (change_list[ev->index].flags == EV_DISABLE) {
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
/*
|
|
|
|
* if the EV_DISABLE is still not passed to a kernel
|
|
|
|
* we will not pass it
|
|
|
|
*/
|
2003-11-26 04:44:56 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
|
|
|
|
"kevent activated: %d: ft:%d",
|
|
|
|
ngx_event_ident(ev->data), event);
|
2003-11-26 04:44:56 +08:00
|
|
|
|
|
|
|
if (ev->index < (u_int) --nchanges) {
|
|
|
|
e = (ngx_event_t *) change_list[nchanges].udata;
|
|
|
|
change_list[ev->index] = change_list[nchanges];
|
|
|
|
e->index = ev->index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
c = ev->data;
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ev->log, 0,
|
2003-10-28 00:16:17 +08:00
|
|
|
"previous event on #%d were not passed in kernel", c->fd);
|
2003-03-21 00:09:44 +08:00
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
return NGX_ERROR;
|
2002-12-23 14:29:22 +08:00
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
return ngx_kqueue_set_event(ev, event, EV_ADD|EV_ENABLE|flags);
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2004-06-16 23:32:11 +08:00
|
|
|
static ngx_int_t ngx_kqueue_del_event(ngx_event_t *ev, int event, u_int flags)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2002-12-25 01:30:59 +08:00
|
|
|
ngx_event_t *e;
|
|
|
|
|
|
|
|
ev->active = 0;
|
2004-01-06 04:55:48 +08:00
|
|
|
ev->disabled = 0;
|
2004-07-02 13:47:00 +08:00
|
|
|
|
2004-06-29 05:03:14 +08:00
|
|
|
if (ngx_thread_main()
|
|
|
|
&& nchanges > 0
|
2003-11-21 14:30:49 +08:00
|
|
|
&& ev->index < (u_int) nchanges
|
|
|
|
&& ((uintptr_t) change_list[ev->index].udata & (uintptr_t) ~1)
|
|
|
|
== (uintptr_t) ev)
|
2002-12-19 15:08:55 +08:00
|
|
|
{
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
|
|
|
|
"kevent deleted: %d: ft:%d",
|
|
|
|
ngx_event_ident(ev->data), event);
|
2003-03-21 00:09:44 +08:00
|
|
|
|
|
|
|
/* if the event is still not passed to a kernel we will not pass it */
|
|
|
|
|
2003-11-21 14:30:49 +08:00
|
|
|
if (ev->index < (u_int) --nchanges) {
|
2002-12-15 14:25:09 +08:00
|
|
|
e = (ngx_event_t *) change_list[nchanges].udata;
|
|
|
|
change_list[ev->index] = change_list[nchanges];
|
|
|
|
e->index = ev->index;
|
|
|
|
}
|
2002-10-05 01:58:04 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2003-10-13 00:49:16 +08:00
|
|
|
/*
|
2004-02-13 04:57:10 +08:00
|
|
|
* when the file descriptor is closed the kqueue automatically deletes
|
2003-10-13 00:49:16 +08:00
|
|
|
* its filters so we do not need to delete explicity the event
|
|
|
|
* before the closing the file descriptor.
|
|
|
|
*/
|
2003-03-21 00:09:44 +08:00
|
|
|
|
2002-12-25 01:30:59 +08:00
|
|
|
if (flags & NGX_CLOSE_EVENT) {
|
2002-12-15 14:25:09 +08:00
|
|
|
return NGX_OK;
|
2002-12-25 01:30:59 +08:00
|
|
|
}
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
if (flags & NGX_DISABLE_EVENT) {
|
|
|
|
ev->disabled = 1;
|
|
|
|
}
|
|
|
|
|
2003-11-21 14:30:49 +08:00
|
|
|
return ngx_kqueue_set_event(ev, event,
|
|
|
|
flags & NGX_DISABLE_EVENT ? EV_DISABLE : EV_DELETE);
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2004-06-16 23:32:11 +08:00
|
|
|
static ngx_int_t ngx_kqueue_set_event(ngx_event_t *ev, int filter, u_int flags)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2003-06-16 02:32:13 +08:00
|
|
|
struct timespec ts;
|
|
|
|
ngx_connection_t *c;
|
2004-06-29 05:03:14 +08:00
|
|
|
struct kevent *kev, kv;
|
2002-12-19 15:08:55 +08:00
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
c = ev->data;
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-12-19 20:45:27 +08:00
|
|
|
ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
|
|
"kevent set event: %d: ft:%d fl:%04X",
|
|
|
|
c->fd, filter, flags);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2004-06-29 05:03:14 +08:00
|
|
|
if (ngx_thread_main() && nchanges >= max_changes) {
|
2002-08-07 00:39:45 +08:00
|
|
|
ngx_log_error(NGX_LOG_WARN, ev->log, 0,
|
2002-09-27 23:05:29 +08:00
|
|
|
"kqueue change list is filled up");
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-12-19 15:08:55 +08:00
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = 0;
|
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
if (kevent(ngx_kqueue, change_list, nchanges, NULL, 0, &ts) == -1) {
|
2003-06-16 02:32:13 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, "kevent() failed");
|
2002-09-13 22:47:42 +08:00
|
|
|
return NGX_ERROR;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
2002-12-25 01:30:59 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
nchanges = 0;
|
|
|
|
}
|
|
|
|
|
2004-06-29 05:03:14 +08:00
|
|
|
kev = ngx_thread_main() ? &change_list[nchanges] : &kv;
|
|
|
|
|
|
|
|
kev->ident = c->fd;
|
|
|
|
kev->filter = filter;
|
|
|
|
kev->flags = flags;
|
|
|
|
kev->udata = (void *) ((uintptr_t) ev | ev->instance);
|
2003-03-04 14:33:48 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (filter == EVFILT_VNODE) {
|
2004-06-29 05:03:14 +08:00
|
|
|
kev->fflags = NOTE_DELETE|NOTE_WRITE|NOTE_EXTEND
|
|
|
|
|NOTE_ATTRIB|NOTE_RENAME
|
2003-11-26 23:42:18 +08:00
|
|
|
#if (__FreeBSD__ == 4 && __FreeBSD_version >= 430000) \
|
|
|
|
|| __FreeBSD_version >= 500018
|
2004-06-29 05:03:14 +08:00
|
|
|
|NOTE_REVOKE
|
2003-11-26 23:42:18 +08:00
|
|
|
#endif
|
|
|
|
;
|
2004-06-29 05:03:14 +08:00
|
|
|
kev->data = 0;
|
2003-03-04 14:33:48 +08:00
|
|
|
|
|
|
|
} else {
|
2003-11-26 04:44:56 +08:00
|
|
|
#if (HAVE_LOWAT_EVENT)
|
|
|
|
if (flags & NGX_LOWAT_EVENT) {
|
2004-06-29 05:03:14 +08:00
|
|
|
kev->fflags = NOTE_LOWAT;
|
|
|
|
kev->data = ev->available;
|
2003-03-04 14:33:48 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
} else {
|
2004-06-29 05:03:14 +08:00
|
|
|
kev->fflags = 0;
|
|
|
|
kev->data = 0;
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
2003-03-04 14:33:48 +08:00
|
|
|
#else
|
2004-06-29 05:03:14 +08:00
|
|
|
kev->fflags = 0;
|
|
|
|
kev->data = 0;
|
2003-03-04 14:33:48 +08:00
|
|
|
#endif
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
2002-10-05 01:58:04 +08:00
|
|
|
|
2004-06-29 05:03:14 +08:00
|
|
|
if (ngx_thread_main()) {
|
|
|
|
ev->index = nchanges;
|
|
|
|
nchanges++;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ts.tv_sec = 0;
|
|
|
|
ts.tv_nsec = 0;
|
2002-10-05 01:58:04 +08:00
|
|
|
|
2004-06-29 05:03:14 +08:00
|
|
|
if (kevent(ngx_kqueue, &kv, 1, NULL, 0, &ts) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, "kevent() failed");
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-09-13 22:47:42 +08:00
|
|
|
return NGX_OK;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2004-03-31 23:26:46 +08:00
|
|
|
static ngx_int_t ngx_kqueue_process_events(ngx_cycle_t *cycle)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
2004-01-30 05:45:01 +08:00
|
|
|
int events;
|
2004-04-05 04:32:09 +08:00
|
|
|
ngx_int_t i, instance;
|
2004-04-22 02:54:33 +08:00
|
|
|
ngx_uint_t lock, accept_lock, expire;
|
2003-11-12 02:13:43 +08:00
|
|
|
ngx_err_t err;
|
|
|
|
ngx_msec_t timer;
|
|
|
|
ngx_event_t *ev;
|
|
|
|
ngx_epoch_msec_t delta;
|
|
|
|
struct timeval tv;
|
|
|
|
struct timespec ts, *tp;
|
2002-12-15 14:25:09 +08:00
|
|
|
|
2004-04-15 04:34:05 +08:00
|
|
|
for ( ;; ) {
|
|
|
|
timer = ngx_event_find_timer();
|
2004-02-26 04:16:15 +08:00
|
|
|
|
|
|
|
#if (NGX_THREADS)
|
2004-04-15 04:34:05 +08:00
|
|
|
if (timer == NGX_TIMER_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2004-02-26 04:16:15 +08:00
|
|
|
|
2004-06-29 00:05:02 +08:00
|
|
|
if (timer == NGX_TIMER_INFINITE || timer > 500) {
|
|
|
|
timer = 500;
|
|
|
|
break;
|
|
|
|
}
|
2004-02-26 04:16:15 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2004-04-15 04:34:05 +08:00
|
|
|
if (timer != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
|
|
"kevent expired timer");
|
|
|
|
|
2004-04-16 13:14:16 +08:00
|
|
|
ngx_event_expire_timers((ngx_msec_t)
|
|
|
|
(ngx_elapsed_msec - ngx_old_elapsed_msec));
|
2004-04-15 04:34:05 +08:00
|
|
|
|
|
|
|
/* TODO: if ngx_threaded then wake up the worker thread */
|
|
|
|
}
|
|
|
|
|
2003-12-06 01:07:27 +08:00
|
|
|
ngx_old_elapsed_msec = ngx_elapsed_msec;
|
2004-04-05 04:32:09 +08:00
|
|
|
expire = 1;
|
2004-04-22 02:54:33 +08:00
|
|
|
accept_lock = 0;
|
2003-01-24 14:20:47 +08:00
|
|
|
|
2004-03-31 23:26:46 +08:00
|
|
|
if (ngx_accept_mutex) {
|
2004-04-22 02:54:33 +08:00
|
|
|
if (ngx_accept_disabled > 0) {
|
|
|
|
ngx_accept_disabled--;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (ngx_trylock_accept_mutex(cycle) == NGX_ERROR) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_accept_mutex_held) {
|
|
|
|
accept_lock = 1;
|
2004-03-31 23:26:46 +08:00
|
|
|
|
2004-04-22 02:54:33 +08:00
|
|
|
} else if (timer == NGX_TIMER_INFINITE
|
|
|
|
|| timer > ngx_accept_mutex_delay)
|
|
|
|
{
|
|
|
|
timer = ngx_accept_mutex_delay;
|
|
|
|
expire = 0;
|
|
|
|
}
|
2004-03-31 23:26:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-04-15 04:34:05 +08:00
|
|
|
if (timer == NGX_TIMER_INFINITE) {
|
|
|
|
tp = NULL;
|
|
|
|
expire = 0;
|
2004-04-15 01:44:28 +08:00
|
|
|
|
2004-04-15 04:34:05 +08:00
|
|
|
} else {
|
2003-01-24 14:20:47 +08:00
|
|
|
ts.tv_sec = timer / 1000;
|
|
|
|
ts.tv_nsec = (timer % 1000) * 1000000;
|
|
|
|
tp = &ts;
|
|
|
|
}
|
|
|
|
|
2004-03-31 23:26:46 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
|
|
"kevent timer: %d", timer);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2003-05-20 00:39:14 +08:00
|
|
|
events = kevent(ngx_kqueue, change_list, nchanges, event_list, nevents, tp);
|
2002-12-19 15:08:55 +08:00
|
|
|
|
2002-09-13 22:47:42 +08:00
|
|
|
if (events == -1) {
|
2003-07-07 14:11:50 +08:00
|
|
|
err = ngx_errno;
|
|
|
|
} else {
|
|
|
|
err = 0;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
nchanges = 0;
|
|
|
|
|
2003-11-12 02:13:43 +08:00
|
|
|
ngx_gettimeofday(&tv);
|
2004-01-30 05:45:01 +08:00
|
|
|
ngx_time_update(tv.tv_sec);
|
2003-11-11 05:09:22 +08:00
|
|
|
|
2004-03-31 23:26:46 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
|
|
|
"kevent events: %d", events);
|
2004-02-03 05:19:52 +08:00
|
|
|
|
2003-12-04 22:53:00 +08:00
|
|
|
delta = ngx_elapsed_msec;
|
|
|
|
ngx_elapsed_msec = tv.tv_sec * 1000 + tv.tv_usec / 1000 - ngx_start_msec;
|
|
|
|
|
2004-01-31 01:39:00 +08:00
|
|
|
if (err) {
|
|
|
|
ngx_log_error((err == NGX_EINTR) ? NGX_LOG_INFO : NGX_LOG_ALERT,
|
2004-03-31 23:26:46 +08:00
|
|
|
cycle->log, err, "kevent() failed");
|
2004-04-01 14:21:13 +08:00
|
|
|
ngx_accept_mutex_unlock();
|
2004-01-31 01:39:00 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-04-15 04:34:05 +08:00
|
|
|
if (timer != NGX_TIMER_INFINITE) {
|
2003-12-04 22:53:00 +08:00
|
|
|
delta = ngx_elapsed_msec - delta;
|
2003-07-07 14:11:50 +08:00
|
|
|
|
2004-03-31 23:26:46 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
2004-01-30 05:45:01 +08:00
|
|
|
"kevent timer: %d, delta: %d", timer, (int) delta);
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
} else {
|
2002-12-25 01:30:59 +08:00
|
|
|
if (events == 0) {
|
2004-03-31 23:26:46 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
2003-06-16 02:32:13 +08:00
|
|
|
"kevent() returned no events without timeout");
|
2004-04-01 14:21:13 +08:00
|
|
|
ngx_accept_mutex_unlock();
|
2002-12-25 01:30:59 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2003-12-04 22:53:00 +08:00
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2004-06-29 00:05:02 +08:00
|
|
|
if (events > 0) {
|
|
|
|
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
|
|
|
|
ngx_accept_mutex_unlock();
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2004-02-27 01:10:01 +08:00
|
|
|
|
2004-06-29 00:05:02 +08:00
|
|
|
lock = 1;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
lock =0;
|
|
|
|
}
|
2004-04-05 04:32:09 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
for (i = 0; i < events; i++) {
|
|
|
|
|
2004-04-08 23:58:25 +08:00
|
|
|
ngx_kqueue_dump_event(cycle->log, &event_list[i]);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
if (event_list[i].flags & EV_ERROR) {
|
2004-03-31 23:26:46 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, event_list[i].data,
|
2003-06-16 02:32:13 +08:00
|
|
|
"kevent() error on %d", event_list[i].ident);
|
2002-08-07 00:39:45 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ev = (ngx_event_t *) event_list[i].udata;
|
2002-12-25 01:30:59 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
switch (event_list[i].filter) {
|
|
|
|
|
|
|
|
case EVFILT_READ:
|
|
|
|
case EVFILT_WRITE:
|
2003-03-21 00:09:44 +08:00
|
|
|
|
2003-05-21 21:28:21 +08:00
|
|
|
instance = (uintptr_t) ev & 1;
|
2003-11-21 14:30:49 +08:00
|
|
|
ev = (ngx_event_t *) ((uintptr_t) ev & (uintptr_t) ~1);
|
2003-05-21 21:28:21 +08:00
|
|
|
|
2004-07-02 23:54:34 +08:00
|
|
|
if (ev->closed || ev->instance != instance) {
|
2003-12-15 04:10:27 +08:00
|
|
|
|
|
|
|
/*
|
2004-02-01 16:10:52 +08:00
|
|
|
* the stale event from a file descriptor
|
2003-12-15 04:10:27 +08:00
|
|
|
* that was just closed in this iteration
|
|
|
|
*/
|
|
|
|
|
2004-03-31 23:26:46 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
|
2003-12-15 04:10:27 +08:00
|
|
|
"kevent: stale event " PTR_FMT, ev);
|
2003-05-21 21:28:21 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-04-13 00:38:09 +08:00
|
|
|
if (ev->log && (ev->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
|
|
|
|
ngx_kqueue_dump_event(ev->log, &event_list[i]);
|
|
|
|
}
|
|
|
|
|
2004-07-02 13:47:00 +08:00
|
|
|
ev->returned_instance = instance;
|
|
|
|
|
2004-07-02 23:54:34 +08:00
|
|
|
#if (NGX_THREADS)
|
|
|
|
|
|
|
|
if (ngx_threaded && !ev->accept) {
|
2004-07-02 13:47:00 +08:00
|
|
|
ev->posted_ready = 1;
|
|
|
|
ev->posted_available += event_list[i].data;
|
|
|
|
|
|
|
|
if (event_list[i].flags & EV_EOF) {
|
|
|
|
ev->posted_eof = 1;
|
|
|
|
ev->posted_errno = event_list[i].fflags;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_post_event(ev);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-07-02 23:54:34 +08:00
|
|
|
#endif
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
ev->available = event_list[i].data;
|
|
|
|
|
|
|
|
if (event_list[i].flags & EV_EOF) {
|
2004-07-02 23:54:34 +08:00
|
|
|
ev->pending_eof = 1;
|
2003-10-28 23:45:41 +08:00
|
|
|
ev->kq_errno = event_list[i].fflags;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
2003-10-13 00:49:16 +08:00
|
|
|
ev->ready = 1;
|
|
|
|
|
|
|
|
break;
|
2003-02-07 01:21:13 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
case EVFILT_VNODE:
|
|
|
|
ev->kq_vnode = 1;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2003-02-07 01:21:13 +08:00
|
|
|
case EVFILT_AIO:
|
2003-10-30 16:51:06 +08:00
|
|
|
ev->complete = 1;
|
|
|
|
ev->ready = 1;
|
2003-02-07 01:21:13 +08:00
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2004-03-31 23:26:46 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
|
2003-11-26 04:44:56 +08:00
|
|
|
"unexpected kevent() filter %d",
|
|
|
|
event_list[i].filter);
|
2004-02-27 01:10:01 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-04-05 04:32:09 +08:00
|
|
|
if (!ngx_threaded && !ngx_accept_mutex_held) {
|
|
|
|
ev->event_handler(ev);
|
2004-02-27 01:10:01 +08:00
|
|
|
continue;
|
2002-12-25 01:30:59 +08:00
|
|
|
}
|
2004-02-27 01:10:01 +08:00
|
|
|
|
2004-04-05 04:32:09 +08:00
|
|
|
if (!ev->accept) {
|
|
|
|
ngx_post_event(ev);
|
|
|
|
continue;
|
2004-03-01 05:03:02 +08:00
|
|
|
}
|
|
|
|
|
2004-04-22 02:54:33 +08:00
|
|
|
if (ngx_accept_disabled > 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-04-05 04:32:09 +08:00
|
|
|
ngx_mutex_unlock(ngx_posted_events_mutex);
|
2004-02-27 01:10:01 +08:00
|
|
|
|
2004-04-05 04:32:09 +08:00
|
|
|
ev->event_handler(ev);
|
2004-03-31 23:26:46 +08:00
|
|
|
|
2004-04-22 02:54:33 +08:00
|
|
|
if (ngx_accept_disabled > 0) {
|
|
|
|
ngx_accept_mutex_unlock();
|
|
|
|
accept_lock = 0;
|
|
|
|
}
|
|
|
|
|
2004-04-05 04:32:09 +08:00
|
|
|
if (i + 1 == events) {
|
|
|
|
lock = 0;
|
|
|
|
break;
|
2004-03-31 23:26:46 +08:00
|
|
|
}
|
|
|
|
|
2004-04-05 04:32:09 +08:00
|
|
|
if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_ERROR) {
|
2004-04-22 02:54:33 +08:00
|
|
|
if (accept_lock) {
|
|
|
|
ngx_accept_mutex_unlock();
|
|
|
|
}
|
2004-04-05 04:32:09 +08:00
|
|
|
return NGX_ERROR;
|
2004-03-31 23:26:46 +08:00
|
|
|
}
|
2004-02-27 01:10:01 +08:00
|
|
|
}
|
|
|
|
|
2004-04-22 02:54:33 +08:00
|
|
|
if (accept_lock) {
|
|
|
|
ngx_accept_mutex_unlock();
|
|
|
|
}
|
|
|
|
|
2004-04-05 04:32:09 +08:00
|
|
|
if (lock) {
|
|
|
|
ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
|
|
}
|
2003-05-20 00:39:14 +08:00
|
|
|
|
2004-04-05 04:32:09 +08:00
|
|
|
if (expire && delta) {
|
|
|
|
ngx_event_expire_timers((ngx_msec_t) delta);
|
2004-02-27 01:10:01 +08:00
|
|
|
}
|
|
|
|
|
2004-06-28 02:01:57 +08:00
|
|
|
if (ngx_posted_events) {
|
|
|
|
if (ngx_threaded) {
|
2004-06-29 00:05:02 +08:00
|
|
|
ngx_cond_signal(ngx_posted_events_cv);
|
2004-06-28 02:01:57 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
ngx_event_process_posted(cycle);
|
|
|
|
}
|
2004-03-31 23:26:46 +08:00
|
|
|
}
|
|
|
|
|
2004-04-05 04:32:09 +08:00
|
|
|
return NGX_OK;
|
2004-02-27 01:10:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-04-08 23:58:25 +08:00
|
|
|
static ngx_inline void ngx_kqueue_dump_event(ngx_log_t *log, struct kevent *kev)
|
|
|
|
{
|
|
|
|
ngx_log_debug6(NGX_LOG_DEBUG_EVENT, log, 0,
|
|
|
|
(kev->ident > 0x8000000 && kev->ident != (unsigned) -1) ?
|
|
|
|
"kevent: " PTR_FMT ": ft:%d fl:%04X ff:%08X d:%d ud:"
|
|
|
|
PTR_FMT:
|
|
|
|
"kevent: %d: ft:%d fl:%04X ff:%08X d:%d ud:" PTR_FMT,
|
|
|
|
kev->ident, kev->filter,
|
|
|
|
kev->flags, kev->fflags,
|
|
|
|
kev->data, kev->udata);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-07 14:11:50 +08:00
|
|
|
static void *ngx_kqueue_create_conf(ngx_cycle_t *cycle)
|
2003-05-20 00:39:14 +08:00
|
|
|
{
|
|
|
|
ngx_kqueue_conf_t *kcf;
|
|
|
|
|
2003-07-07 14:11:50 +08:00
|
|
|
ngx_test_null(kcf, ngx_palloc(cycle->pool, sizeof(ngx_kqueue_conf_t)),
|
2003-05-20 00:39:14 +08:00
|
|
|
NGX_CONF_ERROR);
|
|
|
|
|
|
|
|
kcf->changes = NGX_CONF_UNSET;
|
|
|
|
kcf->events = NGX_CONF_UNSET;
|
|
|
|
|
|
|
|
return kcf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-07 14:11:50 +08:00
|
|
|
static char *ngx_kqueue_init_conf(ngx_cycle_t *cycle, void *conf)
|
2003-05-20 00:39:14 +08:00
|
|
|
{
|
|
|
|
ngx_kqueue_conf_t *kcf = conf;
|
|
|
|
|
2003-11-21 14:30:49 +08:00
|
|
|
ngx_conf_init_value(kcf->changes, 512);
|
|
|
|
ngx_conf_init_value(kcf->events, 512);
|
2003-05-20 00:39:14 +08:00
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|