Porting Toolkit to Symbian. It should work while some sporadic
[silc.git] / lib / silcutil / silcschedule.c
index 7ffcc51088be6edb0b1f22cd0d60a4b9d13f10d0..6d3f09e1c3d3794f4776ea5211a4115c1492494c 100644 (file)
@@ -106,7 +106,7 @@ static void silc_schedule_dispatch_timeout(SilcSchedule schedule,
     }
 
     /* Execute the task if the timeout has expired */
-    if (!silc_compare_timeval(&task->timeout, &curtime) && !dispatch_all)
+    if (silc_compare_timeval(&task->timeout, &curtime) > 0 && !dispatch_all)
       break;
 
     t->valid = FALSE;
@@ -155,7 +155,7 @@ static void silc_schedule_select_timeout(SilcSchedule schedule)
 
     /* If the timeout is in past, we will run the task and all other
        timeout tasks from the past. */
-    if (silc_compare_timeval(&task->timeout, &curtime) && dispatch) {
+    if (silc_compare_timeval(&task->timeout, &curtime) <= 0 && dispatch) {
       silc_schedule_dispatch_timeout(schedule, FALSE);
       if (silc_unlikely(!schedule->valid))
        return;
@@ -476,9 +476,12 @@ static SilcBool silc_schedule_iterate(SilcSchedule schedule, int timeout_usecs)
       continue;
 
     } else {
-      /* Error */
-      if (silc_likely(errno == EINTR))
+      /* Error or special case handling */
+      if (errno == EINTR)
        continue;
+      if (ret == -2)
+       break;
+
       SILC_LOG_ERROR(("Error in select()/poll(): %s", strerror(errno)));
       continue;
     }
@@ -501,6 +504,7 @@ SilcBool silc_schedule_one(SilcSchedule schedule, int timeout_usecs)
 /* Runs the scheduler and blocks here.  When this returns the scheduler
    has ended. */
 
+#ifndef SILC_SYMBIAN
 void silc_schedule(SilcSchedule schedule)
 {
   SILC_LOG_DEBUG(("Running scheduler"));
@@ -510,6 +514,7 @@ void silc_schedule(SilcSchedule schedule)
   silc_schedule_iterate(schedule, -1);
   SILC_SCHEDULE_UNLOCK(schedule);
 }
+#endif /* !SILC_SYMBIAN */
 
 /* Wakes up the scheduler. This is used only in multi-threaded
    environments where threads may add new tasks or remove old tasks
@@ -562,8 +567,8 @@ SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
       ttask = silc_calloc(1, sizeof(*ttask));
       if (silc_unlikely(!ttask))
        goto out;
-    }
-    silc_list_del(schedule->free_tasks, ttask);
+    } else
+      silc_list_del(schedule->free_tasks, ttask);
 
     ttask->header.type = 1;
     ttask->header.callback = callback;
@@ -591,7 +596,7 @@ SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
     prev = NULL;
     while ((tmp = silc_list_get(list)) != SILC_LIST_END) {
       /* If we have shorter timeout, we have found our spot */
-      if (silc_compare_timeval(&ttask->timeout, &tmp->timeout)) {
+      if (silc_compare_timeval(&ttask->timeout, &tmp->timeout) < 0) {
        silc_list_insert(schedule->timeout_queue, prev, ttask);
        break;
       }
@@ -664,6 +669,15 @@ SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
 
  out:
   SILC_SCHEDULE_UNLOCK(schedule);
+
+#ifdef SILC_SYMBIAN
+  /* On symbian we wakeup scheduler immediately after adding timeout task
+     in case the task is added outside the scheduler loop (in some active
+     object). */
+  if (task && task->type == 1)
+    silc_schedule_wakeup(schedule);
+#endif /* SILC_SYMBIAN */
+
   return task;
 }