handling replies continuously from server
[oweals/gnunet.git] / src / util / scheduler.c
index 4128969466e72f83c40db5978e234a6305f67717..6b8d2144392fdb15f0fe26f25a43f72c347871da 100644 (file)
 #include "gnunet_signal_lib.h"
 #include "gnunet_time_lib.h"
 #include "disk.h"
-#define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
 
-#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
-#ifdef LINUX
-#include "execinfo.h"
+#define LOG(kind,...) GNUNET_log_from (kind, "util-scheduler", __VA_ARGS__)
+
+#define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-scheduler", syscall)
 
 
+#ifdef LINUX
+#include "execinfo.h"
 
 /**
  * Use lsof to generate file descriptor reports on select error?
@@ -52,7 +53,7 @@
 /**
  * Check each file descriptor before adding
  */
-#define DEBUG_FDS GNUNET_EXTRA_LOGGING
+#define DEBUG_FDS GNUNET_NO
 
 /**
  * Depth of the traces collected via EXECINFO.
@@ -60,8 +61,6 @@
 #define MAX_TRACE_DEPTH 50
 #endif
 
-#define DEBUG_TASKS GNUNET_EXTRA_LOGGING
-
 /**
  * Should we figure out which tasks are delayed for a while
  * before they are run? (Consider using in combination with EXECINFO).
@@ -74,6 +73,7 @@
  */
 #define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
 
+
 /**
  * Linked list of pending tasks.
  */
@@ -114,11 +114,6 @@ struct Task
    */
   GNUNET_SCHEDULER_TaskIdentifier id;
 
-  /**
-   * Identifier of a prerequisite task.
-   */
-  GNUNET_SCHEDULER_TaskIdentifier prereq_id;
-
   /**
    * Absolute timeout value for the task, or
    * GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
@@ -215,13 +210,6 @@ static struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
  */
 static GNUNET_SCHEDULER_TaskIdentifier last_id;
 
-/**
- * Highest number so that all tasks with smaller identifiers
- * have already completed.  Also the lowest number of a task
- * still waiting to be executed.
- */
-static GNUNET_SCHEDULER_TaskIdentifier lowest_pending_id;
-
 /**
  * Number of tasks on the ready list.
  */
@@ -292,60 +280,6 @@ check_priority (enum GNUNET_SCHEDULER_Priority p)
 }
 
 
-/**
- * Is a task with this identifier still pending?  Also updates
- * "lowest_pending_id" as a side-effect (for faster checks in the
- * future), but only if the return value is "GNUNET_NO" (and
- * the "lowest_pending_id" check failed).
- *
- * @param id which task are we checking for
- * @return GNUNET_YES if so, GNUNET_NO if not
- */
-static int
-is_pending (GNUNET_SCHEDULER_TaskIdentifier id)
-{
-  struct Task *pos;
-  enum GNUNET_SCHEDULER_Priority p;
-  GNUNET_SCHEDULER_TaskIdentifier min;
-
-  if (id < lowest_pending_id)
-    return GNUNET_NO;
-  min = -1;                     /* maximum value */
-  pos = pending;
-  while (pos != NULL)
-  {
-    if (pos->id == id)
-      return GNUNET_YES;
-    if (pos->id < min)
-      min = pos->id;
-    pos = pos->next;
-  }
-  pos = pending_timeout;
-  while (pos != NULL)
-  {
-    if (pos->id == id)
-      return GNUNET_YES;
-    if (pos->id < min)
-      min = pos->id;
-    pos = pos->next;
-  }
-  for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
-  {
-    pos = ready[p];
-    while (pos != NULL)
-    {
-      if (pos->id == id)
-        return GNUNET_YES;
-      if (pos->id < min)
-        min = pos->id;
-      pos = pos->next;
-    }
-  }
-  lowest_pending_id = min;
-  return GNUNET_NO;
-}
-
-
 /**
  * Update all sets and timeout for select.
  *
@@ -374,12 +308,6 @@ update_sets (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws,
   pos = pending;
   while (pos != NULL)
   {
-    if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
-        (GNUNET_YES == is_pending (pos->prereq_id)))
-    {
-      pos = pos->next;
-      continue;
-    }
     if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
     {
       to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
@@ -459,15 +387,7 @@ is_ready (struct Task *task, struct GNUNET_TIME_Absolute now,
     reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
   if (reason == 0)
     return GNUNET_NO;           /* not ready */
