Added SILC Server library.
[silc.git] / lib / silcutil / silcschedule.c
index c7ce66dcc190ef795fbfb96ebc93db1c18476345..8c5cfd66bc361af4b2feb589d5c1328c81daae5c 100644 (file)
@@ -18,7 +18,7 @@
 */
 /* $Id$ */
 
-#include "silcincludes.h"
+#include "silc.h"
 
 /* Platform specific implementation */
 extern const SilcScheduleOps schedule_ops;
@@ -331,17 +331,17 @@ SilcBool silc_schedule_one(SilcSchedule schedule, int timeout_usecs)
   ret = schedule_ops.select(schedule, schedule->internal);
 
   switch (ret) {
+  case 0:
+    /* Timeout */
+    SILC_LOG_DEBUG(("Running timeout tasks"));
+    silc_schedule_dispatch_timeout(schedule, FALSE);
+    break;
   case -1:
     /* Error */
     if (errno == EINTR)
       break;
     SILC_LOG_ERROR(("Error in select(): %s", strerror(errno)));
     break;
-  case 0:
-    /* Timeout */
-    SILC_LOG_DEBUG(("Running timeout tasks"));
-    silc_schedule_dispatch_timeout(schedule, FALSE);
-    break;
   default:
     /* There is some data available now */
     SILC_LOG_DEBUG(("Running fd tasks"));
@@ -424,8 +424,6 @@ SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
     if (!ttask)
       goto out;
 
-    SILC_LOG_DEBUG(("Registering new timeout task %p", ttask));
-
     ttask->header.type = 1;
     ttask->header.callback = callback;
     ttask->header.context = context;
@@ -436,12 +434,15 @@ SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
       silc_gettimeofday(&ttask->timeout);
       ttask->timeout.tv_sec += seconds + (useconds / 1000000L);
       ttask->timeout.tv_usec += (useconds % 1000000L);
-      if (ttask->timeout.tv_usec > 999999L) {
+      if (ttask->timeout.tv_usec >= 1000000L) {
        ttask->timeout.tv_sec += 1;
        ttask->timeout.tv_usec -= 1000000L;
       }
     }
 
+    SILC_LOG_DEBUG(("New timeout task %p: sec=%d, usec=%d", ttask,
+                   seconds, useconds));
+
     /* Add task to correct spot so that the first task in the list has
        the earliest timeout. */
     silc_list_start(schedule->timeout_queue);
@@ -461,7 +462,7 @@ SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
   } else {
     /* Check if fd is already added */
     if (silc_hash_table_find(schedule->fd_queue, SILC_32_TO_PTR(fd),
-                            NULL, NULL))
+                            NULL, (void **)&task))
       goto out;
 
     /* Check max tasks */
@@ -475,7 +476,7 @@ SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
     if (!ftask)
       goto out;
 
-    SILC_LOG_DEBUG(("Registering new fd task %p fd=%d", ftask, fd));
+    SILC_LOG_DEBUG(("New fd task %p fd=%d", ftask, fd));
 
     ftask->header.type = 0;
     ftask->header.callback = callback;