Back to index

php5  5.3.10
kqueue.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Jerome Loyet <jerome@loyet.net>                             |
00016    +----------------------------------------------------------------------+
00017 */
00018 
00019 /* $Id$ */
00020 
00021 #include "../fpm_config.h"
00022 #include "../fpm_events.h"
00023 #include "../fpm.h"
00024 #include "../zlog.h"
00025 
00026 #if HAVE_KQUEUE
00027 
00028 #include <sys/types.h>
00029 #include <sys/event.h>
00030 #include <sys/time.h>
00031 
00032 #include <errno.h>
00033 
00034 static int fpm_event_kqueue_init(int max);
00035 static int fpm_event_kqueue_clean();
00036 static int fpm_event_kqueue_wait(struct fpm_event_queue_s *queue, unsigned long int timeout);
00037 static int fpm_event_kqueue_add(struct fpm_event_s *ev);
00038 static int fpm_event_kqueue_remove(struct fpm_event_s *ev);
00039 
00040 static struct fpm_event_module_s kqueue_module = {
00041        .name = "kqueue",
00042        .support_edge_trigger = 1,
00043        .init = fpm_event_kqueue_init,
00044        .clean = fpm_event_kqueue_clean,
00045        .wait = fpm_event_kqueue_wait,
00046        .add = fpm_event_kqueue_add,
00047        .remove = fpm_event_kqueue_remove, 
00048 };
00049 
00050 static struct kevent *kevents = NULL;
00051 static int nkevents = 0;
00052 static int kfd = 0;
00053 
00054 #endif /* HAVE_KQUEUE */
00055 
00056 /*
00057  * Return the module configuration
00058  */
00059 struct fpm_event_module_s *fpm_event_kqueue_module() /* {{{ */
00060 {
00061 #if HAVE_KQUEUE
00062        return &kqueue_module;
00063 #else
00064        return NULL;
00065 #endif /* HAVE_KQUEUE */
00066 }
00067 /* }}} */
00068 
00069 #if HAVE_KQUEUE
00070 
00071 /*
00072  * init kqueue and stuff
00073  */
00074 static int fpm_event_kqueue_init(int max) /* {{{ */
00075 {
00076        if (max < 1) {
00077               return 0;
00078        }
00079 
00080        kfd = kqueue();
00081        if (kfd < 0) {
00082               zlog(ZLOG_ERROR, "kqueue: unable to initialize");
00083               return -1;
00084        }
00085 
00086        kevents = malloc(sizeof(struct kevent) * max);
00087        if (!kevents) {
00088               zlog(ZLOG_ERROR, "epoll: unable to allocate %d events", max);
00089               return -1;
00090        }
00091 
00092        memset(kevents, 0, sizeof(struct kevent) * max);
00093 
00094        nkevents = max;
00095 
00096        return 0;
00097 }
00098 /* }}} */
00099 
00100 /*
00101  * release kqueue stuff
00102  */
00103 static int fpm_event_kqueue_clean() /* {{{ */
00104 {
00105        if (kevents) {
00106               free(kevents);
00107               kevents = NULL;
00108        }
00109 
00110        nkevents = 0;
00111 
00112        return 0;
00113 }
00114 /* }}} */
00115 
00116 /*
00117  * wait for events or timeout
00118  */
00119 static int fpm_event_kqueue_wait(struct fpm_event_queue_s *queue, unsigned long int timeout) /* {{{ */
00120 {
00121        struct timespec t;
00122        int ret, i;
00123 
00124        /* ensure we have a clean kevents before calling kevent() */
00125        memset(kevents, 0, sizeof(struct kevent) * nkevents);
00126 
00127        /* convert ms to timespec struct */
00128        t.tv_sec = timeout / 1000;
00129        t.tv_nsec = (timeout % 1000) * 1000 * 1000;
00130 
00131        /* wait for incoming event or timeout */
00132        ret = kevent(kfd, NULL, 0, kevents, nkevents, &t);
00133        if (ret == -1) {
00134 
00135               /* trigger error unless signal interrupt */
00136               if (errno != EINTR) {
00137                      zlog(ZLOG_WARNING, "epoll_wait() returns %d", errno);
00138                      return -1;
00139               }
00140        }
00141 
00142        /* fire triggered events */
00143        for (i = 0; i < ret; i++) {
00144               if (kevents[i].udata) {
00145                      struct fpm_event_s *ev = (struct fpm_event_s *)kevents[i].udata;
00146                      fpm_event_fire(ev);
00147                      /* sanity check */
00148                      if (fpm_globals.parent_pid != getpid()) {
00149                             return -2;
00150                      }
00151               }
00152        }
00153 
00154        return ret;
00155 }
00156 /* }}} */
00157 
00158 /*
00159  * Add a FD to to kevent queue
00160  */
00161 static int fpm_event_kqueue_add(struct fpm_event_s *ev) /* {{{ */
00162 {
00163        struct kevent k;
00164        int flags = EV_ADD;
00165 
00166        if (ev->flags & FPM_EV_EDGE) {
00167                      flags = flags | EV_CLEAR;
00168        }
00169 
00170        EV_SET(&k, ev->fd, EVFILT_READ, flags, 0, 0, (void *)ev);
00171 
00172        if (kevent(kfd, &k, 1, NULL, 0, NULL) < 0) {
00173               zlog(ZLOG_ERROR, "kevent: unable to add event");
00174               return -1;
00175        }
00176 
00177        /* mark the event as registered */
00178        ev->index = ev->fd;
00179        return 0;
00180 }
00181 /* }}} */
00182 
00183 /*
00184  * Remove a FD from the kevent queue
00185  */
00186 static int fpm_event_kqueue_remove(struct fpm_event_s *ev) /* {{{ */
00187 {
00188        struct kevent k;
00189        int flags = EV_DELETE;
00190 
00191        if (ev->flags & FPM_EV_EDGE) {
00192                      flags = flags | EV_CLEAR;
00193        }
00194 
00195        EV_SET(&k, ev->fd, EVFILT_READ, flags, 0, 0, (void *)ev);
00196 
00197        if (kevent(kfd, &k, 1, NULL, 0, NULL) < 0) {
00198               zlog(ZLOG_ERROR, "kevent: unable to add event");
00199               return -1;
00200        }
00201 
00202        /* mark the vent as not registered */
00203        ev->index = -1;
00204        return 0;
00205 }
00206 /* }}} */
00207 
00208 #endif /* HAVE_KQUEUE */