2002-08-07 00:39:45 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2002 Igor Sysoev, http://sysoev.ru
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
NEED ? : unify change_list and event_list:
|
|
|
|
event_list = change_list;
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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_types.h>
|
|
|
|
#include <ngx_log.h>
|
|
|
|
#include <ngx_connection.h>
|
|
|
|
#include <ngx_event.h>
|
|
|
|
#include <ngx_kqueue_module.h>
|
|
|
|
|
|
|
|
#if (USE_KQUEUE) && !(HAVE_KQUEUE)
|
|
|
|
#error "kqueue is not supported on this platform"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int kq;
|
|
|
|
static struct kevent *change_list, *event_list;
|
|
|
|
static int nchanges, nevents;
|
|
|
|
|
|
|
|
static ngx_event_t timer_queue;
|
|
|
|
|
2002-09-27 23:05:29 +08:00
|
|
|
int ngx_kqueue_init(int max_connections, ngx_log_t *log)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
|
|
|
int size = sizeof(struct kevent) * 512;
|
|
|
|
|
|
|
|
nchanges = 0;
|
|
|
|
nevents = 512;
|
|
|
|
|
2002-09-13 22:47:42 +08:00
|
|
|
kq = kqueue();
|
|
|
|
|
|
|
|
if (kq == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "kqueue() failed");
|
2002-09-27 23:05:29 +08:00
|
|
|
return NGX_ERROR;
|
2002-08-26 23:18:19 +08:00
|
|
|
}
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-09-27 23:05:29 +08:00
|
|
|
ngx_test_null(change_list, ngx_alloc(size, log), NGX_ERROR);
|
|
|
|
ngx_test_null(event_list, ngx_alloc(size, log), NGX_ERROR);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
timer_queue.timer_prev = &timer_queue;
|
|
|
|
timer_queue.timer_next = &timer_queue;
|
|
|
|
|
|
|
|
#if !(USE_KQUEUE)
|
|
|
|
ngx_event_actions.add = ngx_kqueue_add_event;
|
|
|
|
ngx_event_actions.del = ngx_kqueue_del_event;
|
2002-09-02 22:48:24 +08:00
|
|
|
ngx_event_actions.timer = ngx_kqueue_add_timer;
|
2002-08-07 00:39:45 +08:00
|
|
|
ngx_event_actions.process = ngx_kqueue_process_events;
|
|
|
|
#endif
|
2002-09-27 23:05:29 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int ngx_kqueue_add_event(ngx_event_t *ev, int event, u_int flags)
|
|
|
|
{
|
2002-09-12 22:42:29 +08:00
|
|
|
ev->oneshot = (flags & NGX_ONESHOT_EVENT) ? 1: 0;
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
return ngx_kqueue_set_event(ev, event, EV_ADD | flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ngx_kqueue_del_event(ngx_event_t *ev, int event)
|
|
|
|
{
|
|
|
|
return ngx_kqueue_set_event(ev, event, EV_DELETE);
|
|
|
|
}
|
|
|
|
|
|
|
|
int ngx_kqueue_set_event(ngx_event_t *ev, int filter, u_int flags)
|
|
|
|
{
|
|
|
|
struct timespec ts = { 0, 0 };
|
|
|
|
ngx_connection_t *cn = (ngx_connection_t *) ev->data;
|
|
|
|
|
2002-09-13 22:47:42 +08:00
|
|
|
ngx_log_debug(ev->log, "kqueue set event: %d: ft:%d f:%08x" _
|
2002-08-07 00:39:45 +08:00
|
|
|
cn->fd _ filter _ flags);
|
|
|
|
|
|
|
|
if (nchanges >= nevents) {
|
|
|
|
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
|
|
|
|
|
|
|
if (kevent(kq, change_list, nchanges, NULL, 0, &ts) == -1) {
|
2002-09-13 22:47:42 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_errno, "kevent failed");
|
|
|
|
return NGX_ERROR;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
nchanges = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
change_list[nchanges].ident = cn->fd;
|
|
|
|
change_list[nchanges].filter = filter;
|
|
|
|
change_list[nchanges].flags = flags;
|
|
|
|
change_list[nchanges].fflags = 0;
|
|
|
|
change_list[nchanges].data = 0;
|
|
|
|
change_list[nchanges].udata = ev;
|
|
|
|
nchanges++;
|
|
|
|
|
2002-09-13 22:47:42 +08:00
|
|
|
return NGX_OK;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int ngx_kqueue_process_events(ngx_log_t *log)
|
|
|
|
{
|
|
|
|
int events, i;
|
|
|
|
u_int timer = 0, delta = 0;
|
2002-09-18 01:49:32 +08:00
|
|
|
ngx_event_t *ev;
|
2002-08-07 00:39:45 +08:00
|
|
|
struct timeval tv;
|
|
|
|
struct timespec ts, *tp = NULL;
|
|
|
|
|
|
|
|
if (timer_queue.timer_next != &timer_queue) {
|
|
|
|
timer = timer_queue.timer_next->timer_delta;
|
|
|
|
ts.tv_sec = timer / 1000;
|
|
|
|
ts.tv_nsec = (timer % 1000) * 1000000;
|
|
|
|
tp = &ts;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
delta = tv.tv_sec * 1000 + tv.tv_usec / 1000;
|
|
|
|
}
|
|
|
|
|
2002-09-13 22:47:42 +08:00
|
|
|
ngx_log_debug(log, "kevent timer: %d" _ timer);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
2002-09-13 22:47:42 +08:00
|
|
|
events = kevent(kq, change_list, nchanges, event_list, nevents, tp);
|
|
|
|
if (events == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "kevent failed");
|
|
|
|
return NGX_ERROR;
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
nchanges = 0;
|
|
|
|
|
|
|
|
if (timer) {
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
delta = tv.tv_sec * 1000 + tv.tv_usec / 1000 - delta;
|
|
|
|
|
|
|
|
} else {
|
2002-09-13 22:47:42 +08:00
|
|
|
ngx_assert((events != 0), return NGX_ERROR, log,
|
2002-08-07 00:39:45 +08:00
|
|
|
"kevent returns no events without timeout");
|
|
|
|
}
|
|
|
|
|
2002-09-13 22:47:42 +08:00
|
|
|
ngx_log_debug(log, "kevent timer: %d, delta: %d" _ timer _ delta);
|
2002-08-07 00:39:45 +08:00
|
|
|
|
|
|
|
if (timer) {
|
|
|
|
if (delta >= timer) {
|
2002-09-18 01:49:32 +08:00
|
|
|
for ( ;; ) {
|
|
|
|
ev = timer_queue.timer_next;
|
|
|
|
|
|
|
|
if (ev == &timer_queue || delta < ev->timer_delta)
|
|
|
|
break;
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
delta -= ev->timer_delta;
|
|
|
|
ngx_del_timer(ev);
|
2002-08-26 23:18:19 +08:00
|
|
|
ev->timedout = 1;
|
2002-08-30 00:59:54 +08:00
|
|
|
if (ev->event_handler(ev) == NGX_ERROR)
|
2002-08-26 23:18:19 +08:00
|
|
|
ev->close_handler(ev);
|
2002-08-07 00:39:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
timer_queue.timer_next->timer_delta -= delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < events; i++) {
|
|
|
|
|
2002-09-13 22:47:42 +08:00
|
|
|
ngx_log_debug(log, "kevent: %d: ft:%d f:%08x ff:%08x d:%d ud:%08x" _
|
2002-08-07 00:39:45 +08:00
|
|
|
event_list[i].ident _ event_list[i].filter _
|
|
|
|
event_list[i].flags _ event_list[i].fflags _
|
|
|
|
event_list[i].data _ event_list[i].udata);
|
|
|
|
|
|
|
|
if (event_list[i].flags & EV_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, event_list[i].data,
|
2002-09-13 22:47:42 +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;
|
|
|
|
|
|
|
|
switch (event_list[i].filter) {
|
|
|
|
|
|
|
|
case EVFILT_READ:
|
|
|
|
case EVFILT_WRITE:
|
|
|
|
ev->ready = 1;
|
|
|
|
ev->available = event_list[i].data;
|
|
|
|
|
|
|
|
if (event_list[i].flags & EV_EOF) {
|
|
|
|
ev->eof = 1;
|
|
|
|
ev->error = event_list[i].fflags;
|
|
|
|
}
|
|
|
|
|
2002-09-12 22:42:29 +08:00
|
|
|
if (ev->oneshot)
|
|
|
|
ngx_del_timer(ev);
|
|
|
|
|
2002-08-30 00:59:54 +08:00
|
|
|
if (ev->event_handler(ev) == NGX_ERROR)
|
2002-08-07 00:39:45 +08:00
|
|
|
ev->close_handler(ev);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ngx_assert(0, /* void */, log,
|
2002-09-13 22:47:42 +08:00
|
|
|
"unknown kevent filter %d" _ event_list[i].filter);
|
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-09-02 22:48:24 +08:00
|
|
|
void ngx_kqueue_add_timer(ngx_event_t *ev, ngx_msec_t timer)
|
2002-08-07 00:39:45 +08:00
|
|
|
{
|
|
|
|
ngx_event_t *e;
|
|
|
|
|
2002-09-02 22:48:24 +08:00
|
|
|
ngx_log_debug(ev->log, "set timer: %d" _ timer);
|
|
|
|
|
2002-09-12 22:42:29 +08:00
|
|
|
ngx_assert((!ev->timer_next && !ev->timer_prev), return, ev->log,
|
|
|
|
"timer already set");
|
|
|
|
|
2002-08-07 00:39:45 +08:00
|
|
|
for (e = timer_queue.timer_next;
|
|
|
|
e != &timer_queue && timer > e->timer_delta;
|
|
|
|
e = e->timer_next)
|
|
|
|
timer -= e->timer_delta;
|
|
|
|
|
|
|
|
ev->timer_delta = timer;
|
|
|
|
|
|
|
|
ev->timer_next = e;
|
|
|
|
ev->timer_prev = e->timer_prev;
|
|
|
|
|
|
|
|
e->timer_prev->timer_next = ev;
|
|
|
|
e->timer_prev = ev;
|
|
|
|
}
|