Added SILC Server library.
[silc.git] / lib / silcutil / silcschedule.c
index f05902259eae347d379cf46ff0d36d35e8362d6f..8c5cfd66bc361af4b2feb589d5c1328c81daae5c 100644 (file)
@@ -18,7 +18,7 @@
 */
 /* $Id$ */
 
-#include "silcincludes.h"
+#include "silc.h"
 
 /* Platform specific implementation */
 extern const SilcScheduleOps schedule_ops;
@@ -26,7 +26,7 @@ extern const SilcScheduleOps schedule_ops;
 static void silc_schedule_task_remove(SilcSchedule schedule, SilcTask task);
 static void silc_schedule_dispatch_fd(SilcSchedule schedule);
 static void silc_schedule_dispatch_timeout(SilcSchedule schedule,
-                                          bool dispatch_all);
+                                          SilcBool dispatch_all);
 
 /* Fd task hash table destructor */
 
@@ -78,7 +78,7 @@ SilcSchedule silc_schedule_init(int max_tasks, void *app_context)
    scheduler could not be uninitialized. This happens when the scheduler
    is still valid and silc_schedule_stop has not been called. */
 
-bool silc_schedule_uninit(SilcSchedule schedule)
+SilcBool silc_schedule_uninit(SilcSchedule schedule)
 {
   SILC_LOG_DEBUG(("Uninitializing scheduler"));
 
@@ -172,7 +172,7 @@ static void silc_schedule_dispatch_fd(SilcSchedule schedule)
    tasks are removed here. */
 
 static void silc_schedule_dispatch_timeout(SilcSchedule schedule,
-                                          bool dispatch_all)
+                                          SilcBool dispatch_all)
 {
   SilcTask t;
   SilcTaskTimeout task;
@@ -220,7 +220,7 @@ static void silc_schedule_select_timeout(SilcSchedule schedule)
   SilcTask t;
   SilcTaskTimeout task;
   struct timeval curtime;
-  bool dispatch = TRUE;
+  SilcBool dispatch = TRUE;
 
   /* Get the current time */
   silc_gettimeofday(&curtime);
@@ -278,7 +278,7 @@ static void silc_schedule_select_timeout(SilcSchedule schedule)
 
 /* Runs the scheduler once and then returns. */
 
-bool silc_schedule_one(SilcSchedule schedule, int timeout_usecs)
+SilcBool silc_schedule_one(SilcSchedule schedule, int timeout_usecs)
 {
   struct timeval timeout;
   int ret;
@@ -331,17 +331,17 @@ bool 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;
@@ -688,7 +689,7 @@ static void silc_schedule_task_remove(SilcSchedule schedule, SilcTask task)
    descriptor to set different iomasks. */
 
 void silc_schedule_set_listen_fd(SilcSchedule schedule, SilcUInt32 fd,
-                                SilcTaskEvent mask, bool send_events)
+                                SilcTaskEvent mask, SilcBool send_events)
 {
   SilcTaskFd task;