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)
592 for (unsigned int i = 0; i < t->num_backtrace_strings; i++)
593 LOG (GNUNET_ERROR_TYPE_DEBUG,
594 "Task %p trace %u: %s\n",
597 t->backtrace_strings[i]);
603 * Run at least one task in the highest-priority queue that is not
604 * empty. Keep running tasks until we are either no longer running
605 * "URGENT" tasks or until we have at least one "pending" task (which
606 * may become ready, hence we should select on it). Naturally, if
607 * there are no more ready tasks, we also return.
609 * @param rs FDs ready for reading
610 * @param ws FDs ready for writing
613 run_ready (struct GNUNET_NETWORK_FDSet *rs,
614 struct GNUNET_NETWORK_FDSet *ws)
616 enum GNUNET_SCHEDULER_Priority p;
617 struct GNUNET_SCHEDULER_Task *pos;
619 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
622 if (0 == ready_count)
624 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
625 /* yes, p>0 is correct, 0 is "KEEP" which should
626 * always be an empty queue (see assertion)! */
627 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
633 GNUNET_assert (NULL != pos); /* ready_count wrong? */
634 GNUNET_CONTAINER_DLL_remove (ready_head[p],
638 current_priority = pos->priority;
639 current_lifeness = pos->lifeness;
642 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
643 DELAY_THRESHOLD.rel_value_us)
645 LOG (GNUNET_ERROR_TYPE_DEBUG,
646 "Task %p took %s to be scheduled\n",
648 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
652 tc.reason = pos->reason;
653 tc.read_ready = (NULL == pos->read_set) ? rs : pos->read_set;
654 if ((-1 != pos->read_fd) &&
655 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)))
656 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
657 tc.write_ready = (NULL == pos->write_set) ? ws : pos->write_set;
658 if ((-1 != pos->write_fd) &&
659 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
660 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
661 if ((0 != (tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
662 (-1 != pos->write_fd) &&
663 (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd)))
664 GNUNET_assert (0); // added to ready in previous select loop!
665 LOG (GNUNET_ERROR_TYPE_DEBUG,
666 "Running task: %p\n",
668 pos->callback (pos->callback_cls);
669 dump_backtrace (pos);
674 while ((NULL == pending_head) || (p >= max_priority_added));
679 * Pipe used to communicate shutdown via signal.
681 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
684 * Process ID of this process at the time we installed the various
690 * Signal handler called for SIGPIPE.
702 * Wait for a short time.
703 * Sleeps for @a ms ms (as that should be long enough for virtually all
704 * modern systems to context switch and allow another process to do
707 * @param ms how many ms to wait
710 short_wait (unsigned int ms)
712 struct GNUNET_TIME_Relative timeout;
714 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
715 (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
720 * Signal handler called for signals that should cause us to shutdown.
723 sighandler_shutdown ()
726 int old_errno = errno; /* backup errno */
728 if (getpid () != my_pid)
729 exit (1); /* we have fork'ed since the signal handler was created,
730 * ignore the signal, see https://gnunet.org/vfork discussion */
731 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
732 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
739 * Check if the system is still alive. Trigger shutdown if we
740 * have tasks, but none of them give us lifeness.
742 * @return #GNUNET_OK to continue the main loop,
748 struct GNUNET_SCHEDULER_Task *t;
752 for (t = pending_head; NULL != t; t = t->next)
753 if (t->lifeness == GNUNET_YES)
755 for (t = shutdown_head; NULL != t; t = t->next)
756 if (t->lifeness == GNUNET_YES)
758 for (t = pending_timeout_head; NULL != t; t = t->next)
759 if (t->lifeness == GNUNET_YES)
761 if (NULL != shutdown_head)
763 GNUNET_SCHEDULER_shutdown ();
771 * Initialize and run scheduler. This function will return when all
772 * tasks have completed. On systems with signals, receiving a SIGTERM
773 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown()
774 * to be run after the active task is complete. As a result, SIGTERM
775 * causes all active tasks to be scheduled with reason
776 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
777 * afterwards will execute normally!). Note that any particular signal
778 * will only shut down one scheduler; applications should always only
779 * create a single scheduler.
781 * @param task task to run immediately
782 * @param task_cls closure of @a task
785 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
788 struct GNUNET_NETWORK_FDSet *rs;
789 struct GNUNET_NETWORK_FDSet *ws;
790 struct GNUNET_TIME_Relative timeout;
792 struct GNUNET_SIGNAL_Context *shc_int;
793 struct GNUNET_SIGNAL_Context *shc_term;
794 #if (SIGTERM != GNUNET_TERM_SIG)
795 struct GNUNET_SIGNAL_Context *shc_gterm;
799 struct GNUNET_SIGNAL_Context *shc_quit;
800 struct GNUNET_SIGNAL_Context *shc_hup;
801 struct GNUNET_SIGNAL_Context *shc_pipe;
803 unsigned long long last_tr;
804 unsigned int busy_wait_warning;
805 const struct GNUNET_DISK_FileHandle *pr;
808 GNUNET_assert (NULL == active_task);
809 rs = GNUNET_NETWORK_fdset_create ();
810 ws = GNUNET_NETWORK_fdset_create ();
811 GNUNET_assert (NULL == shutdown_pipe_handle);
812 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
816 GNUNET_assert (NULL != shutdown_pipe_handle);
817 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
818 GNUNET_DISK_PIPE_END_READ);
819 GNUNET_assert (NULL != pr);
821 LOG (GNUNET_ERROR_TYPE_DEBUG,
822 "Registering signal handlers\n");
823 shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
824 &sighandler_shutdown);
825 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
826 &sighandler_shutdown);
827 #if (SIGTERM != GNUNET_TERM_SIG)
828 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
829 &sighandler_shutdown);
832 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
834 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
835 &sighandler_shutdown);
836 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
837 &sighandler_shutdown);
839 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
840 current_lifeness = GNUNET_YES;
841 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
843 GNUNET_SCHEDULER_REASON_STARTUP,
844 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
845 active_task = (void *) (long) -1; /* force passing of sanity check */
846 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
847 &GNUNET_OS_install_parent_control_handler,
851 busy_wait_warning = 0;
852 while (GNUNET_OK == check_lifeness ())
854 GNUNET_NETWORK_fdset_zero (rs);
855 GNUNET_NETWORK_fdset_zero (ws);
856 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
857 update_sets (rs, ws, &timeout);
858 GNUNET_NETWORK_fdset_handle_set (rs, pr);
861 /* no blocking, more work already ready! */
862 timeout = GNUNET_TIME_UNIT_ZERO;
864 if (NULL == scheduler_select)
865 ret = GNUNET_NETWORK_socket_select (rs,
870 ret = scheduler_select (scheduler_select_cls,
875 if (ret == GNUNET_SYSERR)
880 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
885 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
888 if (0 != system (lsof))
889 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
894 struct GNUNET_SCHEDULER_Task *t;
896 for (t = pending_head; NULL != t; t = t->next)
898 if (-1 != t->read_fd)
900 int flags = fcntl (t->read_fd, F_GETFD);
901 if ((flags == -1) && (errno == EBADF))
903 LOG (GNUNET_ERROR_TYPE_ERROR,
904 "Got invalid file descriptor %d!\n",
909 if (-1 != t->write_fd)
911 int flags = fcntl (t->write_fd, F_GETFD);
912 if ((flags == -1) && (errno == EBADF))
914 LOG (GNUNET_ERROR_TYPE_ERROR,
915 "Got invalid file descriptor %d!\n",
927 (0 == timeout.rel_value_us) &&
928 (busy_wait_warning > 16) )
930 LOG (GNUNET_ERROR_TYPE_WARNING,
931 "Looks like we're busy waiting...\n");
932 short_wait (100); /* mitigate */
934 check_ready (rs, ws);
936 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
938 /* consume the signal */
939 GNUNET_DISK_file_read (pr, &c, sizeof (c));
940 /* mark all active tasks as ready due to shutdown */
941 GNUNET_SCHEDULER_shutdown ();
943 if (last_tr == tasks_run)
951 busy_wait_warning = 0;
954 GNUNET_SIGNAL_handler_uninstall (shc_int);
955 GNUNET_SIGNAL_handler_uninstall (shc_term);
956 #if (SIGTERM != GNUNET_TERM_SIG)
957 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
960 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
961 GNUNET_SIGNAL_handler_uninstall (shc_quit);
962 GNUNET_SIGNAL_handler_uninstall (shc_hup);
964 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
965 shutdown_pipe_handle = NULL;
966 GNUNET_NETWORK_fdset_destroy (rs);
967 GNUNET_NETWORK_fdset_destroy (ws);
972 * Obtain the task context, giving the reason why the current task was
975 * @return current tasks' scheduler context
977 const struct GNUNET_SCHEDULER_TaskContext *
978 GNUNET_SCHEDULER_get_task_context ()
980 GNUNET_assert (NULL != active_task);
986 * Get information about the current load of this scheduler. Use this
987 * function to determine if an elective task should be added or simply
988 * dropped (if the decision should be made based on the number of
989 * tasks ready to run).
991 * @param p priority level to look at
992 * @return number of tasks pending right now
995 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
997 struct GNUNET_SCHEDULER_Task *pos;
1000 GNUNET_assert (NULL != active_task);
1001 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
1003 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
1004 p = current_priority;
1006 for (pos = ready_head[check_priority (p)]; NULL != pos; pos = pos->next)
1013 * Cancel the task with the specified identifier.
1014 * The task must not yet have run.
1016 * @param task id of the task to cancel
1017 * @return original closure of the task
1020 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
1022 enum GNUNET_SCHEDULER_Priority p;
1025 GNUNET_assert ( (NULL != active_task) ||
1026 (GNUNET_NO == task->lifeness) );
1027 if (! task->in_ready_list)
1029 if ( (-1 == task->read_fd) &&
1030 (-1 == task->write_fd) &&
1031 (NULL == task->read_set) &&
1032 (NULL == task->write_set) )
1034 if (GNUNET_YES == task->on_shutdown)
1035 GNUNET_CONTAINER_DLL_remove (shutdown_head,
1039 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1040 pending_timeout_tail,
1042 if (task == pending_timeout_last)
1043 pending_timeout_last = NULL;
1047 GNUNET_CONTAINER_DLL_remove (pending_head,
1054 p = check_priority (task->priority);
1055 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1060 ret = task->callback_cls;
1061 LOG (GNUNET_ERROR_TYPE_DEBUG,
1062 "Canceling task %p\n",
1064 destroy_task (task);
1070 * Initialize backtrace data for task @a t
1072 * @param t task to initialize
1075 init_backtrace (struct GNUNET_SCHEDULER_Task *t)
1078 void *backtrace_array[MAX_TRACE_DEPTH];
1080 t->num_backtrace_strings
1081 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1082 t->backtrace_strings =
1083 backtrace_symbols (backtrace_array,
1084 t->num_backtrace_strings);
1091 * Continue the current execution with the given function. This is
1092 * similar to the other "add" functions except that there is no delay
1093 * and the reason code can be specified.
1095 * @param task main function of the task
1096 * @param task_cls closure for @a task
1097 * @param reason reason for task invocation
1098 * @param priority priority to use for the task
1101 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
1103 enum GNUNET_SCHEDULER_Reason reason,
1104 enum GNUNET_SCHEDULER_Priority priority)
1106 struct GNUNET_SCHEDULER_Task *t;
1108 GNUNET_assert (NULL != task);
1109 GNUNET_assert ((NULL != active_task) ||
1110 (GNUNET_SCHEDULER_REASON_STARTUP == reason));
1111 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1115 t->callback_cls = task_cls;
1117 t->start_time = GNUNET_TIME_absolute_get ();
1120 t->priority = priority;
1121 t->lifeness = current_lifeness;
1122 LOG (GNUNET_ERROR_TYPE_DEBUG,
1123 "Adding continuation task %p\n",
1126 queue_ready_task (t);
1131 * Schedule a new task to be run at the specified time. The task
1132 * will be scheduled for execution at time @a at.
1134 * @param at time when the operation should run
1135 * @param priority priority to use for the task
1136 * @param task main function of the task
1137 * @param task_cls closure of @a task
1138 * @return unique task identifier for the job
1139 * only valid until @a task is started!
1141 struct GNUNET_SCHEDULER_Task *
1142 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1143 enum GNUNET_SCHEDULER_Priority priority,
1144 GNUNET_SCHEDULER_TaskCallback task,
1147 struct GNUNET_SCHEDULER_Task *t;
1148 struct GNUNET_SCHEDULER_Task *pos;
1149 struct GNUNET_SCHEDULER_Task *prev;
1151 GNUNET_assert (NULL != active_task);
1152 GNUNET_assert (NULL != task);
1153 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1155 t->callback_cls = task_cls;
1159 t->start_time = GNUNET_TIME_absolute_get ();
1162 t->priority = priority;
1163 t->lifeness = current_lifeness;
1164 /* try tail first (optimization in case we are
1165 * appending to a long list of tasks with timeouts) */
1166 if ( (NULL == pending_timeout_head) ||
1167 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us) )
1169 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1170 pending_timeout_tail,
1175 /* first move from heuristic start backwards to before start time */
1176 prev = pending_timeout_last;
1177 while ( (NULL != prev) &&
1178 (prev->timeout.abs_value_us > t->timeout.abs_value_us) )
1180 /* now, move from heuristic start (or head of list) forward to insertion point */
1182 pos = pending_timeout_head;
1185 while ( (NULL != pos) &&
1186 ( (pos->timeout.abs_value_us <= t->timeout.abs_value_us) ||
1187 (0 != pos->reason) ) )
1192 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1193 pending_timeout_tail,
1197 /* finally, update heuristic insertion point to last insertion... */
1198 pending_timeout_last = t;
1200 LOG (GNUNET_ERROR_TYPE_DEBUG,
1201 "Adding task: %p\n",
1209 * Schedule a new task to be run with a specified delay. The task
1210 * will be scheduled for execution once the delay has expired.
1212 * @param delay when should this operation time out?
1213 * @param priority priority to use for the task
1214 * @param task main function of the task
1215 * @param task_cls closure of @a task
1216 * @return unique task identifier for the job
1217 * only valid until @a task is started!
1219 struct GNUNET_SCHEDULER_Task *
1220 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1221 enum GNUNET_SCHEDULER_Priority priority,
1222 GNUNET_SCHEDULER_TaskCallback task,
1225 return GNUNET_SCHEDULER_add_at_with_priority (GNUNET_TIME_relative_to_absolute (delay),
1233 * Schedule a new task to be run with a specified priority.
1235 * @param prio how important is the new task?
1236 * @param task main function of the task
1237 * @param task_cls closure of @a task
1238 * @return unique task identifier for the job
1239 * only valid until @a task is started!
1241 struct GNUNET_SCHEDULER_Task *
1242 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1243 GNUNET_SCHEDULER_TaskCallback task,
1246 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1254 * Schedule a new task to be run at the specified time. The task
1255 * will be scheduled for execution once specified time has been
1256 * reached. It will be run with the DEFAULT priority.
1258 * @param at time at which this operation should run
1259 * @param task main function of the task
1260 * @param task_cls closure of @a task
1261 * @return unique task identifier for the job
1262 * only valid until @a task is started!
1264 struct GNUNET_SCHEDULER_Task *
1265 GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1266 GNUNET_SCHEDULER_TaskCallback task,
1269 return GNUNET_SCHEDULER_add_at_with_priority (at,
1270 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1277 * Schedule a new task to be run with a specified delay. The task
1278 * will be scheduled for execution once the delay has expired. It
1279 * will be run with the DEFAULT priority.
1281 * @param delay when should this operation time out?
1282 * @param task main function of the task
1283 * @param task_cls closure of @a task
1284 * @return unique task identifier for the job
1285 * only valid until @a task is started!
1287 struct GNUNET_SCHEDULER_Task *
1288 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1289 GNUNET_SCHEDULER_TaskCallback task,
1292 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1293 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1300 * Schedule a new task to be run as soon as possible. Note that this
1301 * does not guarantee that this will be the next task that is being
1302 * run, as other tasks with higher priority (or that are already ready
1303 * to run) might get to run first. Just as with delays, clients must
1304 * not rely on any particular order of execution between tasks
1305 * scheduled concurrently.
1307 * The task will be run with the DEFAULT priority.
1309 * @param task main function of the task
1310 * @param task_cls closure of @a task
1311 * @return unique task identifier for the job
1312 * only valid until @a task is started!
1314 struct GNUNET_SCHEDULER_Task *
1315 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
1318 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO,
1325 * Schedule a new task to be run on shutdown, that is when a CTRL-C
1326 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
1329 * @param task main function of the task
1330 * @param task_cls closure of @a task
1331 * @return unique task identifier for the job
1332 * only valid until @a task is started!
1334 struct GNUNET_SCHEDULER_Task *
1335 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1338 struct GNUNET_SCHEDULER_Task *t;
1340 GNUNET_assert (NULL != active_task);
1341 GNUNET_assert (NULL != task);
1342 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1344 t->callback_cls = task_cls;
1348 t->start_time = GNUNET_TIME_absolute_get ();
1350 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1351 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1352 t->on_shutdown = GNUNET_YES;
1353 t->lifeness = GNUNET_YES;
1354 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1357 LOG (GNUNET_ERROR_TYPE_DEBUG,
1358 "Adding task: %p\n",
1366 * Schedule a new task to be run as soon as possible with the
1367 * (transitive) ignore-shutdown flag either explicitly set or
1368 * explicitly enabled. This task (and all tasks created from it,
1369 * other than by another call to this function) will either count or
1370 * not count for the "lifeness" of the process. This API is only
1371 * useful in a few special cases.
1373 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
1374 * @param task main function of the task
1375 * @param task_cls closure of @a task
1376 * @return unique task identifier for the job
1377 * only valid until @a task is started!
1379 struct GNUNET_SCHEDULER_Task *
1380 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1381 GNUNET_SCHEDULER_TaskCallback task,
1384 struct GNUNET_SCHEDULER_Task *ret;
1386 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1387 ret->lifeness = lifeness;
1393 * Schedule a new task to be run with a specified delay or when any of
1394 * the specified file descriptor sets is ready. The delay can be used
1395 * as a timeout on the socket(s) being ready. The task will be
1396 * scheduled for execution once either the delay has expired or any of
1397 * the socket operations is ready. This is the most general
1398 * function of the "add" family. Note that the "prerequisite_task"
1399 * must be satisfied in addition to any of the other conditions. In
1400 * other words, the task will be started when
1402 * (prerequisite-run)
1408 * @param delay how long should we wait?
1409 * @param priority priority to use
1410 * @param rfd file descriptor we want to read (can be -1)
1411 * @param wfd file descriptors we want to write (can be -1)
1412 * @param task main function of the task
1413 * @param task_cls closure of @a task
1414 * @return unique task identifier for the job
1415 * only valid until @a task is started!
1418 static struct GNUNET_SCHEDULER_Task *
1419 add_without_sets (struct GNUNET_TIME_Relative delay,
1420 enum GNUNET_SCHEDULER_Priority priority,
1423 GNUNET_SCHEDULER_TaskCallback task,
1426 struct GNUNET_SCHEDULER_Task *t;
1428 GNUNET_assert (NULL != active_task);
1429 GNUNET_assert (NULL != task);
1430 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1432 t->callback_cls = task_cls;
1436 int flags = fcntl (rfd, F_GETFD);
1438 if ((flags == -1) && (errno == EBADF))
1440 LOG (GNUNET_ERROR_TYPE_ERROR,
1441 "Got invalid file descriptor %d!\n",
1449 int flags = fcntl (wfd, F_GETFD);
1451 if (flags == -1 && errno == EBADF)
1453 LOG (GNUNET_ERROR_TYPE_ERROR,
1454 "Got invalid file descriptor %d!\n",
1462 GNUNET_assert (wfd >= -1);
1465 t->start_time = GNUNET_TIME_absolute_get ();
1467 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1468 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1469 t->lifeness = current_lifeness;
1470 GNUNET_CONTAINER_DLL_insert (pending_head,
1473 max_priority_added = GNUNET_MAX (max_priority_added,
1475 LOG (GNUNET_ERROR_TYPE_DEBUG,
1485 * Schedule a new task to be run with a specified delay or when the
1486 * specified file descriptor is ready for reading. The delay can be
1487 * used as a timeout on the socket being ready. The task will be
1488 * scheduled for execution once either the delay has expired or the
1489 * socket operation is ready. It will be run with the DEFAULT priority.
1491 * @param delay when should this operation time out?
1492 * @param rfd read file-descriptor
1493 * @param task main function of the task
1494 * @param task_cls closure of @a task
1495 * @return unique task identifier for the job
1496 * only valid until @a task is started!
1498 struct GNUNET_SCHEDULER_Task *
1499 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1500 struct GNUNET_NETWORK_Handle *rfd,
1501 GNUNET_SCHEDULER_TaskCallback task,
1504 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1505 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1506 rfd, task, task_cls);
1511 * Schedule a new task to be run with a specified priority and to be
1512 * run after the specified delay or when the specified file descriptor
1513 * is ready for reading. The delay can be used as a timeout on the
1514 * socket being ready. The task will be scheduled for execution once
1515 * either the delay has expired or the socket operation is ready. It
1516 * will be run with the DEFAULT priority.
1518 * @param delay when should this operation time out?
1519 * @param priority priority to use for the task
1520 * @param rfd read file-descriptor
1521 * @param task main function of the task
1522 * @param task_cls closure of @a task
1523 * @return unique task identifier for the job
1524 * only valid until @a task is started!
1526 struct GNUNET_SCHEDULER_Task *
1527 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1528 enum GNUNET_SCHEDULER_Priority priority,
1529 struct GNUNET_NETWORK_Handle *rfd,
1530 GNUNET_SCHEDULER_TaskCallback task,
1533 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1542 * Schedule a new task to be run with a specified delay or when the
1543 * specified file descriptor is ready for writing. The delay can be
1544 * used as a timeout on the socket being ready. The task will be
1545 * scheduled for execution once either the delay has expired or the
1546 * socket operation is ready. It will be run with the priority of
1549 * @param delay when should this operation time out?
1550 * @param wfd write file-descriptor
1551 * @param task main function of the task
1552 * @param task_cls closure of @a task
1553 * @return unique task identifier for the job
1554 * only valid until @a task is started!
1556 struct GNUNET_SCHEDULER_Task *
1557 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1558 struct GNUNET_NETWORK_Handle *wfd,
1559 GNUNET_SCHEDULER_TaskCallback task,
1562 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1563 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1565 GNUNET_NO, GNUNET_YES,
1570 * Schedule a new task to be run with a specified delay or when the
1571 * specified file descriptor is ready. The delay can be
1572 * used as a timeout on the socket being ready. The task will be
1573 * scheduled for execution once either the delay has expired or the
1574 * socket operation is ready.
1576 * @param delay when should this operation time out?
1577 * @param priority priority of the task
1578 * @param fd file-descriptor
1579 * @param on_read whether to poll the file-descriptor for readability
1580 * @param on_write whether to poll the file-descriptor for writability
1581 * @param task main function of the task
1582 * @param task_cls closure of task
1583 * @return unique task identifier for the job
1584 * only valid until "task" is started!
1586 struct GNUNET_SCHEDULER_Task *
1587 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1588 enum GNUNET_SCHEDULER_Priority priority,
1589 struct GNUNET_NETWORK_Handle *fd,
1592 GNUNET_SCHEDULER_TaskCallback task,
1596 struct GNUNET_NETWORK_FDSet *s;
1597 struct GNUNET_SCHEDULER_Task * ret;
1599 GNUNET_assert (NULL != fd);
1600 s = GNUNET_NETWORK_fdset_create ();
1601 GNUNET_NETWORK_fdset_set (s, fd);
1602 ret = GNUNET_SCHEDULER_add_select (
1605 on_write ? s : NULL,
1607 GNUNET_NETWORK_fdset_destroy (s);
1610 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1611 return add_without_sets (delay, priority,
1612 on_read ? GNUNET_NETWORK_get_fd (fd) : -1,
1613 on_write ? GNUNET_NETWORK_get_fd (fd) : -1,
1620 * Schedule a new task to be run with a specified delay or when the
1621 * specified file descriptor is ready for reading. The delay can be
1622 * used as a timeout on the socket being ready. The task will be
1623 * scheduled for execution once either the delay has expired or the
1624 * socket operation is ready. It will be run with the DEFAULT priority.
1626 * @param delay when should this operation time out?
1627 * @param rfd read file-descriptor
1628 * @param task main function of the task
1629 * @param task_cls closure of @a task
1630 * @return unique task identifier for the job
1631 * only valid until @a task is started!
1633 struct GNUNET_SCHEDULER_Task *
1634 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1635 const struct GNUNET_DISK_FileHandle *rfd,
1636 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1638 return GNUNET_SCHEDULER_add_file_with_priority (
1639 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1640 rfd, GNUNET_YES, GNUNET_NO,
1646 * Schedule a new task to be run with a specified delay or when the
1647 * specified file descriptor is ready for writing. The delay can be
1648 * used as a timeout on the socket being ready. The task will be
1649 * scheduled for execution once either the delay has expired or the
1650 * socket operation is ready. It will be run with the DEFAULT priority.
1652 * @param delay when should this operation time out?
1653 * @param wfd write file-descriptor
1654 * @param task main function of the task
1655 * @param task_cls closure of @a task
1656 * @return unique task identifier for the job
1657 * only valid until @a task is started!
1659 struct GNUNET_SCHEDULER_Task *
1660 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1661 const struct GNUNET_DISK_FileHandle *wfd,
1662 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1664 return GNUNET_SCHEDULER_add_file_with_priority (
1665 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1666 wfd, GNUNET_NO, GNUNET_YES,
1672 * Schedule a new task to be run with a specified delay or when the
1673 * specified file descriptor is ready. The delay can be
1674 * used as a timeout on the socket being ready. The task will be
1675 * scheduled for execution once either the delay has expired or the
1676 * socket operation is ready.
1678 * @param delay when should this operation time out?
1679 * @param priority priority of the task
1680 * @param fd file-descriptor
1681 * @param on_read whether to poll the file-descriptor for readability
1682 * @param on_write whether to poll the file-descriptor for writability
1683 * @param task main function of the task
1684 * @param task_cls closure of @a task
1685 * @return unique task identifier for the job
1686 * only valid until @a task is started!
1688 struct GNUNET_SCHEDULER_Task *
1689 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1690 enum GNUNET_SCHEDULER_Priority priority,
1691 const struct GNUNET_DISK_FileHandle *fd,
1692 int on_read, int on_write,
1693 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1696 struct GNUNET_NETWORK_FDSet *s;
1697 struct GNUNET_SCHEDULER_Task * ret;
1699 GNUNET_assert (NULL != fd);
1700 s = GNUNET_NETWORK_fdset_create ();
1701 GNUNET_NETWORK_fdset_handle_set (s, fd);
1702 ret = GNUNET_SCHEDULER_add_select (
1705 on_write ? s : NULL,
1707 GNUNET_NETWORK_fdset_destroy (s);
1712 GNUNET_DISK_internal_file_handle_ (fd, &real_fd, sizeof (int));
1713 GNUNET_assert (real_fd >= 0);
1714 return add_without_sets (
1716 on_read ? real_fd : -1,
1717 on_write ? real_fd : -1,
1724 * Schedule a new task to be run with a specified delay or when any of
1725 * the specified file descriptor sets is ready. The delay can be used
1726 * as a timeout on the socket(s) being ready. The task will be
1727 * scheduled for execution once either the delay has expired or any of
1728 * the socket operations is ready. This is the most general
1729 * function of the "add" family. Note that the "prerequisite_task"
1730 * must be satisfied in addition to any of the other conditions. In
1731 * other words, the task will be started when
1733 * (prerequisite-run)
1736 * || any-ws-ready) )
1739 * @param prio how important is this task?
1740 * @param delay how long should we wait?
1741 * @param rs set of file descriptors we want to read (can be NULL)
1742 * @param ws set of file descriptors we want to write (can be NULL)
1743 * @param task main function of the task
1744 * @param task_cls closure of @a task
1745 * @return unique task identifier for the job
1746 * only valid until @a task is started!
1748 struct GNUNET_SCHEDULER_Task *
1749 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1750 struct GNUNET_TIME_Relative delay,
1751 const struct GNUNET_NETWORK_FDSet *rs,
1752 const struct GNUNET_NETWORK_FDSet *ws,
1753 GNUNET_SCHEDULER_TaskCallback task,
1756 struct GNUNET_SCHEDULER_Task *t;
1758 if ( (NULL == rs) &&
1760 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1764 GNUNET_assert (NULL != active_task);
1765 GNUNET_assert (NULL != task);
1766 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1768 t->callback_cls = task_cls;
1773 t->read_set = GNUNET_NETWORK_fdset_create ();
1774 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1778 t->write_set = GNUNET_NETWORK_fdset_create ();
1779 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1782 t->start_time = GNUNET_TIME_absolute_get ();
1784 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1786 check_priority ((prio ==
1787 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1789 t->lifeness = current_lifeness;
1790 GNUNET_CONTAINER_DLL_insert (pending_head,
1793 max_priority_added = GNUNET_MAX (max_priority_added,
1795 LOG (GNUNET_ERROR_TYPE_DEBUG,
1804 * Function used by event-loop implementations to signal the scheduler
1805 * that a particular @a task is ready due to an event of type @a et.
1807 * This function will then queue the task to notify the application
1808 * that the task is ready (with the respective priority).
1810 * @param task the task that is ready, NULL for wake up calls
1811 * @param et information about why the task is ready
1814 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1815 enum GNUNET_SCHEDULER_EventType et)
1817 enum GNUNET_SCHEDULER_Reason reason;
1818 struct GNUNET_TIME_Absolute now;
1820 now = GNUNET_TIME_absolute_get ();
1821 reason = task->reason;
1822 if (now.abs_value_us >= task->timeout.abs_value_us)
1823 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1824 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1825 (0 != (GNUNET_SCHEDULER_ET_IN & et)) )
1826 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
1827 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
1828 (0 != (GNUNET_SCHEDULER_ET_OUT & et)) )
1829 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
1830 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1831 task->reason = reason;
1832 task->fds = &task->fdx;
1835 queue_ready_task (task);
1840 * Function called by the driver to tell the scheduler to run some of
1841 * the tasks that are ready. This function may return even though
1842 * there are tasks left to run just to give other tasks a chance as
1843 * well. If we return #GNUNET_YES, the driver should call this
1844 * function again as soon as possible, while if we return #GNUNET_NO
1845 * it must block until the operating system has more work as the
1846 * scheduler has no more work to do right now.
1848 * @param sh scheduler handle that was given to the `loop`
1849 * @return #GNUNET_OK if there are more tasks that are ready,
1850 * and thus we would like to run more (yield to avoid
1851 * blocking other activities for too long)
1852 * #GNUNET_NO if we are done running tasks (yield to block)
1853 * #GNUNET_SYSERR on error
1856 GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh)
1858 enum GNUNET_SCHEDULER_Priority p;
1859 struct GNUNET_SCHEDULER_Task *pos;
1860 struct GNUNET_TIME_Absolute now;
1862 /* check for tasks that reached the timeout! */
1863 now = GNUNET_TIME_absolute_get ();
1864 while (NULL != (pos = pending_timeout_head))
1866 if (now.abs_value_us >= pos->timeout.abs_value_us)
1867 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1868 if (0 == pos->reason)
1870 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1871 pending_timeout_tail,
1873 if (pending_timeout_last == pos)
1874 pending_timeout_last = NULL;
1875 queue_ready_task (pos);
1878 if (0 == ready_count)
1881 /* find out which task priority level we are going to
1882 process this time */
1883 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
1884 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
1885 /* yes, p>0 is correct, 0 is "KEEP" which should
1886 * always be an empty queue (see assertion)! */
1887 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
1889 pos = ready_head[p];
1893 GNUNET_assert (NULL != pos); /* ready_count wrong? */
1895 /* process all tasks at this priority level, then yield */
1896 while (NULL != (pos = ready_head[p]))
1898 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1902 current_priority = pos->priority;
1903 current_lifeness = pos->lifeness;
1906 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
1907 DELAY_THRESHOLD.rel_value_us)
1909 LOG (GNUNET_ERROR_TYPE_DEBUG,
1910 "Task %p took %s to be scheduled\n",
1912 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
1916 tc.reason = pos->reason;
1917 GNUNET_NETWORK_fdset_zero (sh->rs);
1918 GNUNET_NETWORK_fdset_zero (sh->ws);
1919 tc.fds_len = pos->fds_len;
1921 tc.read_ready = (NULL == pos->read_set) ? sh->rs : pos->read_set;
1922 if ( (-1 != pos->read_fd) &&
1923 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
1924 GNUNET_NETWORK_fdset_set_native (sh->rs,
1926 tc.write_ready = (NULL == pos->write_set) ? sh->ws : pos->write_set;
1927 if ((-1 != pos->write_fd) &&
1928 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
1929 GNUNET_NETWORK_fdset_set_native (sh->ws,
1931 LOG (GNUNET_ERROR_TYPE_DEBUG,
1932 "Running task: %p\n",
1934 pos->callback (pos->callback_cls);
1936 dump_backtrace (pos);
1940 if (0 == ready_count)
1947 * Initialize and run scheduler. This function will return when all
1948 * tasks have completed. On systems with signals, receiving a SIGTERM
1949 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
1950 * to be run after the active task is complete. As a result, SIGTERM
1951 * causes all shutdown tasks to be scheduled with reason
1952 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
1953 * afterwards will execute normally!). Note that any particular
1954 * signal will only shut down one scheduler; applications should
1955 * always only create a single scheduler.
1957 * @param driver drive to use for the event loop
1958 * @param task task to run first (and immediately)
1959 * @param task_cls closure of @a task
1960 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1963 GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
1964 GNUNET_SCHEDULER_TaskCallback task,
1968 struct GNUNET_SIGNAL_Context *shc_int;
1969 struct GNUNET_SIGNAL_Context *shc_term;
1970 #if (SIGTERM != GNUNET_TERM_SIG)
1971 struct GNUNET_SIGNAL_Context *shc_gterm;
1974 struct GNUNET_SIGNAL_Context *shc_quit;
1975 struct GNUNET_SIGNAL_Context *shc_hup;
1976 struct GNUNET_SIGNAL_Context *shc_pipe;
1978 struct GNUNET_SCHEDULER_Task tsk;
1979 const struct GNUNET_DISK_FileHandle *pr;
1980 struct GNUNET_SCHEDULER_Handle sh;
1982 /* general set-up */
1983 GNUNET_assert (NULL == active_task);
1984 GNUNET_assert (NULL == shutdown_pipe_handle);
1985 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
1989 GNUNET_assert (NULL != shutdown_pipe_handle);
1990 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
1991 GNUNET_DISK_PIPE_END_READ);
1992 GNUNET_assert (NULL != pr);
1995 /* install signal handlers */
1996 LOG (GNUNET_ERROR_TYPE_DEBUG,
1997 "Registering signal handlers\n");
1998 shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
1999 &sighandler_shutdown);
2000 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2001 &sighandler_shutdown);
2002 #if (SIGTERM != GNUNET_TERM_SIG)
2003 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2004 &sighandler_shutdown);
2007 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2009 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2010 &sighandler_shutdown);
2011 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2012 &sighandler_shutdown);
2015 /* Setup initial tasks */
2016 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2017 current_lifeness = GNUNET_YES;
2023 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
2025 GNUNET_SCHEDULER_REASON_STARTUP,
2026 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
2027 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
2028 &GNUNET_OS_install_parent_control_handler,
2031 driver->set_wakeup (driver->cls,
2032 GNUNET_TIME_absolute_get ());
2034 /* begin main event loop */
2035 sh.rs = GNUNET_NETWORK_fdset_create ();
2036 sh.ws = GNUNET_NETWORK_fdset_create ();
2038 ret = driver->loop (driver->cls,
2040 GNUNET_NETWORK_fdset_destroy (sh.rs);
2041 GNUNET_NETWORK_fdset_destroy (sh.ws);
2043 /* uninstall signal handlers */
2044 GNUNET_SIGNAL_handler_uninstall (shc_int);
2045 GNUNET_SIGNAL_handler_uninstall (shc_term);
2046 #if (SIGTERM != GNUNET_TERM_SIG)
2047 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
2050 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
2051 GNUNET_SIGNAL_handler_uninstall (shc_quit);
2052 GNUNET_SIGNAL_handler_uninstall (shc_hup);
2054 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2055 shutdown_pipe_handle = NULL;
2061 * Obtain the driver for using select() as the event loop.
2063 * @return NULL on error
2065 const struct GNUNET_SCHEDULER_Driver *
2066 GNUNET_SCHEDULER_driver_select ()
2068 GNUNET_break (0); // not implemented
2073 /* end of scheduler.c */