Returns now GNUNET_SYSERR
[oweals/gnunet.git] / src / util / scheduler.c
index 341400bba1b8ef40df29e59c4f69859edf3dcb70..49d491bd7843546e48974b39f0a53159256dd52a 100644 (file)
@@ -163,76 +163,71 @@ struct Task
 
 
 /**
-* List of tasks waiting for an event.
-*/
-struct Task *pending;
-
-/**
-* List of tasks waiting ONLY for a timeout event.
-* Sorted by timeout (earliest first).  Used so that
-* we do not traverse the list of these tasks when
-* building select sets (we just look at the head
-* to determine the respective timeout ONCE).
-*/
-struct Task *pending_timeout;
+ * List of tasks waiting for an event.
+ */
+static struct Task *pending;
 
 /**
-* Last inserted task waiting ONLY for a timeout event.
-* Used to (heuristically) speed up insertion.
-*/
-struct Task *pending_timeout_last;
+ * List of tasks waiting ONLY for a timeout event.
+ * Sorted by timeout (earliest first).  Used so that
+ * we do not traverse the list of these tasks when
+ * building select sets (we just look at the head
+ * to determine the respective timeout ONCE).
+ */
+static struct Task *pending_timeout;
 
 /**
-* ID of the task that is running right now.
-*/
-struct Task *active_task;
+ * Last inserted task waiting ONLY for a timeout event.
+ * Used to (heuristically) speed up insertion.
+ */
+static struct Task *pending_timeout_last;
 
 /**
-* List of tasks ready to run right now,
-* grouped by importance.
-*/
-struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
+ * ID of the task that is running right now.
+ */
+static struct Task *active_task;
 
 /**
-* Identity of the last task queued.  Incremented for each task to
-* generate a unique task ID (it is virtually impossible to start
-* more than 2^64 tasks during the lifetime of a process).
-*/
-GNUNET_SCHEDULER_TaskIdentifier last_id;
+ * List of tasks ready to run right now,
+ * grouped by importance.
+ */
+static struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
 
 /**
-* Highest number so that all tasks with smaller identifiers
-* have already completed.  Also the lowest number of a task
-* still waiting to be executed.
-*/
-GNUNET_SCHEDULER_TaskIdentifier lowest_pending_id;
+ * Identity of the last task queued.  Incremented for each task to
+ * generate a unique task ID (it is virtually impossible to start
+ * more than 2^64 tasks during the lifetime of a process).
+ */
+static GNUNET_SCHEDULER_TaskIdentifier last_id;
 
 /**
-* Number of tasks on the ready list.
-*/
-unsigned int ready_count;
+ * 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;
 
 /**
-* How many tasks have we run so far?
-*/
-unsigned long long tasks_run;
+ * Number of tasks on the ready list.
+ */
+static unsigned int ready_count;
 
 /**
-* Priority of the task running right now.  Only
-* valid while a task is running.
-*/
-enum GNUNET_SCHEDULER_Priority current_priority;
+ * How many tasks have we run so far?
+ */
+static unsigned long long tasks_run;
 
 /**
-* Priority of the highest task added in the current select
-* iteration.
-*/
-enum GNUNET_SCHEDULER_Priority max_priority_added;
+ * Priority of the task running right now.  Only
+ * valid while a task is running.
+ */
+static enum GNUNET_SCHEDULER_Priority current_priority;
 
 /**
-* How 'nice' are we right now?
-*/
-int nice_level;
+ * Priority of the highest task added in the current select
+ * iteration.
+ */
+static enum GNUNET_SCHEDULER_Priority max_priority_added;
 
 
 /**
@@ -257,7 +252,6 @@ check_priority (enum GNUNET_SCHEDULER_Priority p)
  * future), but only if the return value is "GNUNET_NO" (and
  * the "lowest_pending_id" check failed).
  *
- * @param sched the scheduler
  * @param id which task are we checking for
  * @return GNUNET_YES if so, GNUNET_NO if not
  */