-  if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
-  {
-    if (GNUNET_YES == is_pending (task->prereq_id))
-    {
-      task->reason = reason;
-      return GNUNET_NO;         /* prereq waiting */
-    }
-    reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
-  }
+  reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;  
   task->reason = reason;
   return GNUNET_YES;
 }
@@ -526,10 +446,9 @@ check_ready (const struct GNUNET_NETWORK_FDSet *rs,
   pos = pending;
   while (pos != NULL)
   {
-#if DEBUG_TASKS
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking readiness of task: %llu / %p\n",
+    LOG (GNUNET_ERROR_TYPE_DEBUG, 
+        "Checking readiness of task: %llu / %p\n",
          pos->id, pos->callback_cls);
-#endif
     next = pos->next;
     if (GNUNET_YES == is_ready (pos, now, rs, ws))
     {
@@ -677,10 +596,9 @@ run_ready (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws)
         (pos->write_fd != -1) &&
         (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd)))
       GNUNET_abort ();          // added to ready in previous select loop!
-#if DEBUG_TASKS
-    LOG (GNUNET_ERROR_TYPE_DEBUG, "Running task: %llu / %p\n", pos->id,
+    LOG (GNUNET_ERROR_TYPE_DEBUG, 
+        "Running task: %llu / %p\n", pos->id,
          pos->callback_cls);
-#endif
     pos->callback (pos->callback_cls, &tc);
 #if EXECINFO
     int i;
@@ -820,13 +738,11 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
   current_lifeness = GNUNET_YES;
   GNUNET_SCHEDULER_add_continuation (task, task_cls,
                                      GNUNET_SCHEDULER_REASON_STARTUP);
-#if ENABLE_WINDOWS_WORKAROUNDS
   active_task = (void *) (long) -1;     /* force passing of sanity check */
   GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
                                           &GNUNET_OS_install_parent_control_handler,
                                           NULL);
   active_task = NULL;
-#endif
   last_tr = 0;
   busy_wait_warning = 0;
   while (GNUNET_OK == check_lifeness ())
@@ -1037,10 +953,8 @@ GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
     prev->next = t->next;
   }
   ret = t->callback_cls;
-#if DEBUG_TASKS
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling task: %llu / %p\n", task,
        t->callback_cls);
-#endif
   destroy_task (t);
   return ret;
 }
@@ -1087,10 +1001,8 @@ GNUNET_SCHEDULER_add_continuation_with_priority (GNUNET_SCHEDULER_Task task, voi
   t->reason = reason;
   t->priority = priority;
   t->lifeness = current_lifeness;
-#if DEBUG_TASKS
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding continuation task: %llu / %p\n", t->id,
        t->callback_cls);
-#endif
   queue_ready_task (t);
 }
 
@@ -1114,31 +1026,6 @@ GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls,
 }
 
 
-/**
- * Schedule a new task to be run after the specified prerequisite task
- * has completed. It will be run with the DEFAULT priority.
- *
- * @param prerequisite_task run this task after the task with the given
- *        task identifier completes (and any of our other
- *        conditions, such as delay, read or write-readiness
- *        are satisfied).  Use  GNUNET_SCHEDULER_NO_TASK to not have any dependency
- *        on completion of other tasks (this will cause the task to run as
- *        soon as possible).
- * @param task main function of the task
- * @param task_cls closure of task
- * @return unique task identifier for the job
- *         only valid until "task" is started!
- */
-GNUNET_SCHEDULER_TaskIdentifier
-GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
-                            GNUNET_SCHEDULER_Task task, void *task_cls)
-{
-  return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
-                                      prerequisite_task, GNUNET_TIME_UNIT_ZERO,
-                                      NULL, NULL, task, task_cls);
-}
-
-
 /**
  * Schedule a new task to be run with a specified priority.
  *
@@ -1152,7 +1039,7 @@ GNUNET_SCHEDULER_TaskIdentifier
 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
                                     GNUNET_SCHEDULER_Task task, void *task_cls)
 {
-  return GNUNET_SCHEDULER_add_select (prio, GNUNET_SCHEDULER_NO_TASK,
+  return GNUNET_SCHEDULER_add_select (prio, 
                                       GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
                                       task_cls);
 }
@@ -1233,10 +1120,8 @@ GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
   /* hyper-optimization... */
   pending_timeout_last = t;
 
