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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file util/scheduler.c
22 * @brief schedule computations using continuation passing style
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
31 #define LOG(kind,...) GNUNET_log_from (kind, "util-scheduler", __VA_ARGS__)
33 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-scheduler", syscall)
40 * Use lsof to generate file descriptor reports on select error?
41 * (turn off for stable releases).
43 #define USE_LSOF GNUNET_NO
46 * Obtain trace information for all scheduler calls that schedule tasks.
48 #define EXECINFO GNUNET_NO
51 * Check each file descriptor before adding
53 #define DEBUG_FDS GNUNET_NO
56 * Depth of the traces collected via EXECINFO.
58 #define MAX_TRACE_DEPTH 50
62 * Should we figure out which tasks are delayed for a while
63 * before they are run? (Consider using in combination with EXECINFO).
65 #define PROFILE_DELAYS GNUNET_NO
68 * Task that were in the queue for longer than this are reported if
69 * PROFILE_DELAYS is active.
71 #define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
75 * Argument to be passed from the driver to
76 * #GNUNET_SCHEDULER_do_work(). Contains the
77 * scheduler's internal state.
79 struct GNUNET_SCHEDULER_Handle
82 * Passed here to avoid constantly allocating/deallocating
83 * this element, but generally we want to get rid of this.
86 struct GNUNET_NETWORK_FDSet *rs;
89 * Passed here to avoid constantly allocating/deallocating
90 * this element, but generally we want to get rid of this.
93 struct GNUNET_NETWORK_FDSet *ws;
96 * context of the SIGINT handler
98 struct GNUNET_SIGNAL_Context *shc_int;
101 * context of the SIGTERM handler
103 struct GNUNET_SIGNAL_Context *shc_term;
105 #if (SIGTERM != GNUNET_TERM_SIG)
107 * context of the TERM_SIG handler
109 struct GNUNET_SIGNAL_Context *shc_gterm;
114 * context of the SIGQUIT handler
116 struct GNUNET_SIGNAL_Context *shc_quit;
119 * context of the SIGHUP handler
121 struct GNUNET_SIGNAL_Context *shc_hup;
124 * context of hte SIGPIPE handler
126 struct GNUNET_SIGNAL_Context *shc_pipe;
132 * Entry in list of pending tasks.
134 struct GNUNET_SCHEDULER_Task
137 * This is a linked list.
139 struct GNUNET_SCHEDULER_Task *next;
142 * This is a linked list.
144 struct GNUNET_SCHEDULER_Task *prev;
147 * Function to run when ready.
149 GNUNET_SCHEDULER_TaskCallback callback;
152 * Closure for the @e callback.
157 * Information about which FDs are ready for this task (and why).
159 struct GNUNET_SCHEDULER_FdInfo *fds;
162 * Storage location used for @e fds if we want to avoid
163 * a separate malloc() call in the common case that this
164 * task is only about a single FD.
166 struct GNUNET_SCHEDULER_FdInfo fdx;
169 * Size of the @e fds array.
171 unsigned int fds_len;
174 * Do we own the network and file handles referenced by the FdInfo
175 * structs in the fds array. This will only be GNUNET_YES if the
176 * task was created by the #GNUNET_SCHEDULER_add_select function.
181 * Absolute timeout value for the task, or
182 * #GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
184 struct GNUNET_TIME_Absolute timeout;
188 * When was the task scheduled?
190 struct GNUNET_TIME_Absolute start_time;
194 * Why is the task ready? Set after task is added to ready queue.
195 * Initially set to zero. All reasons that have already been
196 * satisfied (i.e. read or write ready) will be set over time.
198 enum GNUNET_SCHEDULER_Reason reason;
203 enum GNUNET_SCHEDULER_Priority priority;
206 * Set if we only wait for reading from a single FD, otherwise -1.
211 * Set if we only wait for writing to a single FD, otherwise -1.
216 * Should the existence of this task in the queue be counted as
217 * reason to not shutdown the scheduler?
222 * Is this task run on shutdown?
227 * Is this task in the ready list?
233 * Array of strings which make up a backtrace from the point when this
234 * task was scheduled (essentially, who scheduled the task?)
236 char **backtrace_strings;
239 * Size of the backtrace_strings array
241 int num_backtrace_strings;
248 * A struct representing an event the select driver is waiting for
252 struct Scheduled *prev;
254 struct Scheduled *next;
257 * the task, the event is related to
259 struct GNUNET_SCHEDULER_Task *task;
262 * information about the network socket / file descriptor where
263 * the event is expected to occur
265 struct GNUNET_SCHEDULER_FdInfo *fdi;
268 * the event types (multiple event types can be ORed) the select
269 * driver is expected to wait for
271 enum GNUNET_SCHEDULER_EventType et;
276 * Driver context used by GNUNET_SCHEDULER_run
281 * the head of a DLL containing information about the events the
282 * select driver is waiting for
284 struct Scheduled *scheduled_head;
287 * the tail of a DLL containing information about the events the
288 * select driver is waiting for
290 struct Scheduled *scheduled_tail;
293 * the time when the select driver will wake up again (after
296 struct GNUNET_TIME_Absolute timeout;
301 * The driver used for the event loop. Will be handed over to
302 * the scheduler in #GNUNET_SCHEDULER_do_work(), persisted
303 * there in this variable for later use in functions like
304 * #GNUNET_SCHEDULER_add_select(), #add_without_sets() and
305 * #GNUNET_SCHEDULER_cancel().
307 static const struct GNUNET_SCHEDULER_Driver *scheduler_driver;
310 * Head of list of tasks waiting for an event.
312 static struct GNUNET_SCHEDULER_Task *pending_head;
315 * Tail of list of tasks waiting for an event.
317 static struct GNUNET_SCHEDULER_Task *pending_tail;
320 * Head of list of tasks waiting for shutdown.
322 static struct GNUNET_SCHEDULER_Task *shutdown_head;
325 * Tail of list of tasks waiting for shutdown.
327 static struct GNUNET_SCHEDULER_Task *shutdown_tail;
330 * List of tasks waiting ONLY for a timeout event.
331 * Sorted by timeout (earliest first). Used so that
332 * we do not traverse the list of these tasks when
333 * building select sets (we just look at the head
334 * to determine the respective timeout ONCE).
336 static struct GNUNET_SCHEDULER_Task *pending_timeout_head;
339 * List of tasks waiting ONLY for a timeout event.
340 * Sorted by timeout (earliest first). Used so that
341 * we do not traverse the list of these tasks when
342 * building select sets (we just look at the head
343 * to determine the respective timeout ONCE).
345 static struct GNUNET_SCHEDULER_Task *pending_timeout_tail;
348 * Last inserted task waiting ONLY for a timeout event.
349 * Used to (heuristically) speed up insertion.
351 static struct GNUNET_SCHEDULER_Task *pending_timeout_last;
354 * ID of the task that is running right now.
356 static struct GNUNET_SCHEDULER_Task *active_task;
359 * Head of list of tasks ready to run right now, grouped by importance.
361 static struct GNUNET_SCHEDULER_Task *ready_head[GNUNET_SCHEDULER_PRIORITY_COUNT];
364 * Tail of list of tasks ready to run right now, grouped by importance.
366 static struct GNUNET_SCHEDULER_Task *ready_tail[GNUNET_SCHEDULER_PRIORITY_COUNT];
369 * Task for installing parent control handlers (it might happen that the
370 * scheduler is shutdown before this task is executed, so
371 * GNUNET_SCHEDULER_shutdown must cancel it in that case)
373 static struct GNUNET_SCHEDULER_Task *install_parent_control_task;
376 * Task for reading from a pipe that signal handlers will use to initiate
379 static struct GNUNET_SCHEDULER_Task *shutdown_pipe_task;
382 * Number of tasks on the ready list.
384 static unsigned int ready_count;
387 * Priority of the task running right now. Only
388 * valid while a task is running.
390 static enum GNUNET_SCHEDULER_Priority current_priority;
393 * Priority of the highest task added in the current select
396 static enum GNUNET_SCHEDULER_Priority max_priority_added;
399 * Value of the 'lifeness' flag for the current task.
401 static int current_lifeness;
404 * Function to use as a select() in the scheduler.
405 * If NULL, we use GNUNET_NETWORK_socket_select().
407 static GNUNET_SCHEDULER_select scheduler_select;
410 * Task context of the current task.
412 static struct GNUNET_SCHEDULER_TaskContext tc;
415 * Closure for #scheduler_select.
417 static void *scheduler_select_cls;
421 * Sets the select function to use in the scheduler (scheduler_select).
423 * @param new_select new select function to use
424 * @param new_select_cls closure for @a new_select
425 * @return previously used select function, NULL for default
428 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
429 void *new_select_cls)
431 scheduler_select = new_select;
432 scheduler_select_cls = new_select_cls;
437 * Check that the given priority is legal (and return it).
439 * @param p priority value to check
440 * @return p on success, 0 on error
442 static enum GNUNET_SCHEDULER_Priority
443 check_priority (enum GNUNET_SCHEDULER_Priority p)
445 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
448 return 0; /* make compiler happy */
453 * chooses the nearest timeout from all pending tasks, to be used
454 * to tell the driver the next wakeup time (using its set_wakeup
457 struct GNUNET_TIME_Absolute
460 struct GNUNET_SCHEDULER_Task *pos;
461 struct GNUNET_TIME_Absolute now;
462 struct GNUNET_TIME_Absolute timeout;
464 pos = pending_timeout_head;
465 now = GNUNET_TIME_absolute_get ();
466 timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
469 if (0 != pos->reason)
475 timeout = pos->timeout;
478 for (pos = pending_head; NULL != pos; pos = pos->next)
480 if (0 != pos->reason)
484 else if ((pos->timeout.abs_value_us != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) &&
485 (timeout.abs_value_us > pos->timeout.abs_value_us))
487 timeout = pos->timeout;
495 * Put a task that is ready for execution into the ready queue.
497 * @param task task ready for execution
500 queue_ready_task (struct GNUNET_SCHEDULER_Task *task)
502 enum GNUNET_SCHEDULER_Priority p = check_priority (task->priority);
504 GNUNET_CONTAINER_DLL_insert (ready_head[p],
507 task->in_ready_list = GNUNET_YES;
513 * Request the shutdown of a scheduler. Marks all tasks
514 * awaiting shutdown as ready. Note that tasks
515 * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call
516 * will be delayed until the next shutdown signal.
519 GNUNET_SCHEDULER_shutdown ()
521 struct GNUNET_SCHEDULER_Task *pos;
523 LOG (GNUNET_ERROR_TYPE_DEBUG,
524 "GNUNET_SCHEDULER_shutdown\n");
525 if (NULL != install_parent_control_task)
527 GNUNET_SCHEDULER_cancel (install_parent_control_task);
528 install_parent_control_task = NULL;
530 if (NULL != shutdown_pipe_task)
532 GNUNET_SCHEDULER_cancel (shutdown_pipe_task);
533 shutdown_pipe_task = NULL;
535 while (NULL != (pos = shutdown_head))
537 GNUNET_CONTAINER_DLL_remove (shutdown_head,
540 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
541 queue_ready_task (pos);
547 * Output stack trace of task @a t.
549 * @param t task to dump stack trace of
552 dump_backtrace (struct GNUNET_SCHEDULER_Task *t)
555 for (unsigned int i = 0; i < t->num_backtrace_strings; i++)
556 LOG (GNUNET_ERROR_TYPE_WARNING,
557 "Task %p trace %u: %s\n",
560 t->backtrace_strings[i]);
568 * Destroy a task (release associated resources)
570 * @param t task to destroy
573 destroy_task (struct GNUNET_SCHEDULER_Task *t)
577 LOG (GNUNET_ERROR_TYPE_DEBUG,
578 "destroying task %p\n",
581 if (GNUNET_YES == t->own_handles)
583 for (i = 0; i != t->fds_len; ++i)
585 const struct GNUNET_NETWORK_Handle *fd = t->fds[i].fd;
586 const struct GNUNET_DISK_FileHandle *fh = t->fds[i].fh;
589 GNUNET_NETWORK_socket_free_memory_only_ ((struct GNUNET_NETWORK_Handle *) fd);
593 // FIXME: on WIN32 this is not enough! A function
594 // GNUNET_DISK_file_free_memory_only would be nice
595 GNUNET_free ((void *) fh);
601 GNUNET_array_grow (t->fds, t->fds_len, 0);
604 GNUNET_free (t->backtrace_strings);
611 * Pipe used to communicate shutdown via signal.
613 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
616 * Process ID of this process at the time we installed the various
622 * Signal handler called for SIGPIPE.
634 // * Wait for a short time.
635 // * Sleeps for @a ms ms (as that should be long enough for virtually all
636 // * modern systems to context switch and allow another process to do
637 // * some 'real' work).
639 // * @param ms how many ms to wait
642 //short_wait (unsigned int ms)
644 // struct GNUNET_TIME_Relative timeout;
646 // timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
647 // (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
652 * Signal handler called for signals that should cause us to shutdown.
655 sighandler_shutdown ()
658 int old_errno = errno; /* backup errno */
660 if (getpid () != my_pid)
661 _exit (1); /* we have fork'ed since the signal handler was created,
662 * ignore the signal, see https://gnunet.org/vfork discussion */
663 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
664 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
671 shutdown_if_no_lifeness ()
673 struct GNUNET_SCHEDULER_Task *t;
677 for (t = pending_head; NULL != t; t = t->next)
678 if (GNUNET_YES == t->lifeness)
680 for (t = shutdown_head; NULL != t; t = t->next)
681 if (GNUNET_YES == t->lifeness)
683 for (t = pending_timeout_head; NULL != t; t = t->next)
684 if (GNUNET_YES == t->lifeness)
687 GNUNET_SCHEDULER_shutdown ();
692 select_loop (struct GNUNET_SCHEDULER_Handle *sh,
693 struct DriverContext *context);
697 * Initialize and run scheduler. This function will return when all
698 * tasks have completed. On systems with signals, receiving a SIGTERM
699 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown()
700 * to be run after the active task is complete. As a result, SIGTERM
701 * causes all active tasks to be scheduled with reason
702 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
703 * afterwards will execute normally!). Note that any particular signal
704 * will only shut down one scheduler; applications should always only
705 * create a single scheduler.
707 * @param task task to run immediately
708 * @param task_cls closure of @a task
711 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
714 struct GNUNET_SCHEDULER_Handle *sh;
715 struct GNUNET_SCHEDULER_Driver *driver;
716 struct DriverContext context = {.scheduled_head = NULL,
717 .scheduled_tail = NULL,
718 .timeout = GNUNET_TIME_absolute_get ()};
720 driver = GNUNET_SCHEDULER_driver_select ();
721 driver->cls = &context;
722 sh = GNUNET_SCHEDULER_driver_init (driver);
723 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
725 GNUNET_SCHEDULER_REASON_STARTUP,
726 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
729 GNUNET_SCHEDULER_driver_done (sh);
730 GNUNET_free (driver);
735 * Obtain the task context, giving the reason why the current task was
738 * @return current tasks' scheduler context
740 const struct GNUNET_SCHEDULER_TaskContext *
741 GNUNET_SCHEDULER_get_task_context ()
743 GNUNET_assert (NULL != active_task);
749 * Get information about the current load of this scheduler. Use this
750 * function to determine if an elective task should be added or simply
751 * dropped (if the decision should be made based on the number of
752 * tasks ready to run).
754 * @param p priority level to look at
755 * @return number of tasks pending right now
758 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
760 struct GNUNET_SCHEDULER_Task *pos;
763 GNUNET_assert (NULL != active_task);
764 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
766 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
767 p = current_priority;
769 for (pos = ready_head[check_priority (p)]; NULL != pos; pos = pos->next)
776 init_fd_info (struct GNUNET_SCHEDULER_Task *t,
777 const struct GNUNET_NETWORK_Handle *const *read_nh,
778 unsigned int read_nh_len,
779 const struct GNUNET_NETWORK_Handle *const *write_nh,
780 unsigned int write_nh_len,
781 const struct GNUNET_DISK_FileHandle *const *read_fh,
782 unsigned int read_fh_len,
783 const struct GNUNET_DISK_FileHandle *const *write_fh,
784 unsigned int write_fh_len)
786 // FIXME: if we have exactly two network handles / exactly two file handles
787 // and they are equal, we can make one FdInfo with both
788 // GNUNET_SCHEDULER_ET_IN and GNUNET_SCHEDULER_ET_OUT set.
789 struct GNUNET_SCHEDULER_FdInfo *fdi;
791 t->fds_len = read_nh_len + write_nh_len + read_fh_len + write_fh_len;
796 if (1 == read_nh_len)
798 GNUNET_assert (NULL != read_nh);
799 GNUNET_assert (NULL != *read_nh);
801 fdi->et = GNUNET_SCHEDULER_ET_IN;
802 fdi->sock = GNUNET_NETWORK_get_fd (*read_nh);
803 t->read_fd = fdi->sock;
806 else if (1 == write_nh_len)
808 GNUNET_assert (NULL != write_nh);
809 GNUNET_assert (NULL != *write_nh);
811 fdi->et = GNUNET_SCHEDULER_ET_OUT;
812 fdi->sock = GNUNET_NETWORK_get_fd (*write_nh);
814 t->write_fd = fdi->sock;
816 else if (1 == read_fh_len)
818 GNUNET_assert (NULL != read_fh);
819 GNUNET_assert (NULL != *read_fh);
821 fdi->et = GNUNET_SCHEDULER_ET_IN;
822 fdi->sock = (*read_fh)->fd; // FIXME: does not work under WIN32
823 t->read_fd = fdi->sock;
828 GNUNET_assert (NULL != write_fh);
829 GNUNET_assert (NULL != *write_fh);
831 fdi->et = GNUNET_SCHEDULER_ET_OUT;
832 fdi->sock = (*write_fh)->fd; // FIXME: does not work under WIN32
834 t->write_fd = fdi->sock;
839 fdi = GNUNET_new_array (t->fds_len, struct GNUNET_SCHEDULER_FdInfo);
844 for (i = 0; i != read_nh_len; ++i)
846 fdi->fd = read_nh[i];
847 GNUNET_assert (NULL != fdi->fd);
848 fdi->et = GNUNET_SCHEDULER_ET_IN;
849 fdi->sock = GNUNET_NETWORK_get_fd (read_nh[i]);
852 for (i = 0; i != write_nh_len; ++i)
854 fdi->fd = write_nh[i];
855 GNUNET_assert (NULL != fdi->fd);
856 fdi->et = GNUNET_SCHEDULER_ET_OUT;
857 fdi->sock = GNUNET_NETWORK_get_fd (write_nh[i]);
860 for (i = 0; i != read_fh_len; ++i)
862 fdi->fh = read_fh[i];
863 GNUNET_assert (NULL != fdi->fh);
864 fdi->et = GNUNET_SCHEDULER_ET_IN;
865 fdi->sock = (read_fh[i])->fd; // FIXME: does not work under WIN32
868 for (i = 0; i != write_fh_len; ++i)
870 fdi->fh = write_fh[i];
871 GNUNET_assert (NULL != fdi->fh);
872 fdi->et = GNUNET_SCHEDULER_ET_OUT;
873 fdi->sock = (write_fh[i])->fd; // FIXME: does not work under WIN32
881 * calls the given function @a func on each FdInfo related to @a t.
882 * Optionally updates the event type field in each FdInfo after calling
886 * @param driver_func the function to call with each FdInfo contained in
888 * @param if_not_ready only call @a driver_func on FdInfos that are not
890 * @param et the event type to be set in each FdInfo after calling
891 * @a driver_func on it, or -1 if no updating not desired.
894 driver_add_multiple (struct GNUNET_SCHEDULER_Task *t)
896 struct GNUNET_SCHEDULER_FdInfo *fdi;
897 int success = GNUNET_YES;
899 for (unsigned int i = 0; i != t->fds_len; ++i)
902 success = scheduler_driver->add (scheduler_driver->cls,
905 fdi->et = GNUNET_SCHEDULER_ET_NONE;
907 if (GNUNET_YES != success)
909 LOG (GNUNET_ERROR_TYPE_ERROR,
910 "driver could not add task\n");
916 install_parent_control_handler (void *cls)
918 install_parent_control_task = NULL;
919 GNUNET_OS_install_parent_control_handler (NULL);
924 shutdown_pipe_cb (void *cls)
927 const struct GNUNET_DISK_FileHandle *pr;
929 shutdown_pipe_task = NULL;
930 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
931 GNUNET_DISK_PIPE_END_READ);
932 GNUNET_assert (! GNUNET_DISK_handle_invalid (pr));
933 /* consume the signal */
934 GNUNET_DISK_file_read (pr, &c, sizeof (c));
935 /* mark all active tasks as ready due to shutdown */
936 GNUNET_SCHEDULER_shutdown ();
938 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
946 * Cancel the task with the specified identifier.
947 * The task must not yet have run. Only allowed to be called as long as the
948 * scheduler is running, that is one of the following conditions is met:
950 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
951 * - #GNUNET_SCHEDULER_driver_init has been run and
952 * #GNUNET_SCHEDULER_driver_done has not been called yet
954 * @param task id of the task to cancel
955 * @return original closure of the task
958 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
960 enum GNUNET_SCHEDULER_Priority p;
964 LOG (GNUNET_ERROR_TYPE_DEBUG,
965 "canceling task %p\n",
968 /* scheduler must be running */
969 GNUNET_assert (NULL != scheduler_driver);
970 is_fd_task = (NULL != task->fds);
973 int del_result = scheduler_driver->del (scheduler_driver->cls, task);
974 if (GNUNET_OK != del_result)
976 LOG (GNUNET_ERROR_TYPE_ERROR,
977 "driver could not delete task\n");
981 if (! task->in_ready_list)
985 GNUNET_CONTAINER_DLL_remove (pending_head,
989 else if (GNUNET_YES == task->on_shutdown)
991 GNUNET_CONTAINER_DLL_remove (shutdown_head,
997 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
998 pending_timeout_tail,
1000 if (pending_timeout_last == task)
1001 pending_timeout_last = NULL;
1006 p = check_priority (task->priority);
1007 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1012 ret = task->callback_cls;
1013 destroy_task (task);
1019 * Initialize backtrace data for task @a t
1021 * @param t task to initialize
1024 init_backtrace (struct GNUNET_SCHEDULER_Task *t)
1027 void *backtrace_array[MAX_TRACE_DEPTH];
1029 t->num_backtrace_strings
1030 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1031 t->backtrace_strings =
1032 backtrace_symbols (backtrace_array,
1033 t->num_backtrace_strings);
1042 * Continue the current execution with the given function. This is
1043 * similar to the other "add" functions except that there is no delay
1044 * and the reason code can be specified.
1046 * @param task main function of the task
1047 * @param task_cls closure for @a task
1048 * @param reason reason for task invocation
1049 * @param priority priority to use for the task
1052 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
1054 enum GNUNET_SCHEDULER_Reason reason,
1055 enum GNUNET_SCHEDULER_Priority priority)
1057 struct GNUNET_SCHEDULER_Task *t;
1059 /* scheduler must be running */
1060 GNUNET_assert (NULL != scheduler_driver);
1061 GNUNET_assert (NULL != task);
1062 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1066 t->callback_cls = task_cls;
1068 t->start_time = GNUNET_TIME_absolute_get ();
1071 t->priority = check_priority (priority);
1072 t->lifeness = current_lifeness;
1073 LOG (GNUNET_ERROR_TYPE_DEBUG,
1074 "Adding continuation task %p\n",
1077 queue_ready_task (t);
1082 * Schedule a new task to be run at the specified time. The task
1083 * will be scheduled for execution at time @a at.
1085 * @param at time when the operation should run
1086 * @param priority priority to use for the task
1087 * @param task main function of the task
1088 * @param task_cls closure of @a task
1089 * @return unique task identifier for the job
1090 * only valid until @a task is started!
1092 struct GNUNET_SCHEDULER_Task *
1093 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1094 enum GNUNET_SCHEDULER_Priority priority,
1095 GNUNET_SCHEDULER_TaskCallback task,
1098 struct GNUNET_SCHEDULER_Task *t;
1099 struct GNUNET_SCHEDULER_Task *pos;
1100 struct GNUNET_SCHEDULER_Task *prev;
1102 /* scheduler must be running */
1103 GNUNET_assert (NULL != scheduler_driver);
1104 GNUNET_assert (NULL != task);
1105 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1107 t->callback_cls = task_cls;
1111 t->start_time = GNUNET_TIME_absolute_get ();
1114 t->priority = check_priority (priority);
1115 t->lifeness = current_lifeness;
1116 /* try tail first (optimization in case we are
1117 * appending to a long list of tasks with timeouts) */
1118 if ( (NULL == pending_timeout_head) ||
1119 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us) )
1121 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1122 pending_timeout_tail,
1127 /* first move from heuristic start backwards to before start time */
1128 prev = pending_timeout_last;
1129 while ( (NULL != prev) &&
1130 (prev->timeout.abs_value_us > t->timeout.abs_value_us) )
1132 /* now, move from heuristic start (or head of list) forward to insertion point */
1134 pos = pending_timeout_head;
1137 while ((NULL != pos) && (pos->timeout.abs_value_us <= t->timeout.abs_value_us))
1142 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1143 pending_timeout_tail,
1147 /* finally, update heuristic insertion point to last insertion... */
1148 pending_timeout_last = t;
1150 LOG (GNUNET_ERROR_TYPE_DEBUG,
1159 * Schedule a new task to be run with a specified delay. The task
1160 * will be scheduled for execution once the delay has expired.
1162 * @param delay when should this operation time out?
1163 * @param priority priority to use for the task
1164 * @param task main function of the task
1165 * @param task_cls closure of @a task
1166 * @return unique task identifier for the job
1167 * only valid until @a task is started!
1169 struct GNUNET_SCHEDULER_Task *
1170 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1171 enum GNUNET_SCHEDULER_Priority priority,
1172 GNUNET_SCHEDULER_TaskCallback task,
1175 return GNUNET_SCHEDULER_add_at_with_priority (GNUNET_TIME_relative_to_absolute (delay),
1183 * Schedule a new task to be run with a specified priority.
1185 * @param prio how important is the new task?
1186 * @param task main function of the task
1187 * @param task_cls closure of @a task
1188 * @return unique task identifier for the job
1189 * only valid until @a task is started!
1191 struct GNUNET_SCHEDULER_Task *
1192 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1193 GNUNET_SCHEDULER_TaskCallback task,
1196 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1204 * Schedule a new task to be run at the specified time. The task
1205 * will be scheduled for execution once specified time has been
1206 * reached. It will be run with the DEFAULT priority.
1208 * @param at time at which this operation should run
1209 * @param task main function of the task
1210 * @param task_cls closure of @a task
1211 * @return unique task identifier for the job
1212 * only valid until @a task is started!
1214 struct GNUNET_SCHEDULER_Task *
1215 GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1216 GNUNET_SCHEDULER_TaskCallback task,
1219 return GNUNET_SCHEDULER_add_at_with_priority (at,
1220 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1227 * Schedule a new task to be run with a specified delay. The task
1228 * will be scheduled for execution once the delay has expired. It
1229 * will be run with the DEFAULT priority.
1231 * @param delay when should this operation time out?
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_delayed (struct GNUNET_TIME_Relative delay,
1239 GNUNET_SCHEDULER_TaskCallback task,
1242 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1243 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1250 * Schedule a new task to be run as soon as possible. Note that this
1251 * does not guarantee that this will be the next task that is being
1252 * run, as other tasks with higher priority (or that are already ready
1253 * to run) might get to run first. Just as with delays, clients must
1254 * not rely on any particular order of execution between tasks
1255 * scheduled concurrently.
1257 * The task will be run with the DEFAULT priority.
1259 * @param task main function of the task
1260 * @param task_cls closure of @a task
1261 * @return unique task identifier for the job
1262 * only valid until @a task is started!
1264 struct GNUNET_SCHEDULER_Task *
1265 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
1268 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO,
1275 * Schedule a new task to be run on shutdown, that is when a CTRL-C
1276 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
1279 * @param task main function of the task
1280 * @param task_cls closure of @a task
1281 * @return unique task identifier for the job
1282 * only valid until @a task is started!
1284 struct GNUNET_SCHEDULER_Task *
1285 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1288 struct GNUNET_SCHEDULER_Task *t;
1290 /* scheduler must be running */
1291 GNUNET_assert (NULL != scheduler_driver);
1292 GNUNET_assert (NULL != task);
1293 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1295 t->callback_cls = task_cls;
1299 t->start_time = GNUNET_TIME_absolute_get ();
1301 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1302 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1303 t->on_shutdown = GNUNET_YES;
1304 t->lifeness = GNUNET_NO;
1305 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1308 LOG (GNUNET_ERROR_TYPE_DEBUG,
1309 "Adding shutdown task %p\n",
1317 * Schedule a new task to be run as soon as possible with the
1318 * (transitive) ignore-shutdown flag either explicitly set or
1319 * explicitly enabled. This task (and all tasks created from it,
1320 * other than by another call to this function) will either count or
1321 * not count for the "lifeness" of the process. This API is only
1322 * useful in a few special cases.
1324 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
1325 * @param task main function of the task
1326 * @param task_cls closure of @a task
1327 * @return unique task identifier for the job
1328 * only valid until @a task is started!
1330 struct GNUNET_SCHEDULER_Task *
1331 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1332 GNUNET_SCHEDULER_TaskCallback task,
1335 struct GNUNET_SCHEDULER_Task *ret;
1337 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1338 ret->lifeness = lifeness;
1345 * check a raw file descriptor and abort if it is bad (for debugging purposes)
1347 * @param t the task related to the file descriptor
1348 * @param raw_fd the raw file descriptor to check
1351 check_fd (struct GNUNET_SCHEDULER_Task *t, int raw_fd)
1355 int flags = fcntl (raw_fd, F_GETFD);
1357 if ((flags == -1) && (errno == EBADF))
1359 LOG (GNUNET_ERROR_TYPE_ERROR,
1360 "Got invalid file descriptor %d!\n",
1371 * Schedule a new task to be run with a specified delay or when any of
1372 * the specified file descriptor sets is ready. The delay can be used
1373 * as a timeout on the socket(s) being ready. The task will be
1374 * scheduled for execution once either the delay has expired or any of
1375 * the socket operations is ready. This is the most general
1376 * function of the "add" family. Note that the "prerequisite_task"
1377 * must be satisfied in addition to any of the other conditions. In
1378 * other words, the task will be started when
1380 * (prerequisite-run)
1386 * @param delay how long should we wait?
1387 * @param priority priority to use
1388 * @param rfd file descriptor we want to read (can be -1)
1389 * @param wfd file descriptors we want to write (can be -1)
1390 * @param task main function of the task
1391 * @param task_cls closure of @a task
1392 * @return unique task identifier for the job
1393 * only valid until @a task is started!
1396 static struct GNUNET_SCHEDULER_Task *
1397 add_without_sets (struct GNUNET_TIME_Relative delay,
1398 enum GNUNET_SCHEDULER_Priority priority,
1399 const struct GNUNET_NETWORK_Handle *read_nh,
1400 const struct GNUNET_NETWORK_Handle *write_nh,
1401 const struct GNUNET_DISK_FileHandle *read_fh,
1402 const struct GNUNET_DISK_FileHandle *write_fh,
1403 GNUNET_SCHEDULER_TaskCallback task,
1406 struct GNUNET_SCHEDULER_Task *t;
1408 /* scheduler must be running */
1409 GNUNET_assert (NULL != scheduler_driver);
1410 GNUNET_assert (NULL != task);
1411 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1422 t->callback_cls = task_cls;
1424 check_fd (t, NULL != read_nh ? GNUNET_NETWORK_get_fd (read_nh) : -1);
1425 check_fd (t, NULL != write_nh ? GNUNET_NETWORK_get_fd (write_nh) : -1);
1426 check_fd (t, NULL != read_fh ? read_fh->fd : -1);
1427 check_fd (t, NULL != write_fh ? write_fh->fd : -1);
1430 t->start_time = GNUNET_TIME_absolute_get ();
1432 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1433 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1434 t->lifeness = current_lifeness;
1435 GNUNET_CONTAINER_DLL_insert (pending_head,
1438 driver_add_multiple (t);
1439 max_priority_added = GNUNET_MAX (max_priority_added,
1448 * Schedule a new task to be run with a specified delay or when the
1449 * specified file descriptor is ready for reading. The delay can be
1450 * used as a timeout on the socket being ready. The task will be
1451 * scheduled for execution once either the delay has expired or the
1452 * socket operation is ready. It will be run with the DEFAULT priority.
1453 * Only allowed to be called as long as the scheduler is running, that
1454 * is one of the following conditions is met:
1456 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1457 * - #GNUNET_SCHEDULER_driver_init has been run and
1458 * #GNUNET_SCHEDULER_driver_done has not been called yet
1460 * @param delay when should this operation time out?
1461 * @param rfd read file-descriptor
1462 * @param task main function of the task
1463 * @param task_cls closure of @a task
1464 * @return unique task identifier for the job
1465 * only valid until @a task is started!
1467 struct GNUNET_SCHEDULER_Task *
1468 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1469 struct GNUNET_NETWORK_Handle *rfd,
1470 GNUNET_SCHEDULER_TaskCallback task,
1473 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1474 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1475 rfd, task, task_cls);
1480 * Schedule a new task to be run with a specified priority and to be
1481 * run after the specified delay or when the specified file descriptor
1482 * is ready for reading. The delay can be used as a timeout on the
1483 * socket being ready. The task will be scheduled for execution once
1484 * either the delay has expired or the socket operation is ready. It
1485 * will be run with the DEFAULT priority.
1486 * Only allowed to be called as long as the scheduler is running, that
1487 * is one of the following conditions is met:
1489 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1490 * - #GNUNET_SCHEDULER_driver_init has been run and
1491 * #GNUNET_SCHEDULER_driver_done has not been called yet
1493 * @param delay when should this operation time out?
1494 * @param priority priority to use for the task
1495 * @param rfd read file-descriptor
1496 * @param task main function of the task
1497 * @param task_cls closure of @a task
1498 * @return unique task identifier for the job
1499 * only valid until @a task is started!
1501 struct GNUNET_SCHEDULER_Task *
1502 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1503 enum GNUNET_SCHEDULER_Priority priority,
1504 struct GNUNET_NETWORK_Handle *rfd,
1505 GNUNET_SCHEDULER_TaskCallback task,
1508 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1517 * Schedule a new task to be run with a specified delay or when the
1518 * specified file descriptor is ready for writing. The delay can be
1519 * used as a timeout on the socket being ready. The task will be
1520 * scheduled for execution once either the delay has expired or the
1521 * socket operation is ready. It will be run with the priority of
1523 * Only allowed to be called as long as the scheduler is running, that
1524 * is one of the following conditions is met:
1526 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1527 * - #GNUNET_SCHEDULER_driver_init has been run and
1528 * #GNUNET_SCHEDULER_driver_done has not been called yet
1530 * @param delay when should this operation time out?
1531 * @param wfd write file-descriptor
1532 * @param task main function of the task
1533 * @param task_cls closure of @a task
1534 * @return unique task identifier for the job
1535 * only valid until @a task is started!
1537 struct GNUNET_SCHEDULER_Task *
1538 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1539 struct GNUNET_NETWORK_Handle *wfd,
1540 GNUNET_SCHEDULER_TaskCallback task,
1543 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1544 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1546 GNUNET_NO, GNUNET_YES,
1551 * Schedule a new task to be run with a specified delay or when the
1552 * specified file descriptor is ready. The delay can be
1553 * used as a timeout on the socket being ready. The task will be
1554 * scheduled for execution once either the delay has expired or the
1555 * socket operation is ready.
1556 * Only allowed to be called as long as the scheduler is running, that
1557 * is one of the following conditions is met:
1559 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1560 * - #GNUNET_SCHEDULER_driver_init has been run and
1561 * #GNUNET_SCHEDULER_driver_done has not been called yet
1563 * @param delay when should this operation time out?
1564 * @param priority priority of the task
1565 * @param fd file-descriptor
1566 * @param on_read whether to poll the file-descriptor for readability
1567 * @param on_write whether to poll the file-descriptor for writability
1568 * @param task main function of the task
1569 * @param task_cls closure of task
1570 * @return unique task identifier for the job
1571 * only valid until "task" is started!
1573 struct GNUNET_SCHEDULER_Task *
1574 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1575 enum GNUNET_SCHEDULER_Priority priority,
1576 struct GNUNET_NETWORK_Handle *fd,
1579 GNUNET_SCHEDULER_TaskCallback task,
1582 /* scheduler must be running */
1583 GNUNET_assert (NULL != scheduler_driver);
1586 struct GNUNET_NETWORK_FDSet *s;
1587 struct GNUNET_SCHEDULER_Task * ret;
1589 GNUNET_assert (NULL != fd);
1590 s = GNUNET_NETWORK_fdset_create ();
1591 GNUNET_NETWORK_fdset_set (s, fd);
1592 ret = GNUNET_SCHEDULER_add_select (
1595 on_write ? s : NULL,
1597 GNUNET_NETWORK_fdset_destroy (s);
1600 GNUNET_assert (on_read || on_write);
1601 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1602 return add_without_sets (delay, priority,
1603 on_read ? fd : NULL,
1604 on_write ? fd : NULL,
1613 * Schedule a new task to be run with a specified delay or when the
1614 * specified file descriptor is ready for reading. The delay can be
1615 * used as a timeout on the socket being ready. The task will be
1616 * scheduled for execution once either the delay has expired or the
1617 * socket operation is ready. It will be run with the DEFAULT priority.
1618 * Only allowed to be called as long as the scheduler is running, that
1619 * is one of the following conditions is met:
1621 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1622 * - #GNUNET_SCHEDULER_driver_init has been run and
1623 * #GNUNET_SCHEDULER_driver_done has not been called yet
1625 * @param delay when should this operation time out?
1626 * @param rfd read file-descriptor
1627 * @param task main function of the task
1628 * @param task_cls closure of @a task
1629 * @return unique task identifier for the job
1630 * only valid until @a task is started!
1632 struct GNUNET_SCHEDULER_Task *
1633 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1634 const struct GNUNET_DISK_FileHandle *rfd,
1635 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1637 return GNUNET_SCHEDULER_add_file_with_priority (
1638 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1639 rfd, GNUNET_YES, GNUNET_NO,
1645 * Schedule a new task to be run with a specified delay or when the
1646 * specified file descriptor is ready for writing. The delay can be
1647 * used as a timeout on the socket being ready. The task will be
1648 * scheduled for execution once either the delay has expired or the
1649 * socket operation is ready. It will be run with the DEFAULT priority.
1650 * Only allowed to be called as long as the scheduler is running, that
1651 * is one of the following conditions is met:
1653 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1654 * - #GNUNET_SCHEDULER_driver_init has been run and
1655 * #GNUNET_SCHEDULER_driver_done has not been called yet
1657 * @param delay when should this operation time out?
1658 * @param wfd write file-descriptor
1659 * @param task main function of the task
1660 * @param task_cls closure of @a task
1661 * @return unique task identifier for the job
1662 * only valid until @a task is started!
1664 struct GNUNET_SCHEDULER_Task *
1665 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1666 const struct GNUNET_DISK_FileHandle *wfd,
1667 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1669 return GNUNET_SCHEDULER_add_file_with_priority (
1670 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1671 wfd, GNUNET_NO, GNUNET_YES,
1677 * Schedule a new task to be run with a specified delay or when the
1678 * specified file descriptor is ready. The delay can be
1679 * used as a timeout on the socket being ready. The task will be
1680 * scheduled for execution once either the delay has expired or the
1681 * socket operation is ready.
1682 * Only allowed to be called as long as the scheduler is running, that
1683 * is one of the following conditions is met:
1685 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1686 * - #GNUNET_SCHEDULER_driver_init has been run and
1687 * #GNUNET_SCHEDULER_driver_done has not been called yet
1689 * @param delay when should this operation time out?
1690 * @param priority priority of the task
1691 * @param fd file-descriptor
1692 * @param on_read whether to poll the file-descriptor for readability
1693 * @param on_write whether to poll the file-descriptor for writability
1694 * @param task main function of the task
1695 * @param task_cls closure of @a task
1696 * @return unique task identifier for the job
1697 * only valid until @a task is started!
1699 struct GNUNET_SCHEDULER_Task *
1700 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1701 enum GNUNET_SCHEDULER_Priority priority,
1702 const struct GNUNET_DISK_FileHandle *fd,
1703 int on_read, int on_write,
1704 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1706 /* scheduler must be running */
1707 GNUNET_assert (NULL != scheduler_driver);
1710 struct GNUNET_NETWORK_FDSet *s;
1711 struct GNUNET_SCHEDULER_Task * ret;
1713 GNUNET_assert (NULL != fd);
1714 s = GNUNET_NETWORK_fdset_create ();
1715 GNUNET_NETWORK_fdset_handle_set (s, fd);
1716 ret = GNUNET_SCHEDULER_add_select (
1719 on_write ? s : NULL,
1721 GNUNET_NETWORK_fdset_destroy (s);
1724 GNUNET_assert (on_read || on_write);
1725 GNUNET_assert (fd->fd >= 0);
1726 return add_without_sets (delay, priority,
1729 on_read ? fd : NULL,
1730 on_write ? fd : NULL,
1737 extract_handles (const struct GNUNET_NETWORK_FDSet *fdset,
1738 const struct GNUNET_NETWORK_Handle ***ntarget,
1739 unsigned int *extracted_nhandles,
1740 const struct GNUNET_DISK_FileHandle ***ftarget,
1741 unsigned int *extracted_fhandles)
1743 // FIXME: this implementation only works for unix, for WIN32 the file handles
1744 // in fdset must be handled separately
1745 const struct GNUNET_NETWORK_Handle **nhandles;
1746 const struct GNUNET_DISK_FileHandle **fhandles;
1747 unsigned int nhandles_len;
1748 unsigned int fhandles_len;
1754 for (int sock = 0; sock != fdset->nsds; ++sock)
1756 if (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (fdset, sock))
1758 struct GNUNET_NETWORK_Handle *nhandle;
1759 struct GNUNET_DISK_FileHandle *fhandle;
1761 nhandle = GNUNET_NETWORK_socket_box_native (sock);
1762 if (NULL != nhandle)
1764 GNUNET_array_append (nhandles, nhandles_len, nhandle);
1768 fhandle = GNUNET_DISK_get_handle_from_int_fd (sock);
1769 if (NULL != fhandle)
1771 GNUNET_array_append (fhandles, fhandles_len, fhandle);
1780 *ntarget = nhandles_len > 0 ? nhandles : NULL;
1781 *ftarget = fhandles_len > 0 ? fhandles : NULL;
1782 *extracted_nhandles = nhandles_len;
1783 *extracted_fhandles = fhandles_len;
1788 * Schedule a new task to be run with a specified delay or when any of
1789 * the specified file descriptor sets is ready. The delay can be used
1790 * as a timeout on the socket(s) being ready. The task will be
1791 * scheduled for execution once either the delay has expired or any of
1792 * the socket operations is ready. This is the most general
1793 * function of the "add" family. Note that the "prerequisite_task"
1794 * must be satisfied in addition to any of the other conditions. In
1795 * other words, the task will be started when
1797 * (prerequisite-run)
1800 * || any-ws-ready) )
1802 * Only allowed to be called as long as the scheduler is running, that
1803 * is one of the following conditions is met:
1805 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1806 * - #GNUNET_SCHEDULER_driver_init has been run and
1807 * #GNUNET_SCHEDULER_driver_done has not been called yet
1809 * @param prio how important is this task?
1810 * @param delay how long should we wait?
1811 * @param rs set of file descriptors we want to read (can be NULL)
1812 * @param ws set of file descriptors we want to write (can be NULL)
1813 * @param task main function of the task
1814 * @param task_cls closure of @a task
1815 * @return unique task identifier for the job
1816 * only valid until @a task is started!
1818 struct GNUNET_SCHEDULER_Task *
1819 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1820 struct GNUNET_TIME_Relative delay,
1821 const struct GNUNET_NETWORK_FDSet *rs,
1822 const struct GNUNET_NETWORK_FDSet *ws,
1823 GNUNET_SCHEDULER_TaskCallback task,
1826 struct GNUNET_SCHEDULER_Task *t;
1827 const struct GNUNET_NETWORK_Handle **read_nhandles = NULL;
1828 const struct GNUNET_NETWORK_Handle **write_nhandles = NULL;
1829 const struct GNUNET_DISK_FileHandle **read_fhandles = NULL;
1830 const struct GNUNET_DISK_FileHandle **write_fhandles = NULL;
1831 unsigned int read_nhandles_len = 0;
1832 unsigned int write_nhandles_len = 0;
1833 unsigned int read_fhandles_len = 0;
1834 unsigned int write_fhandles_len = 0;
1836 /* scheduler must be running */
1837 GNUNET_assert (NULL != scheduler_driver);
1838 GNUNET_assert (NULL != task);
1839 int no_rs = (NULL == rs);
1840 int no_ws = (NULL == ws);
1841 int empty_rs = (NULL != rs) && (0 == rs->nsds);
1842 int empty_ws = (NULL != ws) && (0 == ws->nsds);
1843 int no_fds = (no_rs && no_ws) ||
1844 (empty_rs && empty_ws) ||
1845 (no_rs && empty_ws) ||
1846 (no_ws && empty_rs);
1851 extract_handles (rs,
1855 &read_fhandles_len);
1859 extract_handles (ws,
1861 &write_nhandles_len,
1863 &write_fhandles_len);
1867 * here we consider the case that a GNUNET_NETWORK_FDSet might be empty
1868 * although its maximum FD number (nsds) is greater than 0. We handle
1869 * this case gracefully because some libraries such as libmicrohttpd
1870 * only provide a hint what the maximum FD number in an FD set might be
1871 * and not the exact FD number (see e.g. gnunet-rest-service.c)
1873 int no_fds_extracted = (0 == read_nhandles_len) &&
1874 (0 == read_fhandles_len) &&
1875 (0 == write_nhandles_len) &&
1876 (0 == write_fhandles_len);
1877 if (no_fds || no_fds_extracted)
1878 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1882 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1891 write_fhandles_len);
1893 t->callback_cls = task_cls;
1894 t->own_handles = GNUNET_YES;
1895 /* free the arrays of pointers to network / file handles, the actual
1896 * handles will be freed in destroy_task */
1897 GNUNET_array_grow (read_nhandles, read_nhandles_len, 0);
1898 GNUNET_array_grow (write_nhandles, write_nhandles_len, 0);
1899 GNUNET_array_grow (read_fhandles, read_fhandles_len, 0);
1900 GNUNET_array_grow (write_fhandles, write_fhandles_len, 0);
1902 t->start_time = GNUNET_TIME_absolute_get ();
1904 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1906 check_priority ((prio ==
1907 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1909 t->lifeness = current_lifeness;
1910 GNUNET_CONTAINER_DLL_insert (pending_head,
1913 driver_add_multiple (t);
1914 max_priority_added = GNUNET_MAX (max_priority_added,
1916 LOG (GNUNET_ERROR_TYPE_DEBUG,
1925 * Function used by event-loop implementations to signal the scheduler
1926 * that a particular @a task is ready due to an event specified in the
1927 * et field of @a fdi.
1929 * This function will then queue the task to notify the application
1930 * that the task is ready (with the respective priority).
1932 * @param task the task that is ready
1933 * @param fdi information about the related FD
1936 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1937 struct GNUNET_SCHEDULER_FdInfo *fdi)
1939 enum GNUNET_SCHEDULER_Reason reason;
1941 reason = task->reason;
1942 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1943 (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et)) )
1944 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
1945 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
1946 (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et)) )
1947 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
1948 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1949 task->reason = reason;
1950 if (GNUNET_NO == task->in_ready_list)
1952 GNUNET_CONTAINER_DLL_remove (pending_head,
1955 queue_ready_task (task);
1961 * Function called by external event loop implementations to tell the
1962 * scheduler to run some of the tasks that are ready. Must be called
1963 * only after #GNUNET_SCHEDULER_driver_init has been called and before
1964 * #GNUNET_SCHEDULER_driver_done is called.
1965 * This function may return even though there are tasks left to run
1966 * just to give other tasks a chance as well. If we return #GNUNET_YES,
1967 * the event loop implementation should call this function again as
1968 * soon as possible, while if we return #GNUNET_NO it must block until
1969 * either the operating system has more work (the scheduler has no more
1970 * work to do right now) or the timeout set by the scheduler (using the
1971 * set_wakeup callback) is reached.
1973 * @param sh scheduler handle that was returned by
1974 * #GNUNET_SCHEDULER_driver_init
1975 * @return #GNUNET_YES if there are more tasks that are ready,
1976 * and thus we would like to run more (yield to avoid
1977 * blocking other activities for too long) #GNUNET_NO
1978 * if we are done running tasks (yield to block)
1981 GNUNET_SCHEDULER_do_work (struct GNUNET_SCHEDULER_Handle *sh)
1983 enum GNUNET_SCHEDULER_Priority p;
1984 struct GNUNET_SCHEDULER_Task *pos;
1985 struct GNUNET_TIME_Absolute now;
1987 /* check for tasks that reached the timeout! */
1988 now = GNUNET_TIME_absolute_get ();
1989 pos = pending_timeout_head;
1992 struct GNUNET_SCHEDULER_Task *next = pos->next;
1993 if (now.abs_value_us >= pos->timeout.abs_value_us)
1994 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1995 if (0 == pos->reason)
1997 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1998 pending_timeout_tail,
2000 if (pending_timeout_last == pos)
2001 pending_timeout_last = NULL;
2002 queue_ready_task (pos);
2008 struct GNUNET_SCHEDULER_Task *next = pos->next;
2009 if (now.abs_value_us >= pos->timeout.abs_value_us)
2011 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
2012 GNUNET_CONTAINER_DLL_remove (pending_head,
2015 queue_ready_task (pos);
2020 if (0 == ready_count)
2022 struct GNUNET_TIME_Absolute timeout = get_timeout ();
2024 if (timeout.abs_value_us > now.abs_value_us)
2027 * The event loop called this function before the current timeout was
2028 * reached (and no FD tasks are ready). This is acceptable if
2030 * - the system time was changed while the driver was waiting for
2032 * - an external event loop called GNUnet API functions outside of
2033 * the callbacks called in GNUNET_SCHEDULER_do_work and thus
2034 * wasn't notified about the new timeout
2036 * It might also mean we are busy-waiting because of a programming
2037 * error in the external event loop.
2039 LOG (GNUNET_ERROR_TYPE_DEBUG,
2040 "GNUNET_SCHEDULER_do_work did not find any ready "
2041 "tasks and timeout has not been reached yet.\n");
2046 * the current timeout was reached but no ready tasks were found,
2047 * internal scheduler error!
2054 /* find out which task priority level we are going to
2055 process this time */
2056 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
2057 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
2058 /* yes, p>0 is correct, 0 is "KEEP" which should
2059 * always be an empty queue (see assertion)! */
2060 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
2062 pos = ready_head[p];
2066 GNUNET_assert (NULL != pos); /* ready_count wrong? */
2068 /* process all tasks at this priority level, then yield */
2069 while (NULL != (pos = ready_head[p]))
2071 GNUNET_CONTAINER_DLL_remove (ready_head[p],
2075 current_priority = pos->priority;
2076 current_lifeness = pos->lifeness;
2079 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
2080 DELAY_THRESHOLD.rel_value_us)
2082 LOG (GNUNET_ERROR_TYPE_DEBUG,
2083 "Task %p took %s to be scheduled\n",
2085 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
2089 tc.reason = pos->reason;
2090 GNUNET_NETWORK_fdset_zero (sh->rs);
2091 GNUNET_NETWORK_fdset_zero (sh->ws);
2092 // FIXME: do we have to remove FdInfos from fds if they are not ready?
2093 tc.fds_len = pos->fds_len;
2095 for (unsigned int i = 0; i != pos->fds_len; ++i)
2097 struct GNUNET_SCHEDULER_FdInfo *fdi = &pos->fds[i];
2098 if (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et))
2100 GNUNET_NETWORK_fdset_set_native (sh->rs,
2103 if (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et))
2105 GNUNET_NETWORK_fdset_set_native (sh->ws,
2109 tc.read_ready = sh->rs;
2110 tc.write_ready = sh->ws;
2111 LOG (GNUNET_ERROR_TYPE_DEBUG,
2112 "Running task %p\n",
2114 GNUNET_assert (NULL != pos->callback);
2115 pos->callback (pos->callback_cls);
2116 if (NULL != pos->fds)
2118 int del_result = scheduler_driver->del (scheduler_driver->cls, pos);
2119 if (GNUNET_OK != del_result)
2121 LOG (GNUNET_ERROR_TYPE_ERROR,
2122 "driver could not delete task %p\n", pos);
2127 dump_backtrace (pos);
2131 shutdown_if_no_lifeness ();
2132 if (0 == ready_count)
2134 scheduler_driver->set_wakeup (scheduler_driver->cls,
2138 scheduler_driver->set_wakeup (scheduler_driver->cls,
2139 GNUNET_TIME_absolute_get ());
2145 * Function called by external event loop implementations to initialize
2146 * the scheduler. An external implementation has to provide @a driver
2147 * which contains callbacks for the scheduler (see definition of struct
2148 * #GNUNET_SCHEDULER_Driver). The callbacks are used to instruct the
2149 * external implementation to watch for events. If it detects any of
2150 * those events it is expected to call #GNUNET_SCHEDULER_do_work to let
2151 * the scheduler handle it. If an event is related to a specific task
2152 * (e.g. the scheduler gave instructions to watch a file descriptor),
2153 * the external implementation is expected to mark that task ready
2154 * before by calling #GNUNET_SCHEDULER_task_ready.
2156 * This function has to be called before any tasks are scheduled and
2157 * before GNUNET_SCHEDULER_do_work is called for the first time. It
2158 * allocates resources that have to be freed again by calling
2159 * #GNUNET_SCHEDULER_driver_done.
2161 * This function installs the same signal handlers as
2162 * #GNUNET_SCHEDULER_run. This means SIGTERM (and other similar signals)
2163 * will induce a call to #GNUNET_SCHEDULER_shutdown during the next
2164 * call to #GNUNET_SCHEDULER_do_work. As a result, SIGTERM causes all
2165 * active tasks to be scheduled with reason
2166 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added afterwards
2167 * will execute normally!). Note that any particular signal will only
2168 * shut down one scheduler; applications should always only create a
2171 * @param driver to use for the event loop
2172 * @return handle to be passed to #GNUNET_SCHEDULER_do_work and
2173 * #GNUNET_SCHEDULER_driver_done
2175 struct GNUNET_SCHEDULER_Handle *
2176 GNUNET_SCHEDULER_driver_init (const struct GNUNET_SCHEDULER_Driver *driver)
2178 struct GNUNET_SCHEDULER_Handle *sh;
2179 const struct GNUNET_DISK_FileHandle *pr;
2181 /* scheduler must not be running */
2182 GNUNET_assert (NULL == scheduler_driver);
2183 GNUNET_assert (NULL == shutdown_pipe_handle);
2184 /* general set-up */
2185 sh = GNUNET_new (struct GNUNET_SCHEDULER_Handle);
2186 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
2190 GNUNET_assert (NULL != shutdown_pipe_handle);
2191 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
2192 GNUNET_DISK_PIPE_END_READ);
2194 scheduler_driver = driver;
2196 /* install signal handlers */
2197 LOG (GNUNET_ERROR_TYPE_DEBUG,
2198 "Registering signal handlers\n");
2199 sh->shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2200 &sighandler_shutdown);
2201 sh->shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2202 &sighandler_shutdown);
2203 #if (SIGTERM != GNUNET_TERM_SIG)
2204 sh->shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2205 &sighandler_shutdown);
2208 sh->shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2210 sh->shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2211 &sighandler_shutdown);
2212 sh->shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2213 &sighandler_shutdown);
2216 /* Setup initial tasks */
2217 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2218 current_lifeness = GNUNET_NO;
2219 install_parent_control_task =
2220 GNUNET_SCHEDULER_add_now (&install_parent_control_handler,
2222 shutdown_pipe_task =
2223 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2227 current_lifeness = GNUNET_YES;
2228 scheduler_driver->set_wakeup (scheduler_driver->cls,
2230 /* begin main event loop */
2231 sh->rs = GNUNET_NETWORK_fdset_create ();
2232 sh->ws = GNUNET_NETWORK_fdset_create ();
2233 GNUNET_NETWORK_fdset_handle_set (sh->rs, pr);
2239 * Counter-part of #GNUNET_SCHEDULER_driver_init. Has to be called
2240 * by external event loop implementations after the scheduler has
2241 * shut down. This is the case if both of the following conditions
2244 * - all tasks the scheduler has added through the driver's add
2245 * callback have been removed again through the driver's del
2247 * - the timeout the scheduler has set through the driver's
2248 * add_wakeup callback is FOREVER
2250 * @param sh the handle returned by #GNUNET_SCHEDULER_driver_init
2253 GNUNET_SCHEDULER_driver_done (struct GNUNET_SCHEDULER_Handle *sh)
2255 GNUNET_assert (NULL == pending_head);
2256 GNUNET_assert (NULL == pending_timeout_head);
2257 GNUNET_assert (NULL == shutdown_head);
2258 for (int i = 0; i != GNUNET_SCHEDULER_PRIORITY_COUNT; ++i)
2260 GNUNET_assert (NULL == ready_head[i]);
2262 GNUNET_NETWORK_fdset_destroy (sh->rs);
2263 GNUNET_NETWORK_fdset_destroy (sh->ws);
2265 /* uninstall signal handlers */
2266 GNUNET_SIGNAL_handler_uninstall (sh->shc_int);
2267 GNUNET_SIGNAL_handler_uninstall (sh->shc_term);
2268 #if (SIGTERM != GNUNET_TERM_SIG)
2269 GNUNET_SIGNAL_handler_uninstall (sh->shc_gterm);
2272 GNUNET_SIGNAL_handler_uninstall (sh->shc_pipe);
2273 GNUNET_SIGNAL_handler_uninstall (sh->shc_quit);
2274 GNUNET_SIGNAL_handler_uninstall (sh->shc_hup);
2276 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2277 shutdown_pipe_handle = NULL;
2278 scheduler_driver = NULL;
2284 select_loop (struct GNUNET_SCHEDULER_Handle *sh,
2285 struct DriverContext *context)
2287 struct GNUNET_NETWORK_FDSet *rs;
2288 struct GNUNET_NETWORK_FDSet *ws;
2291 GNUNET_assert (NULL != context);
2292 rs = GNUNET_NETWORK_fdset_create ();
2293 ws = GNUNET_NETWORK_fdset_create ();
2294 while ( (NULL != context->scheduled_head) ||
2295 (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != context->timeout.abs_value_us) )
2297 LOG (GNUNET_ERROR_TYPE_DEBUG,
2298 "select timeout = %s\n",
2299 GNUNET_STRINGS_absolute_time_to_string (context->timeout));
2301 GNUNET_NETWORK_fdset_zero (rs);
2302 GNUNET_NETWORK_fdset_zero (ws);
2304 for (struct Scheduled *pos = context->scheduled_head;
2308 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et))
2310 GNUNET_NETWORK_fdset_set_native (rs, pos->fdi->sock);
2312 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et))
2314 GNUNET_NETWORK_fdset_set_native (ws, pos->fdi->sock);
2317 struct GNUNET_TIME_Relative time_remaining =
2318 GNUNET_TIME_absolute_get_remaining (context->timeout);
2319 if (NULL == scheduler_select)
2321 select_result = GNUNET_NETWORK_socket_select (rs,
2328 select_result = scheduler_select (scheduler_select_cls,
2334 if (select_result == GNUNET_SYSERR)
2339 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
2351 if (0 != system (lsof))
2352 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
2357 for (struct Scheduled *s = context->scheduled_head;
2361 int flags = fcntl (s->fdi->sock,
2364 if ( (flags == -1) &&
2367 LOG (GNUNET_ERROR_TYPE_ERROR,
2368 "Got invalid file descriptor %d!\n",
2371 dump_backtrace (s->task);
2377 GNUNET_NETWORK_fdset_destroy (rs);
2378 GNUNET_NETWORK_fdset_destroy (ws);
2379 return GNUNET_SYSERR;
2381 if (select_result > 0)
2383 for (struct Scheduled *pos = context->scheduled_head;
2387 int is_ready = GNUNET_NO;
2389 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et) &&
2391 GNUNET_NETWORK_fdset_test_native (rs,
2394 pos->fdi->et |= GNUNET_SCHEDULER_ET_IN;
2395 is_ready = GNUNET_YES;
2397 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et) &&
2399 GNUNET_NETWORK_fdset_test_native (ws,
2402 pos->fdi->et |= GNUNET_SCHEDULER_ET_OUT;
2403 is_ready = GNUNET_YES;
2405 if (GNUNET_YES == is_ready)
2407 GNUNET_SCHEDULER_task_ready (pos->task,
2412 if (GNUNET_YES == GNUNET_SCHEDULER_do_work (sh))
2414 LOG (GNUNET_ERROR_TYPE_DEBUG,
2415 "scheduler has more tasks ready!\n");
2418 GNUNET_NETWORK_fdset_destroy (rs);
2419 GNUNET_NETWORK_fdset_destroy (ws);
2425 select_add (void *cls,
2426 struct GNUNET_SCHEDULER_Task *task,
2427 struct GNUNET_SCHEDULER_FdInfo *fdi)
2429 struct DriverContext *context = cls;
2430 GNUNET_assert (NULL != context);
2431 GNUNET_assert (NULL != task);
2432 GNUNET_assert (NULL != fdi);
2433 GNUNET_assert (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et) ||
2434 0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et));
2436 if (!((NULL != fdi->fd) ^ (NULL != fdi->fh)) || (fdi->sock < 0))
2438 /* exactly one out of {fd, hf} must be != NULL and the OS handle must be valid */
2439 return GNUNET_SYSERR;
2442 struct Scheduled *scheduled = GNUNET_new (struct Scheduled);
2443 scheduled->task = task;
2444 scheduled->fdi = fdi;
2445 scheduled->et = fdi->et;
2447 GNUNET_CONTAINER_DLL_insert (context->scheduled_head,
2448 context->scheduled_tail,
2455 select_del (void *cls,
2456 struct GNUNET_SCHEDULER_Task *task)
2458 struct DriverContext *context;
2459 struct Scheduled *pos;
2462 GNUNET_assert (NULL != cls);
2465 ret = GNUNET_SYSERR;
2466 pos = context->scheduled_head;
2469 struct Scheduled *next = pos->next;
2470 if (pos->task == task)
2472 GNUNET_CONTAINER_DLL_remove (context->scheduled_head,
2473 context->scheduled_tail,
2485 select_set_wakeup (void *cls,
2486 struct GNUNET_TIME_Absolute dt)
2488 struct DriverContext *context = cls;
2490 GNUNET_assert (NULL != context);
2491 context->timeout = dt;
2496 * Obtain the driver for using select() as the event loop.
2498 * @return NULL on error
2500 struct GNUNET_SCHEDULER_Driver *
2501 GNUNET_SCHEDULER_driver_select ()
2503 struct GNUNET_SCHEDULER_Driver *select_driver;
2504 select_driver = GNUNET_new (struct GNUNET_SCHEDULER_Driver);
2506 select_driver->add = &select_add;
2507 select_driver->del = &select_del;
2508 select_driver->set_wakeup = &select_set_wakeup;
2510 return select_driver;
2514 /* end of scheduler.c */