Browse Source

2010-04-24 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Added kqueue support. We use poll() for
	SocketCore::isReadable()/isWritable() when kqueue is used.
	* configure.ac
	* src/DownloadEngineFactory.cc
	* src/KqueueEventPoll.cc
	* src/KqueueEventPoll.h
	* src/Makefile.am
	* src/Makefile.in
	* src/OptionHandlerFactory.cc
	* src/main.cc
	* src/prefs.cc
	* src/prefs.h
Tatsuhiro Tsujikawa 15 years ago
parent
commit
5d636df361
13 changed files with 582 additions and 19 deletions
  1. 15 0
      ChangeLog
  2. 6 0
      config.h.in
  3. 61 0
      configure
  4. 20 0
      configure.ac
  5. 28 14
      src/DownloadEngineFactory.cc
  6. 294 0
      src/KqueueEventPoll.cc
  7. 134 0
      src/KqueueEventPoll.h
  8. 4 0
      src/Makefile.am
  9. 8 5
      src/Makefile.in
  10. 5 0
      src/OptionHandlerFactory.cc
  11. 5 0
      src/main.cc
  12. 1 0
      src/prefs.cc
  13. 1 0
      src/prefs.h

+ 15 - 0
ChangeLog

@@ -1,3 +1,18 @@
+2010-04-24  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	Added kqueue support. We use poll() for
+	SocketCore::isReadable()/isWritable() when kqueue is used.
+	* configure.ac
+	* src/DownloadEngineFactory.cc
+	* src/KqueueEventPoll.cc
+	* src/KqueueEventPoll.h
+	* src/Makefile.am
+	* src/Makefile.in
+	* src/OptionHandlerFactory.cc
+	* src/main.cc
+	* src/prefs.cc
+	* src/prefs.h
+
 2010-04-23  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	Added missing timespec.h to SRCS

+ 6 - 0
config.h.in

@@ -205,6 +205,9 @@
 /* Define to 1 if you have the <io.h> header file. */
 #undef HAVE_IO_H
 
+/* Define to 1 if you have the `kqueue' function. */
+#undef HAVE_KQUEUE
+
 /* Define if you have <langinfo.h> and nl_langinfo(CODESET). */
 #undef HAVE_LANGINFO_CODESET
 
@@ -557,6 +560,9 @@
 /* Define if integer division by zero raises signal SIGFPE. */
 #undef INTDIV0_RAISES_SIGFPE
 
+/* Define to 1 if struct kevent.udata is intptr_t */
+#undef KEVENT_UDATA_INTPTR_T
+
 /* Define to 1 if `lstat' dereferences a symlink specified with a trailing
    slash. */
 #undef LSTAT_FOLLOWS_SLASHED_SYMLINK

+ 61 - 0
configure

@@ -600,6 +600,8 @@ ac_func_list=
 ac_subst_vars='am__EXEEXT_FALSE
 am__EXEEXT_TRUE
 LTLIBOBJS
+HAVE_KQUEUE_FALSE
+HAVE_KQUEUE_TRUE
 HAVE_PORT_ASSOCIATE_FALSE
 HAVE_PORT_ASSOCIATE_TRUE
 HAVE_TIMEGETTIME_FALSE
@@ -15171,6 +15173,61 @@ else
 fi
 
 
