2 This file is part of GNUnet
3 Copyright (C) 2009-2017 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file util/scheduler.c
22 * @brief schedule computations using continuation passing style
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util-scheduler", __VA_ARGS__)
31 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-scheduler", syscall)
38 * Use lsof to generate file descriptor reports on select error?
39 * (turn off for stable releases).
41 #define USE_LSOF GNUNET_NO
44 * Obtain trace information for all scheduler calls that schedule tasks.
46 #define EXECINFO GNUNET_NO
49 * Check each file descriptor before adding
51 #define DEBUG_FDS GNUNET_NO
54 * Depth of the traces collected via EXECINFO.
56 #define MAX_TRACE_DEPTH 50
60 * Should we figure out which tasks are delayed for a while
61 * before they are run? (Consider using in combination with EXECINFO).
63 #define PROFILE_DELAYS GNUNET_NO
66 * Task that were in the queue for longer than this are reported if
67 * PROFILE_DELAYS is active.
69 #define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
73 * Argument to be passed from the driver to
74 * #GNUNET_SCHEDULER_run_from_driver(). Contains the
75 * scheduler's internal state.
77 struct GNUNET_SCHEDULER_Handle
80 * Passed here to avoid constantly allocating/deallocating
81 * this element, but generally we want to get rid of this.
84 struct GNUNET_NETWORK_FDSet *rs;
87 * Passed here to avoid constantly allocating/deallocating
88 * this element, but generally we want to get rid of this.
91 struct GNUNET_NETWORK_FDSet *ws;
96 * Entry in list of pending tasks.
98 struct GNUNET_SCHEDULER_Task
101 * This is a linked list.
103 struct GNUNET_SCHEDULER_Task *next;
106 * This is a linked list.
108 struct GNUNET_SCHEDULER_Task *prev;
111 * Function to run when ready.
113 GNUNET_SCHEDULER_TaskCallback callback;
116 * Closure for the @e callback.
121 * Information about which FDs are ready for this task (and why).
123 struct GNUNET_SCHEDULER_FdInfo *fds;
126 * Storage location used for @e fds if we want to avoid
127 * a separate malloc() call in the common case that this
128 * task is only about a single FD.
130 struct GNUNET_SCHEDULER_FdInfo fdx;
133 * Size of the @e fds array.
135 unsigned int fds_len;
138 * if this task is related to multiple FDs this array contains
139 * all FdInfo structs that were marked as ready by calling
140 * #GNUNET_SCHEDULER_task_ready
142 struct GNUNET_SCHEDULER_FdInfo *ready_fds;
145 * Size of the @e ready_fds array
147 unsigned int ready_fds_len;
150 * Do we own the network and file handles referenced by the FdInfo
151 * structs in the fds array. This will only be GNUNET_YES if the
152 * task was created by the #GNUNET_SCHEDULER_add_select function.
157 * Absolute timeout value for the task, or
158 * #GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
160 struct GNUNET_TIME_Absolute timeout;
164 * When was the task scheduled?
166 struct GNUNET_TIME_Absolute start_time;
170 * Why is the task ready? Set after task is added to ready queue.
171 * Initially set to zero. All reasons that have already been
172 * satisfied (i.e. read or write ready) will be set over time.
174 enum GNUNET_SCHEDULER_Reason reason;
179 enum GNUNET_SCHEDULER_Priority priority;
182 * Set if we only wait for reading from a single FD, otherwise -1.
187 * Set if we only wait for writing to a single FD, otherwise -1.
192 * Should the existence of this task in the queue be counted as
193 * reason to not shutdown the scheduler?
198 * Is this task run on shutdown?
203 * Is this task in the ready list?
209 * Array of strings which make up a backtrace from the point when this
210 * task was scheduled (essentially, who scheduled the task?)
212 char **backtrace_strings;
215 * Size of the backtrace_strings array
217 int num_backtrace_strings;
224 * A struct representing an event the select driver is waiting for
228 struct Scheduled *prev;
230 struct Scheduled *next;
233 * the task, the event is related to
235 struct GNUNET_SCHEDULER_Task *task;
238 * information about the network socket / file descriptor where
239 * the event is expected to occur
241 struct GNUNET_SCHEDULER_FdInfo *fdi;
244 * the event types (multiple event types can be ORed) the select
245 * driver is expected to wait for
247 enum GNUNET_SCHEDULER_EventType et;
252 * Driver context used by GNUNET_SCHEDULER_run
257 * the head of a DLL containing information about the events the
258 * select driver is waiting for
260 struct Scheduled *scheduled_head;
263 * the tail of a DLL containing information about the events the
264 * select driver is waiting for
266 struct Scheduled *scheduled_tail;
269 * the time until the select driver will wake up again (after
272 struct GNUNET_TIME_Relative timeout;
277 * The driver used for the event loop. Will be handed over to
278 * the scheduler in #GNUNET_SCHEDULER_run_from_driver(), peristed
279 * there in this variable for later use in functions like
280 * #GNUNET_SCHEDULER_add_select(), #add_without_sets() and
281 * #GNUNET_SCHEDULER_cancel().
283 static const struct GNUNET_SCHEDULER_Driver *scheduler_driver;
286 * Head of list of tasks waiting for an event.
288 static struct GNUNET_SCHEDULER_Task *pending_head;
291 * Tail of list of tasks waiting for an event.
293 static struct GNUNET_SCHEDULER_Task *pending_tail;
296 * Head of list of tasks waiting for shutdown.
298 static struct GNUNET_SCHEDULER_Task *shutdown_head;
301 * Tail of list of tasks waiting for shutdown.
303 static struct GNUNET_SCHEDULER_Task *shutdown_tail;
306 * List of tasks waiting ONLY for a timeout event.
307 * Sorted by timeout (earliest first). Used so that
308 * we do not traverse the list of these tasks when
309 * building select sets (we just look at the head
310 * to determine the respective timeout ONCE).
312 static struct GNUNET_SCHEDULER_Task *pending_timeout_head;
315 * List of tasks waiting ONLY for a timeout event.
316 * Sorted by timeout (earliest first). Used so that
317 * we do not traverse the list of these tasks when
318 * building select sets (we just look at the head
319 * to determine the respective timeout ONCE).
321 static struct GNUNET_SCHEDULER_Task *pending_timeout_tail;
324 * Last inserted task waiting ONLY for a timeout event.
325 * Used to (heuristically) speed up insertion.
327 static struct GNUNET_SCHEDULER_Task *pending_timeout_last;
330 * ID of the task that is running right now.
332 static struct GNUNET_SCHEDULER_Task *active_task;
335 * Head of list of tasks ready to run right now, grouped by importance.
337 static struct GNUNET_SCHEDULER_Task *ready_head[GNUNET_SCHEDULER_PRIORITY_COUNT];
340 * Tail of list of tasks ready to run right now, grouped by importance.
342 static struct GNUNET_SCHEDULER_Task *ready_tail[GNUNET_SCHEDULER_PRIORITY_COUNT];
345 * Number of tasks on the ready list.
347 static unsigned int ready_count;
350 * How many tasks have we run so far?
352 static unsigned long long tasks_run;
355 * Priority of the task running right now. Only
356 * valid while a task is running.
358 static enum GNUNET_SCHEDULER_Priority current_priority;
361 * Priority of the highest task added in the current select
364 static enum GNUNET_SCHEDULER_Priority max_priority_added;
367 * Value of the 'lifeness' flag for the current task.
369 static int current_lifeness;
372 * Function to use as a select() in the scheduler.
373 * If NULL, we use GNUNET_NETWORK_socket_select().
375 static GNUNET_SCHEDULER_select scheduler_select;
378 * Task context of the current task.
380 static struct GNUNET_SCHEDULER_TaskContext tc;
383 * Closure for #scheduler_select.
385 static void *scheduler_select_cls;
388 * Scheduler handle used for the driver functions
390 static struct GNUNET_SCHEDULER_Handle sh;
394 * Sets the select function to use in the scheduler (scheduler_select).
396 * @param new_select new select function to use
397 * @param new_select_cls closure for @a new_select
398 * @return previously used select function, NULL for default
401 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
402 void *new_select_cls)
404 scheduler_select = new_select;
405 scheduler_select_cls = new_select_cls;
410 * Check that the given priority is legal (and return it).
412 * @param p priority value to check
413 * @return p on success, 0 on error
415 static enum GNUNET_SCHEDULER_Priority
416 check_priority (enum GNUNET_SCHEDULER_Priority p)
418 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
421 return 0; /* make compiler happy */
426 * chooses the nearest timeout from all pending tasks, to be used
427 * to tell the driver the next wakeup time (using its set_wakeup
430 struct GNUNET_TIME_Absolute
433 struct GNUNET_SCHEDULER_Task *pos;
434 struct GNUNET_TIME_Absolute now;
435 struct GNUNET_TIME_Absolute timeout;
437 pos = pending_timeout_head;
438 now = GNUNET_TIME_absolute_get ();
439 timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
442 if (0 != pos->reason)
448 timeout = pos->timeout;
451 for (pos = pending_head; NULL != pos; pos = pos->next)
453 if (0 != pos->reason)
457 else if ((pos->timeout.abs_value_us != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) &&
458 (timeout.abs_value_us > pos->timeout.abs_value_us))
460 timeout = pos->timeout;
468 * Put a task that is ready for execution into the ready queue.
470 * @param task task ready for execution
473 queue_ready_task (struct GNUNET_SCHEDULER_Task *task)
475 enum GNUNET_SCHEDULER_Priority p = check_priority (task->priority);
477 GNUNET_CONTAINER_DLL_insert (ready_head[p],
480 task->in_ready_list = GNUNET_YES;
486 * Request the shutdown of a scheduler. Marks all tasks
487 * awaiting shutdown as ready. Note that tasks
488 * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call
489 * will be delayed until the next shutdown signal.
492 GNUNET_SCHEDULER_shutdown ()
494 struct GNUNET_SCHEDULER_Task *pos;
496 while (NULL != (pos = shutdown_head))
498 GNUNET_CONTAINER_DLL_remove (shutdown_head,
501 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
502 queue_ready_task (pos);
508 * Output stack trace of task @a t.
510 * @param t task to dump stack trace of
513 dump_backtrace (struct GNUNET_SCHEDULER_Task *t)
516 for (unsigned int i = 0; i < t->num_backtrace_strings; i++)
517 LOG (GNUNET_ERROR_TYPE_WARNING,
518 "Task %p trace %u: %s\n",
521 t->backtrace_strings[i]);
529 * Destroy a task (release associated resources)
531 * @param t task to destroy
534 destroy_task (struct GNUNET_SCHEDULER_Task *t)
538 LOG (GNUNET_ERROR_TYPE_DEBUG,
539 "destroying task %p\n",
542 if (GNUNET_YES == t->own_handles)
544 for (i = 0; i != t->fds_len; ++i)
546 const struct GNUNET_NETWORK_Handle *fd = t->fds[i].fd;
547 const struct GNUNET_DISK_FileHandle *fh = t->fds[i].fh;
550 GNUNET_NETWORK_socket_free_memory_only_ ((struct GNUNET_NETWORK_Handle *) fd);
554 // FIXME: on WIN32 this is not enough! A function
555 // GNUNET_DISK_file_free_memory_only would be nice
556 GNUNET_free ((void *) fh);
562 GNUNET_array_grow (t->fds, t->fds_len, 0);
564 if (t->ready_fds_len > 0)
566 GNUNET_array_grow (t->ready_fds, t->ready_fds_len, 0);
569 GNUNET_free (t->backtrace_strings);
576 * Pipe used to communicate shutdown via signal.
578 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
581 * Process ID of this process at the time we installed the various
587 * Signal handler called for SIGPIPE.
599 // * Wait for a short time.
600 // * Sleeps for @a ms ms (as that should be long enough for virtually all
601 // * modern systems to context switch and allow another process to do
602 // * some 'real' work).
604 // * @param ms how many ms to wait
607 //short_wait (unsigned int ms)
609 // struct GNUNET_TIME_Relative timeout;
611 // timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
612 // (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
617 * Signal handler called for signals that should cause us to shutdown.
620 sighandler_shutdown ()
623 int old_errno = errno; /* backup errno */
625 if (getpid () != my_pid)
626 exit (1); /* we have fork'ed since the signal handler was created,
627 * ignore the signal, see https://gnunet.org/vfork discussion */
628 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
629 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
636 shutdown_if_no_lifeness ()
638 struct GNUNET_SCHEDULER_Task *t;
642 for (t = pending_head; NULL != t; t = t->next)
643 if (GNUNET_YES == t->lifeness)
645 for (t = shutdown_head; NULL != t; t = t->next)
646 if (GNUNET_YES == t->lifeness)
648 for (t = pending_timeout_head; NULL != t; t = t->next)
649 if (GNUNET_YES == t->lifeness)
651 /* No lifeness! Cancel all pending tasks the driver knows about and shutdown */
655 struct GNUNET_SCHEDULER_Task *next = t->next;
656 GNUNET_SCHEDULER_cancel (t);
659 GNUNET_SCHEDULER_shutdown ();
664 * Initialize and run scheduler. This function will return when all
665 * tasks have completed. On systems with signals, receiving a SIGTERM
666 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown()
667 * to be run after the active task is complete. As a result, SIGTERM
668 * causes all active tasks to be scheduled with reason
669 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
670 * afterwards will execute normally!). Note that any particular signal
671 * will only shut down one scheduler; applications should always only
672 * create a single scheduler.
674 * @param task task to run immediately
675 * @param task_cls closure of @a task
678 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
681 struct GNUNET_SCHEDULER_Driver *driver;
682 struct DriverContext context = {.scheduled_head = NULL,
683 .scheduled_tail = NULL,
684 .timeout = GNUNET_TIME_UNIT_FOREVER_REL};
686 driver = GNUNET_SCHEDULER_driver_select ();
687 driver->cls = &context;
689 GNUNET_SCHEDULER_run_with_driver (driver, task, task_cls);
691 GNUNET_free (driver);
696 * Obtain the task context, giving the reason why the current task was
699 * @return current tasks' scheduler context
701 const struct GNUNET_SCHEDULER_TaskContext *
702 GNUNET_SCHEDULER_get_task_context ()
704 GNUNET_assert (NULL != active_task);
710 * Get information about the current load of this scheduler. Use this
711 * function to determine if an elective task should be added or simply
712 * dropped (if the decision should be made based on the number of
713 * tasks ready to run).
715 * @param p priority level to look at
716 * @return number of tasks pending right now
719 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
721 struct GNUNET_SCHEDULER_Task *pos;
724 GNUNET_assert (NULL != active_task);
725 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
727 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
728 p = current_priority;
730 for (pos = ready_head[check_priority (p)]; NULL != pos; pos = pos->next)
737 init_fd_info (struct GNUNET_SCHEDULER_Task *t,
738 const struct GNUNET_NETWORK_Handle *const *read_nh,
739 unsigned int read_nh_len,
740 const struct GNUNET_NETWORK_Handle *const *write_nh,
741 unsigned int write_nh_len,
742 const struct GNUNET_DISK_FileHandle *const *read_fh,
743 unsigned int read_fh_len,
744 const struct GNUNET_DISK_FileHandle *const *write_fh,
745 unsigned int write_fh_len)
747 // FIXME: if we have exactly two network handles / exactly two file handles
748 // and they are equal, we can make one FdInfo with both
749 // GNUNET_SCHEDULER_ET_IN and GNUNET_SCHEDULER_ET_OUT set.
750 struct GNUNET_SCHEDULER_FdInfo *fdi;
752 t->fds_len = read_nh_len + write_nh_len + read_fh_len + write_fh_len;
757 if (1 == read_nh_len)
759 GNUNET_assert (NULL != read_nh);
760 GNUNET_assert (NULL != *read_nh);
762 fdi->et = GNUNET_SCHEDULER_ET_IN;
763 fdi->sock = GNUNET_NETWORK_get_fd (*read_nh);
764 t->read_fd = fdi->sock;
767 else if (1 == write_nh_len)
769 GNUNET_assert (NULL != write_nh);
770 GNUNET_assert (NULL != *write_nh);
772 fdi->et = GNUNET_SCHEDULER_ET_OUT;
773 fdi->sock = GNUNET_NETWORK_get_fd (*write_nh);
775 t->write_fd = fdi->sock;
777 else if (1 == read_fh_len)
779 GNUNET_assert (NULL != read_fh);
780 GNUNET_assert (NULL != *read_fh);
782 fdi->et = GNUNET_SCHEDULER_ET_IN;
783 fdi->sock = (*read_fh)->fd; // FIXME: does not work under WIN32
784 t->read_fd = fdi->sock;
789 GNUNET_assert (NULL != write_fh);
790 GNUNET_assert (NULL != *write_fh);
792 fdi->et = GNUNET_SCHEDULER_ET_OUT;
793 fdi->sock = (*write_fh)->fd; // FIXME: does not work under WIN32
795 t->write_fd = fdi->sock;
800 fdi = GNUNET_new_array (t->fds_len, struct GNUNET_SCHEDULER_FdInfo);
805 for (i = 0; i != read_nh_len; ++i)
807 fdi->fd = read_nh[i];
808 GNUNET_assert (NULL != fdi->fd);
809 fdi->et = GNUNET_SCHEDULER_ET_IN;
810 fdi->sock = GNUNET_NETWORK_get_fd (read_nh[i]);
813 for (i = 0; i != write_nh_len; ++i)
815 fdi->fd = write_nh[i];
816 GNUNET_assert (NULL != fdi->fd);
817 fdi->et = GNUNET_SCHEDULER_ET_OUT;
818 fdi->sock = GNUNET_NETWORK_get_fd (write_nh[i]);
821 for (i = 0; i != read_fh_len; ++i)
823 fdi->fh = read_fh[i];
824 GNUNET_assert (NULL != fdi->fh);
825 fdi->et = GNUNET_SCHEDULER_ET_IN;
826 fdi->sock = (read_fh[i])->fd; // FIXME: does not work under WIN32
829 for (i = 0; i != write_fh_len; ++i)
831 fdi->fh = write_fh[i];
832 GNUNET_assert (NULL != fdi->fh);
833 fdi->et = GNUNET_SCHEDULER_ET_OUT;
834 fdi->sock = (write_fh[i])->fd; // FIXME: does not work under WIN32
842 * calls the given function @a func on each FdInfo related to @a t.
843 * Optionally updates the event type field in each FdInfo after calling
847 * @param driver_func the function to call with each FdInfo contained in
849 * @param if_not_ready only call @a driver_func on FdInfos that are not
851 * @param et the event type to be set in each FdInfo after calling
852 * @a driver_func on it, or -1 if no updating not desired.
855 driver_add_multiple (struct GNUNET_SCHEDULER_Task *t)
857 struct GNUNET_SCHEDULER_FdInfo *fdi;
858 int success = GNUNET_YES;
860 for (unsigned int i = 0; i != t->fds_len; ++i)
863 success = scheduler_driver->add (scheduler_driver->cls,
866 fdi->et = GNUNET_SCHEDULER_ET_NONE;
868 if (GNUNET_YES != success)
870 LOG (GNUNET_ERROR_TYPE_ERROR,
871 "driver could not add task\n");
877 shutdown_cb (void *cls)
880 const struct GNUNET_DISK_FileHandle *pr;
883 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
884 GNUNET_DISK_PIPE_END_READ);
885 GNUNET_assert (! GNUNET_DISK_handle_invalid (pr));
886 /* consume the signal */
887 GNUNET_DISK_file_read (pr, &c, sizeof (c));
888 /* mark all active tasks as ready due to shutdown */
889 GNUNET_SCHEDULER_shutdown ();
894 * Cancel the task with the specified identifier.
895 * The task must not yet have run. Only allowed to be called as long as the
896 * scheduler is running (#GNUNET_SCHEDULER_run or
897 * #GNUNET_SCHEDULER_run_with_driver has been called and has not returned yet).
899 * @param task id of the task to cancel
900 * @return original closure of the task
903 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
905 enum GNUNET_SCHEDULER_Priority p;
909 LOG (GNUNET_ERROR_TYPE_DEBUG,
910 "canceling task %p\n",
913 /* scheduler must be running */
914 GNUNET_assert (NULL != scheduler_driver);
915 GNUNET_assert ( (NULL != active_task) ||
916 (GNUNET_NO == task->lifeness) );
917 is_fd_task = (NULL != task->fds);
920 int del_result = scheduler_driver->del (scheduler_driver->cls, task);
921 if (GNUNET_OK != del_result)
923 LOG (GNUNET_ERROR_TYPE_ERROR,
924 "driver could not delete task\n");
928 if (! task->in_ready_list)
932 GNUNET_CONTAINER_DLL_remove (pending_head,
936 else if (GNUNET_YES == task->on_shutdown)
938 GNUNET_CONTAINER_DLL_remove (shutdown_head,
944 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
945 pending_timeout_tail,
947 if (pending_timeout_last == task)
948 pending_timeout_last = NULL;
953 p = check_priority (task->priority);
954 GNUNET_CONTAINER_DLL_remove (ready_head[p],
959 ret = task->callback_cls;
966 * Initialize backtrace data for task @a t
968 * @param t task to initialize
971 init_backtrace (struct GNUNET_SCHEDULER_Task *t)
974 void *backtrace_array[MAX_TRACE_DEPTH];
976 t->num_backtrace_strings
977 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
978 t->backtrace_strings =
979 backtrace_symbols (backtrace_array,
980 t->num_backtrace_strings);
989 * Continue the current execution with the given function. This is
990 * similar to the other "add" functions except that there is no delay
991 * and the reason code can be specified.
993 * @param task main function of the task
994 * @param task_cls closure for @a task
995 * @param reason reason for task invocation
996 * @param priority priority to use for the task
999 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
1001 enum GNUNET_SCHEDULER_Reason reason,
1002 enum GNUNET_SCHEDULER_Priority priority)
1004 struct GNUNET_SCHEDULER_Task *t;
1006 GNUNET_assert (NULL != task);
1007 GNUNET_assert ((NULL != active_task) ||
1008 (GNUNET_SCHEDULER_REASON_STARTUP == reason));
1009 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1013 t->callback_cls = task_cls;
1015 t->start_time = GNUNET_TIME_absolute_get ();
1018 t->priority = priority;
1019 t->lifeness = current_lifeness;
1020 LOG (GNUNET_ERROR_TYPE_DEBUG,
1021 "Adding continuation task %p\n",
1024 queue_ready_task (t);
1029 * Schedule a new task to be run at the specified time. The task
1030 * will be scheduled for execution at time @a at.
1032 * @param at time when the operation should run
1033 * @param priority priority to use for the task
1034 * @param task main function of the task
1035 * @param task_cls closure of @a task
1036 * @return unique task identifier for the job
1037 * only valid until @a task is started!
1039 struct GNUNET_SCHEDULER_Task *
1040 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1041 enum GNUNET_SCHEDULER_Priority priority,
1042 GNUNET_SCHEDULER_TaskCallback task,
1045 struct GNUNET_SCHEDULER_Task *t;
1046 struct GNUNET_SCHEDULER_Task *pos;
1047 struct GNUNET_SCHEDULER_Task *prev;
1049 GNUNET_assert (NULL != active_task);
1050 GNUNET_assert (NULL != task);
1051 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1053 t->callback_cls = task_cls;
1057 t->start_time = GNUNET_TIME_absolute_get ();
1060 t->priority = priority;
1061 t->lifeness = current_lifeness;
1062 /* try tail first (optimization in case we are
1063 * appending to a long list of tasks with timeouts) */
1064 if ( (NULL == pending_timeout_head) ||
1065 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us) )
1067 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1068 pending_timeout_tail,
1073 /* first move from heuristic start backwards to before start time */
1074 prev = pending_timeout_last;
1075 while ( (NULL != prev) &&
1076 (prev->timeout.abs_value_us > t->timeout.abs_value_us) )
1078 /* now, move from heuristic start (or head of list) forward to insertion point */
1080 pos = pending_timeout_head;
1083 while ( (NULL != pos) &&
1084 ( (pos->timeout.abs_value_us <= t->timeout.abs_value_us) ||
1085 (0 != pos->reason) ) )
1090 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1091 pending_timeout_tail,
1095 /* finally, update heuristic insertion point to last insertion... */
1096 pending_timeout_last = t;
1098 LOG (GNUNET_ERROR_TYPE_DEBUG,
1107 * Schedule a new task to be run with a specified delay. The task
1108 * will be scheduled for execution once the delay has expired.
1110 * @param delay when should this operation time out?
1111 * @param priority priority to use for the task
1112 * @param task main function of the task
1113 * @param task_cls closure of @a task
1114 * @return unique task identifier for the job
1115 * only valid until @a task is started!
1117 struct GNUNET_SCHEDULER_Task *
1118 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1119 enum GNUNET_SCHEDULER_Priority priority,
1120 GNUNET_SCHEDULER_TaskCallback task,
1123 return GNUNET_SCHEDULER_add_at_with_priority (GNUNET_TIME_relative_to_absolute (delay),
1131 * Schedule a new task to be run with a specified priority.
1133 * @param prio how important is the new task?
1134 * @param task main function of the task
1135 * @param task_cls closure of @a task
1136 * @return unique task identifier for the job
1137 * only valid until @a task is started!
1139 struct GNUNET_SCHEDULER_Task *
1140 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1141 GNUNET_SCHEDULER_TaskCallback task,
1144 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1152 * Schedule a new task to be run at the specified time. The task
1153 * will be scheduled for execution once specified time has been
1154 * reached. It will be run with the DEFAULT priority.
1156 * @param at time at which this operation should run
1157 * @param task main function of the task
1158 * @param task_cls closure of @a task
1159 * @return unique task identifier for the job
1160 * only valid until @a task is started!
1162 struct GNUNET_SCHEDULER_Task *
1163 GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1164 GNUNET_SCHEDULER_TaskCallback task,
1167 return GNUNET_SCHEDULER_add_at_with_priority (at,
1168 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1175 * Schedule a new task to be run with a specified delay. The task
1176 * will be scheduled for execution once the delay has expired. It
1177 * will be run with the DEFAULT priority.
1179 * @param delay when should this operation time out?
1180 * @param task main function of the task
1181 * @param task_cls closure of @a task
1182 * @return unique task identifier for the job
1183 * only valid until @a task is started!
1185 struct GNUNET_SCHEDULER_Task *
1186 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1187 GNUNET_SCHEDULER_TaskCallback task,
1190 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1191 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1198 * Schedule a new task to be run as soon as possible. Note that this
1199 * does not guarantee that this will be the next task that is being
1200 * run, as other tasks with higher priority (or that are already ready
1201 * to run) might get to run first. Just as with delays, clients must
1202 * not rely on any particular order of execution between tasks
1203 * scheduled concurrently.
1205 * The task will be run with the DEFAULT priority.
1207 * @param task main function of the task
1208 * @param task_cls closure of @a task
1209 * @return unique task identifier for the job
1210 * only valid until @a task is started!
1212 struct GNUNET_SCHEDULER_Task *
1213 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
1216 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO,
1223 * Schedule a new task to be run on shutdown, that is when a CTRL-C
1224 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
1227 * @param task main function of the task
1228 * @param task_cls closure of @a task
1229 * @return unique task identifier for the job
1230 * only valid until @a task is started!
1232 struct GNUNET_SCHEDULER_Task *
1233 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1236 struct GNUNET_SCHEDULER_Task *t;
1238 GNUNET_assert (NULL != active_task);
1239 GNUNET_assert (NULL != task);
1240 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1242 t->callback_cls = task_cls;
1246 t->start_time = GNUNET_TIME_absolute_get ();
1248 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1249 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1250 t->on_shutdown = GNUNET_YES;
1251 t->lifeness = GNUNET_NO;
1252 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1255 LOG (GNUNET_ERROR_TYPE_DEBUG,
1256 "Adding shutdown task %p\n",
1264 * Schedule a new task to be run as soon as possible with the
1265 * (transitive) ignore-shutdown flag either explicitly set or
1266 * explicitly enabled. This task (and all tasks created from it,
1267 * other than by another call to this function) will either count or
1268 * not count for the "lifeness" of the process. This API is only
1269 * useful in a few special cases.
1271 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
1272 * @param task main function of the task
1273 * @param task_cls closure of @a task
1274 * @return unique task identifier for the job
1275 * only valid until @a task is started!
1277 struct GNUNET_SCHEDULER_Task *
1278 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1279 GNUNET_SCHEDULER_TaskCallback task,
1282 struct GNUNET_SCHEDULER_Task *ret;
1284 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1285 ret->lifeness = lifeness;
1292 * check a raw file descriptor and abort if it is bad (for debugging purposes)
1294 * @param t the task related to the file descriptor
1295 * @param raw_fd the raw file descriptor to check
1298 check_fd (struct GNUNET_SCHEDULER_Task *t, int raw_fd)
1302 int flags = fcntl (raw_fd, F_GETFD);
1304 if ((flags == -1) && (errno == EBADF))
1306 LOG (GNUNET_ERROR_TYPE_ERROR,
1307 "Got invalid file descriptor %d!\n",
1318 * Schedule a new task to be run with a specified delay or when any of
1319 * the specified file descriptor sets is ready. The delay can be used
1320 * as a timeout on the socket(s) being ready. The task will be
1321 * scheduled for execution once either the delay has expired or any of
1322 * the socket operations is ready. This is the most general
1323 * function of the "add" family. Note that the "prerequisite_task"
1324 * must be satisfied in addition to any of the other conditions. In
1325 * other words, the task will be started when
1327 * (prerequisite-run)
1333 * @param delay how long should we wait?
1334 * @param priority priority to use
1335 * @param rfd file descriptor we want to read (can be -1)
1336 * @param wfd file descriptors we want to write (can be -1)
1337 * @param task main function of the task
1338 * @param task_cls closure of @a task
1339 * @return unique task identifier for the job
1340 * only valid until @a task is started!
1343 static struct GNUNET_SCHEDULER_Task *
1344 add_without_sets (struct GNUNET_TIME_Relative delay,
1345 enum GNUNET_SCHEDULER_Priority priority,
1346 const struct GNUNET_NETWORK_Handle *read_nh,
1347 const struct GNUNET_NETWORK_Handle *write_nh,
1348 const struct GNUNET_DISK_FileHandle *read_fh,
1349 const struct GNUNET_DISK_FileHandle *write_fh,
1350 GNUNET_SCHEDULER_TaskCallback task,
1353 struct GNUNET_SCHEDULER_Task *t;
1355 GNUNET_assert (NULL != active_task);
1356 GNUNET_assert (NULL != task);
1357 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1368 t->callback_cls = task_cls;
1370 check_fd (t, NULL != read_nh ? GNUNET_NETWORK_get_fd (read_nh) : -1);
1371 check_fd (t, NULL != write_nh ? GNUNET_NETWORK_get_fd (write_nh) : -1);
1372 check_fd (t, NULL != read_fh ? read_fh->fd : -1);
1373 check_fd (t, NULL != write_fh ? write_fh->fd : -1);
1376 t->start_time = GNUNET_TIME_absolute_get ();
1378 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1379 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1380 t->lifeness = current_lifeness;
1381 GNUNET_CONTAINER_DLL_insert (pending_head,
1384 driver_add_multiple (t);
1385 max_priority_added = GNUNET_MAX (max_priority_added,
1394 * Schedule a new task to be run with a specified delay or when the
1395 * specified file descriptor is ready for reading. The delay can be
1396 * used as a timeout on the socket being ready. The task will be
1397 * scheduled for execution once either the delay has expired or the
1398 * socket operation is ready. It will be run with the DEFAULT priority.
1399 * Only allowed to be called as long as the scheduler is running
1400 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1401 * called and has not returned yet).
1403 * @param delay when should this operation time out?
1404 * @param rfd read file-descriptor
1405 * @param task main function of the task
1406 * @param task_cls closure of @a task
1407 * @return unique task identifier for the job
1408 * only valid until @a task is started!
1410 struct GNUNET_SCHEDULER_Task *
1411 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1412 struct GNUNET_NETWORK_Handle *rfd,
1413 GNUNET_SCHEDULER_TaskCallback task,
1416 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1417 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1418 rfd, task, task_cls);
1423 * Schedule a new task to be run with a specified priority and to be
1424 * run after the specified delay or when the specified file descriptor
1425 * is ready for reading. The delay can be used as a timeout on the
1426 * socket being ready. The task will be scheduled for execution once
1427 * either the delay has expired or the socket operation is ready. It
1428 * will be run with the DEFAULT priority.
1429 * Only allowed to be called as long as the scheduler is running
1430 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1431 * called and has not returned yet).
1433 * @param delay when should this operation time out?
1434 * @param priority priority to use for the task
1435 * @param rfd read file-descriptor
1436 * @param task main function of the task
1437 * @param task_cls closure of @a task
1438 * @return unique task identifier for the job
1439 * only valid until @a task is started!
1441 struct GNUNET_SCHEDULER_Task *
1442 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1443 enum GNUNET_SCHEDULER_Priority priority,
1444 struct GNUNET_NETWORK_Handle *rfd,
1445 GNUNET_SCHEDULER_TaskCallback task,
1448 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1457 * Schedule a new task to be run with a specified delay or when the
1458 * specified file descriptor is ready for writing. The delay can be
1459 * used as a timeout on the socket being ready. The task will be
1460 * scheduled for execution once either the delay has expired or the
1461 * socket operation is ready. It will be run with the priority of
1463 * Only allowed to be called as long as the scheduler is running
1464 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1465 * called and has not returned yet).
1467 * @param delay when should this operation time out?
1468 * @param wfd write file-descriptor
1469 * @param task main function of the task
1470 * @param task_cls closure of @a task
1471 * @return unique task identifier for the job
1472 * only valid until @a task is started!
1474 struct GNUNET_SCHEDULER_Task *
1475 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1476 struct GNUNET_NETWORK_Handle *wfd,
1477 GNUNET_SCHEDULER_TaskCallback task,
1480 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1481 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1483 GNUNET_NO, GNUNET_YES,
1488 * Schedule a new task to be run with a specified delay or when the
1489 * specified file descriptor is ready. The delay can be
1490 * used as a timeout on the socket being ready. The task will be
1491 * scheduled for execution once either the delay has expired or the
1492 * socket operation is ready.
1493 * Only allowed to be called as long as the scheduler is running
1494 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1495 * called and has not returned yet).
1497 * @param delay when should this operation time out?
1498 * @param priority priority of the task
1499 * @param fd file-descriptor
1500 * @param on_read whether to poll the file-descriptor for readability
1501 * @param on_write whether to poll the file-descriptor for writability
1502 * @param task main function of the task
1503 * @param task_cls closure of task
1504 * @return unique task identifier for the job
1505 * only valid until "task" is started!
1507 struct GNUNET_SCHEDULER_Task *
1508 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1509 enum GNUNET_SCHEDULER_Priority priority,
1510 struct GNUNET_NETWORK_Handle *fd,
1513 GNUNET_SCHEDULER_TaskCallback task,
1516 /* scheduler must be running */
1517 GNUNET_assert (NULL != scheduler_driver);
1520 struct GNUNET_NETWORK_FDSet *s;
1521 struct GNUNET_SCHEDULER_Task * ret;
1523 GNUNET_assert (NULL != fd);
1524 s = GNUNET_NETWORK_fdset_create ();
1525 GNUNET_NETWORK_fdset_set (s, fd);
1526 ret = GNUNET_SCHEDULER_add_select (
1529 on_write ? s : NULL,
1531 GNUNET_NETWORK_fdset_destroy (s);
1534 GNUNET_assert (on_read || on_write);
1535 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1536 return add_without_sets (delay, priority,
1537 on_read ? fd : NULL,
1538 on_write ? fd : NULL,
1547 * Schedule a new task to be run with a specified delay or when the
1548 * specified file descriptor is ready for reading. The delay can be
1549 * used as a timeout on the socket being ready. The task will be
1550 * scheduled for execution once either the delay has expired or the
1551 * socket operation is ready. It will be run with the DEFAULT priority.
1552 * Only allowed to be called as long as the scheduler is running
1553 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1554 * called and has not returned yet).
1556 * @param delay when should this operation time out?
1557 * @param rfd read file-descriptor
1558 * @param task main function of the task
1559 * @param task_cls closure of @a task
1560 * @return unique task identifier for the job
1561 * only valid until @a task is started!
1563 struct GNUNET_SCHEDULER_Task *
1564 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1565 const struct GNUNET_DISK_FileHandle *rfd,
1566 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1568 return GNUNET_SCHEDULER_add_file_with_priority (
1569 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1570 rfd, GNUNET_YES, GNUNET_NO,
1576 * Schedule a new task to be run with a specified delay or when the
1577 * specified file descriptor is ready for writing. The delay can be
1578 * used as a timeout on the socket being ready. The task will be
1579 * scheduled for execution once either the delay has expired or the
1580 * socket operation is ready. It will be run with the DEFAULT priority.
1581 * Only allowed to be called as long as the scheduler is running
1582 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1583 * called and has not returned yet).
1585 * @param delay when should this operation time out?
1586 * @param wfd write file-descriptor
1587 * @param task main function of the task
1588 * @param task_cls closure of @a task
1589 * @return unique task identifier for the job
1590 * only valid until @a task is started!
1592 struct GNUNET_SCHEDULER_Task *
1593 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1594 const struct GNUNET_DISK_FileHandle *wfd,
1595 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1597 return GNUNET_SCHEDULER_add_file_with_priority (
1598 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1599 wfd, GNUNET_NO, GNUNET_YES,
1605 * Schedule a new task to be run with a specified delay or when the
1606 * specified file descriptor is ready. The delay can be
1607 * used as a timeout on the socket being ready. The task will be
1608 * scheduled for execution once either the delay has expired or the
1609 * socket operation is ready.
1610 * Only allowed to be called as long as the scheduler is running
1611 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1612 * called and has not returned yet).
1614 * @param delay when should this operation time out?
1615 * @param priority priority of the task
1616 * @param fd file-descriptor
1617 * @param on_read whether to poll the file-descriptor for readability
1618 * @param on_write whether to poll the file-descriptor for writability
1619 * @param task main function of the task
1620 * @param task_cls closure of @a task
1621 * @return unique task identifier for the job
1622 * only valid until @a task is started!
1624 struct GNUNET_SCHEDULER_Task *
1625 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1626 enum GNUNET_SCHEDULER_Priority priority,
1627 const struct GNUNET_DISK_FileHandle *fd,
1628 int on_read, int on_write,
1629 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1631 /* scheduler must be running */
1632 GNUNET_assert (NULL != scheduler_driver);
1635 struct GNUNET_NETWORK_FDSet *s;
1636 struct GNUNET_SCHEDULER_Task * ret;
1638 GNUNET_assert (NULL != fd);
1639 s = GNUNET_NETWORK_fdset_create ();
1640 GNUNET_NETWORK_fdset_handle_set (s, fd);
1641 ret = GNUNET_SCHEDULER_add_select (
1644 on_write ? s : NULL,
1646 GNUNET_NETWORK_fdset_destroy (s);
1649 GNUNET_assert (on_read || on_write);
1650 GNUNET_assert (fd->fd >= 0);
1651 return add_without_sets (delay, priority,
1654 on_read ? fd : NULL,
1655 on_write ? fd : NULL,
1662 extract_handles (struct GNUNET_SCHEDULER_Task *t,
1663 const struct GNUNET_NETWORK_FDSet *fdset,
1664 const struct GNUNET_NETWORK_Handle ***ntarget,
1665 unsigned int *extracted_nhandles,
1666 const struct GNUNET_DISK_FileHandle ***ftarget,
1667 unsigned int *extracted_fhandles)
1669 // FIXME: this implementation only works for unix, for WIN32 the file handles
1670 // in fdset must be handled separately
1671 const struct GNUNET_NETWORK_Handle **nhandles;
1672 const struct GNUNET_DISK_FileHandle **fhandles;
1673 unsigned int nhandles_len;
1674 unsigned int fhandles_len;
1681 for (int sock = 0; sock != fdset->nsds; ++sock)
1683 if (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (fdset, sock))
1685 struct GNUNET_NETWORK_Handle *nhandle;
1686 struct GNUNET_DISK_FileHandle *fhandle;
1688 nhandle = GNUNET_NETWORK_socket_box_native (sock);
1689 if (NULL != nhandle)
1691 GNUNET_array_append (nhandles, nhandles_len, nhandle);
1695 fhandle = GNUNET_DISK_get_handle_from_int_fd (sock);
1696 if (NULL != fhandle)
1698 GNUNET_array_append (fhandles, fhandles_len, fhandle);
1707 *ntarget = nhandles_len > 0 ? nhandles : NULL;
1708 *ftarget = fhandles_len > 0 ? fhandles : NULL;
1709 *extracted_nhandles = nhandles_len;
1710 *extracted_fhandles = fhandles_len;
1715 * Schedule a new task to be run with a specified delay or when any of
1716 * the specified file descriptor sets is ready. The delay can be used
1717 * as a timeout on the socket(s) being ready. The task will be
1718 * scheduled for execution once either the delay has expired or any of
1719 * the socket operations is ready. This is the most general
1720 * function of the "add" family. Note that the "prerequisite_task"
1721 * must be satisfied in addition to any of the other conditions. In
1722 * other words, the task will be started when
1724 * (prerequisite-run)
1727 * || any-ws-ready) )
1729 * Only allowed to be called as long as the scheduler is running
1730 * (#GNUNET_SCHEDULER_run or #GNUNET_SCHEDULER_run_with_driver has been
1731 * called and has not returned yet).
1733 * @param prio how important is this task?
1734 * @param delay how long should we wait?
1735 * @param rs set of file descriptors we want to read (can be NULL)
1736 * @param ws set of file descriptors we want to write (can be NULL)
1737 * @param task main function of the task
1738 * @param task_cls closure of @a task
1739 * @return unique task identifier for the job
1740 * only valid until @a task is started!
1742 struct GNUNET_SCHEDULER_Task *
1743 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1744 struct GNUNET_TIME_Relative delay,
1745 const struct GNUNET_NETWORK_FDSet *rs,
1746 const struct GNUNET_NETWORK_FDSet *ws,
1747 GNUNET_SCHEDULER_TaskCallback task,
1750 struct GNUNET_SCHEDULER_Task *t;
1751 const struct GNUNET_NETWORK_Handle **read_nhandles;
1752 const struct GNUNET_NETWORK_Handle **write_nhandles;
1753 const struct GNUNET_DISK_FileHandle **read_fhandles;
1754 const struct GNUNET_DISK_FileHandle **write_fhandles;
1755 unsigned int read_nhandles_len, write_nhandles_len,
1756 read_fhandles_len, write_fhandles_len;
1757 int no_fdsets = (NULL == rs) && (NULL == ws);
1758 int no_socket_descriptors =
1759 ((NULL != rs) && (0 == rs->nsds)) && ((NULL != ws) && (0 == ws->nsds));
1761 if (no_fdsets || no_socket_descriptors)
1762 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1766 /* scheduler must be running */
1767 GNUNET_assert (NULL != scheduler_driver);
1768 GNUNET_assert (NULL != active_task);
1769 GNUNET_assert (NULL != task);
1770 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1772 t->callback_cls = task_cls;
1775 t->own_handles = GNUNET_YES;
1776 read_nhandles = NULL;
1777 write_nhandles = NULL;
1778 read_fhandles = NULL;
1779 write_fhandles = NULL;
1780 read_nhandles_len = 0;
1781 write_nhandles_len = 0;
1782 read_fhandles_len = 0;
1783 write_fhandles_len = 0;
1791 &read_fhandles_len);
1798 &write_nhandles_len,
1800 &write_fhandles_len);
1810 write_fhandles_len);
1811 /* free the arrays of pointers to network / file handles, the actual
1812 * handles will be freed in destroy_task */
1813 GNUNET_array_grow (read_nhandles, read_nhandles_len, 0);
1814 GNUNET_array_grow (write_nhandles, write_nhandles_len, 0);
1815 GNUNET_array_grow (read_fhandles, read_fhandles_len, 0);
1816 GNUNET_array_grow (write_fhandles, write_fhandles_len, 0);
1818 t->start_time = GNUNET_TIME_absolute_get ();
1820 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1822 check_priority ((prio ==
1823 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1825 t->lifeness = current_lifeness;
1826 GNUNET_CONTAINER_DLL_insert (pending_head,
1829 driver_add_multiple (t);
1830 max_priority_added = GNUNET_MAX (max_priority_added,
1832 LOG (GNUNET_ERROR_TYPE_DEBUG,
1841 * Function used by event-loop implementations to signal the scheduler
1842 * that a particular @a task is ready due to an event specified in the
1843 * et field of @a fdi.
1845 * This function will then queue the task to notify the application
1846 * that the task is ready (with the respective priority).
1848 * @param task the task that is ready
1849 * @param fdi information about the related FD
1852 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1853 struct GNUNET_SCHEDULER_FdInfo *fdi)
1855 enum GNUNET_SCHEDULER_Reason reason;
1856 struct GNUNET_TIME_Absolute now;
1858 now = GNUNET_TIME_absolute_get ();
1859 reason = task->reason;
1860 if (now.abs_value_us >= task->timeout.abs_value_us)
1861 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1862 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1863 (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et)) )
1864 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
1865 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
1866 (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et)) )
1867 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
1868 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1869 task->reason = reason;
1870 if (GNUNET_NO == task->in_ready_list)
1872 GNUNET_CONTAINER_DLL_remove (pending_head,
1875 queue_ready_task (task);
1881 * Function called by the driver to tell the scheduler to run some of
1882 * the tasks that are ready. This function may return even though
1883 * there are tasks left to run just to give other tasks a chance as
1884 * well. If we return #GNUNET_YES, the driver should call this
1885 * function again as soon as possible, while if we return #GNUNET_NO
1886 * it must block until either the operating system has more work (the
1887 * scheduler has no more work to do right now) or the timeout set by
1888 * the scheduler (using the set_wakeup callback) is reached.
1890 * @param sh scheduler handle that was given to the `loop`
1891 * @return #GNUNET_OK if there are more tasks that are ready,
1892 * and thus we would like to run more (yield to avoid
1893 * blocking other activities for too long)
1894 * #GNUNET_NO if we are done running tasks (yield to block)
1895 * #GNUNET_SYSERR on error, e.g. no tasks were ready
1898 GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh)
1900 enum GNUNET_SCHEDULER_Priority p;
1901 struct GNUNET_SCHEDULER_Task *pos;
1902 struct GNUNET_TIME_Absolute now;
1904 /* check for tasks that reached the timeout! */
1905 now = GNUNET_TIME_absolute_get ();
1906 while (NULL != (pos = pending_timeout_head))
1908 if (now.abs_value_us >= pos->timeout.abs_value_us)
1909 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1910 if (0 == pos->reason)
1912 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1913 pending_timeout_tail,
1915 if (pending_timeout_last == pos)
1916 pending_timeout_last = NULL;
1917 queue_ready_task (pos);
1922 struct GNUNET_SCHEDULER_Task *next = pos->next;
1923 if (now.abs_value_us >= pos->timeout.abs_value_us)
1925 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1926 GNUNET_CONTAINER_DLL_remove (pending_head,
1929 queue_ready_task (pos);
1934 if (0 == ready_count)
1936 LOG (GNUNET_ERROR_TYPE_ERROR,
1937 "GNUNET_SCHEDULER_run_from_driver was called, but no tasks are ready!\n");
1938 return GNUNET_SYSERR;
1941 /* find out which task priority level we are going to
1942 process this time */
1943 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
1944 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
1945 /* yes, p>0 is correct, 0 is "KEEP" which should
1946 * always be an empty queue (see assertion)! */
1947 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
1949 pos = ready_head[p];
1953 GNUNET_assert (NULL != pos); /* ready_count wrong? */
1955 /* process all tasks at this priority level, then yield */
1956 while (NULL != (pos = ready_head[p]))
1958 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1962 current_priority = pos->priority;
1963 current_lifeness = pos->lifeness;
1966 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
1967 DELAY_THRESHOLD.rel_value_us)
1969 LOG (GNUNET_ERROR_TYPE_DEBUG,
1970 "Task %p took %s to be scheduled\n",
1972 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
1976 tc.reason = pos->reason;
1977 GNUNET_NETWORK_fdset_zero (sh->rs);
1978 GNUNET_NETWORK_fdset_zero (sh->ws);
1979 // FIXME: do we have to remove FdInfos from fds if they are not ready?
1980 tc.fds_len = pos->fds_len;
1982 for (unsigned int i = 0; i != pos->fds_len; ++i)
1984 struct GNUNET_SCHEDULER_FdInfo *fdi = &pos->fds[i];
1985 if (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et))
1987 GNUNET_NETWORK_fdset_set_native (sh->rs,
1990 if (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et))
1992 GNUNET_NETWORK_fdset_set_native (sh->ws,
1996 tc.read_ready = sh->rs;
1997 tc.write_ready = sh->ws;
1998 LOG (GNUNET_ERROR_TYPE_DEBUG,
1999 "Running task %p\n",
2001 GNUNET_assert (NULL != pos->callback);
2002 pos->callback (pos->callback_cls);
2003 if (NULL != pos->fds)
2005 int del_result = scheduler_driver->del (scheduler_driver->cls, pos);
2006 if (GNUNET_OK != del_result)
2008 LOG (GNUNET_ERROR_TYPE_ERROR,
2009 "driver could not delete task %p\n", pos);
2014 dump_backtrace (pos);
2018 shutdown_if_no_lifeness ();
2019 if (0 == ready_count)
2021 scheduler_driver->set_wakeup (scheduler_driver->cls,
2025 scheduler_driver->set_wakeup (scheduler_driver->cls,
2026 GNUNET_TIME_absolute_get ());
2032 * Initialize and run scheduler. This function will return when all
2033 * tasks have completed. On systems with signals, receiving a SIGTERM
2034 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
2035 * to be run after the active task is complete. As a result, SIGTERM
2036 * causes all shutdown tasks to be scheduled with reason
2037 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
2038 * afterwards will execute normally!). Note that any particular
2039 * signal will only shut down one scheduler; applications should
2040 * always only create a single scheduler.
2042 * @param driver drive to use for the event loop
2043 * @param task task to run first (and immediately)
2044 * @param task_cls closure of @a task
2045 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2048 GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
2049 GNUNET_SCHEDULER_TaskCallback task,
2053 struct GNUNET_SIGNAL_Context *shc_int;
2054 struct GNUNET_SIGNAL_Context *shc_term;
2055 #if (SIGTERM != GNUNET_TERM_SIG)
2056 struct GNUNET_SIGNAL_Context *shc_gterm;
2059 struct GNUNET_SIGNAL_Context *shc_quit;
2060 struct GNUNET_SIGNAL_Context *shc_hup;
2061 struct GNUNET_SIGNAL_Context *shc_pipe;
2063 struct GNUNET_SCHEDULER_Task tsk;
2064 const struct GNUNET_DISK_FileHandle *pr;
2066 /* general set-up */
2067 GNUNET_assert (NULL == active_task);
2068 GNUNET_assert (NULL == shutdown_pipe_handle);
2069 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
2073 GNUNET_assert (NULL != shutdown_pipe_handle);
2074 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
2075 GNUNET_DISK_PIPE_END_READ);
2077 scheduler_driver = driver;
2079 /* install signal handlers */
2080 LOG (GNUNET_ERROR_TYPE_DEBUG,
2081 "Registering signal handlers\n");
2082 shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2083 &sighandler_shutdown);
2084 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2085 &sighandler_shutdown);
2086 #if (SIGTERM != GNUNET_TERM_SIG)
2087 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2088 &sighandler_shutdown);
2091 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2093 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2094 &sighandler_shutdown);
2095 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2096 &sighandler_shutdown);
2099 /* Setup initial tasks */
2100 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2101 current_lifeness = GNUNET_NO;
2106 GNUNET_SCHEDULER_add_now (&GNUNET_OS_install_parent_control_handler,
2108 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2112 current_lifeness = GNUNET_YES;
2113 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
2115 GNUNET_SCHEDULER_REASON_STARTUP,
2116 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
2118 scheduler_driver->set_wakeup (scheduler_driver->cls,
2120 /* begin main event loop */
2121 sh.rs = GNUNET_NETWORK_fdset_create ();
2122 sh.ws = GNUNET_NETWORK_fdset_create ();
2123 GNUNET_NETWORK_fdset_handle_set (sh.rs, pr);
2124 ret = driver->loop (driver->cls,
2126 GNUNET_NETWORK_fdset_destroy (sh.rs);
2127 GNUNET_NETWORK_fdset_destroy (sh.ws);
2129 /* uninstall signal handlers */
2130 GNUNET_SIGNAL_handler_uninstall (shc_int);
2131 GNUNET_SIGNAL_handler_uninstall (shc_term);
2132 #if (SIGTERM != GNUNET_TERM_SIG)
2133 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
2136 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
2137 GNUNET_SIGNAL_handler_uninstall (shc_quit);
2138 GNUNET_SIGNAL_handler_uninstall (shc_hup);
2140 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2141 shutdown_pipe_handle = NULL;
2142 scheduler_driver = NULL;
2148 select_add (void *cls,
2149 struct GNUNET_SCHEDULER_Task *task,
2150 struct GNUNET_SCHEDULER_FdInfo *fdi)
2152 struct DriverContext *context = cls;
2153 GNUNET_assert (NULL != context);
2154 GNUNET_assert (NULL != task);
2155 GNUNET_assert (NULL != fdi);
2156 GNUNET_assert (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et) ||
2157 0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et));
2159 if (!((NULL != fdi->fd) ^ (NULL != fdi->fh)) || (fdi->sock < 0))
2161 /* exactly one out of {fd, hf} must be != NULL and the OS handle must be valid */
2162 return GNUNET_SYSERR;
2165 struct Scheduled *scheduled = GNUNET_new (struct Scheduled);
2166 scheduled->task = task;
2167 scheduled->fdi = fdi;
2168 scheduled->et = fdi->et;
2170 GNUNET_CONTAINER_DLL_insert (context->scheduled_head,
2171 context->scheduled_tail,
2178 select_del (void *cls,
2179 struct GNUNET_SCHEDULER_Task *task)
2181 struct DriverContext *context;
2182 struct Scheduled *pos;
2185 GNUNET_assert (NULL != cls);
2188 ret = GNUNET_SYSERR;
2189 pos = context->scheduled_head;
2190 if (0 == task->fds_len)
2194 struct Scheduled *next = pos->next;
2195 if (pos->task == task)
2197 GNUNET_CONTAINER_DLL_remove (context->scheduled_head,
2198 context->scheduled_tail,
2210 select_loop (void *cls,
2211 struct GNUNET_SCHEDULER_Handle *sh)
2213 struct GNUNET_NETWORK_FDSet *rs;
2214 struct GNUNET_NETWORK_FDSet *ws;
2215 struct DriverContext *context;
2220 GNUNET_assert (NULL != context);
2221 rs = GNUNET_NETWORK_fdset_create ();
2222 ws = GNUNET_NETWORK_fdset_create ();
2223 tasks_ready = GNUNET_NO;
2224 while (NULL != context->scheduled_head ||
2225 GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us != context->timeout.rel_value_us)
2227 LOG (GNUNET_ERROR_TYPE_DEBUG,
2228 "select timeout = %s\n",
2229 GNUNET_STRINGS_relative_time_to_string (context->timeout, GNUNET_NO));
2231 GNUNET_NETWORK_fdset_zero (rs);
2232 GNUNET_NETWORK_fdset_zero (ws);
2233 struct Scheduled *pos;
2234 for (pos = context->scheduled_head; NULL != pos; pos = pos->next)
2236 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et))
2238 GNUNET_NETWORK_fdset_set_native (rs, pos->fdi->sock);
2240 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et))
2242 GNUNET_NETWORK_fdset_set_native (ws, pos->fdi->sock);
2245 if (NULL == scheduler_select)
2247 select_result = GNUNET_NETWORK_socket_select (rs,
2254 select_result = scheduler_select (scheduler_select_cls,
2260 if (select_result == GNUNET_SYSERR)
2265 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
2270 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
2273 if (0 != system (lsof))
2274 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
2279 struct Scheduled *s;
2280 for (s = context->scheduled_head; NULL != s; s = s->next)
2282 int flags = fcntl (s->fdi->sock, F_GETFD);
2283 if ((flags == -1) && (errno == EBADF))
2285 LOG (GNUNET_ERROR_TYPE_ERROR,
2286 "Got invalid file descriptor %d!\n",
2292 GNUNET_NETWORK_fdset_destroy (rs);
2293 GNUNET_NETWORK_fdset_destroy (ws);
2294 return GNUNET_SYSERR;
2296 for (pos = context->scheduled_head; NULL != pos; pos = pos->next)
2298 int is_ready = GNUNET_NO;
2299 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et) &&
2300 GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, pos->fdi->sock))
2302 pos->fdi->et |= GNUNET_SCHEDULER_ET_IN;
2303 is_ready = GNUNET_YES;
2305 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et) &&
2306 GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, pos->fdi->sock))
2308 pos->fdi->et |= GNUNET_SCHEDULER_ET_OUT;
2309 is_ready = GNUNET_YES;
2311 if (GNUNET_YES == is_ready)
2313 GNUNET_SCHEDULER_task_ready (pos->task, pos->fdi);
2316 tasks_ready = GNUNET_SCHEDULER_run_from_driver (sh);
2317 GNUNET_assert (GNUNET_SYSERR != tasks_ready);
2319 GNUNET_NETWORK_fdset_destroy (rs);
2320 GNUNET_NETWORK_fdset_destroy (ws);
2326 select_set_wakeup(void *cls,
2327 struct GNUNET_TIME_Absolute dt)
2329 struct DriverContext *context = cls;
2330 GNUNET_assert (NULL != context);
2332 context->timeout = GNUNET_TIME_absolute_get_remaining (dt);
2337 * Obtain the driver for using select() as the event loop.
2339 * @return NULL on error
2341 struct GNUNET_SCHEDULER_Driver *
2342 GNUNET_SCHEDULER_driver_select ()
2344 struct GNUNET_SCHEDULER_Driver *select_driver;
2345 select_driver = GNUNET_new (struct GNUNET_SCHEDULER_Driver);
2347 select_driver->loop = &select_loop;
2348 select_driver->add = &select_add;
2349 select_driver->del = &select_del;
2350 select_driver->set_wakeup = &select_set_wakeup;
2352 return select_driver;
2356 /* end of scheduler.c */