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 GNUNET_SCHEDULER_run_with_optional_signals(GNUNET_YES, task, task_cls);
794 GNUNET_SCHEDULER_run_with_optional_signals (int install_signals,
795 GNUNET_SCHEDULER_TaskCallback task,
798 struct GNUNET_NETWORK_FDSet *rs;
799 struct GNUNET_NETWORK_FDSet *ws;
800 struct GNUNET_TIME_Relative timeout;
802 struct GNUNET_SIGNAL_Context *shc_int;
803 struct GNUNET_SIGNAL_Context *shc_term;
804 #if (SIGTERM != GNUNET_TERM_SIG)
805 struct GNUNET_SIGNAL_Context *shc_gterm;
809 struct GNUNET_SIGNAL_Context *shc_quit;
810 struct GNUNET_SIGNAL_Context *shc_hup;
811 struct GNUNET_SIGNAL_Context *shc_pipe;
813 unsigned long long last_tr;
814 unsigned int busy_wait_warning;
815 const struct GNUNET_DISK_FileHandle *pr;
818 GNUNET_assert (NULL == active_task);
819 rs = GNUNET_NETWORK_fdset_create ();
820 ws = GNUNET_NETWORK_fdset_create ();
821 GNUNET_assert (NULL == shutdown_pipe_handle);
822 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
826 GNUNET_assert (NULL != shutdown_pipe_handle);
827 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
828 GNUNET_DISK_PIPE_END_READ);
829 GNUNET_assert (NULL != pr);
832 if (GNUNET_YES == install_signals)
834 LOG (GNUNET_ERROR_TYPE_DEBUG,
835 "Registering signal handlers\n");
836 shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
837 &sighandler_shutdown);
838 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
839 &sighandler_shutdown);
840 #if (SIGTERM != GNUNET_TERM_SIG)
841 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
842 &sighandler_shutdown);
845 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
847 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
848 &sighandler_shutdown);
849 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
850 &sighandler_shutdown);
854 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
855 current_lifeness = GNUNET_YES;
856 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
858 GNUNET_SCHEDULER_REASON_STARTUP,
859 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
860 active_task = (void *) (long) -1; /* force passing of sanity check */
861 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
862 &GNUNET_OS_install_parent_control_handler,
866 busy_wait_warning = 0;
867 while (GNUNET_OK == check_lifeness ())
869 GNUNET_NETWORK_fdset_zero (rs);
870 GNUNET_NETWORK_fdset_zero (ws);
871 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
872 update_sets (rs, ws, &timeout);
873 GNUNET_NETWORK_fdset_handle_set (rs, pr);
876 /* no blocking, more work already ready! */
877 timeout = GNUNET_TIME_UNIT_ZERO;
879 if (NULL == scheduler_select)
880 ret = GNUNET_NETWORK_socket_select (rs,
885 ret = scheduler_select (scheduler_select_cls,
890 if (ret == GNUNET_SYSERR)
895 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
900 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
903 if (0 != system (lsof))
904 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
909 struct GNUNET_SCHEDULER_Task *t;
911 for (t = pending_head; NULL != t; t = t->next)
913 if (-1 != t->read_fd)
915 int flags = fcntl (t->read_fd, F_GETFD);
916 if ((flags == -1) && (errno == EBADF))
918 LOG (GNUNET_ERROR_TYPE_ERROR,
919 "Got invalid file descriptor %d!\n",
924 if (-1 != t->write_fd)
926 int flags = fcntl (t->write_fd, F_GETFD);
927 if ((flags == -1) && (errno == EBADF))
929 LOG (GNUNET_ERROR_TYPE_ERROR,
930 "Got invalid file descriptor %d!\n",
942 (0 == timeout.rel_value_us) &&
943 (busy_wait_warning > 16) )
945 LOG (GNUNET_ERROR_TYPE_WARNING,
946 "Looks like we're busy waiting...\n");
947 short_wait (100); /* mitigate */
949 check_ready (rs, ws);
951 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
953 /* consume the signal */
954 GNUNET_DISK_file_read (pr, &c, sizeof (c));
955 /* mark all active tasks as ready due to shutdown */
956 GNUNET_SCHEDULER_shutdown ();
958 if (last_tr == tasks_run)
966 busy_wait_warning = 0;
970 if (GNUNET_YES == install_signals)
972 GNUNET_SIGNAL_handler_uninstall (shc_int);
973 GNUNET_SIGNAL_handler_uninstall (shc_term);
974 #if (SIGTERM != GNUNET_TERM_SIG)
975 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
978 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
979 GNUNET_SIGNAL_handler_uninstall (shc_quit);
980 GNUNET_SIGNAL_handler_uninstall (shc_hup);
984 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
985 shutdown_pipe_handle = NULL;
986 GNUNET_NETWORK_fdset_destroy (rs);
987 GNUNET_NETWORK_fdset_destroy (ws);
992 * Obtain the task context, giving the reason why the current task was
995 * @return current tasks' scheduler context
997 const struct GNUNET_SCHEDULER_TaskContext *
998 GNUNET_SCHEDULER_get_task_context ()
1000 GNUNET_assert (NULL != active_task);
1006 * Get information about the current load of this scheduler. Use this
1007 * function to determine if an elective task should be added or simply
1008 * dropped (if the decision should be made based on the number of
1009 * tasks ready to run).
1011 * @param p priority level to look at
1012 * @return number of tasks pending right now
1015 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
1017 struct GNUNET_SCHEDULER_Task *pos;
1020 GNUNET_assert (NULL != active_task);
1021 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
1023 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
1024 p = current_priority;
1026 for (pos = ready_head[check_priority (p)]; NULL != pos; pos = pos->next)
1033 * Cancel the task with the specified identifier.
1034 * The task must not yet have run.
1036 * @param task id of the task to cancel
1037 * @return original closure of the task
1040 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
1042 enum GNUNET_SCHEDULER_Priority p;
1045 GNUNET_assert ( (NULL != active_task) ||
1046 (GNUNET_NO == task->lifeness) );
1047 if (! task->in_ready_list)
1049 if ( (-1 == task->read_fd) &&
1050 (-1 == task->write_fd) &&
1051 (NULL == task->read_set) &&
1052 (NULL == task->write_set) )
1054 if (GNUNET_YES == task->on_shutdown)
1055 GNUNET_CONTAINER_DLL_remove (shutdown_head,
1059 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1060 pending_timeout_tail,
1062 if (task == pending_timeout_last)
1063 pending_timeout_last = NULL;
1067 GNUNET_CONTAINER_DLL_remove (pending_head,
1074 p = check_priority (task->priority);
1075 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1080 ret = task->callback_cls;
1081 LOG (GNUNET_ERROR_TYPE_DEBUG,
1082 "Canceling task %p\n",
1084 destroy_task (task);
1090 * Initialize backtrace data for task @a t
1092 * @param t task to initialize
1095 init_backtrace (struct GNUNET_SCHEDULER_Task *t)
1098 void *backtrace_array[MAX_TRACE_DEPTH];
1100 t->num_backtrace_strings
1101 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1102 t->backtrace_strings =
1103 backtrace_symbols (backtrace_array,
1104 t->num_backtrace_strings);
1111 * Continue the current execution with the given function. This is
1112 * similar to the other "add" functions except that there is no delay
1113 * and the reason code can be specified.
1115 * @param task main function of the task
1116 * @param task_cls closure for @a task
1117 * @param reason reason for task invocation
1118 * @param priority priority to use for the task
1121 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
1123 enum GNUNET_SCHEDULER_Reason reason,
1124 enum GNUNET_SCHEDULER_Priority priority)
1126 struct GNUNET_SCHEDULER_Task *t;
1128 GNUNET_assert (NULL != task);
1129 GNUNET_assert ((NULL != active_task) ||
1130 (GNUNET_SCHEDULER_REASON_STARTUP == reason));
1131 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1135 t->callback_cls = task_cls;
1137 t->start_time = GNUNET_TIME_absolute_get ();
1140 t->priority = priority;
1141 t->lifeness = current_lifeness;
1142 LOG (GNUNET_ERROR_TYPE_DEBUG,
1143 "Adding continuation task %p\n",
1146 queue_ready_task (t);
1151 * Schedule a new task to be run at the specified time. The task
1152 * will be scheduled for execution at time @a at.
1154 * @param at time when the operation should run
1155 * @param priority priority to use for the task
1156 * @param task main function of the task
1157 * @param task_cls closure of @a task
1158 * @return unique task identifier for the job
1159 * only valid until @a task is started!
1161 struct GNUNET_SCHEDULER_Task *
1162 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1163 enum GNUNET_SCHEDULER_Priority priority,
1164 GNUNET_SCHEDULER_TaskCallback task,
1167 struct GNUNET_SCHEDULER_Task *t;
1168 struct GNUNET_SCHEDULER_Task *pos;
1169 struct GNUNET_SCHEDULER_Task *prev;
1171 GNUNET_assert (NULL != active_task);
1172 GNUNET_assert (NULL != task);
1173 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1175 t->callback_cls = task_cls;
1179 t->start_time = GNUNET_TIME_absolute_get ();
1182 t->priority = priority;
1183 t->lifeness = current_lifeness;
1184 /* try tail first (optimization in case we are
1185 * appending to a long list of tasks with timeouts) */
1186 if ( (NULL == pending_timeout_head) ||
1187 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us) )
1189 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1190 pending_timeout_tail,
1195 /* first move from heuristic start backwards to before start time */
1196 prev = pending_timeout_last;
1197 while ( (NULL != prev) &&
1198 (prev->timeout.abs_value_us > t->timeout.abs_value_us) )
1200 /* now, move from heuristic start (or head of list) forward to insertion point */
1202 pos = pending_timeout_head;
1205 while ( (NULL != pos) &&
1206 ( (pos->timeout.abs_value_us <= t->timeout.abs_value_us) ||
1207 (0 != pos->reason) ) )
1212 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1213 pending_timeout_tail,
1217 /* finally, update heuristic insertion point to last insertion... */
1218 pending_timeout_last = t;
1220 LOG (GNUNET_ERROR_TYPE_DEBUG,
1221 "Adding task: %p\n",
1229 * Schedule a new task to be run with a specified delay. The task
1230 * will be scheduled for execution once the delay has expired.
1232 * @param delay when should this operation time out?
1233 * @param priority priority to use for the task
1234 * @param task main function of the task
1235 * @param task_cls closure of @a task
1236 * @return unique task identifier for the job
1237 * only valid until @a task is started!
1239 struct GNUNET_SCHEDULER_Task *
1240 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1241 enum GNUNET_SCHEDULER_Priority priority,
1242 GNUNET_SCHEDULER_TaskCallback task,
1245 return GNUNET_SCHEDULER_add_at_with_priority (GNUNET_TIME_relative_to_absolute (delay),
1253 * Schedule a new task to be run with a specified priority.
1255 * @param prio how important is the new task?
1256 * @param task main function of the task
1257 * @param task_cls closure of @a task
1258 * @return unique task identifier for the job
1259 * only valid until @a task is started!
1261 struct GNUNET_SCHEDULER_Task *
1262 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1263 GNUNET_SCHEDULER_TaskCallback task,
1266 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1274 * Schedule a new task to be run at the specified time. The task
1275 * will be scheduled for execution once specified time has been
1276 * reached. It will be run with the DEFAULT priority.
1278 * @param at time at which this operation should run
1279 * @param task main function of the task
1280 * @param task_cls closure of @a task
1281 * @return unique task identifier for the job
1282 * only valid until @a task is started!
1284 struct GNUNET_SCHEDULER_Task *
1285 GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1286 GNUNET_SCHEDULER_TaskCallback task,
1289 return GNUNET_SCHEDULER_add_at_with_priority (at,
1290 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1297 * Schedule a new task to be run with a specified delay. The task
1298 * will be scheduled for execution once the delay has expired. It
1299 * will be run with the DEFAULT priority.
1301 * @param delay when should this operation time out?
1302 * @param task main function of the task
1303 * @param task_cls closure of @a task
1304 * @return unique task identifier for the job
1305 * only valid until @a task is started!
1307 struct GNUNET_SCHEDULER_Task *
1308 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1309 GNUNET_SCHEDULER_TaskCallback task,
1312 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1313 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1320 * Schedule a new task to be run as soon as possible. Note that this
1321 * does not guarantee that this will be the next task that is being
1322 * run, as other tasks with higher priority (or that are already ready
1323 * to run) might get to run first. Just as with delays, clients must
1324 * not rely on any particular order of execution between tasks
1325 * scheduled concurrently.
1327 * The task will be run with the DEFAULT priority.
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_now (GNUNET_SCHEDULER_TaskCallback task,
1338 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO,
1345 * Schedule a new task to be run on shutdown, that is when a CTRL-C
1346 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
1349 * @param task main function of the task
1350 * @param task_cls closure of @a task
1351 * @return unique task identifier for the job
1352 * only valid until @a task is started!
1354 struct GNUNET_SCHEDULER_Task *
1355 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1358 struct GNUNET_SCHEDULER_Task *t;
1360 GNUNET_assert (NULL != active_task);
1361 GNUNET_assert (NULL != task);
1362 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1364 t->callback_cls = task_cls;
1368 t->start_time = GNUNET_TIME_absolute_get ();
1370 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1371 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1372 t->on_shutdown = GNUNET_YES;
1373 t->lifeness = GNUNET_YES;
1374 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1377 LOG (GNUNET_ERROR_TYPE_DEBUG,
1378 "Adding task: %p\n",
1386 * Schedule a new task to be run as soon as possible with the
1387 * (transitive) ignore-shutdown flag either explicitly set or
1388 * explicitly enabled. This task (and all tasks created from it,
1389 * other than by another call to this function) will either count or
1390 * not count for the "lifeness" of the process. This API is only
1391 * useful in a few special cases.
1393 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
1394 * @param task main function of the task
1395 * @param task_cls closure of @a task
1396 * @return unique task identifier for the job
1397 * only valid until @a task is started!
1399 struct GNUNET_SCHEDULER_Task *
1400 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1401 GNUNET_SCHEDULER_TaskCallback task,
1404 struct GNUNET_SCHEDULER_Task *ret;
1406 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1407 ret->lifeness = lifeness;
1413 * Schedule a new task to be run with a specified delay or when any of
1414 * the specified file descriptor sets is ready. The delay can be used
1415 * as a timeout on the socket(s) being ready. The task will be
1416 * scheduled for execution once either the delay has expired or any of
1417 * the socket operations is ready. This is the most general
1418 * function of the "add" family. Note that the "prerequisite_task"
1419 * must be satisfied in addition to any of the other conditions. In
1420 * other words, the task will be started when
1422 * (prerequisite-run)
1428 * @param delay how long should we wait?
1429 * @param priority priority to use
1430 * @param rfd file descriptor we want to read (can be -1)
1431 * @param wfd file descriptors we want to write (can be -1)
1432 * @param task main function of the task
1433 * @param task_cls closure of @a task
1434 * @return unique task identifier for the job
1435 * only valid until @a task is started!
1438 static struct GNUNET_SCHEDULER_Task *
1439 add_without_sets (struct GNUNET_TIME_Relative delay,
1440 enum GNUNET_SCHEDULER_Priority priority,
1443 GNUNET_SCHEDULER_TaskCallback task,
1446 struct GNUNET_SCHEDULER_Task *t;
1448 GNUNET_assert (NULL != active_task);
1449 GNUNET_assert (NULL != task);
1450 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1452 t->callback_cls = task_cls;
1456 int flags = fcntl (rfd, F_GETFD);
1458 if ((flags == -1) && (errno == EBADF))
1460 LOG (GNUNET_ERROR_TYPE_ERROR,
1461 "Got invalid file descriptor %d!\n",
1469 int flags = fcntl (wfd, F_GETFD);
1471 if (flags == -1 && errno == EBADF)
1473 LOG (GNUNET_ERROR_TYPE_ERROR,
1474 "Got invalid file descriptor %d!\n",
1482 GNUNET_assert (wfd >= -1);
1485 t->start_time = GNUNET_TIME_absolute_get ();
1487 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1488 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1489 t->lifeness = current_lifeness;
1490 GNUNET_CONTAINER_DLL_insert (pending_head,
1493 max_priority_added = GNUNET_MAX (max_priority_added,
1495 LOG (GNUNET_ERROR_TYPE_DEBUG,
1505 * Schedule a new task to be run with a specified delay or when the
1506 * specified file descriptor is ready for reading. The delay can be
1507 * used as a timeout on the socket being ready. The task will be
1508 * scheduled for execution once either the delay has expired or the
1509 * socket operation is ready. It will be run with the DEFAULT priority.
1511 * @param delay when should this operation time out?
1512 * @param rfd read file-descriptor
1513 * @param task main function of the task
1514 * @param task_cls closure of @a task
1515 * @return unique task identifier for the job
1516 * only valid until @a task is started!
1518 struct GNUNET_SCHEDULER_Task *
1519 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1520 struct GNUNET_NETWORK_Handle *rfd,
1521 GNUNET_SCHEDULER_TaskCallback task,
1524 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1525 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1526 rfd, task, task_cls);
1531 * Schedule a new task to be run with a specified priority and to be
1532 * run after the specified delay or when the specified file descriptor
1533 * is ready for reading. The delay can be used as a timeout on the
1534 * socket being ready. The task will be scheduled for execution once
1535 * either the delay has expired or the socket operation is ready. It
1536 * will be run with the DEFAULT priority.
1538 * @param delay when should this operation time out?
1539 * @param priority priority to use for the task
1540 * @param rfd read file-descriptor
1541 * @param task main function of the task
1542 * @param task_cls closure of @a task
1543 * @return unique task identifier for the job
1544 * only valid until @a task is started!
1546 struct GNUNET_SCHEDULER_Task *
1547 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1548 enum GNUNET_SCHEDULER_Priority priority,
1549 struct GNUNET_NETWORK_Handle *rfd,
1550 GNUNET_SCHEDULER_TaskCallback task,
1553 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1562 * Schedule a new task to be run with a specified delay or when the
1563 * specified file descriptor is ready for writing. The delay can be
1564 * used as a timeout on the socket being ready. The task will be
1565 * scheduled for execution once either the delay has expired or the
1566 * socket operation is ready. It will be run with the priority of
1569 * @param delay when should this operation time out?
1570 * @param wfd write file-descriptor
1571 * @param task main function of the task
1572 * @param task_cls closure of @a task
1573 * @return unique task identifier for the job
1574 * only valid until @a task is started!
1576 struct GNUNET_SCHEDULER_Task *
1577 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1578 struct GNUNET_NETWORK_Handle *wfd,
1579 GNUNET_SCHEDULER_TaskCallback task,
1582 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1583 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1585 GNUNET_NO, GNUNET_YES,
1590 * Schedule a new task to be run with a specified delay or when the
1591 * specified file descriptor is ready. The delay can be
1592 * used as a timeout on the socket being ready. The task will be
1593 * scheduled for execution once either the delay has expired or the
1594 * socket operation is ready.
1596 * @param delay when should this operation time out?
1597 * @param priority priority of the task
1598 * @param fd file-descriptor
1599 * @param on_read whether to poll the file-descriptor for readability
1600 * @param on_write whether to poll the file-descriptor for writability
1601 * @param task main function of the task
1602 * @param task_cls closure of task
1603 * @return unique task identifier for the job
1604 * only valid until "task" is started!
1606 struct GNUNET_SCHEDULER_Task *
1607 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1608 enum GNUNET_SCHEDULER_Priority priority,
1609 struct GNUNET_NETWORK_Handle *fd,
1612 GNUNET_SCHEDULER_TaskCallback task,
1616 struct GNUNET_NETWORK_FDSet *s;
1617 struct GNUNET_SCHEDULER_Task * ret;
1619 GNUNET_assert (NULL != fd);
1620 s = GNUNET_NETWORK_fdset_create ();
1621 GNUNET_NETWORK_fdset_set (s, fd);
1622 ret = GNUNET_SCHEDULER_add_select (
1625 on_write ? s : NULL,
1627 GNUNET_NETWORK_fdset_destroy (s);
1630 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1631 return add_without_sets (delay, priority,
1632 on_read ? GNUNET_NETWORK_get_fd (fd) : -1,
1633 on_write ? GNUNET_NETWORK_get_fd (fd) : -1,
1640 * Schedule a new task to be run with a specified delay or when the
1641 * specified file descriptor is ready for reading. The delay can be
1642 * used as a timeout on the socket being ready. The task will be
1643 * scheduled for execution once either the delay has expired or the
1644 * socket operation is ready. It will be run with the DEFAULT priority.
1646 * @param delay when should this operation time out?
1647 * @param rfd read file-descriptor
1648 * @param task main function of the task
1649 * @param task_cls closure of @a task
1650 * @return unique task identifier for the job
1651 * only valid until @a task is started!
1653 struct GNUNET_SCHEDULER_Task *
1654 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1655 const struct GNUNET_DISK_FileHandle *rfd,
1656 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1658 return GNUNET_SCHEDULER_add_file_with_priority (
1659 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1660 rfd, GNUNET_YES, GNUNET_NO,
1666 * Schedule a new task to be run with a specified delay or when the
1667 * specified file descriptor is ready for writing. The delay can be
1668 * used as a timeout on the socket being ready. The task will be
1669 * scheduled for execution once either the delay has expired or the
1670 * socket operation is ready. It will be run with the DEFAULT priority.
1672 * @param delay when should this operation time out?
1673 * @param wfd write file-descriptor
1674 * @param task main function of the task
1675 * @param task_cls closure of @a task
1676 * @return unique task identifier for the job
1677 * only valid until @a task is started!
1679 struct GNUNET_SCHEDULER_Task *
1680 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1681 const struct GNUNET_DISK_FileHandle *wfd,
1682 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1684 return GNUNET_SCHEDULER_add_file_with_priority (
1685 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1686 wfd, GNUNET_NO, GNUNET_YES,
1692 * Schedule a new task to be run with a specified delay or when the
1693 * specified file descriptor is ready. The delay can be
1694 * used as a timeout on the socket being ready. The task will be
1695 * scheduled for execution once either the delay has expired or the
1696 * socket operation is ready.
1698 * @param delay when should this operation time out?
1699 * @param priority priority of the task
1700 * @param fd file-descriptor
1701 * @param on_read whether to poll the file-descriptor for readability
1702 * @param on_write whether to poll the file-descriptor for writability
1703 * @param task main function of the task
1704 * @param task_cls closure of @a task
1705 * @return unique task identifier for the job
1706 * only valid until @a task is started!
1708 struct GNUNET_SCHEDULER_Task *
1709 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1710 enum GNUNET_SCHEDULER_Priority priority,
1711 const struct GNUNET_DISK_FileHandle *fd,
1712 int on_read, int on_write,
1713 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1716 struct GNUNET_NETWORK_FDSet *s;
1717 struct GNUNET_SCHEDULER_Task * ret;
1719 GNUNET_assert (NULL != fd);
1720 s = GNUNET_NETWORK_fdset_create ();
1721 GNUNET_NETWORK_fdset_handle_set (s, fd);
1722 ret = GNUNET_SCHEDULER_add_select (
1725 on_write ? s : NULL,
1727 GNUNET_NETWORK_fdset_destroy (s);
1732 GNUNET_DISK_internal_file_handle_ (fd, &real_fd, sizeof (int));
1733 GNUNET_assert (real_fd >= 0);
1734 return add_without_sets (
1736 on_read ? real_fd : -1,
1737 on_write ? real_fd : -1,
1744 * Schedule a new task to be run with a specified delay or when any of
1745 * the specified file descriptor sets is ready. The delay can be used
1746 * as a timeout on the socket(s) being ready. The task will be
1747 * scheduled for execution once either the delay has expired or any of
1748 * the socket operations is ready. This is the most general
1749 * function of the "add" family. Note that the "prerequisite_task"
1750 * must be satisfied in addition to any of the other conditions. In
1751 * other words, the task will be started when
1753 * (prerequisite-run)
1756 * || any-ws-ready) )
1759 * @param prio how important is this task?
1760 * @param delay how long should we wait?
1761 * @param rs set of file descriptors we want to read (can be NULL)
1762 * @param ws set of file descriptors we want to write (can be NULL)
1763 * @param task main function of the task
1764 * @param task_cls closure of @a task
1765 * @return unique task identifier for the job
1766 * only valid until @a task is started!
1768 struct GNUNET_SCHEDULER_Task *
1769 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1770 struct GNUNET_TIME_Relative delay,
1771 const struct GNUNET_NETWORK_FDSet *rs,
1772 const struct GNUNET_NETWORK_FDSet *ws,
1773 GNUNET_SCHEDULER_TaskCallback task,
1776 struct GNUNET_SCHEDULER_Task *t;
1778 if ( (NULL == rs) &&
1780 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1784 GNUNET_assert (NULL != active_task);
1785 GNUNET_assert (NULL != task);
1786 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1788 t->callback_cls = task_cls;
1793 t->read_set = GNUNET_NETWORK_fdset_create ();
1794 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1798 t->write_set = GNUNET_NETWORK_fdset_create ();
1799 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1802 t->start_time = GNUNET_TIME_absolute_get ();
1804 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1806 check_priority ((prio ==
1807 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1809 t->lifeness = current_lifeness;
1810 GNUNET_CONTAINER_DLL_insert (pending_head,
1813 max_priority_added = GNUNET_MAX (max_priority_added,
1815 LOG (GNUNET_ERROR_TYPE_DEBUG,
1824 * Function used by event-loop implementations to signal the scheduler
1825 * that a particular @a task is ready due to an event of type @a et.
1827 * This function will then queue the task to notify the application
1828 * that the task is ready (with the respective priority).
1830 * @param task the task that is ready, NULL for wake up calls
1831 * @param et information about why the task is ready
1834 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1835 enum GNUNET_SCHEDULER_EventType et)
1837 enum GNUNET_SCHEDULER_Reason reason;
1838 struct GNUNET_TIME_Absolute now;
1840 now = GNUNET_TIME_absolute_get ();
1841 reason = task->reason;
1842 if (now.abs_value_us >= task->timeout.abs_value_us)
1843 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1844 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1845 (0 != (GNUNET_SCHEDULER_ET_IN & et)) )
1846 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
1847 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
1848 (0 != (GNUNET_SCHEDULER_ET_OUT & et)) )
1849 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
1850 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1851 task->reason = reason;
1852 task->fds = &task->fdx;
1855 queue_ready_task (task);
1860 * Function called by the driver to tell the scheduler to run some of
1861 * the tasks that are ready. This function may return even though
1862 * there are tasks left to run just to give other tasks a chance as
1863 * well. If we return #GNUNET_YES, the driver should call this
1864 * function again as soon as possible, while if we return #GNUNET_NO
1865 * it must block until the operating system has more work as the
1866 * scheduler has no more work to do right now.
1868 * @param sh scheduler handle that was given to the `loop`
1869 * @return #GNUNET_OK if there are more tasks that are ready,
1870 * and thus we would like to run more (yield to avoid
1871 * blocking other activities for too long)
1872 * #GNUNET_NO if we are done running tasks (yield to block)
1873 * #GNUNET_SYSERR on error
1876 GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh)
1878 enum GNUNET_SCHEDULER_Priority p;
1879 struct GNUNET_SCHEDULER_Task *pos;
1880 struct GNUNET_TIME_Absolute now;
1882 /* check for tasks that reached the timeout! */
1883 now = GNUNET_TIME_absolute_get ();
1884 while (NULL != (pos = pending_timeout_head))
1886 if (now.abs_value_us >= pos->timeout.abs_value_us)
1887 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1888 if (0 == pos->reason)
1890 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1891 pending_timeout_tail,
1893 if (pending_timeout_last == pos)
1894 pending_timeout_last = NULL;
1895 queue_ready_task (pos);
1898 if (0 == ready_count)
1901 /* find out which task priority level we are going to
1902 process this time */
1903 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
1904 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
1905 /* yes, p>0 is correct, 0 is "KEEP" which should
1906 * always be an empty queue (see assertion)! */
1907 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
1909 pos = ready_head[p];
1913 GNUNET_assert (NULL != pos); /* ready_count wrong? */
1915 /* process all tasks at this priority level, then yield */
1916 while (NULL != (pos = ready_head[p]))
1918 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1922 current_priority = pos->priority;
1923 current_lifeness = pos->lifeness;
1926 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
1927 DELAY_THRESHOLD.rel_value_us)
1929 LOG (GNUNET_ERROR_TYPE_DEBUG,
1930 "Task %p took %s to be scheduled\n",
1932 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
1936 tc.reason = pos->reason;
1937 GNUNET_NETWORK_fdset_zero (sh->rs);
1938 GNUNET_NETWORK_fdset_zero (sh->ws);
1939 tc.fds_len = pos->fds_len;
1941 tc.read_ready = (NULL == pos->read_set) ? sh->rs : pos->read_set;
1942 if ( (-1 != pos->read_fd) &&
1943 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
1944 GNUNET_NETWORK_fdset_set_native (sh->rs,
1946 tc.write_ready = (NULL == pos->write_set) ? sh->ws : pos->write_set;
1947 if ((-1 != pos->write_fd) &&
1948 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
1949 GNUNET_NETWORK_fdset_set_native (sh->ws,
1951 LOG (GNUNET_ERROR_TYPE_DEBUG,
1952 "Running task: %p\n",
1954 pos->callback (pos->callback_cls);
1956 dump_backtrace (pos);
1960 if (0 == ready_count)
1967 * Initialize and run scheduler. This function will return when all
1968 * tasks have completed. On systems with signals, receiving a SIGTERM
1969 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
1970 * to be run after the active task is complete. As a result, SIGTERM
1971 * causes all shutdown tasks to be scheduled with reason
1972 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
1973 * afterwards will execute normally!). Note that any particular
1974 * signal will only shut down one scheduler; applications should
1975 * always only create a single scheduler.
1977 * @param driver drive to use for the event loop
1978 * @param task task to run first (and immediately)
1979 * @param task_cls closure of @a task
1980 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
1983 GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
1984 GNUNET_SCHEDULER_TaskCallback task,
1988 struct GNUNET_SIGNAL_Context *shc_int;
1989 struct GNUNET_SIGNAL_Context *shc_term;
1990 #if (SIGTERM != GNUNET_TERM_SIG)
1991 struct GNUNET_SIGNAL_Context *shc_gterm;
1994 struct GNUNET_SIGNAL_Context *shc_quit;
1995 struct GNUNET_SIGNAL_Context *shc_hup;
1996 struct GNUNET_SIGNAL_Context *shc_pipe;
1998 struct GNUNET_SCHEDULER_Task tsk;
1999 const struct GNUNET_DISK_FileHandle *pr;
2000 struct GNUNET_SCHEDULER_Handle sh;
2002 /* general set-up */
2003 GNUNET_assert (NULL == active_task);
2004 GNUNET_assert (NULL == shutdown_pipe_handle);
2005 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
2009 GNUNET_assert (NULL != shutdown_pipe_handle);
2010 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
2011 GNUNET_DISK_PIPE_END_READ);
2012 GNUNET_assert (NULL != pr);
2015 /* install signal handlers */
2016 LOG (GNUNET_ERROR_TYPE_DEBUG,
2017 "Registering signal handlers\n");
2018 shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2019 &sighandler_shutdown);
2020 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2021 &sighandler_shutdown);
2022 #if (SIGTERM != GNUNET_TERM_SIG)
2023 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2024 &sighandler_shutdown);
2027 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2029 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2030 &sighandler_shutdown);
2031 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2032 &sighandler_shutdown);
2035 /* Setup initial tasks */
2036 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2037 current_lifeness = GNUNET_YES;
2043 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
2045 GNUNET_SCHEDULER_REASON_STARTUP,
2046 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
2047 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
2048 &GNUNET_OS_install_parent_control_handler,
2051 driver->set_wakeup (driver->cls,
2052 GNUNET_TIME_absolute_get ());
2054 /* begin main event loop */
2055 sh.rs = GNUNET_NETWORK_fdset_create ();
2056 sh.ws = GNUNET_NETWORK_fdset_create ();
2058 ret = driver->loop (driver->cls,
2060 GNUNET_NETWORK_fdset_destroy (sh.rs);
2061 GNUNET_NETWORK_fdset_destroy (sh.ws);
2063 /* uninstall signal handlers */
2064 GNUNET_SIGNAL_handler_uninstall (shc_int);
2065 GNUNET_SIGNAL_handler_uninstall (shc_term);
2066 #if (SIGTERM != GNUNET_TERM_SIG)
2067 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
2070 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
2071 GNUNET_SIGNAL_handler_uninstall (shc_quit);
2072 GNUNET_SIGNAL_handler_uninstall (shc_hup);
2074 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2075 shutdown_pipe_handle = NULL;
2081 * Obtain the driver for using select() as the event loop.
2083 * @return NULL on error
2085 const struct GNUNET_SCHEDULER_Driver *
2086 GNUNET_SCHEDULER_driver_select ()
2088 GNUNET_break (0); // not implemented
2093 /* end of scheduler.c */