+for ac_func in kqueue
+do :
+  ac_fn_cxx_check_func "$LINENO" "kqueue" "ac_cv_func_kqueue"
+if test "x$ac_cv_func_kqueue" = x""yes; then :
+  cat >>confdefs.h <<_ACEOF
+#define HAVE_KQUEUE 1
+_ACEOF
+ have_kqueue=yes
+fi
+done
+
+ if test "x$have_kqueue" = "xyes"; then
+  HAVE_KQUEUE_TRUE=
+  HAVE_KQUEUE_FALSE='#'
+else
+  HAVE_KQUEUE_TRUE='#'
+  HAVE_KQUEUE_FALSE=
+fi
+
+if test "x$have_kqueue" = "xyes"; then
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether struct kevent.udata is intptr_t" >&5
+$as_echo_n "checking whether struct kevent.udata is intptr_t... " >&6; }
+    cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/time.h>
+
+int
+main ()
+{
+
+struct kevent event;
+event.udata = reinterpret_cast<intptr_t>(&event);
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_cxx_try_compile "$LINENO"; then :
+  kevent_udata_intptr_t=yes
+else
+  kevent_udata_intptr_t=no
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+    { $as_echo "$as_me:${as_lineno-$LINENO}: result: $kevent_udata_intptr_t" >&5
+$as_echo "$kevent_udata_intptr_t" >&6; }
+    if test "x$kevent_udata_intptr_t" = "xyes"; then
+
+$as_echo "#define KEVENT_UDATA_INTPTR_T 1" >>confdefs.h
+
+    fi
+fi
+
 ac_fn_cxx_check_member "$LINENO" "struct sockaddr_in" "sin_len" "ac_cv_member_struct_sockaddr_in_sin_len" "#include <netinet/in.h>
 "
 if test "x$ac_cv_member_struct_sockaddr_in_sin_len" = x""yes; then :
@@ -15525,6 +15582,10 @@ if test -z "${HAVE_PORT_ASSOCIATE_TRUE}" && test -z "${HAVE_PORT_ASSOCIATE_FALSE
   as_fn_error "conditional \"HAVE_PORT_ASSOCIATE\" was never defined.
 Usually this means the macro was only invoked conditionally." "$LINENO" 5
 fi
+if test -z "${HAVE_KQUEUE_TRUE}" && test -z "${HAVE_KQUEUE_FALSE}"; then
+  as_fn_error "conditional \"HAVE_KQUEUE\" was never defined.
+Usually this means the macro was only invoked conditionally." "$LINENO" 5
+fi
 
 : ${CONFIG_STATUS=./config.status}
 ac_write_fail=0

+ 20 - 0
configure.ac

@@ -417,6 +417,26 @@ esac
 AC_CHECK_FUNCS([port_associate], [have_port_associate=yes])
 AM_CONDITIONAL([HAVE_PORT_ASSOCIATE], [test "x$have_port_associate" = "xyes"])
 
+AC_CHECK_FUNCS([kqueue], [have_kqueue=yes])
+AM_CONDITIONAL([HAVE_KQUEUE], [test "x$have_kqueue" = "xyes"])
+if test "x$have_kqueue" = "xyes"; then
+    AC_MSG_CHECKING([whether struct kevent.udata is intptr_t])
+    AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/time.h>
+]],
+[[
+struct kevent event;
+event.udata = reinterpret_cast<intptr_t>(&event);
+]])],
+    [kevent_udata_intptr_t=yes], [kevent_udata_intptr_t=no])
+    AC_MSG_RESULT([$kevent_udata_intptr_t])
+    if test "x$kevent_udata_intptr_t" = "xyes"; then
+       AC_DEFINE([KEVENT_UDATA_INTPTR_T], [1], [Define to 1 if struct kevent.udata is intptr_t])
+    fi
+fi
+
 AC_CHECK_MEMBER([struct sockaddr_in.sin_len],
                 [AC_DEFINE([HAVE_SOCKADDR_IN_SIN_LEN],[1],
                   [Define to 1 if struct sockaddr_in has sin_len member.])],

+ 28 - 14
src/DownloadEngineFactory.cc

@@ -64,6 +64,9 @@
 #ifdef HAVE_PORT_ASSOCIATE
 # include "PortEventPoll.h"
 #endif // HAVE_PORT_ASSOCIATE
+#ifdef HAVE_KQUEUE
+# include "KqueueEventPoll.h"
+#endif // HAVE_KQUEUE
 #include "PollEventPoll.h"
 #include "SelectEventPoll.h"
 #include "DlAbortEx.h"
@@ -96,27 +99,38 @@ DownloadEngineFactory::newDownloadEngine
     }
   } else
 #endif // HAVE_EPLL
