Porting Toolkit to Symbian. It should work while some sporadic
[silc.git] / lib / silcutil / silcschedule.c
index cd5a32030506a105799c1403f4c4f080f1607446..6d3f09e1c3d3794f4776ea5211a4115c1492494c 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1998 - 2006 Pekka Riikonen
+  Copyright (C) 1998 - 2007 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -45,44 +45,34 @@ static void silc_schedule_fd_destructor(void *key, void *context,
 
 static void silc_schedule_dispatch_fd(SilcSchedule schedule)
 {
-  SilcHashTableList htl;
-  SilcTask t;
   SilcTaskFd task;
-  SilcUInt32 fd;
+  SilcTask t;
 
-  silc_hash_table_list(schedule->fd_queue, &htl);
-  while (silc_likely(silc_hash_table_get(&htl, (void **)&fd,
-                                        (void **)&task))) {
+  /* The dispatch list includes only valid tasks, and tasks that have
+     something to dispatch.  Dispatching is atomic; no matter if another
+     thread invalidates a task when we unlock, we dispatch to completion. */
+  SILC_SCHEDULE_UNLOCK(schedule);
+  silc_list_start(schedule->fd_dispatch);
+  while ((task = silc_list_get(schedule->fd_dispatch))) {
     t = (SilcTask)task;
 
-    if (silc_unlikely(!t->valid)) {
-      silc_schedule_task_remove(schedule, t);
-      continue;
-    }
-    if (!task->revents || !task->events)
-      continue;
-
     /* Is the task ready for reading */
-    if (task->revents & SILC_TASK_READ) {
-      SILC_SCHEDULE_UNLOCK(schedule);
+    if (task->revents & SILC_TASK_READ)
       t->callback(schedule, schedule->app_context, SILC_TASK_READ,
                  task->fd, t->context);
-      SILC_SCHEDULE_LOCK(schedule);
-    }
 
     /* Is the task ready for writing */
-    if (t->valid && task->revents & SILC_TASK_WRITE) {
-      SILC_SCHEDULE_UNLOCK(schedule);
+    if (t->valid && task->revents & SILC_TASK_WRITE)
       t->callback(schedule, schedule->app_context, SILC_TASK_WRITE,
                  task->fd, t->context);
-      SILC_SCHEDULE_LOCK(schedule);
-    }
-
-    /* Remove if task was invalidated in the task callback */
-    if (silc_unlikely(!t->valid))
-      silc_schedule_task_remove(schedule, t);
   }
-  silc_hash_table_list_reset(&htl);
+  SILC_SCHEDULE_LOCK(schedule);
+
+  /* Remove invalidated tasks */
+  silc_list_start(schedule->fd_dispatch);
+  while ((task = silc_list_get(schedule->fd_dispatch)))
+    if (silc_unlikely(!task->header.valid))
+      silc_schedule_task_remove(schedule, (SilcTask)task);
 }
 
 /* Executes all tasks whose timeout has expired. The task is removed from
@@ -116,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;
@@ -165,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;
@@ -215,7 +205,7 @@ static void silc_schedule_task_remove(SilcSchedule schedule, SilcTask task)
 
     /* Delete from fd queue */
     silc_hash_table_list(schedule->fd_queue, &htl);
-    while (silc_hash_table_get(&htl, (void **)&fd, (void **)&task))
+    while (silc_hash_table_get(&htl, (void *)&fd, (void *)&task))
       silc_hash_table_del(schedule->fd_queue, SILC_32_TO_PTR(fd));
     silc_hash_table_list_reset(&htl);
 
@@ -335,11 +325,13 @@ SilcSchedule silc_schedule_init(int max_tasks, void *app_context)
   schedule->fd_queue =
     silc_hash_table_alloc(0, silc_hash_uint, NULL, NULL, NULL,
                          silc_schedule_fd_destructor, NULL, TRUE);
-  if (!schedule->fd_queue)
+  if (!schedule->fd_queue) {
+    silc_free(schedule);
     return NULL;
+  }
 
-  silc_list_init(schedule->timeout_queue, struct SilcTaskTimeoutStruct, next);
-  silc_list_init(schedule->free_tasks, struct SilcTaskTimeoutStruct, next);
+  silc_list_init(schedule->timeout_queue, struct SilcTaskStruct, next);
+  silc_list_init(schedule->free_tasks, struct SilcTaskStruct, next);
 
   schedule->app_context = app_context;
   schedule->valid = TRUE;
@@ -350,6 +342,12 @@ SilcSchedule silc_schedule_init(int max_tasks, void *app_context)
 
   /* Initialize the platform specific scheduler. */
   schedule->internal = schedule_ops.init(schedule, app_context);
+  if (!schedule->internal) {
+    silc_hash_table_free(schedule->fd_queue);
+    silc_mutex_free(schedule->lock);
+    silc_free(schedule);
+    return NULL;
+  }
 
   /* Timeout freelist garbage collection */
   silc_schedule_task_add_timeout(schedule, silc_schedule_timeout_gc,
@@ -462,7 +460,7 @@ static SilcBool silc_schedule_iterate(SilcSchedule schedule, int timeout_usecs)
        of the selected file descriptors change status or the selected
        timeout expires. */
     SILC_LOG_DEBUG(("Select"));
-    ret = schedule_ops.select(schedule, schedule->internal);
+    ret = schedule_ops.schedule(schedule, schedule->internal);
 
     if (silc_likely(ret == 0)) {
       /* Timeout */
@@ -478,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;
     }
@@ -503,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"));
@@ -512,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
@@ -564,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;
@@ -593,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;
       }
@@ -605,23 +608,33 @@ SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
     task = (SilcTask)ttask;
 
   } else if (silc_likely(type == SILC_TASK_FD)) {
+    SilcTaskFd ftask;
+
     /* Check if fd is already added */
     if (silc_unlikely(silc_hash_table_find(schedule->fd_queue,
                                           SILC_32_TO_PTR(fd),
-                                          NULL, (void **)&task)))
-      goto out;
+                                          NULL, (void *)&task))) {
+      if (task->valid)
+        goto out;
+
+      /* Remove invalid task.  We must have unique fd key to hash table. */
+      silc_schedule_task_remove(schedule, task);
+    }
 
     /* Check max tasks */
     if (silc_unlikely(schedule->max_tasks > 0 &&
                      silc_hash_table_count(schedule->fd_queue) >=
                      schedule->max_tasks)) {
       SILC_LOG_WARNING(("Scheduler task limit reached: cannot add new task"));
+      task = NULL;
       goto out;
     }
 
