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 * Priority used currently in #GNUNET_SCHEDULER_do_work().
412 static enum GNUNET_SCHEDULER_Priority work_priority;
415 * Function to use as a select() in the scheduler.
416 * If NULL, we use GNUNET_NETWORK_socket_select().
418 static GNUNET_SCHEDULER_select scheduler_select;
421 * Task context of the current task.
423 static struct GNUNET_SCHEDULER_TaskContext tc;
426 * Closure for #scheduler_select.
428 static void *scheduler_select_cls;
432 * Sets the select function to use in the scheduler (scheduler_select).
434 * @param new_select new select function to use
435 * @param new_select_cls closure for @a new_select
436 * @return previously used select function, NULL for default
439 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
440 void *new_select_cls)
442 scheduler_select = new_select;
443 scheduler_select_cls = new_select_cls;
448 * Check that the given priority is legal (and return it).
450 * @param p priority value to check
451 * @return p on success, 0 on error
453 static enum GNUNET_SCHEDULER_Priority
454 check_priority (enum GNUNET_SCHEDULER_Priority p)
456 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
459 return 0; /* make compiler happy */
464 * chooses the nearest timeout from all pending tasks, to be used
465 * to tell the driver the next wakeup time (using its set_wakeup
468 struct GNUNET_TIME_Absolute
471 struct GNUNET_SCHEDULER_Task *pos;
472 struct GNUNET_TIME_Absolute now;
473 struct GNUNET_TIME_Absolute timeout;
475 pos = pending_timeout_head;
476 now = GNUNET_TIME_absolute_get ();
477 timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
480 if (0 != pos->reason)
486 timeout = pos->timeout;
489 for (pos = pending_head; NULL != pos; pos = pos->next)
491 if (0 != pos->reason)
495 else if ((pos->timeout.abs_value_us !=
496 GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) &&
497 (timeout.abs_value_us > pos->timeout.abs_value_us))
499 timeout = pos->timeout;
507 * Put a task that is ready for execution into the ready queue.
509 * @param task task ready for execution
512 queue_ready_task (struct GNUNET_SCHEDULER_Task *task)
514 enum GNUNET_SCHEDULER_Priority p = check_priority (task->priority);
516 GNUNET_CONTAINER_DLL_insert_tail (ready_head[p],
519 task->in_ready_list = GNUNET_YES;
525 * Request the shutdown of a scheduler. Marks all tasks
526 * awaiting shutdown as ready. Note that tasks
527 * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call
528 * will be delayed until the next shutdown signal.
531 GNUNET_SCHEDULER_shutdown ()
533 struct GNUNET_SCHEDULER_Task *pos;
535 LOG (GNUNET_ERROR_TYPE_DEBUG,
536 "GNUNET_SCHEDULER_shutdown\n");
537 if (NULL != install_parent_control_task)
539 GNUNET_SCHEDULER_cancel (install_parent_control_task);
540 install_parent_control_task = NULL;
542 if (NULL != shutdown_pipe_task)
544 GNUNET_SCHEDULER_cancel (shutdown_pipe_task);
545 shutdown_pipe_task = NULL;
547 while (NULL != (pos = shutdown_head))
549 GNUNET_CONTAINER_DLL_remove (shutdown_head,
552 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
553 queue_ready_task (pos);
559 * Output stack trace of task @a t.
561 * @param t task to dump stack trace of
564 dump_backtrace (struct GNUNET_SCHEDULER_Task *t)
567 for (unsigned int i = 0; i < t->num_backtrace_strings; i++)
568 LOG (GNUNET_ERROR_TYPE_WARNING,
569 "Task %p trace %u: %s\n",
572 t->backtrace_strings[i]);
580 * Destroy a task (release associated resources)
582 * @param t task to destroy
585 destroy_task (struct GNUNET_SCHEDULER_Task *t)
589 LOG (GNUNET_ERROR_TYPE_DEBUG,
590 "destroying task %p\n",
593 if (GNUNET_YES == t->own_handles)
595 for (i = 0; i != t->fds_len; ++i)
597 const struct GNUNET_NETWORK_Handle *fd = t->fds[i].fd;
598 const struct GNUNET_DISK_FileHandle *fh = t->fds[i].fh;
601 GNUNET_NETWORK_socket_free_memory_only_ ((struct
602 GNUNET_NETWORK_Handle *) fd);
606 // FIXME: on WIN32 this is not enough! A function
607 // GNUNET_DISK_file_free_memory_only would be nice
608 GNUNET_free ((void *) fh);
614 GNUNET_array_grow (t->fds, t->fds_len, 0);
617 GNUNET_free (t->backtrace_strings);
624 * Pipe used to communicate shutdown via signal.
626 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
629 * Process ID of this process at the time we installed the various
635 * Signal handler called for SIGPIPE.
645 // * Wait for a short time.
646 // * Sleeps for @a ms ms (as that should be long enough for virtually all
647 // * modern systems to context switch and allow another process to do
648 // * some 'real' work).
650 // * @param ms how many ms to wait
653 // short_wait (unsigned int ms)
655 // struct GNUNET_TIME_Relative timeout;
657 // timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
658 // (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
663 * Signal handler called for signals that should cause us to shutdown.
666 sighandler_shutdown ()
669 int old_errno = errno; /* backup errno */
671 if (getpid () != my_pid)
672 _exit (1); /* we have fork'ed since the signal handler was created,
673 * ignore the signal, see https://gnunet.org/vfork discussion */
674 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
675 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
682 shutdown_if_no_lifeness ()
684 struct GNUNET_SCHEDULER_Task *t;
688 for (t = pending_head; NULL != t; t = t->next)
689 if (GNUNET_YES == t->lifeness)
691 for (t = shutdown_head; NULL != t; t = t->next)
692 if (GNUNET_YES == t->lifeness)
694 for (t = pending_timeout_head; NULL != t; t = t->next)
695 if (GNUNET_YES == t->lifeness)
698 GNUNET_SCHEDULER_shutdown ();
703 select_loop (struct GNUNET_SCHEDULER_Handle *sh,
704 struct DriverContext *context);
708 * Initialize and run scheduler. This function will return when all
709 * tasks have completed. On systems with signals, receiving a SIGTERM
710 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown()
711 * to be run after the active task is complete. As a result, SIGTERM
712 * causes all active tasks to be scheduled with reason
713 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
714 * afterwards will execute normally!). Note that any particular signal
715 * will only shut down one scheduler; applications should always only
716 * create a single scheduler.
718 * @param task task to run immediately
719 * @param task_cls closure of @a task
722 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
725 struct GNUNET_SCHEDULER_Handle *sh;
726 struct GNUNET_SCHEDULER_Driver *driver;
727 struct DriverContext context = {
728 .scheduled_head = NULL,
729 .scheduled_tail = NULL,
730 .timeout = GNUNET_TIME_absolute_get ()
733 driver = GNUNET_SCHEDULER_driver_select ();
734 driver->cls = &context;
735 sh = GNUNET_SCHEDULER_driver_init (driver);
736 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
738 GNUNET_SCHEDULER_REASON_STARTUP,
739 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
742 GNUNET_SCHEDULER_driver_done (sh);
743 GNUNET_free (driver);
748 * Obtain the task context, giving the reason why the current task was
751 * @return current tasks' scheduler context
753 const struct GNUNET_SCHEDULER_TaskContext *
754 GNUNET_SCHEDULER_get_task_context ()
756 GNUNET_assert (NULL != active_task);
762 * Get information about the current load of this scheduler. Use this
763 * function to determine if an elective task should be added or simply
764 * dropped (if the decision should be made based on the number of
765 * tasks ready to run).
767 * @param p priority level to look at
768 * @return number of tasks pending right now
771 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
775 GNUNET_assert (NULL != active_task);
776 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
778 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
779 p = current_priority;
781 for (struct GNUNET_SCHEDULER_Task *pos = ready_head[check_priority (p)];
790 init_fd_info (struct GNUNET_SCHEDULER_Task *t,
791 const struct GNUNET_NETWORK_Handle *const *read_nh,
792 unsigned int read_nh_len,
793 const struct GNUNET_NETWORK_Handle *const *write_nh,
794 unsigned int write_nh_len,
795 const struct GNUNET_DISK_FileHandle *const *read_fh,
796 unsigned int read_fh_len,
797 const struct GNUNET_DISK_FileHandle *const *write_fh,
798 unsigned int write_fh_len)
800 // FIXME: if we have exactly two network handles / exactly two file handles
801 // and they are equal, we can make one FdInfo with both
802 // GNUNET_SCHEDULER_ET_IN and GNUNET_SCHEDULER_ET_OUT set.
803 struct GNUNET_SCHEDULER_FdInfo *fdi;
805 t->fds_len = read_nh_len + write_nh_len + read_fh_len + write_fh_len;
810 if (1 == read_nh_len)
812 GNUNET_assert (NULL != read_nh);
813 GNUNET_assert (NULL != *read_nh);
815 fdi->et = GNUNET_SCHEDULER_ET_IN;
816 fdi->sock = GNUNET_NETWORK_get_fd (*read_nh);
817 t->read_fd = fdi->sock;
820 else if (1 == write_nh_len)
822 GNUNET_assert (NULL != write_nh);
823 GNUNET_assert (NULL != *write_nh);
825 fdi->et = GNUNET_SCHEDULER_ET_OUT;
826 fdi->sock = GNUNET_NETWORK_get_fd (*write_nh);
828 t->write_fd = fdi->sock;
830 else if (1 == read_fh_len)
832 GNUNET_assert (NULL != read_fh);
833 GNUNET_assert (NULL != *read_fh);
835 fdi->et = GNUNET_SCHEDULER_ET_IN;
836 fdi->sock = (*read_fh)->fd; // FIXME: does not work under WIN32
837 t->read_fd = fdi->sock;
842 GNUNET_assert (NULL != write_fh);
843 GNUNET_assert (NULL != *write_fh);
845 fdi->et = GNUNET_SCHEDULER_ET_OUT;
846 fdi->sock = (*write_fh)->fd; // FIXME: does not work under WIN32
848 t->write_fd = fdi->sock;
853 fdi = GNUNET_new_array (t->fds_len, struct GNUNET_SCHEDULER_FdInfo);
858 for (i = 0; i != read_nh_len; ++i)
860 fdi->fd = read_nh[i];
861 GNUNET_assert (NULL != fdi->fd);
862 fdi->et = GNUNET_SCHEDULER_ET_IN;
863 fdi->sock = GNUNET_NETWORK_get_fd (read_nh[i]);
866 for (i = 0; i != write_nh_len; ++i)
868 fdi->fd = write_nh[i];
869 GNUNET_assert (NULL != fdi->fd);
870 fdi->et = GNUNET_SCHEDULER_ET_OUT;
871 fdi->sock = GNUNET_NETWORK_get_fd (write_nh[i]);
874 for (i = 0; i != read_fh_len; ++i)
876 fdi->fh = read_fh[i];
877 GNUNET_assert (NULL != fdi->fh);
878 fdi->et = GNUNET_SCHEDULER_ET_IN;
879 fdi->sock = (read_fh[i])->fd; // FIXME: does not work under WIN32
882 for (i = 0; i != write_fh_len; ++i)
884 fdi->fh = write_fh[i];
885 GNUNET_assert (NULL != fdi->fh);
886 fdi->et = GNUNET_SCHEDULER_ET_OUT;
887 fdi->sock = (write_fh[i])->fd; // FIXME: does not work under WIN32
895 * calls the given function @a func on each FdInfo related to @a t.
896 * Optionally updates the event type field in each FdInfo after calling
900 * @param driver_func the function to call with each FdInfo contained in
902 * @param if_not_ready only call @a driver_func on FdInfos that are not
904 * @param et the event type to be set in each FdInfo after calling
905 * @a driver_func on it, or -1 if no updating not desired.
908 driver_add_multiple (struct GNUNET_SCHEDULER_Task *t)
910 struct GNUNET_SCHEDULER_FdInfo *fdi;
911 int success = GNUNET_YES;
913 for (unsigned int i = 0; i != t->fds_len; ++i)
916 success = scheduler_driver->add (scheduler_driver->cls,
919 fdi->et = GNUNET_SCHEDULER_ET_NONE;
921 if (GNUNET_YES != success)
923 LOG (GNUNET_ERROR_TYPE_ERROR,
924 "driver could not add task\n");
930 install_parent_control_handler (void *cls)
933 install_parent_control_task = NULL;
934 GNUNET_OS_install_parent_control_handler (NULL);
939 shutdown_pipe_cb (void *cls)
942 const struct GNUNET_DISK_FileHandle *pr;
945 shutdown_pipe_task = NULL;
946 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
947 GNUNET_DISK_PIPE_END_READ);
948 GNUNET_assert (! GNUNET_DISK_handle_invalid (pr));
949 /* consume the signal */
950 GNUNET_DISK_file_read (pr, &c, sizeof(c));
951 /* mark all active tasks as ready due to shutdown */
952 GNUNET_SCHEDULER_shutdown ();
954 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
962 * Cancel the task with the specified identifier.
963 * The task must not yet have run. Only allowed to be called as long as the
964 * scheduler is running, that is one of the following conditions is met:
966 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
967 * - #GNUNET_SCHEDULER_driver_init has been run and
968 * #GNUNET_SCHEDULER_driver_done has not been called yet
970 * @param task id of the task to cancel
971 * @return original closure of the task
974 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
976 enum GNUNET_SCHEDULER_Priority p;
980 LOG (GNUNET_ERROR_TYPE_DEBUG,
981 "canceling task %p\n",
984 /* scheduler must be running */
985 GNUNET_assert (NULL != scheduler_driver);
986 is_fd_task = (NULL != task->fds);
989 int del_result = scheduler_driver->del (scheduler_driver->cls, task);
990 if (GNUNET_OK != del_result)
992 LOG (GNUNET_ERROR_TYPE_ERROR,
993 "driver could not delete task\n");
997 if (! task->in_ready_list)
1001 GNUNET_CONTAINER_DLL_remove (pending_head,
1005 else if (GNUNET_YES == task->on_shutdown)
1007 GNUNET_CONTAINER_DLL_remove (shutdown_head,
1013 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1014 pending_timeout_tail,
1016 if (pending_timeout_last == task)
1017 pending_timeout_last = NULL;
1022 p = check_priority (task->priority);
1023 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1028 ret = task->callback_cls;
1029 destroy_task (task);
1035 * Initialize backtrace data for task @a t
1037 * @param t task to initialize
1040 init_backtrace (struct GNUNET_SCHEDULER_Task *t)
1043 void *backtrace_array[MAX_TRACE_DEPTH];
1045 t->num_backtrace_strings
1046 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1047 t->backtrace_strings =
1048 backtrace_symbols (backtrace_array,
1049 t->num_backtrace_strings);
1058 * Continue the current execution with the given function. This is
1059 * similar to the other "add" functions except that there is no delay
1060 * and the reason code can be specified.
1062 * @param task main function of the task
1063 * @param task_cls closure for @a task
1064 * @param reason reason for task invocation
1065 * @param priority priority to use for the task
1068 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback
1071 enum GNUNET_SCHEDULER_Reason
1073 enum GNUNET_SCHEDULER_Priority
1076 struct GNUNET_SCHEDULER_Task *t;
1078 /* scheduler must be running */
1079 GNUNET_assert (NULL != scheduler_driver);
1080 GNUNET_assert (NULL != task);
1081 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1085 t->callback_cls = task_cls;
1087 t->start_time = GNUNET_TIME_absolute_get ();
1090 t->priority = check_priority (priority);
1091 t->lifeness = current_lifeness;
1092 LOG (GNUNET_ERROR_TYPE_DEBUG,
1093 "Adding continuation task %p\n",
1096 queue_ready_task (t);
1101 * Schedule a new task to be run at the specified time. The task
1102 * will be scheduled for execution at time @a at.
1104 * @param at time when the operation should run
1105 * @param priority priority to use for the task
1106 * @param task main function of the task
1107 * @param task_cls closure of @a task
1108 * @return unique task identifier for the job
1109 * only valid until @a task is started!
1111 struct GNUNET_SCHEDULER_Task *
1112 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1113 enum GNUNET_SCHEDULER_Priority priority,
1114 GNUNET_SCHEDULER_TaskCallback task,
1117 struct GNUNET_SCHEDULER_Task *t;
1118 struct GNUNET_SCHEDULER_Task *pos;
1119 struct GNUNET_SCHEDULER_Task *prev;
1120 struct GNUNET_TIME_Relative left;
1122 /* scheduler must be running */
1123 GNUNET_assert (NULL != scheduler_driver);
1124 GNUNET_assert (NULL != task);
1125 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1126 GNUNET_async_scope_get (&t->scope);
1128 t->callback_cls = task_cls;
1132 t->start_time = GNUNET_TIME_absolute_get ();
1135 t->priority = check_priority (priority);
1136 t->lifeness = current_lifeness;
1139 left = GNUNET_TIME_absolute_get_remaining (at);
1140 if (0 == left.rel_value_us)
1142 queue_ready_task (t);
1143 if (priority > work_priority)
1144 work_priority = priority;
1148 /* try tail first (optimization in case we are
1149 * appending to a long list of tasks with timeouts) */
1150 if ((NULL == pending_timeout_head) ||
1151 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us))
1153 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1154 pending_timeout_tail,
1159 /* first move from heuristic start backwards to before start time */
1160 prev = pending_timeout_last;
1161 while ((NULL != prev) &&
1162 (prev->timeout.abs_value_us > t->timeout.abs_value_us))
1164 /* now, move from heuristic start (or head of list) forward to insertion point */
1166 pos = pending_timeout_head;
1169 while ((NULL != pos) && (pos->timeout.abs_value_us <=
1170 t->timeout.abs_value_us))
1175 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1176 pending_timeout_tail,
1180 /* finally, update heuristic insertion point to last insertion... */
1181 pending_timeout_last = t;
1182 LOG (GNUNET_ERROR_TYPE_DEBUG,
1190 * Schedule a new task to be run with a specified delay. The task
1191 * will be scheduled for execution once the delay has expired.
1193 * @param delay when should this operation time out?
1194 * @param priority priority to use for the task
1195 * @param task main function of the task
1196 * @param task_cls closure of @a task
1197 * @return unique task identifier for the job
1198 * only valid until @a task is started!
1200 struct GNUNET_SCHEDULER_Task *
1201 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1202 enum GNUNET_SCHEDULER_Priority
1204 GNUNET_SCHEDULER_TaskCallback task,
1207 return GNUNET_SCHEDULER_add_at_with_priority (
1208 GNUNET_TIME_relative_to_absolute (delay),
1216 * Schedule a new task to be run with a specified priority.
1218 * @param prio how important is the new task?
1219 * @param task main function of the task
1220 * @param task_cls closure of @a task
1221 * @return unique task identifier for the job
1222 * only valid until @a task is started!
1224 struct GNUNET_SCHEDULER_Task *
1225 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1226 GNUNET_SCHEDULER_TaskCallback task,
1229 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1237 * Schedule a new task to be run at the specified time. The task
1238 * will be scheduled for execution once specified time has been
1239 * reached. It will be run with the DEFAULT priority.
1241 * @param at time at which this operation should run
1242 * @param task main function of the task
1243 * @param task_cls closure of @a task
1244 * @return unique task identifier for the job
1245 * only valid until @a task is started!
1247 struct GNUNET_SCHEDULER_Task *
1248 GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1249 GNUNET_SCHEDULER_TaskCallback task,
1252 return GNUNET_SCHEDULER_add_at_with_priority (at,
1253 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1260 * Schedule a new task to be run with a specified delay. The task
1261 * will be scheduled for execution once the delay has expired. It
1262 * will be run with the DEFAULT priority.
1264 * @param delay when should this operation time out?
1265 * @param task main function of the task
1266 * @param task_cls closure of @a task
1267 * @return unique task identifier for the job
1268 * only valid until @a task is started!
1270 struct GNUNET_SCHEDULER_Task *
1271 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1272 GNUNET_SCHEDULER_TaskCallback task,
1275 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1276 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1283 * Schedule a new task to be run as soon as possible. Note that this
1284 * does not guarantee that this will be the next task that is being
1285 * run, as other tasks with higher priority (or that are already ready
1286 * to run) might get to run first. Just as with delays, clients must
1287 * not rely on any particular order of execution between tasks
1288 * scheduled concurrently.
1290 * The task will be run with the DEFAULT priority.
1292 * @param task main function of the task
1293 * @param task_cls closure of @a task
1294 * @return unique task identifier for the job
1295 * only valid until @a task is started!
1297 struct GNUNET_SCHEDULER_Task *
1298 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
1301 struct GNUNET_SCHEDULER_Task *t;
1303 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1304 GNUNET_async_scope_get (&t->scope);
1306 t->callback_cls = task_cls;
1310 t->start_time = GNUNET_TIME_absolute_get ();
1312 t->timeout = GNUNET_TIME_UNIT_ZERO_ABS;
1313 t->priority = current_priority;
1314 t->on_shutdown = GNUNET_YES;
1315 t->lifeness = current_lifeness;
1316 queue_ready_task (t);
1323 * Schedule a new task to be run on shutdown, that is when a CTRL-C
1324 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
1327 * @param task main function of the task
1328 * @param task_cls closure of @a task
1329 * @return unique task identifier for the job
1330 * only valid until @a task is started!
1332 struct GNUNET_SCHEDULER_Task *
1333 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1336 struct GNUNET_SCHEDULER_Task *t;
1338 /* scheduler must be running */
1339 GNUNET_assert (NULL != scheduler_driver);
1340 GNUNET_assert (NULL != task);
1341 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1342 GNUNET_async_scope_get (&t->scope);
1344 t->callback_cls = task_cls;
1348 t->start_time = GNUNET_TIME_absolute_get ();
1350 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1351 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1352 t->on_shutdown = GNUNET_YES;
1353 t->lifeness = GNUNET_NO;
1354 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1357 LOG (GNUNET_ERROR_TYPE_DEBUG,
1358 "Adding shutdown task %p\n",
1366 * Schedule a new task to be run as soon as possible with the
1367 * (transitive) ignore-shutdown flag either explicitly set or
1368 * explicitly enabled. This task (and all tasks created from it,
1369 * other than by another call to this function) will either count or
1370 * not count for the "lifeness" of the process. This API is only
1371 * useful in a few special cases.
1373 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
1374 * @param task main function of the task
1375 * @param task_cls closure of @a task
1376 * @return unique task identifier for the job
1377 * only valid until @a task is started!
1379 struct GNUNET_SCHEDULER_Task *
1380 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1381 GNUNET_SCHEDULER_TaskCallback task,
1384 struct GNUNET_SCHEDULER_Task *ret;
1386 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1387 ret->lifeness = lifeness;
1394 * check a raw file descriptor and abort if it is bad (for debugging purposes)
1396 * @param t the task related to the file descriptor
1397 * @param raw_fd the raw file descriptor to check
1400 check_fd (struct GNUNET_SCHEDULER_Task *t, int raw_fd)
1404 int flags = fcntl (raw_fd, F_GETFD);
1406 if ((flags == -1) && (errno == EBADF))
1408 LOG (GNUNET_ERROR_TYPE_ERROR,
1409 "Got invalid file descriptor %d!\n",
1422 * Schedule a new task to be run with a specified delay or when any of
1423 * the specified file descriptor sets is ready. The delay can be used
1424 * as a timeout on the socket(s) being ready. The task will be
1425 * scheduled for execution once either the delay has expired or any of
1426 * the socket operations is ready. This is the most general
1427 * function of the "add" family. Note that the "prerequisite_task"
1428 * must be satisfied in addition to any of the other conditions. In
1429 * other words, the task will be started when
1431 * (prerequisite-run)
1437 * @param delay how long should we wait?
1438 * @param priority priority to use
1439 * @param rfd file descriptor we want to read (can be -1)
1440 * @param wfd file descriptors we want to write (can be -1)
1441 * @param task main function of the task
1442 * @param task_cls closure of @a task
1443 * @return unique task identifier for the job
1444 * only valid until @a task is started!
1446 static struct GNUNET_SCHEDULER_Task *
1447 add_without_sets (struct GNUNET_TIME_Relative delay,
1448 enum GNUNET_SCHEDULER_Priority priority,
1449 const struct GNUNET_NETWORK_Handle *read_nh,
1450 const struct GNUNET_NETWORK_Handle *write_nh,
1451 const struct GNUNET_DISK_FileHandle *read_fh,
1452 const struct GNUNET_DISK_FileHandle *write_fh,
1453 GNUNET_SCHEDULER_TaskCallback task,
1456 struct GNUNET_SCHEDULER_Task *t;
1458 /* scheduler must be running */
1459 GNUNET_assert (NULL != scheduler_driver);
1460 GNUNET_assert (NULL != task);
1461 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1462 GNUNET_async_scope_get (&t->scope);
1473 t->callback_cls = task_cls;
1475 check_fd (t, NULL != read_nh ? GNUNET_NETWORK_get_fd (read_nh) : -1);
1476 check_fd (t, NULL != write_nh ? GNUNET_NETWORK_get_fd (write_nh) : -1);
1477 check_fd (t, NULL != read_fh ? read_fh->fd : -1);
1478 check_fd (t, NULL != write_fh ? write_fh->fd : -1);
1481 t->start_time = GNUNET_TIME_absolute_get ();
1483 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1484 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ?
1485 current_priority : priority);
1486 t->lifeness = current_lifeness;
1487 GNUNET_CONTAINER_DLL_insert (pending_head,
1490 driver_add_multiple (t);
1491 max_priority_added = GNUNET_MAX (max_priority_added,
1499 * Schedule a new task to be run with a specified delay or when the
1500 * specified file descriptor is ready for reading. The delay can be
1501 * used as a timeout on the socket being ready. The task will be
1502 * scheduled for execution once either the delay has expired or the
1503 * socket operation is ready. It will be run with the DEFAULT priority.
1504 * Only allowed to be called as long as the scheduler is running, that
1505 * is one of the following conditions is met:
1507 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1508 * - #GNUNET_SCHEDULER_driver_init has been run and
1509 * #GNUNET_SCHEDULER_driver_done has not been called yet
1511 * @param delay when should this operation time out?
1512 * @param rfd read file-descriptor
1513 * @param task main function of the task
1514 * @param task_cls closure of @a task
1515 * @return unique task identifier for the job
1516 * only valid until @a task is started!
1518 struct GNUNET_SCHEDULER_Task *
1519 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1520 struct GNUNET_NETWORK_Handle *rfd,
1521 GNUNET_SCHEDULER_TaskCallback task,
1524 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1525 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1526 rfd, task, task_cls);
1531 * Schedule a new task to be run with a specified priority and to be
1532 * run after the specified delay or when the specified file descriptor
1533 * is ready for reading. The delay can be used as a timeout on the
1534 * socket being ready. The task will be scheduled for execution once
1535 * either the delay has expired or the socket operation is ready. It
1536 * will be run with the DEFAULT priority.
1537 * Only allowed to be called as long as the scheduler is running, that
1538 * is one of the following conditions is met:
1540 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1541 * - #GNUNET_SCHEDULER_driver_init has been run and
1542 * #GNUNET_SCHEDULER_driver_done has not been called yet
1544 * @param delay when should this operation time out?
1545 * @param priority priority to use for the task
1546 * @param rfd read file-descriptor
1547 * @param task main function of the task
1548 * @param task_cls closure of @a task
1549 * @return unique task identifier for the job
1550 * only valid until @a task is started!
1552 struct GNUNET_SCHEDULER_Task *
1553 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1554 enum GNUNET_SCHEDULER_Priority
1556 struct GNUNET_NETWORK_Handle *rfd,
1557 GNUNET_SCHEDULER_TaskCallback task,
1560 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1569 * Schedule a new task to be run with a specified delay or when the
1570 * specified file descriptor is ready for writing. The delay can be
1571 * used as a timeout on the socket being ready. The task will be
1572 * scheduled for execution once either the delay has expired or the
1573 * socket operation is ready. It will be run with the priority of
1575 * Only allowed to be called as long as the scheduler is running, that
1576 * is one of the following conditions is met:
1578 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1579 * - #GNUNET_SCHEDULER_driver_init has been run and
1580 * #GNUNET_SCHEDULER_driver_done has not been called yet
1582 * @param delay when should this operation time out?
1583 * @param wfd write file-descriptor
1584 * @param task main function of the task
1585 * @param task_cls closure of @a task
1586 * @return unique task identifier for the job
1587 * only valid until @a task is started!
1589 struct GNUNET_SCHEDULER_Task *
1590 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1591 struct GNUNET_NETWORK_Handle *wfd,
1592 GNUNET_SCHEDULER_TaskCallback task,
1595 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1596 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1598 GNUNET_NO, GNUNET_YES,
1604 * Schedule a new task to be run with a specified delay or when the
1605 * specified file descriptor is ready. The delay can be
1606 * used as a timeout on the socket being ready. The task will be
1607 * scheduled for execution once either the delay has expired or the
1608 * socket operation is ready.
1609 * Only allowed to be called as long as the scheduler is running, that
1610 * is one of the following conditions is met:
1612 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1613 * - #GNUNET_SCHEDULER_driver_init has been run and
1614 * #GNUNET_SCHEDULER_driver_done has not been called yet
1616 * @param delay when should this operation time out?
1617 * @param priority priority of the task
1618 * @param fd file-descriptor
1619 * @param on_read whether to poll the file-descriptor for readability
1620 * @param on_write whether to poll the file-descriptor for writability
1621 * @param task main function of the task
1622 * @param task_cls closure of task
1623 * @return unique task identifier for the job
1624 * only valid until "task" is started!
1626 struct GNUNET_SCHEDULER_Task *
1627 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1628 enum GNUNET_SCHEDULER_Priority priority,
1629 struct GNUNET_NETWORK_Handle *fd,
1632 GNUNET_SCHEDULER_TaskCallback task,
1635 /* scheduler must be running */
1636 GNUNET_assert (NULL != scheduler_driver);
1637 GNUNET_assert (on_read || on_write);
1638 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1639 return add_without_sets (delay, priority,
1640 on_read ? fd : NULL,
1641 on_write ? fd : NULL,
1649 * Schedule a new task to be run with a specified delay or when the
1650 * specified file descriptor is ready for reading. The delay can be
1651 * used as a timeout on the socket being ready. The task will be
1652 * scheduled for execution once either the delay has expired or the
1653 * socket operation is ready. It will be run with the DEFAULT priority.
1654 * Only allowed to be called as long as the scheduler is running, that
1655 * is one of the following conditions is met:
1657 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1658 * - #GNUNET_SCHEDULER_driver_init has been run and
1659 * #GNUNET_SCHEDULER_driver_done has not been called yet
1661 * @param delay when should this operation time out?
1662 * @param rfd read file-descriptor
1663 * @param task main function of the task
1664 * @param task_cls closure of @a task
1665 * @return unique task identifier for the job
1666 * only valid until @a task is started!
1668 struct GNUNET_SCHEDULER_Task *
1669 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1670 const struct GNUNET_DISK_FileHandle *rfd,
1671 GNUNET_SCHEDULER_TaskCallback task,
1674 return GNUNET_SCHEDULER_add_file_with_priority (
1675 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1676 rfd, GNUNET_YES, GNUNET_NO,
1682 * Schedule a new task to be run with a specified delay or when the
1683 * specified file descriptor is ready for writing. The delay can be
1684 * used as a timeout on the socket being ready. The task will be
1685 * scheduled for execution once either the delay has expired or the
1686 * socket operation is ready. It will be run with the DEFAULT priority.
1687 * Only allowed to be called as long as the scheduler is running, that
1688 * is one of the following conditions is met:
1690 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1691 * - #GNUNET_SCHEDULER_driver_init has been run and
1692 * #GNUNET_SCHEDULER_driver_done has not been called yet
1694 * @param delay when should this operation time out?
1695 * @param wfd write file-descriptor
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_write_file (struct GNUNET_TIME_Relative delay,
1703 const struct GNUNET_DISK_FileHandle *wfd,
1704 GNUNET_SCHEDULER_TaskCallback task,
1707 return GNUNET_SCHEDULER_add_file_with_priority (
1708 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1709 wfd, GNUNET_NO, GNUNET_YES,
1715 * Schedule a new task to be run with a specified delay or when the
1716 * specified file descriptor is ready. The delay can be
1717 * used as a timeout on the socket being ready. The task will be
1718 * scheduled for execution once either the delay has expired or the
1719 * socket operation is ready.
1720 * Only allowed to be called as long as the scheduler is running, that
1721 * is one of the following conditions is met:
1723 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1724 * - #GNUNET_SCHEDULER_driver_init has been run and
1725 * #GNUNET_SCHEDULER_driver_done has not been called yet
1727 * @param delay when should this operation time out?
1728 * @param priority priority of the task
1729 * @param fd file-descriptor
1730 * @param on_read whether to poll the file-descriptor for readability
1731 * @param on_write whether to poll the file-descriptor for writability
1732 * @param task main function of the task
1733 * @param task_cls closure of @a task
1734 * @return unique task identifier for the job
1735 * only valid until @a task is started!
1737 struct GNUNET_SCHEDULER_Task *
1738 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1739 enum GNUNET_SCHEDULER_Priority
1742 GNUNET_DISK_FileHandle *fd,
1743 int on_read, int on_write,
1744 GNUNET_SCHEDULER_TaskCallback task,
1747 /* scheduler must be running */
1748 GNUNET_assert (NULL != scheduler_driver);
1749 GNUNET_assert (on_read || on_write);
1750 GNUNET_assert (fd->fd >= 0);
1751 return add_without_sets (delay, priority,
1754 on_read ? fd : NULL,
1755 on_write ? fd : NULL,
1761 extract_handles (const struct GNUNET_NETWORK_FDSet *fdset,
1762 const struct GNUNET_NETWORK_Handle ***ntarget,
1763 unsigned int *extracted_nhandles,
1764 const struct GNUNET_DISK_FileHandle ***ftarget,
1765 unsigned int *extracted_fhandles)
1767 // FIXME: this implementation only works for unix, for WIN32 the file handles
1768 // in fdset must be handled separately
1769 const struct GNUNET_NETWORK_Handle **nhandles;
1770 const struct GNUNET_DISK_FileHandle **fhandles;
1771 unsigned int nhandles_len;
1772 unsigned int fhandles_len;
1778 for (int sock = 0; sock != fdset->nsds; ++sock)
1780 if (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (fdset, sock))
1782 struct GNUNET_NETWORK_Handle *nhandle;
1783 struct GNUNET_DISK_FileHandle *fhandle;
1785 nhandle = GNUNET_NETWORK_socket_box_native (sock);
1786 if (NULL != nhandle)
1788 GNUNET_array_append (nhandles, nhandles_len, nhandle);
1792 fhandle = GNUNET_DISK_get_handle_from_int_fd (sock);
1793 if (NULL != fhandle)
1795 GNUNET_array_append (fhandles, fhandles_len, fhandle);
1804 *ntarget = nhandles_len > 0 ? nhandles : NULL;
1805 *ftarget = fhandles_len > 0 ? fhandles : NULL;
1806 *extracted_nhandles = nhandles_len;
1807 *extracted_fhandles = fhandles_len;
1812 * Schedule a new task to be run with a specified delay or when any of
1813 * the specified file descriptor sets is ready. The delay can be used
1814 * as a timeout on the socket(s) being ready. The task will be
1815 * scheduled for execution once either the delay has expired or any of
1816 * the socket operations is ready. This is the most general
1817 * function of the "add" family. Note that the "prerequisite_task"
1818 * must be satisfied in addition to any of the other conditions. In
1819 * other words, the task will be started when
1821 * (prerequisite-run)
1824 * || any-ws-ready) )
1826 * Only allowed to be called as long as the scheduler is running, that
1827 * is one of the following conditions is met:
1829 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1830 * - #GNUNET_SCHEDULER_driver_init has been run and
1831 * #GNUNET_SCHEDULER_driver_done has not been called yet
1833 * @param prio how important is this task?
1834 * @param delay how long should we wait?
1835 * @param rs set of file descriptors we want to read (can be NULL)
1836 * @param ws set of file descriptors we want to write (can be NULL)
1837 * @param task main function of the task
1838 * @param task_cls closure of @a task
1839 * @return unique task identifier for the job
1840 * only valid until @a task is started!
1842 struct GNUNET_SCHEDULER_Task *
1843 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1844 struct GNUNET_TIME_Relative delay,
1845 const struct GNUNET_NETWORK_FDSet *rs,
1846 const struct GNUNET_NETWORK_FDSet *ws,
1847 GNUNET_SCHEDULER_TaskCallback task,
1850 struct GNUNET_SCHEDULER_Task *t;
1851 const struct GNUNET_NETWORK_Handle **read_nhandles = NULL;
1852 const struct GNUNET_NETWORK_Handle **write_nhandles = NULL;
1853 const struct GNUNET_DISK_FileHandle **read_fhandles = NULL;
1854 const struct GNUNET_DISK_FileHandle **write_fhandles = NULL;
1855 unsigned int read_nhandles_len = 0;
1856 unsigned int write_nhandles_len = 0;
1857 unsigned int read_fhandles_len = 0;
1858 unsigned int write_fhandles_len = 0;
1860 /* scheduler must be running */
1861 GNUNET_assert (NULL != scheduler_driver);
1862 GNUNET_assert (NULL != task);
1863 int no_rs = (NULL == rs);
1864 int no_ws = (NULL == ws);
1865 int empty_rs = (NULL != rs) && (0 == rs->nsds);
1866 int empty_ws = (NULL != ws) && (0 == ws->nsds);
1867 int no_fds = (no_rs && no_ws) ||
1868 (empty_rs && empty_ws) ||
1869 (no_rs && empty_ws) ||
1870 (no_ws && empty_rs);
1875 extract_handles (rs,
1879 &read_fhandles_len);
1883 extract_handles (ws,
1885 &write_nhandles_len,
1887 &write_fhandles_len);
1891 * here we consider the case that a GNUNET_NETWORK_FDSet might be empty
1892 * although its maximum FD number (nsds) is greater than 0. We handle
1893 * this case gracefully because some libraries such as libmicrohttpd
1894 * only provide a hint what the maximum FD number in an FD set might be
1895 * and not the exact FD number (see e.g. gnunet-rest-service.c)
1896 */int no_fds_extracted = (0 == read_nhandles_len) &&
1897 (0 == read_fhandles_len) &&
1898 (0 == write_nhandles_len) &&
1899 (0 == write_fhandles_len);
1900 if (no_fds || no_fds_extracted)
1901 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1905 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1906 GNUNET_async_scope_get (&t->scope);
1915 write_fhandles_len);
1917 t->callback_cls = task_cls;
1918 t->own_handles = GNUNET_YES;
1919 /* free the arrays of pointers to network / file handles, the actual
1920 * handles will be freed in destroy_task */
1921 GNUNET_array_grow (read_nhandles, read_nhandles_len, 0);
1922 GNUNET_array_grow (write_nhandles, write_nhandles_len, 0);
1923 GNUNET_array_grow (read_fhandles, read_fhandles_len, 0);
1924 GNUNET_array_grow (write_fhandles, write_fhandles_len, 0);
1926 t->start_time = GNUNET_TIME_absolute_get ();
1928 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1930 check_priority ((prio ==
1931 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1933 t->lifeness = current_lifeness;
1934 GNUNET_CONTAINER_DLL_insert (pending_head,
1937 driver_add_multiple (t);
1938 max_priority_added = GNUNET_MAX (max_priority_added,
1940 LOG (GNUNET_ERROR_TYPE_DEBUG,
1949 * Function used by event-loop implementations to signal the scheduler
1950 * that a particular @a task is ready due to an event specified in the
1951 * et field of @a fdi.
1953 * This function will then queue the task to notify the application
1954 * that the task is ready (with the respective priority).
1956 * @param task the task that is ready
1957 * @param fdi information about the related FD
1960 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1961 struct GNUNET_SCHEDULER_FdInfo *fdi)
1963 enum GNUNET_SCHEDULER_Reason reason;
1965 reason = task->reason;
1966 if ((0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1967 (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et)))
1968 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
1969 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
1970 (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et)))
1971 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
1972 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1973 task->reason = reason;
1974 if (GNUNET_NO == task->in_ready_list)
1976 GNUNET_CONTAINER_DLL_remove (pending_head,
1979 queue_ready_task (task);
1985 * Function called by external event loop implementations to tell the
1986 * scheduler to run some of the tasks that are ready. Must be called
1987 * only after #GNUNET_SCHEDULER_driver_init has been called and before
1988 * #GNUNET_SCHEDULER_driver_done is called.
1989 * This function may return even though there are tasks left to run
1990 * just to give other tasks a chance as well. If we return #GNUNET_YES,
1991 * the event loop implementation should call this function again as
1992 * soon as possible, while if we return #GNUNET_NO it must block until
1993 * either the operating system has more work (the scheduler has no more
1994 * work to do right now) or the timeout set by the scheduler (using the
1995 * set_wakeup callback) is reached.
1997 * @param sh scheduler handle that was returned by
1998 * #GNUNET_SCHEDULER_driver_init
1999 * @return #GNUNET_YES if there are more tasks that are ready,
2000 * and thus we would like to run more (yield to avoid
2001 * blocking other activities for too long) #GNUNET_NO
2002 * if we are done running tasks (yield to block)
2005 GNUNET_SCHEDULER_do_work (struct GNUNET_SCHEDULER_Handle *sh)
2007 struct GNUNET_SCHEDULER_Task *pos;
2008 struct GNUNET_TIME_Absolute now;
2010 /* check for tasks that reached the timeout! */
2011 now = GNUNET_TIME_absolute_get ();
2012 pos = pending_timeout_head;
2015 struct GNUNET_SCHEDULER_Task *next = pos->next;
2016 if (now.abs_value_us >= pos->timeout.abs_value_us)
2017 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
2018 if (0 == pos->reason)
2020 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
2021 pending_timeout_tail,
2023 if (pending_timeout_last == pos)
2024 pending_timeout_last = NULL;
2025 queue_ready_task (pos);
2031 struct GNUNET_SCHEDULER_Task *next = pos->next;
2032 if (now.abs_value_us >= pos->timeout.abs_value_us)
2034 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
2035 GNUNET_CONTAINER_DLL_remove (pending_head,
2038 queue_ready_task (pos);
2043 if (0 == ready_count)
2045 struct GNUNET_TIME_Absolute timeout = get_timeout ();
2047 if (timeout.abs_value_us > now.abs_value_us)
2050 * The event loop called this function before the current timeout was
2051 * reached (and no FD tasks are ready). This is acceptable if
2053 * - the system time was changed while the driver was waiting for
2055 * - an external event loop called GNUnet API functions outside of
2056 * the callbacks called in GNUNET_SCHEDULER_do_work and thus
2057 * wasn't notified about the new timeout
2059 * It might also mean we are busy-waiting because of a programming
2060 * error in the external event loop.
2061 */LOG (GNUNET_ERROR_TYPE_DEBUG,
2062 "GNUNET_SCHEDULER_do_work did not find any ready "
2063 "tasks and timeout has not been reached yet.\n");
2068 * the current timeout was reached but no ready tasks were found,
2069 * internal scheduler error!
2076 /* find out which task priority level we are going to
2077 process this time */
2078 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
2079 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
2080 /* yes, p>0 is correct, 0 is "KEEP" which should
2081 * always be an empty queue (see assertion)! */
2082 for (work_priority = GNUNET_SCHEDULER_PRIORITY_COUNT - 1;
2086 pos = ready_head[work_priority];
2090 GNUNET_assert (NULL != pos); /* ready_count wrong? */
2092 /* process all tasks at this priority level, then yield */
2093 while (NULL != (pos = ready_head[work_priority]))
2095 GNUNET_CONTAINER_DLL_remove (ready_head[work_priority],
2096 ready_tail[work_priority],
2099 current_priority = pos->priority;
2100 current_lifeness = pos->lifeness;
2103 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
2104 DELAY_THRESHOLD.rel_value_us)
2106 LOG (GNUNET_ERROR_TYPE_DEBUG,
2107 "Task %p took %s to be scheduled\n",
2109 GNUNET_STRINGS_relative_time_to_string (
2110 GNUNET_TIME_absolute_get_duration (pos->start_time),
2114 tc.reason = pos->reason;
2115 GNUNET_NETWORK_fdset_zero (sh->rs);
2116 GNUNET_NETWORK_fdset_zero (sh->ws);
2117 // FIXME: do we have to remove FdInfos from fds if they are not ready?
2118 tc.fds_len = pos->fds_len;
2120 for (unsigned int i = 0; i != pos->fds_len; ++i)
2122 struct GNUNET_SCHEDULER_FdInfo *fdi = &pos->fds[i];
2123 if (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et))
2125 GNUNET_NETWORK_fdset_set_native (sh->rs,
2128 if (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et))
2130 GNUNET_NETWORK_fdset_set_native (sh->ws,
2134 tc.read_ready = sh->rs;
2135 tc.write_ready = sh->ws;
2136 LOG (GNUNET_ERROR_TYPE_DEBUG,
2137 "Running task %p\n",
2139 GNUNET_assert (NULL != pos->callback);
2141 struct GNUNET_AsyncScopeSave old_scope;
2142 if (pos->scope.have_scope)
2143 GNUNET_async_scope_enter (&pos->scope.scope_id, &old_scope);
2145 GNUNET_async_scope_get (&old_scope);
2146 pos->callback (pos->callback_cls);
2147 GNUNET_async_scope_restore (&old_scope);
2149 if (NULL != pos->fds)
2151 int del_result = scheduler_driver->del (scheduler_driver->cls, pos);
2152 if (GNUNET_OK != del_result)
2154 LOG (GNUNET_ERROR_TYPE_ERROR,
2155 "driver could not delete task %p\n", pos);
2160 dump_backtrace (pos);
2164 shutdown_if_no_lifeness ();
2165 if (0 == ready_count)
2167 scheduler_driver->set_wakeup (scheduler_driver->cls,
2171 scheduler_driver->set_wakeup (scheduler_driver->cls,
2172 GNUNET_TIME_absolute_get ());
2178 * Function called by external event loop implementations to initialize
2179 * the scheduler. An external implementation has to provide @a driver
2180 * which contains callbacks for the scheduler (see definition of struct
2181 * #GNUNET_SCHEDULER_Driver). The callbacks are used to instruct the
2182 * external implementation to watch for events. If it detects any of
2183 * those events it is expected to call #GNUNET_SCHEDULER_do_work to let
2184 * the scheduler handle it. If an event is related to a specific task
2185 * (e.g. the scheduler gave instructions to watch a file descriptor),
2186 * the external implementation is expected to mark that task ready
2187 * before by calling #GNUNET_SCHEDULER_task_ready.
2189 * This function has to be called before any tasks are scheduled and
2190 * before GNUNET_SCHEDULER_do_work is called for the first time. It
2191 * allocates resources that have to be freed again by calling
2192 * #GNUNET_SCHEDULER_driver_done.
2194 * This function installs the same signal handlers as
2195 * #GNUNET_SCHEDULER_run. This means SIGTERM (and other similar signals)
2196 * will induce a call to #GNUNET_SCHEDULER_shutdown during the next
2197 * call to #GNUNET_SCHEDULER_do_work. As a result, SIGTERM causes all
2198 * active tasks to be scheduled with reason
2199 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added afterwards
2200 * will execute normally!). Note that any particular signal will only
2201 * shut down one scheduler; applications should always only create a
2204 * @param driver to use for the event loop
2205 * @return handle to be passed to #GNUNET_SCHEDULER_do_work and
2206 * #GNUNET_SCHEDULER_driver_done
2208 struct GNUNET_SCHEDULER_Handle *
2209 GNUNET_SCHEDULER_driver_init (const struct GNUNET_SCHEDULER_Driver *driver)
2211 struct GNUNET_SCHEDULER_Handle *sh;
2212 const struct GNUNET_DISK_FileHandle *pr;
2214 /* scheduler must not be running */
2215 GNUNET_assert (NULL == scheduler_driver);
2216 GNUNET_assert (NULL == shutdown_pipe_handle);
2217 /* general set-up */
2218 sh = GNUNET_new (struct GNUNET_SCHEDULER_Handle);
2219 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
2223 GNUNET_assert (NULL != shutdown_pipe_handle);
2224 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
2225 GNUNET_DISK_PIPE_END_READ);
2227 scheduler_driver = driver;
2229 /* install signal handlers */
2230 LOG (GNUNET_ERROR_TYPE_DEBUG,
2231 "Registering signal handlers\n");
2232 sh->shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2233 &sighandler_shutdown);
2234 sh->shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2235 &sighandler_shutdown);
2236 #if (SIGTERM != GNUNET_TERM_SIG)
2237 sh->shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2238 &sighandler_shutdown);
2240 sh->shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2242 sh->shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2243 &sighandler_shutdown);
2244 sh->shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2245 &sighandler_shutdown);
2247 /* Setup initial tasks */
2248 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2249 current_lifeness = GNUNET_NO;
2250 /* ensure this task runs first, by using a priority level reserved for
2251 the scheduler (not really shutdown, but start-up ;-) */
2252 install_parent_control_task =
2253 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_SHUTDOWN,
2254 &install_parent_control_handler,
2256 shutdown_pipe_task =
2257 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2261 current_lifeness = GNUNET_YES;
2262 scheduler_driver->set_wakeup (scheduler_driver->cls,
2264 /* begin main event loop */
2265 sh->rs = GNUNET_NETWORK_fdset_create ();
2266 sh->ws = GNUNET_NETWORK_fdset_create ();
2267 GNUNET_NETWORK_fdset_handle_set (sh->rs, pr);
2273 * Counter-part of #GNUNET_SCHEDULER_driver_init. Has to be called
2274 * by external event loop implementations after the scheduler has
2275 * shut down. This is the case if both of the following conditions
2278 * - all tasks the scheduler has added through the driver's add
2279 * callback have been removed again through the driver's del
2281 * - the timeout the scheduler has set through the driver's
2282 * add_wakeup callback is FOREVER
2284 * @param sh the handle returned by #GNUNET_SCHEDULER_driver_init
2287 GNUNET_SCHEDULER_driver_done (struct GNUNET_SCHEDULER_Handle *sh)
2289 GNUNET_assert (NULL == pending_head);
2290 GNUNET_assert (NULL == pending_timeout_head);
2291 GNUNET_assert (NULL == shutdown_head);
2292 for (int i = 0; i != GNUNET_SCHEDULER_PRIORITY_COUNT; ++i)
2294 GNUNET_assert (NULL == ready_head[i]);
2296 GNUNET_NETWORK_fdset_destroy (sh->rs);
2297 GNUNET_NETWORK_fdset_destroy (sh->ws);
2299 /* uninstall signal handlers */
2300 GNUNET_SIGNAL_handler_uninstall (sh->shc_int);
2301 GNUNET_SIGNAL_handler_uninstall (sh->shc_term);
2302 #if (SIGTERM != GNUNET_TERM_SIG)
2303 GNUNET_SIGNAL_handler_uninstall (sh->shc_gterm);
2305 GNUNET_SIGNAL_handler_uninstall (sh->shc_pipe);
2306 GNUNET_SIGNAL_handler_uninstall (sh->shc_quit);
2307 GNUNET_SIGNAL_handler_uninstall (sh->shc_hup);
2308 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2309 shutdown_pipe_handle = NULL;
2310 scheduler_driver = NULL;
2316 select_loop (struct GNUNET_SCHEDULER_Handle *sh,
2317 struct DriverContext *context)
2319 struct GNUNET_NETWORK_FDSet *rs;
2320 struct GNUNET_NETWORK_FDSet *ws;
2323 GNUNET_assert (NULL != context);
2324 rs = GNUNET_NETWORK_fdset_create ();
2325 ws = GNUNET_NETWORK_fdset_create ();
2326 while ((NULL != context->scheduled_head) ||
2327 (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us !=
2328 context->timeout.abs_value_us))
2330 struct GNUNET_TIME_Relative time_remaining;
2332 LOG (GNUNET_ERROR_TYPE_DEBUG,
2333 "select timeout = %s\n",
2334 GNUNET_STRINGS_absolute_time_to_string (context->timeout));
2336 GNUNET_NETWORK_fdset_zero (rs);
2337 GNUNET_NETWORK_fdset_zero (ws);
2339 for (struct Scheduled *pos = context->scheduled_head;
2343 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et))
2345 GNUNET_NETWORK_fdset_set_native (rs, pos->fdi->sock);
2347 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et))
2349 GNUNET_NETWORK_fdset_set_native (ws, pos->fdi->sock);
2352 time_remaining = GNUNET_TIME_absolute_get_remaining (context->timeout);
2353 if (0 < ready_count)
2354 time_remaining = GNUNET_TIME_UNIT_ZERO;
2355 if (NULL == scheduler_select)
2357 select_result = GNUNET_NETWORK_socket_select (rs,
2364 select_result = scheduler_select (scheduler_select_cls,
2370 if (select_result == GNUNET_SYSERR)
2375 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
2386 if (0 != system (lsof))
2387 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
2391 for (struct Scheduled *s = context->scheduled_head;
2395 int flags = fcntl (s->fdi->sock,
2398 if ((flags == -1) &&
2401 LOG (GNUNET_ERROR_TYPE_ERROR,
2402 "Got invalid file descriptor %d!\n",
2405 dump_backtrace (s->task);
2411 GNUNET_NETWORK_fdset_destroy (rs);
2412 GNUNET_NETWORK_fdset_destroy (ws);
2413 return GNUNET_SYSERR;
2415 if (select_result > 0)
2417 for (struct Scheduled *pos = context->scheduled_head;
2421 int is_ready = GNUNET_NO;
2423 if ((0 != (GNUNET_SCHEDULER_ET_IN & pos->et)) &&
2425 GNUNET_NETWORK_fdset_test_native (rs,
2428 pos->fdi->et |= GNUNET_SCHEDULER_ET_IN;
2429 is_ready = GNUNET_YES;
2431 if ((0 != (GNUNET_SCHEDULER_ET_OUT & pos->et)) &&
2433 GNUNET_NETWORK_fdset_test_native (ws,
2436 pos->fdi->et |= GNUNET_SCHEDULER_ET_OUT;
2437 is_ready = GNUNET_YES;
2439 if (GNUNET_YES == is_ready)
2441 GNUNET_SCHEDULER_task_ready (pos->task,
2446 if (GNUNET_YES == GNUNET_SCHEDULER_do_work (sh))
2448 LOG (GNUNET_ERROR_TYPE_DEBUG,
2449 "scheduler has more tasks ready!\n");
2452 GNUNET_NETWORK_fdset_destroy (rs);
2453 GNUNET_NETWORK_fdset_destroy (ws);
2459 select_add (void *cls,
2460 struct GNUNET_SCHEDULER_Task *task,
2461 struct GNUNET_SCHEDULER_FdInfo *fdi)
2463 struct DriverContext *context = cls;
2465 GNUNET_assert (NULL != context);
2466 GNUNET_assert (NULL != task);
2467 GNUNET_assert (NULL != fdi);
2468 GNUNET_assert (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et) ||
2469 0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et));
2471 if (! ((NULL != fdi->fd) ^ (NULL != fdi->fh)) || (fdi->sock < 0))
2473 /* exactly one out of {fd, hf} must be != NULL and the OS handle must be valid */
2474 return GNUNET_SYSERR;
2477 struct Scheduled *scheduled = GNUNET_new (struct Scheduled);
2478 scheduled->task = task;
2479 scheduled->fdi = fdi;
2480 scheduled->et = fdi->et;
2482 GNUNET_CONTAINER_DLL_insert (context->scheduled_head,
2483 context->scheduled_tail,
2490 select_del (void *cls,
2491 struct GNUNET_SCHEDULER_Task *task)
2493 struct DriverContext *context;
2494 struct Scheduled *pos;
2497 GNUNET_assert (NULL != cls);
2500 ret = GNUNET_SYSERR;
2501 pos = context->scheduled_head;
2504 struct Scheduled *next = pos->next;
2505 if (pos->task == task)
2507 GNUNET_CONTAINER_DLL_remove (context->scheduled_head,
2508 context->scheduled_tail,
2520 select_set_wakeup (void *cls,
2521 struct GNUNET_TIME_Absolute dt)
2523 struct DriverContext *context = cls;
2525 GNUNET_assert (NULL != context);
2526 context->timeout = dt;
2531 * Obtain the driver for using select() as the event loop.
2533 * @return NULL on error
2535 struct GNUNET_SCHEDULER_Driver *
2536 GNUNET_SCHEDULER_driver_select ()
2538 struct GNUNET_SCHEDULER_Driver *select_driver;
2540 select_driver = GNUNET_new (struct GNUNET_SCHEDULER_Driver);
2542 select_driver->add = &select_add;
2543 select_driver->del = &select_del;
2544 select_driver->set_wakeup = &select_set_wakeup;
2546 return select_driver;
2551 * Change the async scope for the currently executing task and (transitively)
2552 * for all tasks scheduled by the current task after calling this function.
2553 * Nested tasks can begin their own nested async scope.
2555 * Once the current task is finished, the async scope ID is reset to
2556 * its previous value.
2558 * Must only be called from a running task.
2560 * @param aid the asynchronous scope id to enter
2563 GNUNET_SCHEDULER_begin_async_scope (struct GNUNET_AsyncScopeId *aid)
2565 struct GNUNET_AsyncScopeSave dummy_old_scope;
2567 GNUNET_assert (NULL != active_task);
2568 /* Since we're in a task, the context will be automatically
2569 restored by the scheduler. */
2570 GNUNET_async_scope_enter (aid, &dummy_old_scope);
2574 /* end of scheduler.c */