Date: Monday, August 28, 2006 @ 16:28:11
  Author: gilles
    Path: /cvsroot/carob/carob

Modified: include/ControllerPinger.hpp (1.1 -> 1.2)
          src/ControllerPinger.cpp (1.1 -> 1.2)

Integrated Bruno Corsi dos Santos' poll patch (modified to fit new 
ControllerPinger class)
Renamed select_timeout to poll_timeout_ms for clarity, fixed select use of this 
variable (was considering ms as µs)


------------------------------+
 include/ControllerPinger.hpp |    2 -
 src/ControllerPinger.cpp     |   71 +++++++++++++++++++++++++++++++++++++----
 2 files changed, 66 insertions(+), 7 deletions(-)


Index: carob/include/ControllerPinger.hpp
diff -u carob/include/ControllerPinger.hpp:1.1 
carob/include/ControllerPinger.hpp:1.2
--- carob/include/ControllerPinger.hpp:1.1      Fri Aug 25 19:56:56 2006
+++ carob/include/ControllerPinger.hpp  Mon Aug 28 16:28:10 2006
@@ -78,7 +78,7 @@
    * Number of milliseconds to wait for the controllers to respond to a ping
    * connection attempt
    */
-  static const int            select_timeout = 20;
+  static const int            poll_timeout_ms = 20;
   /** Prevent creation from the outside */
   ControllerPinger() {};
   /** Prevent destruction from the outside */
Index: carob/src/ControllerPinger.cpp
diff -u carob/src/ControllerPinger.cpp:1.1 carob/src/ControllerPinger.cpp:1.2
--- carob/src/ControllerPinger.cpp:1.1  Fri Aug 25 19:56:56 2006
+++ carob/src/ControllerPinger.cpp      Mon Aug 28 16:28:10 2006
@@ -17,6 +17,9 @@
  *
  * Initial developer(s): Gilles Rayrat
  * Contributor(s): Marc Herbert, Zsolt Simon
+ * #ifdef USING_POLL
+ * Contributors += Bruno Corsi dos Santos
+ * #endif
  */
 
 #include "ControllerPinger.hpp"
@@ -25,6 +28,13 @@
 #include "Common.hpp"
 #include "Connection.hpp"
 
+#ifdef USING_POLL
+#include <sys/poll.h>
+#define POLL_FUNCTION L"Poll"
+#else
+#define POLL_FUNCTION L"Select"
+#endif
+
 #include <vector>
 
 // library-wide objects
@@ -39,7 +49,7 @@
   /** mutex on suspected controllers */
   CarobNS::CriticalSection             suspected_controllers_CS;
   /** # of controllers that have fail since this process has been created */
-  int                         controller_failure_number = 0;
+  int                                   controller_failure_number = 0;
 };
 
 using namespace CarobNS;
@@ -134,17 +144,34 @@
     return;
 
   LockScope scLs(&suspected_controllers_CS);
-  
+
   // list of suspected controllers we can connect to 
   std::vector<SuspectController> suspected_and_connected_controllers;
+#ifdef USING_POLL
+  //create the structures to use poll syscall
+  unsigned int nfds = 0;
+  unsigned int maxnfds = suspected_controllers.size();
+  // Allocate the largest number that nfds can reach
+  struct pollfd *ufds = static_cast<struct pollfd *> (calloc(maxnfds, 
sizeof(struct pollfd)));
+  if(!ufds)
+  {
+    // Couldn't alloc the memory...
+    if (isErrorEnabled())
+    {
+      logError(fctName, L"Calloc of poll fds failed");
+    }
+    return;
+  }
+#else
   //create set of fds for polling
   fd_set writableControllers;
   FD_ZERO(&writableControllers);
   struct timeval tv;
   tv.tv_sec = 0;
-  tv.tv_usec = select_timeout;
+  tv.tv_usec = poll_timeout_ms*1000; // milli -> micro seconds conversion
   // fdMax is *not* the number of fds, but the maximum value of created sockets
   int fdMax = -1;
+#endif
   // return of polling function
   int retVal = 0;
   // Ping each controller and add the ping socket in the set of fds for polling
@@ -152,6 +179,7 @@
       iter != suspected_controllers.end(); iter++)
   {
     SuspectController sc;
+    sc.pingSocketPtr = NULL; // prevents bad deletes 
     try
     {
       if (isDebugEnabled())
@@ -167,9 +195,16 @@
         suspected_and_connected_controllers.push_back(sc);
         // add its socket to the polling set
         int sock = sc.pingSocketPtr->getFd();
+#ifdef USING_POLL
+        ufds[nfds].fd = sock;
+        ufds[nfds].events = POLLOUT;
+        nfds++;
+        // The ufds[nfds].revents is is going to be modified
+#else
         if (sock > fdMax)
           fdMax = sock;
         FD_SET(sock, &writableControllers);
+#endif
       }
     }
     catch (...)
@@ -177,14 +212,18 @@
       delete sc.pingSocketPtr;
     }
   }
+#ifdef USING_POLL
+  retVal = poll(ufds, nfds, poll_timeout_ms);
+#else
   retVal = select(fdMax+1, NULL, &writableControllers, NULL, &tv);
+#endif
   if (retVal == -1)
   {
     if (isErrorEnabled())
     {
-      logError(fctName, L"Select returned error #"+toUserString(errno));
+      logError(fctName, wstring(POLL_FUNCTION) + L" returned error 
#"+toUserString(errno));
     }
-    return;
+    // clean-up will be done a the end of this function
   }
   // if (retVal == 0) => timeout, no controller up again, do nothing
   // If controllers are found up again, let's remove them from the list of
@@ -192,19 +231,39 @@
   if (retVal > 0)
   {
     // iterates through controller list to find the ones that are up again
+#ifdef USING_POLL
+    int i=0;
+#endif
     for (std::vector<SuspectController>::iterator iter = 
suspected_and_connected_controllers.begin();
         iter != suspected_and_connected_controllers.end(); iter++)
     {
+#ifdef USING_POLL
+      if (ufds[i].revents & POLLOUT)
+#else
       if (FD_ISSET(iter->pingSocketPtr->getFd(), &writableControllers))
+#endif
       {
         // send ping command, upon wich controller closes its connection with
         // the client that issued this ping
         iter->pingSocketPtr->writeJavaInt(Ping);
         removeControllerFromSuspectList(iter->controllerInfo);
       }
-      delete iter->pingSocketPtr; //free the ping socket
+#ifdef USING_POLL
+      i++;
+#endif
     }
   }
+
+  // clean-up
+  for (std::vector<SuspectController>::iterator iter = 
suspected_and_connected_controllers.begin();
+    iter != suspected_and_connected_controllers.end(); iter++)
+  {
+    delete iter->pingSocketPtr;
+  }
+  suspected_and_connected_controllers.clear();
+#ifdef USING_POLL
+  free(ufds);
+#endif
 }
 
 /*static*/

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to