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 handlers 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;
225 struct Scheduled *prev;
227 struct Scheduled *next;
229 struct GNUNET_SCHEDULER_Task *task;
231 struct GNUNET_SCHEDULER_FdInfo *fdi;
233 enum GNUNET_SCHEDULER_EventType et;
240 * Driver context used by GNUNET_SCHEDULER_run
244 struct Scheduled *scheduled_head;
246 struct Scheduled *scheduled_tail;
248 struct GNUNET_TIME_Relative timeout;
253 * The driver used for the event loop. Will be handed over to
254 * the scheduler in #GNUNET_SCHEDULER_run_from_driver(), peristed
255 * there in this variable for later use in functions like
256 * #GNUNET_SCHEDULER_add_select(), #add_without_sets() and
257 * #GNUNET_SCHEDULER_cancel().
259 static const struct GNUNET_SCHEDULER_Driver *scheduler_driver;
262 * Head of list of tasks waiting for an event.
264 static struct GNUNET_SCHEDULER_Task *pending_head;
267 * Tail of list of tasks waiting for an event.
269 static struct GNUNET_SCHEDULER_Task *pending_tail;
272 * Head of list of tasks waiting for shutdown.
274 static struct GNUNET_SCHEDULER_Task *shutdown_head;
277 * Tail of list of tasks waiting for shutdown.
279 static struct GNUNET_SCHEDULER_Task *shutdown_tail;
282 * List of tasks waiting ONLY for a timeout event.
283 * Sorted by timeout (earliest first). Used so that
284 * we do not traverse the list of these tasks when
285 * building select sets (we just look at the head
286 * to determine the respective timeout ONCE).
288 static struct GNUNET_SCHEDULER_Task *pending_timeout_head;
291 * List of tasks waiting ONLY for a timeout event.
292 * Sorted by timeout (earliest first). Used so that
293 * we do not traverse the list of these tasks when
294 * building select sets (we just look at the head
295 * to determine the respective timeout ONCE).
297 static struct GNUNET_SCHEDULER_Task *pending_timeout_tail;
300 * Last inserted task waiting ONLY for a timeout event.
301 * Used to (heuristically) speed up insertion.
303 static struct GNUNET_SCHEDULER_Task *pending_timeout_last;
306 * ID of the task that is running right now.
308 static struct GNUNET_SCHEDULER_Task *active_task;
311 * Head of list of tasks ready to run right now, grouped by importance.
313 static struct GNUNET_SCHEDULER_Task *ready_head[GNUNET_SCHEDULER_PRIORITY_COUNT];
316 * Tail of list of tasks ready to run right now, grouped by importance.
318 static struct GNUNET_SCHEDULER_Task *ready_tail[GNUNET_SCHEDULER_PRIORITY_COUNT];
321 * Number of tasks on the ready list.
323 static unsigned int ready_count;
326 * How many tasks have we run so far?
328 static unsigned long long tasks_run;
331 * Priority of the task running right now. Only
332 * valid while a task is running.
334 static enum GNUNET_SCHEDULER_Priority current_priority;
337 * Priority of the highest task added in the current select
340 static enum GNUNET_SCHEDULER_Priority max_priority_added;
343 * Value of the 'lifeness' flag for the current task.
345 static int current_lifeness;
348 * Function to use as a select() in the scheduler.
349 * If NULL, we use GNUNET_NETWORK_socket_select().
351 static GNUNET_SCHEDULER_select scheduler_select;
354 * Task context of the current task.
356 static struct GNUNET_SCHEDULER_TaskContext tc;
359 * Closure for #scheduler_select.
361 static void *scheduler_select_cls;
364 * Scheduler handle used for the driver functions
366 static struct GNUNET_SCHEDULER_Handle sh;
370 * Sets the select function to use in the scheduler (scheduler_select).
372 * @param new_select new select function to use
373 * @param new_select_cls closure for @a new_select
374 * @return previously used select function, NULL for default
377 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
378 void *new_select_cls)
380 scheduler_select = new_select;
381 scheduler_select_cls = new_select_cls;
386 * Check that the given priority is legal (and return it).
388 * @param p priority value to check
389 * @return p on success, 0 on error
391 static enum GNUNET_SCHEDULER_Priority
392 check_priority (enum GNUNET_SCHEDULER_Priority p)
394 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
397 return 0; /* make compiler happy */
402 * chooses the nearest timeout from all pending tasks, to be used
403 * to tell the driver the next wakeup time (using its set_wakeup
406 struct GNUNET_TIME_Absolute
409 struct GNUNET_SCHEDULER_Task *pos;
410 struct GNUNET_TIME_Absolute now;
411 struct GNUNET_TIME_Absolute timeout;
413 pos = pending_timeout_head;
414 now = GNUNET_TIME_absolute_get ();
415 timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
418 if (0 != pos->reason)
424 timeout = pos->timeout;
427 for (pos = pending_head; NULL != pos; pos = pos->next)
429 if (0 != pos->reason)
433 else if ((pos->timeout.abs_value_us != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) &&
434 (timeout.abs_value_us > pos->timeout.abs_value_us))
436 timeout = pos->timeout;
444 * Put a task that is ready for execution into the ready queue.
446 * @param task task ready for execution
449 queue_ready_task (struct GNUNET_SCHEDULER_Task *task)
451 enum GNUNET_SCHEDULER_Priority p = check_priority (task->priority);
453 GNUNET_CONTAINER_DLL_insert (ready_head[p],
456 task->in_ready_list = GNUNET_YES;
462 * Request the shutdown of a scheduler. Marks all tasks
463 * awaiting shutdown as ready. Note that tasks
464 * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call
465 * will be delayed until the next shutdown signal.
468 GNUNET_SCHEDULER_shutdown ()
470 struct GNUNET_SCHEDULER_Task *pos;
472 while (NULL != (pos = shutdown_head))
474 GNUNET_CONTAINER_DLL_remove (shutdown_head,
477 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
478 queue_ready_task (pos);
484 * Output stack trace of task @a t.
486 * @param t task to dump stack trace of
489 dump_backtrace (struct GNUNET_SCHEDULER_Task *t)
494 for (i = 0; i < t->num_backtrace_strings; i++)
495 LOG (GNUNET_ERROR_TYPE_WARNING,
496 "Task %p trace %u: %s\n",
499 t->backtrace_strings[i]);
505 * Destroy a task (release associated resources)
507 * @param t task to destroy
510 destroy_task (struct GNUNET_SCHEDULER_Task *t)
514 if (GNUNET_YES == t->own_handlers)
516 for (i = 0; i != t->fds_len; ++i)
518 const struct GNUNET_NETWORK_Handle *fd = t->fds[i].fd;
519 const struct GNUNET_DISK_FileHandle *fh = t->fds[i].fh;
522 GNUNET_NETWORK_socket_free_memory_only_ ((struct GNUNET_NETWORK_Handle *) fd);
526 // FIXME: on WIN32 this is not enough! A function
527 // GNUNET_DISK_file_free_memory_only would be nice
528 GNUNET_free ((void *) fh);
534 GNUNET_array_grow (t->fds, t->fds_len, 0);
536 if (t->ready_fds_len > 0)
538 GNUNET_array_grow (t->ready_fds, t->ready_fds_len, 0);
541 GNUNET_free (t->backtrace_strings);
548 * Pipe used to communicate shutdown via signal.
550 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
553 * Process ID of this process at the time we installed the various
559 * Signal handler called for SIGPIPE.
571 * Wait for a short time.
572 * Sleeps for @a ms ms (as that should be long enough for virtually all
573 * modern systems to context switch and allow another process to do
576 * @param ms how many ms to wait
579 short_wait (unsigned int ms)
581 struct GNUNET_TIME_Relative timeout;
583 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
584 (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
589 * Signal handler called for signals that should cause us to shutdown.
592 sighandler_shutdown ()
595 int old_errno = errno; /* backup errno */
597 if (getpid () != my_pid)
598 exit (1); /* we have fork'ed since the signal handler was created,
599 * ignore the signal, see https://gnunet.org/vfork discussion */
600 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
601 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
608 shutdown_if_no_lifeness ()
610 struct GNUNET_SCHEDULER_Task *t;
614 for (t = pending_head; NULL != t; t = t->next)
615 if (GNUNET_YES == t->lifeness)
617 for (t = shutdown_head; NULL != t; t = t->next)
618 if (GNUNET_YES == t->lifeness)
620 for (t = pending_timeout_head; NULL != t; t = t->next)
621 if (GNUNET_YES == t->lifeness)
623 /* No lifeness! Cancel all pending tasks the driver knows about and shutdown */
627 struct GNUNET_SCHEDULER_Task *next = t->next;
628 GNUNET_SCHEDULER_cancel (t);
631 GNUNET_SCHEDULER_shutdown ();
636 * Initialize and run scheduler. This function will return when all
637 * tasks have completed. On systems with signals, receiving a SIGTERM
638 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown()
639 * to be run after the active task is complete. As a result, SIGTERM
640 * causes all active tasks to be scheduled with reason
641 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
642 * afterwards will execute normally!). Note that any particular signal
643 * will only shut down one scheduler; applications should always only
644 * create a single scheduler.
646 * @param task task to run immediately
647 * @param task_cls closure of @a task
650 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
653 struct GNUNET_SCHEDULER_Driver *driver;
654 struct DriverContext context = {.scheduled_head = NULL,
655 .scheduled_tail = NULL,
656 .timeout = GNUNET_TIME_UNIT_FOREVER_REL};
658 driver = GNUNET_SCHEDULER_driver_select ();
659 driver->cls = &context;
661 GNUNET_SCHEDULER_run_with_driver (driver, task, task_cls);
663 GNUNET_free (driver);
668 * Obtain the task context, giving the reason why the current task was
671 * @return current tasks' scheduler context
673 const struct GNUNET_SCHEDULER_TaskContext *
674 GNUNET_SCHEDULER_get_task_context ()
676 GNUNET_assert (NULL != active_task);
682 * Get information about the current load of this scheduler. Use this
683 * function to determine if an elective task should be added or simply
684 * dropped (if the decision should be made based on the number of
685 * tasks ready to run).
687 * @param p priority level to look at
688 * @return number of tasks pending right now
691 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
693 struct GNUNET_SCHEDULER_Task *pos;
696 GNUNET_assert (NULL != active_task);
697 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
699 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
700 p = current_priority;
702 for (pos = ready_head[check_priority (p)]; NULL != pos; pos = pos->next)
709 init_fd_info (struct GNUNET_SCHEDULER_Task *t,
710 const struct GNUNET_NETWORK_Handle *const *read_nh,
711 unsigned int read_nh_len,
712 const struct GNUNET_NETWORK_Handle *const *write_nh,
713 unsigned int write_nh_len,
714 const struct GNUNET_DISK_FileHandle *const *read_fh,
715 unsigned int read_fh_len,
716 const struct GNUNET_DISK_FileHandle *const *write_fh,
717 unsigned int write_fh_len)
719 // FIXME: if we have exactly two network handles / exactly two file handles
720 // and they are equal, we can make one FdInfo with both
721 // GNUNET_SCHEDULER_ET_IN and GNUNET_SCHEDULER_ET_OUT set.
722 struct GNUNET_SCHEDULER_FdInfo *fdi;
724 t->fds_len = read_nh_len + write_nh_len + read_fh_len + write_fh_len;
729 if (1 == read_nh_len)
732 GNUNET_assert (NULL != fdi->fd);
733 fdi->et = GNUNET_SCHEDULER_ET_IN;
734 fdi->sock = GNUNET_NETWORK_get_fd (*read_nh);
735 t->read_fd = fdi->sock;
738 else if (1 == write_nh_len)
741 GNUNET_assert (NULL != fdi->fd);
742 fdi->et = GNUNET_SCHEDULER_ET_OUT;
743 fdi->sock = GNUNET_NETWORK_get_fd (*write_nh);
745 t->write_fd = fdi->sock;
747 else if (1 == read_fh_len)
750 GNUNET_assert (NULL != fdi->fh);
751 fdi->et = GNUNET_SCHEDULER_ET_IN;
752 fdi->sock = (*read_fh)->fd; // FIXME: does not work under WIN32
753 t->read_fd = fdi->sock;
759 GNUNET_assert (NULL != fdi->fh);
760 fdi->et = GNUNET_SCHEDULER_ET_OUT;
761 fdi->sock = (*write_fh)->fd; // FIXME: does not work under WIN32
763 t->write_fd = fdi->sock;
768 fdi = GNUNET_new_array (t->fds_len, struct GNUNET_SCHEDULER_FdInfo);
773 for (i = 0; i != read_nh_len; ++i)
775 fdi->fd = read_nh[i];
776 GNUNET_assert (NULL != fdi->fd);
777 fdi->et = GNUNET_SCHEDULER_ET_IN;
778 fdi->sock = GNUNET_NETWORK_get_fd (read_nh[i]);
781 for (i = 0; i != write_nh_len; ++i)
783 fdi->fd = write_nh[i];
784 GNUNET_assert (NULL != fdi->fd);
785 fdi->et = GNUNET_SCHEDULER_ET_OUT;
786 fdi->sock = GNUNET_NETWORK_get_fd (write_nh[i]);
789 for (i = 0; i != read_fh_len; ++i)
791 fdi->fh = read_fh[i];
792 GNUNET_assert (NULL != fdi->fh);
793 fdi->et = GNUNET_SCHEDULER_ET_IN;
794 fdi->sock = (read_fh[i])->fd; // FIXME: does not work under WIN32
797 for (i = 0; i != write_fh_len; ++i)
799 fdi->fh = write_fh[i];
800 GNUNET_assert (NULL != fdi->fh);
801 fdi->et = GNUNET_SCHEDULER_ET_OUT;
802 fdi->sock = (write_fh[i])->fd; // FIXME: does not work under WIN32
810 * calls the given function @a func on each FdInfo related to @a t.
811 * Optionally updates the event type field in each FdInfo after calling
815 * @param driver_func the function to call with each FdInfo contained in
817 * @param if_not_ready only call @a driver_func on FdInfos that are not
819 * @param et the event type to be set in each FdInfo after calling
820 * @a driver_func on it, or -1 if no updating not desired.
822 void scheduler_multi_function_call (struct GNUNET_SCHEDULER_Task *t,
823 int (*driver_func)(),
825 enum GNUNET_SCHEDULER_EventType et)
827 struct GNUNET_SCHEDULER_FdInfo *fdi;
828 int success = GNUNET_YES;
830 for (int i = 0; i != t->fds_len; ++i)
833 if ((GNUNET_NO == if_not_ready) || (GNUNET_SCHEDULER_ET_NONE == fdi->et))
835 success = driver_func (scheduler_driver->cls, t, fdi) && success;
842 if (GNUNET_YES != success)
844 LOG (GNUNET_ERROR_TYPE_ERROR,
845 "driver call not successful");
851 shutdown_cb (void *cls)
854 const struct GNUNET_DISK_FileHandle *pr;
856 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
857 GNUNET_DISK_PIPE_END_READ);
858 GNUNET_assert (! GNUNET_DISK_handle_invalid (pr));
859 /* consume the signal */
860 GNUNET_DISK_file_read (pr, &c, sizeof (c));
861 /* mark all active tasks as ready due to shutdown */
862 GNUNET_SCHEDULER_shutdown ();
867 * Cancel the task with the specified identifier.
868 * The task must not yet have run. Only allowed to be called as long as the
869 * scheduler is running (#GNUNET_SCHEDULER_run or
870 * #GNUNET_SCHEDULER_run_with_driver has been called and has not returned yet).
872 * @param task id of the task to cancel
873 * @return original closure of the task
876 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
878 enum GNUNET_SCHEDULER_Priority p;
881 /* scheduler must be running */
882 GNUNET_assert (NULL != scheduler_driver);
883 GNUNET_assert ( (NULL != active_task) ||
884 (GNUNET_NO == task->lifeness) );
885 if (! task->in_ready_list)
887 if (NULL == task->fds)
889 if (GNUNET_YES == task->on_shutdown)
890 GNUNET_CONTAINER_DLL_remove (shutdown_head,
895 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
896 pending_timeout_tail,
898 if (pending_timeout_last == task)
899 pending_timeout_last = NULL;
901 //TODO check if this is redundant
902 if (task == pending_timeout_last)
903 pending_timeout_last = NULL;
907 GNUNET_CONTAINER_DLL_remove (pending_head,
910 scheduler_multi_function_call(task, scheduler_driver->del, GNUNET_NO, -1);
915 p = check_priority (task->priority);
916 GNUNET_CONTAINER_DLL_remove (ready_head[p],
921 ret = task->callback_cls;
928 * Initialize backtrace data for task @a t
930 * @param t task to initialize
933 init_backtrace (struct GNUNET_SCHEDULER_Task *t)
936 void *backtrace_array[MAX_TRACE_DEPTH];
938 t->num_backtrace_strings
939 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
940 t->backtrace_strings =
941 backtrace_symbols (backtrace_array,
942 t->num_backtrace_strings);
949 * Continue the current execution with the given function. This is
950 * similar to the other "add" functions except that there is no delay
951 * and the reason code can be specified.
953 * @param task main function of the task
954 * @param task_cls closure for @a task
955 * @param reason reason for task invocation
956 * @param priority priority to use for the task
959 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
961 enum GNUNET_SCHEDULER_Reason reason,
962 enum GNUNET_SCHEDULER_Priority priority)
964 struct GNUNET_SCHEDULER_Task *t;
966 GNUNET_assert (NULL != task);
967 GNUNET_assert ((NULL != active_task) ||
968 (GNUNET_SCHEDULER_REASON_STARTUP == reason));
969 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
973 t->callback_cls = task_cls;
975 t->start_time = GNUNET_TIME_absolute_get ();
978 t->priority = priority;
979 t->lifeness = current_lifeness;
980 LOG (GNUNET_ERROR_TYPE_DEBUG,
981 "Adding continuation task %p\n",
984 queue_ready_task (t);
989 * Schedule a new task to be run at the specified time. The task
990 * will be scheduled for execution at time @a at.
992 * @param at time when the operation should run
993 * @param priority priority to use for the task
994 * @param task main function of the task
995 * @param task_cls closure of @a task
996 * @return unique task identifier for the job
997 * only valid until @a task is started!
999 struct GNUNET_SCHEDULER_Task *
1000 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1001 enum GNUNET_SCHEDULER_Priority priority,
1002 GNUNET_SCHEDULER_TaskCallback task,
1005 struct GNUNET_SCHEDULER_Task *t;
1006 struct GNUNET_SCHEDULER_Task *pos;
1007 struct GNUNET_SCHEDULER_Task *prev;
1009 GNUNET_assert (NULL != active_task);
1010 GNUNET_assert (NULL != task);
1011 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1013 t->callback_cls = task_cls;
1017 t->start_time = GNUNET_TIME_absolute_get ();
1020 t->priority = priority;
1021 t->lifeness = current_lifeness;
1022 /* try tail first (optimization in case we are
1023 * appending to a long list of tasks with timeouts) */
1024 if ( (NULL == pending_timeout_head) ||
1025 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us) )
1027 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1028 pending_timeout_tail,
1033 /* first move from heuristic start backwards to before start time */
1034 prev = pending_timeout_last;
1035 while ( (NULL != prev) &&
1036 (prev->timeout.abs_value_us > t->timeout.abs_value_us) )
1038 /* now, move from heuristic start (or head of list) forward to insertion point */
1040 pos = pending_timeout_head;
1043 while ( (NULL != pos) &&
1044 ( (pos->timeout.abs_value_us <= t->timeout.abs_value_us) ||
1045 (0 != pos->reason) ) )
1050 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1051 pending_timeout_tail,
1055 /* finally, update heuristic insertion point to last insertion... */
1056 pending_timeout_last = t;
1058 LOG (GNUNET_ERROR_TYPE_DEBUG,
1067 * Schedule a new task to be run with a specified delay. The task
1068 * will be scheduled for execution once the delay has expired.
1070 * @param delay when should this operation time out?
1071 * @param priority priority to use for the task
1072 * @param task main function of the task
1073 * @param task_cls closure of @a task
1074 * @return unique task identifier for the job
1075 * only valid until @a task is started!
1077 struct GNUNET_SCHEDULER_Task *
1078 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1079 enum GNUNET_SCHEDULER_Priority priority,
1080 GNUNET_SCHEDULER_TaskCallback task,
1083 return GNUNET_SCHEDULER_add_at_with_priority (GNUNET_TIME_relative_to_absolute (delay),
1091 * Schedule a new task to be run with a specified priority.
1093 * @param prio how important is the new task?
1094 * @param task main function of the task
1095 * @param task_cls closure of @a task
1096 * @return unique task identifier for the job
1097 * only valid until @a task is started!
1099 struct GNUNET_SCHEDULER_Task *
1100 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1101 GNUNET_SCHEDULER_TaskCallback task,
1104 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1112 * Schedule a new task to be run at the specified time. The task
1113 * will be scheduled for execution once specified time has been
1114 * reached. It will be run with the DEFAULT priority.
1116 * @param at time at which this operation should run
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_at (struct GNUNET_TIME_Absolute at,
1124 GNUNET_SCHEDULER_TaskCallback task,
1127 return GNUNET_SCHEDULER_add_at_with_priority (at,
1128 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1135 * Schedule a new task to be run with a specified delay. The task
1136 * will be scheduled for execution once the delay has expired. It
1137 * will be run with the DEFAULT priority.
1139 * @param delay when should this operation time out?
1140 * @param task main function of the task
1141 * @param task_cls closure of @a task
1142 * @return unique task identifier for the job
1143 * only valid until @a task is started!
1145 struct GNUNET_SCHEDULER_Task *
1146 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1147 GNUNET_SCHEDULER_TaskCallback task,
1150 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1151 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1158 * Schedule a new task to be run as soon as possible. Note that this
1159 * does not guarantee that this will be the next task that is being
1160 * run, as other tasks with higher priority (or that are already ready
1161 * to run) might get to run first. Just as with delays, clients must
1162 * not rely on any particular order of execution between tasks
1163 * scheduled concurrently.
1165 * The task will be run with the DEFAULT priority.
1167 * @param task main function of the task
1168 * @param task_cls closure of @a task
1169 * @return unique task identifier for the job
1170 * only valid until @a task is started!
1172 struct GNUNET_SCHEDULER_Task *
1173 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
1176 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO,
1183 * Schedule a new task to be run on shutdown, that is when a CTRL-C
1184 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
1187 * @param task main function of the task
1188 * @param task_cls closure of @a task
1189 * @return unique task identifier for the job
1190 * only valid until @a task is started!
1192 struct GNUNET_SCHEDULER_Task *
1193 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1196 struct GNUNET_SCHEDULER_Task *t;
1198 GNUNET_assert (NULL != active_task);
1199 GNUNET_assert (NULL != task);
1200 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1202 t->callback_cls = task_cls;
1206 t->start_time = GNUNET_TIME_absolute_get ();
1208 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1209 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1210 t->on_shutdown = GNUNET_YES;
1211 t->lifeness = GNUNET_NO;
1212 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1215 LOG (GNUNET_ERROR_TYPE_DEBUG,
1216 "Adding shutdown task %p\n",
1224 * Schedule a new task to be run as soon as possible with the
1225 * (transitive) ignore-shutdown flag either explicitly set or
1226 * explicitly enabled. This task (and all tasks created from it,
1227 * other than by another call to this function) will either count or
1228 * not count for the "lifeness" of the process. This API is only
1229 * useful in a few special cases.
1231 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
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_now_with_lifeness (int lifeness,
1239 GNUNET_SCHEDULER_TaskCallback task,
1242 struct GNUNET_SCHEDULER_Task *ret;
1244 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1245 ret->lifeness = lifeness;
1252 * check a raw file descriptor and abort if it is bad (for debugging purposes)
1254 * @param t the task related to the file descriptor
1255 * @param raw_fd the raw file descriptor to check
1258 check_fd (struct GNUNET_SCHEDULER_Task *t, int raw_fd)
1262 int flags = fcntl (raw_fd, F_GETFD);
1264 if ((flags == -1) && (errno == EBADF))
1266 LOG (GNUNET_ERROR_TYPE_ERROR,
1267 "Got invalid file descriptor %d!\n",
1278 * Schedule a new task to be run with a specified delay or when any of
1279 * the specified file descriptor sets is ready. The delay can be used
1280 * as a timeout on the socket(s) being ready. The task will be
1281 * scheduled for execution once either the delay has expired or any of
1282 * the socket operations is ready. This is the most general
1283 * function of the "add" family. Note that the "prerequisite_task"
1284 * must be satisfied in addition to any of the other conditions. In
1285 * other words, the task will be started when
1287 * (prerequisite-run)
1293 * @param delay how long should we wait?
1294 * @param priority priority to use
1295 * @param rfd file descriptor we want to read (can be -1)
1296 * @param wfd file descriptors we want to write (can be -1)
1297 * @param task main function of the task
1298 * @param task_cls closure of @a task
1299 * @return unique task identifier for the job
1300 * only valid until @a task is started!
1303 static struct GNUNET_SCHEDULER_Task *
1304 add_without_sets (struct GNUNET_TIME_Relative delay,
1305 enum GNUNET_SCHEDULER_Priority priority,
1306 const struct GNUNET_NETWORK_Handle *read_nh,
1307 const struct GNUNET_NETWORK_Handle *write_nh,
1308 const struct GNUNET_DISK_FileHandle *read_fh,
1309 const struct GNUNET_DISK_FileHandle *write_fh,
1310 GNUNET_SCHEDULER_TaskCallback task,
1313 struct GNUNET_SCHEDULER_Task *t;
1315 GNUNET_assert (NULL != active_task);
1316 GNUNET_assert (NULL != task);
1317 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1328 t->callback_cls = task_cls;
1330 check_fd (t, NULL != read_nh ? GNUNET_NETWORK_get_fd (read_nh) : -1);
1331 check_fd (t, NULL != write_nh ? GNUNET_NETWORK_get_fd (write_nh) : -1);
1332 check_fd (t, NULL != read_fh ? read_fh->fd : -1);
1333 check_fd (t, NULL != write_fh ? write_fh->fd : -1);
1336 t->start_time = GNUNET_TIME_absolute_get ();
1338 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1339 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1340 t->lifeness = current_lifeness;
1341 GNUNET_CONTAINER_DLL_insert (pending_head,
1344 scheduler_multi_function_call (t, scheduler_driver->add, GNUNET_NO, GNUNET_SCHEDULER_ET_NONE);
1345 max_priority_added = GNUNET_MAX (max_priority_added,
1347 LOG (GNUNET_ERROR_TYPE_DEBUG,
1357 * Schedule a new task to be run with a specified delay or when the
1358 * specified file descriptor is ready for reading. The delay can be
1359 * used as a timeout on the socket being ready. The task will be
1360 * scheduled for execution once either the delay has expired or the
1361 * socket operation is ready. It will be run with the DEFAULT priority.
1362 * Only allowed to be called as long as the scheduler is running
1363 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1364 * called and has not returned yet).
1366 * @param delay when should this operation time out?
1367 * @param rfd read file-descriptor
1368 * @param task main function of the task
1369 * @param task_cls closure of @a task
1370 * @return unique task identifier for the job
1371 * only valid until @a task is started!
1373 struct GNUNET_SCHEDULER_Task *
1374 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1375 struct GNUNET_NETWORK_Handle *rfd,
1376 GNUNET_SCHEDULER_TaskCallback task,
1379 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1380 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1381 rfd, task, task_cls);
1386 * Schedule a new task to be run with a specified priority and to be
1387 * run after the specified delay or when the specified file descriptor
1388 * is ready for reading. The delay can be used as a timeout on the
1389 * socket being ready. The task will be scheduled for execution once
1390 * either the delay has expired or the socket operation is ready. It
1391 * will be run with the DEFAULT priority.
1392 * Only allowed to be called as long as the scheduler is running
1393 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1394 * called and has not returned yet).
1396 * @param delay when should this operation time out?
1397 * @param priority priority to use for the task
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_with_priority (struct GNUNET_TIME_Relative delay,
1406 enum GNUNET_SCHEDULER_Priority priority,
1407 struct GNUNET_NETWORK_Handle *rfd,
1408 GNUNET_SCHEDULER_TaskCallback task,
1411 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1420 * Schedule a new task to be run with a specified delay or when the
1421 * specified file descriptor is ready for writing. The delay can be
1422 * used as a timeout on the socket being ready. The task will be
1423 * scheduled for execution once either the delay has expired or the
1424 * socket operation is ready. It will be run with the priority of
1426 * Only allowed to be called as long as the scheduler is running
1427 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1428 * called and has not returned yet).
1430 * @param delay when should this operation time out?
1431 * @param wfd write file-descriptor
1432 * @param task main function of the task
1433 * @param task_cls closure of @a task
1434 * @return unique task identifier for the job
1435 * only valid until @a task is started!
1437 struct GNUNET_SCHEDULER_Task *
1438 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1439 struct GNUNET_NETWORK_Handle *wfd,
1440 GNUNET_SCHEDULER_TaskCallback task,
1443 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1444 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1446 GNUNET_NO, GNUNET_YES,
1451 * Schedule a new task to be run with a specified delay or when the
1452 * specified file descriptor is ready. 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.
1456 * Only allowed to be called as long as the scheduler is running
1457 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1458 * called and has not returned yet).
1460 * @param delay when should this operation time out?
1461 * @param priority priority of the task
1462 * @param fd file-descriptor
1463 * @param on_read whether to poll the file-descriptor for readability
1464 * @param on_write whether to poll the file-descriptor for writability
1465 * @param task main function of the task
1466 * @param task_cls closure of task
1467 * @return unique task identifier for the job
1468 * only valid until "task" is started!
1470 struct GNUNET_SCHEDULER_Task *
1471 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1472 enum GNUNET_SCHEDULER_Priority priority,
1473 struct GNUNET_NETWORK_Handle *fd,
1476 GNUNET_SCHEDULER_TaskCallback task,
1479 /* scheduler must be running */
1480 GNUNET_assert (NULL != scheduler_driver);
1483 struct GNUNET_NETWORK_FDSet *s;
1484 struct GNUNET_SCHEDULER_Task * ret;
1486 GNUNET_assert (NULL != fd);
1487 s = GNUNET_NETWORK_fdset_create ();
1488 GNUNET_NETWORK_fdset_set (s, fd);
1489 ret = GNUNET_SCHEDULER_add_select (
1492 on_write ? s : NULL,
1494 GNUNET_NETWORK_fdset_destroy (s);
1497 GNUNET_assert (on_read || on_write);
1498 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1499 return add_without_sets (delay, priority,
1500 on_read ? fd : NULL,
1501 on_write ? fd : NULL,
1510 * Schedule a new task to be run with a specified delay or when the
1511 * specified file descriptor is ready for reading. The delay can be
1512 * used as a timeout on the socket being ready. The task will be
1513 * scheduled for execution once either the delay has expired or the
1514 * socket operation is ready. It will be run with the DEFAULT priority.
1515 * Only allowed to be called as long as the scheduler is running
1516 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1517 * called and has not returned yet).
1519 * @param delay when should this operation time out?
1520 * @param rfd read file-descriptor
1521 * @param task main function of the task
1522 * @param task_cls closure of @a task
1523 * @return unique task identifier for the job
1524 * only valid until @a task is started!
1526 struct GNUNET_SCHEDULER_Task *
1527 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1528 const struct GNUNET_DISK_FileHandle *rfd,
1529 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1531 return GNUNET_SCHEDULER_add_file_with_priority (
1532 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1533 rfd, GNUNET_YES, GNUNET_NO,
1539 * Schedule a new task to be run with a specified delay or when the
1540 * specified file descriptor is ready for writing. The delay can be
1541 * used as a timeout on the socket being ready. The task will be
1542 * scheduled for execution once either the delay has expired or the
1543 * socket operation is ready. It will be run with the DEFAULT priority.
1544 * Only allowed to be called as long as the scheduler is running
1545 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1546 * called and has not returned yet).
1548 * @param delay when should this operation time out?
1549 * @param wfd write file-descriptor
1550 * @param task main function of the task
1551 * @param task_cls closure of @a task
1552 * @return unique task identifier for the job
1553 * only valid until @a task is started!
1555 struct GNUNET_SCHEDULER_Task *
1556 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1557 const struct GNUNET_DISK_FileHandle *wfd,
1558 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1560 return GNUNET_SCHEDULER_add_file_with_priority (
1561 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1562 wfd, GNUNET_NO, GNUNET_YES,
1568 * Schedule a new task to be run with a specified delay or when the
1569 * specified file descriptor is ready. The delay can be
1570 * used as a timeout on the socket being ready. The task will be
1571 * scheduled for execution once either the delay has expired or the
1572 * socket operation is ready.
1573 * Only allowed to be called as long as the scheduler is running
1574 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1575 * called and has not returned yet).
1577 * @param delay when should this operation time out?
1578 * @param priority priority of the task
1579 * @param fd file-descriptor
1580 * @param on_read whether to poll the file-descriptor for readability
1581 * @param on_write whether to poll the file-descriptor for writability
1582 * @param task main function of the task
1583 * @param task_cls closure of @a task
1584 * @return unique task identifier for the job
1585 * only valid until @a task is started!
1587 struct GNUNET_SCHEDULER_Task *
1588 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1589 enum GNUNET_SCHEDULER_Priority priority,
1590 const struct GNUNET_DISK_FileHandle *fd,
1591 int on_read, int on_write,
1592 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1594 /* scheduler must be running */
1595 GNUNET_assert (NULL != scheduler_driver);
1598 struct GNUNET_NETWORK_FDSet *s;
1599 struct GNUNET_SCHEDULER_Task * ret;
1601 GNUNET_assert (NULL != fd);
1602 s = GNUNET_NETWORK_fdset_create ();
1603 GNUNET_NETWORK_fdset_handle_set (s, fd);
1604 ret = GNUNET_SCHEDULER_add_select (
1607 on_write ? s : NULL,
1609 GNUNET_NETWORK_fdset_destroy (s);
1612 GNUNET_assert (on_read || on_write);
1613 GNUNET_assert (fd->fd >= 0);
1614 return add_without_sets (delay, priority,
1617 on_read ? fd : NULL,
1618 on_write ? fd : NULL,
1625 extract_handles (struct GNUNET_SCHEDULER_Task *t,
1626 const struct GNUNET_NETWORK_FDSet *fdset,
1627 const struct GNUNET_NETWORK_Handle ***ntarget,
1628 unsigned int *extracted_nhandles,
1629 const struct GNUNET_DISK_FileHandle ***ftarget,
1630 unsigned int *extracted_fhandles)
1632 // FIXME: this implementation only works for unix, for WIN32 the file handles
1633 // in fdset must be handled separately
1634 const struct GNUNET_NETWORK_Handle **nhandles;
1635 const struct GNUNET_DISK_FileHandle **fhandles;
1636 unsigned int nhandles_len, fhandles_len;
1643 for (sock = 0; sock != fdset->nsds; ++sock)
1645 if (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (fdset, sock))
1647 struct GNUNET_NETWORK_Handle *nhandle;
1648 struct GNUNET_DISK_FileHandle *fhandle;
1650 nhandle = GNUNET_NETWORK_socket_box_native (sock);
1651 if (NULL != nhandle)
1653 GNUNET_array_append (nhandles, nhandles_len, nhandle);
1657 fhandle = GNUNET_DISK_get_handle_from_int_fd (sock);
1658 if (NULL != fhandle)
1660 GNUNET_array_append (fhandles, fhandles_len, fhandle);
1669 *ntarget = nhandles_len > 0 ? nhandles : NULL;
1670 *ftarget = fhandles_len > 0 ? fhandles : NULL;
1671 *extracted_nhandles = nhandles_len;
1672 *extracted_fhandles = fhandles_len;
1677 * Schedule a new task to be run with a specified delay or when any of
1678 * the specified file descriptor sets is ready. The delay can be used
1679 * as a timeout on the socket(s) being ready. The task will be
1680 * scheduled for execution once either the delay has expired or any of
1681 * the socket operations is ready. This is the most general
1682 * function of the "add" family. Note that the "prerequisite_task"
1683 * must be satisfied in addition to any of the other conditions. In
1684 * other words, the task will be started when
1686 * (prerequisite-run)
1689 * || any-ws-ready) )
1691 * Only allowed to be called as long as the scheduler is running
1692 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1693 * called and has not returned yet).
1695 * @param prio how important is this task?
1696 * @param delay how long should we wait?
1697 * @param rs set of file descriptors we want to read (can be NULL)
1698 * @param ws set of file descriptors we want to write (can be NULL)
1699 * @param task main function of the task
1700 * @param task_cls closure of @a task
1701 * @return unique task identifier for the job
1702 * only valid until @a task is started!
1704 struct GNUNET_SCHEDULER_Task *
1705 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1706 struct GNUNET_TIME_Relative delay,
1707 const struct GNUNET_NETWORK_FDSet *rs,
1708 const struct GNUNET_NETWORK_FDSet *ws,
1709 GNUNET_SCHEDULER_TaskCallback task,
1712 struct GNUNET_SCHEDULER_Task *t;
1713 const struct GNUNET_NETWORK_Handle **read_nhandles;
1714 const struct GNUNET_NETWORK_Handle **write_nhandles;
1715 const struct GNUNET_DISK_FileHandle **read_fhandles;
1716 const struct GNUNET_DISK_FileHandle **write_fhandles;
1717 unsigned int read_nhandles_len, write_nhandles_len,
1718 read_fhandles_len, write_fhandles_len;
1720 if (((NULL == rs) && (NULL == ws)) || ((0 == rs->nsds) && (0 == ws->nsds)))
1721 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1725 /* scheduler must be running */
1726 GNUNET_assert (NULL != scheduler_driver);
1727 GNUNET_assert (NULL != active_task);
1728 GNUNET_assert (NULL != task);
1729 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1731 t->callback_cls = task_cls;
1734 t->own_handlers = GNUNET_YES;
1735 read_nhandles = NULL;
1736 write_nhandles = NULL;
1737 read_fhandles = NULL;
1738 write_fhandles = NULL;
1739 read_nhandles_len = 0;
1740 write_nhandles_len = 0;
1741 read_fhandles_len = 0;
1742 write_fhandles_len = 0;
1750 &read_fhandles_len);
1757 &write_nhandles_len,
1759 &write_fhandles_len);
1769 write_fhandles_len);
1770 /* free the arrays of pointers to network / file handles, the actual
1771 * handles will be freed in destroy_task */
1772 GNUNET_array_grow (read_nhandles, read_nhandles_len, 0);
1773 GNUNET_array_grow (write_nhandles, write_nhandles_len, 0);
1774 GNUNET_array_grow (read_fhandles, read_fhandles_len, 0);
1775 GNUNET_array_grow (write_fhandles, write_fhandles_len, 0);
1777 t->start_time = GNUNET_TIME_absolute_get ();
1779 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1781 check_priority ((prio ==
1782 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1784 t->lifeness = current_lifeness;
1785 GNUNET_CONTAINER_DLL_insert (pending_head,
1788 scheduler_multi_function_call (t, scheduler_driver->add, GNUNET_NO, GNUNET_SCHEDULER_ET_NONE);
1789 max_priority_added = GNUNET_MAX (max_priority_added,
1791 LOG (GNUNET_ERROR_TYPE_DEBUG,
1800 * Function used by event-loop implementations to signal the scheduler
1801 * that a particular @a task is ready due to an event of type @a et.
1803 * This function will then queue the task to notify the application
1804 * that the task is ready (with the respective priority).
1806 * @param task the task that is ready, NULL for wake up calls
1807 * @param fdi information about the related FD
1810 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1811 struct GNUNET_SCHEDULER_FdInfo *fdi)
1813 enum GNUNET_SCHEDULER_Reason reason;
1814 struct GNUNET_TIME_Absolute now;
1816 now = GNUNET_TIME_absolute_get ();
1817 reason = task->reason;
1818 if (now.abs_value_us >= task->timeout.abs_value_us)
1819 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1820 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1821 (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et)) )
1822 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
1823 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
1824 (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et)) )
1825 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
1826 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1827 task->reason = reason;
1828 if (GNUNET_NO == task->in_ready_list)
1830 GNUNET_CONTAINER_DLL_remove (pending_head,
1833 queue_ready_task (task);
1839 * Function called by the driver to tell the scheduler to run some of
1840 * the tasks that are ready. This function may return even though
1841 * there are tasks left to run just to give other tasks a chance as
1842 * well. If we return #GNUNET_YES, the driver should call this
1843 * function again as soon as possible, while if we return #GNUNET_NO
1844 * it must block until the operating system has more work as the
1845 * scheduler has no more work to do right now.
1847 * @param sh scheduler handle that was given to the `loop`
1848 * @return #GNUNET_OK if there are more tasks that are ready,
1849 * and thus we would like to run more (yield to avoid
1850 * blocking other activities for too long)
1851 * #GNUNET_NO if we are done running tasks (yield to block)
1852 * #GNUNET_SYSERR on error
1855 GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh)
1857 enum GNUNET_SCHEDULER_Priority p;
1858 struct GNUNET_SCHEDULER_Task *pos;
1859 struct GNUNET_TIME_Absolute now;
1861 /* check for tasks that reached the timeout! */
1862 now = GNUNET_TIME_absolute_get ();
1863 while (NULL != (pos = pending_timeout_head))
1865 if (now.abs_value_us >= pos->timeout.abs_value_us)
1866 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1867 if (0 == pos->reason)
1869 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1870 pending_timeout_tail,
1872 if (pending_timeout_last == pos)
1873 pending_timeout_last = NULL;
1874 queue_ready_task (pos);
1879 struct GNUNET_SCHEDULER_Task *next = pos->next;
1880 if (now.abs_value_us >= pos->timeout.abs_value_us)
1882 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1883 GNUNET_CONTAINER_DLL_remove (pending_head,
1886 queue_ready_task (pos);
1891 if (0 == ready_count)
1893 LOG (GNUNET_ERROR_TYPE_WARNING,
1898 /* find out which task priority level we are going to
1899 process this time */
1900 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
1901 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
1902 /* yes, p>0 is correct, 0 is "KEEP" which should
1903 * always be an empty queue (see assertion)! */
1904 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
1906 pos = ready_head[p];
1910 GNUNET_assert (NULL != pos); /* ready_count wrong? */
1912 /* process all tasks at this priority level, then yield */
1913 while (NULL != (pos = ready_head[p]))
1915 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1919 current_priority = pos->priority;
1920 current_lifeness = pos->lifeness;
1923 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
1924 DELAY_THRESHOLD.rel_value_us)
1926 LOG (GNUNET_ERROR_TYPE_DEBUG,
1927 "Task %p took %s to be scheduled\n",
1929 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
1933 tc.reason = pos->reason;
1934 GNUNET_NETWORK_fdset_zero (sh->rs);
1935 GNUNET_NETWORK_fdset_zero (sh->ws);
1936 // FIXME: do we have to remove FdInfos from fds if they are not ready?
1937 tc.fds_len = pos->fds_len;
1939 for (int i = 0; i != pos->fds_len; ++i)
1941 struct GNUNET_SCHEDULER_FdInfo *fdi = &pos->fds[i];
1942 if (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et))
1944 GNUNET_NETWORK_fdset_set_native (sh->rs,
1947 if (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et))
1949 GNUNET_NETWORK_fdset_set_native (sh->ws,
1953 tc.read_ready = sh->rs;
1954 tc.write_ready = sh->ws;
1955 LOG (GNUNET_ERROR_TYPE_DEBUG,
1956 "Running task %p\n",
1958 pos->callback (pos->callback_cls);
1959 scheduler_multi_function_call (pos, scheduler_driver->del, GNUNET_YES, -1);
1961 dump_backtrace (pos);
1965 shutdown_if_no_lifeness ();
1966 if (0 == ready_count)
1968 scheduler_driver->set_wakeup (scheduler_driver->cls,
1972 scheduler_driver->set_wakeup (scheduler_driver->cls,
1973 GNUNET_TIME_absolute_get ());
1979 * Initialize and run scheduler. This function will return when all
1980 * tasks have completed. On systems with signals, receiving a SIGTERM
1981 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
1982 * to be run after the active task is complete. As a result, SIGTERM
1983 * causes all shutdown tasks to be scheduled with reason
1984 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
1985 * afterwards will execute normally!). Note that any particular
1986 * signal will only shut down one scheduler; applications should
1987 * always only create a single scheduler.
1989 * @param driver drive to use for the event loop
1990 * @param task task to run first (and immediately)
1991 * @param task_cls closure of @a task
1992 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1995 GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
1996 GNUNET_SCHEDULER_TaskCallback task,
2000 struct GNUNET_SIGNAL_Context *shc_int;
2001 struct GNUNET_SIGNAL_Context *shc_term;
2002 #if (SIGTERM != GNUNET_TERM_SIG)
2003 struct GNUNET_SIGNAL_Context *shc_gterm;
2006 struct GNUNET_SIGNAL_Context *shc_quit;
2007 struct GNUNET_SIGNAL_Context *shc_hup;
2008 struct GNUNET_SIGNAL_Context *shc_pipe;
2010 struct GNUNET_SCHEDULER_Task tsk;
2011 const struct GNUNET_DISK_FileHandle *pr;
2013 /* general set-up */
2014 GNUNET_assert (NULL == active_task);
2015 GNUNET_assert (NULL == shutdown_pipe_handle);
2016 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
2020 GNUNET_assert (NULL != shutdown_pipe_handle);
2021 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
2022 GNUNET_DISK_PIPE_END_READ);
2024 scheduler_driver = driver;
2026 /* install signal handlers */
2027 LOG (GNUNET_ERROR_TYPE_DEBUG,
2028 "Registering signal handlers\n");
2029 shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2030 &sighandler_shutdown);
2031 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2032 &sighandler_shutdown);
2033 #if (SIGTERM != GNUNET_TERM_SIG)
2034 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2035 &sighandler_shutdown);
2038 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2040 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2041 &sighandler_shutdown);
2042 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2043 &sighandler_shutdown);
2046 /* Setup initial tasks */
2047 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2048 current_lifeness = GNUNET_NO;
2053 GNUNET_SCHEDULER_add_now (&GNUNET_OS_install_parent_control_handler,
2055 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2059 current_lifeness = GNUNET_YES;
2060 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
2062 GNUNET_SCHEDULER_REASON_STARTUP,
2063 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
2065 scheduler_driver->set_wakeup (scheduler_driver->cls,
2067 /* begin main event loop */
2068 sh.rs = GNUNET_NETWORK_fdset_create ();
2069 sh.ws = GNUNET_NETWORK_fdset_create ();
2070 GNUNET_NETWORK_fdset_handle_set (sh.rs, pr);
2071 ret = driver->loop (driver->cls,
2073 GNUNET_NETWORK_fdset_destroy (sh.rs);
2074 GNUNET_NETWORK_fdset_destroy (sh.ws);
2076 /* uninstall signal handlers */
2077 GNUNET_SIGNAL_handler_uninstall (shc_int);
2078 GNUNET_SIGNAL_handler_uninstall (shc_term);
2079 #if (SIGTERM != GNUNET_TERM_SIG)
2080 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
2083 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
2084 GNUNET_SIGNAL_handler_uninstall (shc_quit);
2085 GNUNET_SIGNAL_handler_uninstall (shc_hup);
2087 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2088 shutdown_pipe_handle = NULL;
2089 scheduler_driver = NULL;
2095 select_add (void *cls,
2096 struct GNUNET_SCHEDULER_Task *task,
2097 struct GNUNET_SCHEDULER_FdInfo *fdi)
2099 struct DriverContext *context = cls;
2100 GNUNET_assert (NULL != context);
2101 GNUNET_assert (NULL != task);
2102 GNUNET_assert (NULL != fdi);
2103 GNUNET_assert (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et) ||
2104 0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et));
2106 if (!((NULL != fdi->fd) ^ (NULL != fdi->fh)) || (fdi->sock < 0))
2108 /* exactly one out of {fd, hf} must be != NULL and the OS handle must be valid */
2109 return GNUNET_SYSERR;
2112 struct Scheduled *scheduled = GNUNET_new (struct Scheduled);
2113 scheduled->task = task;
2114 scheduled->fdi = fdi;
2115 scheduled->et = fdi->et;
2117 GNUNET_CONTAINER_DLL_insert (context->scheduled_head,
2118 context->scheduled_tail,
2125 select_del (void *cls,
2126 struct GNUNET_SCHEDULER_Task *task,
2127 struct GNUNET_SCHEDULER_FdInfo *fdi)
2129 struct DriverContext *context;
2130 struct Scheduled *pos;
2133 GNUNET_assert (NULL != cls);
2136 ret = GNUNET_SYSERR;
2137 for (pos = context->scheduled_head; NULL != pos; pos = pos->next)
2139 if (pos->task == task && pos->fdi == fdi)
2141 GNUNET_CONTAINER_DLL_remove (context->scheduled_head,
2142 context->scheduled_tail,
2153 select_loop (void *cls,
2154 struct GNUNET_SCHEDULER_Handle *sh)
2156 struct GNUNET_NETWORK_FDSet *rs;
2157 struct GNUNET_NETWORK_FDSet *ws;
2158 struct DriverContext *context;
2161 unsigned long long last_tr;
2162 unsigned int busy_wait_warning;
2165 GNUNET_assert (NULL != context);
2166 rs = GNUNET_NETWORK_fdset_create ();
2167 ws = GNUNET_NETWORK_fdset_create ();
2168 tasks_ready = GNUNET_NO;
2170 busy_wait_warning = 0;
2171 while (NULL != context->scheduled_head || GNUNET_YES == tasks_ready)
2173 LOG (GNUNET_ERROR_TYPE_WARNING,
2174 "[%p] timeout = %s\n",
2176 GNUNET_STRINGS_relative_time_to_string (context->timeout, GNUNET_NO));
2178 GNUNET_NETWORK_fdset_zero (rs);
2179 GNUNET_NETWORK_fdset_zero (ws);
2180 struct Scheduled *pos;
2181 for (pos = context->scheduled_head; NULL != pos; pos = pos->next)
2183 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et))
2185 GNUNET_NETWORK_fdset_set_native (rs, pos->fdi->sock);
2187 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et))
2189 GNUNET_NETWORK_fdset_set_native (ws, pos->fdi->sock);
2192 if (NULL == scheduler_select)
2194 select_result = GNUNET_NETWORK_socket_select (rs,
2201 select_result = scheduler_select (scheduler_select_cls,
2207 if (select_result == GNUNET_SYSERR)
2209 LOG (GNUNET_ERROR_TYPE_WARNING,
2210 "select_result = GNUNET_SYSERR\n");
2214 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
2219 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
2222 if (0 != system (lsof))
2223 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
2228 struct GNUNET_SCHEDULER_Task *t;
2229 // FIXME: pending_head is a scheduler-internal variable!
2230 for (t = pending_head; NULL != t; t = t->next)
2232 if (-1 != t->read_fd)
2234 int flags = fcntl (t->read_fd, F_GETFD);
2235 if ((flags == -1) && (errno == EBADF))
2237 LOG (GNUNET_ERROR_TYPE_ERROR,
2238 "Got invalid file descriptor %d!\n",
2243 if (-1 != t->write_fd)
2245 int flags = fcntl (t->write_fd, F_GETFD);
2246 if ((flags == -1) && (errno == EBADF))
2248 LOG (GNUNET_ERROR_TYPE_ERROR,
2249 "Got invalid file descriptor %d!\n",
2257 return GNUNET_SYSERR;
2259 if ( (0 == select_result) &&
2260 (0 == context->timeout.rel_value_us) &&
2261 (busy_wait_warning > 16) )
2263 LOG (GNUNET_ERROR_TYPE_WARNING,
2264 "[%p] Looks like we're busy waiting...\n",
2266 //GNUNET_assert (0);
2267 short_wait (100); /* mitigate */
2269 for (pos = context->scheduled_head; NULL != pos; pos = pos->next)
2271 int is_ready = GNUNET_NO;
2272 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et) &&
2273 GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, pos->fdi->sock))
2275 pos->fdi->et |= GNUNET_SCHEDULER_ET_IN;
2276 is_ready = GNUNET_YES;
2278 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et) &&
2279 GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, pos->fdi->sock))
2281 pos->fdi->et |= GNUNET_SCHEDULER_ET_OUT;
2282 is_ready = GNUNET_YES;
2284 if (GNUNET_YES == is_ready)
2286 GNUNET_CONTAINER_DLL_remove (context->scheduled_head,
2287 context->scheduled_tail,
2289 GNUNET_SCHEDULER_task_ready (pos->task, pos->fdi);
2292 tasks_ready = GNUNET_SCHEDULER_run_from_driver (sh);
2293 LOG (GNUNET_ERROR_TYPE_WARNING,
2294 "[%p] tasks_ready: %d\n",
2297 // FIXME: tasks_run is a driver-internal variable! Instead we should increment
2298 // a local variable tasks_ready_count everytime we're calling GNUNET_SCHEDULER_task_ready.
2299 if (last_tr == tasks_run)
2301 LOG (GNUNET_ERROR_TYPE_WARNING,
2302 "[%p] no tasks run\n",
2305 busy_wait_warning++;
2309 last_tr = tasks_run;
2310 busy_wait_warning = 0;
2318 select_set_wakeup(void *cls,
2319 struct GNUNET_TIME_Absolute dt)
2321 struct DriverContext *context = cls;
2322 GNUNET_assert (NULL != context);
2324 context->timeout = GNUNET_TIME_absolute_get_remaining (dt);
2329 * Obtain the driver for using select() as the event loop.
2331 * @return NULL on error
2333 struct GNUNET_SCHEDULER_Driver *
2334 GNUNET_SCHEDULER_driver_select ()
2336 struct GNUNET_SCHEDULER_Driver *select_driver;
2337 select_driver = GNUNET_new (struct GNUNET_SCHEDULER_Driver);
2339 select_driver->loop = &select_loop;
2340 select_driver->add = &select_add;
2341 select_driver->del = &select_del;
2342 select_driver->set_wakeup = &select_set_wakeup;
2344 return select_driver;
2348 /* end of scheduler.c */