Added kqueue support in spdyd.

This commit is contained in:
Tatsuhiro Tsujikawa 2012-02-10 02:06:46 +09:00
parent bc8b7212b2
commit 6d35f7e470
6 changed files with 200 additions and 12 deletions

View File

@ -77,11 +77,6 @@ if test "x${have_cunit}" = "xyes"; then
fi fi
AM_CONDITIONAL([HAVE_CUNIT], [ test "x${have_cunit}" = "xyes" ]) AM_CONDITIONAL([HAVE_CUNIT], [ test "x${have_cunit}" = "xyes" ])
AX_HAVE_EPOLL([have_epoll=yes], [have_epoll=no])
if test "x${have_epoll}" = "xyes"; then
AC_DEFINE([HAVE_EPOLL], [1], [Define to 1 if you have the `epoll`.])
fi
AM_CONDITIONAL([HAVE_EPOLL], [ test "x${have_epoll}" = "xyes" ])
# openssl (for examples) # openssl (for examples)
PKG_CHECK_MODULES([OPENSSL], [openssl >= 1.0.1]) PKG_CHECK_MODULES([OPENSSL], [openssl >= 1.0.1])
@ -114,6 +109,15 @@ AC_CHECK_FUNCS([ \
memset \ memset \
]) ])
AX_HAVE_EPOLL([have_epoll=yes], [have_epoll=no])
if test "x${have_epoll}" = "xyes"; then
AC_DEFINE([HAVE_EPOLL], [1], [Define to 1 if you have the `epoll`.])
fi
AM_CONDITIONAL([HAVE_EPOLL], [ test "x${have_epoll}" = "xyes" ])
AC_CHECK_FUNCS([kqueue], [have_kqueue=yes])
AM_CONDITIONAL([HAVE_KQUEUE], [test "x${have_kqueue}" = "xyes"])
AC_CONFIG_FILES([ AC_CONFIG_FILES([
Makefile Makefile
lib/Makefile lib/Makefile

View File

@ -31,6 +31,8 @@
#ifdef HAVE_EPOLL #ifdef HAVE_EPOLL
# include "EventPoll_epoll.h" # include "EventPoll_epoll.h"
#endif // HAVE_EPOLL #elif HAVE_KQUEUE
# include "EventPoll_kqueue.h"
#endif // HAVE_KQUEUE
#endif // EVENT_POLL_H #endif // EVENT_POLL_H

View File

@ -0,0 +1,113 @@
/*
* Spdylay - SPDY Library
*
* Copyright (c) 2012 Tatsuhiro Tsujikawa
*
* 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 "EventPoll_kqueue.h"
#include <unistd.h>
#include <cerrno>
#include <cassert>
namespace spdylay {
EventPoll::EventPoll(size_t max_events)
: max_events_(max_events), num_events_(0)
{
kq_ = kqueue();
assert(kq_ != -1);
evlist_ = new struct kevent[max_events_];
}
EventPoll::~EventPoll()
{
if(kq_ != -1) {
close(kq_);
}
delete [] evlist_;
}
int EventPoll::poll(int timeout)
{
timespec ts, *ts_ptr;
if(timeout == -1) {
ts_ptr = 0;
} else {
ts.tv_sec = timeout/1000;
ts.tv_nsec = (timeout%1000)*1000000;
ts_ptr = &ts;
}
num_events_ = 0;
int n;
while((n = kevent(kq_, evlist_, 0, evlist_, max_events_, ts_ptr)) == -1 &&
errno == EINTR);
if(n > 0) {
num_events_ = n;
}
return n;
}
int EventPoll::get_num_events()
{
return num_events_;
}
void* EventPoll::get_user_data(size_t p)
{
return evlist_[p].udata;
}
int EventPoll::get_events(size_t p)
{
int events = 0;
short filter = evlist_[p].filter;
if(filter == EVFILT_READ) {
events |= EP_POLLIN;
}
if(filter == EVFILT_WRITE) {
events |= EP_POLLOUT;
}
return events;
}
namespace {
int update_event(int kq, int fd, int events, void *user_data)
{
struct kevent changelist[2];
EV_SET(&changelist[0], fd, EVFILT_READ,
EV_ADD | ((events & EP_POLLIN) ? EV_ENABLE : EV_DISABLE),
0, 0, user_data);
EV_SET(&changelist[1], fd, EVFILT_WRITE,
EV_ADD | ((events & EP_POLLOUT) ? EV_ENABLE : EV_DISABLE),
0, 0, user_data);
timespec ts = { 0, 0 };
return kevent(kq, changelist, 2, changelist, 0, &ts);
}
} // namespace
int EventPoll::ctl_event(int op, int fd, int events, void *user_data)
{
return update_event(kq_, fd, events, user_data);
}
} // namespace spdylay

View File

@ -0,0 +1,62 @@
/*
* Spdylay - SPDY Library
*
* Copyright (c) 2012 Tatsuhiro Tsujikawa
*
* 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.
*/
#ifndef EVENT_POLL_KQUEUE_H
#define EVENT_POLL_KQUEUE_H
#include <cstdlib>
#include <sys/types.h>
#include <sys/event.h>
#include <sys/time.h>
#include "EventPollEvent.h"
namespace spdylay {
class EventPoll {
public:
EventPoll(size_t max_events);
~EventPoll();
// Returns 0 if this function succeeds, or -1.
// On success
int poll(int timeout);
// Returns number of events detected in previous call of poll().
int get_num_events();
// Returns events of p-eth event.
int get_events(size_t p);
// Returns user data of p-th event.
void* get_user_data(size_t p);
// Adds/Modifies event to watch.
int ctl_event(int op, int fd, int events, void *user_data);
private:
int kq_;
size_t max_events_;
struct kevent *evlist_;
size_t num_events_;
};
} // namespace spdylay
#endif // EVENT_POLL_KQUEUE_H

View File

@ -27,20 +27,24 @@ AM_CPPFLAGS = -I$(srcdir)/../lib/includes -I$(builddir)/../lib/includes \
AM_LDFLAGS = @OPENSSL_LIBS@ AM_LDFLAGS = @OPENSSL_LIBS@
LDADD = $(top_builddir)/lib/libspdylay.la LDADD = $(top_builddir)/lib/libspdylay.la
bin_PROGRAMS = spdycat bin_PROGRAMS = spdycat spdyd
HELPER_OBJECTS = uri.cc util.cc spdylay_ssl.cc HELPER_OBJECTS = uri.cc util.cc spdylay_ssl.cc
HELPER_HFILES = uri.h util.h spdylay_ssl.h HELPER_HFILES = uri.h util.h spdylay_ssl.h
spdycat_SOURCES = ${HELPER_OBJECTS} ${HELPER_HFILES} spdycat.cc spdycat_SOURCES = ${HELPER_OBJECTS} ${HELPER_HFILES} spdycat.cc
if HAVE_EPOLL
spdyd_SOURCES = ${HELPER_OBJECTS} ${HELPER_HFILES} spdyd.cc EventPoll.h \ spdyd_SOURCES = ${HELPER_OBJECTS} ${HELPER_HFILES} spdyd.cc EventPoll.h \
EventPollEvent.h EventPollEvent.h
if HAVE_EPOLL
spdyd_SOURCES += EventPoll_epoll.cc EventPoll_epoll.h spdyd_SOURCES += EventPoll_epoll.cc EventPoll_epoll.h
bin_PROGRAMS += spdyd
endif # HAVE_EPOLL endif # HAVE_EPOLL
if HAVE_KQUEUE
spdyd_SOURCES += EventPoll_kqueue.cc EventPoll_kqueue.h
endif # HAVE_KQUEUE

View File

@ -169,7 +169,10 @@ int make_listen_socket(uint16_t port)
memset(&hints, 0, sizeof(addrinfo)); memset(&hints, 0, sizeof(addrinfo));
hints.ai_family = AF_UNSPEC; hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_ADDRCONFIG | AI_PASSIVE; hints.ai_flags = AI_PASSIVE;
#ifdef AI_ADDRCONFIG
hints.ai_flags |= AI_ADDRCONFIG;
#endif // AI_ADDRCONFIG
addrinfo *res, *rp; addrinfo *res, *rp;
r = getaddrinfo(0, service, &hints, &res); r = getaddrinfo(0, service, &hints, &res);
if(r != 0) { if(r != 0) {