Browse Source

Use std::set for socketEntries_ and nameResolverEntries_ in KqueueEventPoll

Tatsuhiro Tsujikawa 13 years ago
parent
commit
d8948056b3
2 changed files with 21 additions and 24 deletions
  1. 12 21
      src/KqueueEventPoll.cc
  2. 9 3
      src/KqueueEventPoll.h

+ 12 - 21
src/KqueueEventPoll.cc

@@ -141,9 +141,8 @@ void KqueueEventPoll::poll(const struct timeval& tv)
   // 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) {
+  for(KAsyncNameResolverEntrySet::iterator i = nameResolverEntries_.begin(),
+        eoi = nameResolverEntries_.end(); i != eoi; ++i) {
     (*i)->processTimeout();
     (*i)->removeSocketEvents(this);
     (*i)->addSocketEvents(this);
@@ -172,9 +171,7 @@ 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,
-                     DerefLess<SharedHandle<KSocketEntry> >());
+  KSocketEntrySet::iterator i = socketEntries_.lower_bound(socketEntry);
   int r = 0;
   struct timespec zeroTimeout = { 0, 0 };
   struct kevent changelist[2];
@@ -213,7 +210,7 @@ bool KqueueEventPoll::addEvents(sock_t socket, Command* command,
 
 #ifdef ENABLE_ASYNC_DNS
 bool KqueueEventPoll::addEvents(sock_t socket, Command* command, int events,
-                               const SharedHandle<AsyncNameResolver>& rs)
+                                const SharedHandle<AsyncNameResolver>& rs)
 {
   return addEvents(socket, KADNSEvent(rs, command, socket, events));
 }
@@ -223,10 +220,11 @@ 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,
-                     DerefLess<SharedHandle<KSocketEntry> >());
-  if(i != socketEntries_.end() && *(*i) == *socketEntry) {
+  KSocketEntrySet::iterator i = socketEntries_.find(socketEntry);
+  if(i == socketEntries_.end()) {
+    A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
+    return false;
+  } else {
     event.removeSelf(*i);
     int r = 0;
     struct timespec zeroTimeout = { 0, 0 };
@@ -244,9 +242,6 @@ bool KqueueEventPoll::deleteEvents(sock_t socket,
     } else {
       return true;
     }
-  } else {
-    A2_LOG_DEBUG(fmt("Socket %d is not found in SocketEntries.", socket));
-    return false;
   }
 }
 
@@ -271,11 +266,9 @@ bool KqueueEventPoll::addNameResolver
 {
   SharedHandle<KAsyncNameResolverEntry> entry
     (new KAsyncNameResolverEntry(resolver, command));
-  std::deque<SharedHandle<KAsyncNameResolverEntry> >::iterator itr =
-    std::find_if(nameResolverEntries_.begin(), nameResolverEntries_.end(),
-                 derefEqual(entry));
+  KAsyncNameResolverEntrySet::iterator itr = nameResolverEntries_.find(entry);
   if(itr == nameResolverEntries_.end()) {
-    nameResolverEntries_.push_back(entry);
+    nameResolverEntries_.insert(entry);
     entry->addSocketEvents(this);
     return true;
   } else {
@@ -288,9 +281,7 @@ bool KqueueEventPoll::deleteNameResolver
 {
   SharedHandle<KAsyncNameResolverEntry> entry
     (new KAsyncNameResolverEntry(resolver, command));
-  std::deque<SharedHandle<KAsyncNameResolverEntry> >::iterator itr =
-    std::find_if(nameResolverEntries_.begin(), nameResolverEntries_.end(),
-                 derefEqual(entry));
+  KAsyncNameResolverEntrySet::iterator itr = nameResolverEntries_.find(entry);
   if(itr == nameResolverEntries_.end()) {
     return false;
   } else {

+ 9 - 3
src/KqueueEventPoll.h

@@ -41,9 +41,10 @@
 #include <sys/event.h>
 #include <sys/time.h>
 
-#include <deque>
+#include <set>
 
 #include "Event.h"
+#include "a2functional.h"
 #ifdef ENABLE_ASYNC_DNS
 # include "AsyncNameResolver.h"
 #endif // ENABLE_ASYNC_DNS
@@ -74,9 +75,14 @@ private:
   friend int accumulateEvent(int events, const KEvent& event);
 
 private:
-  std::deque<SharedHandle<KSocketEntry> > socketEntries_;
+  typedef std::set<SharedHandle<KSocketEntry>,
+                   DerefLess<SharedHandle<KSocketEntry> > > KSocketEntrySet;
+  KSocketEntrySet socketEntries_;
 #ifdef ENABLE_ASYNC_DNS
-  std::deque<SharedHandle<KAsyncNameResolverEntry> > nameResolverEntries_;
+  typedef std::set<SharedHandle<KAsyncNameResolverEntry>,
+                   DerefLess<SharedHandle<KAsyncNameResolverEntry> > >
+  KAsyncNameResolverEntrySet;
+  KAsyncNameResolverEntrySet nameResolverEntries_;
 #endif // ENABLE_ASYNC_DNS
 
   int kqfd_;