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"
33 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
35 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
42 * Use lsof to generate file descriptor reports on select error?
43 * (turn off for stable releases).
45 #define USE_LSOF GNUNET_NO
48 * Obtain trace information for all scheduler calls that schedule tasks.
50 #define EXECINFO GNUNET_NO
53 * Check each file descriptor before adding
55 #define DEBUG_FDS GNUNET_EXTRA_LOGGING
58 * Depth of the traces collected via EXECINFO.
60 #define MAX_TRACE_DEPTH 50
63 #define DEBUG_TASKS GNUNET_EXTRA_LOGGING
66 * Should we figure out which tasks are delayed for a while
67 * before they are run? (Consider using in combination with EXECINFO).
69 #define PROFILE_DELAYS GNUNET_NO
72 * Task that were in the queue for longer than this are reported if
73 * PROFILE_DELAYS is active.
75 #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);
530 LOG (GNUNET_ERROR_TYPE_DEBUG, "Checking readiness of task: %llu / %p\n",
531 pos->id, pos->callback_cls);
534 if (GNUNET_YES == is_ready (pos, now, rs, ws))
540 queue_ready_task (pos);
551 * Request the shutdown of a scheduler. Marks all currently
552 * pending tasks as ready because of shutdown. This will
553 * cause all tasks to run (as soon as possible, respecting
554 * priorities and prerequisite tasks). Note that tasks
555 * scheduled AFTER this call may still be delayed arbitrarily.
558 GNUNET_SCHEDULER_shutdown ()
563 pos = pending_timeout;
566 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
567 /* we don't move the task into the ready queue yet; check_ready
568 * will do that later, possibly adding additional
569 * readiness-factors */
575 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
576 /* we don't move the task into the ready queue yet; check_ready
577 * will do that later, possibly adding additional
578 * readiness-factors */
581 for (i = 0; i < GNUNET_SCHEDULER_PRIORITY_COUNT; i++)
586 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
587 /* we don't move the task into the ready queue yet; check_ready
588 * will do that later, possibly adding additional
589 * readiness-factors */
597 * Destroy a task (release associated resources)
599 * @param t task to destroy
602 destroy_task (struct Task *t)
604 if (NULL != t->read_set)
605 GNUNET_NETWORK_fdset_destroy (t->read_set);
606 if (NULL != t->write_set)
607 GNUNET_NETWORK_fdset_destroy (t->write_set);
609 GNUNET_free (t->backtrace_strings);
616 * Run at least one task in the highest-priority queue that is not
617 * empty. Keep running tasks until we are either no longer running
618 * "URGENT" tasks or until we have at least one "pending" task (which
619 * may become ready, hence we should select on it). Naturally, if
620 * there are no more ready tasks, we also return.
622 * @param rs FDs ready for reading
623 * @param ws FDs ready for writing
626 run_ready (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws)
628 enum GNUNET_SCHEDULER_Priority p;
630 struct GNUNET_SCHEDULER_TaskContext tc;
632 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
635 if (ready_count == 0)
637 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
638 /* yes, p>0 is correct, 0 is "KEEP" which should
639 * always be an empty queue (see assertion)! */
640 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
646 GNUNET_assert (pos != NULL); /* ready_count wrong? */
647 ready[p] = pos->next;
649 if (current_priority != pos->priority)
651 current_priority = pos->priority;
652 (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
655 current_lifeness = pos->lifeness;
658 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
659 DELAY_THRESHOLD.rel_value)
661 LOG (GNUNET_ERROR_TYPE_ERROR, "Task %llu took %llums to be scheduled\n",
664 GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value);
667 tc.reason = pos->reason;
668 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
669 if ((pos->read_fd != -1) &&
670 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)))
671 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
672 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
673 if ((pos->write_fd != -1) &&
674 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
675 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
676 if (((tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
677 (pos->write_fd != -1) &&
678 (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd)))
679 GNUNET_abort (); // added to ready in previous select loop!
681 LOG (GNUNET_ERROR_TYPE_DEBUG, "Running task: %llu / %p\n", pos->id,
684 pos->callback (pos->callback_cls, &tc);
688 for (i = 0; i < pos->num_backtrace_strings; i++)
689 LOG (GNUNET_ERROR_TYPE_ERROR, "Task %llu trace %d: %s\n", pos->id, i,
690 pos->backtrace_strings[i]);
696 while ((pending == NULL) || (p >= max_priority_added));
700 * Pipe used to communicate shutdown via signal.
702 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
705 * Process ID of this process at the time we installed the various
711 * Signal handler called for SIGPIPE.
721 * Signal handler called for signals that should cause us to shutdown.
724 sighandler_shutdown ()
727 int old_errno = errno; /* backup errno */
729 if (getpid () != my_pid)
730 exit (1); /* we have fork'ed since the signal handler was created,
731 * ignore the signal, see https://gnunet.org/vfork discussion */
732 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
733 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
740 * Check if the system is still life. Trigger shutdown if we
741 * have tasks, but none of them give us lifeness.
743 * @return GNUNET_OK to continue the main loop,
753 for (t = pending; NULL != t; t = t->next)
754 if (t->lifeness == GNUNET_YES)
756 for (t = pending_timeout; NULL != t; t = t->next)
757 if (t->lifeness == GNUNET_YES)
759 if ((NULL != pending) || (NULL != pending_timeout))
761 GNUNET_SCHEDULER_shutdown ();
769 * Initialize and run scheduler. This function will return when all
770 * tasks have completed. On systems with signals, receiving a SIGTERM
771 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
772 * to be run after the active task is complete. As a result, SIGTERM
773 * causes all active tasks to be scheduled with reason
774 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
775 * afterwards will execute normally!). Note that any particular signal
776 * will only shut down one scheduler; applications should always only
777 * create a single scheduler.
779 * @param task task to run immediately
780 * @param task_cls closure of task
783 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
785 struct GNUNET_NETWORK_FDSet *rs;
786 struct GNUNET_NETWORK_FDSet *ws;
787 struct GNUNET_TIME_Relative timeout;
789 struct GNUNET_SIGNAL_Context *shc_int;
790 struct GNUNET_SIGNAL_Context *shc_term;
793 struct GNUNET_SIGNAL_Context *shc_quit;
794 struct GNUNET_SIGNAL_Context *shc_hup;
795 struct GNUNET_SIGNAL_Context *shc_pipe;
797 unsigned long long last_tr;
798 unsigned int busy_wait_warning;
799 const struct GNUNET_DISK_FileHandle *pr;
802 GNUNET_assert (active_task == NULL);
803 rs = GNUNET_NETWORK_fdset_create ();
804 ws = GNUNET_NETWORK_fdset_create ();
805 GNUNET_assert (shutdown_pipe_handle == NULL);
806 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
807 GNUNET_assert (shutdown_pipe_handle != NULL);
808 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
809 GNUNET_DISK_PIPE_END_READ);
810 GNUNET_assert (pr != NULL);
812 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
813 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
815 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
816 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
817 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
819 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
820 current_lifeness = GNUNET_YES;
821 GNUNET_SCHEDULER_add_continuation (task, task_cls,
822 GNUNET_SCHEDULER_REASON_STARTUP);
823 #if ENABLE_WINDOWS_WORKAROUNDS
824 active_task = (void *) (long) -1; /* force passing of sanity check */
825 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
826 &GNUNET_OS_install_parent_control_handler,
831 busy_wait_warning = 0;
832 while (GNUNET_OK == check_lifeness ())
834 GNUNET_NETWORK_fdset_zero (rs);
835 GNUNET_NETWORK_fdset_zero (ws);
836 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
837 update_sets (rs, ws, &timeout);
838 GNUNET_NETWORK_fdset_handle_set (rs, pr);
841 /* no blocking, more work already ready! */
842 timeout = GNUNET_TIME_UNIT_ZERO;
844 if (NULL == scheduler_select)
845 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
847 ret = scheduler_select (scheduler_select_cls, rs, ws, NULL, timeout);
848 if (ret == GNUNET_SYSERR)
853 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
858 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
861 if (0 != system (lsof))
862 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "system");
868 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
870 LOG (GNUNET_ERROR_TYPE_WARNING, _("Looks like we're busy waiting...\n"));
871 sleep (1); /* mitigate */
873 check_ready (rs, ws);
875 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
877 /* consume the signal */
878 GNUNET_DISK_file_read (pr, &c, sizeof (c));
879 /* mark all active tasks as ready due to shutdown */
880 GNUNET_SCHEDULER_shutdown ();
882 if (last_tr == tasks_run)
889 busy_wait_warning = 0;
892 GNUNET_SIGNAL_handler_uninstall (shc_int);
893 GNUNET_SIGNAL_handler_uninstall (shc_term);
895 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
896 GNUNET_SIGNAL_handler_uninstall (shc_quit);
897 GNUNET_SIGNAL_handler_uninstall (shc_hup);
899 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
900 shutdown_pipe_handle = NULL;
901 GNUNET_NETWORK_fdset_destroy (rs);
902 GNUNET_NETWORK_fdset_destroy (ws);
907 * Obtain the reason code for why the current task was
908 * started. Will return the same value as
909 * the GNUNET_SCHEDULER_TaskContext's reason field.
911 * @return reason(s) why the current task is run
913 enum GNUNET_SCHEDULER_Reason
914 GNUNET_SCHEDULER_get_reason ()
916 GNUNET_assert (active_task != NULL);
917 return active_task->reason;
922 * Get information about the current load of this scheduler. Use this
923 * function to determine if an elective task should be added or simply
924 * dropped (if the decision should be made based on the number of
925 * tasks ready to run).
927 * @param p priority level to look at
928 * @return number of tasks pending right now
931 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
936 GNUNET_assert (active_task != NULL);
937 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
939 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
940 p = current_priority;
942 pos = ready[check_priority (p)];
953 * Cancel the task with the specified identifier.
954 * The task must not yet have run.
956 * @param task id of the task to cancel
957 * @return original closure of the task
960 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
964 enum GNUNET_SCHEDULER_Priority p;
968 GNUNET_assert (active_task != NULL);
991 if (pending_timeout_last == t)
992 pending_timeout_last = NULL;
998 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT)
1000 LOG (GNUNET_ERROR_TYPE_ERROR, _("Attempt to cancel dead task %llu!\n"),
1001 (unsigned long long) task);
1027 pending_timeout = t->next;
1037 prev->next = t->next;
1039 ret = t->callback_cls;
1041 LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling task: %llu / %p\n", task,
1050 * Continue the current execution with the given function. This is
1051 * similar to the other "add" functions except that there is no delay
1052 * and the reason code can be specified.
1054 * @param task main function of the task
1055 * @param task_cls closure for 'main'
1056 * @param reason reason for task invocation
1059 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls,
1060 enum GNUNET_SCHEDULER_Reason reason)
1065 void *backtrace_array[50];
1068 GNUNET_assert (NULL != task);
1069 GNUNET_assert ((active_task != NULL) ||
1070 (reason == GNUNET_SCHEDULER_REASON_STARTUP));
1071 t = GNUNET_malloc (sizeof (struct Task));
1073 t->num_backtrace_strings = backtrace (backtrace_array, 50);
1074 t->backtrace_strings =
1075 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1080 t->callback_cls = task_cls;
1083 t->start_time = GNUNET_TIME_absolute_get ();
1086 t->priority = current_priority;
1087 t->lifeness = current_lifeness;
1089 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding continuation task: %llu / %p\n", t->id,
1092 queue_ready_task (t);
1098 * Schedule a new task to be run after the specified prerequisite task
1099 * has completed. It will be run with the priority of the calling
1102 * @param prerequisite_task run this task after the task with the given
1103 * task identifier completes (and any of our other
1104 * conditions, such as delay, read or write-readiness
1105 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1106 * on completion of other tasks (this will cause the task to run as
1107 * soon as possible).
1108 * @param task main function of the task
1109 * @param task_cls closure of task
1110 * @return unique task identifier for the job
1111 * only valid until "task" is started!
1113 GNUNET_SCHEDULER_TaskIdentifier
1114 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1115 GNUNET_SCHEDULER_Task task, void *task_cls)
1117 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1118 prerequisite_task, GNUNET_TIME_UNIT_ZERO,
1119 NULL, NULL, task, task_cls);
1124 * Schedule a new task to be run with a specified priority.
1126 * @param prio how important is the new task?
1127 * @param task main function of the task
1128 * @param task_cls closure of task
1129 * @return unique task identifier for the job
1130 * only valid until "task" is started!
1132 GNUNET_SCHEDULER_TaskIdentifier
1133 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1134 GNUNET_SCHEDULER_Task task, void *task_cls)
1136 return GNUNET_SCHEDULER_add_select (prio, GNUNET_SCHEDULER_NO_TASK,
1137 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1144 * Schedule a new task to be run with a specified delay. The task
1145 * will be scheduled for execution once the delay has expired. It
1146 * will be run with the priority of the calling task.
1148 * @param delay when should this operation time out? Use
1149 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1150 * @param task main function of the task
1151 * @param task_cls closure of task
1152 * @return unique task identifier for the job
1153 * only valid until "task" is started!
1155 GNUNET_SCHEDULER_TaskIdentifier
1156 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1157 GNUNET_SCHEDULER_Task task, void *task_cls)
1160 /* new, optimized version */
1166 void *backtrace_array[MAX_TRACE_DEPTH];
1169 GNUNET_assert (active_task != NULL);
1170 GNUNET_assert (NULL != task);
1171 t = GNUNET_malloc (sizeof (struct Task));
1173 t->callback_cls = task_cls;
1175 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1176 t->backtrace_strings =
1177 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1183 t->start_time = GNUNET_TIME_absolute_get ();
1185 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1186 t->priority = current_priority;
1187 t->lifeness = current_lifeness;
1188 /* try tail first (optimization in case we are
1189 * appending to a long list of tasks with timeouts) */
1190 prev = pending_timeout_last;
1193 if (prev->timeout.abs_value > t->timeout.abs_value)
1196 pos = prev->next; /* heuristic success! */
1200 /* heuristic failed, do traversal of timeout list */
1201 pos = pending_timeout;
1203 while ((pos != NULL) &&
1204 ((pos->timeout.abs_value <= t->timeout.abs_value) ||
1205 (pos->reason != 0)))
1211 pending_timeout = t;
1215 /* hyper-optimization... */
1216 pending_timeout_last = t;
1219 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1225 for (i = 0; i < t->num_backtrace_strings; i++)
1226 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1227 t->backtrace_strings[i]);
1232 /* unoptimized version */
1233 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1234 GNUNET_SCHEDULER_NO_TASK, delay, NULL,
1235 NULL, task, task_cls);
1242 * Schedule a new task to be run as soon as possible. The task
1243 * will be run with the priority of the calling task.
1245 * @param task main function of the task
1246 * @param task_cls closure of task
1247 * @return unique task identifier for the job
1248 * only valid until "task" is started!
1250 GNUNET_SCHEDULER_TaskIdentifier
1251 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task, void *task_cls)
1253 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1254 GNUNET_SCHEDULER_NO_TASK,
1255 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1261 * Schedule a new task to be run as soon as possible with the
1262 * (transitive) ignore-shutdown flag either explicitly set or
1263 * explicitly enabled. This task (and all tasks created from it,
1264 * other than by another call to this function) will either count or
1265 * not count for the 'lifeness' of the process. This API is only
1266 * useful in a few special cases.
1268 * @param lifeness GNUNET_YES if the task counts for lifeness, GNUNET_NO if not.
1269 * @param task main function of the task
1270 * @param task_cls closure of task
1271 * @return unique task identifier for the job
1272 * only valid until "task" is started!
1274 GNUNET_SCHEDULER_TaskIdentifier
1275 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1276 GNUNET_SCHEDULER_Task task,
1279 GNUNET_SCHEDULER_TaskIdentifier ret;
1282 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1283 GNUNET_SCHEDULER_NO_TASK,
1284 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1286 GNUNET_assert (pending->id == ret);
1287 pending->lifeness = lifeness;
1295 * Schedule a new task to be run with a specified delay or when any of
1296 * the specified file descriptor sets is ready. The delay can be used
1297 * as a timeout on the socket(s) being ready. The task will be
1298 * scheduled for execution once either the delay has expired or any of
1299 * the socket operations is ready. This is the most general
1300 * function of the "add" family. Note that the "prerequisite_task"
1301 * must be satisfied in addition to any of the other conditions. In
1302 * other words, the task will be started when
1304 * (prerequisite-run)
1308 * || shutdown-active )
1311 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1312 * which means that the task will only be run after we receive SIGTERM
1313 * @param rfd file descriptor we want to read (can be -1)
1314 * @param wfd file descriptors we want to write (can be -1)
1315 * @param task main function of the task
1316 * @param task_cls closure of task
1317 * @return unique task identifier for the job
1318 * only valid until "task" is started!
1321 static GNUNET_SCHEDULER_TaskIdentifier
1322 add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd,
1323 GNUNET_SCHEDULER_Task task, void *task_cls)
1328 void *backtrace_array[MAX_TRACE_DEPTH];
1331 GNUNET_assert (active_task != NULL);
1332 GNUNET_assert (NULL != task);
1333 t = GNUNET_malloc (sizeof (struct Task));
1335 t->callback_cls = task_cls;
1337 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1338 t->backtrace_strings =
1339 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1344 int flags = fcntl (rfd, F_GETFD);
1346 if ((flags == -1) && (errno == EBADF))
1348 LOG (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", rfd);
1352 for (i = 0; i < t->num_backtrace_strings; i++)
1353 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n", t->backtrace_strings[i]);
1360 int flags = fcntl (wfd, F_GETFD);
1362 if (flags == -1 && errno == EBADF)
1364 LOG (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", wfd);
1368 for (i = 0; i < t->num_backtrace_strings; i++)
1369 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n", t->backtrace_strings[i]);
1376 GNUNET_assert (wfd >= -1);
1380 t->start_time = GNUNET_TIME_absolute_get ();
1382 t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
1383 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1384 t->priority = check_priority (current_priority);
1385 t->lifeness = current_lifeness;
1388 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1390 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1396 for (i = 0; i < t->num_backtrace_strings; i++)
1397 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1398 t->backtrace_strings[i]);
1407 * Schedule a new task to be run with a specified delay or when the
1408 * specified file descriptor is ready for reading. The delay can be
1409 * used as a timeout on the socket being ready. The task will be
1410 * scheduled for execution once either the delay has expired or the
1411 * socket operation is ready. It will be run with the priority of
1414 * @param delay when should this operation time out? Use
1415 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1416 * @param rfd read file-descriptor
1417 * @param task main function of the task
1418 * @param task_cls closure of task
1419 * @return unique task identifier for the job
1420 * only valid until "task" is started!
1422 GNUNET_SCHEDULER_TaskIdentifier
1423 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1424 struct GNUNET_NETWORK_Handle *rfd,
1425 GNUNET_SCHEDULER_Task task, void *task_cls)
1428 struct GNUNET_NETWORK_FDSet *rs;
1429 GNUNET_SCHEDULER_TaskIdentifier ret;
1431 GNUNET_assert (rfd != NULL);
1432 rs = GNUNET_NETWORK_fdset_create ();
1433 GNUNET_NETWORK_fdset_set (rs, rfd);
1435 GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1436 GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL,
1438 GNUNET_NETWORK_fdset_destroy (rs);
1441 return add_without_sets (delay, GNUNET_NETWORK_get_fd (rfd), -1, task,
1448 * Schedule a new task to be run with a specified delay or when the
1449 * specified file descriptor is ready for writing. The delay can be
1450 * used as a timeout on the socket being ready. The task will be
1451 * scheduled for execution once either the delay has expired or the
1452 * socket operation is ready. It will be run with the priority of
1455 * @param delay when should this operation time out? Use
1456 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1457 * @param wfd write file-descriptor
1458 * @param task main function of the task
1459 * @param task_cls closure of task
1460 * @return unique task identifier for the job
1461 * only valid until "task" is started!
1463 GNUNET_SCHEDULER_TaskIdentifier
1464 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1465 struct GNUNET_NETWORK_Handle *wfd,
1466 GNUNET_SCHEDULER_Task task, void *task_cls)
1469 struct GNUNET_NETWORK_FDSet *ws;
1470 GNUNET_SCHEDULER_TaskIdentifier ret;
1472 GNUNET_assert (wfd != NULL);
1473 ws = GNUNET_NETWORK_fdset_create ();
1474 GNUNET_NETWORK_fdset_set (ws, wfd);
1476 GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1477 GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws,
1479 GNUNET_NETWORK_fdset_destroy (ws);
1482 GNUNET_assert (GNUNET_NETWORK_get_fd (wfd) >= 0);
1483 return add_without_sets (delay, -1, GNUNET_NETWORK_get_fd (wfd), task,
1490 * Schedule a new task to be run with a specified delay or when the
1491 * specified file descriptor is ready for reading. The delay can be
1492 * used as a timeout on the socket being ready. The task will be
1493 * scheduled for execution once either the delay has expired or the
1494 * socket operation is ready. It will be run with the priority of
1497 * @param delay when should this operation time out? Use
1498 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1499 * @param rfd read file-descriptor
1500 * @param task main function of the task
1501 * @param task_cls closure of task
1502 * @return unique task identifier for the job
1503 * only valid until "task" is started!
1505 GNUNET_SCHEDULER_TaskIdentifier
1506 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1507 const struct GNUNET_DISK_FileHandle *rfd,
1508 GNUNET_SCHEDULER_Task task, void *task_cls)
1511 struct GNUNET_NETWORK_FDSet *rs;
1512 GNUNET_SCHEDULER_TaskIdentifier ret;
1514 GNUNET_assert (rfd != NULL);
1515 rs = GNUNET_NETWORK_fdset_create ();
1516 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1518 GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1519 GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL,
1521 GNUNET_NETWORK_fdset_destroy (rs);
1526 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1527 return add_without_sets (delay, fd, -1, task, task_cls);
1534 * Schedule a new task to be run with a specified delay or when the
1535 * specified file descriptor is ready for writing. The delay can be
1536 * used as a timeout on the socket being ready. The task will be
1537 * scheduled for execution once either the delay has expired or the
1538 * socket operation is ready. It will be run with the priority of
1541 * @param delay when should this operation time out? Use
1542 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1543 * @param wfd write file-descriptor
1544 * @param task main function of the task
1545 * @param task_cls closure of task
1546 * @return unique task identifier for the job
1547 * only valid until "task" is started!
1549 GNUNET_SCHEDULER_TaskIdentifier
1550 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1551 const struct GNUNET_DISK_FileHandle *wfd,
1552 GNUNET_SCHEDULER_Task task, void *task_cls)
1555 struct GNUNET_NETWORK_FDSet *ws;
1556 GNUNET_SCHEDULER_TaskIdentifier ret;
1558 GNUNET_assert (wfd != NULL);
1559 ws = GNUNET_NETWORK_fdset_create ();
1560 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1562 GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1563 GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws,
1565 GNUNET_NETWORK_fdset_destroy (ws);
1570 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1571 GNUNET_assert (fd >= 0);
1572 return add_without_sets (delay, -1, fd, task, task_cls);
1580 * Schedule a new task to be run with a specified delay or when any of
1581 * the specified file descriptor sets is ready. The delay can be used
1582 * as a timeout on the socket(s) being ready. The task will be
1583 * scheduled for execution once either the delay has expired or any of
1584 * the socket operations is ready. This is the most general
1585 * function of the "add" family. Note that the "prerequisite_task"
1586 * must be satisfied in addition to any of the other conditions. In
1587 * other words, the task will be started when
1589 * (prerequisite-run)
1593 * || (shutdown-active && run-on-shutdown) )
1596 * @param prio how important is this task?
1597 * @param prerequisite_task run this task after the task with the given
1598 * task identifier completes (and any of our other
1599 * conditions, such as delay, read or write-readiness
1600 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1601 * on completion of other tasks.
1602 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1603 * which means that the task will only be run after we receive SIGTERM
1604 * @param rs set of file descriptors we want to read (can be NULL)
1605 * @param ws set of file descriptors we want to write (can be NULL)
1606 * @param task main function of the task
1607 * @param task_cls closure of task
1608 * @return unique task identifier for the job
1609 * only valid until "task" is started!
1611 GNUNET_SCHEDULER_TaskIdentifier
1612 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1613 GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1614 struct GNUNET_TIME_Relative delay,
1615 const struct GNUNET_NETWORK_FDSet *rs,
1616 const struct GNUNET_NETWORK_FDSet *ws,
1617 GNUNET_SCHEDULER_Task task, void *task_cls)
1622 void *backtrace_array[MAX_TRACE_DEPTH];
1625 GNUNET_assert (active_task != NULL);
1626 GNUNET_assert (NULL != task);
1627 t = GNUNET_malloc (sizeof (struct Task));
1629 t->callback_cls = task_cls;
1631 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1632 t->backtrace_strings =
1633 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1639 t->read_set = GNUNET_NETWORK_fdset_create ();
1640 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1644 t->write_set = GNUNET_NETWORK_fdset_create ();
1645 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1649 t->start_time = GNUNET_TIME_absolute_get ();
1651 t->prereq_id = prerequisite_task;
1652 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1654 check_priority ((prio ==
1655 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1657 t->lifeness = current_lifeness;
1660 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1662 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1668 for (i = 0; i < t->num_backtrace_strings; i++)
1669 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1670 t->backtrace_strings[i]);
1675 /* end of scheduler.c */