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"
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
59 #define DEBUG_TASKS GNUNET_NO
62 * Should we figure out which tasks are delayed for a while
63 * before they are run? (Consider using in combination with EXECINFO).
65 #define PROFILE_DELAYS GNUNET_NO
68 * Task that were in the queue for longer than this are reported if
69 * PROFILE_DELAYS is active.
71 #define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
74 * Linked list of pending tasks.
79 * This is a linked list.
84 * Function to run when ready.
86 GNUNET_SCHEDULER_Task callback;
89 * Closure for the callback.
94 * Set of file descriptors this task is waiting
95 * for for reading. Once ready, this is updated
96 * to reflect the set of file descriptors ready
99 struct GNUNET_NETWORK_FDSet *read_set;
102 * Set of file descriptors this task is waiting for for writing.
103 * Once ready, this is updated to reflect the set of file
104 * descriptors ready for operation.
106 struct GNUNET_NETWORK_FDSet *write_set;
109 * Unique task identifier.
111 GNUNET_SCHEDULER_TaskIdentifier id;
114 * Identifier of a prerequisite task.
116 GNUNET_SCHEDULER_TaskIdentifier prereq_id;
119 * Absolute timeout value for the task, or
120 * GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
122 struct GNUNET_TIME_Absolute timeout;
126 * When was the task scheduled?
128 struct GNUNET_TIME_Absolute start_time;
132 * Why is the task ready? Set after task is added to ready queue.
133 * Initially set to zero. All reasons that have already been
134 * satisfied (i.e. read or write ready) will be set over time.
136 enum GNUNET_SCHEDULER_Reason reason;
141 enum GNUNET_SCHEDULER_Priority priority;
144 * Set if we only wait for reading from a single FD, otherwise -1.
149 * Set if we only wait for writing to a single FD, otherwise -1.
154 * Should the existence of this task in the queue be counted as
155 * reason to not shutdown the scheduler?
161 * Array of strings which make up a backtrace from the point when this
162 * task was scheduled (essentially, who scheduled the task?)
164 char **backtrace_strings;
167 * Size of the backtrace_strings array
169 int num_backtrace_strings;
177 * List of tasks waiting for an event.
179 static struct Task *pending;
182 * List of tasks waiting ONLY for a timeout event.
183 * Sorted by timeout (earliest first). Used so that
184 * we do not traverse the list of these tasks when
185 * building select sets (we just look at the head
186 * to determine the respective timeout ONCE).
188 static struct Task *pending_timeout;
191 * Last inserted task waiting ONLY for a timeout event.
192 * Used to (heuristically) speed up insertion.
194 static struct Task *pending_timeout_last;
197 * ID of the task that is running right now.
199 static struct Task *active_task;
202 * List of tasks ready to run right now,
203 * grouped by importance.
205 static struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
208 * Identity of the last task queued. Incremented for each task to
209 * generate a unique task ID (it is virtually impossible to start
210 * more than 2^64 tasks during the lifetime of a process).
212 static GNUNET_SCHEDULER_TaskIdentifier last_id;
215 * Highest number so that all tasks with smaller identifiers
216 * have already completed. Also the lowest number of a task
217 * still waiting to be executed.
219 static GNUNET_SCHEDULER_TaskIdentifier lowest_pending_id;
222 * Number of tasks on the ready list.
224 static unsigned int ready_count;
227 * How many tasks have we run so far?
229 static unsigned long long tasks_run;
232 * Priority of the task running right now. Only
233 * valid while a task is running.
235 static enum GNUNET_SCHEDULER_Priority current_priority;
238 * Priority of the highest task added in the current select
241 static enum GNUNET_SCHEDULER_Priority max_priority_added;
244 * Value of the 'lifeness' flag for the current task.
246 static int current_lifeness;
249 * Function to use as a select() in the scheduler.
250 * If NULL, we use GNUNET_NETWORK_socket_select ().
252 static GNUNET_SCHEDULER_select scheduler_select;
255 * Closure for 'scheduler_select'.
257 static void *scheduler_select_cls;
260 * Sets the select function to use in the scheduler (scheduler_select).
262 * @param new_select new select function to use
263 * @return previously used select function, NULL for default
266 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
267 void *new_select_cls)
269 scheduler_select = new_select;
270 scheduler_select_cls = new_select_cls;
275 * Check that the given priority is legal (and return it).
277 * @param p priority value to check
278 * @return p on success, 0 on error
280 static enum GNUNET_SCHEDULER_Priority
281 check_priority (enum GNUNET_SCHEDULER_Priority p)
283 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
286 return 0; /* make compiler happy */
291 * Is a task with this identifier still pending? Also updates
292 * "lowest_pending_id" as a side-effect (for faster checks in the
293 * future), but only if the return value is "GNUNET_NO" (and
294 * the "lowest_pending_id" check failed).
296 * @param id which task are we checking for
297 * @return GNUNET_YES if so, GNUNET_NO if not
300 is_pending (GNUNET_SCHEDULER_TaskIdentifier id)
303 enum GNUNET_SCHEDULER_Priority p;
304 GNUNET_SCHEDULER_TaskIdentifier min;
306 if (id < lowest_pending_id)
308 min = -1; /* maximum value */
318 pos = pending_timeout;
327 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
339 lowest_pending_id = min;
345 * Update all sets and timeout for select.
347 * @param rs read-set, set to all FDs we would like to read (updated)
348 * @param ws write-set, set to all FDs we would like to write (updated)
349 * @param timeout next timeout (updated)
352 update_sets (struct GNUNET_NETWORK_FDSet *rs,
353 struct GNUNET_NETWORK_FDSet *ws,
354 struct GNUNET_TIME_Relative *timeout)
357 struct GNUNET_TIME_Absolute now;
358 struct GNUNET_TIME_Relative to;
360 now = GNUNET_TIME_absolute_get ();
361 pos = pending_timeout;
364 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
365 if (timeout->rel_value > to.rel_value)
367 if (pos->reason != 0)
368 *timeout = GNUNET_TIME_UNIT_ZERO;
373 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
374 (GNUNET_YES == is_pending (pos->prereq_id)))
379 if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
381 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
382 if (timeout->rel_value > to.rel_value)
385 if (pos->read_fd != -1)
386 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
387 if (pos->write_fd != -1)
388 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
389 if (pos->read_set != NULL)
390 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
391 if (pos->write_set != NULL)
392 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
393 if (pos->reason != 0)
394 *timeout = GNUNET_TIME_UNIT_ZERO;
401 * Check if the ready set overlaps with the set we want to have ready.
402 * If so, update the want set (set all FDs that are ready). If not,
405 * @param ready set that is ready
406 * @param want set that we want to be ready
407 * @return GNUNET_YES if there was some overlap
410 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
411 struct GNUNET_NETWORK_FDSet *want)
413 if ((NULL == want) || (NULL == ready))
415 if (GNUNET_NETWORK_fdset_overlap (ready, want))
417 /* copy all over (yes, there maybe unrelated bits,
418 * but this should not hurt well-written clients) */
419 GNUNET_NETWORK_fdset_copy (want, ready);
427 * Check if the given task is eligible to run now.
428 * Also set the reason why it is eligible.
430 * @param task task to check if it is ready
431 * @param now the current time
432 * @param rs set of FDs ready for reading
433 * @param ws set of FDs ready for writing
434 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
437 is_ready (struct Task *task,
438 struct GNUNET_TIME_Absolute now,
439 const struct GNUNET_NETWORK_FDSet *rs,
440 const struct GNUNET_NETWORK_FDSet *ws)
442 enum GNUNET_SCHEDULER_Reason reason;
444 reason = task->reason;
445 if (now.abs_value >= task->timeout.abs_value)
446 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
447 if ((0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
448 (((task->read_fd != -1) &&
449 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd))) ||
450 (set_overlaps (rs, task->read_set))))
451 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
452 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
453 (((task->write_fd != -1) &&
454 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)))
455 || (set_overlaps (ws, task->write_set))))
456 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
458 return GNUNET_NO; /* not ready */
459 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
461 if (GNUNET_YES == is_pending (task->prereq_id))
463 task->reason = reason;
464 return GNUNET_NO; /* prereq waiting */
466 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
468 task->reason = reason;
474 * Put a task that is ready for execution into the ready queue.
476 * @param task task ready for execution
479 queue_ready_task (struct Task *task)
481 enum GNUNET_SCHEDULER_Priority p = task->priority;
483 if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
484 p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
485 task->next = ready[check_priority (p)];
486 ready[check_priority (p)] = task;
492 * Check which tasks are ready and move them
493 * to the respective ready queue.
495 * @param rs FDs ready for reading
496 * @param ws FDs ready for writing
499 check_ready (const struct GNUNET_NETWORK_FDSet *rs,
500 const struct GNUNET_NETWORK_FDSet *ws)
505 struct GNUNET_TIME_Absolute now;
507 now = GNUNET_TIME_absolute_get ();
509 pos = pending_timeout;
513 if (now.abs_value >= pos->timeout.abs_value)
514 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
515 if (0 == pos->reason)
517 pending_timeout = next;
518 if (pending_timeout_last == pos)
519 pending_timeout_last = NULL;
520 queue_ready_task (pos);
527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
528 "Checking readiness of task: %llu / %p\n",
529 pos->id, pos->callback_cls);
532 if (GNUNET_YES == is_ready (pos, now, rs, ws))
538 queue_ready_task (pos);
549 * Request the shutdown of a scheduler. Marks all currently
550 * pending tasks as ready because of shutdown. This will
551 * cause all tasks to run (as soon as possible, respecting
552 * priorities and prerequisite tasks). Note that tasks
553 * scheduled AFTER this call may still be delayed arbitrarily.
556 GNUNET_SCHEDULER_shutdown ()
561 pos = pending_timeout;
564 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
565 /* we don't move the task into the ready queue yet; check_ready
566 * will do that later, possibly adding additional
567 * readiness-factors */
573 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
574 /* we don't move the task into the ready queue yet; check_ready
575 * will do that later, possibly adding additional
576 * readiness-factors */
579 for (i = 0; i < GNUNET_SCHEDULER_PRIORITY_COUNT; i++)
584 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
585 /* we don't move the task into the ready queue yet; check_ready
586 * will do that later, possibly adding additional
587 * readiness-factors */
595 * Destroy a task (release associated resources)
597 * @param t task to destroy
600 destroy_task (struct Task *t)
602 if (NULL != t->read_set)
603 GNUNET_NETWORK_fdset_destroy (t->read_set);
604 if (NULL != t->write_set)
605 GNUNET_NETWORK_fdset_destroy (t->write_set);
607 GNUNET_free (t->backtrace_strings);
614 * Run at least one task in the highest-priority queue that is not
615 * empty. Keep running tasks until we are either no longer running
616 * "URGENT" tasks or until we have at least one "pending" task (which
617 * may become ready, hence we should select on it). Naturally, if
618 * there are no more ready tasks, we also return.
620 * @param rs FDs ready for reading
621 * @param ws FDs ready for writing
624 run_ready (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws)
626 enum GNUNET_SCHEDULER_Priority p;
628 struct GNUNET_SCHEDULER_TaskContext tc;
630 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
633 if (ready_count == 0)
635 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
636 /* yes, p>0 is correct, 0 is "KEEP" which should
637 * always be an empty queue (see assertion)! */
638 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
644 GNUNET_assert (pos != NULL); /* ready_count wrong? */
645 ready[p] = pos->next;
647 if (current_priority != pos->priority)
649 current_priority = pos->priority;
650 (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
653 current_lifeness = pos->lifeness;
656 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
657 DELAY_THRESHOLD.rel_value)
659 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
660 "Task %llu took %llums to be scheduled\n",
663 GNUNET_TIME_absolute_get_duration
664 (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 abort (); // added to ready in previous select loop!
681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
682 "Running task: %llu / %p\n", pos->id, pos->callback_cls);
684 pos->callback (pos->callback_cls, &tc);
688 for (i = 0; i < pos->num_backtrace_strings; i++)
689 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
690 "Task %llu trace %d: %s\n",
691 pos->id, i, pos->backtrace_strings[i]);
697 while ((pending == NULL) || (p >= max_priority_added));
701 * Pipe used to communicate shutdown via signal.
703 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
706 * Signal handler called for SIGPIPE.
716 * Signal handler called for signals that should cause us to shutdown.
719 sighandler_shutdown ()
722 int old_errno = errno; /* backup errno */
724 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
725 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
732 * Check if the system is still life. Trigger shutdown if we
733 * have tasks, but none of them give us lifeness.
735 * @return GNUNET_OK to continue the main loop,
745 for (t = pending; NULL != t; t = t->next)
746 if (t->lifeness == GNUNET_YES)
748 for (t = pending_timeout; NULL != t; t = t->next)
749 if (t->lifeness == GNUNET_YES)
751 if ((NULL != pending) || (NULL != pending_timeout))
753 GNUNET_SCHEDULER_shutdown ();
761 * Initialize and run scheduler. This function will return when all
762 * tasks have completed. On systems with signals, receiving a SIGTERM
763 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
764 * to be run after the active task is complete. As a result, SIGTERM
765 * causes all active tasks to be scheduled with reason
766 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
767 * afterwards will execute normally!). Note that any particular signal
768 * will only shut down one scheduler; applications should always only
769 * create a single scheduler.
771 * @param task task to run immediately
772 * @param task_cls closure of task
775 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
777 struct GNUNET_NETWORK_FDSet *rs;
778 struct GNUNET_NETWORK_FDSet *ws;
779 struct GNUNET_TIME_Relative timeout;
781 struct GNUNET_SIGNAL_Context *shc_int;
782 struct GNUNET_SIGNAL_Context *shc_term;
785 struct GNUNET_SIGNAL_Context *shc_quit;
786 struct GNUNET_SIGNAL_Context *shc_hup;
787 struct GNUNET_SIGNAL_Context *shc_pipe;
789 unsigned long long last_tr;
790 unsigned int busy_wait_warning;
791 const struct GNUNET_DISK_FileHandle *pr;
794 GNUNET_assert (active_task == NULL);
795 rs = GNUNET_NETWORK_fdset_create ();
796 ws = GNUNET_NETWORK_fdset_create ();
797 GNUNET_assert (shutdown_pipe_handle == NULL);
798 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
799 GNUNET_assert (shutdown_pipe_handle != NULL);
800 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
801 GNUNET_DISK_PIPE_END_READ);
802 GNUNET_assert (pr != NULL);
803 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
804 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
806 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
807 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
808 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
810 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
811 current_lifeness = GNUNET_YES;
812 GNUNET_SCHEDULER_add_continuation (task,
813 task_cls, GNUNET_SCHEDULER_REASON_STARTUP);
814 #if ENABLE_WINDOWS_WORKAROUNDS
815 active_task = (void *) (long) -1; /* force passing of sanity check */
816 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
817 &GNUNET_OS_install_parent_control_handler,
822 busy_wait_warning = 0;
823 while (GNUNET_OK == check_lifeness ())
825 GNUNET_NETWORK_fdset_zero (rs);
826 GNUNET_NETWORK_fdset_zero (ws);
827 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
828 update_sets (rs, ws, &timeout);
829 GNUNET_NETWORK_fdset_handle_set (rs, pr);
832 /* no blocking, more work already ready! */
833 timeout = GNUNET_TIME_UNIT_ZERO;
835 if (NULL == scheduler_select)
836 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
838 ret = scheduler_select (scheduler_select_cls, rs, ws, NULL, timeout);
839 if (ret == GNUNET_SYSERR)
844 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
849 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
852 if (0 != system (lsof))
853 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "system");
859 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
861 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
862 _("Looks like we're busy waiting...\n"));
863 sleep (1); /* mitigate */
865 check_ready (rs, ws);
867 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
869 /* consume the signal */
870 GNUNET_DISK_file_read (pr, &c, sizeof (c));
871 /* mark all active tasks as ready due to shutdown */
872 GNUNET_SCHEDULER_shutdown ();
874 if (last_tr == tasks_run)
881 busy_wait_warning = 0;
884 GNUNET_SIGNAL_handler_uninstall (shc_int);
885 GNUNET_SIGNAL_handler_uninstall (shc_term);
887 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
888 GNUNET_SIGNAL_handler_uninstall (shc_quit);
889 GNUNET_SIGNAL_handler_uninstall (shc_hup);
891 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
892 shutdown_pipe_handle = NULL;
893 GNUNET_NETWORK_fdset_destroy (rs);
894 GNUNET_NETWORK_fdset_destroy (ws);
899 * Obtain the reason code for why the current task was
900 * started. Will return the same value as
901 * the GNUNET_SCHEDULER_TaskContext's reason field.
903 * @return reason(s) why the current task is run
905 enum GNUNET_SCHEDULER_Reason
906 GNUNET_SCHEDULER_get_reason ()
908 GNUNET_assert (active_task != NULL);
909 return active_task->reason;
914 * Get information about the current load of this scheduler. Use this
915 * function to determine if an elective task should be added or simply
916 * dropped (if the decision should be made based on the number of
917 * tasks ready to run).
919 * @param p priority level to look at
920 * @return number of tasks pending right now
923 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
928 GNUNET_assert (active_task != NULL);
929 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
931 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
932 p = current_priority;
934 pos = ready[check_priority (p)];
945 * Cancel the task with the specified identifier.
946 * The task must not yet have run.
948 * @param task id of the task to cancel
949 * @return original closure of the task
952 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
956 enum GNUNET_SCHEDULER_Priority p;
960 GNUNET_assert (active_task != NULL);
983 if (pending_timeout_last == t)
984 pending_timeout_last = NULL;
990 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT)
992 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
993 _("Attempt to cancel dead task %llu!\n"),
994 (unsigned long long) task);
1020 pending_timeout = t->next;
1030 prev->next = t->next;
1032 ret = t->callback_cls;
1034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1035 "Canceling task: %llu / %p\n", task, t->callback_cls);
1043 * Continue the current execution with the given function. This is
1044 * similar to the other "add" functions except that there is no delay
1045 * and the reason code can be specified.
1047 * @param task main function of the task
1048 * @param task_cls closure for 'main'
1049 * @param reason reason for task invocation
1052 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task,
1054 enum GNUNET_SCHEDULER_Reason reason)
1059 void *backtrace_array[50];
1062 GNUNET_assert (NULL != task);
1063 GNUNET_assert ((active_task != NULL) ||
1064 (reason == GNUNET_SCHEDULER_REASON_STARTUP));
1065 t = GNUNET_malloc (sizeof (struct Task));
1067 t->num_backtrace_strings = backtrace (backtrace_array, 50);
1068 t->backtrace_strings =
1069 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1074 t->callback_cls = task_cls;
1077 t->start_time = GNUNET_TIME_absolute_get ();
1080 t->priority = current_priority;
1081 t->lifeness = current_lifeness;
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "Adding continuation task: %llu / %p\n", t->id, t->callback_cls);
1086 queue_ready_task (t);
1092 * Schedule a new task to be run after the specified prerequisite task
1093 * has completed. It will be run with the priority of the calling
1096 * @param prerequisite_task run this task after the task with the given
1097 * task identifier completes (and any of our other
1098 * conditions, such as delay, read or write-readiness
1099 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1100 * on completion of other tasks (this will cause the task to run as
1101 * soon as possible).
1102 * @param task main function of the task
1103 * @param task_cls closure of task
1104 * @return unique task identifier for the job
1105 * only valid until "task" is started!
1107 GNUNET_SCHEDULER_TaskIdentifier
1108 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1109 GNUNET_SCHEDULER_Task task, void *task_cls)
1111 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1113 GNUNET_TIME_UNIT_ZERO,
1114 NULL, NULL, task, task_cls);
1119 * Schedule a new task to be run with a specified priority.
1121 * @param prio how important is the new task?
1122 * @param task main function of the task
1123 * @param task_cls closure of task
1124 * @return unique task identifier for the job
1125 * only valid until "task" is started!
1127 GNUNET_SCHEDULER_TaskIdentifier
1128 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1129 GNUNET_SCHEDULER_Task task, void *task_cls)
1131 return GNUNET_SCHEDULER_add_select (prio,
1132 GNUNET_SCHEDULER_NO_TASK,
1133 GNUNET_TIME_UNIT_ZERO,
1134 NULL, NULL, task, task_cls);
1140 * Schedule a new task to be run with a specified delay. The task
1141 * will be scheduled for execution once the delay has expired. It
1142 * will be run with the priority of the calling task.
1144 * @param delay when should this operation time out? Use
1145 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1146 * @param task main function of the task
1147 * @param task_cls closure of task
1148 * @return unique task identifier for the job
1149 * only valid until "task" is started!
1151 GNUNET_SCHEDULER_TaskIdentifier
1152 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1153 GNUNET_SCHEDULER_Task task, void *task_cls)
1156 /* new, optimized version */
1162 void *backtrace_array[MAX_TRACE_DEPTH];
1165 GNUNET_assert (active_task != NULL);
1166 GNUNET_assert (NULL != task);
1167 t = GNUNET_malloc (sizeof (struct Task));
1169 t->callback_cls = task_cls;
1171 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1172 t->backtrace_strings =
1173 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1179 t->start_time = GNUNET_TIME_absolute_get ();
1181 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1182 t->priority = current_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 prev = pending_timeout_last;
1189 if (prev->timeout.abs_value > t->timeout.abs_value)
1192 pos = prev->next; /* heuristic success! */
1196 /* heuristic failed, do traversal of timeout list */
1197 pos = pending_timeout;
1199 while ((pos != NULL) &&
1200 ((pos->timeout.abs_value <= t->timeout.abs_value) ||
1201 (pos->reason != 0)))
1207 pending_timeout = t;
1211 /* hyper-optimization... */
1212 pending_timeout_last = t;
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1216 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1221 for (i = 0; i < t->num_backtrace_strings; i++)
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "Task %llu trace %d: %s\n", t->id, i, t->backtrace_strings[i]);
1228 /* unoptimized version */
1229 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1230 GNUNET_SCHEDULER_NO_TASK, delay,
1231 NULL, NULL, task, task_cls);
1238 * Schedule a new task to be run as soon as possible. The task
1239 * will be run with the priority of the calling task.
1241 * @param task main function of the task
1242 * @param task_cls closure of task
1243 * @return unique task identifier for the job
1244 * only valid until "task" is started!
1246 GNUNET_SCHEDULER_TaskIdentifier
1247 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task, void *task_cls)
1249 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1250 GNUNET_SCHEDULER_NO_TASK,
1251 GNUNET_TIME_UNIT_ZERO,
1252 NULL, NULL, task, task_cls);
1257 * Schedule a new task to be run as soon as possible with the
1258 * (transitive) ignore-shutdown flag either explicitly set or
1259 * explicitly enabled. This task (and all tasks created from it,
1260 * other than by another call to this function) will either count or
1261 * not count for the 'lifeness' of the process. This API is only
1262 * useful in a few special cases.
1264 * @param lifeness GNUNET_YES if the task counts for lifeness, GNUNET_NO if not.
1265 * @param task main function of the task
1266 * @param task_cls closure of task
1267 * @return unique task identifier for the job
1268 * only valid until "task" is started!
1270 GNUNET_SCHEDULER_TaskIdentifier
1271 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1272 GNUNET_SCHEDULER_Task task,
1275 GNUNET_SCHEDULER_TaskIdentifier ret;
1277 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1278 GNUNET_SCHEDULER_NO_TASK,
1279 GNUNET_TIME_UNIT_ZERO,
1280 NULL, NULL, task, task_cls);
1281 GNUNET_assert (pending->id == ret);
1282 pending->lifeness = lifeness;
1290 * Schedule a new task to be run with a specified delay or when any of
1291 * the specified file descriptor sets is ready. The delay can be used
1292 * as a timeout on the socket(s) being ready. The task will be
1293 * scheduled for execution once either the delay has expired or any of
1294 * the socket operations is ready. This is the most general
1295 * function of the "add" family. Note that the "prerequisite_task"
1296 * must be satisfied in addition to any of the other conditions. In
1297 * other words, the task will be started when
1299 * (prerequisite-run)
1303 * || shutdown-active )
1306 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1307 * which means that the task will only be run after we receive SIGTERM
1308 * @param rfd file descriptor we want to read (can be -1)
1309 * @param wfd file descriptors we want to write (can be -1)
1310 * @param task main function of the task
1311 * @param task_cls closure of task
1312 * @return unique task identifier for the job
1313 * only valid until "task" is started!
1316 GNUNET_SCHEDULER_TaskIdentifier
1317 add_without_sets (struct GNUNET_TIME_Relative delay,
1318 int rfd, int wfd, GNUNET_SCHEDULER_Task task, void *task_cls)
1323 void *backtrace_array[MAX_TRACE_DEPTH];
1326 GNUNET_assert (active_task != NULL);
1327 GNUNET_assert (NULL != task);
1328 t = GNUNET_malloc (sizeof (struct Task));
1330 t->callback_cls = task_cls;
1332 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1333 t->backtrace_strings =
1334 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1339 int flags = fcntl (rfd, F_GETFD);
1341 if (flags == -1 && errno == EBADF)
1343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n",
1348 for (i = 0; i < t->num_backtrace_strings; i++)
1349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1350 "Trace: %s\n", t->backtrace_strings[i]);
1357 int flags = fcntl (wfd, F_GETFD);
1359 if (flags == -1 && errno == EBADF)
1361 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n",
1366 for (i = 0; i < t->num_backtrace_strings; i++)
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1368 "Trace: %s\n", t->backtrace_strings[i]);
1375 GNUNET_assert (wfd >= -1);
1379 t->start_time = GNUNET_TIME_absolute_get ();
1381 t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
1382 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1383 t->priority = check_priority (current_priority);
1384 t->lifeness = current_lifeness;
1387 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1390 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1395 for (i = 0; i < t->num_backtrace_strings; i++)
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 "Task %llu trace %d: %s\n", t->id, i, t->backtrace_strings[i]);
1406 * Schedule a new task to be run with a specified delay or when the
1407 * specified file descriptor is ready for reading. The delay can be
1408 * used as a timeout on the socket being ready. The task will be
1409 * scheduled for execution once either the delay has expired or the
1410 * socket operation is ready. It will be run with the priority of
1413 * @param delay when should this operation time out? Use
1414 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1415 * @param rfd read file-descriptor
1416 * @param task main function of the task
1417 * @param task_cls closure of task
1418 * @return unique task identifier for the job
1419 * only valid until "task" is started!
1421 GNUNET_SCHEDULER_TaskIdentifier
1422 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1423 struct GNUNET_NETWORK_Handle *rfd,
1424 GNUNET_SCHEDULER_Task task, void *task_cls)
1427 struct GNUNET_NETWORK_FDSet *rs;
1428 GNUNET_SCHEDULER_TaskIdentifier ret;
1430 GNUNET_assert (rfd != NULL);
1431 rs = GNUNET_NETWORK_fdset_create ();
1432 GNUNET_NETWORK_fdset_set (rs, rfd);
1433 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1434 GNUNET_SCHEDULER_NO_TASK, delay,
1435 rs, NULL, task, task_cls);
1436 GNUNET_NETWORK_fdset_destroy (rs);
1439 return add_without_sets (delay,
1440 GNUNET_NETWORK_get_fd (rfd), -1, task, task_cls);
1446 * Schedule a new task to be run with a specified delay or when the
1447 * specified file descriptor is ready for writing. The delay can be
1448 * used as a timeout on the socket being ready. The task will be
1449 * scheduled for execution once either the delay has expired or the
1450 * socket operation is ready. It will be run with the priority of
1453 * @param delay when should this operation time out? Use
1454 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1455 * @param wfd write file-descriptor
1456 * @param task main function of the task
1457 * @param task_cls closure of task
1458 * @return unique task identifier for the job
1459 * only valid until "task" is started!
1461 GNUNET_SCHEDULER_TaskIdentifier
1462 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1463 struct GNUNET_NETWORK_Handle *wfd,
1464 GNUNET_SCHEDULER_Task task, void *task_cls)
1467 struct GNUNET_NETWORK_FDSet *ws;
1468 GNUNET_SCHEDULER_TaskIdentifier ret;
1470 GNUNET_assert (wfd != NULL);
1471 ws = GNUNET_NETWORK_fdset_create ();
1472 GNUNET_NETWORK_fdset_set (ws, wfd);
1473 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1474 GNUNET_SCHEDULER_NO_TASK, delay,
1475 NULL, ws, task, task_cls);
1476 GNUNET_NETWORK_fdset_destroy (ws);
1479 GNUNET_assert (GNUNET_NETWORK_get_fd (wfd) >= 0);
1480 return add_without_sets (delay,
1481 -1, GNUNET_NETWORK_get_fd (wfd), task, task_cls);
1487 * Schedule a new task to be run with a specified delay or when the
1488 * specified file descriptor is ready for reading. The delay can be
1489 * used as a timeout on the socket being ready. The task will be
1490 * scheduled for execution once either the delay has expired or the
1491 * socket operation is ready. It will be run with the priority of
1494 * @param delay when should this operation time out? Use
1495 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1496 * @param rfd read file-descriptor
1497 * @param task main function of the task
1498 * @param task_cls closure of task
1499 * @return unique task identifier for the job
1500 * only valid until "task" is started!
1502 GNUNET_SCHEDULER_TaskIdentifier
1503 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1504 const struct GNUNET_DISK_FileHandle *rfd,
1505 GNUNET_SCHEDULER_Task task, void *task_cls)
1508 struct GNUNET_NETWORK_FDSet *rs;
1509 GNUNET_SCHEDULER_TaskIdentifier ret;
1511 GNUNET_assert (rfd != NULL);
1512 rs = GNUNET_NETWORK_fdset_create ();
1513 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1514 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1515 GNUNET_SCHEDULER_NO_TASK, delay,
1516 rs, NULL, task, task_cls);
1517 GNUNET_NETWORK_fdset_destroy (rs);
1522 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1523 return add_without_sets (delay, fd, -1, task, task_cls);
1530 * Schedule a new task to be run with a specified delay or when the
1531 * specified file descriptor is ready for writing. The delay can be
1532 * used as a timeout on the socket being ready. The task will be
1533 * scheduled for execution once either the delay has expired or the
1534 * socket operation is ready. It will be run with the priority of
1537 * @param delay when should this operation time out? Use
1538 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1539 * @param wfd write file-descriptor
1540 * @param task main function of the task
1541 * @param task_cls closure of task
1542 * @return unique task identifier for the job
1543 * only valid until "task" is started!
1545 GNUNET_SCHEDULER_TaskIdentifier
1546 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1547 const struct GNUNET_DISK_FileHandle *wfd,
1548 GNUNET_SCHEDULER_Task task, void *task_cls)
1551 struct GNUNET_NETWORK_FDSet *ws;
1552 GNUNET_SCHEDULER_TaskIdentifier ret;
1554 GNUNET_assert (wfd != NULL);
1555 ws = GNUNET_NETWORK_fdset_create ();
1556 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1557 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1558 GNUNET_SCHEDULER_NO_TASK,
1559 delay, NULL, ws, task, task_cls);
1560 GNUNET_NETWORK_fdset_destroy (ws);
1565 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1566 GNUNET_assert (fd >= 0);
1567 return add_without_sets (delay, -1, fd, task, task_cls);
1575 * Schedule a new task to be run with a specified delay or when any of
1576 * the specified file descriptor sets is ready. The delay can be used
1577 * as a timeout on the socket(s) being ready. The task will be
1578 * scheduled for execution once either the delay has expired or any of
1579 * the socket operations is ready. This is the most general
1580 * function of the "add" family. Note that the "prerequisite_task"
1581 * must be satisfied in addition to any of the other conditions. In
1582 * other words, the task will be started when
1584 * (prerequisite-run)
1588 * || (shutdown-active && run-on-shutdown) )
1591 * @param prio how important is this task?
1592 * @param prerequisite_task run this task after the task with the given
1593 * task identifier completes (and any of our other
1594 * conditions, such as delay, read or write-readiness
1595 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1596 * on completion of other tasks.
1597 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1598 * which means that the task will only be run after we receive SIGTERM
1599 * @param rs set of file descriptors we want to read (can be NULL)
1600 * @param ws set of file descriptors we want to write (can be NULL)
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 GNUNET_SCHEDULER_TaskIdentifier
1607 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1608 GNUNET_SCHEDULER_TaskIdentifier
1610 struct GNUNET_TIME_Relative delay,
1611 const struct GNUNET_NETWORK_FDSet *rs,
1612 const struct GNUNET_NETWORK_FDSet *ws,
1613 GNUNET_SCHEDULER_Task task, void *task_cls)
1618 void *backtrace_array[MAX_TRACE_DEPTH];
1621 GNUNET_assert (active_task != NULL);
1622 GNUNET_assert (NULL != task);
1623 t = GNUNET_malloc (sizeof (struct Task));
1625 t->callback_cls = task_cls;
1627 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1628 t->backtrace_strings =
1629 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1635 t->read_set = GNUNET_NETWORK_fdset_create ();
1636 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1640 t->write_set = GNUNET_NETWORK_fdset_create ();
1641 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1645 t->start_time = GNUNET_TIME_absolute_get ();
1647 t->prereq_id = prerequisite_task;
1648 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1650 check_priority ((prio ==
1651 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority
1653 t->lifeness = current_lifeness;
1656 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1659 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1664 for (i = 0; i < t->num_backtrace_strings; i++)
1665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1666 "Task %llu trace %d: %s\n", t->id, i, t->backtrace_strings[i]);
1671 /* end of scheduler.c */