scheduler API change to allow use with js event loop
authorlurchi <lurchi@strangeplace.net>
Thu, 1 Feb 2018 18:54:54 +0000 (19:54 +0100)
committerlurchi <lurchi@strangeplace.net>
Thu, 1 Feb 2018 18:55:15 +0000 (19:55 +0100)
- GNUNET_SCHEDUELR_run_with_driver has been replaced with
GNUNET_SCHEDULER_driver_init and GNUNET_SCHEDUELR_driver_done
- GNUNET_SCHEDULER_run_from_driver has been renamed to
GNUNET_SCHEDULER_do_work (as it's no longer being called from a driver
callback)
- documentation has been updated

src/include/gnunet_scheduler_lib.h
src/util/scheduler.c

index d2805a68503caa2751753172499158de9eef1cdd..e5c361567967dd44df1fe3f6edefcfa2b5c57ec0 100644 (file)
@@ -237,29 +237,32 @@ struct GNUNET_SCHEDULER_Handle;
 
 
 /**
- * Function called by the driver to tell the scheduler to run some of
- * the tasks that are ready.  This function may return even though
- * there are tasks left to run just to give other tasks a chance as
- * well.  If we return #GNUNET_YES, the driver should call this
- * function again as soon as possible, while if we return #GNUNET_NO
- * it must block until either the operating system has more work (the
- * scheduler has no more work to do right now) or the timeout set by
- * the scheduler (using the set_wakeup callback) is reached.
- *
- * @param sh scheduler handle that was given to the `loop`
- * @return #GNUNET_OK if there are more tasks that are ready,
- *          and thus we would like to run more (yield to avoid
- *          blocking other activities for too long)
- *         #GNUNET_NO if we are done running tasks (yield to block)
- *         #GNUNET_SYSERR on error, e.g. no tasks were ready
+ * Function called by external event loop implementations to tell the
+ * scheduler to run some of the tasks that are ready. Must be called
+ * only after #GNUNET_SCHEDULER_driver_init has been called and before 
+ * #GNUNET_SCHEDULER_driver_done is called.
+ * This function may return even though there are tasks left to run
+ * just to give other tasks a chance as well.  If we return #GNUNET_YES,
+ * the event loop implementation should call this function again as
+ * soon as possible, while if we return #GNUNET_NO it must block until
+ * either the operating system has more work (the scheduler has no more
+ * work to do right now) or the timeout set by the scheduler (using the
+ * set_wakeup callback) is reached.
+ *
+ * @param sh scheduler handle that was returned by
+ *        #GNUNET_SCHEDULER_driver_init
+ * @return #GNUNET_YES if there are more tasks that are ready,
+ *         and thus we would like to run more (yield to avoid
+ *         blocking other activities for too long) #GNUNET_NO
+ *         if we are done running tasks (yield to block)
  */
 int
-GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh);
+GNUNET_SCHEDULER_do_work (struct GNUNET_SCHEDULER_Handle *sh);
 
 
 /**
- * API a driver has to implement for
- * #GNUNET_SCHEDULER_run_with_driver().
+ * API an external event loop has to implement for
+ * #GNUNET_SCHEDULER_driver_init.
  */
 struct GNUNET_SCHEDULER_Driver
 {
@@ -311,23 +314,6 @@ struct GNUNET_SCHEDULER_Driver
   (*set_wakeup)(void *cls,
                struct GNUNET_TIME_Absolute dt);
 
-  /**
-   * Event loop's "main" function, to be called from
-   * #GNUNET_SCHEDULER_run_with_driver() to actually
-   * launch the loop. The loop should run as long as
-   * tasks (added by the add callback) are available 
-   * OR the wakeup time (added by the set_wakeup
-   * callback) is not FOREVER.
-   *
-   * @param cls closure
-   * @param sh scheduler handle to pass to
-   *     #GNUNET_SCHEDULER_run_from_driver()
-   * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
-   */
-  int
-  (*loop)(void *cls,
-         struct GNUNET_SCHEDULER_Handle *sh);
-
 };
 
 