-    SilcTaskFd ftask = silc_calloc(1, sizeof(*ftask));
-    if (silc_unlikely(!ftask))
+    ftask = silc_calloc(1, sizeof(*ftask));
+    if (silc_unlikely(!ftask)) {
+      task = NULL;
       goto out;
+    }
 
     SILC_LOG_DEBUG(("New fd task %p fd=%d", ftask, fd));
 
@@ -632,8 +645,18 @@ SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
     ftask->events = SILC_TASK_READ;
     ftask->fd = fd;
 
-    /* Add task */
-    silc_hash_table_add(schedule->fd_queue, SILC_32_TO_PTR(fd), ftask);
+    /* Add task and schedule it */
+    if (!silc_hash_table_add(schedule->fd_queue, SILC_32_TO_PTR(fd), ftask)) {
+      silc_free(ftask);
+      task = NULL;
+      goto out;
+    }
+    if (!schedule_ops.schedule_fd(schedule, schedule->internal,
+                                 ftask, ftask->events)) {
+      silc_hash_table_del(schedule->fd_queue, SILC_32_TO_PTR(fd));
+      task = NULL;
+      goto out;
+    }
 
     task = (SilcTask)ftask;
 
@@ -646,12 +669,21 @@ 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;
 }
 
 /* Invalidates task */
 
