Task deletion functions now return boolean value.
[silc.git] / lib / silcutil / silcschedule.c
index 2e546ee8576a6ee919bf97e9902268c3431a14d9..790b242ff5633a337ae4cdcfa6b803f73c7eb9d4 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
@@ -595,23 +595,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));
 
@@ -622,8 +632,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;
 
@@ -641,7 +661,7 @@ SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
 
 /* 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;
@@ -663,20 +683,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));
 
@@ -685,24 +708,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"));
 
@@ -711,8 +742,10 @@ 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)
+    if (task->callback == callback) {
       task->valid = FALSE;
+      ret = TRUE;
+    }
   }
   silc_hash_table_list_reset(&htl);
 
@@ -720,20 +753,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"));
 
@@ -742,8 +781,10 @@ 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)
+    if (task->context == context) {
       task->valid = FALSE;
+      ret = TRUE;
+    }
   }
   silc_hash_table_list_reset(&htl);
 
@@ -751,26 +792,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);
 
@@ -778,31 +825,38 @@ 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)) {
+    if (!schedule_ops.schedule_fd(schedule, schedule->internal, task, mask)) {
+      SILC_SCHEDULE_UNLOCK(schedule);
+      return FALSE;
+    }
     task->events = mask;
-    schedule_ops.schedule_fd(schedule, schedule->internal, task, mask);
     if (silc_unlikely(send_events) && mask) {
       task->revents = mask;
       silc_schedule_dispatch_fd(schedule);
@@ -810,9 +864,11 @@ void silc_schedule_set_listen_fd(SilcSchedule schedule, SilcUInt32 fd,
   }
 
   SILC_SCHEDULE_UNLOCK(schedule);
+
+  return TRUE;
 }
 
-/* Returns the file descriptors current requested event mask. */
+/* Returns the file descriptor's current requested event mask. */
 
 SilcTaskEvent silc_schedule_get_fd_events(SilcSchedule schedule,
                                          SilcUInt32 fd)