@@ -341,25 +327,55 @@ typedef void
 
 
 /**
- * Initialize and run scheduler.  This function will return when all
- * tasks have completed.  On systems with signals, receiving a SIGTERM
- * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
- * to be run after the active task is complete.  As a result, SIGTERM
- * causes all shutdown tasks to be scheduled with reason
- * #GNUNET_SCHEDULER_REASON_SHUTDOWN.  (However, tasks added
- * afterwards will execute normally!).  Note that any particular
- * signal will only shut down one scheduler; applications should
- * always only create a single scheduler.
+ * Function called by external event loop implementations to initialize
+ * the scheduler. An external implementation has to provide @a driver
+ * which contains callbacks for the scheduler (see definition of struct
+ * #GNUNET_SCHEDULER_Driver) for instructing the external implementation
+ * to watch for events. If it detects any event it is expected to call
+ * #GNUNET_SCHEDULER_do_work to let the scheduler handle it. If an event
+ * is related to a specific task (e.g. the scheduler gave instructions
+ * to watch a file descriptor), the external implementation is expected
+ * to mark that task ready before by calling #GNUNET_SCHEDULER_task_ready.
+
+ * This function has to be called before any tasks are scheduled and
+ * before GNUNET_SCHEDULER_do_work is called for the first time. It 
+ * allocates resources that have to be freed again by calling
+ * #GNUNET_SCHEDULER_driver_done.
  *
- * @param driver drive to use for the event loop
- * @param task task to run first (and immediately)
- * @param task_cls closure of @a task
- * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
+ * This function installs the same signal handlers as 
+ * #GNUNET_SCHEDULER_run. This means SIGTERM (and other similar signals)
+ * will induce a call to #GNUNET_SCHEDULER_shutdown during the next
+ * call to #GNUNET_SCHEDULER_do_work. As a result, SIGTERM causes all
+ * active tasks to be scheduled with reason
+ * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added afterwards
+ * will execute normally!). Note that any particular signal will only
+ * shut down one scheduler; applications should always only create a
+ * single scheduler.
+ *
+ * @param driver to use for the event loop
+ * @return handle to be passed to #GNUNET_SCHEDULER_do_work and
+ *         #GNUNET_SCHEDULER_driver_done
  */
-int
-GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
-                                 GNUNET_SCHEDULER_TaskCallback task,
-                                 void *task_cls);
+struct GNUNET_SCHEDULER_Handle *
+GNUNET_SCHEDULER_driver_init (const struct GNUNET_SCHEDULER_Driver *driver);
+
+
+/**
+ * Counter-part of #GNUNET_SCHEDULER_driver_init. Has to be called
+ * by external event loop implementations after the scheduler has
+ * shut down. This is the case if both of the following conditions
+ * are met:
+ * 
+ * - all tasks the scheduler has added through the driver's add
+ *   callback have been removed again through the driver's del
+ *   callback
+ * - the timeout the scheduler has set through the driver's
+ *   add_wakeup callback is FOREVER
+ *
+ * @param sh the handle returned by #GNUNET_SCHEDULER_driver_init
+ */
+void
+GNUNET_SCHEDULER_driver_done (struct GNUNET_SCHEDULER_Handle *sh);
 
 
 /**
@@ -462,10 +478,15 @@ GNUNET_SCHEDULER_get_task_context (void);
 
 /**
  * Cancel the task with the specified identifier.
- * The task must not yet have run.
+ * The task must not yet have run. Only allowed to be called as long as the
+ * scheduler is running, that is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
  * @param task id of the task to cancel
- * @return the closure of the callback of the cancelled task
+ * @return original closure of the task
  */
 void *
 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task);
@@ -634,6 +655,12 @@ GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
  * 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.
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
  * @param delay when should this operation time out?
  * @param rfd read file-descriptor
@@ -656,6 +683,12 @@ GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
  * 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.
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
  * @param delay when should this operation time out?
  * @param priority priority to use for the task
@@ -678,9 +711,16 @@ GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
  * specified file descriptor is ready for writing.  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.
+ * socket operation is ready.  It will be run with the priority of
+ * the calling task.
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
- * @param delay when should this operation time out?
+ * @param delay when should this operation time out?
  * @param wfd write file-descriptor
  * @param task main function of the task
  * @param task_cls closure of @a task