@@ -309,7 +303,6 @@ is_pending (GNUNET_SCHEDULER_TaskIdentifier id)
 /**
  * Update all sets and timeout for select.
  *
- * @param sched the scheduler
  * @param rs read-set, set to all FDs we would like to read (updated)
  * @param ws write-set, set to all FDs we would like to write (updated)
  * @param timeout next timeout (updated)
@@ -393,7 +386,6 @@ set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
  * Check if the given task is eligible to run now.
  * Also set the reason why it is eligible.
  *
- * @param sched the scheduler
  * @param task task to check if it is ready
  * @param now the current time
  * @param rs set of FDs ready for reading
@@ -519,8 +511,6 @@ check_ready (const struct GNUNET_NETWORK_FDSet *rs,
  * cause all tasks to run (as soon as possible, respecting
  * priorities and prerequisite tasks).  Note that tasks
  * scheduled AFTER this call may still be delayed arbitrarily.
- *
- * @param sched the scheduler
  */
 void
 GNUNET_SCHEDULER_shutdown ()
@@ -587,7 +577,6 @@ destroy_task (struct Task *t)
  * may become ready, hence we should select on it).  Naturally, if
  * there are no more ready tasks, we also return.  
  *
- * @param sched the scheduler
  * @param rs FDs ready for reading
  * @param ws FDs ready for writing
  */
@@ -733,6 +722,7 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
   const struct GNUNET_DISK_FileHandle *pr;
   char c;
 
+  GNUNET_assert (active_task == NULL);
   rs = GNUNET_NETWORK_fdset_create ();
   ws = GNUNET_NETWORK_fdset_create ();
   GNUNET_assert (shutdown_pipe_handle == NULL);
@@ -780,7 +770,7 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
          snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid());
          close (1);
          dup2 (2, 1);
-         system (lsof);                  
+         ret = system (lsof);            
 #endif
 #endif
           abort ();
@@ -830,12 +820,12 @@ GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
  * started.  Will return the same value as 
  * the GNUNET_SCHEDULER_TaskContext's reason field.
  *
- * @param sched scheduler to query
  * @return reason(s) why the current task is run
  */
 enum GNUNET_SCHEDULER_Reason
 GNUNET_SCHEDULER_get_reason ()
 {
+  GNUNET_assert (active_task != NULL);
   return active_task->reason;
 }
 
@@ -846,7 +836,6 @@ GNUNET_SCHEDULER_get_reason ()
  * dropped (if the decision should be made based on the number of
  * tasks ready to run).
  *
- * @param sched scheduler to query
  * @param p priority level to look at
  * @return number of tasks pending right now
  */
@@ -856,6 +845,7 @@ GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
   struct Task *pos;
   unsigned int ret;
 
+  GNUNET_assert (active_task != NULL);
   if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
     return ready_count;
   if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
@@ -875,7 +865,6 @@ GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
  * Cancel the task with the specified identifier.
  * The task must not yet have run.
  *
- * @param sched scheduler to use
  * @param task id of the task to cancel
  * @return original closure of the task
  */
@@ -888,6 +877,7 @@ GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
   int to;
   void *ret;
 
+  GNUNET_assert (active_task != NULL);
   to = 0;
   prev = NULL;
   t = pending;
@@ -968,7 +958,6 @@ GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
  * similar to the other "add" functions except that there is no delay
  * and the reason code can be specified.
  *
- * @param sched scheduler to use
  * @param task main function of the task
  * @param task_cls closure for 'main'
  * @param reason reason for task invocation
@@ -982,6 +971,9 @@ GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task,
 #if EXECINFO
   void *backtrace_array[50];
 #endif
+
+  GNUNET_assert ( (active_task != NULL) ||
+                 (reason == GNUNET_SCHEDULER_REASON_STARTUP) );
   t = GNUNET_malloc (sizeof (struct Task));
 #if EXECINFO
   t->num_backtrace_strings = backtrace(backtrace_array, 50);
@@ -1012,7 +1004,6 @@ GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task,
  * has completed. It will be run with the priority of the calling
  * task.
  *
- * @param sched scheduler to use
  * @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
