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;
94 * Driver we used for the event loop.
96 const struct GNUNET_SCHEDULER_Driver *driver;
102 * Entry in list of pending tasks.
104 struct GNUNET_SCHEDULER_Task
107 * This is a linked list.
109 struct GNUNET_SCHEDULER_Task *next;
112 * This is a linked list.
114 struct GNUNET_SCHEDULER_Task *prev;
117 * Function to run when ready.
119 GNUNET_SCHEDULER_TaskCallback callback;
122 * Closure for the @e callback.
127 * Handle to the scheduler's state.
129 const struct GNUNET_SCHEDULER_Handle *sh;
132 * Set of file descriptors this task is waiting
133 * for for reading. Once ready, this is updated
134 * to reflect the set of file descriptors ready
137 struct GNUNET_NETWORK_FDSet *read_set;
140 * Set of file descriptors this task is waiting for for writing.
141 * Once ready, this is updated to reflect the set of file
142 * descriptors ready for operation.
144 struct GNUNET_NETWORK_FDSet *write_set;
147 * Information about which FDs are ready for this task (and why).
149 const struct GNUNET_SCHEDULER_FdInfo *fds;
152 * Storage location used for @e fds if we want to avoid
153 * a separate malloc() call in the common case that this
154 * task is only about a single FD.
156 struct GNUNET_SCHEDULER_FdInfo fdx;
159 * Absolute timeout value for the task, or
160 * #GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
162 struct GNUNET_TIME_Absolute timeout;
166 * When was the task scheduled?
168 struct GNUNET_TIME_Absolute start_time;
172 * Size of the @e fds array.
174 unsigned int fds_len;
177 * Why is the task ready? Set after task is added to ready queue.
178 * Initially set to zero. All reasons that have already been
179 * satisfied (i.e. read or write ready) will be set over time.
181 enum GNUNET_SCHEDULER_Reason reason;
186 enum GNUNET_SCHEDULER_Priority priority;
189 * Set if we only wait for reading from a single FD, otherwise -1.
194 * Set if we only wait for writing to a single FD, otherwise -1.
199 * Should the existence of this task in the queue be counted as
200 * reason to not shutdown the scheduler?
205 * Is this task run on shutdown?
210 * Is this task in the ready list?
216 * Array of strings which make up a backtrace from the point when this
217 * task was scheduled (essentially, who scheduled the task?)
219 char **backtrace_strings;
222 * Size of the backtrace_strings array
224 int num_backtrace_strings;
232 * Head of list of tasks waiting for an event.
234 static struct GNUNET_SCHEDULER_Task *pending_head;
237 * Tail of list of tasks waiting for an event.
239 static struct GNUNET_SCHEDULER_Task *pending_tail;
242 * Head of list of tasks waiting for shutdown.
244 static struct GNUNET_SCHEDULER_Task *shutdown_head;
247 * Tail of list of tasks waiting for shutdown.
249 static struct GNUNET_SCHEDULER_Task *shutdown_tail;
252 * List of tasks waiting ONLY for a timeout event.
253 * Sorted by timeout (earliest first). Used so that
254 * we do not traverse the list of these tasks when
255 * building select sets (we just look at the head
256 * to determine the respective timeout ONCE).
258 static struct GNUNET_SCHEDULER_Task *pending_timeout_head;
261 * List of tasks waiting ONLY for a timeout event.
262 * Sorted by timeout (earliest first). Used so that
263 * we do not traverse the list of these tasks when
264 * building select sets (we just look at the head
265 * to determine the respective timeout ONCE).
267 static struct GNUNET_SCHEDULER_Task *pending_timeout_tail;
270 * Last inserted task waiting ONLY for a timeout event.
271 * Used to (heuristically) speed up insertion.
273 static struct GNUNET_SCHEDULER_Task *pending_timeout_last;
276 * ID of the task that is running right now.
278 static struct GNUNET_SCHEDULER_Task *active_task;
281 * Head of list of tasks ready to run right now, grouped by importance.
283 static struct GNUNET_SCHEDULER_Task *ready_head[GNUNET_SCHEDULER_PRIORITY_COUNT];
286 * Tail of list of tasks ready to run right now, grouped by importance.
288 static struct GNUNET_SCHEDULER_Task *ready_tail[GNUNET_SCHEDULER_PRIORITY_COUNT];
291 * Number of tasks on the ready list.
293 static unsigned int ready_count;
296 * How many tasks have we run so far?
298 static unsigned long long tasks_run;
301 * Priority of the task running right now. Only
302 * valid while a task is running.
304 static enum GNUNET_SCHEDULER_Priority current_priority;
307 * Priority of the highest task added in the current select
310 static enum GNUNET_SCHEDULER_Priority max_priority_added;
313 * Value of the 'lifeness' flag for the current task.
315 static int current_lifeness;
318 * Function to use as a select() in the scheduler.
319 * If NULL, we use GNUNET_NETWORK_socket_select().
321 static GNUNET_SCHEDULER_select scheduler_select;
324 * Task context of the current task.
326 static struct GNUNET_SCHEDULER_TaskContext tc;
329 * Closure for #scheduler_select.
331 static void *scheduler_select_cls;
335 * Sets the select function to use in the scheduler (scheduler_select).
337 * @param new_select new select function to use
338 * @param new_select_cls closure for @a new_select
339 * @return previously used select function, NULL for default
342 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
343 void *new_select_cls)
345 scheduler_select = new_select;
346 scheduler_select_cls = new_select_cls;
351 * Check that the given priority is legal (and return it).
353 * @param p priority value to check
354 * @return p on success, 0 on error
356 static enum GNUNET_SCHEDULER_Priority
357 check_priority (enum GNUNET_SCHEDULER_Priority p)
359 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
362 return 0; /* make compiler happy */
367 * Update all sets and timeout for select.
369 * @param rs read-set, set to all FDs we would like to read (updated)
370 * @param ws write-set, set to all FDs we would like to write (updated)
371 * @param timeout next timeout (updated)
374 update_sets (struct GNUNET_NETWORK_FDSet *rs,
375 struct GNUNET_NETWORK_FDSet *ws,
376 struct GNUNET_TIME_Relative *timeout)
378 struct GNUNET_SCHEDULER_Task *pos;
379 struct GNUNET_TIME_Absolute now;
380 struct GNUNET_TIME_Relative to;
382 now = GNUNET_TIME_absolute_get ();
383 pos = pending_timeout_head;
386 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
387 if (timeout->rel_value_us > to.rel_value_us)
389 if (0 != pos->reason)
390 *timeout = GNUNET_TIME_UNIT_ZERO;
392 for (pos = pending_head; NULL != pos; pos = pos->next)
394 if (pos->timeout.abs_value_us != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
396 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
397 if (timeout->rel_value_us > to.rel_value_us)
400 if (-1 != pos->read_fd)
401 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
402 if (-1 != pos->write_fd)
403 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
404 if (NULL != pos->read_set)
405 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
406 if (NULL != pos->write_set)
407 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
408 if (0 != pos->reason)
409 *timeout = GNUNET_TIME_UNIT_ZERO;
415 * Check if the ready set overlaps with the set we want to have ready.
416 * If so, update the want set (set all FDs that are ready). If not,
419 * @param ready set that is ready
420 * @param want set that we want to be ready
421 * @return #GNUNET_YES if there was some overlap
424 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
425 struct GNUNET_NETWORK_FDSet *want)
427 if ((NULL == want) || (NULL == ready))
429 if (GNUNET_NETWORK_fdset_overlap (ready, want))
431 /* copy all over (yes, there maybe unrelated bits,
432 * but this should not hurt well-written clients) */
433 GNUNET_NETWORK_fdset_copy (want, ready);
441 * Check if the given task is eligible to run now.
442 * Also set the reason why it is eligible.
444 * @param task task to check if it is ready
445 * @param now the current time
446 * @param rs set of FDs ready for reading
447 * @param ws set of FDs ready for writing
448 * @return #GNUNET_YES if we can run it, #GNUNET_NO if not.
451 is_ready (struct GNUNET_SCHEDULER_Task *task,
452 struct GNUNET_TIME_Absolute now,
453 const struct GNUNET_NETWORK_FDSet *rs,
454 const struct GNUNET_NETWORK_FDSet *ws)
456 enum GNUNET_SCHEDULER_Reason reason;
458 reason = task->reason;
459 if (now.abs_value_us >= task->timeout.abs_value_us)
460 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
461 if ((0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
462 (((task->read_fd != -1) &&
463 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd))) ||
464 (set_overlaps (rs, task->read_set))))
465 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
466 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
467 (((task->write_fd != -1) &&
468 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)))
469 || (set_overlaps (ws, task->write_set))))
470 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
472 return GNUNET_NO; /* not ready */
473 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
474 task->reason = reason;
480 * Put a task that is ready for execution into the ready queue.
482 * @param task task ready for execution
485 queue_ready_task (struct GNUNET_SCHEDULER_Task *task)
487 enum GNUNET_SCHEDULER_Priority p = check_priority (task->priority);
489 GNUNET_CONTAINER_DLL_insert (ready_head[p],
492 task->in_ready_list = GNUNET_YES;
498 * Check which tasks are ready and move them
499 * to the respective ready queue.
501 * @param rs FDs ready for reading
502 * @param ws FDs ready for writing
505 check_ready (const struct GNUNET_NETWORK_FDSet *rs,
506 const struct GNUNET_NETWORK_FDSet *ws)
508 struct GNUNET_SCHEDULER_Task *pos;
509 struct GNUNET_SCHEDULER_Task *next;
510 struct GNUNET_TIME_Absolute now;
512 now = GNUNET_TIME_absolute_get ();
513 while (NULL != (pos = pending_timeout_head))
515 if (now.abs_value_us >= pos->timeout.abs_value_us)
516 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
517 if (0 == pos->reason)
519 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
520 pending_timeout_tail,
522 if (pending_timeout_last == pos)
523 pending_timeout_last = NULL;
524 queue_ready_task (pos);
530 if (GNUNET_YES == is_ready (pos, now, rs, ws))
532 GNUNET_CONTAINER_DLL_remove (pending_head,
535 queue_ready_task (pos);
543 * Request the shutdown of a scheduler. Marks all tasks
544 * awaiting shutdown as ready. Note that tasks
545 * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call
546 * will be delayed until the next shutdown signal.
549 GNUNET_SCHEDULER_shutdown ()
551 struct GNUNET_SCHEDULER_Task *pos;
553 while (NULL != (pos = shutdown_head))
555 GNUNET_CONTAINER_DLL_remove (shutdown_head,
558 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
559 queue_ready_task (pos);
565 * Destroy a task (release associated resources)
567 * @param t task to destroy
570 destroy_task (struct GNUNET_SCHEDULER_Task *t)
572 if (NULL != t->read_set)
573 GNUNET_NETWORK_fdset_destroy (t->read_set);
574 if (NULL != t->write_set)
575 GNUNET_NETWORK_fdset_destroy (t->write_set);
577 GNUNET_free (t->backtrace_strings);
584 * Output stack trace of task @a t.
586 * @param t task to dump stack trace of
589 dump_backtrace (struct GNUNET_SCHEDULER_Task *t)
594 for (i = 0; i < t->num_backtrace_strings; i++)
595 LOG (GNUNET_ERROR_TYPE_DEBUG,
596 "Task %p trace %u: %s\n",
599 t->backtrace_strings[i]);
605 * Run at least one task in the highest-priority queue that is not
606 * empty. Keep running tasks until we are either no longer running
607 * "URGENT" tasks or until we have at least one "pending" task (which
608 * may become ready, hence we should select on it). Naturally, if
609 * there are no more ready tasks, we also return.
611 * @param rs FDs ready for reading
612 * @param ws FDs ready for writing
615 run_ready (struct GNUNET_NETWORK_FDSet *rs,
616 struct GNUNET_NETWORK_FDSet *ws)
618 enum GNUNET_SCHEDULER_Priority p;
619 struct GNUNET_SCHEDULER_Task *pos;
621 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
624 if (0 == ready_count)
626 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
627 /* yes, p>0 is correct, 0 is "KEEP" which should
628 * always be an empty queue (see assertion)! */
629 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
635 GNUNET_assert (NULL != pos); /* ready_count wrong? */
636 GNUNET_CONTAINER_DLL_remove (ready_head[p],
640 current_priority = pos->priority;
641 current_lifeness = pos->lifeness;
644 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
645 DELAY_THRESHOLD.rel_value_us)
647 LOG (GNUNET_ERROR_TYPE_DEBUG,
648 "Task %p took %s to be scheduled\n",
650 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
654 tc.reason = pos->reason;
655 tc.read_ready = (NULL == pos->read_set) ? rs : pos->read_set;
656 if ((-1 != pos->read_fd) &&
657 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)))
658 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
659 tc.write_ready = (NULL == pos->write_set) ? ws : pos->write_set;
660 if ((-1 != pos->write_fd) &&
661 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
662 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
663 if ((0 != (tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
664 (-1 != pos->write_fd) &&
665 (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd)))
666 GNUNET_assert (0); // added to ready in previous select loop!
667 LOG (GNUNET_ERROR_TYPE_DEBUG,
668 "Running task: %p\n",
670 pos->callback (pos->callback_cls);
671 dump_backtrace (pos);
676 while ((NULL == pending_head) || (p >= max_priority_added));
681 * Pipe used to communicate shutdown via signal.
683 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
686 * Process ID of this process at the time we installed the various
692 * Signal handler called for SIGPIPE.
704 * Wait for a short time.
705 * Sleeps for @a ms ms (as that should be long enough for virtually all
706 * modern systems to context switch and allow another process to do
709 * @param ms how many ms to wait
712 short_wait (unsigned int ms)
714 struct GNUNET_TIME_Relative timeout;
716 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
717 (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
722 * Signal handler called for signals that should cause us to shutdown.
725 sighandler_shutdown ()
728 int old_errno = errno; /* backup errno */
730 if (getpid () != my_pid)
731 exit (1); /* we have fork'ed since the signal handler was created,
732 * ignore the signal, see https://gnunet.org/vfork discussion */
733 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
734 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
741 * Check if the system is still alive. Trigger shutdown if we
742 * have tasks, but none of them give us lifeness.
744 * @return #GNUNET_OK to continue the main loop,
750 struct GNUNET_SCHEDULER_Task *t;
754 for (t = pending_head; NULL != t; t = t->next)
755 if (t->lifeness == GNUNET_YES)
757 for (t = shutdown_head; NULL != t; t = t->next)
758 if (t->lifeness == GNUNET_YES)
760 for (t = pending_timeout_head; NULL != t; t = t->next)
761 if (t->lifeness == GNUNET_YES)
763 if (NULL != shutdown_head)
765 GNUNET_SCHEDULER_shutdown ();
773 * Initialize and run scheduler. This function will return when all
774 * tasks have completed. On systems with signals, receiving a SIGTERM
775 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown()
776 * to be run after the active task is complete. As a result, SIGTERM
777 * causes all active tasks to be scheduled with reason
778 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
779 * afterwards will execute normally!). Note that any particular signal
780 * will only shut down one scheduler; applications should always only
781 * create a single scheduler.
783 * @param task task to run immediately
784 * @param task_cls closure of @a task
787 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
790 struct GNUNET_NETWORK_FDSet *rs;
791 struct GNUNET_NETWORK_FDSet *ws;
792 struct GNUNET_TIME_Relative timeout;
794 struct GNUNET_SIGNAL_Context *shc_int;
795 struct GNUNET_SIGNAL_Context *shc_term;
796 #if (SIGTERM != GNUNET_TERM_SIG)
797 struct GNUNET_SIGNAL_Context *shc_gterm;
801 struct GNUNET_SIGNAL_Context *shc_quit;
802 struct GNUNET_SIGNAL_Context *shc_hup;
803 struct GNUNET_SIGNAL_Context *shc_pipe;
805 unsigned long long last_tr;
806 unsigned int busy_wait_warning;
807 const struct GNUNET_DISK_FileHandle *pr;
810 GNUNET_assert (NULL == active_task);
811 rs = GNUNET_NETWORK_fdset_create ();
812 ws = GNUNET_NETWORK_fdset_create ();
813 GNUNET_assert (NULL == shutdown_pipe_handle);
814 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
818 GNUNET_assert (NULL != shutdown_pipe_handle);
819 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
820 GNUNET_DISK_PIPE_END_READ);
821 GNUNET_assert (NULL != pr);
823 LOG (GNUNET_ERROR_TYPE_DEBUG,
824 "Registering signal handlers\n");
825 shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
826 &sighandler_shutdown);
827 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
828 &sighandler_shutdown);
829 #if (SIGTERM != GNUNET_TERM_SIG)
830 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
831 &sighandler_shutdown);
834 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
836 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
837 &sighandler_shutdown);
838 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
839 &sighandler_shutdown);
841 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
842 current_lifeness = GNUNET_YES;
843 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
845 GNUNET_SCHEDULER_REASON_STARTUP,
846 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
847 active_task = (void *) (long) -1; /* force passing of sanity check */
848 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
849 &GNUNET_OS_install_parent_control_handler,
853 busy_wait_warning = 0;
854 while (GNUNET_OK == check_lifeness ())
856 GNUNET_NETWORK_fdset_zero (rs);
857 GNUNET_NETWORK_fdset_zero (ws);
858 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
859 update_sets (rs, ws, &timeout);
860 GNUNET_NETWORK_fdset_handle_set (rs, pr);
863 /* no blocking, more work already ready! */
864 timeout = GNUNET_TIME_UNIT_ZERO;
866 if (NULL == scheduler_select)
867 ret = GNUNET_NETWORK_socket_select (rs,
872 ret = scheduler_select (scheduler_select_cls,
877 if (ret == GNUNET_SYSERR)
882 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
887 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
890 if (0 != system (lsof))
891 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
896 struct GNUNET_SCHEDULER_Task *t;
898 for (t = pending_head; NULL != t; t = t->next)
900 if (-1 != t->read_fd)
902 int flags = fcntl (t->read_fd, F_GETFD);
903 if ((flags == -1) && (errno == EBADF))
905 LOG (GNUNET_ERROR_TYPE_ERROR,
906 "Got invalid file descriptor %d!\n",
911 if (-1 != t->write_fd)
913 int flags = fcntl (t->write_fd, F_GETFD);
914 if ((flags == -1) && (errno == EBADF))
916 LOG (GNUNET_ERROR_TYPE_ERROR,
917 "Got invalid file descriptor %d!\n",
929 (0 == timeout.rel_value_us) &&
930 (busy_wait_warning > 16) )
932 LOG (GNUNET_ERROR_TYPE_WARNING,
933 "Looks like we're busy waiting...\n");
934 short_wait (100); /* mitigate */
936 check_ready (rs, ws);
938 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
940 /* consume the signal */
941 GNUNET_DISK_file_read (pr, &c, sizeof (c));
942 /* mark all active tasks as ready due to shutdown */
943 GNUNET_SCHEDULER_shutdown ();
945 if (last_tr == tasks_run)
953 busy_wait_warning = 0;
956 GNUNET_SIGNAL_handler_uninstall (shc_int);
957 GNUNET_SIGNAL_handler_uninstall (shc_term);
958 #if (SIGTERM != GNUNET_TERM_SIG)
959 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
962 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
963 GNUNET_SIGNAL_handler_uninstall (shc_quit);
964 GNUNET_SIGNAL_handler_uninstall (shc_hup);
966 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
967 shutdown_pipe_handle = NULL;
968 GNUNET_NETWORK_fdset_destroy (rs);
969 GNUNET_NETWORK_fdset_destroy (ws);
974 * Obtain the task context, giving the reason why the current task was
977 * @return current tasks' scheduler context
979 const struct GNUNET_SCHEDULER_TaskContext *
980 GNUNET_SCHEDULER_get_task_context ()
982 GNUNET_assert (NULL != active_task);
988 * Get information about the current load of this scheduler. Use this
989 * function to determine if an elective task should be added or simply
990 * dropped (if the decision should be made based on the number of
991 * tasks ready to run).
993 * @param p priority level to look at
994 * @return number of tasks pending right now
997 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
999 struct GNUNET_SCHEDULER_Task *pos;
1002 GNUNET_assert (NULL != active_task);
1003 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
1005 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
1006 p = current_priority;
1008 for (pos = ready_head[check_priority (p)]; NULL != pos; pos = pos->next)
1015 * Cancel the task with the specified identifier.
1016 * The task must not yet have run.
1018 * @param task id of the task to cancel
1019 * @return original closure of the task
1022 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
1024 enum GNUNET_SCHEDULER_Priority p;
1027 GNUNET_assert ( (NULL != active_task) ||
1028 (GNUNET_NO == task->lifeness) );
1029 if (! task->in_ready_list)
1031 if ( (-1 == task->read_fd) &&
1032 (-1 == task->write_fd) &&
1033 (NULL == task->read_set) &&
1034 (NULL == task->write_set) )
1036 if (GNUNET_YES == task->on_shutdown)
1037 GNUNET_CONTAINER_DLL_remove (shutdown_head,
1041 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1042 pending_timeout_tail,
1044 if (task == pending_timeout_last)
1045 pending_timeout_last = NULL;
1049 GNUNET_CONTAINER_DLL_remove (pending_head,
1056 p = check_priority (task->priority);
1057 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1062 ret = task->callback_cls;
1063 LOG (GNUNET_ERROR_TYPE_DEBUG,
1064 "Canceling task %p\n",
1066 destroy_task (task);
1072 * Initialize backtrace data for task @a t
1074 * @param t task to initialize
1077 init_backtrace (struct GNUNET_SCHEDULER_Task *t)
1080 void *backtrace_array[MAX_TRACE_DEPTH];
1082 t->num_backtrace_strings
1083 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1084 t->backtrace_strings =
1085 backtrace_symbols (backtrace_array,
1086 t->num_backtrace_strings);
1093 * Continue the current execution with the given function. This is
1094 * similar to the other "add" functions except that there is no delay
1095 * and the reason code can be specified.
1097 * @param task main function of the task
1098 * @param task_cls closure for @a task
1099 * @param reason reason for task invocation
1100 * @param priority priority to use for the task
1103 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
1105 enum GNUNET_SCHEDULER_Reason reason,
1106 enum GNUNET_SCHEDULER_Priority priority)
1108 struct GNUNET_SCHEDULER_Task *t;
1110 GNUNET_assert (NULL != task);
1111 GNUNET_assert ((NULL != active_task) ||
1112 (GNUNET_SCHEDULER_REASON_STARTUP == reason));
1113 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1117 t->callback_cls = task_cls;
1119 t->start_time = GNUNET_TIME_absolute_get ();
1122 t->priority = priority;
1123 t->lifeness = current_lifeness;
1124 LOG (GNUNET_ERROR_TYPE_DEBUG,
1125 "Adding continuation task %p\n",
1128 queue_ready_task (t);
1133 * Schedule a new task to be run at the specified time. The task
1134 * will be scheduled for execution at time @a at.
1136 * @param at time when the operation should run
1137 * @param priority priority to use for the task
1138 * @param task main function of the task
1139 * @param task_cls closure of @a task
1140 * @return unique task identifier for the job
1141 * only valid until @a task is started!
1143 struct GNUNET_SCHEDULER_Task *
1144 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1145 enum GNUNET_SCHEDULER_Priority priority,
1146 GNUNET_SCHEDULER_TaskCallback task,
1149 struct GNUNET_SCHEDULER_Task *t;
1150 struct GNUNET_SCHEDULER_Task *pos;
1151 struct GNUNET_SCHEDULER_Task *prev;
1153 GNUNET_assert (NULL != active_task);
1154 GNUNET_assert (NULL != task);
1155 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1157 t->callback_cls = task_cls;
1161 t->start_time = GNUNET_TIME_absolute_get ();
1164 t->priority = priority;
1165 t->lifeness = current_lifeness;
1166 /* try tail first (optimization in case we are
1167 * appending to a long list of tasks with timeouts) */
1168 if ( (NULL == pending_timeout_head) ||
1169 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us) )
1171 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1172 pending_timeout_tail,
1177 /* first move from heuristic start backwards to before start time */
1178 prev = pending_timeout_last;
1179 while ( (NULL != prev) &&
1180 (prev->timeout.abs_value_us > t->timeout.abs_value_us) )
1182 /* now, move from heuristic start (or head of list) forward to insertion point */
1184 pos = pending_timeout_head;
1187 while ( (NULL != pos) &&
1188 ( (pos->timeout.abs_value_us <= t->timeout.abs_value_us) ||
1189 (0 != pos->reason) ) )
1194 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1195 pending_timeout_tail,
1199 /* finally, update heuristic insertion point to last insertion... */
1200 pending_timeout_last = t;
1202 LOG (GNUNET_ERROR_TYPE_DEBUG,
1203 "Adding task: %p\n",
1211 * Schedule a new task to be run with a specified delay. The task
1212 * will be scheduled for execution once the delay has expired.
1214 * @param delay when should this operation time out?
1215 * @param priority priority to use for the task
1216 * @param task main function of the task
1217 * @param task_cls closure of @a task
1218 * @return unique task identifier for the job
1219 * only valid until @a task is started!
1221 struct GNUNET_SCHEDULER_Task *
1222 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1223 enum GNUNET_SCHEDULER_Priority priority,
1224 GNUNET_SCHEDULER_TaskCallback task,
1227 return GNUNET_SCHEDULER_add_at_with_priority (GNUNET_TIME_relative_to_absolute (delay),
1235 * Schedule a new task to be run with a specified priority.
1237 * @param prio how important is the new task?
1238 * @param task main function of the task
1239 * @param task_cls closure of @a task
1240 * @return unique task identifier for the job
1241 * only valid until @a task is started!
1243 struct GNUNET_SCHEDULER_Task *
1244 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1245 GNUNET_SCHEDULER_TaskCallback task,
1248 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1256 * Schedule a new task to be run at the specified time. The task
1257 * will be scheduled for execution once specified time has been
1258 * reached. It will be run with the DEFAULT priority.
1260 * @param at time at which this operation should run
1261 * @param task main function of the task
1262 * @param task_cls closure of @a task
1263 * @return unique task identifier for the job
1264 * only valid until @a task is started!
1266 struct GNUNET_SCHEDULER_Task *
1267 GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1268 GNUNET_SCHEDULER_TaskCallback task,
1271 return GNUNET_SCHEDULER_add_at_with_priority (at,
1272 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1279 * Schedule a new task to be run with a specified delay. The task
1280 * will be scheduled for execution once the delay has expired. It
1281 * will be run with the DEFAULT priority.
1283 * @param delay when should this operation time out?
1284 * @param task main function of the task
1285 * @param task_cls closure of @a task
1286 * @return unique task identifier for the job
1287 * only valid until @a task is started!
1289 struct GNUNET_SCHEDULER_Task *
1290 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1291 GNUNET_SCHEDULER_TaskCallback task,
1294 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1295 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1302 * Schedule a new task to be run as soon as possible. Note that this
1303 * does not guarantee that this will be the next task that is being
1304 * run, as other tasks with higher priority (or that are already ready
1305 * to run) might get to run first. Just as with delays, clients must
1306 * not rely on any particular order of execution between tasks
1307 * scheduled concurrently.
1309 * The task will be run with the DEFAULT priority.
1311 * @param task main function of the task
1312 * @param task_cls closure of @a task
1313 * @return unique task identifier for the job
1314 * only valid until @a task is started!
1316 struct GNUNET_SCHEDULER_Task *
1317 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
1320 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO,
1327 * Schedule a new task to be run on shutdown, that is when a CTRL-C
1328 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
1331 * @param task main function of the task
1332 * @param task_cls closure of @a task
1333 * @return unique task identifier for the job
1334 * only valid until @a task is started!
1336 struct GNUNET_SCHEDULER_Task *
1337 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1340 struct GNUNET_SCHEDULER_Task *t;
1342 GNUNET_assert (NULL != active_task);
1343 GNUNET_assert (NULL != task);
1344 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1346 t->callback_cls = task_cls;
1350 t->start_time = GNUNET_TIME_absolute_get ();
1352 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1353 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1354 t->on_shutdown = GNUNET_YES;
1355 t->lifeness = GNUNET_YES;
1356 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1359 LOG (GNUNET_ERROR_TYPE_DEBUG,
1360 "Adding task: %p\n",
1368 * Schedule a new task to be run as soon as possible with the
1369 * (transitive) ignore-shutdown flag either explicitly set or
1370 * explicitly enabled. This task (and all tasks created from it,
1371 * other than by another call to this function) will either count or
1372 * not count for the "lifeness" of the process. This API is only
1373 * useful in a few special cases.
1375 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
1376 * @param task main function of the task
1377 * @param task_cls closure of @a task
1378 * @return unique task identifier for the job
1379 * only valid until @a task is started!
1381 struct GNUNET_SCHEDULER_Task *
1382 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1383 GNUNET_SCHEDULER_TaskCallback task,
1386 struct GNUNET_SCHEDULER_Task *ret;
1388 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1389 ret->lifeness = lifeness;
1395 * Schedule a new task to be run with a specified delay or when any of
1396 * the specified file descriptor sets is ready. The delay can be used
1397 * as a timeout on the socket(s) being ready. The task will be
1398 * scheduled for execution once either the delay has expired or any of
1399 * the socket operations is ready. This is the most general
1400 * function of the "add" family. Note that the "prerequisite_task"
1401 * must be satisfied in addition to any of the other conditions. In
1402 * other words, the task will be started when
1404 * (prerequisite-run)
1410 * @param delay how long should we wait?
1411 * @param priority priority to use
1412 * @param rfd file descriptor we want to read (can be -1)
1413 * @param wfd file descriptors we want to write (can be -1)
1414 * @param task main function of the task
1415 * @param task_cls closure of @a task
1416 * @return unique task identifier for the job
1417 * only valid until @a task is started!
1420 static struct GNUNET_SCHEDULER_Task *
1421 add_without_sets (struct GNUNET_TIME_Relative delay,
1422 enum GNUNET_SCHEDULER_Priority priority,
1425 GNUNET_SCHEDULER_TaskCallback task,
1428 struct GNUNET_SCHEDULER_Task *t;
1430 GNUNET_assert (NULL != active_task);
1431 GNUNET_assert (NULL != task);
1432 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1434 t->callback_cls = task_cls;
1438 int flags = fcntl (rfd, F_GETFD);
1440 if ((flags == -1) && (errno == EBADF))
1442 LOG (GNUNET_ERROR_TYPE_ERROR,
1443 "Got invalid file descriptor %d!\n",
1451 int flags = fcntl (wfd, F_GETFD);
1453 if (flags == -1 && errno == EBADF)
1455 LOG (GNUNET_ERROR_TYPE_ERROR,
1456 "Got invalid file descriptor %d!\n",
1464 GNUNET_assert (wfd >= -1);
1467 t->start_time = GNUNET_TIME_absolute_get ();
1469 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1470 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1471 t->lifeness = current_lifeness;
1472 GNUNET_CONTAINER_DLL_insert (pending_head,
1475 max_priority_added = GNUNET_MAX (max_priority_added,
1477 LOG (GNUNET_ERROR_TYPE_DEBUG,
1487 * Schedule a new task to be run with a specified delay or when the
1488 * specified file descriptor is ready for reading. The delay can be
1489 * used as a timeout on the socket being ready. The task will be
1490 * scheduled for execution once either the delay has expired or the
1491 * socket operation is ready. It will be run with the DEFAULT priority.
1493 * @param delay when should this operation time out?
1494 * @param rfd read file-descriptor
1495 * @param task main function of the task
1496 * @param task_cls closure of @a task
1497 * @return unique task identifier for the job
1498 * only valid until @a task is started!
1500 struct GNUNET_SCHEDULER_Task *
1501 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1502 struct GNUNET_NETWORK_Handle *rfd,
1503 GNUNET_SCHEDULER_TaskCallback task,
1506 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1507 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1508 rfd, task, task_cls);
1513 * Schedule a new task to be run with a specified priority and to be
1514 * run after the specified delay or when the specified file descriptor
1515 * is ready for reading. The delay can be used as a timeout on the
1516 * socket being ready. The task will be scheduled for execution once
1517 * either the delay has expired or the socket operation is ready. It
1518 * will be run with the DEFAULT priority.
1520 * @param delay when should this operation time out?
1521 * @param priority priority to use for the task
1522 * @param rfd read file-descriptor
1523 * @param task main function of the task
1524 * @param task_cls closure of @a task
1525 * @return unique task identifier for the job
1526 * only valid until @a task is started!
1528 struct GNUNET_SCHEDULER_Task *
1529 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1530 enum GNUNET_SCHEDULER_Priority priority,
1531 struct GNUNET_NETWORK_Handle *rfd,
1532 GNUNET_SCHEDULER_TaskCallback task,
1535 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1544 * Schedule a new task to be run with a specified delay or when the
1545 * specified file descriptor is ready for writing. The delay can be
1546 * used as a timeout on the socket being ready. The task will be
1547 * scheduled for execution once either the delay has expired or the
1548 * socket operation is ready. It will be run with the priority of
1551 * @param delay when should this operation time out?
1552 * @param wfd write file-descriptor
1553 * @param task main function of the task
1554 * @param task_cls closure of @a task
1555 * @return unique task identifier for the job
1556 * only valid until @a task is started!
1558 struct GNUNET_SCHEDULER_Task *
1559 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1560 struct GNUNET_NETWORK_Handle *wfd,
1561 GNUNET_SCHEDULER_TaskCallback task,
1564 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1565 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1567 GNUNET_NO, GNUNET_YES,
1572 * Schedule a new task to be run with a specified delay or when the
1573 * specified file descriptor is ready. The delay can be
1574 * used as a timeout on the socket being ready. The task will be
1575 * scheduled for execution once either the delay has expired or the
1576 * socket operation is ready.
1578 * @param delay when should this operation time out?
1579 * @param priority priority of the task
1580 * @param fd file-descriptor
1581 * @param on_read whether to poll the file-descriptor for readability
1582 * @param on_write whether to poll the file-descriptor for writability
1583 * @param task main function of the task
1584 * @param task_cls closure of task
1585 * @return unique task identifier for the job
1586 * only valid until "task" is started!
1588 struct GNUNET_SCHEDULER_Task *
1589 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1590 enum GNUNET_SCHEDULER_Priority priority,
1591 struct GNUNET_NETWORK_Handle *fd,
1594 GNUNET_SCHEDULER_TaskCallback task,
1598 struct GNUNET_NETWORK_FDSet *s;
1599 struct GNUNET_SCHEDULER_Task * ret;
1601 GNUNET_assert (NULL != fd);
1602 s = GNUNET_NETWORK_fdset_create ();
1603 GNUNET_NETWORK_fdset_set (s, fd);
1604 ret = GNUNET_SCHEDULER_add_select (
1607 on_write ? s : NULL,
1609 GNUNET_NETWORK_fdset_destroy (s);
1612 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1613 return add_without_sets (delay, priority,
1614 on_read ? GNUNET_NETWORK_get_fd (fd) : -1,
1615 on_write ? GNUNET_NETWORK_get_fd (fd) : -1,
1622 * Schedule a new task to be run with a specified delay or when the
1623 * specified file descriptor is ready for reading. The delay can be
1624 * used as a timeout on the socket being ready. The task will be
1625 * scheduled for execution once either the delay has expired or the
1626 * socket operation is ready. It will be run with the DEFAULT priority.
1628 * @param delay when should this operation time out?
1629 * @param rfd read file-descriptor
1630 * @param task main function of the task
1631 * @param task_cls closure of @a task
1632 * @return unique task identifier for the job
1633 * only valid until @a task is started!
1635 struct GNUNET_SCHEDULER_Task *
1636 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1637 const struct GNUNET_DISK_FileHandle *rfd,
1638 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1640 return GNUNET_SCHEDULER_add_file_with_priority (
1641 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1642 rfd, GNUNET_YES, GNUNET_NO,
1648 * Schedule a new task to be run with a specified delay or when the
1649 * specified file descriptor is ready for writing. The delay can be
1650 * used as a timeout on the socket being ready. The task will be
1651 * scheduled for execution once either the delay has expired or the
1652 * socket operation is ready. It will be run with the DEFAULT priority.
1654 * @param delay when should this operation time out?
1655 * @param wfd write file-descriptor
1656 * @param task main function of the task
1657 * @param task_cls closure of @a task
1658 * @return unique task identifier for the job
1659 * only valid until @a task is started!
1661 struct GNUNET_SCHEDULER_Task *
1662 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1663 const struct GNUNET_DISK_FileHandle *wfd,
1664 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1666 return GNUNET_SCHEDULER_add_file_with_priority (
1667 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1668 wfd, GNUNET_NO, GNUNET_YES,
1674 * Schedule a new task to be run with a specified delay or when the
1675 * specified file descriptor is ready. The delay can be
1676 * used as a timeout on the socket being ready. The task will be
1677 * scheduled for execution once either the delay has expired or the
1678 * socket operation is ready.
1680 * @param delay when should this operation time out?
1681 * @param priority priority of the task
1682 * @param fd file-descriptor
1683 * @param on_read whether to poll the file-descriptor for readability
1684 * @param on_write whether to poll the file-descriptor for writability
1685 * @param task main function of the task
1686 * @param task_cls closure of @a task
1687 * @return unique task identifier for the job
1688 * only valid until @a task is started!
1690 struct GNUNET_SCHEDULER_Task *
1691 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1692 enum GNUNET_SCHEDULER_Priority priority,
1693 const struct GNUNET_DISK_FileHandle *fd,
1694 int on_read, int on_write,
1695 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1698 struct GNUNET_NETWORK_FDSet *s;
1699 struct GNUNET_SCHEDULER_Task * ret;
1701 GNUNET_assert (NULL != fd);
1702 s = GNUNET_NETWORK_fdset_create ();
1703 GNUNET_NETWORK_fdset_handle_set (s, fd);
1704 ret = GNUNET_SCHEDULER_add_select (
1707 on_write ? s : NULL,
1709 GNUNET_NETWORK_fdset_destroy (s);
1714 GNUNET_DISK_internal_file_handle_ (fd, &real_fd, sizeof (int));
1715 GNUNET_assert (real_fd >= 0);
1716 return add_without_sets (
1718 on_read ? real_fd : -1,
1719 on_write ? real_fd : -1,
1726 * Schedule a new task to be run with a specified delay or when any of
1727 * the specified file descriptor sets is ready. The delay can be used
1728 * as a timeout on the socket(s) being ready. The task will be
1729 * scheduled for execution once either the delay has expired or any of
1730 * the socket operations is ready. This is the most general
1731 * function of the "add" family. Note that the "prerequisite_task"
1732 * must be satisfied in addition to any of the other conditions. In
1733 * other words, the task will be started when
1735 * (prerequisite-run)
1738 * || any-ws-ready) )
1741 * @param prio how important is this task?
1742 * @param delay how long should we wait?
1743 * @param rs set of file descriptors we want to read (can be NULL)
1744 * @param ws set of file descriptors we want to write (can be NULL)
1745 * @param task main function of the task
1746 * @param task_cls closure of @a task
1747 * @return unique task identifier for the job
1748 * only valid until @a task is started!
1750 struct GNUNET_SCHEDULER_Task *
1751 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1752 struct GNUNET_TIME_Relative delay,
1753 const struct GNUNET_NETWORK_FDSet *rs,
1754 const struct GNUNET_NETWORK_FDSet *ws,
1755 GNUNET_SCHEDULER_TaskCallback task,
1758 struct GNUNET_SCHEDULER_Task *t;
1760 if ( (NULL == rs) &&
1762 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1766 GNUNET_assert (NULL != active_task);
1767 GNUNET_assert (NULL != task);
1768 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1770 t->callback_cls = task_cls;
1775 t->read_set = GNUNET_NETWORK_fdset_create ();
1776 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1780 t->write_set = GNUNET_NETWORK_fdset_create ();
1781 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1784 t->start_time = GNUNET_TIME_absolute_get ();
1786 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1788 check_priority ((prio ==
1789 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1791 t->lifeness = current_lifeness;
1792 GNUNET_CONTAINER_DLL_insert (pending_head,
1795 max_priority_added = GNUNET_MAX (max_priority_added,
1797 LOG (GNUNET_ERROR_TYPE_DEBUG,
1806 * Function used by event-loop implementations to signal the scheduler
1807 * that a particular @a task is ready due to an event of type @a et.
1809 * This function will then queue the task to notify the application
1810 * that the task is ready (with the respective priority).
1812 * @param task the task that is ready, NULL for wake up calls
1813 * @param et information about why the task is ready
1816 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1817 enum GNUNET_SCHEDULER_EventType et)
1819 enum GNUNET_SCHEDULER_Reason reason;
1820 struct GNUNET_TIME_Absolute now;
1822 now = GNUNET_TIME_absolute_get ();
1823 reason = task->reason;
1824 if (now.abs_value_us >= task->timeout.abs_value_us)
1825 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1826 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1827 (0 != (GNUNET_SCHEDULER_ET_IN & et)) )
1828 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
1829 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
1830 (0 != (GNUNET_SCHEDULER_ET_OUT & et)) )
1831 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
1832 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1833 task->reason = reason;
1834 task->fds = &task->fdx;
1837 queue_ready_task (task);
1842 * Function called by the driver to tell the scheduler to run some of
1843 * the tasks that are ready. This function may return even though
1844 * there are tasks left to run just to give other tasks a chance as
1845 * well. If we return #GNUNET_YES, the driver should call this
1846 * function again as soon as possible, while if we return #GNUNET_NO
1847 * it must block until the operating system has more work as the
1848 * scheduler has no more work to do right now.
1850 * @param sh scheduler handle that was given to the `loop`
1851 * @return #GNUNET_OK if there are more tasks that are ready,
1852 * and thus we would like to run more (yield to avoid
1853 * blocking other activities for too long)
1854 * #GNUNET_NO if we are done running tasks (yield to block)
1855 * #GNUNET_SYSERR on error
1858 GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh)
1860 enum GNUNET_SCHEDULER_Priority p;
1861 struct GNUNET_SCHEDULER_Task *pos;
1862 struct GNUNET_TIME_Absolute now;
1864 /* check for tasks that reached the timeout! */
1865 now = GNUNET_TIME_absolute_get ();
1866 while (NULL != (pos = pending_timeout_head))
1868 if (now.abs_value_us >= pos->timeout.abs_value_us)
1869 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1870 if (0 == pos->reason)
1872 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1873 pending_timeout_tail,
1875 if (pending_timeout_last == pos)
1876 pending_timeout_last = NULL;
1877 queue_ready_task (pos);
1880 if (0 == ready_count)
1883 /* find out which task priority level we are going to
1884 process this time */
1885 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
1886 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
1887 /* yes, p>0 is correct, 0 is "KEEP" which should
1888 * always be an empty queue (see assertion)! */
1889 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
1891 pos = ready_head[p];
1895 GNUNET_assert (NULL != pos); /* ready_count wrong? */
1897 /* process all tasks at this priority level, then yield */
1898 while (NULL != (pos = ready_head[p]))
1900 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1904 current_priority = pos->priority;
1905 current_lifeness = pos->lifeness;
1908 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
1909 DELAY_THRESHOLD.rel_value_us)
1911 LOG (GNUNET_ERROR_TYPE_DEBUG,
1912 "Task %p took %s to be scheduled\n",
1914 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
1918 tc.reason = pos->reason;
1919 GNUNET_NETWORK_fdset_zero (sh->rs);
1920 GNUNET_NETWORK_fdset_zero (sh->ws);
1921 tc.fds_len = pos->fds_len;
1923 tc.read_ready = (NULL == pos->read_set) ? sh->rs : pos->read_set;
1924 if ( (-1 != pos->read_fd) &&
1925 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
1926 GNUNET_NETWORK_fdset_set_native (sh->rs,
1928 tc.write_ready = (NULL == pos->write_set) ? sh->ws : pos->write_set;
1929 if ((-1 != pos->write_fd) &&
1930 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
1931 GNUNET_NETWORK_fdset_set_native (sh->ws,
1933 LOG (GNUNET_ERROR_TYPE_DEBUG,
1934 "Running task: %p\n",
1936 pos->callback (pos->callback_cls);
1938 dump_backtrace (pos);
1942 if (0 == ready_count)
1949 * Initialize and run scheduler. This function will return when all
1950 * tasks have completed. On systems with signals, receiving a SIGTERM
1951 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
1952 * to be run after the active task is complete. As a result, SIGTERM
1953 * causes all shutdown tasks to be scheduled with reason
1954 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
1955 * afterwards will execute normally!). Note that any particular
1956 * signal will only shut down one scheduler; applications should
1957 * always only create a single scheduler.
1959 * @param driver drive to use for the event loop
1960 * @param task task to run first (and immediately)
1961 * @param task_cls closure of @a task
1962 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1965 GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
1966 GNUNET_SCHEDULER_TaskCallback task,
1970 struct GNUNET_SIGNAL_Context *shc_int;
1971 struct GNUNET_SIGNAL_Context *shc_term;
1972 #if (SIGTERM != GNUNET_TERM_SIG)
1973 struct GNUNET_SIGNAL_Context *shc_gterm;
1976 struct GNUNET_SIGNAL_Context *shc_quit;
1977 struct GNUNET_SIGNAL_Context *shc_hup;
1978 struct GNUNET_SIGNAL_Context *shc_pipe;
1980 struct GNUNET_SCHEDULER_Task tsk;
1981 const struct GNUNET_DISK_FileHandle *pr;
1982 struct GNUNET_SCHEDULER_Handle sh;
1984 /* general set-up */
1985 GNUNET_assert (NULL == active_task);
1986 GNUNET_assert (NULL == shutdown_pipe_handle);
1987 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
1991 GNUNET_assert (NULL != shutdown_pipe_handle);
1992 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
1993 GNUNET_DISK_PIPE_END_READ);
1994 GNUNET_assert (NULL != pr);
1997 /* install signal handlers */
1998 LOG (GNUNET_ERROR_TYPE_DEBUG,
1999 "Registering signal handlers\n");
2000 shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2001 &sighandler_shutdown);
2002 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2003 &sighandler_shutdown);
2004 #if (SIGTERM != GNUNET_TERM_SIG)
2005 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2006 &sighandler_shutdown);
2009 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2011 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2012 &sighandler_shutdown);
2013 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2014 &sighandler_shutdown);
2017 /* Setup initial tasks */
2018 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2019 current_lifeness = GNUNET_YES;
2025 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
2027 GNUNET_SCHEDULER_REASON_STARTUP,
2028 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
2029 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
2030 &GNUNET_OS_install_parent_control_handler,
2033 driver->set_wakeup (driver->cls,
2034 GNUNET_TIME_absolute_get ());
2036 /* begin main event loop */
2037 sh.rs = GNUNET_NETWORK_fdset_create ();
2038 sh.ws = GNUNET_NETWORK_fdset_create ();
2040 ret = driver->loop (driver->cls,
2042 GNUNET_NETWORK_fdset_destroy (sh.rs);
2043 GNUNET_NETWORK_fdset_destroy (sh.ws);
2045 /* uninstall signal handlers */
2046 GNUNET_SIGNAL_handler_uninstall (shc_int);
2047 GNUNET_SIGNAL_handler_uninstall (shc_term);
2048 #if (SIGTERM != GNUNET_TERM_SIG)
2049 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
2052 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
2053 GNUNET_SIGNAL_handler_uninstall (shc_quit);
2054 GNUNET_SIGNAL_handler_uninstall (shc_hup);
2056 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2057 shutdown_pipe_handle = NULL;
2063 * Obtain the driver for using select() as the event loop.
2065 * @return NULL on error
2067 const struct GNUNET_SCHEDULER_Driver *
2068 GNUNET_SCHEDULER_driver_select ()
2070 GNUNET_break (0); // not implemented
2075 /* end of scheduler.c */