@@ -700,6 +740,12 @@ GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
  * 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.
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
  * @param delay when should this operation time out?
  * @param priority priority of the task
@@ -707,7 +753,7 @@ GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
  * @param on_read whether to poll the file-descriptor for readability
  * @param on_write whether to poll the file-descriptor for writability
  * @param task main function of the task
- * @param task_cls closure of @a task
+ * @param task_cls closure of task
  * @return unique task identifier for the job
  *         only valid until "task" is started!
  */
@@ -720,19 +766,26 @@ GNUNET_SCHEDULER_add_net_with_priority  (struct GNUNET_TIME_Relative delay,
                                          GNUNET_SCHEDULER_TaskCallback task,
                                          void *task_cls);
 
+
 /**
  * Schedule a new task to be run with a 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.
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
- * @param delay when should this operation time out?
+ * @param delay when should this operation time out?
  * @param rfd read file-descriptor
  * @param task main function of the task
  * @param task_cls closure of @a task
  * @return unique task identifier for the job
- *         only valid until "task" is started!
+ *         only valid until @a task is started!
  */
 struct GNUNET_SCHEDULER_Task *
 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
@@ -747,8 +800,14 @@ GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
  * 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.
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
- * @param delay when should this operation time out?
+ * @param delay when should this operation time out?
  * @param wfd write file-descriptor
  * @param task main function of the task
  * @param task_cls closure of @a task
@@ -768,6 +827,12 @@ GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
  * 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.
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
  * @param delay when should this operation time out?
  * @param priority priority of the task
@@ -801,8 +866,14 @@ GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
  * (prerequisite-run)
  * && (delay-ready
  *     || any-rs-ready
- *     || any-ws-ready)
+ *     || any-ws-ready) )
  * </code>
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
  * @param prio how important is this task?
  * @param delay how long should we wait?
@@ -811,7 +882,7 @@ GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
  * @param task main function of the task
  * @param task_cls closure of @a task
  * @return unique task identifier for the job
- *         only valid until "task" is started!
+ *         only valid until @a task is started!
  */
 struct GNUNET_SCHEDULER_Task *
 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
index 98334490b4f3794ce43c1ee728bfbe5f1662e951..05a951e19836373fcf719258df01c4206d0cbaf7 100644 (file)
@@ -73,7 +73,7 @@
 
 /**
  * Argument to be passed from the driver to
- * #GNUNET_SCHEDULER_run_from_driver().  Contains the
+ * #GNUNET_SCHEDULER_do_work().  Contains the
  * scheduler's internal state.
  */
 struct GNUNET_SCHEDULER_Handle
@@ -91,6 +91,40 @@ struct GNUNET_SCHEDULER_Handle
    * @deprecated
    */
   struct GNUNET_NETWORK_FDSet *ws;
+
+  /**
+   * context of the SIGINT handler
+   */
+  struct GNUNET_SIGNAL_Context *shc_int;
+
+  /**
+   * context of the SIGTERM handler
+   */
+  struct GNUNET_SIGNAL_Context *shc_term;
+
+#if (SIGTERM != GNUNET_TERM_SIG)
+  /**
+   * context of the TERM_SIG handler
+   */
+  struct GNUNET_SIGNAL_Context *shc_gterm;
+#endif
+
+#ifndef MINGW
+  /**
+   * context of the SIGQUIT handler
+   */
+  struct GNUNET_SIGNAL_Context *shc_quit;
+
+  /**
+   * context of the SIGHUP handler
+   */
+  struct GNUNET_SIGNAL_Context *shc_hup;
+
+  /**
+   * context of hte SIGPIPE handler
+   */
+  struct GNUNET_SIGNAL_Context *shc_pipe;
+#endif
 };
 
 
@@ -265,7 +299,7 @@ struct DriverContext
 
 /**
  * The driver used for the event loop. Will be handed over to
- * the scheduler in #GNUNET_SCHEDULER_run_from_driver(), peristed
+ * the scheduler in #GNUNET_SCHEDULER_do_work(), persisted
  * there in this variable for later use in functions like
  * #GNUNET_SCHEDULER_add_select(), #add_without_sets() and
  * #GNUNET_SCHEDULER_cancel().
@@ -382,11 +416,6 @@ static struct GNUNET_SCHEDULER_TaskContext tc;
  */
 static void *scheduler_select_cls;
 