-void silc_schedule_task_del(SilcSchedule schedule, SilcTask task)
+SilcBool silc_schedule_task_del(SilcSchedule schedule, SilcTask task)
 {
   if (silc_unlikely(task == SILC_ALL_TASKS)) {
     SilcHashTableList htl;
@@ -662,7 +694,7 @@ void silc_schedule_task_del(SilcSchedule schedule, SilcTask task)
 
     /* Delete from fd queue */
     silc_hash_table_list(schedule->fd_queue, &htl);
-    while (silc_hash_table_get(&htl, NULL, (void **)&task))
+    while (silc_hash_table_get(&htl, NULL, (void *)&task))
       task->valid = FALSE;
     silc_hash_table_list_reset(&htl);
 
@@ -673,20 +705,23 @@ void silc_schedule_task_del(SilcSchedule schedule, SilcTask task)
       task->valid = FALSE;
 
     SILC_SCHEDULE_UNLOCK(schedule);
-    return;
+    return TRUE;
   }
 
   SILC_LOG_DEBUG(("Unregistering task %p", task));
   SILC_SCHEDULE_LOCK(schedule);
   task->valid = FALSE;
   SILC_SCHEDULE_UNLOCK(schedule);
+
+  return TRUE;
 }
 
 /* Invalidate task by fd */
 
-void silc_schedule_task_del_by_fd(SilcSchedule schedule, SilcUInt32 fd)
+SilcBool silc_schedule_task_del_by_fd(SilcSchedule schedule, SilcUInt32 fd)
 {
   SilcTask task = NULL;
+  SilcBool ret = FALSE;
 
   SILC_LOG_DEBUG(("Unregister task by fd %d", fd));
 
@@ -695,24 +730,32 @@ void silc_schedule_task_del_by_fd(SilcSchedule schedule, SilcUInt32 fd)
   /* fd is unique, so there is only one task with this fd in the table */
   if (silc_likely(silc_hash_table_find(schedule->fd_queue,
                                       SILC_32_TO_PTR(fd), NULL,
-                                      (void **)&task)))
+                                      (void *)&task))) {
+    SILC_LOG_DEBUG(("Deleting task %p", task));
     task->valid = FALSE;
+    ret = TRUE;
+  }
 
   SILC_SCHEDULE_UNLOCK(schedule);
 
   /* If it is signal, remove it */
-  if (silc_unlikely(!task))
+  if (silc_unlikely(!task)) {
     schedule_ops.signal_unregister(schedule, schedule->internal, fd);
+    ret = TRUE;
+  }
+
+  return ret;
 }
 
 /* Invalidate task by task callback. */
 
