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, \
42 * Use lsof to generate file descriptor reports on select error?
43 * (turn off for stable releases).
45 #define USE_LSOF GNUNET_NO
48 * Obtain trace information for all scheduler calls that schedule tasks.
50 #define EXECINFO GNUNET_NO
53 * Check each file descriptor before adding
55 #define DEBUG_FDS GNUNET_NO
58 * Depth of the traces collected via EXECINFO.
60 #define MAX_TRACE_DEPTH 50
64 * Should we figure out which tasks are delayed for a while
65 * before they are run? (Consider using in combination with EXECINFO).
67 #define PROFILE_DELAYS GNUNET_NO
70 * Task that were in the queue for longer than this are reported if
71 * PROFILE_DELAYS is active.
73 #define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
77 * Argument to be passed from the driver to
78 * #GNUNET_SCHEDULER_do_work(). Contains the
79 * scheduler's internal state.
81 struct GNUNET_SCHEDULER_Handle
84 * Passed here to avoid constantly allocating/deallocating
85 * this element, but generally we want to get rid of this.
88 struct GNUNET_NETWORK_FDSet *rs;
91 * Passed here to avoid constantly allocating/deallocating
92 * this element, but generally we want to get rid of this.
95 struct GNUNET_NETWORK_FDSet *ws;
98 * context of the SIGINT handler
100 struct GNUNET_SIGNAL_Context *shc_int;
103 * context of the SIGTERM handler
105 struct GNUNET_SIGNAL_Context *shc_term;
107 #if (SIGTERM != GNUNET_TERM_SIG)
109 * context of the TERM_SIG handler
111 struct GNUNET_SIGNAL_Context *shc_gterm;
115 * context of the SIGQUIT handler
117 struct GNUNET_SIGNAL_Context *shc_quit;
120 * context of the SIGHUP handler
122 struct GNUNET_SIGNAL_Context *shc_hup;
125 * context of hte SIGPIPE handler
127 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;
245 * Asynchronous scope of the task that scheduled this scope,
247 struct GNUNET_AsyncScopeSave scope;
252 * A struct representing an event the select driver is waiting for
256 struct Scheduled *prev;
258 struct Scheduled *next;
261 * the task, the event is related to
263 struct GNUNET_SCHEDULER_Task *task;
266 * information about the network socket / file descriptor where
267 * the event is expected to occur
269 struct GNUNET_SCHEDULER_FdInfo *fdi;
272 * the event types (multiple event types can be ORed) the select
273 * driver is expected to wait for
275 enum GNUNET_SCHEDULER_EventType et;
280 * Driver context used by GNUNET_SCHEDULER_run
285 * the head of a DLL containing information about the events the
286 * select driver is waiting for
288 struct Scheduled *scheduled_head;
291 * the tail of a DLL containing information about the events the
292 * select driver is waiting for
294 struct Scheduled *scheduled_tail;
297 * the time when the select driver will wake up again (after
300 struct GNUNET_TIME_Absolute timeout;
305 * The driver used for the event loop. Will be handed over to
306 * the scheduler in #GNUNET_SCHEDULER_do_work(), persisted
307 * there in this variable for later use in functions like
308 * #GNUNET_SCHEDULER_add_select(), #add_without_sets() and
309 * #GNUNET_SCHEDULER_cancel().
311 static const struct GNUNET_SCHEDULER_Driver *scheduler_driver;
314 * Head of list of tasks waiting for an event.
316 static struct GNUNET_SCHEDULER_Task *pending_head;
319 * Tail of list of tasks waiting for an event.
321 static struct GNUNET_SCHEDULER_Task *pending_tail;
324 * Head of list of tasks waiting for shutdown.
326 static struct GNUNET_SCHEDULER_Task *shutdown_head;
329 * Tail of list of tasks waiting for shutdown.
331 static struct GNUNET_SCHEDULER_Task *shutdown_tail;
334 * List of tasks waiting ONLY for a timeout event.
335 * Sorted by timeout (earliest first). Used so that
336 * we do not traverse the list of these tasks when
337 * building select sets (we just look at the head
338 * to determine the respective timeout ONCE).
340 static struct GNUNET_SCHEDULER_Task *pending_timeout_head;
343 * List of tasks waiting ONLY for a timeout event.
344 * Sorted by timeout (earliest first). Used so that
345 * we do not traverse the list of these tasks when
346 * building select sets (we just look at the head
347 * to determine the respective timeout ONCE).
349 static struct GNUNET_SCHEDULER_Task *pending_timeout_tail;
352 * Last inserted task waiting ONLY for a timeout event.
353 * Used to (heuristically) speed up insertion.
355 static struct GNUNET_SCHEDULER_Task *pending_timeout_last;
358 * ID of the task that is running right now.
360 static struct GNUNET_SCHEDULER_Task *active_task;
363 * Head of list of tasks ready to run right now, grouped by importance.
366 GNUNET_SCHEDULER_Task *ready_head[GNUNET_SCHEDULER_PRIORITY_COUNT];
369 * Tail of list of tasks ready to run right now, grouped by importance.
372 GNUNET_SCHEDULER_Task *ready_tail[GNUNET_SCHEDULER_PRIORITY_COUNT];
375 * Task for installing parent control handlers (it might happen that the
376 * scheduler is shutdown before this task is executed, so
377 * GNUNET_SCHEDULER_shutdown must cancel it in that case)
379 static struct GNUNET_SCHEDULER_Task *install_parent_control_task;
382 * Task for reading from a pipe that signal handlers will use to initiate
385 static struct GNUNET_SCHEDULER_Task *shutdown_pipe_task;
388 * Number of tasks on the ready list.
390 static unsigned int ready_count;
393 * Priority of the task running right now. Only
394 * valid while a task is running.
396 static enum GNUNET_SCHEDULER_Priority current_priority;
399 * Priority of the highest task added in the current select
402 static enum GNUNET_SCHEDULER_Priority max_priority_added;
405 * Value of the 'lifeness' flag for the current task.
407 static int current_lifeness;
410 * Function to use as a select() in the scheduler.
411 * If NULL, we use GNUNET_NETWORK_socket_select().
413 static GNUNET_SCHEDULER_select scheduler_select;
416 * Task context of the current task.
418 static struct GNUNET_SCHEDULER_TaskContext tc;
421 * Closure for #scheduler_select.
423 static void *scheduler_select_cls;
427 * Sets the select function to use in the scheduler (scheduler_select).
429 * @param new_select new select function to use
430 * @param new_select_cls closure for @a new_select
431 * @return previously used select function, NULL for default
434 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
435 void *new_select_cls)
437 scheduler_select = new_select;
438 scheduler_select_cls = new_select_cls;
443 * Check that the given priority is legal (and return it).
445 * @param p priority value to check
446 * @return p on success, 0 on error
448 static enum GNUNET_SCHEDULER_Priority
449 check_priority (enum GNUNET_SCHEDULER_Priority p)
451 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
454 return 0; /* make compiler happy */
459 * chooses the nearest timeout from all pending tasks, to be used
460 * to tell the driver the next wakeup time (using its set_wakeup
463 struct GNUNET_TIME_Absolute
466 struct GNUNET_SCHEDULER_Task *pos;
467 struct GNUNET_TIME_Absolute now;
468 struct GNUNET_TIME_Absolute timeout;
470 pos = pending_timeout_head;
471 now = GNUNET_TIME_absolute_get ();
472 timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
475 if (0 != pos->reason)
481 timeout = pos->timeout;
484 for (pos = pending_head; NULL != pos; pos = pos->next)
486 if (0 != pos->reason)
490 else if ((pos->timeout.abs_value_us !=
491 GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) &&
492 (timeout.abs_value_us > pos->timeout.abs_value_us))
494 timeout = pos->timeout;
502 * Put a task that is ready for execution into the ready queue.
504 * @param task task ready for execution
507 queue_ready_task (struct GNUNET_SCHEDULER_Task *task)
509 enum GNUNET_SCHEDULER_Priority p = check_priority (task->priority);
511 GNUNET_CONTAINER_DLL_insert (ready_head[p],
514 task->in_ready_list = GNUNET_YES;
520 * Request the shutdown of a scheduler. Marks all tasks
521 * awaiting shutdown as ready. Note that tasks
522 * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call
523 * will be delayed until the next shutdown signal.
526 GNUNET_SCHEDULER_shutdown ()
528 struct GNUNET_SCHEDULER_Task *pos;
530 LOG (GNUNET_ERROR_TYPE_DEBUG,
531 "GNUNET_SCHEDULER_shutdown\n");
532 if (NULL != install_parent_control_task)
534 GNUNET_SCHEDULER_cancel (install_parent_control_task);
535 install_parent_control_task = NULL;
537 if (NULL != shutdown_pipe_task)
539 GNUNET_SCHEDULER_cancel (shutdown_pipe_task);
540 shutdown_pipe_task = NULL;
542 while (NULL != (pos = shutdown_head))
544 GNUNET_CONTAINER_DLL_remove (shutdown_head,
547 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
548 queue_ready_task (pos);
554 * Output stack trace of task @a t.
556 * @param t task to dump stack trace of
559 dump_backtrace (struct GNUNET_SCHEDULER_Task *t)
562 for (unsigned int i = 0; i < t->num_backtrace_strings; i++)
563 LOG (GNUNET_ERROR_TYPE_WARNING,
564 "Task %p trace %u: %s\n",
567 t->backtrace_strings[i]);
575 * Destroy a task (release associated resources)
577 * @param t task to destroy
580 destroy_task (struct GNUNET_SCHEDULER_Task *t)
584 LOG (GNUNET_ERROR_TYPE_DEBUG,
585 "destroying task %p\n",
588 if (GNUNET_YES == t->own_handles)
590 for (i = 0; i != t->fds_len; ++i)
592 const struct GNUNET_NETWORK_Handle *fd = t->fds[i].fd;
593 const struct GNUNET_DISK_FileHandle *fh = t->fds[i].fh;
596 GNUNET_NETWORK_socket_free_memory_only_ ((struct
597 GNUNET_NETWORK_Handle *) fd);
601 // FIXME: on WIN32 this is not enough! A function
602 // GNUNET_DISK_file_free_memory_only would be nice
603 GNUNET_free ((void *) fh);
609 GNUNET_array_grow (t->fds, t->fds_len, 0);
612 GNUNET_free (t->backtrace_strings);
619 * Pipe used to communicate shutdown via signal.
621 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
624 * Process ID of this process at the time we installed the various
630 * Signal handler called for SIGPIPE.
640 // * Wait for a short time.
641 // * Sleeps for @a ms ms (as that should be long enough for virtually all
642 // * modern systems to context switch and allow another process to do
643 // * some 'real' work).
645 // * @param ms how many ms to wait
648 // short_wait (unsigned int ms)
650 // struct GNUNET_TIME_Relative timeout;
652 // timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
653 // (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
658 * Signal handler called for signals that should cause us to shutdown.
661 sighandler_shutdown ()
664 int old_errno = errno; /* backup errno */
666 if (getpid () != my_pid)
667 _exit (1); /* we have fork'ed since the signal handler was created,
668 * ignore the signal, see https://gnunet.org/vfork discussion */
669 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
670 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
677 shutdown_if_no_lifeness ()
679 struct GNUNET_SCHEDULER_Task *t;
683 for (t = pending_head; NULL != t; t = t->next)
684 if (GNUNET_YES == t->lifeness)
686 for (t = shutdown_head; NULL != t; t = t->next)
687 if (GNUNET_YES == t->lifeness)
689 for (t = pending_timeout_head; NULL != t; t = t->next)
690 if (GNUNET_YES == t->lifeness)
693 GNUNET_SCHEDULER_shutdown ();
698 select_loop (struct GNUNET_SCHEDULER_Handle *sh,
699 struct DriverContext *context);
703 * Initialize and run scheduler. This function will return when all
704 * tasks have completed. On systems with signals, receiving a SIGTERM
705 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown()
706 * to be run after the active task is complete. As a result, SIGTERM
707 * causes all active tasks to be scheduled with reason
708 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
709 * afterwards will execute normally!). Note that any particular signal
710 * will only shut down one scheduler; applications should always only
711 * create a single scheduler.
713 * @param task task to run immediately
714 * @param task_cls closure of @a task
717 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
720 struct GNUNET_SCHEDULER_Handle *sh;
721 struct GNUNET_SCHEDULER_Driver *driver;
722 struct DriverContext context = { .scheduled_head = NULL,
723 .scheduled_tail = NULL,
724 .timeout = GNUNET_TIME_absolute_get () };
726 driver = GNUNET_SCHEDULER_driver_select ();
727 driver->cls = &context;
728 sh = GNUNET_SCHEDULER_driver_init (driver);
729 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
731 GNUNET_SCHEDULER_REASON_STARTUP,
732 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
735 GNUNET_SCHEDULER_driver_done (sh);
736 GNUNET_free (driver);
741 * Obtain the task context, giving the reason why the current task was
744 * @return current tasks' scheduler context
746 const struct GNUNET_SCHEDULER_TaskContext *
747 GNUNET_SCHEDULER_get_task_context ()
749 GNUNET_assert (NULL != active_task);
755 * Get information about the current load of this scheduler. Use this
756 * function to determine if an elective task should be added or simply
757 * dropped (if the decision should be made based on the number of
758 * tasks ready to run).
760 * @param p priority level to look at
761 * @return number of tasks pending right now
764 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
766 struct GNUNET_SCHEDULER_Task *pos;
769 GNUNET_assert (NULL != active_task);
770 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
772 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
773 p = current_priority;
775 for (pos = ready_head[check_priority (p)]; NULL != pos; pos = pos->next)
782 init_fd_info (struct GNUNET_SCHEDULER_Task *t,
783 const struct GNUNET_NETWORK_Handle *const *read_nh,
784 unsigned int read_nh_len,
785 const struct GNUNET_NETWORK_Handle *const *write_nh,
786 unsigned int write_nh_len,
787 const struct GNUNET_DISK_FileHandle *const *read_fh,
788 unsigned int read_fh_len,
789 const struct GNUNET_DISK_FileHandle *const *write_fh,
790 unsigned int write_fh_len)
792 // FIXME: if we have exactly two network handles / exactly two file handles
793 // and they are equal, we can make one FdInfo with both
794 // GNUNET_SCHEDULER_ET_IN and GNUNET_SCHEDULER_ET_OUT set.
795 struct GNUNET_SCHEDULER_FdInfo *fdi;
797 t->fds_len = read_nh_len + write_nh_len + read_fh_len + write_fh_len;
802 if (1 == read_nh_len)
804 GNUNET_assert (NULL != read_nh);
805 GNUNET_assert (NULL != *read_nh);
807 fdi->et = GNUNET_SCHEDULER_ET_IN;
808 fdi->sock = GNUNET_NETWORK_get_fd (*read_nh);
809 t->read_fd = fdi->sock;
812 else if (1 == write_nh_len)
814 GNUNET_assert (NULL != write_nh);
815 GNUNET_assert (NULL != *write_nh);
817 fdi->et = GNUNET_SCHEDULER_ET_OUT;
818 fdi->sock = GNUNET_NETWORK_get_fd (*write_nh);
820 t->write_fd = fdi->sock;
822 else if (1 == read_fh_len)
824 GNUNET_assert (NULL != read_fh);
825 GNUNET_assert (NULL != *read_fh);
827 fdi->et = GNUNET_SCHEDULER_ET_IN;
828 fdi->sock = (*read_fh)->fd; // FIXME: does not work under WIN32
829 t->read_fd = fdi->sock;
834 GNUNET_assert (NULL != write_fh);
835 GNUNET_assert (NULL != *write_fh);
837 fdi->et = GNUNET_SCHEDULER_ET_OUT;
838 fdi->sock = (*write_fh)->fd; // FIXME: does not work under WIN32
840 t->write_fd = fdi->sock;
845 fdi = GNUNET_new_array (t->fds_len, struct GNUNET_SCHEDULER_FdInfo);
850 for (i = 0; i != read_nh_len; ++i)
852 fdi->fd = read_nh[i];
853 GNUNET_assert (NULL != fdi->fd);
854 fdi->et = GNUNET_SCHEDULER_ET_IN;
855 fdi->sock = GNUNET_NETWORK_get_fd (read_nh[i]);
858 for (i = 0; i != write_nh_len; ++i)
860 fdi->fd = write_nh[i];
861 GNUNET_assert (NULL != fdi->fd);
862 fdi->et = GNUNET_SCHEDULER_ET_OUT;
863 fdi->sock = GNUNET_NETWORK_get_fd (write_nh[i]);
866 for (i = 0; i != read_fh_len; ++i)
868 fdi->fh = read_fh[i];
869 GNUNET_assert (NULL != fdi->fh);
870 fdi->et = GNUNET_SCHEDULER_ET_IN;
871 fdi->sock = (read_fh[i])->fd; // FIXME: does not work under WIN32
874 for (i = 0; i != write_fh_len; ++i)
876 fdi->fh = write_fh[i];
877 GNUNET_assert (NULL != fdi->fh);
878 fdi->et = GNUNET_SCHEDULER_ET_OUT;
879 fdi->sock = (write_fh[i])->fd; // FIXME: does not work under WIN32
887 * calls the given function @a func on each FdInfo related to @a t.
888 * Optionally updates the event type field in each FdInfo after calling
892 * @param driver_func the function to call with each FdInfo contained in
894 * @param if_not_ready only call @a driver_func on FdInfos that are not
896 * @param et the event type to be set in each FdInfo after calling
897 * @a driver_func on it, or -1 if no updating not desired.
900 driver_add_multiple (struct GNUNET_SCHEDULER_Task *t)
902 struct GNUNET_SCHEDULER_FdInfo *fdi;
903 int success = GNUNET_YES;
905 for (unsigned int i = 0; i != t->fds_len; ++i)
908 success = scheduler_driver->add (scheduler_driver->cls,
911 fdi->et = GNUNET_SCHEDULER_ET_NONE;
913 if (GNUNET_YES != success)
915 LOG (GNUNET_ERROR_TYPE_ERROR,
916 "driver could not add task\n");
922 install_parent_control_handler (void *cls)
925 install_parent_control_task = NULL;
926 GNUNET_OS_install_parent_control_handler (NULL);
931 shutdown_pipe_cb (void *cls)
934 const struct GNUNET_DISK_FileHandle *pr;
937 shutdown_pipe_task = NULL;
938 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
939 GNUNET_DISK_PIPE_END_READ);
940 GNUNET_assert (! GNUNET_DISK_handle_invalid (pr));
941 /* consume the signal */
942 GNUNET_DISK_file_read (pr, &c, sizeof(c));
943 /* mark all active tasks as ready due to shutdown */
944 GNUNET_SCHEDULER_shutdown ();
946 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
954 * Cancel the task with the specified identifier.
955 * The task must not yet have run. Only allowed to be called as long as the
956 * scheduler is running, that is one of the following conditions is met:
958 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
959 * - #GNUNET_SCHEDULER_driver_init has been run and
960 * #GNUNET_SCHEDULER_driver_done has not been called yet
962 * @param task id of the task to cancel
963 * @return original closure of the task
966 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
968 enum GNUNET_SCHEDULER_Priority p;
972 LOG (GNUNET_ERROR_TYPE_DEBUG,
973 "canceling task %p\n",
976 /* scheduler must be running */
977 GNUNET_assert (NULL != scheduler_driver);
978 is_fd_task = (NULL != task->fds);
981 int del_result = scheduler_driver->del (scheduler_driver->cls, task);
982 if (GNUNET_OK != del_result)
984 LOG (GNUNET_ERROR_TYPE_ERROR,
985 "driver could not delete task\n");
989 if (! task->in_ready_list)
993 GNUNET_CONTAINER_DLL_remove (pending_head,
997 else if (GNUNET_YES == task->on_shutdown)
999 GNUNET_CONTAINER_DLL_remove (shutdown_head,
1005 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1006 pending_timeout_tail,
1008 if (pending_timeout_last == task)
1009 pending_timeout_last = NULL;
1014 p = check_priority (task->priority);
1015 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1020 ret = task->callback_cls;
1021 destroy_task (task);
1027 * Initialize backtrace data for task @a t
1029 * @param t task to initialize
1032 init_backtrace (struct GNUNET_SCHEDULER_Task *t)
1035 void *backtrace_array[MAX_TRACE_DEPTH];
1037 t->num_backtrace_strings
1038 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1039 t->backtrace_strings =
1040 backtrace_symbols (backtrace_array,
1041 t->num_backtrace_strings);
1050 * Continue the current execution with the given function. This is
1051 * similar to the other "add" functions except that there is no delay
1052 * and the reason code can be specified.
1054 * @param task main function of the task
1055 * @param task_cls closure for @a task
1056 * @param reason reason for task invocation
1057 * @param priority priority to use for the task
1060 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback
1063 enum GNUNET_SCHEDULER_Reason
1065 enum GNUNET_SCHEDULER_Priority
1068 struct GNUNET_SCHEDULER_Task *t;
1070 /* scheduler must be running */
1071 GNUNET_assert (NULL != scheduler_driver);
1072 GNUNET_assert (NULL != task);
1073 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1077 t->callback_cls = task_cls;
1079 t->start_time = GNUNET_TIME_absolute_get ();
1082 t->priority = check_priority (priority);
1083 t->lifeness = current_lifeness;
1084 LOG (GNUNET_ERROR_TYPE_DEBUG,
1085 "Adding continuation task %p\n",
1088 queue_ready_task (t);
1093 * Schedule a new task to be run at the specified time. The task
1094 * will be scheduled for execution at time @a at.
1096 * @param at time when the operation should run
1097 * @param priority priority to use for the task
1098 * @param task main function of the task
1099 * @param task_cls closure of @a task
1100 * @return unique task identifier for the job
1101 * only valid until @a task is started!
1103 struct GNUNET_SCHEDULER_Task *
1104 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1105 enum GNUNET_SCHEDULER_Priority priority,
1106 GNUNET_SCHEDULER_TaskCallback task,
1109 struct GNUNET_SCHEDULER_Task *t;
1110 struct GNUNET_SCHEDULER_Task *pos;
1111 struct GNUNET_SCHEDULER_Task *prev;
1113 /* scheduler must be running */
1114 GNUNET_assert (NULL != scheduler_driver);
1115 GNUNET_assert (NULL != task);
1116 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1117 GNUNET_async_scope_get (&t->scope);
1119 t->callback_cls = task_cls;
1123 t->start_time = GNUNET_TIME_absolute_get ();
1126 t->priority = check_priority (priority);
1127 t->lifeness = current_lifeness;
1128 /* try tail first (optimization in case we are
1129 * appending to a long list of tasks with timeouts) */
1130 if ((NULL == pending_timeout_head) ||
1131 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us))
1133 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1134 pending_timeout_tail,
1139 /* first move from heuristic start backwards to before start time */
1140 prev = pending_timeout_last;
1141 while ((NULL != prev) &&
1142 (prev->timeout.abs_value_us > t->timeout.abs_value_us))
1144 /* now, move from heuristic start (or head of list) forward to insertion point */
1146 pos = pending_timeout_head;
1149 while ((NULL != pos) && (pos->timeout.abs_value_us <=
1150 t->timeout.abs_value_us))
1155 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1156 pending_timeout_tail,
1160 /* finally, update heuristic insertion point to last insertion... */
1161 pending_timeout_last = t;
1163 LOG (GNUNET_ERROR_TYPE_DEBUG,
1172 * Schedule a new task to be run with a specified delay. The task
1173 * will be scheduled for execution once the delay has expired.
1175 * @param delay when should this operation time out?
1176 * @param priority priority to use for the task
1177 * @param task main function of the task
1178 * @param task_cls closure of @a task
1179 * @return unique task identifier for the job
1180 * only valid until @a task is started!
1182 struct GNUNET_SCHEDULER_Task *
1183 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1184 enum GNUNET_SCHEDULER_Priority
1186 GNUNET_SCHEDULER_TaskCallback task,
1189 return GNUNET_SCHEDULER_add_at_with_priority (
1190 GNUNET_TIME_relative_to_absolute (delay),
1198 * Schedule a new task to be run with a specified priority.
1200 * @param prio how important is the new task?
1201 * @param task main function of the task
1202 * @param task_cls closure of @a task
1203 * @return unique task identifier for the job
1204 * only valid until @a task is started!
1206 struct GNUNET_SCHEDULER_Task *
1207 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1208 GNUNET_SCHEDULER_TaskCallback task,
1211 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1219 * Schedule a new task to be run at the specified time. The task
1220 * will be scheduled for execution once specified time has been
1221 * reached. It will be run with the DEFAULT priority.
1223 * @param at time at which this operation should run
1224 * @param task main function of the task
1225 * @param task_cls closure of @a task
1226 * @return unique task identifier for the job
1227 * only valid until @a task is started!
1229 struct GNUNET_SCHEDULER_Task *
1230 GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1231 GNUNET_SCHEDULER_TaskCallback task,
1234 return GNUNET_SCHEDULER_add_at_with_priority (at,
1235 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1242 * Schedule a new task to be run with a specified delay. The task
1243 * will be scheduled for execution once the delay has expired. It
1244 * will be run with the DEFAULT priority.
1246 * @param delay when should this operation time out?
1247 * @param task main function of the task
1248 * @param task_cls closure of @a task
1249 * @return unique task identifier for the job
1250 * only valid until @a task is started!
1252 struct GNUNET_SCHEDULER_Task *
1253 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1254 GNUNET_SCHEDULER_TaskCallback task,
1257 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1258 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1265 * Schedule a new task to be run as soon as possible. Note that this
1266 * does not guarantee that this will be the next task that is being
1267 * run, as other tasks with higher priority (or that are already ready
1268 * to run) might get to run first. Just as with delays, clients must
1269 * not rely on any particular order of execution between tasks
1270 * scheduled concurrently.
1272 * The task will be run with the DEFAULT priority.
1274 * @param task main function of the task
1275 * @param task_cls closure of @a task
1276 * @return unique task identifier for the job
1277 * only valid until @a task is started!
1279 struct GNUNET_SCHEDULER_Task *
1280 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
1283 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO,
1290 * Schedule a new task to be run on shutdown, that is when a CTRL-C
1291 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
1294 * @param task main function of the task
1295 * @param task_cls closure of @a task
1296 * @return unique task identifier for the job
1297 * only valid until @a task is started!
1299 struct GNUNET_SCHEDULER_Task *
1300 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1303 struct GNUNET_SCHEDULER_Task *t;
1305 /* scheduler must be running */
1306 GNUNET_assert (NULL != scheduler_driver);
1307 GNUNET_assert (NULL != task);
1308 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1309 GNUNET_async_scope_get (&t->scope);
1311 t->callback_cls = task_cls;
1315 t->start_time = GNUNET_TIME_absolute_get ();
1317 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1318 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1319 t->on_shutdown = GNUNET_YES;
1320 t->lifeness = GNUNET_NO;
1321 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1324 LOG (GNUNET_ERROR_TYPE_DEBUG,
1325 "Adding shutdown task %p\n",
1333 * Schedule a new task to be run as soon as possible with the
1334 * (transitive) ignore-shutdown flag either explicitly set or
1335 * explicitly enabled. This task (and all tasks created from it,
1336 * other than by another call to this function) will either count or
1337 * not count for the "lifeness" of the process. This API is only
1338 * useful in a few special cases.
1340 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
1341 * @param task main function of the task
1342 * @param task_cls closure of @a task
1343 * @return unique task identifier for the job
1344 * only valid until @a task is started!
1346 struct GNUNET_SCHEDULER_Task *
1347 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1348 GNUNET_SCHEDULER_TaskCallback task,
1351 struct GNUNET_SCHEDULER_Task *ret;
1353 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1354 ret->lifeness = lifeness;
1361 * check a raw file descriptor and abort if it is bad (for debugging purposes)
1363 * @param t the task related to the file descriptor
1364 * @param raw_fd the raw file descriptor to check
1367 check_fd (struct GNUNET_SCHEDULER_Task *t, int raw_fd)
1371 int flags = fcntl (raw_fd, F_GETFD);
1373 if ((flags == -1) && (errno == EBADF))
1375 LOG (GNUNET_ERROR_TYPE_ERROR,
1376 "Got invalid file descriptor %d!\n",
1387 * Schedule a new task to be run with a specified delay or when any of
1388 * the specified file descriptor sets is ready. The delay can be used
1389 * as a timeout on the socket(s) being ready. The task will be
1390 * scheduled for execution once either the delay has expired or any of
1391 * the socket operations is ready. This is the most general
1392 * function of the "add" family. Note that the "prerequisite_task"
1393 * must be satisfied in addition to any of the other conditions. In
1394 * other words, the task will be started when
1396 * (prerequisite-run)
1402 * @param delay how long should we wait?
1403 * @param priority priority to use
1404 * @param rfd file descriptor we want to read (can be -1)
1405 * @param wfd file descriptors we want to write (can be -1)
1406 * @param task main function of the task
1407 * @param task_cls closure of @a task
1408 * @return unique task identifier for the job
1409 * only valid until @a task is started!
1411 static struct GNUNET_SCHEDULER_Task *
1412 add_without_sets (struct GNUNET_TIME_Relative delay,
1413 enum GNUNET_SCHEDULER_Priority priority,
1414 const struct GNUNET_NETWORK_Handle *read_nh,
1415 const struct GNUNET_NETWORK_Handle *write_nh,
1416 const struct GNUNET_DISK_FileHandle *read_fh,
1417 const struct GNUNET_DISK_FileHandle *write_fh,
1418 GNUNET_SCHEDULER_TaskCallback task,
1421 struct GNUNET_SCHEDULER_Task *t;
1423 /* scheduler must be running */
1424 GNUNET_assert (NULL != scheduler_driver);
1425 GNUNET_assert (NULL != task);
1426 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1427 GNUNET_async_scope_get (&t->scope);
1438 t->callback_cls = task_cls;
1440 check_fd (t, NULL != read_nh ? GNUNET_NETWORK_get_fd (read_nh) : -1);
1441 check_fd (t, NULL != write_nh ? GNUNET_NETWORK_get_fd (write_nh) : -1);
1442 check_fd (t, NULL != read_fh ? read_fh->fd : -1);
1443 check_fd (t, NULL != write_fh ? write_fh->fd : -1);
1446 t->start_time = GNUNET_TIME_absolute_get ();
1448 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1449 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ?
1450 current_priority : priority);
1451 t->lifeness = current_lifeness;
1452 GNUNET_CONTAINER_DLL_insert (pending_head,
1455 driver_add_multiple (t);
1456 max_priority_added = GNUNET_MAX (max_priority_added,
1464 * Schedule a new task to be run with a specified delay or when the
1465 * specified file descriptor is ready for reading. The delay can be
1466 * used as a timeout on the socket being ready. The task will be
1467 * scheduled for execution once either the delay has expired or the
1468 * socket operation is ready. It will be run with the DEFAULT priority.
1469 * Only allowed to be called as long as the scheduler is running, that
1470 * is one of the following conditions is met:
1472 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1473 * - #GNUNET_SCHEDULER_driver_init has been run and
1474 * #GNUNET_SCHEDULER_driver_done has not been called yet
1476 * @param delay when should this operation time out?
1477 * @param rfd read file-descriptor
1478 * @param task main function of the task
1479 * @param task_cls closure of @a task
1480 * @return unique task identifier for the job
1481 * only valid until @a task is started!
1483 struct GNUNET_SCHEDULER_Task *
1484 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1485 struct GNUNET_NETWORK_Handle *rfd,
1486 GNUNET_SCHEDULER_TaskCallback task,
1489 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1490 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1491 rfd, task, task_cls);
1496 * Schedule a new task to be run with a specified priority and to be
1497 * run after the specified delay or when the specified file descriptor
1498 * is ready for reading. The delay can be used as a timeout on the
1499 * socket being ready. The task will be scheduled for execution once
1500 * either the delay has expired or the socket operation is ready. It
1501 * will be run with the DEFAULT priority.
1502 * Only allowed to be called as long as the scheduler is running, that
1503 * is one of the following conditions is met:
1505 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1506 * - #GNUNET_SCHEDULER_driver_init has been run and
1507 * #GNUNET_SCHEDULER_driver_done has not been called yet
1509 * @param delay when should this operation time out?
1510 * @param priority priority to use for the task
1511 * @param rfd read file-descriptor
1512 * @param task main function of the task
1513 * @param task_cls closure of @a task
1514 * @return unique task identifier for the job
1515 * only valid until @a task is started!
1517 struct GNUNET_SCHEDULER_Task *
1518 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1519 enum GNUNET_SCHEDULER_Priority
1521 struct GNUNET_NETWORK_Handle *rfd,
1522 GNUNET_SCHEDULER_TaskCallback task,
1525 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1534 * Schedule a new task to be run with a specified delay or when the
1535 * specified file descriptor is ready for writing. The delay can be
1536 * used as a timeout on the socket being ready. The task will be
1537 * scheduled for execution once either the delay has expired or the
1538 * socket operation is ready. It will be run with the priority of
1540 * Only allowed to be called as long as the scheduler is running, that
1541 * is one of the following conditions is met:
1543 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1544 * - #GNUNET_SCHEDULER_driver_init has been run and
1545 * #GNUNET_SCHEDULER_driver_done has not been called yet
1547 * @param delay when should this operation time out?
1548 * @param wfd write file-descriptor
1549 * @param task main function of the task
1550 * @param task_cls closure of @a task
1551 * @return unique task identifier for the job
1552 * only valid until @a task is started!
1554 struct GNUNET_SCHEDULER_Task *
1555 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1556 struct GNUNET_NETWORK_Handle *wfd,
1557 GNUNET_SCHEDULER_TaskCallback task,
1560 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1561 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1563 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, that
1574 * is one of the following conditions is met:
1576 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1577 * - #GNUNET_SCHEDULER_driver_init has been run and
1578 * #GNUNET_SCHEDULER_driver_done has not been called yet
1580 * @param delay when should this operation time out?
1581 * @param priority priority of the task
1582 * @param fd file-descriptor
1583 * @param on_read whether to poll the file-descriptor for readability
1584 * @param on_write whether to poll the file-descriptor for writability
1585 * @param task main function of the task
1586 * @param task_cls closure of task
1587 * @return unique task identifier for the job
1588 * only valid until "task" is started!
1590 struct GNUNET_SCHEDULER_Task *
1591 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1592 enum GNUNET_SCHEDULER_Priority priority,
1593 struct GNUNET_NETWORK_Handle *fd,
1596 GNUNET_SCHEDULER_TaskCallback task,
1599 /* scheduler must be running */
1600 GNUNET_assert (NULL != scheduler_driver);
1601 GNUNET_assert (on_read || on_write);
1602 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1603 return add_without_sets (delay, priority,
1604 on_read ? fd : NULL,
1605 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,
1638 return GNUNET_SCHEDULER_add_file_with_priority (
1639 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1640 rfd, GNUNET_YES, GNUNET_NO,
1646 * Schedule a new task to be run with a specified delay or when the
1647 * specified file descriptor is ready for writing. The delay can be
1648 * used as a timeout on the socket being ready. The task will be
1649 * scheduled for execution once either the delay has expired or the
1650 * socket operation is ready. It will be run with the DEFAULT priority.
1651 * Only allowed to be called as long as the scheduler is running, that
1652 * is one of the following conditions is met:
1654 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1655 * - #GNUNET_SCHEDULER_driver_init has been run and
1656 * #GNUNET_SCHEDULER_driver_done has not been called yet
1658 * @param delay when should this operation time out?
1659 * @param wfd write file-descriptor
1660 * @param task main function of the task
1661 * @param task_cls closure of @a task
1662 * @return unique task identifier for the job
1663 * only valid until @a task is started!
1665 struct GNUNET_SCHEDULER_Task *
1666 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1667 const struct GNUNET_DISK_FileHandle *wfd,
1668 GNUNET_SCHEDULER_TaskCallback task,
1671 return GNUNET_SCHEDULER_add_file_with_priority (
1672 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1673 wfd, GNUNET_NO, GNUNET_YES,
1679 * Schedule a new task to be run with a specified delay or when the
1680 * specified file descriptor is ready. The delay can be
1681 * used as a timeout on the socket being ready. The task will be
1682 * scheduled for execution once either the delay has expired or the
1683 * socket operation is ready.
1684 * Only allowed to be called as long as the scheduler is running, that
1685 * is one of the following conditions is met:
1687 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1688 * - #GNUNET_SCHEDULER_driver_init has been run and
1689 * #GNUNET_SCHEDULER_driver_done has not been called yet
1691 * @param delay when should this operation time out?
1692 * @param priority priority of the task
1693 * @param fd file-descriptor
1694 * @param on_read whether to poll the file-descriptor for readability
1695 * @param on_write whether to poll the file-descriptor for writability
1696 * @param task main function of the task
1697 * @param task_cls closure of @a task
1698 * @return unique task identifier for the job
1699 * only valid until @a task is started!
1701 struct GNUNET_SCHEDULER_Task *
1702 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1703 enum GNUNET_SCHEDULER_Priority
1706 GNUNET_DISK_FileHandle *fd,
1707 int on_read, int on_write,
1708 GNUNET_SCHEDULER_TaskCallback task,
1711 /* scheduler must be running */
1712 GNUNET_assert (NULL != scheduler_driver);
1713 GNUNET_assert (on_read || on_write);
1714 GNUNET_assert (fd->fd >= 0);
1715 return add_without_sets (delay, priority,
1718 on_read ? fd : NULL,
1719 on_write ? fd : NULL,
1725 extract_handles (const struct GNUNET_NETWORK_FDSet *fdset,
1726 const struct GNUNET_NETWORK_Handle ***ntarget,
1727 unsigned int *extracted_nhandles,
1728 const struct GNUNET_DISK_FileHandle ***ftarget,
1729 unsigned int *extracted_fhandles)
1731 // FIXME: this implementation only works for unix, for WIN32 the file handles
1732 // in fdset must be handled separately
1733 const struct GNUNET_NETWORK_Handle **nhandles;
1734 const struct GNUNET_DISK_FileHandle **fhandles;
1735 unsigned int nhandles_len;
1736 unsigned int fhandles_len;
1742 for (int sock = 0; sock != fdset->nsds; ++sock)
1744 if (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (fdset, sock))
1746 struct GNUNET_NETWORK_Handle *nhandle;
1747 struct GNUNET_DISK_FileHandle *fhandle;
1749 nhandle = GNUNET_NETWORK_socket_box_native (sock);
1750 if (NULL != nhandle)
1752 GNUNET_array_append (nhandles, nhandles_len, nhandle);
1756 fhandle = GNUNET_DISK_get_handle_from_int_fd (sock);
1757 if (NULL != fhandle)
1759 GNUNET_array_append (fhandles, fhandles_len, fhandle);
1768 *ntarget = nhandles_len > 0 ? nhandles : NULL;
1769 *ftarget = fhandles_len > 0 ? fhandles : NULL;
1770 *extracted_nhandles = nhandles_len;
1771 *extracted_fhandles = fhandles_len;
1776 * Schedule a new task to be run with a specified delay or when any of
1777 * the specified file descriptor sets is ready. The delay can be used
1778 * as a timeout on the socket(s) being ready. The task will be
1779 * scheduled for execution once either the delay has expired or any of
1780 * the socket operations is ready. This is the most general
1781 * function of the "add" family. Note that the "prerequisite_task"
1782 * must be satisfied in addition to any of the other conditions. In
1783 * other words, the task will be started when
1785 * (prerequisite-run)
1788 * || any-ws-ready) )
1790 * Only allowed to be called as long as the scheduler is running, that
1791 * is one of the following conditions is met:
1793 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1794 * - #GNUNET_SCHEDULER_driver_init has been run and
1795 * #GNUNET_SCHEDULER_driver_done has not been called yet
1797 * @param prio how important is this task?
1798 * @param delay how long should we wait?
1799 * @param rs set of file descriptors we want to read (can be NULL)
1800 * @param ws set of file descriptors we want to write (can be NULL)
1801 * @param task main function of the task
1802 * @param task_cls closure of @a task
1803 * @return unique task identifier for the job
1804 * only valid until @a task is started!
1806 struct GNUNET_SCHEDULER_Task *
1807 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1808 struct GNUNET_TIME_Relative delay,
1809 const struct GNUNET_NETWORK_FDSet *rs,
1810 const struct GNUNET_NETWORK_FDSet *ws,
1811 GNUNET_SCHEDULER_TaskCallback task,
1814 struct GNUNET_SCHEDULER_Task *t;
1815 const struct GNUNET_NETWORK_Handle **read_nhandles = NULL;
1816 const struct GNUNET_NETWORK_Handle **write_nhandles = NULL;
1817 const struct GNUNET_DISK_FileHandle **read_fhandles = NULL;
1818 const struct GNUNET_DISK_FileHandle **write_fhandles = NULL;
1819 unsigned int read_nhandles_len = 0;
1820 unsigned int write_nhandles_len = 0;
1821 unsigned int read_fhandles_len = 0;
1822 unsigned int write_fhandles_len = 0;
1824 /* scheduler must be running */
1825 GNUNET_assert (NULL != scheduler_driver);
1826 GNUNET_assert (NULL != task);
1827 int no_rs = (NULL == rs);
1828 int no_ws = (NULL == ws);
1829 int empty_rs = (NULL != rs) && (0 == rs->nsds);
1830 int empty_ws = (NULL != ws) && (0 == ws->nsds);
1831 int no_fds = (no_rs && no_ws) ||
1832 (empty_rs && empty_ws) ||
1833 (no_rs && empty_ws) ||
1834 (no_ws && empty_rs);
1839 extract_handles (rs,
1843 &read_fhandles_len);
1847 extract_handles (ws,
1849 &write_nhandles_len,
1851 &write_fhandles_len);
1855 * here we consider the case that a GNUNET_NETWORK_FDSet might be empty
1856 * although its maximum FD number (nsds) is greater than 0. We handle
1857 * this case gracefully because some libraries such as libmicrohttpd
1858 * only provide a hint what the maximum FD number in an FD set might be
1859 * and not the exact FD number (see e.g. gnunet-rest-service.c)
1861 int no_fds_extracted = (0 == read_nhandles_len) &&
1862 (0 == read_fhandles_len) &&
1863 (0 == write_nhandles_len) &&
1864 (0 == write_fhandles_len);
1865 if (no_fds || no_fds_extracted)
1866 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1870 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1871 GNUNET_async_scope_get (&t->scope);
1880 write_fhandles_len);
1882 t->callback_cls = task_cls;
1883 t->own_handles = GNUNET_YES;
1884 /* free the arrays of pointers to network / file handles, the actual
1885 * handles will be freed in destroy_task */
1886 GNUNET_array_grow (read_nhandles, read_nhandles_len, 0);
1887 GNUNET_array_grow (write_nhandles, write_nhandles_len, 0);
1888 GNUNET_array_grow (read_fhandles, read_fhandles_len, 0);
1889 GNUNET_array_grow (write_fhandles, write_fhandles_len, 0);
1891 t->start_time = GNUNET_TIME_absolute_get ();
1893 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1895 check_priority ((prio ==
1896 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1898 t->lifeness = current_lifeness;
1899 GNUNET_CONTAINER_DLL_insert (pending_head,
1902 driver_add_multiple (t);
1903 max_priority_added = GNUNET_MAX (max_priority_added,
1905 LOG (GNUNET_ERROR_TYPE_DEBUG,
1914 * Function used by event-loop implementations to signal the scheduler
1915 * that a particular @a task is ready due to an event specified in the
1916 * et field of @a fdi.
1918 * This function will then queue the task to notify the application
1919 * that the task is ready (with the respective priority).
1921 * @param task the task that is ready
1922 * @param fdi information about the related FD
1925 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1926 struct GNUNET_SCHEDULER_FdInfo *fdi)
1928 enum GNUNET_SCHEDULER_Reason reason;
1930 reason = task->reason;
1931 if ((0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1932 (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et)))
1933 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
1934 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
1935 (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et)))
1936 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
1937 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1938 task->reason = reason;
1939 if (GNUNET_NO == task->in_ready_list)
1941 GNUNET_CONTAINER_DLL_remove (pending_head,
1944 queue_ready_task (task);
1950 * Function called by external event loop implementations to tell the
1951 * scheduler to run some of the tasks that are ready. Must be called
1952 * only after #GNUNET_SCHEDULER_driver_init has been called and before
1953 * #GNUNET_SCHEDULER_driver_done is called.
1954 * This function may return even though there are tasks left to run
1955 * just to give other tasks a chance as well. If we return #GNUNET_YES,
1956 * the event loop implementation should call this function again as
1957 * soon as possible, while if we return #GNUNET_NO it must block until
1958 * either the operating system has more work (the scheduler has no more
1959 * work to do right now) or the timeout set by the scheduler (using the
1960 * set_wakeup callback) is reached.
1962 * @param sh scheduler handle that was returned by
1963 * #GNUNET_SCHEDULER_driver_init
1964 * @return #GNUNET_YES if there are more tasks that are ready,
1965 * and thus we would like to run more (yield to avoid
1966 * blocking other activities for too long) #GNUNET_NO
1967 * if we are done running tasks (yield to block)
1970 GNUNET_SCHEDULER_do_work (struct GNUNET_SCHEDULER_Handle *sh)
1972 enum GNUNET_SCHEDULER_Priority p;
1973 struct GNUNET_SCHEDULER_Task *pos;
1974 struct GNUNET_TIME_Absolute now;
1976 /* check for tasks that reached the timeout! */
1977 now = GNUNET_TIME_absolute_get ();
1978 pos = pending_timeout_head;
1981 struct GNUNET_SCHEDULER_Task *next = pos->next;
1982 if (now.abs_value_us >= pos->timeout.abs_value_us)
1983 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1984 if (0 == pos->reason)
1986 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1987 pending_timeout_tail,
1989 if (pending_timeout_last == pos)
1990 pending_timeout_last = NULL;
1991 queue_ready_task (pos);
1997 struct GNUNET_SCHEDULER_Task *next = pos->next;
1998 if (now.abs_value_us >= pos->timeout.abs_value_us)
2000 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
2001 GNUNET_CONTAINER_DLL_remove (pending_head,
2004 queue_ready_task (pos);
2009 if (0 == ready_count)
2011 struct GNUNET_TIME_Absolute timeout = get_timeout ();
2013 if (timeout.abs_value_us > now.abs_value_us)
2016 * The event loop called this function before the current timeout was
2017 * reached (and no FD tasks are ready). This is acceptable if
2019 * - the system time was changed while the driver was waiting for
2021 * - an external event loop called GNUnet API functions outside of
2022 * the callbacks called in GNUNET_SCHEDULER_do_work and thus
2023 * wasn't notified about the new timeout
2025 * It might also mean we are busy-waiting because of a programming
2026 * error in the external event loop.
2028 LOG (GNUNET_ERROR_TYPE_DEBUG,
2029 "GNUNET_SCHEDULER_do_work did not find any ready "
2030 "tasks and timeout has not been reached yet.\n");
2035 * the current timeout was reached but no ready tasks were found,
2036 * internal scheduler error!
2043 /* find out which task priority level we are going to
2044 process this time */
2045 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
2046 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
2047 /* yes, p>0 is correct, 0 is "KEEP" which should
2048 * always be an empty queue (see assertion)! */
2049 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
2051 pos = ready_head[p];
2055 GNUNET_assert (NULL != pos); /* ready_count wrong? */
2057 /* process all tasks at this priority level, then yield */
2058 while (NULL != (pos = ready_head[p]))
2060 GNUNET_CONTAINER_DLL_remove (ready_head[p],
2064 current_priority = pos->priority;
2065 current_lifeness = pos->lifeness;
2068 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
2069 DELAY_THRESHOLD.rel_value_us)
2071 LOG (GNUNET_ERROR_TYPE_DEBUG,
2072 "Task %p took %s to be scheduled\n",
2074 GNUNET_STRINGS_relative_time_to_string (
2075 GNUNET_TIME_absolute_get_duration (pos->start_time),
2079 tc.reason = pos->reason;
2080 GNUNET_NETWORK_fdset_zero (sh->rs);
2081 GNUNET_NETWORK_fdset_zero (sh->ws);
2082 // FIXME: do we have to remove FdInfos from fds if they are not ready?
2083 tc.fds_len = pos->fds_len;
2085 for (unsigned int i = 0; i != pos->fds_len; ++i)
2087 struct GNUNET_SCHEDULER_FdInfo *fdi = &pos->fds[i];
2088 if (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et))
2090 GNUNET_NETWORK_fdset_set_native (sh->rs,
2093 if (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et))
2095 GNUNET_NETWORK_fdset_set_native (sh->ws,
2099 tc.read_ready = sh->rs;
2100 tc.write_ready = sh->ws;
2101 LOG (GNUNET_ERROR_TYPE_DEBUG,
2102 "Running task %p\n",
2104 GNUNET_assert (NULL != pos->callback);
2106 struct GNUNET_AsyncScopeSave old_scope;
2107 if (pos->scope.have_scope)
2108 GNUNET_async_scope_enter (&pos->scope.scope_id, &old_scope);
2110 GNUNET_async_scope_get (&old_scope);
2111 pos->callback (pos->callback_cls);
2112 GNUNET_async_scope_restore (&old_scope);
2114 if (NULL != pos->fds)
2116 int del_result = scheduler_driver->del (scheduler_driver->cls, pos);
2117 if (GNUNET_OK != del_result)
2119 LOG (GNUNET_ERROR_TYPE_ERROR,
2120 "driver could not delete task %p\n", pos);
2125 dump_backtrace (pos);
2129 shutdown_if_no_lifeness ();
2130 if (0 == ready_count)
2132 scheduler_driver->set_wakeup (scheduler_driver->cls,
2136 scheduler_driver->set_wakeup (scheduler_driver->cls,
2137 GNUNET_TIME_absolute_get ());
2143 * Function called by external event loop implementations to initialize
2144 * the scheduler. An external implementation has to provide @a driver
2145 * which contains callbacks for the scheduler (see definition of struct
2146 * #GNUNET_SCHEDULER_Driver). The callbacks are used to instruct the
2147 * external implementation to watch for events. If it detects any of
2148 * those events it is expected to call #GNUNET_SCHEDULER_do_work to let
2149 * the scheduler handle it. If an event is related to a specific task
2150 * (e.g. the scheduler gave instructions to watch a file descriptor),
2151 * the external implementation is expected to mark that task ready
2152 * before by calling #GNUNET_SCHEDULER_task_ready.
2154 * This function has to be called before any tasks are scheduled and
2155 * before GNUNET_SCHEDULER_do_work is called for the first time. It
2156 * allocates resources that have to be freed again by calling
2157 * #GNUNET_SCHEDULER_driver_done.
2159 * This function installs the same signal handlers as
2160 * #GNUNET_SCHEDULER_run. This means SIGTERM (and other similar signals)
2161 * will induce a call to #GNUNET_SCHEDULER_shutdown during the next
2162 * call to #GNUNET_SCHEDULER_do_work. As a result, SIGTERM causes all
2163 * active tasks to be scheduled with reason
2164 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added afterwards
2165 * will execute normally!). Note that any particular signal will only
2166 * shut down one scheduler; applications should always only create a
2169 * @param driver to use for the event loop
2170 * @return handle to be passed to #GNUNET_SCHEDULER_do_work and
2171 * #GNUNET_SCHEDULER_driver_done
2173 struct GNUNET_SCHEDULER_Handle *
2174 GNUNET_SCHEDULER_driver_init (const struct GNUNET_SCHEDULER_Driver *driver)
2176 struct GNUNET_SCHEDULER_Handle *sh;
2177 const struct GNUNET_DISK_FileHandle *pr;
2179 /* scheduler must not be running */
2180 GNUNET_assert (NULL == scheduler_driver);
2181 GNUNET_assert (NULL == shutdown_pipe_handle);
2182 /* general set-up */
2183 sh = GNUNET_new (struct GNUNET_SCHEDULER_Handle);
2184 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
2188 GNUNET_assert (NULL != shutdown_pipe_handle);
2189 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
2190 GNUNET_DISK_PIPE_END_READ);
2192 scheduler_driver = driver;
2194 /* install signal handlers */
2195 LOG (GNUNET_ERROR_TYPE_DEBUG,
2196 "Registering signal handlers\n");
2197 sh->shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2198 &sighandler_shutdown);
2199 sh->shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2200 &sighandler_shutdown);
2201 #if (SIGTERM != GNUNET_TERM_SIG)
2202 sh->shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2203 &sighandler_shutdown);
2205 sh->shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2207 sh->shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2208 &sighandler_shutdown);
2209 sh->shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2210 &sighandler_shutdown);
2212 /* Setup initial tasks */
2213 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2214 current_lifeness = GNUNET_NO;
2215 install_parent_control_task =
2216 GNUNET_SCHEDULER_add_now (&install_parent_control_handler,
2218 shutdown_pipe_task =
2219 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2223 current_lifeness = GNUNET_YES;
2224 scheduler_driver->set_wakeup (scheduler_driver->cls,
2226 /* begin main event loop */
2227 sh->rs = GNUNET_NETWORK_fdset_create ();
2228 sh->ws = GNUNET_NETWORK_fdset_create ();
2229 GNUNET_NETWORK_fdset_handle_set (sh->rs, pr);
2235 * Counter-part of #GNUNET_SCHEDULER_driver_init. Has to be called
2236 * by external event loop implementations after the scheduler has
2237 * shut down. This is the case if both of the following conditions
2240 * - all tasks the scheduler has added through the driver's add
2241 * callback have been removed again through the driver's del
2243 * - the timeout the scheduler has set through the driver's
2244 * add_wakeup callback is FOREVER
2246 * @param sh the handle returned by #GNUNET_SCHEDULER_driver_init
2249 GNUNET_SCHEDULER_driver_done (struct GNUNET_SCHEDULER_Handle *sh)
2251 GNUNET_assert (NULL == pending_head);
2252 GNUNET_assert (NULL == pending_timeout_head);
2253 GNUNET_assert (NULL == shutdown_head);
2254 for (int i = 0; i != GNUNET_SCHEDULER_PRIORITY_COUNT; ++i)
2256 GNUNET_assert (NULL == ready_head[i]);
2258 GNUNET_NETWORK_fdset_destroy (sh->rs);
2259 GNUNET_NETWORK_fdset_destroy (sh->ws);
2261 /* uninstall signal handlers */
2262 GNUNET_SIGNAL_handler_uninstall (sh->shc_int);
2263 GNUNET_SIGNAL_handler_uninstall (sh->shc_term);
2264 #if (SIGTERM != GNUNET_TERM_SIG)
2265 GNUNET_SIGNAL_handler_uninstall (sh->shc_gterm);
2267 GNUNET_SIGNAL_handler_uninstall (sh->shc_pipe);
2268 GNUNET_SIGNAL_handler_uninstall (sh->shc_quit);
2269 GNUNET_SIGNAL_handler_uninstall (sh->shc_hup);
2270 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2271 shutdown_pipe_handle = NULL;
2272 scheduler_driver = NULL;
2278 select_loop (struct GNUNET_SCHEDULER_Handle *sh,
2279 struct DriverContext *context)
2281 struct GNUNET_NETWORK_FDSet *rs;
2282 struct GNUNET_NETWORK_FDSet *ws;
2285 GNUNET_assert (NULL != context);
2286 rs = GNUNET_NETWORK_fdset_create ();
2287 ws = GNUNET_NETWORK_fdset_create ();
2288 while ((NULL != context->scheduled_head) ||
2289 (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us !=
2290 context->timeout.abs_value_us))
2292 LOG (GNUNET_ERROR_TYPE_DEBUG,
2293 "select timeout = %s\n",
2294 GNUNET_STRINGS_absolute_time_to_string (context->timeout));
2296 GNUNET_NETWORK_fdset_zero (rs);
2297 GNUNET_NETWORK_fdset_zero (ws);
2299 for (struct Scheduled *pos = context->scheduled_head;
2303 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et))
2305 GNUNET_NETWORK_fdset_set_native (rs, pos->fdi->sock);
2307 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et))
2309 GNUNET_NETWORK_fdset_set_native (ws, pos->fdi->sock);
2312 struct GNUNET_TIME_Relative time_remaining =
2313 GNUNET_TIME_absolute_get_remaining (context->timeout);
2314 if (NULL == scheduler_select)
2316 select_result = GNUNET_NETWORK_socket_select (rs,
2323 select_result = scheduler_select (scheduler_select_cls,
2329 if (select_result == GNUNET_SYSERR)
2334 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
2345 if (0 != system (lsof))
2346 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
2350 for (struct Scheduled *s = context->scheduled_head;
2354 int flags = fcntl (s->fdi->sock,
2357 if ((flags == -1) &&
2360 LOG (GNUNET_ERROR_TYPE_ERROR,
2361 "Got invalid file descriptor %d!\n",
2364 dump_backtrace (s->task);
2370 GNUNET_NETWORK_fdset_destroy (rs);
2371 GNUNET_NETWORK_fdset_destroy (ws);
2372 return GNUNET_SYSERR;
2374 if (select_result > 0)
2376 for (struct Scheduled *pos = context->scheduled_head;
2380 int is_ready = GNUNET_NO;
2382 if ((0 != (GNUNET_SCHEDULER_ET_IN & pos->et)) &&
2384 GNUNET_NETWORK_fdset_test_native (rs,
2387 pos->fdi->et |= GNUNET_SCHEDULER_ET_IN;
2388 is_ready = GNUNET_YES;
2390 if ((0 != (GNUNET_SCHEDULER_ET_OUT & pos->et)) &&
2392 GNUNET_NETWORK_fdset_test_native (ws,
2395 pos->fdi->et |= GNUNET_SCHEDULER_ET_OUT;
2396 is_ready = GNUNET_YES;
2398 if (GNUNET_YES == is_ready)
2400 GNUNET_SCHEDULER_task_ready (pos->task,
2405 if (GNUNET_YES == GNUNET_SCHEDULER_do_work (sh))
2407 LOG (GNUNET_ERROR_TYPE_DEBUG,
2408 "scheduler has more tasks ready!\n");
2411 GNUNET_NETWORK_fdset_destroy (rs);
2412 GNUNET_NETWORK_fdset_destroy (ws);
2418 select_add (void *cls,
2419 struct GNUNET_SCHEDULER_Task *task,
2420 struct GNUNET_SCHEDULER_FdInfo *fdi)
2422 struct DriverContext *context = cls;
2424 GNUNET_assert (NULL != context);
2425 GNUNET_assert (NULL != task);
2426 GNUNET_assert (NULL != fdi);
2427 GNUNET_assert (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et) ||
2428 0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et));
2430 if (! ((NULL != fdi->fd) ^ (NULL != fdi->fh)) || (fdi->sock < 0))
2432 /* exactly one out of {fd, hf} must be != NULL and the OS handle must be valid */
2433 return GNUNET_SYSERR;
2436 struct Scheduled *scheduled = GNUNET_new (struct Scheduled);
2437 scheduled->task = task;
2438 scheduled->fdi = fdi;
2439 scheduled->et = fdi->et;
2441 GNUNET_CONTAINER_DLL_insert (context->scheduled_head,
2442 context->scheduled_tail,
2449 select_del (void *cls,
2450 struct GNUNET_SCHEDULER_Task *task)
2452 struct DriverContext *context;
2453 struct Scheduled *pos;
2456 GNUNET_assert (NULL != cls);
2459 ret = GNUNET_SYSERR;
2460 pos = context->scheduled_head;
2463 struct Scheduled *next = pos->next;
2464 if (pos->task == task)
2466 GNUNET_CONTAINER_DLL_remove (context->scheduled_head,
2467 context->scheduled_tail,
2479 select_set_wakeup (void *cls,
2480 struct GNUNET_TIME_Absolute dt)
2482 struct DriverContext *context = cls;
2484 GNUNET_assert (NULL != context);
2485 context->timeout = dt;
2490 * Obtain the driver for using select() as the event loop.
2492 * @return NULL on error
2494 struct GNUNET_SCHEDULER_Driver *
2495 GNUNET_SCHEDULER_driver_select ()
2497 struct GNUNET_SCHEDULER_Driver *select_driver;
2499 select_driver = GNUNET_new (struct GNUNET_SCHEDULER_Driver);
2501 select_driver->add = &select_add;
2502 select_driver->del = &select_del;
2503 select_driver->set_wakeup = &select_set_wakeup;
2505 return select_driver;
2510 * Change the async scope for the currently executing task and (transitively)
2511 * for all tasks scheduled by the current task after calling this function.
2512 * Nested tasks can begin their own nested async scope.
2514 * Once the current task is finished, the async scope ID is reset to
2515 * its previous value.
2517 * Must only be called from a running task.
2519 * @param aid the asynchronous scope id to enter
2522 GNUNET_SCHEDULER_begin_async_scope (struct GNUNET_AsyncScopeId *aid)
2524 struct GNUNET_AsyncScopeSave dummy_old_scope;
2526 GNUNET_assert (NULL != active_task);
2527 /* Since we're in a task, the context will be automatically
2528 restored by the scheduler. */
2529 GNUNET_async_scope_enter (aid, &dummy_old_scope);
2533 /* end of scheduler.c */