-/**
- * Scheduler handle used for the driver functions
- */
-static struct GNUNET_SCHEDULER_Handle sh;
-
 
 /**
  * Sets the select function to use in the scheduler (scheduler_select).
@@ -659,6 +688,10 @@ shutdown_if_no_lifeness ()
 }
 
 
+int
+select_loop (struct GNUNET_SCHEDULER_Handle *sh, struct DriverContext *context);
+
+
 /**
  * Initialize and run scheduler.  This function will return when all
  * tasks have completed.  On systems with signals, receiving a SIGTERM
@@ -677,16 +710,21 @@ void
 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
                       void *task_cls)
 {
+  struct GNUNET_SCHEDULER_Handle *sh;
   struct GNUNET_SCHEDULER_Driver *driver;
   struct DriverContext context = {.scheduled_head = NULL,
                                   .scheduled_tail = NULL,
-                                  .timeout = GNUNET_TIME_UNIT_FOREVER_ABS};
+                                  .timeout = GNUNET_TIME_absolute_get ()};
 
   driver = GNUNET_SCHEDULER_driver_select ();
   driver->cls = &context;
-
-  GNUNET_SCHEDULER_run_with_driver (driver, task, task_cls);
-
+  sh = GNUNET_SCHEDULER_driver_init (driver);
+  GNUNET_SCHEDULER_add_with_reason_and_priority (task,
+                                                 task_cls,
+                                                 GNUNET_SCHEDULER_REASON_STARTUP,
+                                                 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
+  select_loop (sh, &context);
+  GNUNET_SCHEDULER_driver_done (sh);
   GNUNET_free (driver);
 }
 
@@ -900,8 +938,11 @@ shutdown_pipe_cb (void *cls)
 /**
  * Cancel the task with the specified identifier.
  * The task must not yet have run. Only allowed to be called as long as the
- * scheduler is running (#GNUNET_SCHEDULER_run or
- * #GNUNET_SCHEDULER_run_with_driver has been called and has not returned yet).
+ * scheduler is running, that is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
  * @param task id of the task to cancel
  * @return original closure of the task
@@ -1401,9 +1442,12 @@ add_without_sets (struct GNUNET_TIME_Relative delay,
  * 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.
- * Only allowed to be called as long as the scheduler is running
- * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
- * called and has not returned yet).
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
  * @param delay when should this operation time out?
  * @param rfd read file-descriptor
@@ -1431,9 +1475,12 @@ GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
  * 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.
- * Only allowed to be called as long as the scheduler is running
- * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
- * called and has not returned yet).
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
  * @param delay when should this operation time out?
  * @param priority priority to use for the task
@@ -1465,9 +1512,12 @@ GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
  * scheduled for execution once either the delay has expired or the
  * socket operation is ready.  It will be run with the priority of
  * the calling task.
- * Only allowed to be called as long as the scheduler is running
- * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
- * called and has not returned yet).
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
  * @param delay when should this operation time out?
  * @param wfd write file-descriptor
@@ -1495,9 +1545,12 @@ GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
  * 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.
- * Only allowed to be called as long as the scheduler is running
- * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
- * called and has not returned yet).
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
  * @param delay when should this operation time out?
  * @param priority priority of the task
@@ -1554,9 +1607,12 @@ GNUNET_SCHEDULER_add_net_with_priority  (struct GNUNET_TIME_Relative delay,
  * 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.
- * Only allowed to be called as long as the scheduler is running
- * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
- * called and has not returned yet).
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
  * @param delay when should this operation time out?
  * @param rfd read file-descriptor
@@ -1583,9 +1639,12 @@ GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
  * 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.
- * Only allowed to be called as long as the scheduler is running
- * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
- * called and has not returned yet).
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
  * @param delay when should this operation time out?
  * @param wfd write file-descriptor
@@ -1612,9 +1671,12 @@ GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
  * 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.
- * Only allowed to be called as long as the scheduler is running
- * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
- * called and has not returned yet).
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
  * @param delay when should this operation time out?
  * @param priority priority of the task
@@ -1729,9 +1791,12 @@ extract_handles (const struct GNUNET_NETWORK_FDSet *fdset,
  *     || any-rs-ready
  *     || any-ws-ready) )
  * </code>
- * Only allowed to be called as long as the scheduler is running
- * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
- * called and has not returned yet).
+ * Only allowed to be called as long as the scheduler is running, that
+ * is one of the following conditions is met:
+ *
+ * - #GNUNET_SCHEDULER_run has been called and has not returned yet
+ * - #GNUNET_SCHEDULER_driver_init has been run and
+ *   #GNUNET_SCHEDULER_driver_done has not been called yet
  *
  * @param prio how important is this task?
  * @param delay how long should we wait?
@@ -1886,23 +1951,27 @@ GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
 
 
 /**
- * Function called by the driver to tell the scheduler to run some of
- * the tasks that are ready.  This function may return even though
- * there are tasks left to run just to give other tasks a chance as
- * well.  If we return #GNUNET_YES, the driver should call this
- * function again as soon as possible, while if we return #GNUNET_NO
- * it must block until either the operating system has more work (the
- * scheduler has no more work to do right now) or the timeout set by
- * the scheduler (using the set_wakeup callback) is reached.
+ * Function called by external event loop implementations to tell the
+ * scheduler to run some of the tasks that are ready. Must be called
+ * only after #GNUNET_SCHEDULER_driver_init has been called and before 
+ * #GNUNET_SCHEDULER_driver_done is called.
+ * This function may return even though there are tasks left to run
+ * just to give other tasks a chance as well.  If we return #GNUNET_YES,
+ * the event loop implementation should call this function again as
+ * soon as possible, while if we return #GNUNET_NO it must block until
+ * either the operating system has more work (the scheduler has no more
+ * work to do right now) or the timeout set by the scheduler (using the
+ * set_wakeup callback) is reached.
  *
- * @param sh scheduler handle that was given to the `loop`
+ * @param sh scheduler handle that was returned by
+ *        #GNUNET_SCHEDULER_driver_init
  * @return #GNUNET_YES if there are more tasks that are ready,
- *          and thus we would like to run more (yield to avoid
- *          blocking other activities for too long)
- *         #GNUNET_NO if we are done running tasks (yield to block)
+ *         and thus we would like to run more (yield to avoid
+ *         blocking other activities for too long) #GNUNET_NO
+ *         if we are done running tasks (yield to block)
  */
 int
-GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh)
+GNUNET_SCHEDULER_do_work (struct GNUNET_SCHEDULER_Handle *sh)
 {
   enum GNUNET_SCHEDULER_Priority p;
   struct GNUNET_SCHEDULER_Task *pos;
@@ -1955,7 +2024,7 @@ GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh)
        * also be a programming error in the driver though.
        */
       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                  "GNUNET_SCHEDULER_run_from_driver did not find any ready "
+                  "GNUNET_SCHEDULER_do_work did not find any ready "
                   "tasks and timeout has not been reached yet.");
       return GNUNET_NO;
     }
@@ -2056,43 +2125,46 @@ GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh)
 
 
 /**
- * Initialize and run scheduler.  This function will return when all
- * tasks have completed.  On systems with signals, receiving a SIGTERM
- * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
- * to be run after the active task is complete.  As a result, SIGTERM
- * causes all shutdown tasks to be scheduled with reason
- * #GNUNET_SCHEDULER_REASON_SHUTDOWN.  (However, tasks added
- * afterwards will execute normally!).  Note that any particular
- * signal will only shut down one scheduler; applications should
- * always only create a single scheduler.
+ * Function called by external event loop implementations to initialize
+ * the scheduler. An external implementation has to provide @a driver
+ * which contains callbacks for the scheduler (see definition of struct
+ * #GNUNET_SCHEDULER_Driver) for instructing the external implementation
+ * to watch for events. If it detects any event it is expected to call
+ * #GNUNET_SCHEDULER_do_work to let the scheduler handle it. If an event
+ * is related to a specific task (e.g. the scheduler gave instructions
+ * to watch a file descriptor), the external implementation is expected
+ * to mark that task ready before by calling #GNUNET_SCHEDULER_task_ready.
+
+ * This function has to be called before any tasks are scheduled and
+ * before GNUNET_SCHEDULER_do_work is called for the first time. It 
+ * allocates resources that have to be freed again by calling
+ * #GNUNET_SCHEDULER_driver_done.
  *
- * @param driver drive to use for the event loop
- * @param task task to run first (and immediately)
- * @param task_cls closure of @a task
- * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
+ * This function installs the same signal handlers as 
+ * #GNUNET_SCHEDULER_run. This means SIGTERM (and other similar signals)
+ * will induce a call to #GNUNET_SCHEDULER_shutdown during the next
+ * call to #GNUNET_SCHEDULER_do_work. As a result, SIGTERM causes all
+ * active tasks to be scheduled with reason
+ * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added afterwards
+ * will execute normally!). Note that any particular signal will only
+ * shut down one scheduler; applications should always only create a
+ * single scheduler.
+ *
+ * @param driver to use for the event loop
+ * @return handle to be passed to #GNUNET_SCHEDULER_do_work and
+ *         #GNUNET_SCHEDULER_driver_done
  */