@@ -1038,7 +1029,6 @@ GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
 /**
  * Schedule a new task to be run with a specified priority.
  *
- * @param sched scheduler to use
  * @param prio how important is the new task?
  * @param task main function of the task
  * @param task_cls closure of task
@@ -1063,7 +1053,6 @@ GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
  * will be scheduled for execution once the delay has expired. It
  * will be run with the priority of the calling task.
  *
- * @param sched scheduler to use
  * @param delay when should this operation time out? Use 
  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
  * @param task main function of the task
@@ -1084,6 +1073,7 @@ GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
   void *backtrace_array[MAX_TRACE_DEPTH];
 #endif
 
+  GNUNET_assert (active_task != NULL);
   GNUNET_assert (NULL != task);
   t = GNUNET_malloc (sizeof (struct Task));
   t->callback = task;
@@ -1148,8 +1138,7 @@ GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
 
 #else
   /* unoptimized version */
-  return GNUNET_SCHEDULER_add_select (sched,
-                                      GNUNET_SCHEDULER_PRIORITY_KEEP,
+  return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
                                       GNUNET_SCHEDULER_NO_TASK, delay,
                                       NULL, NULL, task, task_cls);
 #endif
@@ -1161,7 +1150,6 @@ GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
  * Schedule a new task to be run as soon as possible. The task
  * will be run with the priority of the calling task.
  *
- * @param sched scheduler to use
  * @param task main function of the task
  * @param task_cls closure of task
  * @return unique task identifier for the job
@@ -1197,7 +1185,6 @@ GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task,
  *     || shutdown-active )
  * </code>
  *
- * @param sched scheduler to use
  * @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 rfd file descriptor we want to read (can be -1)
@@ -1218,6 +1205,7 @@ add_without_sets (struct GNUNET_TIME_Relative delay,
   void *backtrace_array[MAX_TRACE_DEPTH];
 #endif
 
+  GNUNET_assert (active_task != NULL);
   GNUNET_assert (NULL != task);
   t = GNUNET_malloc (sizeof (struct Task));
   t->callback = task;
@@ -1266,7 +1254,6 @@ add_without_sets (struct GNUNET_TIME_Relative delay,
  * socket operation is ready.  It will be run with the priority of
  * the calling task.
  *
- * @param sched scheduler to use
  * @param delay when should this operation time out? Use 
  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
  * @param rfd read file-descriptor
@@ -1296,7 +1283,6 @@ GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
  * socket operation is ready.  It will be run with the priority of
  * the calling task.
  *
- * @param sched scheduler to use
  * @param delay when should this operation time out? Use 
  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
  * @param wfd write file-descriptor
@@ -1326,7 +1312,6 @@ GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
  * socket operation is ready. It will be run with the priority of
  * the calling task.
  *
- * @param sched scheduler to use
  * @param delay when should this operation time out? Use 
  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
  * @param rfd read file-descriptor
@@ -1347,8 +1332,7 @@ GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
   GNUNET_assert (rfd != NULL);
   rs = GNUNET_NETWORK_fdset_create ();
   GNUNET_NETWORK_fdset_handle_set (rs, rfd);
-  ret = GNUNET_SCHEDULER_add_select (sched,
-                                     GNUNET_SCHEDULER_PRIORITY_KEEP,
+  ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
                                      GNUNET_SCHEDULER_NO_TASK, delay,
                                      rs, NULL, task, task_cls);
   GNUNET_NETWORK_fdset_destroy (rs);
@@ -1375,7 +1359,6 @@ GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
  * socket operation is ready. It will be run with the priority of
  * the calling task.
  *
- * @param sched scheduler to use
  * @param delay when should this operation time out? Use 
  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
  * @param wfd write file-descriptor
@@ -1396,8 +1379,7 @@ GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
   GNUNET_assert (wfd != NULL);
   ws = GNUNET_NETWORK_fdset_create ();
   GNUNET_NETWORK_fdset_handle_set (ws, wfd);
-  ret = GNUNET_SCHEDULER_add_select (sched,
-                                     GNUNET_SCHEDULER_PRIORITY_KEEP,
+  ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
                                      GNUNET_SCHEDULER_NO_TASK,
                                      delay, NULL, ws, task, task_cls);
   GNUNET_NETWORK_fdset_destroy (ws);
@@ -1434,7 +1416,6 @@ GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
  *     || (shutdown-active && run-on-shutdown) )
  * </code>
  *
- * @param sched scheduler to use
  * @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
@@ -1464,6 +1445,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 != task);
   t = GNUNET_malloc (sizeof (struct Task));
   t->callback = task;