-#if DEBUG_TASKS
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
        t->callback_cls);
-#endif
 #if EXECINFO
   int i;
 
@@ -1309,7 +1194,6 @@ GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
 
   ret =
       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
-                                   GNUNET_SCHEDULER_NO_TASK,
                                    GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
                                    task_cls);
   GNUNET_assert (pending->id == ret);
@@ -1409,17 +1293,14 @@ add_without_sets (struct GNUNET_TIME_Relative delay,
 #if PROFILE_DELAYS
   t->start_time = GNUNET_TIME_absolute_get ();
 #endif
-  t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
   t->timeout = GNUNET_TIME_relative_to_absolute (delay);
   t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
   t->lifeness = current_lifeness;
   t->next = pending;
   pending = t;
   max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
-#if DEBUG_TASKS
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
        t->callback_cls);
-#endif
 #if EXECINFO
   int i;
 
@@ -1462,7 +1343,7 @@ GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
   GNUNET_NETWORK_fdset_set (rs, rfd);
   ret =
     GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
-                                GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL,
+                                delay, rs, NULL,
                                 task, task_cls);
   GNUNET_NETWORK_fdset_destroy (rs);
   return ret;
@@ -1505,7 +1386,7 @@ GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
   GNUNET_NETWORK_fdset_set (ws, wfd);
   ret =
     GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
-                                GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws,
+                                delay, NULL, ws,
                                    task, task_cls);
   GNUNET_NETWORK_fdset_destroy (ws);
   return ret;
@@ -1548,7 +1429,7 @@ GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
   GNUNET_NETWORK_fdset_handle_set (rs, rfd);
   ret =
       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
-                                   GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL,
+                                   delay, rs, NULL,
                                    task, task_cls);
   GNUNET_NETWORK_fdset_destroy (rs);
   return ret;
@@ -1593,7 +1474,7 @@ GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
   GNUNET_NETWORK_fdset_handle_set (ws, wfd);
   ret =
       GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
-                                   GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws,
+                                   delay, NULL, ws,
                                    task, task_cls);
   GNUNET_NETWORK_fdset_destroy (ws);
   return ret;
@@ -1629,11 +1510,6 @@ GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
  * </code>
  *
  * @param prio how important is this task?
- * @param prerequisite_task run this task after the task with the given
- *        task identifier completes (and any of our other
- *        conditions, such as delay, read or write-readiness
- *        are satisfied).  Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
- *        on completion of other tasks.
  * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
  *        which means that the task will only be run after we receive SIGTERM
  * @param rs set of file descriptors we want to read (can be NULL)
@@ -1645,7 +1521,6 @@ GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
  */
 GNUNET_SCHEDULER_TaskIdentifier
 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
-                             GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
                              struct GNUNET_TIME_Relative delay,
                              const struct GNUNET_NETWORK_FDSet *rs,
                              const struct GNUNET_NETWORK_FDSet *ws,
@@ -1683,7 +1558,6 @@ GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
 #if PROFILE_DELAYS
   t->start_time = GNUNET_TIME_absolute_get ();
 #endif
-  t->prereq_id = prerequisite_task;
   t->timeout = GNUNET_TIME_relative_to_absolute (delay);
   t->priority =
       check_priority ((prio ==
@@ -1693,10 +1567,8 @@ GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
   t->next = pending;
   pending = t;
   max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
-#if DEBUG_TASKS
   LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
        t->callback_cls);
-#endif
 #if EXECINFO
   int i;