-int
-GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
-                                  GNUNET_SCHEDULER_TaskCallback task,
-                                  void *task_cls)
+struct GNUNET_SCHEDULER_Handle *
+GNUNET_SCHEDULER_driver_init (const struct GNUNET_SCHEDULER_Driver *driver)
 {
-  int ret;
-  struct GNUNET_SIGNAL_Context *shc_int;
-  struct GNUNET_SIGNAL_Context *shc_term;
-#if (SIGTERM != GNUNET_TERM_SIG)
-  struct GNUNET_SIGNAL_Context *shc_gterm;
-#endif
-#ifndef MINGW
-  struct GNUNET_SIGNAL_Context *shc_quit;
-  struct GNUNET_SIGNAL_Context *shc_hup;
-  struct GNUNET_SIGNAL_Context *shc_pipe;
-#endif
+  struct GNUNET_SCHEDULER_Handle *sh;
   struct GNUNET_SCHEDULER_Task tsk;
   const struct GNUNET_DISK_FileHandle *pr;
 
   /* general set-up */
   GNUNET_assert (NULL == active_task);
   GNUNET_assert (NULL == shutdown_pipe_handle);
+  sh = GNUNET_new (struct GNUNET_SCHEDULER_Handle);
   shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
                                            GNUNET_NO,
                                            GNUNET_NO,
@@ -2106,21 +2178,21 @@ GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
   /* install signal handlers */
   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);
+  sh->shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
+                                               &sighandler_shutdown);
+  sh->shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
+                                                &sighandler_shutdown);
 #if (SIGTERM != GNUNET_TERM_SIG)
-  shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
-               &sighandler_shutdown);
+  sh->shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
+                                                 &sighandler_shutdown);
 #endif
 #ifndef MINGW
-  shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
-              &sighandler_pipe);
-  shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
-              &sighandler_shutdown);
-  shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
-             &sighandler_shutdown);
+  sh->shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
+                                                &sighandler_pipe);
+  sh->shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
+                                                &sighandler_shutdown);
+  sh->shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
+                                               &sighandler_shutdown);
 #endif
 
   /* Setup initial tasks */
@@ -2139,19 +2211,33 @@ GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
                                     &shutdown_pipe_cb,
                                     NULL);
   current_lifeness = GNUNET_YES;
