2 This file is part of GNUnet
3 (C) 2009-2013 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 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 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_util_lib.h"
30 #define LOG(kind,...) GNUNET_log_from (kind, "util-scheduler", __VA_ARGS__)
32 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-scheduler", syscall)
39 * Use lsof to generate file descriptor reports on select error?
40 * (turn off for stable releases).
42 #define USE_LSOF GNUNET_NO
45 * Obtain trace information for all scheduler calls that schedule tasks.
47 #define EXECINFO GNUNET_NO
50 * Check each file descriptor before adding
52 #define DEBUG_FDS GNUNET_NO
55 * Depth of the traces collected via EXECINFO.
57 #define MAX_TRACE_DEPTH 50
61 * Should we figure out which tasks are delayed for a while
62 * before they are run? (Consider using in combination with EXECINFO).
64 #define PROFILE_DELAYS GNUNET_NO
67 * Task that were in the queue for longer than this are reported if
68 * PROFILE_DELAYS is active.
70 #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 @e 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 * Absolute timeout value for the task, or
115 * GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
117 struct GNUNET_TIME_Absolute timeout;
121 * When was the task scheduled?
123 struct GNUNET_TIME_Absolute start_time;
127 * Why is the task ready? Set after task is added to ready queue.
128 * Initially set to zero. All reasons that have already been
129 * satisfied (i.e. read or write ready) will be set over time.
131 enum GNUNET_SCHEDULER_Reason reason;
136 enum GNUNET_SCHEDULER_Priority priority;
139 * Set if we only wait for reading from a single FD, otherwise -1.
144 * Set if we only wait for writing to a single FD, otherwise -1.
149 * Should the existence of this task in the queue be counted as
150 * reason to not shutdown the scheduler?
156 * Array of strings which make up a backtrace from the point when this
157 * task was scheduled (essentially, who scheduled the task?)
159 char **backtrace_strings;
162 * Size of the backtrace_strings array
164 int num_backtrace_strings;
172 * List of tasks waiting for an event.
174 static struct Task *pending;
177 * List of tasks waiting ONLY for a timeout event.
178 * Sorted by timeout (earliest first). Used so that
179 * we do not traverse the list of these tasks when
180 * building select sets (we just look at the head
181 * to determine the respective timeout ONCE).
183 static struct Task *pending_timeout;
186 * Last inserted task waiting ONLY for a timeout event.
187 * Used to (heuristically) speed up insertion.
189 static struct Task *pending_timeout_last;
192 * ID of the task that is running right now.
194 static struct Task *active_task;
197 * List of tasks ready to run right now,
198 * grouped by importance.
200 static struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
203 * Identity of the last task queued. Incremented for each task to
204 * generate a unique task ID (it is virtually impossible to start
205 * more than 2^64 tasks during the lifetime of a process).
207 static GNUNET_SCHEDULER_TaskIdentifier last_id;
210 * Number of tasks on the ready list.
212 static unsigned int ready_count;
215 * How many tasks have we run so far?
217 static unsigned long long tasks_run;
220 * Priority of the task running right now. Only
221 * valid while a task is running.
223 static enum GNUNET_SCHEDULER_Priority current_priority;
226 * Priority of the highest task added in the current select
229 static enum GNUNET_SCHEDULER_Priority max_priority_added;
232 * Value of the 'lifeness' flag for the current task.
234 static int current_lifeness;
237 * Function to use as a select() in the scheduler.
238 * If NULL, we use GNUNET_NETWORK_socket_select().
240 static GNUNET_SCHEDULER_select scheduler_select;
243 * Closure for 'scheduler_select'.
245 static void *scheduler_select_cls;
248 * Sets the select function to use in the scheduler (scheduler_select).
250 * @param new_select new select function to use
251 * @param new_select_cls closure for @a new_select
252 * @return previously used select function, NULL for default
255 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
256 void *new_select_cls)
258 scheduler_select = new_select;
259 scheduler_select_cls = new_select_cls;
264 * Check that the given priority is legal (and return it).
266 * @param p priority value to check
267 * @return p on success, 0 on error
269 static enum GNUNET_SCHEDULER_Priority
270 check_priority (enum GNUNET_SCHEDULER_Priority p)
272 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
275 return 0; /* make compiler happy */
280 * Update all sets and timeout for select.
282 * @param rs read-set, set to all FDs we would like to read (updated)
283 * @param ws write-set, set to all FDs we would like to write (updated)
284 * @param timeout next timeout (updated)
287 update_sets (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws,
288 struct GNUNET_TIME_Relative *timeout)
291 struct GNUNET_TIME_Absolute now;
292 struct GNUNET_TIME_Relative to;
294 now = GNUNET_TIME_absolute_get ();
295 pos = pending_timeout;
298 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
299 if (timeout->rel_value_us > to.rel_value_us)
301 if (pos->reason != 0)
302 *timeout = GNUNET_TIME_UNIT_ZERO;
307 if (pos->timeout.abs_value_us != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
309 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
310 if (timeout->rel_value_us > to.rel_value_us)
313 if (-1 != pos->read_fd)
314 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
315 if (-1 != pos->write_fd)
316 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
317 if (NULL != pos->read_set)
318 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
319 if (NULL != pos->write_set)
320 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
321 if (0 != pos->reason)
322 *timeout = GNUNET_TIME_UNIT_ZERO;
329 * Check if the ready set overlaps with the set we want to have ready.
330 * If so, update the want set (set all FDs that are ready). If not,
333 * @param ready set that is ready
334 * @param want set that we want to be ready
335 * @return GNUNET_YES if there was some overlap
338 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
339 struct GNUNET_NETWORK_FDSet *want)
341 if ((NULL == want) || (NULL == ready))
343 if (GNUNET_NETWORK_fdset_overlap (ready, want))
345 /* copy all over (yes, there maybe unrelated bits,
346 * but this should not hurt well-written clients) */
347 GNUNET_NETWORK_fdset_copy (want, ready);
355 * Check if the given task is eligible to run now.
356 * Also set the reason why it is eligible.
358 * @param task task to check if it is ready
359 * @param now the current time
360 * @param rs set of FDs ready for reading
361 * @param ws set of FDs ready for writing
362 * @return #GNUNET_YES if we can run it, #GNUNET_NO if not.
365 is_ready (struct Task *task, struct GNUNET_TIME_Absolute now,
366 const struct GNUNET_NETWORK_FDSet *rs,
367 const struct GNUNET_NETWORK_FDSet *ws)
369 enum GNUNET_SCHEDULER_Reason reason;
371 reason = task->reason;
372 if (now.abs_value_us >= task->timeout.abs_value_us)
373 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
374 if ((0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
375 (((task->read_fd != -1) &&
376 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd))) ||
377 (set_overlaps (rs, task->read_set))))
378 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
379 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
380 (((task->write_fd != -1) &&
381 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)))
382 || (set_overlaps (ws, task->write_set))))
383 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
385 return GNUNET_NO; /* not ready */
386 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
387 task->reason = reason;
393 * Put a task that is ready for execution into the ready queue.
395 * @param task task ready for execution
398 queue_ready_task (struct Task *task)
400 enum GNUNET_SCHEDULER_Priority p = task->priority;
402 if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
403 p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
404 task->next = ready[check_priority (p)];
405 ready[check_priority (p)] = task;
411 * Check which tasks are ready and move them
412 * to the respective ready queue.
414 * @param rs FDs ready for reading
415 * @param ws FDs ready for writing
418 check_ready (const struct GNUNET_NETWORK_FDSet *rs,
419 const struct GNUNET_NETWORK_FDSet *ws)
424 struct GNUNET_TIME_Absolute now;
426 now = GNUNET_TIME_absolute_get ();
428 pos = pending_timeout;
432 if (now.abs_value_us >= pos->timeout.abs_value_us)
433 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
434 if (0 == pos->reason)
436 pending_timeout = next;
437 if (pending_timeout_last == pos)
438 pending_timeout_last = NULL;
439 queue_ready_task (pos);
445 LOG (GNUNET_ERROR_TYPE_DEBUG,
446 "Checking readiness of task: %llu / %p\n",
447 pos->id, pos->callback_cls);
449 if (GNUNET_YES == is_ready (pos, now, rs, ws))
455 queue_ready_task (pos);
466 * Request the shutdown of a scheduler. Marks all currently
467 * pending tasks as ready because of shutdown. This will
468 * cause all tasks to run (as soon as possible, respecting
469 * priorities and prerequisite tasks). Note that tasks
470 * scheduled AFTER this call may still be delayed arbitrarily.
473 GNUNET_SCHEDULER_shutdown ()
478 pos = pending_timeout;
481 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
482 /* we don't move the task into the ready queue yet; check_ready
483 * will do that later, possibly adding additional
484 * readiness-factors */
490 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
491 /* we don't move the task into the ready queue yet; check_ready
492 * will do that later, possibly adding additional
493 * readiness-factors */
496 for (i = 0; i < GNUNET_SCHEDULER_PRIORITY_COUNT; i++)
501 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
502 /* we don't move the task into the ready queue yet; check_ready
503 * will do that later, possibly adding additional
504 * readiness-factors */
512 * Destroy a task (release associated resources)
514 * @param t task to destroy
517 destroy_task (struct Task *t)
519 if (NULL != t->read_set)
520 GNUNET_NETWORK_fdset_destroy (t->read_set);
521 if (NULL != t->write_set)
522 GNUNET_NETWORK_fdset_destroy (t->write_set);
524 GNUNET_free (t->backtrace_strings);
531 * Run at least one task in the highest-priority queue that is not
532 * empty. Keep running tasks until we are either no longer running
533 * "URGENT" tasks or until we have at least one "pending" task (which
534 * may become ready, hence we should select on it). Naturally, if
535 * there are no more ready tasks, we also return.
537 * @param rs FDs ready for reading
538 * @param ws FDs ready for writing
541 run_ready (struct GNUNET_NETWORK_FDSet *rs,
542 struct GNUNET_NETWORK_FDSet *ws)
544 enum GNUNET_SCHEDULER_Priority p;
546 struct GNUNET_SCHEDULER_TaskContext tc;
548 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
551 if (0 == ready_count)
553 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
554 /* yes, p>0 is correct, 0 is "KEEP" which should
555 * always be an empty queue (see assertion)! */
556 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
562 GNUNET_assert (NULL != pos); /* ready_count wrong? */
563 ready[p] = pos->next;
565 current_priority = pos->priority;
566 current_lifeness = pos->lifeness;
569 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
570 DELAY_THRESHOLD.rel_value_us)
572 LOG (GNUNET_ERROR_TYPE_DEBUG,
573 "Task %llu took %s to be scheduled\n",
574 (unsigned long long) pos->id,
575 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
579 tc.reason = pos->reason;
580 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
581 if ((pos->read_fd != -1) &&
582 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)))
583 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
584 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
585 if ((pos->write_fd != -1) &&
586 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
587 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
588 if (((tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
589 (pos->write_fd != -1) &&
590 (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd)))
591 GNUNET_abort (); // added to ready in previous select loop!
592 LOG (GNUNET_ERROR_TYPE_DEBUG,
593 "Running task: %llu / %p\n", pos->id,
595 pos->callback (pos->callback_cls, &tc);
599 for (i = 0; i < pos->num_backtrace_strings; i++)
600 LOG (GNUNET_ERROR_TYPE_ERROR,
601 "Task %llu trace %d: %s\n",
604 pos->backtrace_strings[i]);
610 while ((NULL == pending) || (p >= max_priority_added));
614 * Pipe used to communicate shutdown via signal.
616 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
619 * Process ID of this process at the time we installed the various
625 * Signal handler called for SIGPIPE.
637 * Wait for a short time.
638 * Sleeps for @a ms ms (as that should be long enough for virtually all
639 * modern systems to context switch and allow another process to do
642 * @param ms how many ms to wait
645 short_wait (unsigned int ms)
647 struct GNUNET_TIME_Relative timeout;
649 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
650 (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
655 * Signal handler called for signals that should cause us to shutdown.
658 sighandler_shutdown ()
661 int old_errno = errno; /* backup errno */
663 if (getpid () != my_pid)
664 exit (1); /* we have fork'ed since the signal handler was created,
665 * ignore the signal, see https://gnunet.org/vfork discussion */
666 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
667 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
674 * Check if the system is still life. Trigger shutdown if we
675 * have tasks, but none of them give us lifeness.
677 * @return #GNUNET_OK to continue the main loop,
687 for (t = pending; NULL != t; t = t->next)
688 if (t->lifeness == GNUNET_YES)
690 for (t = pending_timeout; NULL != t; t = t->next)
691 if (t->lifeness == GNUNET_YES)
693 if ((NULL != pending) || (NULL != pending_timeout))
695 GNUNET_SCHEDULER_shutdown ();
703 * Initialize and run scheduler. This function will return when all
704 * tasks have completed. On systems with signals, receiving a SIGTERM
705 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown()
706 * to be run after the active task is complete. As a result, SIGTERM
707 * causes all active tasks to be scheduled with reason
708 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
709 * afterwards will execute normally!). Note that any particular signal
710 * will only shut down one scheduler; applications should always only
711 * create a single scheduler.
713 * @param task task to run immediately
714 * @param task_cls closure of @a task
717 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
719 struct GNUNET_NETWORK_FDSet *rs;
720 struct GNUNET_NETWORK_FDSet *ws;
721 struct GNUNET_TIME_Relative timeout;
723 struct GNUNET_SIGNAL_Context *shc_int;
724 struct GNUNET_SIGNAL_Context *shc_term;
725 #if (SIGTERM != GNUNET_TERM_SIG)
726 struct GNUNET_SIGNAL_Context *shc_gterm;
730 struct GNUNET_SIGNAL_Context *shc_quit;
731 struct GNUNET_SIGNAL_Context *shc_hup;
732 struct GNUNET_SIGNAL_Context *shc_pipe;
734 unsigned long long last_tr;
735 unsigned int busy_wait_warning;
736 const struct GNUNET_DISK_FileHandle *pr;
739 GNUNET_assert (NULL == active_task);
740 rs = GNUNET_NETWORK_fdset_create ();
741 ws = GNUNET_NETWORK_fdset_create ();
742 GNUNET_assert (NULL == shutdown_pipe_handle);
743 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
744 GNUNET_assert (NULL != shutdown_pipe_handle);
745 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
746 GNUNET_DISK_PIPE_END_READ);
747 GNUNET_assert (pr != NULL);
749 LOG (GNUNET_ERROR_TYPE_DEBUG, "Registering signal handlers\n");
750 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
751 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
752 #if (SIGTERM != GNUNET_TERM_SIG)
753 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG, &sighandler_shutdown);
756 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
757 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
758 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
760 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
761 current_lifeness = GNUNET_YES;
762 GNUNET_SCHEDULER_add_continuation (task, task_cls,
763 GNUNET_SCHEDULER_REASON_STARTUP);
764 active_task = (void *) (long) -1; /* force passing of sanity check */
765 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
766 &GNUNET_OS_install_parent_control_handler,
770 busy_wait_warning = 0;
771 while (GNUNET_OK == check_lifeness ())
773 GNUNET_NETWORK_fdset_zero (rs);
774 GNUNET_NETWORK_fdset_zero (ws);
775 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
776 update_sets (rs, ws, &timeout);
777 GNUNET_NETWORK_fdset_handle_set (rs, pr);
780 /* no blocking, more work already ready! */
781 timeout = GNUNET_TIME_UNIT_ZERO;
783 if (NULL == scheduler_select)
784 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
786 ret = scheduler_select (scheduler_select_cls, rs, ws, NULL, timeout);
787 if (ret == GNUNET_SYSERR)
792 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
797 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
800 if (0 != system (lsof))
801 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
809 (0 == timeout.rel_value_us) &&
810 (busy_wait_warning > 16) )
812 LOG (GNUNET_ERROR_TYPE_WARNING,
813 _("Looks like we're busy waiting...\n"));
814 short_wait (100); /* mitigate */
816 check_ready (rs, ws);
818 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
820 /* consume the signal */
821 GNUNET_DISK_file_read (pr, &c, sizeof (c));
822 /* mark all active tasks as ready due to shutdown */
823 GNUNET_SCHEDULER_shutdown ();
825 if (last_tr == tasks_run)
833 busy_wait_warning = 0;
836 GNUNET_SIGNAL_handler_uninstall (shc_int);
837 GNUNET_SIGNAL_handler_uninstall (shc_term);
838 #if (SIGTERM != GNUNET_TERM_SIG)
839 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
842 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
843 GNUNET_SIGNAL_handler_uninstall (shc_quit);
844 GNUNET_SIGNAL_handler_uninstall (shc_hup);
846 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
847 shutdown_pipe_handle = NULL;
848 GNUNET_NETWORK_fdset_destroy (rs);
849 GNUNET_NETWORK_fdset_destroy (ws);
854 * Obtain the reason code for why the current task was
855 * started. Will return the same value as
856 * the `struct GNUNET_SCHEDULER_TaskContext`'s reason field.
858 * @return reason(s) why the current task is run
860 enum GNUNET_SCHEDULER_Reason
861 GNUNET_SCHEDULER_get_reason ()
863 GNUNET_assert (active_task != NULL);
864 return active_task->reason;
869 * Get information about the current load of this scheduler. Use this
870 * function to determine if an elective task should be added or simply
871 * dropped (if the decision should be made based on the number of
872 * tasks ready to run).
874 * @param p priority level to look at
875 * @return number of tasks pending right now
878 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
883 GNUNET_assert (active_task != NULL);
884 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
886 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
887 p = current_priority;
889 pos = ready[check_priority (p)];
900 * Cancel the task with the specified identifier.
901 * The task must not yet have run.
903 * @param task id of the task to cancel
904 * @return original closure of the task
907 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
911 enum GNUNET_SCHEDULER_Priority p;
915 GNUNET_assert (NULL != active_task);
938 if (pending_timeout_last == t)
939 pending_timeout_last = NULL;
945 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT)
947 LOG (GNUNET_ERROR_TYPE_ERROR,
948 _("Attempt to cancel dead task %llu!\n"),
949 (unsigned long long) task);
975 pending_timeout = t->next;
985 prev->next = t->next;
987 ret = t->callback_cls;
988 LOG (GNUNET_ERROR_TYPE_DEBUG,
989 "Canceling task: %llu / %p\n",
998 * Continue the current execution with the given function. This is
999 * similar to the other "add" functions except that there is no delay
1000 * and the reason code can be specified.
1002 * @param task main function of the task
1003 * @param task_cls closure for @a task
1004 * @param reason reason for task invocation
1005 * @param priority priority to use for the task
1008 GNUNET_SCHEDULER_add_continuation_with_priority (GNUNET_SCHEDULER_Task task, void *task_cls,
1009 enum GNUNET_SCHEDULER_Reason reason,
1010 enum GNUNET_SCHEDULER_Priority priority)
1015 void *backtrace_array[50];
1018 GNUNET_assert (NULL != task);
1019 GNUNET_assert ((NULL != active_task) ||
1020 (GNUNET_SCHEDULER_REASON_STARTUP == reason));
1021 t = GNUNET_new (struct Task);
1023 t->num_backtrace_strings = backtrace (backtrace_array, 50);
1024 t->backtrace_strings =
1025 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1030 t->callback_cls = task_cls;
1033 t->start_time = GNUNET_TIME_absolute_get ();
1036 t->priority = priority;
1037 t->lifeness = current_lifeness;
1038 LOG (GNUNET_ERROR_TYPE_DEBUG,
1039 "Adding continuation task: %llu / %p\n",
1042 queue_ready_task (t);
1047 * Continue the current execution with the given function. This is
1048 * similar to the other "add" functions except that there is no delay
1049 * and the reason code can be specified.
1051 * @param task main function of the task
1052 * @param task_cls closure for @a task
1053 * @param reason reason for task invocation
1056 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls,
1057 enum GNUNET_SCHEDULER_Reason reason)
1059 GNUNET_SCHEDULER_add_continuation_with_priority (task, task_cls,
1061 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
1066 * Schedule a new task to be run with a specified priority.
1068 * @param prio how important is the new task?
1069 * @param task main function of the task
1070 * @param task_cls closure of @a task
1071 * @return unique task identifier for the job
1072 * only valid until @a task is started!
1074 GNUNET_SCHEDULER_TaskIdentifier
1075 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1076 GNUNET_SCHEDULER_Task task, void *task_cls)
1078 return GNUNET_SCHEDULER_add_select (prio,
1079 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1086 * Schedule a new task to be run with a specified delay. The task
1087 * will be scheduled for execution once the delay has expired.
1089 * @param delay when should this operation time out? Use
1090 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1091 * @param priority priority to use for the task
1092 * @param task main function of the task
1093 * @param task_cls closure of @a task
1094 * @return unique task identifier for the job
1095 * only valid until @a task is started!
1097 GNUNET_SCHEDULER_TaskIdentifier
1098 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1099 enum GNUNET_SCHEDULER_Priority priority,
1100 GNUNET_SCHEDULER_Task task, void *task_cls)
1107 void *backtrace_array[MAX_TRACE_DEPTH];
1110 GNUNET_assert (NULL != active_task);
1111 GNUNET_assert (NULL != task);
1112 t = GNUNET_new (struct Task);
1114 t->callback_cls = task_cls;
1116 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1117 t->backtrace_strings =
1118 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1124 t->start_time = GNUNET_TIME_absolute_get ();
1126 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1127 t->priority = priority;
1128 t->lifeness = current_lifeness;
1129 /* try tail first (optimization in case we are
1130 * appending to a long list of tasks with timeouts) */
1131 prev = pending_timeout_last;
1134 if (prev->timeout.abs_value_us > t->timeout.abs_value_us)
1137 pos = prev->next; /* heuristic success! */
1141 /* heuristic failed, do traversal of timeout list */
1142 pos = pending_timeout;
1144 while ((pos != NULL) &&
1145 ((pos->timeout.abs_value_us <= t->timeout.abs_value_us) ||
1146 (0 != pos->reason)))
1152 pending_timeout = t;
1156 /* hyper-optimization... */
1157 pending_timeout_last = t;
1159 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1164 for (i = 0; i < t->num_backtrace_strings; i++)
1165 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1166 t->backtrace_strings[i]);
1173 * Schedule a new task to be run with a specified delay. The task
1174 * will be scheduled for execution once the delay has expired. It
1175 * will be run with the DEFAULT priority.
1177 * @param delay when should this operation time out? Use
1178 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1179 * @param task main function of the task
1180 * @param task_cls closure of task
1181 * @return unique task identifier for the job
1182 * only valid until "task" is started!
1184 GNUNET_SCHEDULER_TaskIdentifier
1185 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1186 GNUNET_SCHEDULER_Task task, void *task_cls)
1188 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1189 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1195 * Schedule a new task to be run as soon as possible. Note that this
1196 * does not guarantee that this will be the next task that is being
1197 * run, as other tasks with higher priority (or that are already ready
1198 * to run) might get to run first. Just as with delays, clients must
1199 * not rely on any particular order of execution between tasks
1200 * scheduled concurrently.
1202 * The task will be run with the DEFAULT priority.
1204 * @param task main function of the task
1205 * @param task_cls closure of @a task
1206 * @return unique task identifier for the job
1207 * only valid until "task" is started!
1209 GNUNET_SCHEDULER_TaskIdentifier
1210 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task, void *task_cls)
1212 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO, task, task_cls);
1217 * Schedule a new task to be run as soon as possible with the
1218 * (transitive) ignore-shutdown flag either explicitly set or
1219 * explicitly enabled. This task (and all tasks created from it,
1220 * other than by another call to this function) will either count or
1221 * not count for the "lifeness" of the process. This API is only
1222 * useful in a few special cases.
1224 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
1225 * @param task main function of the task
1226 * @param task_cls closure of @a task
1227 * @return unique task identifier for the job
1228 * only valid until @a task is started!
1230 GNUNET_SCHEDULER_TaskIdentifier
1231 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1232 GNUNET_SCHEDULER_Task task,
1235 GNUNET_SCHEDULER_TaskIdentifier ret;
1238 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1239 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1241 GNUNET_assert (pending->id == ret);
1242 pending->lifeness = lifeness;
1248 * Schedule a new task to be run with a specified delay or when any of
1249 * the specified file descriptor sets is ready. The delay can be used
1250 * as a timeout on the socket(s) being ready. The task will be
1251 * scheduled for execution once either the delay has expired or any of
1252 * the socket operations is ready. This is the most general
1253 * function of the "add" family. Note that the "prerequisite_task"
1254 * must be satisfied in addition to any of the other conditions. In
1255 * other words, the task will be started when
1257 * (prerequisite-run)
1261 * || shutdown-active )
1264 * @param delay how long should we wait? Use #GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1265 * which means that the task will only be run after we receive SIGTERM
1266 * @param priority priority to use
1267 * @param rfd file descriptor we want to read (can be -1)
1268 * @param wfd file descriptors we want to write (can be -1)
1269 * @param task main function of the task
1270 * @param task_cls closure of @a task
1271 * @return unique task identifier for the job
1272 * only valid until "task" is started!
1275 static GNUNET_SCHEDULER_TaskIdentifier
1276 add_without_sets (struct GNUNET_TIME_Relative delay,
1277 enum GNUNET_SCHEDULER_Priority priority,
1279 GNUNET_SCHEDULER_Task task, void *task_cls)
1284 void *backtrace_array[MAX_TRACE_DEPTH];
1287 GNUNET_assert (NULL != active_task);
1288 GNUNET_assert (NULL != task);
1289 t = GNUNET_new (struct Task);
1291 t->callback_cls = task_cls;
1293 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1294 t->backtrace_strings =
1295 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1300 int flags = fcntl (rfd, F_GETFD);
1302 if ((flags == -1) && (errno == EBADF))
1304 LOG (GNUNET_ERROR_TYPE_ERROR,
1305 "Got invalid file descriptor %d!\n",
1310 for (i = 0; i < t->num_backtrace_strings; i++)
1311 LOG (GNUNET_ERROR_TYPE_DEBUG,
1313 t->backtrace_strings[i]);
1320 int flags = fcntl (wfd, F_GETFD);
1322 if (flags == -1 && errno == EBADF)
1324 LOG (GNUNET_ERROR_TYPE_ERROR,
1325 "Got invalid file descriptor %d!\n",
1330 for (i = 0; i < t->num_backtrace_strings; i++)
1331 LOG (GNUNET_ERROR_TYPE_DEBUG,
1333 t->backtrace_strings[i]);
1340 GNUNET_assert (wfd >= -1);
1344 t->start_time = GNUNET_TIME_absolute_get ();
1346 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1347 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1348 t->lifeness = current_lifeness;
1351 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1352 LOG (GNUNET_ERROR_TYPE_DEBUG,
1353 "Adding task: %llu / %p\n",
1359 for (i = 0; i < t->num_backtrace_strings; i++)
1360 LOG (GNUNET_ERROR_TYPE_DEBUG,
1361 "Task %llu trace %d: %s\n",
1364 t->backtrace_strings[i]);
1373 * Schedule a new task to be run with a specified delay or when the
1374 * specified file descriptor is ready for reading. The delay can be
1375 * used as a timeout on the socket being ready. The task will be
1376 * scheduled for execution once either the delay has expired or the
1377 * socket operation is ready. It will be run with the DEFAULT priority.
1379 * @param delay when should this operation time out? Use
1380 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1381 * @param rfd read file-descriptor
1382 * @param task main function of the task
1383 * @param task_cls closure of @a task
1384 * @return unique task identifier for the job
1385 * only valid until @a task is started!
1387 GNUNET_SCHEDULER_TaskIdentifier
1388 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1389 struct GNUNET_NETWORK_Handle *rfd,
1390 GNUNET_SCHEDULER_Task task, void *task_cls)
1392 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1393 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1394 rfd, task, task_cls);
1399 * Schedule a new task to be run with a specified priority and to be
1400 * run after the specified delay or when the specified file descriptor
1401 * is ready for reading. The delay can be used as a timeout on the
1402 * socket being ready. The task will be scheduled for execution once
1403 * either the delay has expired or the socket operation is ready. It
1404 * will be run with the DEFAULT priority.
1406 * @param delay when should this operation time out? Use
1407 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1408 * @param priority priority to use for the task
1409 * @param rfd read file-descriptor
1410 * @param task main function of the task
1411 * @param task_cls closure of @a task
1412 * @return unique task identifier for the job
1413 * only valid until @a task is started!
1415 GNUNET_SCHEDULER_TaskIdentifier
1416 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1417 enum GNUNET_SCHEDULER_Priority priority,
1418 struct GNUNET_NETWORK_Handle *rfd,
1419 GNUNET_SCHEDULER_Task task, void *task_cls)
1421 return GNUNET_SCHEDULER_add_net_with_priority (
1423 rfd, GNUNET_YES, GNUNET_NO,
1429 * Schedule a new task to be run with a specified delay or when the
1430 * specified file descriptor is ready for writing. The delay can be
1431 * used as a timeout on the socket being ready. The task will be
1432 * scheduled for execution once either the delay has expired or the
1433 * socket operation is ready. It will be run with the priority of
1436 * @param delay when should this operation time out? Use
1437 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1438 * @param wfd write file-descriptor
1439 * @param task main function of the task
1440 * @param task_cls closure of @a task
1441 * @return unique task identifier for the job
1442 * only valid until @a task is started!
1444 GNUNET_SCHEDULER_TaskIdentifier
1445 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1446 struct GNUNET_NETWORK_Handle *wfd,
1447 GNUNET_SCHEDULER_Task task, void *task_cls)
1449 return GNUNET_SCHEDULER_add_net_with_priority (
1450 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1451 wfd, GNUNET_NO, GNUNET_YES,
1456 * Schedule a new task to be run with a specified delay or when the
1457 * specified file descriptor is ready. The delay can be
1458 * used as a timeout on the socket being ready. The task will be
1459 * scheduled for execution once either the delay has expired or the
1460 * socket operation is ready.
1462 * @param delay when should this operation time out? Use
1463 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1464 * @param priority priority of the task
1465 * @param fd file-descriptor
1466 * @param on_read whether to poll the file-descriptor for readability
1467 * @param on_write whether to poll the file-descriptor for writability
1468 * @param task main function of the task
1469 * @param task_cls closure of task
1470 * @return unique task identifier for the job
1471 * only valid until "task" is started!
1473 GNUNET_SCHEDULER_TaskIdentifier
1474 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1475 enum GNUNET_SCHEDULER_Priority priority,
1476 struct GNUNET_NETWORK_Handle *fd,
1477 int on_read, int on_write,
1478 GNUNET_SCHEDULER_Task task, void *task_cls)
1481 struct GNUNET_NETWORK_FDSet *s;
1482 GNUNET_SCHEDULER_TaskIdentifier ret;
1484 GNUNET_assert (fd != NULL);
1485 s = GNUNET_NETWORK_fdset_create ();
1486 GNUNET_NETWORK_fdset_set (s, fd);
1487 ret = GNUNET_SCHEDULER_add_select (
1490 on_write ? s : NULL,
1492 GNUNET_NETWORK_fdset_destroy (s);
1495 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1496 return add_without_sets (
1498 on_read ? GNUNET_NETWORK_get_fd (fd) : -1,
1499 on_write ? GNUNET_NETWORK_get_fd (fd) : -1,
1506 * Schedule a new task to be run with a specified delay or when the
1507 * specified file descriptor is ready for reading. The delay can be
1508 * used as a timeout on the socket being ready. The task will be
1509 * scheduled for execution once either the delay has expired or the
1510 * socket operation is ready. It will be run with the DEFAULT priority.
1512 * @param delay when should this operation time out? Use
1513 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1514 * @param rfd read file-descriptor
1515 * @param task main function of the task
1516 * @param task_cls closure of @a task
1517 * @return unique task identifier for the job
1518 * only valid until @a task is started!
1520 GNUNET_SCHEDULER_TaskIdentifier
1521 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1522 const struct GNUNET_DISK_FileHandle *rfd,
1523 GNUNET_SCHEDULER_Task task, void *task_cls)
1525 return GNUNET_SCHEDULER_add_file_with_priority (
1526 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1527 rfd, GNUNET_YES, GNUNET_NO,
1533 * Schedule a new task to be run with a specified delay or when the
1534 * specified file descriptor is ready for writing. The delay can be
1535 * used as a timeout on the socket being ready. The task will be
1536 * scheduled for execution once either the delay has expired or the
1537 * socket operation is ready. It will be run with the DEFAULT priority.
1539 * @param delay when should this operation time out? Use
1540 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1541 * @param wfd write file-descriptor
1542 * @param task main function of the task
1543 * @param task_cls closure of @a task
1544 * @return unique task identifier for the job
1545 * only valid until @a task is started!
1547 GNUNET_SCHEDULER_TaskIdentifier
1548 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1549 const struct GNUNET_DISK_FileHandle *wfd,
1550 GNUNET_SCHEDULER_Task task, void *task_cls)
1552 return GNUNET_SCHEDULER_add_file_with_priority (
1553 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1554 wfd, GNUNET_NO, GNUNET_YES,
1560 * Schedule a new task to be run with a specified delay or when the
1561 * specified file descriptor is ready. The delay can be
1562 * used as a timeout on the socket being ready. The task will be
1563 * scheduled for execution once either the delay has expired or the
1564 * socket operation is ready.
1566 * @param delay when should this operation time out? Use
1567 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1568 * @param priority priority of the task
1569 * @param fd file-descriptor
1570 * @param on_read whether to poll the file-descriptor for readability
1571 * @param on_write whether to poll the file-descriptor for writability
1572 * @param task main function of the task
1573 * @param task_cls closure of @a task
1574 * @return unique task identifier for the job
1575 * only valid until @a task is started!
1577 GNUNET_SCHEDULER_TaskIdentifier
1578 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1579 enum GNUNET_SCHEDULER_Priority priority,
1580 const struct GNUNET_DISK_FileHandle *fd,
1581 int on_read, int on_write,
1582 GNUNET_SCHEDULER_Task task, void *task_cls)
1585 struct GNUNET_NETWORK_FDSet *s;
1586 GNUNET_SCHEDULER_TaskIdentifier ret;
1588 GNUNET_assert (fd != NULL);
1589 s = GNUNET_NETWORK_fdset_create ();
1590 GNUNET_NETWORK_fdset_handle_set (s, fd);
1591 ret = GNUNET_SCHEDULER_add_select (
1594 on_write ? s : NULL,
1596 GNUNET_NETWORK_fdset_destroy (s);
1601 GNUNET_DISK_internal_file_handle_ (fd, &real_fd, sizeof (int));
1602 GNUNET_assert (real_fd >= 0);
1603 return add_without_sets (
1605 on_read ? real_fd : -1,
1606 on_write ? real_fd : -1,
1613 * Schedule a new task to be run with a specified delay or when any of
1614 * the specified file descriptor sets is ready. The delay can be used
1615 * as a timeout on the socket(s) being ready. The task will be
1616 * scheduled for execution once either the delay has expired or any of
1617 * the socket operations is ready. This is the most general
1618 * function of the "add" family. Note that the "prerequisite_task"
1619 * must be satisfied in addition to any of the other conditions. In
1620 * other words, the task will be started when
1622 * (prerequisite-run)
1626 * || (shutdown-active && run-on-shutdown) )
1629 * @param prio how important is this task?
1630 * @param delay how long should we wait? Use #GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1631 * which means that the task will only be run after we receive SIGTERM
1632 * @param rs set of file descriptors we want to read (can be NULL)
1633 * @param ws set of file descriptors we want to write (can be NULL)
1634 * @param task main function of the task
1635 * @param task_cls closure of @a task
1636 * @return unique task identifier for the job
1637 * only valid until @a task is started!
1639 GNUNET_SCHEDULER_TaskIdentifier
1640 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1641 struct GNUNET_TIME_Relative delay,
1642 const struct GNUNET_NETWORK_FDSet *rs,
1643 const struct GNUNET_NETWORK_FDSet *ws,
1644 GNUNET_SCHEDULER_Task task, void *task_cls)
1648 void *backtrace_array[MAX_TRACE_DEPTH];
1651 GNUNET_assert (NULL != active_task);
1652 GNUNET_assert (NULL != task);
1653 t = GNUNET_new (struct Task);
1655 t->callback_cls = task_cls;
1657 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1658 t->backtrace_strings =
1659 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1665 t->read_set = GNUNET_NETWORK_fdset_create ();
1666 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1670 t->write_set = GNUNET_NETWORK_fdset_create ();
1671 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1675 t->start_time = GNUNET_TIME_absolute_get ();
1677 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1679 check_priority ((prio ==
1680 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1682 t->lifeness = current_lifeness;
1685 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1686 LOG (GNUNET_ERROR_TYPE_DEBUG,
1687 "Adding task: %llu / %p\n",
1693 for (i = 0; i < t->num_backtrace_strings; i++)
1694 LOG (GNUNET_ERROR_TYPE_DEBUG,
1695 "Task %llu trace %d: %s\n",
1697 t->backtrace_strings[i]);
1702 /* end of scheduler.c */