2 This file is part of GNUnet
3 Copyright (C) 2009-2017 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file util/scheduler.c
22 * @brief schedule computations using continuation passing style
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util-scheduler", __VA_ARGS__)
33 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-scheduler", syscall)
40 * Use lsof to generate file descriptor reports on select error?
41 * (turn off for stable releases).
43 #define USE_LSOF GNUNET_NO
46 * Obtain trace information for all scheduler calls that schedule tasks.
48 #define EXECINFO GNUNET_NO
51 * Check each file descriptor before adding
53 #define DEBUG_FDS GNUNET_NO
56 * Depth of the traces collected via EXECINFO.
58 #define MAX_TRACE_DEPTH 50
62 * Should we figure out which tasks are delayed for a while
63 * before they are run? (Consider using in combination with EXECINFO).
65 #define PROFILE_DELAYS GNUNET_NO
68 * Task that were in the queue for longer than this are reported if
69 * PROFILE_DELAYS is active.
71 #define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
75 * Argument to be passed from the driver to
76 * #GNUNET_SCHEDULER_run_from_driver(). Contains the
77 * scheduler's internal state.
79 struct GNUNET_SCHEDULER_Handle
82 * Passed here to avoid constantly allocating/deallocating
83 * this element, but generally we want to get rid of this.
86 struct GNUNET_NETWORK_FDSet *rs;
89 * Passed here to avoid constantly allocating/deallocating
90 * this element, but generally we want to get rid of this.
93 struct GNUNET_NETWORK_FDSet *ws;
98 * Entry in list of pending tasks.
100 struct GNUNET_SCHEDULER_Task
103 * This is a linked list.
105 struct GNUNET_SCHEDULER_Task *next;
108 * This is a linked list.
110 struct GNUNET_SCHEDULER_Task *prev;
113 * Function to run when ready.
115 GNUNET_SCHEDULER_TaskCallback callback;
118 * Closure for the @e callback.
123 * Information about which FDs are ready for this task (and why).
125 struct GNUNET_SCHEDULER_FdInfo *fds;
128 * Storage location used for @e fds if we want to avoid
129 * a separate malloc() call in the common case that this
130 * task is only about a single FD.
132 struct GNUNET_SCHEDULER_FdInfo fdx;
135 * Size of the @e fds array.
137 unsigned int fds_len;
140 * Do we own the network and file handles referenced by the FdInfo
141 * structs in the fds array. This will only be GNUNET_YES if the
142 * task was created by the #GNUNET_SCHEDULER_add_select function.
147 * Absolute timeout value for the task, or
148 * #GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
150 struct GNUNET_TIME_Absolute timeout;
154 * When was the task scheduled?
156 struct GNUNET_TIME_Absolute start_time;
160 * Why is the task ready? Set after task is added to ready queue.
161 * Initially set to zero. All reasons that have already been
162 * satisfied (i.e. read or write ready) will be set over time.
164 enum GNUNET_SCHEDULER_Reason reason;
169 enum GNUNET_SCHEDULER_Priority priority;
172 * Set if we only wait for reading from a single FD, otherwise -1.
177 * Set if we only wait for writing to a single FD, otherwise -1.
182 * Should the existence of this task in the queue be counted as
183 * reason to not shutdown the scheduler?
188 * Is this task run on shutdown?
193 * Is this task in the ready list?
199 * Array of strings which make up a backtrace from the point when this
200 * task was scheduled (essentially, who scheduled the task?)
202 char **backtrace_strings;
205 * Size of the backtrace_strings array
207 int num_backtrace_strings;
214 * A struct representing an event the select driver is waiting for
218 struct Scheduled *prev;
220 struct Scheduled *next;
223 * the task, the event is related to
225 struct GNUNET_SCHEDULER_Task *task;
228 * information about the network socket / file descriptor where
229 * the event is expected to occur
231 struct GNUNET_SCHEDULER_FdInfo *fdi;
234 * the event types (multiple event types can be ORed) the select
235 * driver is expected to wait for
237 enum GNUNET_SCHEDULER_EventType et;
242 * Driver context used by GNUNET_SCHEDULER_run
247 * the head of a DLL containing information about the events the
248 * select driver is waiting for
250 struct Scheduled *scheduled_head;
253 * the tail of a DLL containing information about the events the
254 * select driver is waiting for
256 struct Scheduled *scheduled_tail;
259 * the time when the select driver will wake up again (after
262 struct GNUNET_TIME_Absolute timeout;
267 * The driver used for the event loop. Will be handed over to
268 * the scheduler in #GNUNET_SCHEDULER_run_from_driver(), peristed
269 * there in this variable for later use in functions like
270 * #GNUNET_SCHEDULER_add_select(), #add_without_sets() and
271 * #GNUNET_SCHEDULER_cancel().
273 static const struct GNUNET_SCHEDULER_Driver *scheduler_driver;
276 * Head of list of tasks waiting for an event.
278 static struct GNUNET_SCHEDULER_Task *pending_head;
281 * Tail of list of tasks waiting for an event.
283 static struct GNUNET_SCHEDULER_Task *pending_tail;
286 * Head of list of tasks waiting for shutdown.
288 static struct GNUNET_SCHEDULER_Task *shutdown_head;
291 * Tail of list of tasks waiting for shutdown.
293 static struct GNUNET_SCHEDULER_Task *shutdown_tail;
296 * List of tasks waiting ONLY for a timeout event.
297 * Sorted by timeout (earliest first). Used so that
298 * we do not traverse the list of these tasks when
299 * building select sets (we just look at the head
300 * to determine the respective timeout ONCE).
302 static struct GNUNET_SCHEDULER_Task *pending_timeout_head;
305 * List of tasks waiting ONLY for a timeout event.
306 * Sorted by timeout (earliest first). Used so that
307 * we do not traverse the list of these tasks when
308 * building select sets (we just look at the head
309 * to determine the respective timeout ONCE).
311 static struct GNUNET_SCHEDULER_Task *pending_timeout_tail;
314 * Last inserted task waiting ONLY for a timeout event.
315 * Used to (heuristically) speed up insertion.
317 static struct GNUNET_SCHEDULER_Task *pending_timeout_last;
320 * ID of the task that is running right now.
322 static struct GNUNET_SCHEDULER_Task *active_task;
325 * Head of list of tasks ready to run right now, grouped by importance.
327 static struct GNUNET_SCHEDULER_Task *ready_head[GNUNET_SCHEDULER_PRIORITY_COUNT];
330 * Tail of list of tasks ready to run right now, grouped by importance.
332 static struct GNUNET_SCHEDULER_Task *ready_tail[GNUNET_SCHEDULER_PRIORITY_COUNT];
335 * Task for installing parent control handlers (it might happen that the
336 * scheduler is shutdown before this task is executed, so
337 * GNUNET_SCHEDULER_shutdown must cancel it in that case)
339 static struct GNUNET_SCHEDULER_Task *install_parent_control_task;
342 * Task for reading from a pipe that signal handlers will use to initiate
345 static struct GNUNET_SCHEDULER_Task *shutdown_pipe_task;
348 * Number of tasks on the ready list.
350 static unsigned int ready_count;
353 * Priority of the task running right now. Only
354 * valid while a task is running.
356 static enum GNUNET_SCHEDULER_Priority current_priority;
359 * Priority of the highest task added in the current select
362 static enum GNUNET_SCHEDULER_Priority max_priority_added;
365 * Value of the 'lifeness' flag for the current task.
367 static int current_lifeness;
370 * Function to use as a select() in the scheduler.
371 * If NULL, we use GNUNET_NETWORK_socket_select().
373 static GNUNET_SCHEDULER_select scheduler_select;
376 * Task context of the current task.
378 static struct GNUNET_SCHEDULER_TaskContext tc;
381 * Closure for #scheduler_select.
383 static void *scheduler_select_cls;
386 * Scheduler handle used for the driver functions
388 static struct GNUNET_SCHEDULER_Handle sh;
392 * Sets the select function to use in the scheduler (scheduler_select).
394 * @param new_select new select function to use
395 * @param new_select_cls closure for @a new_select
396 * @return previously used select function, NULL for default
399 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
400 void *new_select_cls)
402 scheduler_select = new_select;
403 scheduler_select_cls = new_select_cls;
408 * Check that the given priority is legal (and return it).
410 * @param p priority value to check
411 * @return p on success, 0 on error
413 static enum GNUNET_SCHEDULER_Priority
414 check_priority (enum GNUNET_SCHEDULER_Priority p)
416 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
419 return 0; /* make compiler happy */
424 * chooses the nearest timeout from all pending tasks, to be used
425 * to tell the driver the next wakeup time (using its set_wakeup
428 struct GNUNET_TIME_Absolute
431 struct GNUNET_SCHEDULER_Task *pos;
432 struct GNUNET_TIME_Absolute now;
433 struct GNUNET_TIME_Absolute timeout;
435 pos = pending_timeout_head;
436 now = GNUNET_TIME_absolute_get ();
437 timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
440 if (0 != pos->reason)
446 timeout = pos->timeout;
449 for (pos = pending_head; NULL != pos; pos = pos->next)
451 if (0 != pos->reason)
455 else if ((pos->timeout.abs_value_us != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) &&
456 (timeout.abs_value_us > pos->timeout.abs_value_us))
458 timeout = pos->timeout;
466 * Put a task that is ready for execution into the ready queue.
468 * @param task task ready for execution
471 queue_ready_task (struct GNUNET_SCHEDULER_Task *task)
473 enum GNUNET_SCHEDULER_Priority p = check_priority (task->priority);
475 GNUNET_CONTAINER_DLL_insert (ready_head[p],
478 task->in_ready_list = GNUNET_YES;
484 * Request the shutdown of a scheduler. Marks all tasks
485 * awaiting shutdown as ready. Note that tasks
486 * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call
487 * will be delayed until the next shutdown signal.
490 GNUNET_SCHEDULER_shutdown ()
492 struct GNUNET_SCHEDULER_Task *pos;
494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
495 "GNUNET_SCHEDULER_shutdown\n");
496 if (NULL != install_parent_control_task)
498 GNUNET_SCHEDULER_cancel (install_parent_control_task);
499 install_parent_control_task = NULL;
501 if (NULL != shutdown_pipe_task)
503 GNUNET_SCHEDULER_cancel (shutdown_pipe_task);
504 shutdown_pipe_task = NULL;
506 while (NULL != (pos = shutdown_head))
508 GNUNET_CONTAINER_DLL_remove (shutdown_head,
511 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
512 queue_ready_task (pos);
518 * Output stack trace of task @a t.
520 * @param t task to dump stack trace of
523 dump_backtrace (struct GNUNET_SCHEDULER_Task *t)
526 for (unsigned int i = 0; i < t->num_backtrace_strings; i++)
527 LOG (GNUNET_ERROR_TYPE_WARNING,
528 "Task %p trace %u: %s\n",
531 t->backtrace_strings[i]);
539 * Destroy a task (release associated resources)
541 * @param t task to destroy
544 destroy_task (struct GNUNET_SCHEDULER_Task *t)
548 LOG (GNUNET_ERROR_TYPE_DEBUG,
549 "destroying task %p\n",
552 if (GNUNET_YES == t->own_handles)
554 for (i = 0; i != t->fds_len; ++i)
556 const struct GNUNET_NETWORK_Handle *fd = t->fds[i].fd;
557 const struct GNUNET_DISK_FileHandle *fh = t->fds[i].fh;
560 GNUNET_NETWORK_socket_free_memory_only_ ((struct GNUNET_NETWORK_Handle *) fd);
564 // FIXME: on WIN32 this is not enough! A function
565 // GNUNET_DISK_file_free_memory_only would be nice
566 GNUNET_free ((void *) fh);
572 GNUNET_array_grow (t->fds, t->fds_len, 0);
575 GNUNET_free (t->backtrace_strings);
582 * Pipe used to communicate shutdown via signal.
584 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
587 * Process ID of this process at the time we installed the various
593 * Signal handler called for SIGPIPE.
605 // * Wait for a short time.
606 // * Sleeps for @a ms ms (as that should be long enough for virtually all
607 // * modern systems to context switch and allow another process to do
608 // * some 'real' work).
610 // * @param ms how many ms to wait
613 //short_wait (unsigned int ms)
615 // struct GNUNET_TIME_Relative timeout;
617 // timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
618 // (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
623 * Signal handler called for signals that should cause us to shutdown.
626 sighandler_shutdown ()
629 int old_errno = errno; /* backup errno */
631 if (getpid () != my_pid)
632 exit (1); /* we have fork'ed since the signal handler was created,
633 * ignore the signal, see https://gnunet.org/vfork discussion */
634 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
635 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
642 shutdown_if_no_lifeness ()
644 struct GNUNET_SCHEDULER_Task *t;
648 for (t = pending_head; NULL != t; t = t->next)
649 if (GNUNET_YES == t->lifeness)
651 for (t = shutdown_head; NULL != t; t = t->next)
652 if (GNUNET_YES == t->lifeness)
654 for (t = pending_timeout_head; NULL != t; t = t->next)
655 if (GNUNET_YES == t->lifeness)
658 GNUNET_SCHEDULER_shutdown ();
663 * Initialize and run scheduler. This function will return when all
664 * tasks have completed. On systems with signals, receiving a SIGTERM
665 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown()
666 * to be run after the active task is complete. As a result, SIGTERM
667 * causes all active tasks to be scheduled with reason
668 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
669 * afterwards will execute normally!). Note that any particular signal
670 * will only shut down one scheduler; applications should always only
671 * create a single scheduler.
673 * @param task task to run immediately
674 * @param task_cls closure of @a task
677 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
680 struct GNUNET_SCHEDULER_Driver *driver;
681 struct DriverContext context = {.scheduled_head = NULL,
682 .scheduled_tail = NULL,
683 .timeout = GNUNET_TIME_UNIT_FOREVER_ABS};
685 driver = GNUNET_SCHEDULER_driver_select ();
686 driver->cls = &context;
688 GNUNET_SCHEDULER_run_with_driver (driver, task, task_cls);
690 GNUNET_free (driver);
695 * Obtain the task context, giving the reason why the current task was
698 * @return current tasks' scheduler context
700 const struct GNUNET_SCHEDULER_TaskContext *
701 GNUNET_SCHEDULER_get_task_context ()
703 GNUNET_assert (NULL != active_task);
709 * Get information about the current load of this scheduler. Use this
710 * function to determine if an elective task should be added or simply
711 * dropped (if the decision should be made based on the number of
712 * tasks ready to run).
714 * @param p priority level to look at
715 * @return number of tasks pending right now
718 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
720 struct GNUNET_SCHEDULER_Task *pos;
723 GNUNET_assert (NULL != active_task);
724 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
726 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
727 p = current_priority;
729 for (pos = ready_head[check_priority (p)]; NULL != pos; pos = pos->next)
736 init_fd_info (struct GNUNET_SCHEDULER_Task *t,
737 const struct GNUNET_NETWORK_Handle *const *read_nh,
738 unsigned int read_nh_len,
739 const struct GNUNET_NETWORK_Handle *const *write_nh,
740 unsigned int write_nh_len,
741 const struct GNUNET_DISK_FileHandle *const *read_fh,
742 unsigned int read_fh_len,
743 const struct GNUNET_DISK_FileHandle *const *write_fh,
744 unsigned int write_fh_len)
746 // FIXME: if we have exactly two network handles / exactly two file handles
747 // and they are equal, we can make one FdInfo with both
748 // GNUNET_SCHEDULER_ET_IN and GNUNET_SCHEDULER_ET_OUT set.
749 struct GNUNET_SCHEDULER_FdInfo *fdi;
751 t->fds_len = read_nh_len + write_nh_len + read_fh_len + write_fh_len;
756 if (1 == read_nh_len)
758 GNUNET_assert (NULL != read_nh);
759 GNUNET_assert (NULL != *read_nh);
761 fdi->et = GNUNET_SCHEDULER_ET_IN;
762 fdi->sock = GNUNET_NETWORK_get_fd (*read_nh);
763 t->read_fd = fdi->sock;
766 else if (1 == write_nh_len)
768 GNUNET_assert (NULL != write_nh);
769 GNUNET_assert (NULL != *write_nh);
771 fdi->et = GNUNET_SCHEDULER_ET_OUT;
772 fdi->sock = GNUNET_NETWORK_get_fd (*write_nh);
774 t->write_fd = fdi->sock;
776 else if (1 == read_fh_len)
778 GNUNET_assert (NULL != read_fh);
779 GNUNET_assert (NULL != *read_fh);
781 fdi->et = GNUNET_SCHEDULER_ET_IN;
782 fdi->sock = (*read_fh)->fd; // FIXME: does not work under WIN32
783 t->read_fd = fdi->sock;
788 GNUNET_assert (NULL != write_fh);
789 GNUNET_assert (NULL != *write_fh);
791 fdi->et = GNUNET_SCHEDULER_ET_OUT;
792 fdi->sock = (*write_fh)->fd; // FIXME: does not work under WIN32
794 t->write_fd = fdi->sock;
799 fdi = GNUNET_new_array (t->fds_len, struct GNUNET_SCHEDULER_FdInfo);
804 for (i = 0; i != read_nh_len; ++i)
806 fdi->fd = read_nh[i];
807 GNUNET_assert (NULL != fdi->fd);
808 fdi->et = GNUNET_SCHEDULER_ET_IN;
809 fdi->sock = GNUNET_NETWORK_get_fd (read_nh[i]);
812 for (i = 0; i != write_nh_len; ++i)
814 fdi->fd = write_nh[i];
815 GNUNET_assert (NULL != fdi->fd);
816 fdi->et = GNUNET_SCHEDULER_ET_OUT;
817 fdi->sock = GNUNET_NETWORK_get_fd (write_nh[i]);
820 for (i = 0; i != read_fh_len; ++i)
822 fdi->fh = read_fh[i];
823 GNUNET_assert (NULL != fdi->fh);
824 fdi->et = GNUNET_SCHEDULER_ET_IN;
825 fdi->sock = (read_fh[i])->fd; // FIXME: does not work under WIN32
828 for (i = 0; i != write_fh_len; ++i)
830 fdi->fh = write_fh[i];
831 GNUNET_assert (NULL != fdi->fh);
832 fdi->et = GNUNET_SCHEDULER_ET_OUT;
833 fdi->sock = (write_fh[i])->fd; // FIXME: does not work under WIN32
841 * calls the given function @a func on each FdInfo related to @a t.
842 * Optionally updates the event type field in each FdInfo after calling
846 * @param driver_func the function to call with each FdInfo contained in
848 * @param if_not_ready only call @a driver_func on FdInfos that are not
850 * @param et the event type to be set in each FdInfo after calling
851 * @a driver_func on it, or -1 if no updating not desired.
854 driver_add_multiple (struct GNUNET_SCHEDULER_Task *t)
856 struct GNUNET_SCHEDULER_FdInfo *fdi;
857 int success = GNUNET_YES;
859 for (unsigned int i = 0; i != t->fds_len; ++i)
862 success = scheduler_driver->add (scheduler_driver->cls,
865 fdi->et = GNUNET_SCHEDULER_ET_NONE;
867 if (GNUNET_YES != success)
869 LOG (GNUNET_ERROR_TYPE_ERROR,
870 "driver could not add task\n");
876 install_parent_control_handler (void *cls)
878 install_parent_control_task = NULL;
879 GNUNET_OS_install_parent_control_handler (NULL);
884 shutdown_pipe_cb (void *cls)
887 const struct GNUNET_DISK_FileHandle *pr;
889 shutdown_pipe_task = NULL;
890 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
891 GNUNET_DISK_PIPE_END_READ);
892 GNUNET_assert (! GNUNET_DISK_handle_invalid (pr));
893 /* consume the signal */
894 GNUNET_DISK_file_read (pr, &c, sizeof (c));
895 /* mark all active tasks as ready due to shutdown */
896 GNUNET_SCHEDULER_shutdown ();
901 * Cancel the task with the specified identifier.
902 * The task must not yet have run. Only allowed to be called as long as the
903 * scheduler is running (#GNUNET_SCHEDULER_run or
904 * #GNUNET_SCHEDULER_run_with_driver has been called and has not returned yet).
906 * @param task id of the task to cancel
907 * @return original closure of the task
910 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
912 enum GNUNET_SCHEDULER_Priority p;
916 LOG (GNUNET_ERROR_TYPE_DEBUG,
917 "canceling task %p\n",
920 /* scheduler must be running */
921 GNUNET_assert (NULL != scheduler_driver);
922 GNUNET_assert ( (NULL != active_task) ||
923 (GNUNET_NO == task->lifeness) );
924 is_fd_task = (NULL != task->fds);
927 int del_result = scheduler_driver->del (scheduler_driver->cls, task);
928 if (GNUNET_OK != del_result)
930 LOG (GNUNET_ERROR_TYPE_ERROR,
931 "driver could not delete task\n");
935 if (! task->in_ready_list)
939 GNUNET_CONTAINER_DLL_remove (pending_head,
943 else if (GNUNET_YES == task->on_shutdown)
945 GNUNET_CONTAINER_DLL_remove (shutdown_head,
951 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
952 pending_timeout_tail,
954 if (pending_timeout_last == task)
955 pending_timeout_last = NULL;
960 p = check_priority (task->priority);
961 GNUNET_CONTAINER_DLL_remove (ready_head[p],
966 ret = task->callback_cls;
973 * Initialize backtrace data for task @a t
975 * @param t task to initialize
978 init_backtrace (struct GNUNET_SCHEDULER_Task *t)
981 void *backtrace_array[MAX_TRACE_DEPTH];
983 t->num_backtrace_strings
984 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
985 t->backtrace_strings =
986 backtrace_symbols (backtrace_array,
987 t->num_backtrace_strings);
996 * Continue the current execution with the given function. This is
997 * similar to the other "add" functions except that there is no delay
998 * and the reason code can be specified.
1000 * @param task main function of the task
1001 * @param task_cls closure for @a task
1002 * @param reason reason for task invocation
1003 * @param priority priority to use for the task
1006 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
1008 enum GNUNET_SCHEDULER_Reason reason,
1009 enum GNUNET_SCHEDULER_Priority priority)
1011 struct GNUNET_SCHEDULER_Task *t;
1013 GNUNET_assert (NULL != task);
1014 GNUNET_assert ((NULL != active_task) ||
1015 (GNUNET_SCHEDULER_REASON_STARTUP == reason));
1016 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1020 t->callback_cls = task_cls;
1022 t->start_time = GNUNET_TIME_absolute_get ();
1025 t->priority = check_priority (priority);
1026 t->lifeness = current_lifeness;
1027 LOG (GNUNET_ERROR_TYPE_DEBUG,
1028 "Adding continuation task %p\n",
1031 queue_ready_task (t);
1036 * Schedule a new task to be run at the specified time. The task
1037 * will be scheduled for execution at time @a at.
1039 * @param at time when the operation should run
1040 * @param priority priority to use for the task
1041 * @param task main function of the task
1042 * @param task_cls closure of @a task
1043 * @return unique task identifier for the job
1044 * only valid until @a task is started!
1046 struct GNUNET_SCHEDULER_Task *
1047 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1048 enum GNUNET_SCHEDULER_Priority priority,
1049 GNUNET_SCHEDULER_TaskCallback task,
1052 struct GNUNET_SCHEDULER_Task *t;
1053 struct GNUNET_SCHEDULER_Task *pos;
1054 struct GNUNET_SCHEDULER_Task *prev;
1056 GNUNET_assert (NULL != active_task);
1057 GNUNET_assert (NULL != task);
1058 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1060 t->callback_cls = task_cls;
1064 t->start_time = GNUNET_TIME_absolute_get ();
1067 t->priority = check_priority (priority);
1068 t->lifeness = current_lifeness;
1069 /* try tail first (optimization in case we are
1070 * appending to a long list of tasks with timeouts) */
1071 if ( (NULL == pending_timeout_head) ||
1072 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us) )
1074 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1075 pending_timeout_tail,
1080 /* first move from heuristic start backwards to before start time */
1081 prev = pending_timeout_last;
1082 while ( (NULL != prev) &&
1083 (prev->timeout.abs_value_us > t->timeout.abs_value_us) )
1085 /* now, move from heuristic start (or head of list) forward to insertion point */
1087 pos = pending_timeout_head;
1090 while ((NULL != pos) && (pos->timeout.abs_value_us <= t->timeout.abs_value_us))
1095 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1096 pending_timeout_tail,
1100 /* finally, update heuristic insertion point to last insertion... */
1101 pending_timeout_last = t;
1103 LOG (GNUNET_ERROR_TYPE_DEBUG,
1112 * Schedule a new task to be run with a specified delay. The task
1113 * will be scheduled for execution once the delay has expired.
1115 * @param delay when should this operation time out?
1116 * @param priority priority to use for the task
1117 * @param task main function of the task
1118 * @param task_cls closure of @a task
1119 * @return unique task identifier for the job
1120 * only valid until @a task is started!
1122 struct GNUNET_SCHEDULER_Task *
1123 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1124 enum GNUNET_SCHEDULER_Priority priority,
1125 GNUNET_SCHEDULER_TaskCallback task,
1128 return GNUNET_SCHEDULER_add_at_with_priority (GNUNET_TIME_relative_to_absolute (delay),
1136 * Schedule a new task to be run with a specified priority.
1138 * @param prio how important is the new task?
1139 * @param task main function of the task
1140 * @param task_cls closure of @a task
1141 * @return unique task identifier for the job
1142 * only valid until @a task is started!
1144 struct GNUNET_SCHEDULER_Task *
1145 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1146 GNUNET_SCHEDULER_TaskCallback task,
1149 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1157 * Schedule a new task to be run at the specified time. The task
1158 * will be scheduled for execution once specified time has been
1159 * reached. It will be run with the DEFAULT priority.
1161 * @param at time at which this operation should run
1162 * @param task main function of the task
1163 * @param task_cls closure of @a task
1164 * @return unique task identifier for the job
1165 * only valid until @a task is started!
1167 struct GNUNET_SCHEDULER_Task *
1168 GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1169 GNUNET_SCHEDULER_TaskCallback task,
1172 return GNUNET_SCHEDULER_add_at_with_priority (at,
1173 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1180 * Schedule a new task to be run with a specified delay. The task
1181 * will be scheduled for execution once the delay has expired. It
1182 * will be run with the DEFAULT priority.
1184 * @param delay when should this operation time out?
1185 * @param task main function of the task
1186 * @param task_cls closure of @a task
1187 * @return unique task identifier for the job
1188 * only valid until @a task is started!
1190 struct GNUNET_SCHEDULER_Task *
1191 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1192 GNUNET_SCHEDULER_TaskCallback task,
1195 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1196 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1203 * Schedule a new task to be run as soon as possible. Note that this
1204 * does not guarantee that this will be the next task that is being
1205 * run, as other tasks with higher priority (or that are already ready
1206 * to run) might get to run first. Just as with delays, clients must
1207 * not rely on any particular order of execution between tasks
1208 * scheduled concurrently.
1210 * The task will be run with the DEFAULT priority.
1212 * @param task main function of the task
1213 * @param task_cls closure of @a task
1214 * @return unique task identifier for the job
1215 * only valid until @a task is started!
1217 struct GNUNET_SCHEDULER_Task *
1218 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
1221 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO,
1228 * Schedule a new task to be run on shutdown, that is when a CTRL-C
1229 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
1232 * @param task main function of the task
1233 * @param task_cls closure of @a task
1234 * @return unique task identifier for the job
1235 * only valid until @a task is started!
1237 struct GNUNET_SCHEDULER_Task *
1238 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1241 struct GNUNET_SCHEDULER_Task *t;
1243 GNUNET_assert (NULL != active_task);
1244 GNUNET_assert (NULL != task);
1245 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1247 t->callback_cls = task_cls;
1251 t->start_time = GNUNET_TIME_absolute_get ();
1253 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1254 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1255 t->on_shutdown = GNUNET_YES;
1256 t->lifeness = GNUNET_NO;
1257 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1260 LOG (GNUNET_ERROR_TYPE_DEBUG,
1261 "Adding shutdown task %p\n",
1269 * Schedule a new task to be run as soon as possible with the
1270 * (transitive) ignore-shutdown flag either explicitly set or
1271 * explicitly enabled. This task (and all tasks created from it,
1272 * other than by another call to this function) will either count or
1273 * not count for the "lifeness" of the process. This API is only
1274 * useful in a few special cases.
1276 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
1277 * @param task main function of the task
1278 * @param task_cls closure of @a task
1279 * @return unique task identifier for the job
1280 * only valid until @a task is started!
1282 struct GNUNET_SCHEDULER_Task *
1283 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1284 GNUNET_SCHEDULER_TaskCallback task,
1287 struct GNUNET_SCHEDULER_Task *ret;
1289 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1290 ret->lifeness = lifeness;
1297 * check a raw file descriptor and abort if it is bad (for debugging purposes)
1299 * @param t the task related to the file descriptor
1300 * @param raw_fd the raw file descriptor to check
1303 check_fd (struct GNUNET_SCHEDULER_Task *t, int raw_fd)
1307 int flags = fcntl (raw_fd, F_GETFD);
1309 if ((flags == -1) && (errno == EBADF))
1311 LOG (GNUNET_ERROR_TYPE_ERROR,
1312 "Got invalid file descriptor %d!\n",
1323 * Schedule a new task to be run with a specified delay or when any of
1324 * the specified file descriptor sets is ready. The delay can be used
1325 * as a timeout on the socket(s) being ready. The task will be
1326 * scheduled for execution once either the delay has expired or any of
1327 * the socket operations is ready. This is the most general
1328 * function of the "add" family. Note that the "prerequisite_task"
1329 * must be satisfied in addition to any of the other conditions. In
1330 * other words, the task will be started when
1332 * (prerequisite-run)
1338 * @param delay how long should we wait?
1339 * @param priority priority to use
1340 * @param rfd file descriptor we want to read (can be -1)
1341 * @param wfd file descriptors we want to write (can be -1)
1342 * @param task main function of the task
1343 * @param task_cls closure of @a task
1344 * @return unique task identifier for the job
1345 * only valid until @a task is started!
1348 static struct GNUNET_SCHEDULER_Task *
1349 add_without_sets (struct GNUNET_TIME_Relative delay,
1350 enum GNUNET_SCHEDULER_Priority priority,
1351 const struct GNUNET_NETWORK_Handle *read_nh,
1352 const struct GNUNET_NETWORK_Handle *write_nh,
1353 const struct GNUNET_DISK_FileHandle *read_fh,
1354 const struct GNUNET_DISK_FileHandle *write_fh,
1355 GNUNET_SCHEDULER_TaskCallback task,
1358 struct GNUNET_SCHEDULER_Task *t;
1360 GNUNET_assert (NULL != active_task);
1361 GNUNET_assert (NULL != task);
1362 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1373 t->callback_cls = task_cls;
1375 check_fd (t, NULL != read_nh ? GNUNET_NETWORK_get_fd (read_nh) : -1);
1376 check_fd (t, NULL != write_nh ? GNUNET_NETWORK_get_fd (write_nh) : -1);
1377 check_fd (t, NULL != read_fh ? read_fh->fd : -1);
1378 check_fd (t, NULL != write_fh ? write_fh->fd : -1);
1381 t->start_time = GNUNET_TIME_absolute_get ();
1383 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1384 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1385 t->lifeness = current_lifeness;
1386 GNUNET_CONTAINER_DLL_insert (pending_head,
1389 driver_add_multiple (t);
1390 max_priority_added = GNUNET_MAX (max_priority_added,
1399 * Schedule a new task to be run with a specified delay or when the
1400 * specified file descriptor is ready for reading. The delay can be
1401 * used as a timeout on the socket being ready. The task will be
1402 * scheduled for execution once either the delay has expired or the
1403 * socket operation is ready. It will be run with the DEFAULT priority.
1404 * Only allowed to be called as long as the scheduler is running
1405 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1406 * called and has not returned yet).
1408 * @param delay when should this operation time out?
1409 * @param rfd read file-descriptor
1410 * @param task main function of the task
1411 * @param task_cls closure of @a task
1412 * @return unique task identifier for the job
1413 * only valid until @a task is started!
1415 struct GNUNET_SCHEDULER_Task *
1416 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1417 struct GNUNET_NETWORK_Handle *rfd,
1418 GNUNET_SCHEDULER_TaskCallback task,
1421 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1422 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1423 rfd, task, task_cls);
1428 * Schedule a new task to be run with a specified priority and to be
1429 * run after the specified delay or when the specified file descriptor
1430 * is ready for reading. The delay can be used as a timeout on the
1431 * socket being ready. The task will be scheduled for execution once
1432 * either the delay has expired or the socket operation is ready. It
1433 * will be run with the DEFAULT priority.
1434 * Only allowed to be called as long as the scheduler is running
1435 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1436 * called and has not returned yet).
1438 * @param delay when should this operation time out?
1439 * @param priority priority to use for the task
1440 * @param rfd read file-descriptor
1441 * @param task main function of the task
1442 * @param task_cls closure of @a task
1443 * @return unique task identifier for the job
1444 * only valid until @a task is started!
1446 struct GNUNET_SCHEDULER_Task *
1447 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1448 enum GNUNET_SCHEDULER_Priority priority,
1449 struct GNUNET_NETWORK_Handle *rfd,
1450 GNUNET_SCHEDULER_TaskCallback task,
1453 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1462 * Schedule a new task to be run with a specified delay or when the
1463 * specified file descriptor is ready for writing. The delay can be
1464 * used as a timeout on the socket being ready. The task will be
1465 * scheduled for execution once either the delay has expired or the
1466 * socket operation is ready. It will be run with the priority of
1468 * Only allowed to be called as long as the scheduler is running
1469 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1470 * called and has not returned yet).
1472 * @param delay when should this operation time out?
1473 * @param wfd write file-descriptor
1474 * @param task main function of the task
1475 * @param task_cls closure of @a task
1476 * @return unique task identifier for the job
1477 * only valid until @a task is started!
1479 struct GNUNET_SCHEDULER_Task *
1480 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1481 struct GNUNET_NETWORK_Handle *wfd,
1482 GNUNET_SCHEDULER_TaskCallback task,
1485 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1486 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1488 GNUNET_NO, GNUNET_YES,
1493 * Schedule a new task to be run with a specified delay or when the
1494 * specified file descriptor is ready. The delay can be
1495 * used as a timeout on the socket being ready. The task will be
1496 * scheduled for execution once either the delay has expired or the
1497 * socket operation is ready.
1498 * Only allowed to be called as long as the scheduler is running
1499 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1500 * called and has not returned yet).
1502 * @param delay when should this operation time out?
1503 * @param priority priority of the task
1504 * @param fd file-descriptor
1505 * @param on_read whether to poll the file-descriptor for readability
1506 * @param on_write whether to poll the file-descriptor for writability
1507 * @param task main function of the task
1508 * @param task_cls closure of task
1509 * @return unique task identifier for the job
1510 * only valid until "task" is started!
1512 struct GNUNET_SCHEDULER_Task *
1513 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1514 enum GNUNET_SCHEDULER_Priority priority,
1515 struct GNUNET_NETWORK_Handle *fd,
1518 GNUNET_SCHEDULER_TaskCallback task,
1521 /* scheduler must be running */
1522 GNUNET_assert (NULL != scheduler_driver);
1525 struct GNUNET_NETWORK_FDSet *s;
1526 struct GNUNET_SCHEDULER_Task * ret;
1528 GNUNET_assert (NULL != fd);
1529 s = GNUNET_NETWORK_fdset_create ();
1530 GNUNET_NETWORK_fdset_set (s, fd);
1531 ret = GNUNET_SCHEDULER_add_select (
1534 on_write ? s : NULL,
1536 GNUNET_NETWORK_fdset_destroy (s);
1539 GNUNET_assert (on_read || on_write);
1540 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1541 return add_without_sets (delay, priority,
1542 on_read ? fd : NULL,
1543 on_write ? fd : NULL,
1552 * Schedule a new task to be run with a specified delay or when the
1553 * specified file descriptor is ready for reading. The delay can be
1554 * used as a timeout on the socket being ready. The task will be
1555 * scheduled for execution once either the delay has expired or the
1556 * socket operation is ready. It will be run with the DEFAULT priority.
1557 * Only allowed to be called as long as the scheduler is running
1558 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1559 * called and has not returned yet).
1561 * @param delay when should this operation time out?
1562 * @param rfd read file-descriptor
1563 * @param task main function of the task
1564 * @param task_cls closure of @a task
1565 * @return unique task identifier for the job
1566 * only valid until @a task is started!
1568 struct GNUNET_SCHEDULER_Task *
1569 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1570 const struct GNUNET_DISK_FileHandle *rfd,
1571 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1573 return GNUNET_SCHEDULER_add_file_with_priority (
1574 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1575 rfd, GNUNET_YES, GNUNET_NO,
1581 * Schedule a new task to be run with a specified delay or when the
1582 * specified file descriptor is ready for writing. The delay can be
1583 * used as a timeout on the socket being ready. The task will be
1584 * scheduled for execution once either the delay has expired or the
1585 * socket operation is ready. It will be run with the DEFAULT priority.
1586 * Only allowed to be called as long as the scheduler is running
1587 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1588 * called and has not returned yet).
1590 * @param delay when should this operation time out?
1591 * @param wfd write file-descriptor
1592 * @param task main function of the task
1593 * @param task_cls closure of @a task
1594 * @return unique task identifier for the job
1595 * only valid until @a task is started!
1597 struct GNUNET_SCHEDULER_Task *
1598 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1599 const struct GNUNET_DISK_FileHandle *wfd,
1600 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1602 return GNUNET_SCHEDULER_add_file_with_priority (
1603 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1604 wfd, GNUNET_NO, GNUNET_YES,
1610 * Schedule a new task to be run with a specified delay or when the
1611 * specified file descriptor is ready. The delay can be
1612 * used as a timeout on the socket being ready. The task will be
1613 * scheduled for execution once either the delay has expired or the
1614 * socket operation is ready.
1615 * Only allowed to be called as long as the scheduler is running
1616 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1617 * called and has not returned yet).
1619 * @param delay when should this operation time out?
1620 * @param priority priority of the task
1621 * @param fd file-descriptor
1622 * @param on_read whether to poll the file-descriptor for readability
1623 * @param on_write whether to poll the file-descriptor for writability
1624 * @param task main function of the task
1625 * @param task_cls closure of @a task
1626 * @return unique task identifier for the job
1627 * only valid until @a task is started!
1629 struct GNUNET_SCHEDULER_Task *
1630 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1631 enum GNUNET_SCHEDULER_Priority priority,
1632 const struct GNUNET_DISK_FileHandle *fd,
1633 int on_read, int on_write,
1634 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1636 /* scheduler must be running */
1637 GNUNET_assert (NULL != scheduler_driver);
1640 struct GNUNET_NETWORK_FDSet *s;
1641 struct GNUNET_SCHEDULER_Task * ret;
1643 GNUNET_assert (NULL != fd);
1644 s = GNUNET_NETWORK_fdset_create ();
1645 GNUNET_NETWORK_fdset_handle_set (s, fd);
1646 ret = GNUNET_SCHEDULER_add_select (
1649 on_write ? s : NULL,
1651 GNUNET_NETWORK_fdset_destroy (s);
1654 GNUNET_assert (on_read || on_write);
1655 GNUNET_assert (fd->fd >= 0);
1656 return add_without_sets (delay, priority,
1659 on_read ? fd : NULL,
1660 on_write ? fd : NULL,
1667 extract_handles (const struct GNUNET_NETWORK_FDSet *fdset,
1668 const struct GNUNET_NETWORK_Handle ***ntarget,
1669 unsigned int *extracted_nhandles,
1670 const struct GNUNET_DISK_FileHandle ***ftarget,
1671 unsigned int *extracted_fhandles)
1673 // FIXME: this implementation only works for unix, for WIN32 the file handles
1674 // in fdset must be handled separately
1675 const struct GNUNET_NETWORK_Handle **nhandles;
1676 const struct GNUNET_DISK_FileHandle **fhandles;
1677 unsigned int nhandles_len;
1678 unsigned int fhandles_len;
1684 for (int sock = 0; sock != fdset->nsds; ++sock)
1686 if (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (fdset, sock))
1688 struct GNUNET_NETWORK_Handle *nhandle;
1689 struct GNUNET_DISK_FileHandle *fhandle;
1691 nhandle = GNUNET_NETWORK_socket_box_native (sock);
1692 if (NULL != nhandle)
1694 GNUNET_array_append (nhandles, nhandles_len, nhandle);
1698 fhandle = GNUNET_DISK_get_handle_from_int_fd (sock);
1699 if (NULL != fhandle)
1701 GNUNET_array_append (fhandles, fhandles_len, fhandle);
1710 *ntarget = nhandles_len > 0 ? nhandles : NULL;
1711 *ftarget = fhandles_len > 0 ? fhandles : NULL;
1712 *extracted_nhandles = nhandles_len;
1713 *extracted_fhandles = fhandles_len;
1718 * Schedule a new task to be run with a specified delay or when any of
1719 * the specified file descriptor sets is ready. The delay can be used
1720 * as a timeout on the socket(s) being ready. The task will be
1721 * scheduled for execution once either the delay has expired or any of
1722 * the socket operations is ready. This is the most general
1723 * function of the "add" family. Note that the "prerequisite_task"
1724 * must be satisfied in addition to any of the other conditions. In
1725 * other words, the task will be started when
1727 * (prerequisite-run)
1730 * || any-ws-ready) )
1732 * Only allowed to be called as long as the scheduler is running
1733 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1734 * called and has not returned yet).
1736 * @param prio how important is this task?
1737 * @param delay how long should we wait?
1738 * @param rs set of file descriptors we want to read (can be NULL)
1739 * @param ws set of file descriptors we want to write (can be NULL)
1740 * @param task main function of the task
1741 * @param task_cls closure of @a task
1742 * @return unique task identifier for the job
1743 * only valid until @a task is started!
1745 struct GNUNET_SCHEDULER_Task *
1746 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1747 struct GNUNET_TIME_Relative delay,
1748 const struct GNUNET_NETWORK_FDSet *rs,
1749 const struct GNUNET_NETWORK_FDSet *ws,
1750 GNUNET_SCHEDULER_TaskCallback task,
1753 struct GNUNET_SCHEDULER_Task *t;
1754 const struct GNUNET_NETWORK_Handle **read_nhandles = NULL;
1755 const struct GNUNET_NETWORK_Handle **write_nhandles = NULL;
1756 const struct GNUNET_DISK_FileHandle **read_fhandles = NULL;
1757 const struct GNUNET_DISK_FileHandle **write_fhandles = NULL;
1758 unsigned int read_nhandles_len = 0;
1759 unsigned int write_nhandles_len = 0;
1760 unsigned int read_fhandles_len = 0;
1761 unsigned int write_fhandles_len = 0;
1763 /* scheduler must be running */
1764 GNUNET_assert (NULL != scheduler_driver);
1765 GNUNET_assert (NULL != active_task);
1766 GNUNET_assert (NULL != task);
1767 int no_rs = (NULL == rs);
1768 int no_ws = (NULL == ws);
1769 int empty_rs = (NULL != rs) && (0 == rs->nsds);
1770 int empty_ws = (NULL != ws) && (0 == ws->nsds);
1771 int no_fds = (no_rs && no_ws) ||
1772 (empty_rs && empty_ws) ||
1773 (no_rs && empty_ws) ||
1774 (no_ws && empty_rs);
1779 extract_handles (rs,
1783 &read_fhandles_len);
1787 extract_handles (ws,
1789 &write_nhandles_len,
1791 &write_fhandles_len);
1795 * here we consider the case that a GNUNET_NETWORK_FDSet might be empty
1796 * although its maximum FD number (nsds) is greater than 0. We handle
1797 * this case gracefully because some libraries such as libmicrohttpd
1798 * only provide a hint what the maximum FD number in an FD set might be
1799 * and not the exact FD number (see e.g. gnunet-rest-service.c)
1801 int no_fds_extracted = (0 == read_nhandles_len) &&
1802 (0 == read_fhandles_len) &&
1803 (0 == write_nhandles_len) &&
1804 (0 == write_fhandles_len);
1805 if (no_fds || no_fds_extracted)
1806 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1810 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1819 write_fhandles_len);
1821 t->callback_cls = task_cls;
1822 t->own_handles = GNUNET_YES;
1823 /* free the arrays of pointers to network / file handles, the actual
1824 * handles will be freed in destroy_task */
1825 GNUNET_array_grow (read_nhandles, read_nhandles_len, 0);
1826 GNUNET_array_grow (write_nhandles, write_nhandles_len, 0);
1827 GNUNET_array_grow (read_fhandles, read_fhandles_len, 0);
1828 GNUNET_array_grow (write_fhandles, write_fhandles_len, 0);
1830 t->start_time = GNUNET_TIME_absolute_get ();
1832 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1834 check_priority ((prio ==
1835 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1837 t->lifeness = current_lifeness;
1838 GNUNET_CONTAINER_DLL_insert (pending_head,
1841 driver_add_multiple (t);
1842 max_priority_added = GNUNET_MAX (max_priority_added,
1844 LOG (GNUNET_ERROR_TYPE_DEBUG,
1853 * Function used by event-loop implementations to signal the scheduler
1854 * that a particular @a task is ready due to an event specified in the
1855 * et field of @a fdi.
1857 * This function will then queue the task to notify the application
1858 * that the task is ready (with the respective priority).
1860 * @param task the task that is ready
1861 * @param fdi information about the related FD
1864 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1865 struct GNUNET_SCHEDULER_FdInfo *fdi)
1867 enum GNUNET_SCHEDULER_Reason reason;
1869 reason = task->reason;
1870 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1871 (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et)) )
1872 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
1873 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
1874 (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et)) )
1875 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
1876 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1877 task->reason = reason;
1878 if (GNUNET_NO == task->in_ready_list)
1880 GNUNET_CONTAINER_DLL_remove (pending_head,
1883 queue_ready_task (task);
1889 * Function called by the driver to tell the scheduler to run some of
1890 * the tasks that are ready. This function may return even though
1891 * there are tasks left to run just to give other tasks a chance as
1892 * well. If we return #GNUNET_YES, the driver should call this
1893 * function again as soon as possible, while if we return #GNUNET_NO
1894 * it must block until either the operating system has more work (the
1895 * scheduler has no more work to do right now) or the timeout set by
1896 * the scheduler (using the set_wakeup callback) is reached.
1898 * @param sh scheduler handle that was given to the `loop`
1899 * @return #GNUNET_OK if there are more tasks that are ready,
1900 * and thus we would like to run more (yield to avoid
1901 * blocking other activities for too long)
1902 * #GNUNET_NO if we are done running tasks (yield to block)
1903 * #GNUNET_SYSERR on error, e.g. no tasks were ready
1906 GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh)
1908 enum GNUNET_SCHEDULER_Priority p;
1909 struct GNUNET_SCHEDULER_Task *pos;
1910 struct GNUNET_TIME_Absolute now;
1912 /* check for tasks that reached the timeout! */
1913 now = GNUNET_TIME_absolute_get ();
1914 pos = pending_timeout_head;
1917 struct GNUNET_SCHEDULER_Task *next = pos->next;
1918 if (now.abs_value_us >= pos->timeout.abs_value_us)
1919 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1920 if (0 == pos->reason)
1922 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1923 pending_timeout_tail,
1925 if (pending_timeout_last == pos)
1926 pending_timeout_last = NULL;
1927 queue_ready_task (pos);
1933 struct GNUNET_SCHEDULER_Task *next = pos->next;
1934 if (now.abs_value_us >= pos->timeout.abs_value_us)
1936 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1937 GNUNET_CONTAINER_DLL_remove (pending_head,
1940 queue_ready_task (pos);
1945 if (0 == ready_count)
1947 LOG (GNUNET_ERROR_TYPE_ERROR,
1948 "GNUNET_SCHEDULER_run_from_driver was called, but no tasks are ready!\n");
1949 return GNUNET_SYSERR;
1952 /* find out which task priority level we are going to
1953 process this time */
1954 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
1955 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
1956 /* yes, p>0 is correct, 0 is "KEEP" which should
1957 * always be an empty queue (see assertion)! */
1958 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
1960 pos = ready_head[p];
1964 GNUNET_assert (NULL != pos); /* ready_count wrong? */
1966 /* process all tasks at this priority level, then yield */
1967 while (NULL != (pos = ready_head[p]))
1969 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1973 current_priority = pos->priority;
1974 current_lifeness = pos->lifeness;
1977 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
1978 DELAY_THRESHOLD.rel_value_us)
1980 LOG (GNUNET_ERROR_TYPE_DEBUG,
1981 "Task %p took %s to be scheduled\n",
1983 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
1987 tc.reason = pos->reason;
1988 GNUNET_NETWORK_fdset_zero (sh->rs);
1989 GNUNET_NETWORK_fdset_zero (sh->ws);
1990 // FIXME: do we have to remove FdInfos from fds if they are not ready?
1991 tc.fds_len = pos->fds_len;
1993 for (unsigned int i = 0; i != pos->fds_len; ++i)
1995 struct GNUNET_SCHEDULER_FdInfo *fdi = &pos->fds[i];
1996 if (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et))
1998 GNUNET_NETWORK_fdset_set_native (sh->rs,
2001 if (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et))
2003 GNUNET_NETWORK_fdset_set_native (sh->ws,
2007 tc.read_ready = sh->rs;
2008 tc.write_ready = sh->ws;
2009 LOG (GNUNET_ERROR_TYPE_DEBUG,
2010 "Running task %p\n",
2012 GNUNET_assert (NULL != pos->callback);
2013 pos->callback (pos->callback_cls);
2014 if (NULL != pos->fds)
2016 int del_result = scheduler_driver->del (scheduler_driver->cls, pos);
2017 if (GNUNET_OK != del_result)
2019 LOG (GNUNET_ERROR_TYPE_ERROR,
2020 "driver could not delete task %p\n", pos);
2025 dump_backtrace (pos);
2028 shutdown_if_no_lifeness ();
2029 if (0 == ready_count)
2031 scheduler_driver->set_wakeup (scheduler_driver->cls,
2035 scheduler_driver->set_wakeup (scheduler_driver->cls,
2036 GNUNET_TIME_absolute_get ());
2042 * Initialize and run scheduler. This function will return when all
2043 * tasks have completed. On systems with signals, receiving a SIGTERM
2044 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
2045 * to be run after the active task is complete. As a result, SIGTERM
2046 * causes all shutdown tasks to be scheduled with reason
2047 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
2048 * afterwards will execute normally!). Note that any particular
2049 * signal will only shut down one scheduler; applications should
2050 * always only create a single scheduler.
2052 * @param driver drive to use for the event loop
2053 * @param task task to run first (and immediately)
2054 * @param task_cls closure of @a task
2055 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2058 GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
2059 GNUNET_SCHEDULER_TaskCallback task,
2063 struct GNUNET_SIGNAL_Context *shc_int;
2064 struct GNUNET_SIGNAL_Context *shc_term;
2065 #if (SIGTERM != GNUNET_TERM_SIG)
2066 struct GNUNET_SIGNAL_Context *shc_gterm;
2069 struct GNUNET_SIGNAL_Context *shc_quit;
2070 struct GNUNET_SIGNAL_Context *shc_hup;
2071 struct GNUNET_SIGNAL_Context *shc_pipe;
2073 struct GNUNET_SCHEDULER_Task tsk;
2074 const struct GNUNET_DISK_FileHandle *pr;
2076 /* general set-up */
2077 GNUNET_assert (NULL == active_task);
2078 GNUNET_assert (NULL == shutdown_pipe_handle);
2079 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
2083 GNUNET_assert (NULL != shutdown_pipe_handle);
2084 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
2085 GNUNET_DISK_PIPE_END_READ);
2087 scheduler_driver = driver;
2089 /* install signal handlers */
2090 LOG (GNUNET_ERROR_TYPE_DEBUG,
2091 "Registering signal handlers\n");
2092 shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2093 &sighandler_shutdown);
2094 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2095 &sighandler_shutdown);
2096 #if (SIGTERM != GNUNET_TERM_SIG)
2097 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2098 &sighandler_shutdown);
2101 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2103 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2104 &sighandler_shutdown);
2105 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2106 &sighandler_shutdown);
2109 /* Setup initial tasks */
2110 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2111 current_lifeness = GNUNET_NO;
2116 install_parent_control_task =
2117 GNUNET_SCHEDULER_add_now (&install_parent_control_handler,
2119 shutdown_pipe_task =
2120 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2124 current_lifeness = GNUNET_YES;
2125 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
2127 GNUNET_SCHEDULER_REASON_STARTUP,
2128 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
2130 scheduler_driver->set_wakeup (scheduler_driver->cls,
2132 /* begin main event loop */
2133 sh.rs = GNUNET_NETWORK_fdset_create ();
2134 sh.ws = GNUNET_NETWORK_fdset_create ();
2135 GNUNET_NETWORK_fdset_handle_set (sh.rs, pr);
2136 ret = driver->loop (driver->cls,
2138 GNUNET_assert (NULL == pending_head);
2139 GNUNET_assert (NULL == pending_timeout_head);
2140 GNUNET_assert (NULL == shutdown_head);
2141 for (int i = 0; i != GNUNET_SCHEDULER_PRIORITY_COUNT; ++i)
2143 GNUNET_assert (NULL == ready_head[i]);
2145 GNUNET_NETWORK_fdset_destroy (sh.rs);
2146 GNUNET_NETWORK_fdset_destroy (sh.ws);
2148 /* uninstall signal handlers */
2149 GNUNET_SIGNAL_handler_uninstall (shc_int);
2150 GNUNET_SIGNAL_handler_uninstall (shc_term);
2151 #if (SIGTERM != GNUNET_TERM_SIG)
2152 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
2155 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
2156 GNUNET_SIGNAL_handler_uninstall (shc_quit);
2157 GNUNET_SIGNAL_handler_uninstall (shc_hup);
2159 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2160 shutdown_pipe_handle = NULL;
2161 scheduler_driver = NULL;
2167 select_add (void *cls,
2168 struct GNUNET_SCHEDULER_Task *task,
2169 struct GNUNET_SCHEDULER_FdInfo *fdi)
2171 struct DriverContext *context = cls;
2172 GNUNET_assert (NULL != context);
2173 GNUNET_assert (NULL != task);
2174 GNUNET_assert (NULL != fdi);
2175 GNUNET_assert (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et) ||
2176 0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et));
2178 if (!((NULL != fdi->fd) ^ (NULL != fdi->fh)) || (fdi->sock < 0))
2180 /* exactly one out of {fd, hf} must be != NULL and the OS handle must be valid */
2181 return GNUNET_SYSERR;
2184 struct Scheduled *scheduled = GNUNET_new (struct Scheduled);
2185 scheduled->task = task;
2186 scheduled->fdi = fdi;
2187 scheduled->et = fdi->et;
2189 GNUNET_CONTAINER_DLL_insert (context->scheduled_head,
2190 context->scheduled_tail,
2197 select_del (void *cls,
2198 struct GNUNET_SCHEDULER_Task *task)
2200 struct DriverContext *context;
2201 struct Scheduled *pos;
2204 GNUNET_assert (NULL != cls);
2207 ret = GNUNET_SYSERR;
2208 pos = context->scheduled_head;
2211 struct Scheduled *next = pos->next;
2212 if (pos->task == task)
2214 GNUNET_CONTAINER_DLL_remove (context->scheduled_head,
2215 context->scheduled_tail,
2227 select_loop (void *cls,
2228 struct GNUNET_SCHEDULER_Handle *sh)
2230 struct GNUNET_NETWORK_FDSet *rs;
2231 struct GNUNET_NETWORK_FDSet *ws;
2232 struct DriverContext *context;
2237 GNUNET_assert (NULL != context);
2238 rs = GNUNET_NETWORK_fdset_create ();
2239 ws = GNUNET_NETWORK_fdset_create ();
2240 tasks_ready = GNUNET_NO;
2241 while (NULL != context->scheduled_head ||
2242 GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != context->timeout.abs_value_us)
2244 LOG (GNUNET_ERROR_TYPE_DEBUG,
2245 "select timeout = %s\n",
2246 GNUNET_STRINGS_absolute_time_to_string (context->timeout));
2248 GNUNET_NETWORK_fdset_zero (rs);
2249 GNUNET_NETWORK_fdset_zero (ws);
2250 struct Scheduled *pos;
2251 for (pos = context->scheduled_head; NULL != pos; pos = pos->next)
2253 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et))
2255 GNUNET_NETWORK_fdset_set_native (rs, pos->fdi->sock);
2257 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et))
2259 GNUNET_NETWORK_fdset_set_native (ws, pos->fdi->sock);
2262 struct GNUNET_TIME_Relative time_remaining =
2263 GNUNET_TIME_absolute_get_remaining (context->timeout);
2264 if (NULL == scheduler_select)
2266 select_result = GNUNET_NETWORK_socket_select (rs,
2273 select_result = scheduler_select (scheduler_select_cls,
2279 if (select_result == GNUNET_SYSERR)
2284 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
2289 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
2292 if (0 != system (lsof))
2293 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
2298 struct Scheduled *s;
2299 for (s = context->scheduled_head; NULL != s; s = s->next)
2301 int flags = fcntl (s->fdi->sock, F_GETFD);
2302 if ((flags == -1) && (errno == EBADF))
2304 LOG (GNUNET_ERROR_TYPE_ERROR,
2305 "Got invalid file descriptor %d!\n",
2311 GNUNET_NETWORK_fdset_destroy (rs);
2312 GNUNET_NETWORK_fdset_destroy (ws);
2313 return GNUNET_SYSERR;
2315 if (select_result > 0)
2317 for (pos = context->scheduled_head; NULL != pos; pos = pos->next)
2319 int is_ready = GNUNET_NO;
2320 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et) &&
2321 GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, pos->fdi->sock))
2323 pos->fdi->et |= GNUNET_SCHEDULER_ET_IN;
2324 is_ready = GNUNET_YES;
2326 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et) &&
2327 GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, pos->fdi->sock))
2329 pos->fdi->et |= GNUNET_SCHEDULER_ET_OUT;
2330 is_ready = GNUNET_YES;
2332 if (GNUNET_YES == is_ready)
2334 GNUNET_SCHEDULER_task_ready (pos->task, pos->fdi);
2340 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
2341 if (now.abs_value_us < context->timeout.abs_value_us)
2343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2344 "select was expected to return at %" PRIu64 ", "
2345 "but returned already at %" PRIu64 "\n",
2346 context->timeout.abs_value_us,
2351 tasks_ready = GNUNET_SCHEDULER_run_from_driver (sh);
2352 GNUNET_assert (GNUNET_SYSERR != tasks_ready);
2354 GNUNET_NETWORK_fdset_destroy (rs);
2355 GNUNET_NETWORK_fdset_destroy (ws);
2361 select_set_wakeup (void *cls,
2362 struct GNUNET_TIME_Absolute dt)
2364 struct DriverContext *context = cls;
2366 GNUNET_assert (NULL != context);
2367 context->timeout = dt;
2372 * Obtain the driver for using select() as the event loop.
2374 * @return NULL on error
2376 struct GNUNET_SCHEDULER_Driver *
2377 GNUNET_SCHEDULER_driver_select ()
2379 struct GNUNET_SCHEDULER_Driver *select_driver;
2380 select_driver = GNUNET_new (struct GNUNET_SCHEDULER_Driver);
2382 select_driver->loop = &select_loop;
2383 select_driver->add = &select_add;
2384 select_driver->del = &select_del;
2385 select_driver->set_wakeup = &select_set_wakeup;
2387 return select_driver;
2391 /* end of scheduler.c */