-  GNUNET_SCHEDULER_add_with_reason_and_priority (task,
-                                                 task_cls,
-                                                 GNUNET_SCHEDULER_REASON_STARTUP,
-                                                 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
   active_task = NULL;
   scheduler_driver->set_wakeup (scheduler_driver->cls,
                                 get_timeout ());
   /* begin main event loop */
-  sh.rs = GNUNET_NETWORK_fdset_create ();
-  sh.ws = GNUNET_NETWORK_fdset_create ();
-  GNUNET_NETWORK_fdset_handle_set (sh.rs, pr);
-  ret = driver->loop (driver->cls,
-                      &sh);
+  sh->rs = GNUNET_NETWORK_fdset_create ();
+  sh->ws = GNUNET_NETWORK_fdset_create ();
+  GNUNET_NETWORK_fdset_handle_set (sh->rs, pr);
+  return sh;
+}
+
+
+/**
+ * Counter-part of #GNUNET_SCHEDULER_driver_init. Has to be called
+ * by external event loop implementations after the scheduler has
+ * shut down. This is the case if both of the following conditions
+ * are met:
+ * 
+ * - all tasks the scheduler has added through the driver's add
+ *   callback have been removed again through the driver's del
+ *   callback
+ * - the timeout the scheduler has set through the driver's
+ *   add_wakeup callback is FOREVER
+ *
+ * @param sh the handle returned by #GNUNET_SCHEDULER_driver_init
+ */
+void GNUNET_SCHEDULER_driver_done (struct GNUNET_SCHEDULER_Handle *sh)
+{
   GNUNET_assert (NULL == pending_head);
   GNUNET_assert (NULL == pending_timeout_head);
   GNUNET_assert (NULL == shutdown_head);
@@ -2159,97 +2245,34 @@ GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
   {
     GNUNET_assert (NULL == ready_head[i]);
   }
-  GNUNET_NETWORK_fdset_destroy (sh.rs);
-  GNUNET_NETWORK_fdset_destroy (sh.ws);
+  GNUNET_NETWORK_fdset_destroy (sh->rs);
+  GNUNET_NETWORK_fdset_destroy (sh->ws);
 
   /* uninstall signal handlers */
-  GNUNET_SIGNAL_handler_uninstall (shc_int);
-  GNUNET_SIGNAL_handler_uninstall (shc_term);
+  GNUNET_SIGNAL_handler_uninstall (sh->shc_int);
+  GNUNET_SIGNAL_handler_uninstall (sh->shc_term);
 #if (SIGTERM != GNUNET_TERM_SIG)
-  GNUNET_SIGNAL_handler_uninstall (shc_gterm);
+  GNUNET_SIGNAL_handler_uninstall (sh->shc_gterm);
 #endif
 #ifndef MINGW
-  GNUNET_SIGNAL_handler_uninstall (shc_pipe);
-  GNUNET_SIGNAL_handler_uninstall (shc_quit);
-  GNUNET_SIGNAL_handler_uninstall (shc_hup);
+  GNUNET_SIGNAL_handler_uninstall (sh->shc_pipe);
+  GNUNET_SIGNAL_handler_uninstall (sh->shc_quit);
+  GNUNET_SIGNAL_handler_uninstall (sh->shc_hup);
 #endif
   GNUNET_DISK_pipe_close (shutdown_pipe_handle);
   shutdown_pipe_handle = NULL;
   scheduler_driver = NULL;
-  return ret;
-}
-
-
-int
-select_add (void *cls,
-            struct GNUNET_SCHEDULER_Task *task,
-            struct GNUNET_SCHEDULER_FdInfo *fdi)
-{
-  struct DriverContext *context = cls;
-  GNUNET_assert (NULL != context);
-  GNUNET_assert (NULL != task);
-  GNUNET_assert (NULL != fdi);
-  GNUNET_assert (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et) ||
-                 0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et));
-
-  if (!((NULL != fdi->fd) ^ (NULL != fdi->fh)) || (fdi->sock < 0))
-  {
-    /* exactly one out of {fd, hf} must be != NULL and the OS handle must be valid */
-    return GNUNET_SYSERR;
-  }
-
-  struct Scheduled *scheduled = GNUNET_new (struct Scheduled);
-  scheduled->task = task;
-  scheduled->fdi = fdi;
-  scheduled->et = fdi->et;
-
-  GNUNET_CONTAINER_DLL_insert (context->scheduled_head,
-                               context->scheduled_tail,
-                               scheduled);
-  return GNUNET_OK;
+  GNUNET_free (sh);
 }
 
 
 int