-#ifdef HAVE_PORT_ASSOCIATE
-    if(pollMethod == V_PORT) {
-      SharedHandle<PortEventPoll> pp(new PortEventPoll());
-      if(pp->good()) {
-        eventPoll = pp;
+#ifdef HAVE_KQUEUE
+    if(pollMethod == V_KQUEUE) {
+      SharedHandle<KqueueEventPoll> kp(new KqueueEventPoll());
+      if(kp->good()) {
+        eventPoll = kp;
       } else {
-        throw DL_ABORT_EX("Initializing PortEventPoll failed."
+        throw DL_ABORT_EX("Initializing KqueueEventPoll failed."
                           " Try --event-poll=select");
       }
     } else
+#endif // HAVE_KQUEUE
+#ifdef HAVE_PORT_ASSOCIATE
+      if(pollMethod == V_PORT) {
+        SharedHandle<PortEventPoll> pp(new PortEventPoll());
+        if(pp->good()) {
+          eventPoll = pp;
+        } else {
+          throw DL_ABORT_EX("Initializing PortEventPoll failed."
+                            " Try --event-poll=select");
+        }
+      } else
 #endif // HAVE_PORT_ASSOCIATE
 #ifdef HAVE_POLL
-      if(pollMethod == V_POLL) {
-        eventPoll.reset(new PollEventPoll());
-      } else
+        if(pollMethod == V_POLL) {
+          eventPoll.reset(new PollEventPoll());
+        } else
 #endif // HAVE_POLL
-        if(pollMethod == V_SELECT) {
-          eventPoll.reset(new SelectEventPoll());
-        } else {
-          abort();
-        }
+          if(pollMethod == V_SELECT) {
+            eventPoll.reset(new SelectEventPoll());
+          } else {
+            abort();
+          }
   DownloadEngineHandle e(new DownloadEngine(eventPoll));
   e->option = op;
 

+ 294 - 0
src/KqueueEventPoll.cc

@@ -0,0 +1,294 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2010 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "KqueueEventPoll.h"
+
+#include <cstring>
+#include <algorithm>
+#include <numeric>
+
+#include "Command.h"
+#include "LogFactory.h"
+#include "Logger.h"
+
+#ifdef KEVENT_UDATA_INTPTR_T
+# define PTR_TO_UDATA(X) (reinterpret_cast<intptr_t>(X))
+#else // !KEVENT_UDATA_INTPTR_T
+# define PTR_TO_UDATA(X) (X)
+#endif // !KEVENT_UDATA_INTPTR_T
+
+namespace aria2 {
+
+KqueueEventPoll::KSocketEntry::KSocketEntry(sock_t s):
+  SocketEntry<KCommandEvent, KADNSEvent>(s) {}
+
+int accumulateEvent(int events, const KqueueEventPoll::KEvent& event)
+{
+  return events|event.getEvents();
+}
+
+size_t KqueueEventPoll::KSocketEntry::getEvents
+(struct kevent* eventlist)
+{
+  int events;
+#ifdef ENABLE_ASYNC_DNS
+  events =
+    std::accumulate(_adnsEvents.begin(),
+                    _adnsEvents.end(),
+                    std::accumulate(_commandEvents.begin(),
+                                    _commandEvents.end(), 0, accumulateEvent),
+                    accumulateEvent);
+#else // !ENABLE_ASYNC_DNS
+  events =
+    std::accumulate(_commandEvents.begin(), _commandEvents.end(), 0,
+                    accumulateEvent);
+#endif // !ENABLE_ASYNC_DNS
+  EV_SET(&eventlist[0], _socket, EVFILT_READ,
+         EV_ADD|((events&KqueueEventPoll::IEV_READ)?EV_ENABLE:EV_DISABLE),
+         0, 0, PTR_TO_UDATA(this));
+  EV_SET(&eventlist[1], _socket, EVFILT_WRITE,
+         EV_ADD|((events&KqueueEventPoll::IEV_WRITE)?EV_ENABLE:EV_DISABLE),
+         0, 0, PTR_TO_UDATA(this));
+  return 2;
+}
+
+KqueueEventPoll::KqueueEventPoll():
+  _kqEventsSize(KQUEUE_EVENTS_MAX),
+  _kqEvents(new struct kevent[_kqEventsSize]),
+  _logger(LogFactory::getInstance())
+{
+  _kqfd = kqueue();
+}
+
+KqueueEventPoll::~KqueueEventPoll()
+{
+  if(_kqfd != -1) {
+    int r;
+    while((r = close(_kqfd)) == -1 && errno == EINTR);
+    if(r == -1) {
+      _logger->error("Error occurred while closing kqueue file descriptor"
+                     " %d: %s",
+                     _kqfd, strerror(errno));
+    }
+  }
+  delete [] _kqEvents;
+}
+
+bool KqueueEventPoll::good() const
+{
+  return _kqfd != -1;
+}
+
+void KqueueEventPoll::poll(const struct timeval& tv)
+{
+  struct timespec timeout = { tv.tv_sec, tv.tv_usec*1000 };
+  int res;
+  while((res = kevent(_kqfd, _kqEvents, 0, _kqEvents, _kqEventsSize, &timeout))
+        == -1 && errno == EINTR);
+  if(res > 0) {
+    for(int i = 0; i < res; ++i) {
+      KSocketEntry* p = reinterpret_cast<KSocketEntry*>(_kqEvents[i].udata);
+      int events = 0;
+      int filter = _kqEvents[i].filter;
+      if(filter == EVFILT_READ) {
+        events = KqueueEventPoll::IEV_READ;
+      } else if(filter == EVFILT_WRITE) {
+        events = KqueueEventPoll::IEV_WRITE;
+      }
+      p->processEvents(events);
+    }
+  }
+
+#ifdef ENABLE_ASYNC_DNS
+  // It turns out that we have to call ares_process_fd before ares's
+  // own timeout and ares may create new sockets or closes socket in
+  // their API. So we call ares_process_fd for all ares_channel and
+  // re-register their sockets.
+  for(std::deque<SharedHandle<KAsyncNameResolverEntry> >::iterator i =
+        _nameResolverEntries.begin(), eoi = _nameResolverEntries.end();
+      i != eoi; ++i) {
+    (*i)->processTimeout();
+    (*i)->removeSocketEvents(this);
+    (*i)->addSocketEvents(this);
+  }
+#endif // ENABLE_ASYNC_DNS
+
+  // TODO timeout of name resolver is determined in Command(AbstractCommand,
+  // DHTEntryPoint...Command)
+}
+
+static int translateEvents(EventPoll::EventType events)
+{
+  int newEvents = 0;
+  if(EventPoll::EVENT_READ&events) {
+    newEvents |= KqueueEventPoll::IEV_READ;
+  }
+  if(EventPoll::EVENT_WRITE&events) {
+    newEvents |= KqueueEventPoll::IEV_WRITE;
+  }
+  return newEvents;
+}
+
+bool KqueueEventPoll::addEvents
+(sock_t socket, const KqueueEventPoll::KEvent& event)
+{
+  SharedHandle<KSocketEntry> socketEntry(new KSocketEntry(socket));
+  std::deque<SharedHandle<KSocketEntry> >::iterator i =
+    std::lower_bound(_socketEntries.begin(), _socketEntries.end(), socketEntry);
+  int r = 0;
+  struct timespec zeroTimeout = { 0, 0 };
+  struct kevent changelist[2];
+  size_t n;
+  if(i != _socketEntries.end() && (*i) == socketEntry) {
+    event.addSelf(*i);
+    n = (*i)->getEvents(changelist);
+  } else {
+    _socketEntries.insert(i, socketEntry);
+    if(_socketEntries.size() > _kqEventsSize) {
+      _kqEventsSize *= 2;
+      delete [] _kqEvents;
+      _kqEvents = new struct kevent[_kqEventsSize];
+    }
+    event.addSelf(socketEntry);
+    n = socketEntry->getEvents(changelist);
+  }
+  r = kevent(_kqfd, changelist, n, changelist, 0, &zeroTimeout);
+  if(r == -1) {
+    if(_logger->debug()) {
+      _logger->debug("Failed to add socket event %d:%s",
+                     socket, strerror(errno));
+    }
+    return false;
+  } else {
+    return true;
+  }
+}
+
+bool KqueueEventPoll::addEvents(sock_t socket, Command* command,
+                               EventPoll::EventType events)
+{
+  int kqEvents = translateEvents(events);
+  return addEvents(socket, KCommandEvent(command, kqEvents));
+}
+
+#ifdef ENABLE_ASYNC_DNS
+bool KqueueEventPoll::addEvents(sock_t socket, Command* command, int events,
+                               const SharedHandle<AsyncNameResolver>& rs)
+{
+  return addEvents(socket, KADNSEvent(rs, command, socket, events));
+}
+#endif // ENABLE_ASYNC_DNS
+
+bool KqueueEventPoll::deleteEvents(sock_t socket,
+                                  const KqueueEventPoll::KEvent& event)
+{
+  SharedHandle<KSocketEntry> socketEntry(new KSocketEntry(socket));
+  std::deque<SharedHandle<KSocketEntry> >::iterator i =
+    std::lower_bound(_socketEntries.begin(), _socketEntries.end(), socketEntry);
+  if(i != _socketEntries.end() && (*i) == socketEntry) {
+    event.removeSelf(*i);
+    int r = 0;
+    struct timespec zeroTimeout = { 0, 0 };
+    struct kevent changelist[2];
+    size_t n = (*i)->getEvents(changelist);
+    r = kevent(_kqfd, changelist, n, changelist, 0, &zeroTimeout);
+    if((*i)->eventEmpty()) {
+      _socketEntries.erase(i);
+    }
+    if(r == -1) {
+      if(_logger->debug()) {
+        _logger->debug("Failed to delete socket event:%s", strerror(errno));
+      }
+      return false;
+    } else {
+      return true;
+    }
+  } else {
+    if(_logger->debug()) {
+      _logger->debug("Socket %d is not found in SocketEntries.", socket);
+    }
+    return false;
+  }
+}
+
+#ifdef ENABLE_ASYNC_DNS
+bool KqueueEventPoll::deleteEvents(sock_t socket, Command* command,
+                                  const SharedHandle<AsyncNameResolver>& rs)
+{
+  return deleteEvents(socket, KADNSEvent(rs, command, socket, 0));
+}
+#endif // ENABLE_ASYNC_DNS
+
+bool KqueueEventPoll::deleteEvents(sock_t socket, Command* command,
+                                  EventPoll::EventType events)
+{
+  int kqEvents = translateEvents(events);
+  return deleteEvents(socket, KCommandEvent(command, kqEvents));
+}
+
+#ifdef ENABLE_ASYNC_DNS
+bool KqueueEventPoll::addNameResolver
+(const SharedHandle<AsyncNameResolver>& resolver, Command* command)
+{
+  SharedHandle<KAsyncNameResolverEntry> entry
+    (new KAsyncNameResolverEntry(resolver, command));
+  std::deque<SharedHandle<KAsyncNameResolverEntry> >::iterator itr =
+    std::find(_nameResolverEntries.begin(), _nameResolverEntries.end(), entry);
+  if(itr == _nameResolverEntries.end()) {
+    _nameResolverEntries.push_back(entry);
+    entry->addSocketEvents(this);
+    return true;
+  } else {
+    return false;
+  }
+}
+
+bool KqueueEventPoll::deleteNameResolver
+(const SharedHandle<AsyncNameResolver>& resolver, Command* command)
+{
+  SharedHandle<KAsyncNameResolverEntry> entry
+    (new KAsyncNameResolverEntry(resolver, command));
+  std::deque<SharedHandle<KAsyncNameResolverEntry> >::iterator itr =
+    std::find(_nameResolverEntries.begin(), _nameResolverEntries.end(), entry);
+  if(itr == _nameResolverEntries.end()) {
+    return false;
+  } else {
+    (*itr)->removeSocketEvents(this);
+    _nameResolverEntries.erase(itr);
+    return true;
+  }
+}
+#endif // ENABLE_ASYNC_DNS
+
+} // namespace aria2

+ 134 - 0
src/KqueueEventPoll.h

@@ -0,0 +1,134 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2010 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#ifndef _D_KQUEUE_EVENT_POLL_H_
+#define _D_KQUEUE_EVENT_POLL_H_
+
+#include "EventPoll.h"
+
+#include <sys/types.h>
+#include <sys/event.h>
+#include <sys/time.h>
+
+#include <deque>
+
+#include "Event.h"
+#ifdef ENABLE_ASYNC_DNS
+# include "AsyncNameResolver.h"
+#endif // ENABLE_ASYNC_DNS
+
+namespace aria2 {
+
+class Logger;
+
+class KqueueEventPoll : public EventPoll {
+private:
+  class KSocketEntry;
+
+  typedef Event<KSocketEntry> KEvent;
+
+  typedef CommandEvent<KSocketEntry, KqueueEventPoll> KCommandEvent;
+  typedef ADNSEvent<KSocketEntry, KqueueEventPoll> KADNSEvent;
+  typedef AsyncNameResolverEntry<KqueueEventPoll> KAsyncNameResolverEntry;
+  friend class AsyncNameResolverEntry<KqueueEventPoll>;
+
+  class KSocketEntry:
+    public SocketEntry<KCommandEvent, KADNSEvent> {
+  public:
+    KSocketEntry(sock_t socket);
+
+    // eventlist should be at least size 2.  This function returns the
+    // number of filled struct kevent in eventlist.
+    size_t getEvents(struct kevent* eventlist);
+  };
+
+  friend int accumulateEvent(int events, const KEvent& event);
+
+private:
+  std::deque<SharedHandle<KSocketEntry> > _socketEntries;
+#ifdef ENABLE_ASYNC_DNS
+  std::deque<SharedHandle<KAsyncNameResolverEntry> > _nameResolverEntries;
+#endif // ENABLE_ASYNC_DNS
+
+  int _kqfd;
+
+  size_t _kqEventsSize;
+
+  struct kevent* _kqEvents;
+
+  static const size_t KQUEUE_EVENTS_MAX = 1024;
+
+  Logger* _logger;
+
+  bool addEvents(sock_t socket, const KEvent& event);
+
+  bool deleteEvents(sock_t socket, const KEvent& event);
+
+  bool addEvents(sock_t socket, Command* command, int events,
+                 const SharedHandle<AsyncNameResolver>& rs);
+
+  bool deleteEvents(sock_t socket, Command* command,
+                    const SharedHandle<AsyncNameResolver>& rs);
+
+public:
+  KqueueEventPoll();
+
+  bool good() const;
+
+  virtual ~KqueueEventPoll();
+
+  virtual void poll(const struct timeval& tv);
+
+  virtual bool addEvents(sock_t socket,
+                         Command* command, EventPoll::EventType events);
+
+  virtual bool deleteEvents(sock_t socket,
+                            Command* command, EventPoll::EventType events);
+#ifdef ENABLE_ASYNC_DNS
+
+  virtual bool addNameResolver(const SharedHandle<AsyncNameResolver>& resolver,
+                               Command* command);
+  virtual bool deleteNameResolver
+  (const SharedHandle<AsyncNameResolver>& resolver, Command* command);
+#endif // ENABLE_ASYNC_DNS
+
+  static const int IEV_READ = POLLIN;
+  static const int IEV_WRITE = POLLOUT;
+  static const int IEV_ERROR = POLLERR;
+  static const int IEV_HUP = POLLHUP;
+};
+
+} // namespace aria2
+
+#endif // _D_KQUEUE_EVENT_POLL_H_

+ 4 - 0
src/Makefile.am

@@ -535,6 +535,10 @@ if HAVE_PORT_ASSOCIATE
 SRCS += PortEventPoll.cc PortEventPoll.h
 endif # HAVE_PORT_ASSOCIATE
 
+if HAVE_KQUEUE
+SRCS += KqueueEventPoll.cc KqueueEventPoll.h
+endif # HAVE_KQUEUE
+
 noinst_LIBRARIES = libaria2c.a
 libaria2c_a_SOURCES = $(SRCS)
 aria2c_LDADD = libaria2c.a @LIBINTL@ @ALLOCA@ @LIBGNUTLS_LIBS@\

+ 8 - 5
src/Makefile.in

@@ -281,6 +281,7 @@ bin_PROGRAMS = aria2c$(EXEEXT)
 @HAVE_MACH_ABSOLUTE_TIME_TRUE@am__append_28 = clock_gettime_osx.cc clock_gettime_osx.h
 @HAVE_POLL_TRUE@am__append_29 = PollEventPoll.cc PollEventPoll.h
 @HAVE_PORT_ASSOCIATE_TRUE@am__append_30 = PortEventPoll.cc PortEventPoll.h
+@HAVE_KQUEUE_TRUE@am__append_31 = KqueueEventPoll.cc KqueueEventPoll.h
 subdir = src
 DIST_COMMON = $(srcdir)/Makefile.am $(srcdir)/Makefile.in alloca.c
 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
@@ -609,7 +610,7 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
 	timegm.c timegm.h daemon.cc daemon.h clock_gettime_mingw.cc \
 	clock_gettime_mingw.h clock_gettime_osx.cc clock_gettime_osx.h \
 	PollEventPoll.cc PollEventPoll.h PortEventPoll.cc \
-	PortEventPoll.h
+	PortEventPoll.h KqueueEventPoll.cc KqueueEventPoll.h
 @ENABLE_XML_RPC_TRUE@am__objects_1 =  \
 @ENABLE_XML_RPC_TRUE@	XmlRpcRequestParserController.$(OBJEXT) \
 @ENABLE_XML_RPC_TRUE@	XmlRpcRequestParserStateMachine.$(OBJEXT) \
@@ -792,7 +793,8 @@ am__objects_6 =
 @HAVE_MACH_ABSOLUTE_TIME_TRUE@	clock_gettime_osx.$(OBJEXT)
 @HAVE_POLL_TRUE@am__objects_29 = PollEventPoll.$(OBJEXT)
 @HAVE_PORT_ASSOCIATE_TRUE@am__objects_30 = PortEventPoll.$(OBJEXT)
-am__objects_31 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \
+@HAVE_KQUEUE_TRUE@am__objects_31 = KqueueEventPoll.$(OBJEXT)
+am__objects_32 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \
 	AbstractCommand.$(OBJEXT) \
 	InitiateConnectionCommandFactory.$(OBJEXT) \
 	DownloadCommand.$(OBJEXT) \
@@ -880,8 +882,8 @@ am__objects_31 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \
 	$(am__objects_21) $(am__objects_22) $(am__objects_23) \
 	$(am__objects_24) $(am__objects_25) $(am__objects_26) \
 	$(am__objects_27) $(am__objects_28) $(am__objects_29) \
-	$(am__objects_30)
-am_libaria2c_a_OBJECTS = $(am__objects_31)
+	$(am__objects_30) $(am__objects_31)
+am_libaria2c_a_OBJECTS = $(am__objects_32)
 libaria2c_a_OBJECTS = $(am_libaria2c_a_OBJECTS)
 am__installdirs = "$(DESTDIR)$(bindir)"
 PROGRAMS = $(bin_PROGRAMS)
@@ -1218,7 +1220,7 @@ SRCS = Socket.h SocketCore.cc SocketCore.h BinaryStream.h Command.cc \
 	$(am__append_21) $(am__append_22) $(am__append_23) \
 	$(am__append_24) $(am__append_25) $(am__append_26) \
 	$(am__append_27) $(am__append_28) $(am__append_29) \
-	$(am__append_30)
+	$(am__append_30) $(am__append_31)
 noinst_LIBRARIES = libaria2c.a
 libaria2c_a_SOURCES = $(SRCS)
 aria2c_LDADD = libaria2c.a @LIBINTL@ @ALLOCA@ @LIBGNUTLS_LIBS@\
@@ -1503,6 +1505,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/InitiatorMSEHandshakeCommand.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/IteratableChecksumValidator.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/IteratableChunkChecksumValidator.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/KqueueEventPoll.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/LibgnutlsTLSContext.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/LibsslTLSContext.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/LogFactory.Po@am__quote@

+ 5 - 0
src/OptionHandlerFactory.cc

@@ -210,6 +210,9 @@ OptionHandlers OptionHandlerFactory::createOptionHandlers()
 #ifdef HAVE_EPOLL
       V_EPOLL,
 #endif // HAVE_EPOLL
+#ifdef HAVE_KQUEUE
+      V_KQUEUE,
+#endif // HAVE_KQUEUE
 #ifdef HAVE_PORT_ASSOCIATE
       V_PORT,
 #endif // HAVE_PORT_ASSOCIATE
@@ -223,6 +226,8 @@ OptionHandlers OptionHandlerFactory::createOptionHandlers()
                                     TEXT_EVENT_POLL,
 #ifdef HAVE_EPOLL
                                     V_EPOLL,
+#elif HAVE_KQUEUE
+                                    V_KQUEUE,
 #elif HAVE_PORT_ASSOCIATE
                                     V_PORT,
 #else

+ 5 - 0
src/main.cc

@@ -192,6 +192,11 @@ downloadresultcode::RESULT main(int argc, char* argv[])
     SocketCore::useEpoll();
   } else
 #endif // HAVE_EPOLL
+#ifdef HAVE_KQUEUE
+    if(pollMethod == V_KQUEUE) {
+      SocketCore::usePoll();
+    } else
+#endif // HAVE_KQUEUE
 #ifdef HAVE_PORT_ASSOCIATE
     if(pollMethod == V_PORT) {
       SocketCore::usePort();

+ 1 - 0
src/prefs.cc

@@ -150,6 +150,7 @@ const std::string PREF_MAX_FILE_NOT_FOUND("max-file-not-found");
 // value: epoll | select
 const std::string PREF_EVENT_POLL("event-poll");
 const std::string V_EPOLL("epoll");
+const std::string V_KQUEUE("kqueue");
 const std::string V_PORT("port");
 const std::string V_POLL("poll");
 const std::string V_SELECT("select");

+ 1 - 0
src/prefs.h

@@ -154,6 +154,7 @@ extern const std::string PREF_MAX_FILE_NOT_FOUND;
 // value: epoll | select
 extern const std::string PREF_EVENT_POLL;
 extern const std::string V_EPOLL;
+extern const std::string V_KQUEUE;
 extern const std::string V_PORT;
 extern const std::string V_POLL;
 extern const std::string V_SELECT;