modified IOMultiplexer (added epoll & kevent support)
[TransparentIRC.git] / src / IOEngine_select.c
index e37b10496651c5ece864c18a07c6ff97f898a317..8303308f3876f2dcb68a85f76ed36f63987c5f58 100644 (file)
@@ -57,25 +57,24 @@ static void engine_select_loop(struct timeval *timeout) {
             if(iohandler_wants_writes(iofd))
                 FD_SET(iofd->fd, &write_fds);
         }
-        if(iofd->type == IOTYPE_TIMER || iofd->timeout.tv_sec || iofd->timeout.tv_usec) {
-            tdiff.tv_sec = iofd->timeout.tv_sec - now.tv_sec;
-            tdiff.tv_usec = iofd->timeout.tv_usec - now.tv_usec;
-            
-            if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
-                //exec timer
-                iohandler_events(iofd, 0, 0);
-                if(iofd->type == IOTYPE_TIMER)
-                    iohandler_close(iofd);
-                continue;
-            } else if(tdiff.tv_usec < 0) {
-                tdiff.tv_sec--;
-                tdiff.tv_usec += 1000000; //1 sec
-            }
-            if(timeval_is_smaler((&tdiff), timeout)) {
-                timeout->tv_sec = tdiff.tv_sec;
-                timeout->tv_usec = tdiff.tv_usec;
-            }
+    }
+    
+    while(timer_priority) {
+        tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
+        tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
+        if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
+            iohandler_events(timer_priority, 0, 0);
+            iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+            continue;
+        } else if(tdiff.tv_usec < 0) {
+            tdiff.tv_sec--;
+            tdiff.tv_usec += 1000000; //1 sec
+        }
+        if(timeval_is_smaler((&tdiff), timeout)) {
+            timeout->tv_sec = tdiff.tv_sec;
+            timeout->tv_usec = tdiff.tv_usec;
         }
+        break;
     }
     
     //select system call
@@ -83,7 +82,7 @@ static void engine_select_loop(struct timeval *timeout) {
     
     if (select_result < 0) {
         if (errno != EINTR) {
-            //hard fail
+            iohandler_log(IOLOG_FATAL, "select() failed with errno %d: %s", errno, strerror(errno));
             return;
         }
     }
@@ -99,19 +98,20 @@ static void engine_select_loop(struct timeval *timeout) {
                 continue;
             }
         }
-        if(iofd->type == IOTYPE_TIMER || iofd->timeout.tv_sec || iofd->timeout.tv_usec) {
-            tdiff.tv_sec = iofd->timeout.tv_sec - now.tv_sec;
-            tdiff.tv_usec = iofd->timeout.tv_usec - now.tv_usec;
-            
-            if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
-                //exec timer
-                iohandler_events(iofd, 0, 0);
-                if(iofd->type == IOTYPE_TIMER)
-                    iohandler_close(iofd);
-                continue;
-            }
+    }
+    
+    //check timers
+    while(timer_priority) {
+        tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
+        tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
+        if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
+            iohandler_events(timer_priority, 0, 0);
+            iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+            continue;
         }
+        break;
     }
+    
 }
 
 static void engine_select_cleanup() {