-select_del (void *cls,
-            struct GNUNET_SCHEDULER_Task *task)
-{
-  struct DriverContext *context;
-  struct Scheduled *pos;
-  int ret;
-
-  GNUNET_assert (NULL != cls);
-
-  context = cls;
-  ret = GNUNET_SYSERR;
-  pos = context->scheduled_head;
-  while (NULL != pos)
-  {
-    struct Scheduled *next = pos->next;
-    if (pos->task == task)
-    {
-      GNUNET_CONTAINER_DLL_remove (context->scheduled_head,
-                                   context->scheduled_tail,
-                                   pos);
-      GNUNET_free (pos);
-      ret = GNUNET_OK;
-    }
-    pos = next;
-  }
-  return ret;
-}
-
-
-int
-select_loop (void *cls,
-             struct GNUNET_SCHEDULER_Handle *sh)
+select_loop (struct GNUNET_SCHEDULER_Handle *sh, struct DriverContext *context)
 {
   struct GNUNET_NETWORK_FDSet *rs;
   struct GNUNET_NETWORK_FDSet *ws;
-  struct DriverContext *context;
   int select_result;
 
-  context = cls;
   GNUNET_assert (NULL != context);
   rs = GNUNET_NETWORK_fdset_create ();
   ws = GNUNET_NETWORK_fdset_create ();
@@ -2350,7 +2373,7 @@ select_loop (void *cls,
         }
       }
     }
-    if (GNUNET_YES == GNUNET_SCHEDULER_run_from_driver (sh))
+    if (GNUNET_YES == GNUNET_SCHEDULER_do_work (sh))
     {
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "scheduler has more tasks ready!\n");
@@ -2362,6 +2385,66 @@ select_loop (void *cls,
 }
 
 
+int
+select_add (void *cls,
+            struct GNUNET_SCHEDULER_Task *task,
+            struct GNUNET_SCHEDULER_FdInfo *fdi)
+{
+  struct DriverContext *context = cls;
+  GNUNET_assert (NULL != context);
+  GNUNET_assert (NULL != task);
+  GNUNET_assert (NULL != fdi);
+  GNUNET_assert (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et) ||
+                 0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et));
+
+  if (!((NULL != fdi->fd) ^ (NULL != fdi->fh)) || (fdi->sock < 0))
+  {
+    /* exactly one out of {fd, hf} must be != NULL and the OS handle must be valid */
+    return GNUNET_SYSERR;
+  }
+
+  struct Scheduled *scheduled = GNUNET_new (struct Scheduled);
+  scheduled->task = task;
+  scheduled->fdi = fdi;
+  scheduled->et = fdi->et;
+
+  GNUNET_CONTAINER_DLL_insert (context->scheduled_head,
+                               context->scheduled_tail,
+                               scheduled);
+  return GNUNET_OK;
+}
+
+
+int
+select_del (void *cls,
+            struct GNUNET_SCHEDULER_Task *task)
+{
+  struct DriverContext *context;
+  struct Scheduled *pos;
+  int ret;
+
+  GNUNET_assert (NULL != cls);
+
+  context = cls;
+  ret = GNUNET_SYSERR;
+  pos = context->scheduled_head;
+  while (NULL != pos)
+  {
+    struct Scheduled *next = pos->next;
+    if (pos->task == task)
+    {
+      GNUNET_CONTAINER_DLL_remove (context->scheduled_head,
+                                   context->scheduled_tail,
+                                   pos);
+      GNUNET_free (pos);
+      ret = GNUNET_OK;
+    }
+    pos = next;
+  }
+  return ret;
+}
+
+
 void
 select_set_wakeup (void *cls,
                    struct GNUNET_TIME_Absolute dt)
@@ -2384,7 +2467,6 @@ GNUNET_SCHEDULER_driver_select ()
   struct GNUNET_SCHEDULER_Driver *select_driver;
   select_driver = GNUNET_new (struct GNUNET_SCHEDULER_Driver);
 
-  select_driver->loop = &select_loop;
   select_driver->add = &select_add;
   select_driver->del = &select_del;
   select_driver->set_wakeup = &select_set_wakeup;