2 This file is part of GNUnet
3 (C) 2009, 2011 Christian Grothoff (and other contributing authors)
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 2, 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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/scheduler.c
23 * @brief schedule computations using continuation passing style
24 * @author Christian Grothoff
27 #include "gnunet_common.h"
28 #include "gnunet_os_lib.h"
29 #include "gnunet_scheduler_lib.h"
30 #include "gnunet_signal_lib.h"
31 #include "gnunet_time_lib.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "util-scheduler", __VA_ARGS__)
36 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-scheduler", syscall)
43 * Use lsof to generate file descriptor reports on select error?
44 * (turn off for stable releases).
46 #define USE_LSOF GNUNET_NO
49 * Obtain trace information for all scheduler calls that schedule tasks.
51 #define EXECINFO GNUNET_NO
54 * Check each file descriptor before adding
56 #define DEBUG_FDS GNUNET_NO
59 * Depth of the traces collected via EXECINFO.
61 #define MAX_TRACE_DEPTH 50
65 * Should we figure out which tasks are delayed for a while
66 * before they are run? (Consider using in combination with EXECINFO).
68 #define PROFILE_DELAYS GNUNET_NO
71 * Task that were in the queue for longer than this are reported if
72 * PROFILE_DELAYS is active.
74 #define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
78 * Linked list of pending tasks.
83 * This is a linked list.
88 * Function to run when ready.
90 GNUNET_SCHEDULER_Task callback;
93 * Closure for the callback.
98 * Set of file descriptors this task is waiting
99 * for for reading. Once ready, this is updated
100 * to reflect the set of file descriptors ready
103 struct GNUNET_NETWORK_FDSet *read_set;
106 * Set of file descriptors this task is waiting for for writing.
107 * Once ready, this is updated to reflect the set of file
108 * descriptors ready for operation.
110 struct GNUNET_NETWORK_FDSet *write_set;
113 * Unique task identifier.
115 GNUNET_SCHEDULER_TaskIdentifier id;
118 * Identifier of a prerequisite task.
120 GNUNET_SCHEDULER_TaskIdentifier prereq_id;
123 * Absolute timeout value for the task, or
124 * GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
126 struct GNUNET_TIME_Absolute timeout;
130 * When was the task scheduled?
132 struct GNUNET_TIME_Absolute start_time;
136 * Why is the task ready? Set after task is added to ready queue.
137 * Initially set to zero. All reasons that have already been
138 * satisfied (i.e. read or write ready) will be set over time.
140 enum GNUNET_SCHEDULER_Reason reason;
145 enum GNUNET_SCHEDULER_Priority priority;
148 * Set if we only wait for reading from a single FD, otherwise -1.
153 * Set if we only wait for writing to a single FD, otherwise -1.
158 * Should the existence of this task in the queue be counted as
159 * reason to not shutdown the scheduler?
165 * Array of strings which make up a backtrace from the point when this
166 * task was scheduled (essentially, who scheduled the task?)
168 char **backtrace_strings;
171 * Size of the backtrace_strings array
173 int num_backtrace_strings;
181 * List of tasks waiting for an event.
183 static struct Task *pending;
186 * List of tasks waiting ONLY for a timeout event.
187 * Sorted by timeout (earliest first). Used so that
188 * we do not traverse the list of these tasks when
189 * building select sets (we just look at the head
190 * to determine the respective timeout ONCE).
192 static struct Task *pending_timeout;
195 * Last inserted task waiting ONLY for a timeout event.
196 * Used to (heuristically) speed up insertion.
198 static struct Task *pending_timeout_last;
201 * ID of the task that is running right now.
203 static struct Task *active_task;
206 * List of tasks ready to run right now,
207 * grouped by importance.
209 static struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
212 * Identity of the last task queued. Incremented for each task to
213 * generate a unique task ID (it is virtually impossible to start
214 * more than 2^64 tasks during the lifetime of a process).
216 static GNUNET_SCHEDULER_TaskIdentifier last_id;
219 * Highest number so that all tasks with smaller identifiers
220 * have already completed. Also the lowest number of a task
221 * still waiting to be executed.
223 static GNUNET_SCHEDULER_TaskIdentifier lowest_pending_id;
226 * Number of tasks on the ready list.
228 static unsigned int ready_count;
231 * How many tasks have we run so far?
233 static unsigned long long tasks_run;
236 * Priority of the task running right now. Only
237 * valid while a task is running.
239 static enum GNUNET_SCHEDULER_Priority current_priority;
242 * Priority of the highest task added in the current select
245 static enum GNUNET_SCHEDULER_Priority max_priority_added;
248 * Value of the 'lifeness' flag for the current task.
250 static int current_lifeness;
253 * Function to use as a select() in the scheduler.
254 * If NULL, we use GNUNET_NETWORK_socket_select ().
256 static GNUNET_SCHEDULER_select scheduler_select;
259 * Closure for 'scheduler_select'.
261 static void *scheduler_select_cls;
264 * Sets the select function to use in the scheduler (scheduler_select).
266 * @param new_select new select function to use
267 * @param new_select_cls closure for 'new_select'
268 * @return previously used select function, NULL for default
271 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
272 void *new_select_cls)
274 scheduler_select = new_select;
275 scheduler_select_cls = new_select_cls;
280 * Check that the given priority is legal (and return it).
282 * @param p priority value to check
283 * @return p on success, 0 on error
285 static enum GNUNET_SCHEDULER_Priority
286 check_priority (enum GNUNET_SCHEDULER_Priority p)
288 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
291 return 0; /* make compiler happy */
296 * Is a task with this identifier still pending? Also updates
297 * "lowest_pending_id" as a side-effect (for faster checks in the
298 * future), but only if the return value is "GNUNET_NO" (and
299 * the "lowest_pending_id" check failed).
301 * @param id which task are we checking for
302 * @return GNUNET_YES if so, GNUNET_NO if not
305 is_pending (GNUNET_SCHEDULER_TaskIdentifier id)
308 enum GNUNET_SCHEDULER_Priority p;
309 GNUNET_SCHEDULER_TaskIdentifier min;
311 if (id < lowest_pending_id)
313 min = -1; /* maximum value */
323 pos = pending_timeout;
332 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
344 lowest_pending_id = min;
350 * Update all sets and timeout for select.
352 * @param rs read-set, set to all FDs we would like to read (updated)
353 * @param ws write-set, set to all FDs we would like to write (updated)
354 * @param timeout next timeout (updated)
357 update_sets (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws,
358 struct GNUNET_TIME_Relative *timeout)
361 struct GNUNET_TIME_Absolute now;
362 struct GNUNET_TIME_Relative to;
364 now = GNUNET_TIME_absolute_get ();
365 pos = pending_timeout;
368 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
369 if (timeout->rel_value > to.rel_value)
371 if (pos->reason != 0)
372 *timeout = GNUNET_TIME_UNIT_ZERO;
377 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
378 (GNUNET_YES == is_pending (pos->prereq_id)))
383 if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
385 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
386 if (timeout->rel_value > to.rel_value)
389 if (pos->read_fd != -1)
390 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
391 if (pos->write_fd != -1)
392 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
393 if (pos->read_set != NULL)
394 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
395 if (pos->write_set != NULL)
396 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
397 if (pos->reason != 0)
398 *timeout = GNUNET_TIME_UNIT_ZERO;
405 * Check if the ready set overlaps with the set we want to have ready.
406 * If so, update the want set (set all FDs that are ready). If not,
409 * @param ready set that is ready
410 * @param want set that we want to be ready
411 * @return GNUNET_YES if there was some overlap
414 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
415 struct GNUNET_NETWORK_FDSet *want)
417 if ((NULL == want) || (NULL == ready))
419 if (GNUNET_NETWORK_fdset_overlap (ready, want))
421 /* copy all over (yes, there maybe unrelated bits,
422 * but this should not hurt well-written clients) */
423 GNUNET_NETWORK_fdset_copy (want, ready);
431 * Check if the given task is eligible to run now.
432 * Also set the reason why it is eligible.
434 * @param task task to check if it is ready
435 * @param now the current time
436 * @param rs set of FDs ready for reading
437 * @param ws set of FDs ready for writing
438 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
441 is_ready (struct Task *task, struct GNUNET_TIME_Absolute now,
442 const struct GNUNET_NETWORK_FDSet *rs,
443 const struct GNUNET_NETWORK_FDSet *ws)
445 enum GNUNET_SCHEDULER_Reason reason;
447 reason = task->reason;
448 if (now.abs_value >= task->timeout.abs_value)
449 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
450 if ((0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
451 (((task->read_fd != -1) &&
452 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd))) ||
453 (set_overlaps (rs, task->read_set))))
454 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
455 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
456 (((task->write_fd != -1) &&
457 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)))
458 || (set_overlaps (ws, task->write_set))))
459 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
461 return GNUNET_NO; /* not ready */
462 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
464 if (GNUNET_YES == is_pending (task->prereq_id))
466 task->reason = reason;
467 return GNUNET_NO; /* prereq waiting */
469 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
471 task->reason = reason;
477 * Put a task that is ready for execution into the ready queue.
479 * @param task task ready for execution
482 queue_ready_task (struct Task *task)
484 enum GNUNET_SCHEDULER_Priority p = task->priority;
486 if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
487 p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
488 task->next = ready[check_priority (p)];
489 ready[check_priority (p)] = task;
495 * Check which tasks are ready and move them
496 * to the respective ready queue.
498 * @param rs FDs ready for reading
499 * @param ws FDs ready for writing
502 check_ready (const struct GNUNET_NETWORK_FDSet *rs,
503 const struct GNUNET_NETWORK_FDSet *ws)
508 struct GNUNET_TIME_Absolute now;
510 now = GNUNET_TIME_absolute_get ();
512 pos = pending_timeout;
516 if (now.abs_value >= pos->timeout.abs_value)
517 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
518 if (0 == pos->reason)
520 pending_timeout = next;
521 if (pending_timeout_last == pos)
522 pending_timeout_last = NULL;
523 queue_ready_task (pos);
529 LOG (GNUNET_ERROR_TYPE_DEBUG,
530 "Checking readiness of task: %llu / %p\n",
531 pos->id, pos->callback_cls);
533 if (GNUNET_YES == is_ready (pos, now, rs, ws))
539 queue_ready_task (pos);
550 * Request the shutdown of a scheduler. Marks all currently
551 * pending tasks as ready because of shutdown. This will
552 * cause all tasks to run (as soon as possible, respecting
553 * priorities and prerequisite tasks). Note that tasks
554 * scheduled AFTER this call may still be delayed arbitrarily.
557 GNUNET_SCHEDULER_shutdown ()
562 pos = pending_timeout;
565 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
566 /* we don't move the task into the ready queue yet; check_ready
567 * will do that later, possibly adding additional
568 * readiness-factors */
574 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
575 /* we don't move the task into the ready queue yet; check_ready
576 * will do that later, possibly adding additional
577 * readiness-factors */
580 for (i = 0; i < GNUNET_SCHEDULER_PRIORITY_COUNT; i++)
585 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
586 /* we don't move the task into the ready queue yet; check_ready
587 * will do that later, possibly adding additional
588 * readiness-factors */
596 * Destroy a task (release associated resources)
598 * @param t task to destroy
601 destroy_task (struct Task *t)
603 if (NULL != t->read_set)
604 GNUNET_NETWORK_fdset_destroy (t->read_set);
605 if (NULL != t->write_set)
606 GNUNET_NETWORK_fdset_destroy (t->write_set);
608 GNUNET_free (t->backtrace_strings);
615 * Run at least one task in the highest-priority queue that is not
616 * empty. Keep running tasks until we are either no longer running
617 * "URGENT" tasks or until we have at least one "pending" task (which
618 * may become ready, hence we should select on it). Naturally, if
619 * there are no more ready tasks, we also return.
621 * @param rs FDs ready for reading
622 * @param ws FDs ready for writing
625 run_ready (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws)
627 enum GNUNET_SCHEDULER_Priority p;
629 struct GNUNET_SCHEDULER_TaskContext tc;
631 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
634 if (ready_count == 0)
636 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
637 /* yes, p>0 is correct, 0 is "KEEP" which should
638 * always be an empty queue (see assertion)! */
639 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
645 GNUNET_assert (pos != NULL); /* ready_count wrong? */
646 ready[p] = pos->next;
648 if (current_priority != pos->priority)
650 current_priority = pos->priority;
651 (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
654 current_lifeness = pos->lifeness;
657 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
658 DELAY_THRESHOLD.rel_value)
660 LOG (GNUNET_ERROR_TYPE_ERROR, "Task %llu took %llums to be scheduled\n",
663 GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value);
666 tc.reason = pos->reason;
667 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
668 if ((pos->read_fd != -1) &&
669 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)))
670 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
671 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
672 if ((pos->write_fd != -1) &&
673 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
674 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
675 if (((tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
676 (pos->write_fd != -1) &&
677 (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd)))
678 GNUNET_abort (); // added to ready in previous select loop!
679 LOG (GNUNET_ERROR_TYPE_DEBUG,
680 "Running task: %llu / %p\n", pos->id,
682 pos->callback (pos->callback_cls, &tc);
686 for (i = 0; i < pos->num_backtrace_strings; i++)
687 LOG (GNUNET_ERROR_TYPE_ERROR, "Task %llu trace %d: %s\n", pos->id, i,
688 pos->backtrace_strings[i]);
694 while ((pending == NULL) || (p >= max_priority_added));
698 * Pipe used to communicate shutdown via signal.
700 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
703 * Process ID of this process at the time we installed the various
709 * Signal handler called for SIGPIPE.
719 * Signal handler called for signals that should cause us to shutdown.
722 sighandler_shutdown ()
725 int old_errno = errno; /* backup errno */
727 if (getpid () != my_pid)
728 exit (1); /* we have fork'ed since the signal handler was created,
729 * ignore the signal, see https://gnunet.org/vfork discussion */
730 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
731 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
738 * Check if the system is still life. Trigger shutdown if we
739 * have tasks, but none of them give us lifeness.
741 * @return GNUNET_OK to continue the main loop,
751 for (t = pending; NULL != t; t = t->next)
752 if (t->lifeness == GNUNET_YES)
754 for (t = pending_timeout; NULL != t; t = t->next)
755 if (t->lifeness == GNUNET_YES)
757 if ((NULL != pending) || (NULL != pending_timeout))
759 GNUNET_SCHEDULER_shutdown ();
767 * Initialize and run scheduler. This function will return when all
768 * tasks have completed. On systems with signals, receiving a SIGTERM
769 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
770 * to be run after the active task is complete. As a result, SIGTERM
771 * causes all active tasks to be scheduled with reason
772 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
773 * afterwards will execute normally!). Note that any particular signal
774 * will only shut down one scheduler; applications should always only
775 * create a single scheduler.
777 * @param task task to run immediately
778 * @param task_cls closure of task
781 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
783 struct GNUNET_NETWORK_FDSet *rs;
784 struct GNUNET_NETWORK_FDSet *ws;
785 struct GNUNET_TIME_Relative timeout;
787 struct GNUNET_SIGNAL_Context *shc_int;
788 struct GNUNET_SIGNAL_Context *shc_term;
791 struct GNUNET_SIGNAL_Context *shc_quit;
792 struct GNUNET_SIGNAL_Context *shc_hup;
793 struct GNUNET_SIGNAL_Context *shc_pipe;
795 unsigned long long last_tr;
796 unsigned int busy_wait_warning;
797 const struct GNUNET_DISK_FileHandle *pr;
800 GNUNET_assert (active_task == NULL);
801 rs = GNUNET_NETWORK_fdset_create ();
802 ws = GNUNET_NETWORK_fdset_create ();
803 GNUNET_assert (shutdown_pipe_handle == NULL);
804 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
805 GNUNET_assert (shutdown_pipe_handle != NULL);
806 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
807 GNUNET_DISK_PIPE_END_READ);
808 GNUNET_assert (pr != NULL);
810 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
811 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
813 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
814 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
815 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
817 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
818 current_lifeness = GNUNET_YES;
819 GNUNET_SCHEDULER_add_continuation (task, task_cls,
820 GNUNET_SCHEDULER_REASON_STARTUP);
821 active_task = (void *) (long) -1; /* force passing of sanity check */
822 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
823 &GNUNET_OS_install_parent_control_handler,
827 busy_wait_warning = 0;
828 while (GNUNET_OK == check_lifeness ())
830 GNUNET_NETWORK_fdset_zero (rs);
831 GNUNET_NETWORK_fdset_zero (ws);
832 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
833 update_sets (rs, ws, &timeout);
834 GNUNET_NETWORK_fdset_handle_set (rs, pr);
837 /* no blocking, more work already ready! */
838 timeout = GNUNET_TIME_UNIT_ZERO;
840 if (NULL == scheduler_select)
841 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
843 ret = scheduler_select (scheduler_select_cls, rs, ws, NULL, timeout);
844 if (ret == GNUNET_SYSERR)
849 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
854 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
857 if (0 != system (lsof))
858 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "system");
864 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
866 LOG (GNUNET_ERROR_TYPE_WARNING, _("Looks like we're busy waiting...\n"));
867 sleep (1); /* mitigate */
869 check_ready (rs, ws);
871 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
873 /* consume the signal */
874 GNUNET_DISK_file_read (pr, &c, sizeof (c));
875 /* mark all active tasks as ready due to shutdown */
876 GNUNET_SCHEDULER_shutdown ();
878 if (last_tr == tasks_run)
885 busy_wait_warning = 0;
888 GNUNET_SIGNAL_handler_uninstall (shc_int);
889 GNUNET_SIGNAL_handler_uninstall (shc_term);
891 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
892 GNUNET_SIGNAL_handler_uninstall (shc_quit);
893 GNUNET_SIGNAL_handler_uninstall (shc_hup);
895 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
896 shutdown_pipe_handle = NULL;
897 GNUNET_NETWORK_fdset_destroy (rs);
898 GNUNET_NETWORK_fdset_destroy (ws);
903 * Obtain the reason code for why the current task was
904 * started. Will return the same value as
905 * the GNUNET_SCHEDULER_TaskContext's reason field.
907 * @return reason(s) why the current task is run
909 enum GNUNET_SCHEDULER_Reason
910 GNUNET_SCHEDULER_get_reason ()
912 GNUNET_assert (active_task != NULL);
913 return active_task->reason;
918 * Get information about the current load of this scheduler. Use this
919 * function to determine if an elective task should be added or simply
920 * dropped (if the decision should be made based on the number of
921 * tasks ready to run).
923 * @param p priority level to look at
924 * @return number of tasks pending right now
927 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
932 GNUNET_assert (active_task != NULL);
933 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
935 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
936 p = current_priority;
938 pos = ready[check_priority (p)];
949 * Cancel the task with the specified identifier.
950 * The task must not yet have run.
952 * @param task id of the task to cancel
953 * @return original closure of the task
956 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
960 enum GNUNET_SCHEDULER_Priority p;
964 GNUNET_assert (active_task != NULL);
987 if (pending_timeout_last == t)
988 pending_timeout_last = NULL;
994 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT)
996 LOG (GNUNET_ERROR_TYPE_ERROR, _("Attempt to cancel dead task %llu!\n"),
997 (unsigned long long) task);
1023 pending_timeout = t->next;
1033 prev->next = t->next;
1035 ret = t->callback_cls;
1036 LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling task: %llu / %p\n", task,
1044 * Continue the current execution with the given function. This is
1045 * similar to the other "add" functions except that there is no delay
1046 * and the reason code can be specified.
1048 * @param task main function of the task
1049 * @param task_cls closure for 'main'
1050 * @param reason reason for task invocation
1051 * @param priority priority to use for the task
1054 GNUNET_SCHEDULER_add_continuation_with_priority (GNUNET_SCHEDULER_Task task, void *task_cls,
1055 enum GNUNET_SCHEDULER_Reason reason,
1056 enum GNUNET_SCHEDULER_Priority priority)
1061 void *backtrace_array[50];
1064 GNUNET_assert (NULL != task);
1065 GNUNET_assert ((active_task != NULL) ||
1066 (reason == GNUNET_SCHEDULER_REASON_STARTUP));
1067 t = GNUNET_malloc (sizeof (struct Task));
1069 t->num_backtrace_strings = backtrace (backtrace_array, 50);
1070 t->backtrace_strings =
1071 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1076 t->callback_cls = task_cls;
1079 t->start_time = GNUNET_TIME_absolute_get ();
1082 t->priority = priority;
1083 t->lifeness = current_lifeness;
1084 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding continuation task: %llu / %p\n", t->id,
1086 queue_ready_task (t);
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 'main'
1097 * @param reason reason for task invocation
1100 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls,
1101 enum GNUNET_SCHEDULER_Reason reason)
1103 GNUNET_SCHEDULER_add_continuation_with_priority (task, task_cls,
1105 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
1110 * Schedule a new task to be run after the specified prerequisite task
1111 * has completed. It will be run with the DEFAULT priority.
1113 * @param prerequisite_task run this task after the task with the given
1114 * task identifier completes (and any of our other
1115 * conditions, such as delay, read or write-readiness
1116 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1117 * on completion of other tasks (this will cause the task to run as
1118 * soon as possible).
1119 * @param task main function of the task
1120 * @param task_cls closure of task
1121 * @return unique task identifier for the job
1122 * only valid until "task" is started!
1124 GNUNET_SCHEDULER_TaskIdentifier
1125 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1126 GNUNET_SCHEDULER_Task task, void *task_cls)
1128 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1129 prerequisite_task, GNUNET_TIME_UNIT_ZERO,
1130 NULL, NULL, task, task_cls);
1135 * Schedule a new task to be run with a specified priority.
1137 * @param prio how important is the new task?
1138 * @param task main function of the task
1139 * @param task_cls closure of task
1140 * @return unique task identifier for the job
1141 * only valid until "task" is started!
1143 GNUNET_SCHEDULER_TaskIdentifier
1144 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1145 GNUNET_SCHEDULER_Task task, void *task_cls)
1147 return GNUNET_SCHEDULER_add_select (prio, GNUNET_SCHEDULER_NO_TASK,
1148 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1155 * Schedule a new task to be run with a specified delay. The task
1156 * will be scheduled for execution once the delay has expired.
1158 * @param delay when should this operation time out? Use
1159 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1160 * @param priority priority to use for the task
1161 * @param task main function of the task
1162 * @param task_cls closure of task
1163 * @return unique task identifier for the job
1164 * only valid until "task" is started!
1166 GNUNET_SCHEDULER_TaskIdentifier
1167 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1168 enum GNUNET_SCHEDULER_Priority priority,
1169 GNUNET_SCHEDULER_Task task, void *task_cls)
1176 void *backtrace_array[MAX_TRACE_DEPTH];
1179 GNUNET_assert (active_task != NULL);
1180 GNUNET_assert (NULL != task);
1181 t = GNUNET_malloc (sizeof (struct Task));
1183 t->callback_cls = task_cls;
1185 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1186 t->backtrace_strings =
1187 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1193 t->start_time = GNUNET_TIME_absolute_get ();
1195 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1196 t->priority = priority;
1197 t->lifeness = current_lifeness;
1198 /* try tail first (optimization in case we are
1199 * appending to a long list of tasks with timeouts) */
1200 prev = pending_timeout_last;
1203 if (prev->timeout.abs_value > t->timeout.abs_value)
1206 pos = prev->next; /* heuristic success! */
1210 /* heuristic failed, do traversal of timeout list */
1211 pos = pending_timeout;
1213 while ((pos != NULL) &&
1214 ((pos->timeout.abs_value <= t->timeout.abs_value) ||
1215 (pos->reason != 0)))
1221 pending_timeout = t;
1225 /* hyper-optimization... */
1226 pending_timeout_last = t;
1228 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1233 for (i = 0; i < t->num_backtrace_strings; i++)
1234 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1235 t->backtrace_strings[i]);
1242 * Schedule a new task to be run with a specified delay. The task
1243 * will be scheduled for execution once the delay has expired. It
1244 * will be run with the DEFAULT priority.
1246 * @param delay when should this operation time out? Use
1247 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1248 * @param task main function of the task
1249 * @param task_cls closure of task
1250 * @return unique task identifier for the job
1251 * only valid until "task" is started!
1253 GNUNET_SCHEDULER_TaskIdentifier
1254 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1255 GNUNET_SCHEDULER_Task task, void *task_cls)
1257 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1258 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1264 * Schedule a new task to be run as soon as possible. The task
1265 * will be run with the DEFAULT priority.
1267 * @param task main function of the task
1268 * @param task_cls closure of task
1269 * @return unique task identifier for the job
1270 * only valid until "task" is started!
1272 GNUNET_SCHEDULER_TaskIdentifier
1273 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task, void *task_cls)
1275 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO, task, task_cls);
1280 * Schedule a new task to be run as soon as possible with the
1281 * (transitive) ignore-shutdown flag either explicitly set or
1282 * explicitly enabled. This task (and all tasks created from it,
1283 * other than by another call to this function) will either count or
1284 * not count for the 'lifeness' of the process. This API is only
1285 * useful in a few special cases.
1287 * @param lifeness GNUNET_YES if the task counts for lifeness, GNUNET_NO if not.
1288 * @param task main function of the task
1289 * @param task_cls closure of task
1290 * @return unique task identifier for the job
1291 * only valid until "task" is started!
1293 GNUNET_SCHEDULER_TaskIdentifier
1294 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1295 GNUNET_SCHEDULER_Task task,
1298 GNUNET_SCHEDULER_TaskIdentifier ret;
1301 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1302 GNUNET_SCHEDULER_NO_TASK,
1303 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1305 GNUNET_assert (pending->id == ret);
1306 pending->lifeness = lifeness;
1312 * Schedule a new task to be run with a specified delay or when any of
1313 * the specified file descriptor sets is ready. The delay can be used
1314 * as a timeout on the socket(s) being ready. The task will be
1315 * scheduled for execution once either the delay has expired or any of
1316 * the socket operations is ready. This is the most general
1317 * function of the "add" family. Note that the "prerequisite_task"
1318 * must be satisfied in addition to any of the other conditions. In
1319 * other words, the task will be started when
1321 * (prerequisite-run)
1325 * || shutdown-active )
1328 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1329 * which means that the task will only be run after we receive SIGTERM
1330 * @param priority priority to use
1331 * @param rfd file descriptor we want to read (can be -1)
1332 * @param wfd file descriptors we want to write (can be -1)
1333 * @param task main function of the task
1334 * @param task_cls closure of task
1335 * @return unique task identifier for the job
1336 * only valid until "task" is started!
1339 static GNUNET_SCHEDULER_TaskIdentifier
1340 add_without_sets (struct GNUNET_TIME_Relative delay,
1341 enum GNUNET_SCHEDULER_Priority priority,
1343 GNUNET_SCHEDULER_Task task, void *task_cls)
1348 void *backtrace_array[MAX_TRACE_DEPTH];
1351 GNUNET_assert (active_task != NULL);
1352 GNUNET_assert (NULL != task);
1353 t = GNUNET_malloc (sizeof (struct Task));
1355 t->callback_cls = task_cls;
1357 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1358 t->backtrace_strings =
1359 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1364 int flags = fcntl (rfd, F_GETFD);
1366 if ((flags == -1) && (errno == EBADF))
1368 LOG (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", rfd);
1372 for (i = 0; i < t->num_backtrace_strings; i++)
1373 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n", t->backtrace_strings[i]);
1380 int flags = fcntl (wfd, F_GETFD);
1382 if (flags == -1 && errno == EBADF)
1384 LOG (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", wfd);
1388 for (i = 0; i < t->num_backtrace_strings; i++)
1389 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n", t->backtrace_strings[i]);
1396 GNUNET_assert (wfd >= -1);
1400 t->start_time = GNUNET_TIME_absolute_get ();
1402 t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
1403 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1404 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1405 t->lifeness = current_lifeness;
1408 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1409 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1414 for (i = 0; i < t->num_backtrace_strings; i++)
1415 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1416 t->backtrace_strings[i]);
1425 * Schedule a new task to be run with a specified delay or when the
1426 * specified file descriptor is ready for reading. The delay can be
1427 * used as a timeout on the socket being ready. The task will be
1428 * scheduled for execution once either the delay has expired or the
1429 * socket operation is ready. It will be run with the DEFAULT priority.
1431 * @param delay when should this operation time out? Use
1432 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1433 * @param rfd read file-descriptor
1434 * @param task main function of the task
1435 * @param task_cls closure of task
1436 * @return unique task identifier for the job
1437 * only valid until "task" is started!
1439 GNUNET_SCHEDULER_TaskIdentifier
1440 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1441 struct GNUNET_NETWORK_Handle *rfd,
1442 GNUNET_SCHEDULER_Task task, void *task_cls)
1445 struct GNUNET_NETWORK_FDSet *rs;
1446 GNUNET_SCHEDULER_TaskIdentifier ret;
1448 GNUNET_assert (rfd != NULL);
1449 rs = GNUNET_NETWORK_fdset_create ();
1450 GNUNET_NETWORK_fdset_set (rs, rfd);
1452 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1453 GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL,
1455 GNUNET_NETWORK_fdset_destroy (rs);
1458 return add_without_sets (delay,
1459 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1460 GNUNET_NETWORK_get_fd (rfd), -1, task,
1467 * Schedule a new task to be run with a specified delay or when the
1468 * specified file descriptor is ready for writing. The delay can be
1469 * used as a timeout on the socket being ready. The task will be
1470 * scheduled for execution once either the delay has expired or the
1471 * socket operation is ready. It will be run with the priority of
1474 * @param delay when should this operation time out? Use
1475 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1476 * @param wfd write file-descriptor
1477 * @param task main function of the task
1478 * @param task_cls closure of task
1479 * @return unique task identifier for the job
1480 * only valid until "task" is started!
1482 GNUNET_SCHEDULER_TaskIdentifier
1483 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1484 struct GNUNET_NETWORK_Handle *wfd,
1485 GNUNET_SCHEDULER_Task task, void *task_cls)
1488 struct GNUNET_NETWORK_FDSet *ws;
1489 GNUNET_SCHEDULER_TaskIdentifier ret;
1491 GNUNET_assert (wfd != NULL);
1492 ws = GNUNET_NETWORK_fdset_create ();
1493 GNUNET_NETWORK_fdset_set (ws, wfd);
1495 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1496 GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws,
1498 GNUNET_NETWORK_fdset_destroy (ws);
1501 GNUNET_assert (GNUNET_NETWORK_get_fd (wfd) >= 0);
1502 return add_without_sets (delay,
1503 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1504 -1, GNUNET_NETWORK_get_fd (wfd), task,
1511 * Schedule a new task to be run with a specified delay or when the
1512 * specified file descriptor is ready for reading. The delay can be
1513 * used as a timeout on the socket being ready. The task will be
1514 * scheduled for execution once either the delay has expired or the
1515 * socket operation is ready. It will be run with the DEFAULT priority.
1517 * @param delay when should this operation time out? Use
1518 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1519 * @param rfd read file-descriptor
1520 * @param task main function of the task
1521 * @param task_cls closure of task
1522 * @return unique task identifier for the job
1523 * only valid until "task" is started!
1525 GNUNET_SCHEDULER_TaskIdentifier
1526 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1527 const struct GNUNET_DISK_FileHandle *rfd,
1528 GNUNET_SCHEDULER_Task task, void *task_cls)
1531 struct GNUNET_NETWORK_FDSet *rs;
1532 GNUNET_SCHEDULER_TaskIdentifier ret;
1534 GNUNET_assert (rfd != NULL);
1535 rs = GNUNET_NETWORK_fdset_create ();
1536 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1538 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1539 GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL,
1541 GNUNET_NETWORK_fdset_destroy (rs);
1546 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1547 return add_without_sets (delay,
1548 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1549 fd, -1, task, task_cls);
1556 * Schedule a new task to be run with a specified delay or when the
1557 * specified file descriptor is ready for writing. The delay can be
1558 * used as a timeout on the socket being ready. The task will be
1559 * scheduled for execution once either the delay has expired or the
1560 * socket operation is ready. It will be run with the DEFAULT priority.
1562 * @param delay when should this operation time out? Use
1563 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1564 * @param wfd write file-descriptor
1565 * @param task main function of the task
1566 * @param task_cls closure of task
1567 * @return unique task identifier for the job
1568 * only valid until "task" is started!
1570 GNUNET_SCHEDULER_TaskIdentifier
1571 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1572 const struct GNUNET_DISK_FileHandle *wfd,
1573 GNUNET_SCHEDULER_Task task, void *task_cls)
1576 struct GNUNET_NETWORK_FDSet *ws;
1577 GNUNET_SCHEDULER_TaskIdentifier ret;
1579 GNUNET_assert (wfd != NULL);
1580 ws = GNUNET_NETWORK_fdset_create ();
1581 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1583 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1584 GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws,
1586 GNUNET_NETWORK_fdset_destroy (ws);
1591 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1592 GNUNET_assert (fd >= 0);
1593 return add_without_sets (delay,
1594 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1595 -1, fd, task, task_cls);
1603 * Schedule a new task to be run with a specified delay or when any of
1604 * the specified file descriptor sets is ready. The delay can be used
1605 * as a timeout on the socket(s) being ready. The task will be
1606 * scheduled for execution once either the delay has expired or any of
1607 * the socket operations is ready. This is the most general
1608 * function of the "add" family. Note that the "prerequisite_task"
1609 * must be satisfied in addition to any of the other conditions. In
1610 * other words, the task will be started when
1612 * (prerequisite-run)
1616 * || (shutdown-active && run-on-shutdown) )
1619 * @param prio how important is this task?
1620 * @param prerequisite_task run this task after the task with the given
1621 * task identifier completes (and any of our other
1622 * conditions, such as delay, read or write-readiness
1623 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1624 * on completion of other tasks.
1625 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1626 * which means that the task will only be run after we receive SIGTERM
1627 * @param rs set of file descriptors we want to read (can be NULL)
1628 * @param ws set of file descriptors we want to write (can be NULL)
1629 * @param task main function of the task
1630 * @param task_cls closure of task
1631 * @return unique task identifier for the job
1632 * only valid until "task" is started!
1634 GNUNET_SCHEDULER_TaskIdentifier
1635 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1636 GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1637 struct GNUNET_TIME_Relative delay,
1638 const struct GNUNET_NETWORK_FDSet *rs,
1639 const struct GNUNET_NETWORK_FDSet *ws,
1640 GNUNET_SCHEDULER_Task task, void *task_cls)
1645 void *backtrace_array[MAX_TRACE_DEPTH];
1648 GNUNET_assert (active_task != NULL);
1649 GNUNET_assert (NULL != task);
1650 t = GNUNET_malloc (sizeof (struct Task));
1652 t->callback_cls = task_cls;
1654 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1655 t->backtrace_strings =
1656 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1662 t->read_set = GNUNET_NETWORK_fdset_create ();
1663 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1667 t->write_set = GNUNET_NETWORK_fdset_create ();
1668 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1672 t->start_time = GNUNET_TIME_absolute_get ();
1674 t->prereq_id = prerequisite_task;
1675 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1677 check_priority ((prio ==
1678 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1680 t->lifeness = current_lifeness;
1683 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1684 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1689 for (i = 0; i < t->num_backtrace_strings; i++)
1690 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1691 t->backtrace_strings[i]);
1696 /* end of scheduler.c */