- test for external iterator
[oweals/gnunet.git] / src / util / scheduler.c
index 8d3106032c1806fa586bc389c54576d2effc2639..5b90d7e4f3eaf450f237660fbca91516e0cd86b2 100644 (file)
@@ -1,10 +1,10 @@
 /*
       This file is part of GNUnet
-      (C) 2009, 2011 Christian Grothoff (and other contributing authors)
+      (C) 2009-2013 Christian Grothoff (and other contributing authors)
 
       GNUnet is free software; you can redistribute it and/or modify
       it under the terms of the GNU General Public License as published
-      by the Free Software Foundation; either version 2, or (at your
+      by the Free Software Foundation; either version 3, or (at your
       option) any later version.
 
       GNUnet is distributed in the hope that it will be useful, but
@@ -36,7 +36,7 @@
 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-scheduler", syscall)
 
 
-#ifdef LINUX
+#if HAVE_EXECINFO_H
 #include "execinfo.h"
 
 /**
@@ -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.
  *
@@ -366,7 +300,7 @@ update_sets (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws,
   if (pos != NULL)
   {
     to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
-    if (timeout->rel_value > to.rel_value)
+    if (timeout->rel_value_us > to.rel_value_us)
       *timeout = to;
     if (pos->reason != 0)
       *timeout = GNUNET_TIME_UNIT_ZERO;
@@ -374,16 +308,10 @@ 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)
+    if (pos->timeout.abs_value_us != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
     {
       to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
-      if (timeout->rel_value > to.rel_value)
+      if (timeout->rel_value_us > to.rel_value_us)
         *timeout = to;
     }
     if (pos->read_fd != -1)
@@ -445,7 +373,7 @@ is_ready (struct Task *task, struct GNUNET_TIME_Absolute now,
   enum GNUNET_SCHEDULER_Reason reason;
 
   reason = task->reason;
-  if (now.abs_value >= task->timeout.abs_value)
+  if (now.abs_value_us >= task->timeout.abs_value_us)
     reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
   if ((0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
       (((task->read_fd != -1) &&
@@ -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;
 }
@@ -513,7 +433,7 @@ check_ready (const struct GNUNET_NETWORK_FDSet *rs,
   while (pos != NULL)
   {
     next = pos->next;
-    if (now.abs_value >= pos->timeout.abs_value)
+    if (now.abs_value_us >= pos->timeout.abs_value_us)
       pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
     if (0 == pos->reason)
       break;
@@ -645,22 +565,17 @@ run_ready (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws)
     GNUNET_assert (pos != NULL);        /* ready_count wrong? */
     ready[p] = pos->next;
     ready_count--;
-    if (current_priority != pos->priority)
-    {
-      current_priority = pos->priority;
-      (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
-                                             pos->priority);
-    }
+    current_priority = pos->priority;
     current_lifeness = pos->lifeness;
     active_task = pos;
 #if PROFILE_DELAYS
-    if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
-        DELAY_THRESHOLD.rel_value)
+    if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
+        DELAY_THRESHOLD.rel_value_us)
     {
-      LOG (GNUNET_ERROR_TYPE_ERROR, "Task %llu took %llums to be scheduled\n",
-           pos->id,
-           (unsigned long long)
-           GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value);
+      LOG (GNUNET_ERROR_TYPE_DEBUG, 
+          "Task %llu took %s to be scheduled\n",
+           (unsigned long long) pos->id,
+           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time), GNUNET_YES));
     }
 #endif
     tc.reason = pos->reason;
@@ -807,6 +722,7 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
                                 GNUNET_DISK_PIPE_END_READ);
   GNUNET_assert (pr != NULL);
   my_pid = getpid ();
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Registering signal handlers\n");
   shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
   shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
 #ifndef MINGW
@@ -861,7 +777,7 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
       GNUNET_abort ();
       break;
     }
-    if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
+    if ((0 == ret) && (0 == timeout.rel_value_us) && (busy_wait_warning > 16))
     {
       LOG (GNUNET_ERROR_TYPE_WARNING, _("Looks like we're busy waiting...\n"));
       sleep (1);                /* mitigate */
@@ -1175,7 +1091,7 @@ GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
   prev = pending_timeout_last;
   if (prev != NULL)
   {
-    if (prev->timeout.abs_value > t->timeout.abs_value)
+    if (prev->timeout.abs_value_us > t->timeout.abs_value_us)
       prev = NULL;
     else
       pos = prev->next;         /* heuristic success! */
@@ -1186,8 +1102,8 @@ GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
     pos = pending_timeout;
   }
   while ((pos != NULL) &&
-         ((pos->timeout.abs_value <= t->timeout.abs_value) ||
-          (pos->reason != 0)))
+         ((pos->timeout.abs_value_us <= t->timeout.abs_value_us) ||
+          (0 != pos->reason)))
   {
     prev = pos;
     pos = pos->next;
@@ -1373,7 +1289,6 @@ 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;
@@ -1414,6 +1329,35 @@ GNUNET_SCHEDULER_TaskIdentifier
 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
                                struct GNUNET_NETWORK_Handle *rfd,
                                GNUNET_SCHEDULER_Task task, void *task_cls)
+{
+  return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
+                                                     GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+                                                     rfd, task, task_cls);
+}
+
+
+/**
+ * Schedule a new task to be run with a specified priority and to be
+ * run after the specified delay or when the specified file descriptor
+ * is ready for reading.  The delay can be used as a timeout on the
+ * socket being ready.  The task will be scheduled for execution once
+ * either the delay has expired or the socket operation is ready.  It
+ * will be run with the DEFAULT priority.
+ *
+ * @param delay when should this operation time out? Use
+ *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
+ * @param priority priority to use for the task
+ * @param rfd read file-descriptor
+ * @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_read_net_with_priority (struct GNUNET_TIME_Relative delay,
+                                            enum GNUNET_SCHEDULER_Priority priority,
+                                            struct GNUNET_NETWORK_Handle *rfd,
+                                            GNUNET_SCHEDULER_Task task, void *task_cls)
 {
 #if MINGW
   struct GNUNET_NETWORK_FDSet *rs;
@@ -1423,20 +1367,21 @@ GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
   rs = GNUNET_NETWORK_fdset_create ();
   GNUNET_NETWORK_fdset_set (rs, rfd);
   ret =
-    GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+    GNUNET_SCHEDULER_add_select (priority,
                                 delay, rs, NULL,
                                 task, task_cls);
   GNUNET_NETWORK_fdset_destroy (rs);
   return ret;
 #else
   return add_without_sets (delay, 
-                          GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+                          priority,
                           GNUNET_NETWORK_get_fd (rfd), -1, task,
                            task_cls);
 #endif
 }
 
 
+
 /**
  * Schedule a new task to be run with a specified delay or when the
  * specified file descriptor is ready for writing.  The delay can be
@@ -1613,7 +1558,7 @@ GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
   void *backtrace_array[MAX_TRACE_DEPTH];
 #endif
 
-  GNUNET_assert (active_task != NULL);
+  GNUNET_assert (NULL != active_task);
   GNUNET_assert (NULL != task);
   t = GNUNET_malloc (sizeof (struct Task));
   t->callback = task;