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"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util-scheduler", __VA_ARGS__)
31 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-scheduler", syscall)
38 * Use lsof to generate file descriptor reports on select error?
39 * (turn off for stable releases).
41 #define USE_LSOF GNUNET_NO
44 * Obtain trace information for all scheduler calls that schedule tasks.
46 #define EXECINFO GNUNET_NO
49 * Check each file descriptor before adding
51 #define DEBUG_FDS GNUNET_NO
54 * Depth of the traces collected via EXECINFO.
56 #define MAX_TRACE_DEPTH 50
60 * Should we figure out which tasks are delayed for a while
61 * before they are run? (Consider using in combination with EXECINFO).
63 #define PROFILE_DELAYS GNUNET_NO
66 * Task that were in the queue for longer than this are reported if
67 * PROFILE_DELAYS is active.
69 #define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
73 * Argument to be passed from the driver to
74 * #GNUNET_SCHEDULER_run_from_driver(). Contains the
75 * scheduler's internal state.
77 struct GNUNET_SCHEDULER_Handle
80 * Passed here to avoid constantly allocating/deallocating
81 * this element, but generally we want to get rid of this.
84 struct GNUNET_NETWORK_FDSet *rs;
87 * Passed here to avoid constantly allocating/deallocating
88 * this element, but generally we want to get rid of this.
91 struct GNUNET_NETWORK_FDSet *ws;
96 * Entry in list of pending tasks.
98 struct GNUNET_SCHEDULER_Task
101 * This is a linked list.
103 struct GNUNET_SCHEDULER_Task *next;
106 * This is a linked list.
108 struct GNUNET_SCHEDULER_Task *prev;
111 * Function to run when ready.
113 GNUNET_SCHEDULER_TaskCallback callback;
116 * Closure for the @e callback.
121 * Information about which FDs are ready for this task (and why).
123 struct GNUNET_SCHEDULER_FdInfo *fds;
126 * Storage location used for @e fds if we want to avoid
127 * a separate malloc() call in the common case that this
128 * task is only about a single FD.
130 struct GNUNET_SCHEDULER_FdInfo fdx;
133 * Size of the @e fds array.
135 unsigned int fds_len;
138 * if this task is related to multiple FDs this array contains
139 * all FdInfo structs that were marked as ready by calling
140 * #GNUNET_SCHEDULER_task_ready
142 struct GNUNET_SCHEDULER_FdInfo *ready_fds;
145 * Size of the @e ready_fds array
147 unsigned int ready_fds_len;
150 * Do we own the network and file handles referenced by the FdInfo
151 * structs in the fds array. This will only be GNUNET_YES if the
152 * task was created by the #GNUNET_SCHEDULER_add_select function.
157 * Absolute timeout value for the task, or
158 * #GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
160 struct GNUNET_TIME_Absolute timeout;
164 * When was the task scheduled?
166 struct GNUNET_TIME_Absolute start_time;
170 * Why is the task ready? Set after task is added to ready queue.
171 * Initially set to zero. All reasons that have already been
172 * satisfied (i.e. read or write ready) will be set over time.
174 enum GNUNET_SCHEDULER_Reason reason;
179 enum GNUNET_SCHEDULER_Priority priority;
182 * Set if we only wait for reading from a single FD, otherwise -1.
187 * Set if we only wait for writing to a single FD, otherwise -1.
192 * Should the existence of this task in the queue be counted as
193 * reason to not shutdown the scheduler?
198 * Is this task run on shutdown?
203 * Is this task in the ready list?
209 * Array of strings which make up a backtrace from the point when this
210 * task was scheduled (essentially, who scheduled the task?)
212 char **backtrace_strings;
215 * Size of the backtrace_strings array
217 int num_backtrace_strings;
224 * A struct representing an event the select driver is waiting for
228 struct Scheduled *prev;
230 struct Scheduled *next;
233 * the task, the event is related to
235 struct GNUNET_SCHEDULER_Task *task;
238 * information about the network socket / file descriptor where
239 * the event is expected to occur
241 struct GNUNET_SCHEDULER_FdInfo *fdi;
244 * the event types (multiple event types can be ORed) the select
245 * driver is expected to wait for
247 enum GNUNET_SCHEDULER_EventType et;
252 * Driver context used by GNUNET_SCHEDULER_run
257 * the head of a DLL containing information about the events the
258 * select driver is waiting for
260 struct Scheduled *scheduled_head;
263 * the tail of a DLL containing information about the events the
264 * select driver is waiting for
266 struct Scheduled *scheduled_tail;
269 * the time until the select driver will wake up again (after
272 struct GNUNET_TIME_Relative timeout;
277 * The driver used for the event loop. Will be handed over to
278 * the scheduler in #GNUNET_SCHEDULER_run_from_driver(), peristed
279 * there in this variable for later use in functions like
280 * #GNUNET_SCHEDULER_add_select(), #add_without_sets() and
281 * #GNUNET_SCHEDULER_cancel().
283 static const struct GNUNET_SCHEDULER_Driver *scheduler_driver;
286 * Head of list of tasks waiting for an event.
288 static struct GNUNET_SCHEDULER_Task *pending_head;
291 * Tail of list of tasks waiting for an event.
293 static struct GNUNET_SCHEDULER_Task *pending_tail;
296 * Head of list of tasks waiting for shutdown.
298 static struct GNUNET_SCHEDULER_Task *shutdown_head;
301 * Tail of list of tasks waiting for shutdown.
303 static struct GNUNET_SCHEDULER_Task *shutdown_tail;
306 * List of tasks waiting ONLY for a timeout event.
307 * Sorted by timeout (earliest first). Used so that
308 * we do not traverse the list of these tasks when
309 * building select sets (we just look at the head
310 * to determine the respective timeout ONCE).
312 static struct GNUNET_SCHEDULER_Task *pending_timeout_head;
315 * List of tasks waiting ONLY for a timeout event.
316 * Sorted by timeout (earliest first). Used so that
317 * we do not traverse the list of these tasks when
318 * building select sets (we just look at the head
319 * to determine the respective timeout ONCE).
321 static struct GNUNET_SCHEDULER_Task *pending_timeout_tail;
324 * Last inserted task waiting ONLY for a timeout event.
325 * Used to (heuristically) speed up insertion.
327 static struct GNUNET_SCHEDULER_Task *pending_timeout_last;
330 * ID of the task that is running right now.
332 static struct GNUNET_SCHEDULER_Task *active_task;
335 * Head of list of tasks ready to run right now, grouped by importance.
337 static struct GNUNET_SCHEDULER_Task *ready_head[GNUNET_SCHEDULER_PRIORITY_COUNT];
340 * Tail of list of tasks ready to run right now, grouped by importance.
342 static struct GNUNET_SCHEDULER_Task *ready_tail[GNUNET_SCHEDULER_PRIORITY_COUNT];
345 * Number of tasks on the ready list.
347 static unsigned int ready_count;
350 * How many tasks have we run so far?
352 static unsigned long long tasks_run;
355 * Priority of the task running right now. Only
356 * valid while a task is running.
358 static enum GNUNET_SCHEDULER_Priority current_priority;
361 * Priority of the highest task added in the current select
364 static enum GNUNET_SCHEDULER_Priority max_priority_added;
367 * Value of the 'lifeness' flag for the current task.
369 static int current_lifeness;
372 * Function to use as a select() in the scheduler.
373 * If NULL, we use GNUNET_NETWORK_socket_select().
375 static GNUNET_SCHEDULER_select scheduler_select;
378 * Task context of the current task.
380 static struct GNUNET_SCHEDULER_TaskContext tc;
383 * Closure for #scheduler_select.
385 static void *scheduler_select_cls;
388 * Scheduler handle used for the driver functions
390 static struct GNUNET_SCHEDULER_Handle sh;
394 * Sets the select function to use in the scheduler (scheduler_select).
396 * @param new_select new select function to use
397 * @param new_select_cls closure for @a new_select
398 * @return previously used select function, NULL for default
401 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
402 void *new_select_cls)
404 scheduler_select = new_select;
405 scheduler_select_cls = new_select_cls;
410 * Check that the given priority is legal (and return it).
412 * @param p priority value to check
413 * @return p on success, 0 on error
415 static enum GNUNET_SCHEDULER_Priority
416 check_priority (enum GNUNET_SCHEDULER_Priority p)
418 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
421 return 0; /* make compiler happy */
426 * chooses the nearest timeout from all pending tasks, to be used
427 * to tell the driver the next wakeup time (using its set_wakeup
430 struct GNUNET_TIME_Absolute
433 struct GNUNET_SCHEDULER_Task *pos;
434 struct GNUNET_TIME_Absolute now;
435 struct GNUNET_TIME_Absolute timeout;
437 pos = pending_timeout_head;
438 now = GNUNET_TIME_absolute_get ();
439 timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
442 if (0 != pos->reason)
448 timeout = pos->timeout;
451 for (pos = pending_head; NULL != pos; pos = pos->next)
453 if (0 != pos->reason)
457 else if ((pos->timeout.abs_value_us != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) &&
458 (timeout.abs_value_us > pos->timeout.abs_value_us))
460 timeout = pos->timeout;
468 * Put a task that is ready for execution into the ready queue.
470 * @param task task ready for execution
473 queue_ready_task (struct GNUNET_SCHEDULER_Task *task)
475 enum GNUNET_SCHEDULER_Priority p = check_priority (task->priority);
477 GNUNET_CONTAINER_DLL_insert (ready_head[p],
480 task->in_ready_list = GNUNET_YES;
486 * Request the shutdown of a scheduler. Marks all tasks
487 * awaiting shutdown as ready. Note that tasks
488 * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call
489 * will be delayed until the next shutdown signal.
492 GNUNET_SCHEDULER_shutdown ()
494 struct GNUNET_SCHEDULER_Task *pos;
496 while (NULL != (pos = shutdown_head))
498 GNUNET_CONTAINER_DLL_remove (shutdown_head,
501 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
502 queue_ready_task (pos);
508 * Output stack trace of task @a t.
510 * @param t task to dump stack trace of
513 dump_backtrace (struct GNUNET_SCHEDULER_Task *t)
516 for (unsigned int i = 0; i < t->num_backtrace_strings; i++)
517 LOG (GNUNET_ERROR_TYPE_WARNING,
518 "Task %p trace %u: %s\n",
521 t->backtrace_strings[i]);
529 * Destroy a task (release associated resources)
531 * @param t task to destroy
534 destroy_task (struct GNUNET_SCHEDULER_Task *t)
538 LOG (GNUNET_ERROR_TYPE_DEBUG,
539 "destroying task %p\n",
542 if (GNUNET_YES == t->own_handles)
544 for (i = 0; i != t->fds_len; ++i)
546 const struct GNUNET_NETWORK_Handle *fd = t->fds[i].fd;
547 const struct GNUNET_DISK_FileHandle *fh = t->fds[i].fh;
550 GNUNET_NETWORK_socket_free_memory_only_ ((struct GNUNET_NETWORK_Handle *) fd);
554 // FIXME: on WIN32 this is not enough! A function
555 // GNUNET_DISK_file_free_memory_only would be nice
556 GNUNET_free ((void *) fh);
562 GNUNET_array_grow (t->fds, t->fds_len, 0);
564 if (t->ready_fds_len > 0)
566 GNUNET_array_grow (t->ready_fds, t->ready_fds_len, 0);
569 GNUNET_free (t->backtrace_strings);
576 * Pipe used to communicate shutdown via signal.
578 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
581 * Process ID of this process at the time we installed the various
587 * Signal handler called for SIGPIPE.
599 // * Wait for a short time.
600 // * Sleeps for @a ms ms (as that should be long enough for virtually all
601 // * modern systems to context switch and allow another process to do
602 // * some 'real' work).
604 // * @param ms how many ms to wait
607 //short_wait (unsigned int ms)
609 // struct GNUNET_TIME_Relative timeout;
611 // timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
612 // (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
617 * Signal handler called for signals that should cause us to shutdown.
620 sighandler_shutdown ()
623 int old_errno = errno; /* backup errno */
625 if (getpid () != my_pid)
626 exit (1); /* we have fork'ed since the signal handler was created,
627 * ignore the signal, see https://gnunet.org/vfork discussion */
628 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
629 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
636 shutdown_if_no_lifeness ()
638 struct GNUNET_SCHEDULER_Task *t;
642 for (t = pending_head; NULL != t; t = t->next)
643 if (GNUNET_YES == t->lifeness)
645 for (t = shutdown_head; NULL != t; t = t->next)
646 if (GNUNET_YES == t->lifeness)
648 for (t = pending_timeout_head; NULL != t; t = t->next)
649 if (GNUNET_YES == t->lifeness)
651 /* No lifeness! Cancel all pending tasks the driver knows about and shutdown */
655 struct GNUNET_SCHEDULER_Task *next = t->next;
656 GNUNET_SCHEDULER_cancel (t);
659 GNUNET_SCHEDULER_shutdown ();
664 * Initialize and run scheduler. This function will return when all
665 * tasks have completed. On systems with signals, receiving a SIGTERM
666 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown()
667 * to be run after the active task is complete. As a result, SIGTERM
668 * causes all active tasks to be scheduled with reason
669 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
670 * afterwards will execute normally!). Note that any particular signal
671 * will only shut down one scheduler; applications should always only
672 * create a single scheduler.
674 * @param task task to run immediately
675 * @param task_cls closure of @a task
678 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
681 struct GNUNET_SCHEDULER_Driver *driver;
682 struct DriverContext context = {.scheduled_head = NULL,
683 .scheduled_tail = NULL,
684 .timeout = GNUNET_TIME_UNIT_FOREVER_REL};
686 driver = GNUNET_SCHEDULER_driver_select ();
687 driver->cls = &context;
689 GNUNET_SCHEDULER_run_with_driver (driver, task, task_cls);
691 GNUNET_free (driver);
696 * Obtain the task context, giving the reason why the current task was
699 * @return current tasks' scheduler context
701 const struct GNUNET_SCHEDULER_TaskContext *
702 GNUNET_SCHEDULER_get_task_context ()
704 GNUNET_assert (NULL != active_task);
710 * Get information about the current load of this scheduler. Use this
711 * function to determine if an elective task should be added or simply
712 * dropped (if the decision should be made based on the number of
713 * tasks ready to run).
715 * @param p priority level to look at
716 * @return number of tasks pending right now
719 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
721 struct GNUNET_SCHEDULER_Task *pos;
724 GNUNET_assert (NULL != active_task);
725 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
727 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
728 p = current_priority;
730 for (pos = ready_head[check_priority (p)]; NULL != pos; pos = pos->next)
737 init_fd_info (struct GNUNET_SCHEDULER_Task *t,
738 const struct GNUNET_NETWORK_Handle *const *read_nh,
739 unsigned int read_nh_len,
740 const struct GNUNET_NETWORK_Handle *const *write_nh,
741 unsigned int write_nh_len,
742 const struct GNUNET_DISK_FileHandle *const *read_fh,
743 unsigned int read_fh_len,
744 const struct GNUNET_DISK_FileHandle *const *write_fh,
745 unsigned int write_fh_len)
747 // FIXME: if we have exactly two network handles / exactly two file handles
748 // and they are equal, we can make one FdInfo with both
749 // GNUNET_SCHEDULER_ET_IN and GNUNET_SCHEDULER_ET_OUT set.
750 struct GNUNET_SCHEDULER_FdInfo *fdi;
752 t->fds_len = read_nh_len + write_nh_len + read_fh_len + write_fh_len;
757 if (1 == read_nh_len)
760 GNUNET_assert (NULL != fdi->fd);
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)
769 GNUNET_assert (NULL != fdi->fd);
770 fdi->et = GNUNET_SCHEDULER_ET_OUT;
771 fdi->sock = GNUNET_NETWORK_get_fd (*write_nh);
773 t->write_fd = fdi->sock;
775 else if (1 == read_fh_len)
778 GNUNET_assert (NULL != fdi->fh);
779 fdi->et = GNUNET_SCHEDULER_ET_IN;
780 fdi->sock = (*read_fh)->fd; // FIXME: does not work under WIN32
781 t->read_fd = fdi->sock;
787 GNUNET_assert (NULL != fdi->fh);
788 fdi->et = GNUNET_SCHEDULER_ET_OUT;
789 fdi->sock = (*write_fh)->fd; // FIXME: does not work under WIN32
791 t->write_fd = fdi->sock;
796 fdi = GNUNET_new_array (t->fds_len, struct GNUNET_SCHEDULER_FdInfo);
801 for (i = 0; i != read_nh_len; ++i)
803 fdi->fd = read_nh[i];
804 GNUNET_assert (NULL != fdi->fd);
805 fdi->et = GNUNET_SCHEDULER_ET_IN;
806 fdi->sock = GNUNET_NETWORK_get_fd (read_nh[i]);
809 for (i = 0; i != write_nh_len; ++i)
811 fdi->fd = write_nh[i];
812 GNUNET_assert (NULL != fdi->fd);
813 fdi->et = GNUNET_SCHEDULER_ET_OUT;
814 fdi->sock = GNUNET_NETWORK_get_fd (write_nh[i]);
817 for (i = 0; i != read_fh_len; ++i)
819 fdi->fh = read_fh[i];
820 GNUNET_assert (NULL != fdi->fh);
821 fdi->et = GNUNET_SCHEDULER_ET_IN;
822 fdi->sock = (read_fh[i])->fd; // FIXME: does not work under WIN32
825 for (i = 0; i != write_fh_len; ++i)
827 fdi->fh = write_fh[i];
828 GNUNET_assert (NULL != fdi->fh);
829 fdi->et = GNUNET_SCHEDULER_ET_OUT;
830 fdi->sock = (write_fh[i])->fd; // FIXME: does not work under WIN32
838 * calls the given function @a func on each FdInfo related to @a t.
839 * Optionally updates the event type field in each FdInfo after calling
843 * @param driver_func the function to call with each FdInfo contained in
845 * @param if_not_ready only call @a driver_func on FdInfos that are not
847 * @param et the event type to be set in each FdInfo after calling
848 * @a driver_func on it, or -1 if no updating not desired.
850 void driver_add_multiple (struct GNUNET_SCHEDULER_Task *t,
851 enum GNUNET_SCHEDULER_EventType et)
853 struct GNUNET_SCHEDULER_FdInfo *fdi;
854 int success = GNUNET_YES;
856 for (int i = 0; i != t->fds_len; ++i)
859 success = scheduler_driver->add (scheduler_driver->cls, t, fdi) && success;
865 if (GNUNET_YES != success)
867 LOG (GNUNET_ERROR_TYPE_ERROR,
868 "driver could not add task\n");
874 shutdown_cb (void *cls)
877 const struct GNUNET_DISK_FileHandle *pr;
879 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
880 GNUNET_DISK_PIPE_END_READ);
881 GNUNET_assert (! GNUNET_DISK_handle_invalid (pr));
882 /* consume the signal */
883 GNUNET_DISK_file_read (pr, &c, sizeof (c));
884 /* mark all active tasks as ready due to shutdown */
885 GNUNET_SCHEDULER_shutdown ();
890 * Cancel the task with the specified identifier.
891 * The task must not yet have run. Only allowed to be called as long as the
892 * scheduler is running (#GNUNET_SCHEDULER_run or
893 * #GNUNET_SCHEDULER_run_with_driver has been called and has not returned yet).
895 * @param task id of the task to cancel
896 * @return original closure of the task
899 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
901 enum GNUNET_SCHEDULER_Priority p;
905 LOG (GNUNET_ERROR_TYPE_DEBUG,
906 "canceling task %p\n",
909 /* scheduler must be running */
910 GNUNET_assert (NULL != scheduler_driver);
911 GNUNET_assert ( (NULL != active_task) ||
912 (GNUNET_NO == task->lifeness) );
913 is_fd_task = (NULL != task->fds);
916 int del_result = scheduler_driver->del (scheduler_driver->cls, task);
917 if (GNUNET_OK != del_result)
919 LOG (GNUNET_ERROR_TYPE_ERROR,
920 "driver could not delete task\n");
924 if (! task->in_ready_list)
928 GNUNET_CONTAINER_DLL_remove (pending_head,
932 else if (GNUNET_YES == task->on_shutdown)
934 GNUNET_CONTAINER_DLL_remove (shutdown_head,
940 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
941 pending_timeout_tail,
943 if (pending_timeout_last == task)
944 pending_timeout_last = NULL;
949 p = check_priority (task->priority);
950 GNUNET_CONTAINER_DLL_remove (ready_head[p],
955 ret = task->callback_cls;
962 * Initialize backtrace data for task @a t
964 * @param t task to initialize
967 init_backtrace (struct GNUNET_SCHEDULER_Task *t)
970 void *backtrace_array[MAX_TRACE_DEPTH];
972 t->num_backtrace_strings
973 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
974 t->backtrace_strings =
975 backtrace_symbols (backtrace_array,
976 t->num_backtrace_strings);
983 * Continue the current execution with the given function. This is
984 * similar to the other "add" functions except that there is no delay
985 * and the reason code can be specified.
987 * @param task main function of the task
988 * @param task_cls closure for @a task
989 * @param reason reason for task invocation
990 * @param priority priority to use for the task
993 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
995 enum GNUNET_SCHEDULER_Reason reason,
996 enum GNUNET_SCHEDULER_Priority priority)
998 struct GNUNET_SCHEDULER_Task *t;
1000 GNUNET_assert (NULL != task);
1001 GNUNET_assert ((NULL != active_task) ||
1002 (GNUNET_SCHEDULER_REASON_STARTUP == reason));
1003 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1007 t->callback_cls = task_cls;
1009 t->start_time = GNUNET_TIME_absolute_get ();
1012 t->priority = priority;
1013 t->lifeness = current_lifeness;
1014 LOG (GNUNET_ERROR_TYPE_DEBUG,
1015 "Adding continuation task %p\n",
1018 queue_ready_task (t);
1023 * Schedule a new task to be run at the specified time. The task
1024 * will be scheduled for execution at time @a at.
1026 * @param at time when the operation should run
1027 * @param priority priority to use for the task
1028 * @param task main function of the task
1029 * @param task_cls closure of @a task
1030 * @return unique task identifier for the job
1031 * only valid until @a task is started!
1033 struct GNUNET_SCHEDULER_Task *
1034 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1035 enum GNUNET_SCHEDULER_Priority priority,
1036 GNUNET_SCHEDULER_TaskCallback task,
1039 struct GNUNET_SCHEDULER_Task *t;
1040 struct GNUNET_SCHEDULER_Task *pos;
1041 struct GNUNET_SCHEDULER_Task *prev;
1043 GNUNET_assert (NULL != active_task);
1044 GNUNET_assert (NULL != task);
1045 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1047 t->callback_cls = task_cls;
1051 t->start_time = GNUNET_TIME_absolute_get ();
1054 t->priority = priority;
1055 t->lifeness = current_lifeness;
1056 /* try tail first (optimization in case we are
1057 * appending to a long list of tasks with timeouts) */
1058 if ( (NULL == pending_timeout_head) ||
1059 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us) )
1061 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1062 pending_timeout_tail,
1067 /* first move from heuristic start backwards to before start time */
1068 prev = pending_timeout_last;
1069 while ( (NULL != prev) &&
1070 (prev->timeout.abs_value_us > t->timeout.abs_value_us) )
1072 /* now, move from heuristic start (or head of list) forward to insertion point */
1074 pos = pending_timeout_head;
1077 while ( (NULL != pos) &&
1078 ( (pos->timeout.abs_value_us <= t->timeout.abs_value_us) ||
1079 (0 != pos->reason) ) )
1084 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1085 pending_timeout_tail,
1089 /* finally, update heuristic insertion point to last insertion... */
1090 pending_timeout_last = t;
1092 LOG (GNUNET_ERROR_TYPE_DEBUG,
1101 * Schedule a new task to be run with a specified delay. The task
1102 * will be scheduled for execution once the delay has expired.
1104 * @param delay when should this operation time out?
1105 * @param priority priority to use for the task
1106 * @param task main function of the task
1107 * @param task_cls closure of @a task
1108 * @return unique task identifier for the job
1109 * only valid until @a task is started!
1111 struct GNUNET_SCHEDULER_Task *
1112 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1113 enum GNUNET_SCHEDULER_Priority priority,
1114 GNUNET_SCHEDULER_TaskCallback task,
1117 return GNUNET_SCHEDULER_add_at_with_priority (GNUNET_TIME_relative_to_absolute (delay),
1125 * Schedule a new task to be run with a specified priority.
1127 * @param prio how important is the new task?
1128 * @param task main function of the task
1129 * @param task_cls closure of @a task
1130 * @return unique task identifier for the job
1131 * only valid until @a task is started!
1133 struct GNUNET_SCHEDULER_Task *
1134 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1135 GNUNET_SCHEDULER_TaskCallback task,
1138 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1146 * Schedule a new task to be run at the specified time. The task
1147 * will be scheduled for execution once specified time has been
1148 * reached. It will be run with the DEFAULT priority.
1150 * @param at time at which this operation should run
1151 * @param task main function of the task
1152 * @param task_cls closure of @a task
1153 * @return unique task identifier for the job
1154 * only valid until @a task is started!
1156 struct GNUNET_SCHEDULER_Task *
1157 GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1158 GNUNET_SCHEDULER_TaskCallback task,
1161 return GNUNET_SCHEDULER_add_at_with_priority (at,
1162 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1169 * Schedule a new task to be run with a specified delay. The task
1170 * will be scheduled for execution once the delay has expired. It
1171 * will be run with the DEFAULT priority.
1173 * @param delay when should this operation time out?
1174 * @param task main function of the task
1175 * @param task_cls closure of @a task
1176 * @return unique task identifier for the job
1177 * only valid until @a task is started!
1179 struct GNUNET_SCHEDULER_Task *
1180 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1181 GNUNET_SCHEDULER_TaskCallback task,
1184 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1185 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1192 * Schedule a new task to be run as soon as possible. Note that this
1193 * does not guarantee that this will be the next task that is being
1194 * run, as other tasks with higher priority (or that are already ready
1195 * to run) might get to run first. Just as with delays, clients must
1196 * not rely on any particular order of execution between tasks
1197 * scheduled concurrently.
1199 * The task will be run with the DEFAULT priority.
1201 * @param task main function of the task
1202 * @param task_cls closure of @a task
1203 * @return unique task identifier for the job
1204 * only valid until @a task is started!
1206 struct GNUNET_SCHEDULER_Task *
1207 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
1210 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO,
1217 * Schedule a new task to be run on shutdown, that is when a CTRL-C
1218 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
1221 * @param task main function of the task
1222 * @param task_cls closure of @a task
1223 * @return unique task identifier for the job
1224 * only valid until @a task is started!
1226 struct GNUNET_SCHEDULER_Task *
1227 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1230 struct GNUNET_SCHEDULER_Task *t;
1232 GNUNET_assert (NULL != active_task);
1233 GNUNET_assert (NULL != task);
1234 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1236 t->callback_cls = task_cls;
1240 t->start_time = GNUNET_TIME_absolute_get ();
1242 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1243 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1244 t->on_shutdown = GNUNET_YES;
1245 t->lifeness = GNUNET_NO;
1246 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1249 LOG (GNUNET_ERROR_TYPE_DEBUG,
1250 "Adding shutdown task %p\n",
1258 * Schedule a new task to be run as soon as possible with the
1259 * (transitive) ignore-shutdown flag either explicitly set or
1260 * explicitly enabled. This task (and all tasks created from it,
1261 * other than by another call to this function) will either count or
1262 * not count for the "lifeness" of the process. This API is only
1263 * useful in a few special cases.
1265 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
1266 * @param task main function of the task
1267 * @param task_cls closure of @a task
1268 * @return unique task identifier for the job
1269 * only valid until @a task is started!
1271 struct GNUNET_SCHEDULER_Task *
1272 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1273 GNUNET_SCHEDULER_TaskCallback task,
1276 struct GNUNET_SCHEDULER_Task *ret;
1278 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1279 ret->lifeness = lifeness;
1286 * check a raw file descriptor and abort if it is bad (for debugging purposes)
1288 * @param t the task related to the file descriptor
1289 * @param raw_fd the raw file descriptor to check
1292 check_fd (struct GNUNET_SCHEDULER_Task *t, int raw_fd)
1296 int flags = fcntl (raw_fd, F_GETFD);
1298 if ((flags == -1) && (errno == EBADF))
1300 LOG (GNUNET_ERROR_TYPE_ERROR,
1301 "Got invalid file descriptor %d!\n",
1312 * Schedule a new task to be run with a specified delay or when any of
1313 * the specified file descriptor sets is ready. The delay can be used
1314 * as a timeout on the socket(s) being ready. The task will be
1315 * scheduled for execution once either the delay has expired or any of
1316 * the socket operations is ready. This is the most general
1317 * function of the "add" family. Note that the "prerequisite_task"
1318 * must be satisfied in addition to any of the other conditions. In
1319 * other words, the task will be started when
1321 * (prerequisite-run)
1327 * @param delay how long should we wait?
1328 * @param priority priority to use
1329 * @param rfd file descriptor we want to read (can be -1)
1330 * @param wfd file descriptors we want to write (can be -1)
1331 * @param task main function of the task
1332 * @param task_cls closure of @a task
1333 * @return unique task identifier for the job
1334 * only valid until @a task is started!
1337 static struct GNUNET_SCHEDULER_Task *
1338 add_without_sets (struct GNUNET_TIME_Relative delay,
1339 enum GNUNET_SCHEDULER_Priority priority,
1340 const struct GNUNET_NETWORK_Handle *read_nh,
1341 const struct GNUNET_NETWORK_Handle *write_nh,
1342 const struct GNUNET_DISK_FileHandle *read_fh,
1343 const struct GNUNET_DISK_FileHandle *write_fh,
1344 GNUNET_SCHEDULER_TaskCallback task,
1347 struct GNUNET_SCHEDULER_Task *t;
1349 GNUNET_assert (NULL != active_task);
1350 GNUNET_assert (NULL != task);
1351 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1362 t->callback_cls = task_cls;
1364 check_fd (t, NULL != read_nh ? GNUNET_NETWORK_get_fd (read_nh) : -1);
1365 check_fd (t, NULL != write_nh ? GNUNET_NETWORK_get_fd (write_nh) : -1);
1366 check_fd (t, NULL != read_fh ? read_fh->fd : -1);
1367 check_fd (t, NULL != write_fh ? write_fh->fd : -1);
1370 t->start_time = GNUNET_TIME_absolute_get ();
1372 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1373 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1374 t->lifeness = current_lifeness;
1375 GNUNET_CONTAINER_DLL_insert (pending_head,
1378 driver_add_multiple (t, GNUNET_SCHEDULER_ET_NONE);
1379 max_priority_added = GNUNET_MAX (max_priority_added,
1388 * Schedule a new task to be run with a specified delay or when the
1389 * specified file descriptor is ready for reading. The delay can be
1390 * used as a timeout on the socket being ready. The task will be
1391 * scheduled for execution once either the delay has expired or the
1392 * socket operation is ready. It will be run with the DEFAULT priority.
1393 * Only allowed to be called as long as the scheduler is running
1394 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1395 * called and has not returned yet).
1397 * @param delay when should this operation time out?
1398 * @param rfd read file-descriptor
1399 * @param task main function of the task
1400 * @param task_cls closure of @a task
1401 * @return unique task identifier for the job
1402 * only valid until @a task is started!
1404 struct GNUNET_SCHEDULER_Task *
1405 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1406 struct GNUNET_NETWORK_Handle *rfd,
1407 GNUNET_SCHEDULER_TaskCallback task,
1410 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1411 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1412 rfd, task, task_cls);
1417 * Schedule a new task to be run with a specified priority and to be
1418 * run after the specified delay or when the specified file descriptor
1419 * is ready for reading. The delay can be used as a timeout on the
1420 * socket being ready. The task will be scheduled for execution once
1421 * either the delay has expired or the socket operation is ready. It
1422 * will be run with the DEFAULT priority.
1423 * Only allowed to be called as long as the scheduler is running
1424 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1425 * called and has not returned yet).
1427 * @param delay when should this operation time out?
1428 * @param priority priority to use for the task
1429 * @param rfd read file-descriptor
1430 * @param task main function of the task
1431 * @param task_cls closure of @a task
1432 * @return unique task identifier for the job
1433 * only valid until @a task is started!
1435 struct GNUNET_SCHEDULER_Task *
1436 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1437 enum GNUNET_SCHEDULER_Priority priority,
1438 struct GNUNET_NETWORK_Handle *rfd,
1439 GNUNET_SCHEDULER_TaskCallback task,
1442 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1451 * Schedule a new task to be run with a specified delay or when the
1452 * specified file descriptor is ready for writing. The delay can be
1453 * used as a timeout on the socket being ready. The task will be
1454 * scheduled for execution once either the delay has expired or the
1455 * socket operation is ready. It will be run with the priority of
1457 * Only allowed to be called as long as the scheduler is running
1458 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1459 * called and has not returned yet).
1461 * @param delay when should this operation time out?
1462 * @param wfd write file-descriptor
1463 * @param task main function of the task
1464 * @param task_cls closure of @a task
1465 * @return unique task identifier for the job
1466 * only valid until @a task is started!
1468 struct GNUNET_SCHEDULER_Task *
1469 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1470 struct GNUNET_NETWORK_Handle *wfd,
1471 GNUNET_SCHEDULER_TaskCallback task,
1474 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1475 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1477 GNUNET_NO, GNUNET_YES,
1482 * Schedule a new task to be run with a specified delay or when the
1483 * specified file descriptor is ready. The delay can be
1484 * used as a timeout on the socket being ready. The task will be
1485 * scheduled for execution once either the delay has expired or the
1486 * socket operation is ready.
1487 * Only allowed to be called as long as the scheduler is running
1488 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1489 * called and has not returned yet).
1491 * @param delay when should this operation time out?
1492 * @param priority priority of the task
1493 * @param fd file-descriptor
1494 * @param on_read whether to poll the file-descriptor for readability
1495 * @param on_write whether to poll the file-descriptor for writability
1496 * @param task main function of the task
1497 * @param task_cls closure of task
1498 * @return unique task identifier for the job
1499 * only valid until "task" is started!
1501 struct GNUNET_SCHEDULER_Task *
1502 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1503 enum GNUNET_SCHEDULER_Priority priority,
1504 struct GNUNET_NETWORK_Handle *fd,
1507 GNUNET_SCHEDULER_TaskCallback task,
1510 /* scheduler must be running */
1511 GNUNET_assert (NULL != scheduler_driver);
1514 struct GNUNET_NETWORK_FDSet *s;
1515 struct GNUNET_SCHEDULER_Task * ret;
1517 GNUNET_assert (NULL != fd);
1518 s = GNUNET_NETWORK_fdset_create ();
1519 GNUNET_NETWORK_fdset_set (s, fd);
1520 ret = GNUNET_SCHEDULER_add_select (
1523 on_write ? s : NULL,
1525 GNUNET_NETWORK_fdset_destroy (s);
1528 GNUNET_assert (on_read || on_write);
1529 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1530 return add_without_sets (delay, priority,
1531 on_read ? fd : NULL,
1532 on_write ? fd : NULL,
1541 * Schedule a new task to be run with a specified delay or when the
1542 * specified file descriptor is ready for reading. The delay can be
1543 * used as a timeout on the socket being ready. The task will be
1544 * scheduled for execution once either the delay has expired or the
1545 * socket operation is ready. It will be run with the DEFAULT priority.
1546 * Only allowed to be called as long as the scheduler is running
1547 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1548 * called and has not returned yet).
1550 * @param delay when should this operation time out?
1551 * @param rfd read file-descriptor
1552 * @param task main function of the task
1553 * @param task_cls closure of @a task
1554 * @return unique task identifier for the job
1555 * only valid until @a task is started!
1557 struct GNUNET_SCHEDULER_Task *
1558 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1559 const struct GNUNET_DISK_FileHandle *rfd,
1560 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1562 return GNUNET_SCHEDULER_add_file_with_priority (
1563 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1564 rfd, GNUNET_YES, GNUNET_NO,
1570 * Schedule a new task to be run with a specified delay or when the
1571 * specified file descriptor is ready for writing. The delay can be
1572 * used as a timeout on the socket being ready. The task will be
1573 * scheduled for execution once either the delay has expired or the
1574 * socket operation is ready. It will be run with the DEFAULT priority.
1575 * Only allowed to be called as long as the scheduler is running
1576 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1577 * called and has not returned yet).
1579 * @param delay when should this operation time out?
1580 * @param wfd write file-descriptor
1581 * @param task main function of the task
1582 * @param task_cls closure of @a task
1583 * @return unique task identifier for the job
1584 * only valid until @a task is started!
1586 struct GNUNET_SCHEDULER_Task *
1587 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1588 const struct GNUNET_DISK_FileHandle *wfd,
1589 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1591 return GNUNET_SCHEDULER_add_file_with_priority (
1592 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1593 wfd, GNUNET_NO, GNUNET_YES,
1599 * Schedule a new task to be run with a specified delay or when the
1600 * specified file descriptor is ready. The delay can be
1601 * used as a timeout on the socket being ready. The task will be
1602 * scheduled for execution once either the delay has expired or the
1603 * socket operation is ready.
1604 * Only allowed to be called as long as the scheduler is running
1605 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1606 * called and has not returned yet).
1608 * @param delay when should this operation time out?
1609 * @param priority priority of the task
1610 * @param fd file-descriptor
1611 * @param on_read whether to poll the file-descriptor for readability
1612 * @param on_write whether to poll the file-descriptor for writability
1613 * @param task main function of the task
1614 * @param task_cls closure of @a task
1615 * @return unique task identifier for the job
1616 * only valid until @a task is started!
1618 struct GNUNET_SCHEDULER_Task *
1619 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1620 enum GNUNET_SCHEDULER_Priority priority,
1621 const struct GNUNET_DISK_FileHandle *fd,
1622 int on_read, int on_write,
1623 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1625 /* scheduler must be running */
1626 GNUNET_assert (NULL != scheduler_driver);
1629 struct GNUNET_NETWORK_FDSet *s;
1630 struct GNUNET_SCHEDULER_Task * ret;
1632 GNUNET_assert (NULL != fd);
1633 s = GNUNET_NETWORK_fdset_create ();
1634 GNUNET_NETWORK_fdset_handle_set (s, fd);
1635 ret = GNUNET_SCHEDULER_add_select (
1638 on_write ? s : NULL,
1640 GNUNET_NETWORK_fdset_destroy (s);
1643 GNUNET_assert (on_read || on_write);
1644 GNUNET_assert (fd->fd >= 0);
1645 return add_without_sets (delay, priority,
1648 on_read ? fd : NULL,
1649 on_write ? fd : NULL,
1656 extract_handles (struct GNUNET_SCHEDULER_Task *t,
1657 const struct GNUNET_NETWORK_FDSet *fdset,
1658 const struct GNUNET_NETWORK_Handle ***ntarget,
1659 unsigned int *extracted_nhandles,
1660 const struct GNUNET_DISK_FileHandle ***ftarget,
1661 unsigned int *extracted_fhandles)
1663 // FIXME: this implementation only works for unix, for WIN32 the file handles
1664 // in fdset must be handled separately
1665 const struct GNUNET_NETWORK_Handle **nhandles;
1666 const struct GNUNET_DISK_FileHandle **fhandles;
1667 unsigned int nhandles_len, fhandles_len;
1674 for (sock = 0; sock != fdset->nsds; ++sock)
1676 if (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (fdset, sock))
1678 struct GNUNET_NETWORK_Handle *nhandle;
1679 struct GNUNET_DISK_FileHandle *fhandle;
1681 nhandle = GNUNET_NETWORK_socket_box_native (sock);
1682 if (NULL != nhandle)
1684 GNUNET_array_append (nhandles, nhandles_len, nhandle);
1688 fhandle = GNUNET_DISK_get_handle_from_int_fd (sock);
1689 if (NULL != fhandle)
1691 GNUNET_array_append (fhandles, fhandles_len, fhandle);
1700 *ntarget = nhandles_len > 0 ? nhandles : NULL;
1701 *ftarget = fhandles_len > 0 ? fhandles : NULL;
1702 *extracted_nhandles = nhandles_len;
1703 *extracted_fhandles = fhandles_len;
1708 * Schedule a new task to be run with a specified delay or when any of
1709 * the specified file descriptor sets is ready. The delay can be used
1710 * as a timeout on the socket(s) being ready. The task will be
1711 * scheduled for execution once either the delay has expired or any of
1712 * the socket operations is ready. This is the most general
1713 * function of the "add" family. Note that the "prerequisite_task"
1714 * must be satisfied in addition to any of the other conditions. In
1715 * other words, the task will be started when
1717 * (prerequisite-run)
1720 * || any-ws-ready) )
1722 * Only allowed to be called as long as the scheduler is running
1723 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1724 * called and has not returned yet).
1726 * @param prio how important is this task?
1727 * @param delay how long should we wait?
1728 * @param rs set of file descriptors we want to read (can be NULL)
1729 * @param ws set of file descriptors we want to write (can be NULL)
1730 * @param task main function of the task
1731 * @param task_cls closure of @a task
1732 * @return unique task identifier for the job
1733 * only valid until @a task is started!
1735 struct GNUNET_SCHEDULER_Task *
1736 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1737 struct GNUNET_TIME_Relative delay,
1738 const struct GNUNET_NETWORK_FDSet *rs,
1739 const struct GNUNET_NETWORK_FDSet *ws,
1740 GNUNET_SCHEDULER_TaskCallback task,
1743 struct GNUNET_SCHEDULER_Task *t;
1744 const struct GNUNET_NETWORK_Handle **read_nhandles;
1745 const struct GNUNET_NETWORK_Handle **write_nhandles;
1746 const struct GNUNET_DISK_FileHandle **read_fhandles;
1747 const struct GNUNET_DISK_FileHandle **write_fhandles;
1748 unsigned int read_nhandles_len, write_nhandles_len,
1749 read_fhandles_len, write_fhandles_len;
1751 if (((NULL == rs) && (NULL == ws)) || ((0 == rs->nsds) && (0 == ws->nsds)))
1752 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1756 /* scheduler must be running */
1757 GNUNET_assert (NULL != scheduler_driver);
1758 GNUNET_assert (NULL != active_task);
1759 GNUNET_assert (NULL != task);
1760 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1762 t->callback_cls = task_cls;
1765 t->own_handles = GNUNET_YES;
1766 read_nhandles = NULL;
1767 write_nhandles = NULL;
1768 read_fhandles = NULL;
1769 write_fhandles = NULL;
1770 read_nhandles_len = 0;
1771 write_nhandles_len = 0;
1772 read_fhandles_len = 0;
1773 write_fhandles_len = 0;
1781 &read_fhandles_len);
1788 &write_nhandles_len,
1790 &write_fhandles_len);
1800 write_fhandles_len);
1801 /* free the arrays of pointers to network / file handles, the actual
1802 * handles will be freed in destroy_task */
1803 GNUNET_array_grow (read_nhandles, read_nhandles_len, 0);
1804 GNUNET_array_grow (write_nhandles, write_nhandles_len, 0);
1805 GNUNET_array_grow (read_fhandles, read_fhandles_len, 0);
1806 GNUNET_array_grow (write_fhandles, write_fhandles_len, 0);
1808 t->start_time = GNUNET_TIME_absolute_get ();
1810 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1812 check_priority ((prio ==
1813 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1815 t->lifeness = current_lifeness;
1816 GNUNET_CONTAINER_DLL_insert (pending_head,
1819 driver_add_multiple (t, GNUNET_SCHEDULER_ET_NONE);
1820 max_priority_added = GNUNET_MAX (max_priority_added,
1822 LOG (GNUNET_ERROR_TYPE_DEBUG,
1831 * Function used by event-loop implementations to signal the scheduler
1832 * that a particular @a task is ready due to an event specified in the
1833 * et field of @a fdi.
1835 * This function will then queue the task to notify the application
1836 * that the task is ready (with the respective priority).
1838 * @param task the task that is ready
1839 * @param fdi information about the related FD
1842 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1843 struct GNUNET_SCHEDULER_FdInfo *fdi)
1845 enum GNUNET_SCHEDULER_Reason reason;
1846 struct GNUNET_TIME_Absolute now;
1848 now = GNUNET_TIME_absolute_get ();
1849 reason = task->reason;
1850 if (now.abs_value_us >= task->timeout.abs_value_us)
1851 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1852 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1853 (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et)) )
1854 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
1855 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
1856 (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et)) )
1857 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
1858 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1859 task->reason = reason;
1860 if (GNUNET_NO == task->in_ready_list)
1862 GNUNET_CONTAINER_DLL_remove (pending_head,
1865 queue_ready_task (task);
1871 * Function called by the driver to tell the scheduler to run some of
1872 * the tasks that are ready. This function may return even though
1873 * there are tasks left to run just to give other tasks a chance as
1874 * well. If we return #GNUNET_YES, the driver should call this
1875 * function again as soon as possible, while if we return #GNUNET_NO
1876 * it must block until either the operating system has more work (the
1877 * scheduler has no more work to do right now) or the timeout set by
1878 * the scheduler (using the set_wakeup callback) is reached.
1880 * @param sh scheduler handle that was given to the `loop`
1881 * @return #GNUNET_OK if there are more tasks that are ready,
1882 * and thus we would like to run more (yield to avoid
1883 * blocking other activities for too long)
1884 * #GNUNET_NO if we are done running tasks (yield to block)
1885 * #GNUNET_SYSERR on error, e.g. no tasks were ready
1888 GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh)
1890 enum GNUNET_SCHEDULER_Priority p;
1891 struct GNUNET_SCHEDULER_Task *pos;
1892 struct GNUNET_TIME_Absolute now;
1894 /* check for tasks that reached the timeout! */
1895 now = GNUNET_TIME_absolute_get ();
1896 while (NULL != (pos = pending_timeout_head))
1898 if (now.abs_value_us >= pos->timeout.abs_value_us)
1899 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1900 if (0 == pos->reason)
1902 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1903 pending_timeout_tail,
1905 if (pending_timeout_last == pos)
1906 pending_timeout_last = NULL;
1907 queue_ready_task (pos);
1912 struct GNUNET_SCHEDULER_Task *next = pos->next;
1913 if (now.abs_value_us >= pos->timeout.abs_value_us)
1915 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1916 GNUNET_CONTAINER_DLL_remove (pending_head,
1919 queue_ready_task (pos);
1924 if (0 == ready_count)
1926 LOG (GNUNET_ERROR_TYPE_ERROR,
1927 "GNUNET_SCHEDULER_run_from_driver was called, but no tasks are ready!\n");
1928 return GNUNET_SYSERR;
1931 /* find out which task priority level we are going to
1932 process this time */
1933 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
1934 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
1935 /* yes, p>0 is correct, 0 is "KEEP" which should
1936 * always be an empty queue (see assertion)! */
1937 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
1939 pos = ready_head[p];
1943 GNUNET_assert (NULL != pos); /* ready_count wrong? */
1945 /* process all tasks at this priority level, then yield */
1946 while (NULL != (pos = ready_head[p]))
1948 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1952 current_priority = pos->priority;
1953 current_lifeness = pos->lifeness;
1956 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
1957 DELAY_THRESHOLD.rel_value_us)
1959 LOG (GNUNET_ERROR_TYPE_DEBUG,
1960 "Task %p took %s to be scheduled\n",
1962 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
1966 tc.reason = pos->reason;
1967 GNUNET_NETWORK_fdset_zero (sh->rs);
1968 GNUNET_NETWORK_fdset_zero (sh->ws);
1969 // FIXME: do we have to remove FdInfos from fds if they are not ready?
1970 tc.fds_len = pos->fds_len;
1972 for (int i = 0; i != pos->fds_len; ++i)
1974 struct GNUNET_SCHEDULER_FdInfo *fdi = &pos->fds[i];
1975 if (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et))
1977 GNUNET_NETWORK_fdset_set_native (sh->rs,
1980 if (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et))
1982 GNUNET_NETWORK_fdset_set_native (sh->ws,
1986 tc.read_ready = sh->rs;
1987 tc.write_ready = sh->ws;
1988 LOG (GNUNET_ERROR_TYPE_DEBUG,
1989 "Running task %p\n",
1991 GNUNET_assert (NULL != pos->callback);
1992 pos->callback (pos->callback_cls);
1993 if (NULL != pos->fds)
1995 int del_result = scheduler_driver->del (scheduler_driver->cls, pos);
1996 if (GNUNET_OK != del_result)
1998 LOG (GNUNET_ERROR_TYPE_ERROR,
1999 "driver could not delete task\n");
2004 dump_backtrace (pos);
2008 shutdown_if_no_lifeness ();
2009 if (0 == ready_count)
2011 scheduler_driver->set_wakeup (scheduler_driver->cls,
2015 scheduler_driver->set_wakeup (scheduler_driver->cls,
2016 GNUNET_TIME_absolute_get ());
2022 * Initialize and run scheduler. This function will return when all
2023 * tasks have completed. On systems with signals, receiving a SIGTERM
2024 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
2025 * to be run after the active task is complete. As a result, SIGTERM
2026 * causes all shutdown tasks to be scheduled with reason
2027 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
2028 * afterwards will execute normally!). Note that any particular
2029 * signal will only shut down one scheduler; applications should
2030 * always only create a single scheduler.
2032 * @param driver drive to use for the event loop
2033 * @param task task to run first (and immediately)
2034 * @param task_cls closure of @a task
2035 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2038 GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
2039 GNUNET_SCHEDULER_TaskCallback task,
2043 struct GNUNET_SIGNAL_Context *shc_int;
2044 struct GNUNET_SIGNAL_Context *shc_term;
2045 #if (SIGTERM != GNUNET_TERM_SIG)
2046 struct GNUNET_SIGNAL_Context *shc_gterm;
2049 struct GNUNET_SIGNAL_Context *shc_quit;
2050 struct GNUNET_SIGNAL_Context *shc_hup;
2051 struct GNUNET_SIGNAL_Context *shc_pipe;
2053 struct GNUNET_SCHEDULER_Task tsk;
2054 const struct GNUNET_DISK_FileHandle *pr;
2056 /* general set-up */
2057 GNUNET_assert (NULL == active_task);
2058 GNUNET_assert (NULL == shutdown_pipe_handle);
2059 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
2063 GNUNET_assert (NULL != shutdown_pipe_handle);
2064 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
2065 GNUNET_DISK_PIPE_END_READ);
2067 scheduler_driver = driver;
2069 /* install signal handlers */
2070 LOG (GNUNET_ERROR_TYPE_DEBUG,
2071 "Registering signal handlers\n");
2072 shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2073 &sighandler_shutdown);
2074 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2075 &sighandler_shutdown);
2076 #if (SIGTERM != GNUNET_TERM_SIG)
2077 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2078 &sighandler_shutdown);
2081 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2083 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2084 &sighandler_shutdown);
2085 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2086 &sighandler_shutdown);
2089 /* Setup initial tasks */
2090 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2091 current_lifeness = GNUNET_NO;
2096 GNUNET_SCHEDULER_add_now (&GNUNET_OS_install_parent_control_handler,
2098 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2102 current_lifeness = GNUNET_YES;
2103 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
2105 GNUNET_SCHEDULER_REASON_STARTUP,
2106 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
2108 scheduler_driver->set_wakeup (scheduler_driver->cls,
2110 /* begin main event loop */
2111 sh.rs = GNUNET_NETWORK_fdset_create ();
2112 sh.ws = GNUNET_NETWORK_fdset_create ();
2113 GNUNET_NETWORK_fdset_handle_set (sh.rs, pr);
2114 ret = driver->loop (driver->cls,
2116 GNUNET_NETWORK_fdset_destroy (sh.rs);
2117 GNUNET_NETWORK_fdset_destroy (sh.ws);
2119 /* uninstall signal handlers */
2120 GNUNET_SIGNAL_handler_uninstall (shc_int);
2121 GNUNET_SIGNAL_handler_uninstall (shc_term);
2122 #if (SIGTERM != GNUNET_TERM_SIG)
2123 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
2126 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
2127 GNUNET_SIGNAL_handler_uninstall (shc_quit);
2128 GNUNET_SIGNAL_handler_uninstall (shc_hup);
2130 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2131 shutdown_pipe_handle = NULL;
2132 scheduler_driver = NULL;
2138 select_add (void *cls,
2139 struct GNUNET_SCHEDULER_Task *task,
2140 struct GNUNET_SCHEDULER_FdInfo *fdi)
2142 struct DriverContext *context = cls;
2143 GNUNET_assert (NULL != context);
2144 GNUNET_assert (NULL != task);
2145 GNUNET_assert (NULL != fdi);
2146 GNUNET_assert (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et) ||
2147 0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et));
2149 if (!((NULL != fdi->fd) ^ (NULL != fdi->fh)) || (fdi->sock < 0))
2151 /* exactly one out of {fd, hf} must be != NULL and the OS handle must be valid */
2152 return GNUNET_SYSERR;
2155 struct Scheduled *scheduled = GNUNET_new (struct Scheduled);
2156 scheduled->task = task;
2157 scheduled->fdi = fdi;
2158 scheduled->et = fdi->et;
2160 GNUNET_CONTAINER_DLL_insert (context->scheduled_head,
2161 context->scheduled_tail,
2168 select_del (void *cls,
2169 struct GNUNET_SCHEDULER_Task *task)
2171 struct DriverContext *context;
2172 struct Scheduled *pos;
2175 GNUNET_assert (NULL != cls);
2178 ret = GNUNET_SYSERR;
2179 pos = context->scheduled_head;
2182 struct Scheduled *next = pos->next;
2183 if (pos->task == task)
2185 GNUNET_CONTAINER_DLL_remove (context->scheduled_head,
2186 context->scheduled_tail,
2198 select_loop (void *cls,
2199 struct GNUNET_SCHEDULER_Handle *sh)
2201 struct GNUNET_NETWORK_FDSet *rs;
2202 struct GNUNET_NETWORK_FDSet *ws;
2203 struct DriverContext *context;
2208 GNUNET_assert (NULL != context);
2209 rs = GNUNET_NETWORK_fdset_create ();
2210 ws = GNUNET_NETWORK_fdset_create ();
2211 tasks_ready = GNUNET_NO;
2212 while (NULL != context->scheduled_head ||
2213 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != context->timeout.rel_value_us)
2215 LOG (GNUNET_ERROR_TYPE_DEBUG,
2216 "select timeout = %s\n",
2217 GNUNET_STRINGS_relative_time_to_string (context->timeout, GNUNET_NO));
2219 GNUNET_NETWORK_fdset_zero (rs);
2220 GNUNET_NETWORK_fdset_zero (ws);
2221 struct Scheduled *pos;
2222 for (pos = context->scheduled_head; NULL != pos; pos = pos->next)
2224 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et))
2226 GNUNET_NETWORK_fdset_set_native (rs, pos->fdi->sock);
2228 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et))
2230 GNUNET_NETWORK_fdset_set_native (ws, pos->fdi->sock);
2233 if (NULL == scheduler_select)
2235 select_result = GNUNET_NETWORK_socket_select (rs,
2242 select_result = scheduler_select (scheduler_select_cls,
2248 if (select_result == GNUNET_SYSERR)
2253 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
2258 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
2261 if (0 != system (lsof))
2262 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
2267 struct Scheduled *s;
2268 for (s = context->scheduled_head; NULL != s; s = s->next)
2270 int flags = fcntl (s->fdi->sock, F_GETFD);
2271 if ((flags == -1) && (errno == EBADF))
2273 LOG (GNUNET_ERROR_TYPE_ERROR,
2274 "Got invalid file descriptor %d!\n",
2280 GNUNET_NETWORK_fdset_destroy (rs);
2281 GNUNET_NETWORK_fdset_destroy (ws);
2282 return GNUNET_SYSERR;
2284 for (pos = context->scheduled_head; NULL != pos; pos = pos->next)
2286 int is_ready = GNUNET_NO;
2287 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et) &&
2288 GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, pos->fdi->sock))
2290 pos->fdi->et |= GNUNET_SCHEDULER_ET_IN;
2291 is_ready = GNUNET_YES;
2293 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et) &&
2294 GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, pos->fdi->sock))
2296 pos->fdi->et |= GNUNET_SCHEDULER_ET_OUT;
2297 is_ready = GNUNET_YES;
2299 if (GNUNET_YES == is_ready)
2301 GNUNET_SCHEDULER_task_ready (pos->task, pos->fdi);
2304 tasks_ready = GNUNET_SCHEDULER_run_from_driver (sh);
2305 GNUNET_assert (GNUNET_SYSERR != tasks_ready);
2307 GNUNET_NETWORK_fdset_destroy (rs);
2308 GNUNET_NETWORK_fdset_destroy (ws);
2314 select_set_wakeup(void *cls,
2315 struct GNUNET_TIME_Absolute dt)
2317 struct DriverContext *context = cls;
2318 GNUNET_assert (NULL != context);
2320 context->timeout = GNUNET_TIME_absolute_get_remaining (dt);
2325 * Obtain the driver for using select() as the event loop.
2327 * @return NULL on error
2329 struct GNUNET_SCHEDULER_Driver *
2330 GNUNET_SCHEDULER_driver_select ()
2332 struct GNUNET_SCHEDULER_Driver *select_driver;
2333 select_driver = GNUNET_new (struct GNUNET_SCHEDULER_Driver);
2335 select_driver->loop = &select_loop;
2336 select_driver->add = &select_add;
2337 select_driver->del = &select_del;
2338 select_driver->set_wakeup = &select_set_wakeup;
2340 return select_driver;
2344 /* end of scheduler.c */