-void silc_schedule_task_del_by_callback(SilcSchedule schedule,
-                                       SilcTaskCallback callback)
+SilcBool silc_schedule_task_del_by_callback(SilcSchedule schedule,
+                                           SilcTaskCallback callback)
 {
   SilcTask task;
   SilcHashTableList htl;
   SilcList list;
+  SilcBool ret = FALSE;
 
   SILC_LOG_DEBUG(("Unregister task by callback"));
 
@@ -720,9 +763,11 @@ void silc_schedule_task_del_by_callback(SilcSchedule schedule,
 
   /* Delete from fd queue */
   silc_hash_table_list(schedule->fd_queue, &htl);
-  while (silc_hash_table_get(&htl, NULL, (void **)&task)) {
-    if (task->callback == callback)
+  while (silc_hash_table_get(&htl, NULL, (void *)&task)) {
+    if (task->callback == callback) {
       task->valid = FALSE;
+      ret = TRUE;
+    }
   }
   silc_hash_table_list_reset(&htl);
 
@@ -730,20 +775,26 @@ void silc_schedule_task_del_by_callback(SilcSchedule schedule,
   list = schedule->timeout_queue;
   silc_list_start(list);
   while ((task = (SilcTask)silc_list_get(list))) {
-    if (task->callback == callback)
+    if (task->callback == callback) {
       task->valid = FALSE;
+      ret = TRUE;
+    }
   }
 
   SILC_SCHEDULE_UNLOCK(schedule);
+
+  return ret;
 }
 
 /* Invalidate task by context. */
 
-void silc_schedule_task_del_by_context(SilcSchedule schedule, void *context)
+SilcBool silc_schedule_task_del_by_context(SilcSchedule schedule,
+                                          void *context)
 {
   SilcTask task;
   SilcHashTableList htl;
   SilcList list;
+  SilcBool ret = FALSE;
 
   SILC_LOG_DEBUG(("Unregister task by context"));
 
@@ -751,9 +802,11 @@ void silc_schedule_task_del_by_context(SilcSchedule schedule, void *context)
 
   /* Delete from fd queue */
   silc_hash_table_list(schedule->fd_queue, &htl);
-  while (silc_hash_table_get(&htl, NULL, (void **)&task)) {
-    if (task->context == context)
+  while (silc_hash_table_get(&htl, NULL, (void *)&task)) {
+    if (task->context == context) {
       task->valid = FALSE;
+      ret = TRUE;
+    }
   }
   silc_hash_table_list_reset(&htl);
 
@@ -761,26 +814,32 @@ void silc_schedule_task_del_by_context(SilcSchedule schedule, void *context)
   list = schedule->timeout_queue;
   silc_list_start(list);
   while ((task = (SilcTask)silc_list_get(list))) {
-    if (task->context == context)
+    if (task->context == context) {
+      ret = TRUE;
       task->valid = FALSE;
+    }
   }
 
   SILC_SCHEDULE_UNLOCK(schedule);
+
+  return ret;
 }
 
 /* Invalidate task by all */
 
-void silc_schedule_task_del_by_all(SilcSchedule schedule, int fd,
-                                  SilcTaskCallback callback, void *context)
+SilcBool silc_schedule_task_del_by_all(SilcSchedule schedule, int fd,
+                                      SilcTaskCallback callback,
+                                      void *context)
 {
   SilcTask task;
   SilcList list;
+  SilcBool ret = FALSE;
 
   SILC_LOG_DEBUG(("Unregister task by fd, callback and context"));
 
   /* For fd task, callback and context is irrelevant as fd is unique */
   if (fd)
-    silc_schedule_task_del_by_fd(schedule, fd);
+    return silc_schedule_task_del_by_fd(schedule, fd);
 
   SILC_SCHEDULE_LOCK(schedule);
 
@@ -788,37 +847,67 @@ void silc_schedule_task_del_by_all(SilcSchedule schedule, int fd,
   list = schedule->timeout_queue;
   silc_list_start(list);
   while ((task = (SilcTask)silc_list_get(list))) {
-    if (task->callback == callback && task->context == context)
+    if (task->callback == callback && task->context == context) {
       task->valid = FALSE;
+      ret = TRUE;
+    }
   }
 
   SILC_SCHEDULE_UNLOCK(schedule);
+
+  return TRUE;
 }
 
 /* Sets a file descriptor to be listened by scheduler. One can call this
    directly if wanted. This can be called multiple times for one file
    descriptor to set different iomasks. */
 
-void silc_schedule_set_listen_fd(SilcSchedule schedule, SilcUInt32 fd,
-                                SilcTaskEvent mask, SilcBool send_events)
+SilcBool silc_schedule_set_listen_fd(SilcSchedule schedule, SilcUInt32 fd,
+                                    SilcTaskEvent mask, SilcBool send_events)
 {
   SilcTaskFd task;
 
   if (silc_unlikely(!schedule->valid))
-    return;
+    return FALSE;
 
   SILC_SCHEDULE_LOCK(schedule);
 
   if (silc_hash_table_find(schedule->fd_queue, SILC_32_TO_PTR(fd),
-                          NULL, (void **)&task)) {
+                          NULL, (void *)&task)) {
+    if (!schedule_ops.schedule_fd(schedule, schedule->internal, task, mask)) {
+      SILC_SCHEDULE_UNLOCK(schedule);
+      return FALSE;
+    }
     task->events = mask;
-    if (silc_unlikely(send_events)) {
+    if (silc_unlikely(send_events) && mask) {
       task->revents = mask;
       silc_schedule_dispatch_fd(schedule);
     }
   }
 
   SILC_SCHEDULE_UNLOCK(schedule);
+
+  return TRUE;
+}
+
+/* Returns the file descriptor's current requested event mask. */
+
+SilcTaskEvent silc_schedule_get_fd_events(SilcSchedule schedule,
+                                         SilcUInt32 fd)
+{
+  SilcTaskFd task;
+  SilcTaskEvent event = 0;
+
+  if (silc_unlikely(!schedule->valid))
+    return 0;
+
+  SILC_SCHEDULE_LOCK(schedule);
+  if (silc_hash_table_find(schedule->fd_queue, SILC_32_TO_PTR(fd),
+                          NULL, (void *)&task))
+    event = task->events;
+  SILC_SCHEDULE_UNLOCK(schedule);
+
+  return event;
 }
 
 /* Removes a file descriptor from listen list. */