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 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 (pos->read_fd != -1)
314 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
315 if (pos->write_fd != -1)
316 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
317 if (pos->read_set != NULL)
318 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
319 if (pos->write_set != NULL)
320 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
321 if (pos->reason != 0)
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, struct GNUNET_NETWORK_FDSet *ws)
543 enum GNUNET_SCHEDULER_Priority p;
545 struct GNUNET_SCHEDULER_TaskContext tc;
547 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
550 if (ready_count == 0)
552 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
553 /* yes, p>0 is correct, 0 is "KEEP" which should
554 * always be an empty queue (see assertion)! */
555 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
561 GNUNET_assert (pos != NULL); /* ready_count wrong? */
562 ready[p] = pos->next;
564 current_priority = pos->priority;
565 current_lifeness = pos->lifeness;
568 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
569 DELAY_THRESHOLD.rel_value_us)
571 LOG (GNUNET_ERROR_TYPE_DEBUG,
572 "Task %llu took %s to be scheduled\n",
573 (unsigned long long) pos->id,
574 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time), GNUNET_YES));
577 tc.reason = pos->reason;
578 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
579 if ((pos->read_fd != -1) &&
580 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)))
581 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
582 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
583 if ((pos->write_fd != -1) &&
584 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
585 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
586 if (((tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
587 (pos->write_fd != -1) &&
588 (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd)))
589 GNUNET_abort (); // added to ready in previous select loop!
590 LOG (GNUNET_ERROR_TYPE_DEBUG,
591 "Running task: %llu / %p\n", pos->id,
593 pos->callback (pos->callback_cls, &tc);
597 for (i = 0; i < pos->num_backtrace_strings; i++)
598 LOG (GNUNET_ERROR_TYPE_ERROR, "Task %llu trace %d: %s\n", pos->id, i,
599 pos->backtrace_strings[i]);
605 while ((pending == NULL) || (p >= max_priority_added));
609 * Pipe used to communicate shutdown via signal.
611 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
614 * Process ID of this process at the time we installed the various
620 * Signal handler called for SIGPIPE.
632 * Wait for a short time.
633 * Sleeps for @a ms ms (as that should be long enough for virtually all
634 * modern systems to context switch and allow another process to do
637 * @param ms how many ms to wait
640 short_wait (unsigned int ms)
642 struct GNUNET_TIME_Relative timeout;
644 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
645 (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
650 * Signal handler called for signals that should cause us to shutdown.
653 sighandler_shutdown ()
656 int old_errno = errno; /* backup errno */
658 if (getpid () != my_pid)
659 exit (1); /* we have fork'ed since the signal handler was created,
660 * ignore the signal, see https://gnunet.org/vfork discussion */
661 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
662 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
669 * Check if the system is still life. Trigger shutdown if we
670 * have tasks, but none of them give us lifeness.
672 * @return #GNUNET_OK to continue the main loop,
682 for (t = pending; NULL != t; t = t->next)
683 if (t->lifeness == GNUNET_YES)
685 for (t = pending_timeout; NULL != t; t = t->next)
686 if (t->lifeness == GNUNET_YES)
688 if ((NULL != pending) || (NULL != pending_timeout))
690 GNUNET_SCHEDULER_shutdown ();
698 * Initialize and run scheduler. This function will return when all
699 * tasks have completed. On systems with signals, receiving a SIGTERM
700 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
701 * to be run after the active task is complete. As a result, SIGTERM
702 * causes all active tasks to be scheduled with reason
703 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
704 * afterwards will execute normally!). Note that any particular signal
705 * will only shut down one scheduler; applications should always only
706 * create a single scheduler.
708 * @param task task to run immediately
709 * @param task_cls closure of task
712 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
714 struct GNUNET_NETWORK_FDSet *rs;
715 struct GNUNET_NETWORK_FDSet *ws;
716 struct GNUNET_TIME_Relative timeout;
718 struct GNUNET_SIGNAL_Context *shc_int;
719 struct GNUNET_SIGNAL_Context *shc_term;
722 struct GNUNET_SIGNAL_Context *shc_quit;
723 struct GNUNET_SIGNAL_Context *shc_hup;
724 struct GNUNET_SIGNAL_Context *shc_pipe;
726 unsigned long long last_tr;
727 unsigned int busy_wait_warning;
728 const struct GNUNET_DISK_FileHandle *pr;
731 GNUNET_assert (active_task == NULL);
732 rs = GNUNET_NETWORK_fdset_create ();
733 ws = GNUNET_NETWORK_fdset_create ();
734 GNUNET_assert (shutdown_pipe_handle == NULL);
735 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
736 GNUNET_assert (shutdown_pipe_handle != NULL);
737 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
738 GNUNET_DISK_PIPE_END_READ);
739 GNUNET_assert (pr != NULL);
741 LOG (GNUNET_ERROR_TYPE_DEBUG, "Registering signal handlers\n");
742 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
743 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
745 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
746 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
747 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
749 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
750 current_lifeness = GNUNET_YES;
751 GNUNET_SCHEDULER_add_continuation (task, task_cls,
752 GNUNET_SCHEDULER_REASON_STARTUP);
753 active_task = (void *) (long) -1; /* force passing of sanity check */
754 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
755 &GNUNET_OS_install_parent_control_handler,
759 busy_wait_warning = 0;
760 while (GNUNET_OK == check_lifeness ())
762 GNUNET_NETWORK_fdset_zero (rs);
763 GNUNET_NETWORK_fdset_zero (ws);
764 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
765 update_sets (rs, ws, &timeout);
766 GNUNET_NETWORK_fdset_handle_set (rs, pr);
769 /* no blocking, more work already ready! */
770 timeout = GNUNET_TIME_UNIT_ZERO;
772 if (NULL == scheduler_select)
773 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
775 ret = scheduler_select (scheduler_select_cls, rs, ws, NULL, timeout);
776 if (ret == GNUNET_SYSERR)
781 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
786 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
789 if (0 != system (lsof))
790 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "system");
796 if ((0 == ret) && (0 == timeout.rel_value_us) && (busy_wait_warning > 16))
798 LOG (GNUNET_ERROR_TYPE_WARNING, _("Looks like we're busy waiting...\n"));
799 short_wait (100); /* mitigate */
801 check_ready (rs, ws);
803 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
805 /* consume the signal */
806 GNUNET_DISK_file_read (pr, &c, sizeof (c));
807 /* mark all active tasks as ready due to shutdown */
808 GNUNET_SCHEDULER_shutdown ();
810 if (last_tr == tasks_run)
818 busy_wait_warning = 0;
821 GNUNET_SIGNAL_handler_uninstall (shc_int);
822 GNUNET_SIGNAL_handler_uninstall (shc_term);
824 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
825 GNUNET_SIGNAL_handler_uninstall (shc_quit);
826 GNUNET_SIGNAL_handler_uninstall (shc_hup);
828 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
829 shutdown_pipe_handle = NULL;
830 GNUNET_NETWORK_fdset_destroy (rs);
831 GNUNET_NETWORK_fdset_destroy (ws);
836 * Obtain the reason code for why the current task was
837 * started. Will return the same value as
838 * the GNUNET_SCHEDULER_TaskContext's reason field.
840 * @return reason(s) why the current task is run
842 enum GNUNET_SCHEDULER_Reason
843 GNUNET_SCHEDULER_get_reason ()
845 GNUNET_assert (active_task != NULL);
846 return active_task->reason;
851 * Get information about the current load of this scheduler. Use this
852 * function to determine if an elective task should be added or simply
853 * dropped (if the decision should be made based on the number of
854 * tasks ready to run).
856 * @param p priority level to look at
857 * @return number of tasks pending right now
860 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
865 GNUNET_assert (active_task != NULL);
866 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
868 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
869 p = current_priority;
871 pos = ready[check_priority (p)];
882 * Cancel the task with the specified identifier.
883 * The task must not yet have run.
885 * @param task id of the task to cancel
886 * @return original closure of the task
889 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
893 enum GNUNET_SCHEDULER_Priority p;
897 GNUNET_assert (active_task != NULL);
920 if (pending_timeout_last == t)
921 pending_timeout_last = NULL;
927 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT)
929 LOG (GNUNET_ERROR_TYPE_ERROR, _("Attempt to cancel dead task %llu!\n"),
930 (unsigned long long) task);
956 pending_timeout = t->next;
966 prev->next = t->next;
968 ret = t->callback_cls;
969 LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling task: %llu / %p\n", task,
977 * Continue the current execution with the given function. This is
978 * similar to the other "add" functions except that there is no delay
979 * and the reason code can be specified.
981 * @param task main function of the task
982 * @param task_cls closure for 'main'
983 * @param reason reason for task invocation
984 * @param priority priority to use for the task
987 GNUNET_SCHEDULER_add_continuation_with_priority (GNUNET_SCHEDULER_Task task, void *task_cls,
988 enum GNUNET_SCHEDULER_Reason reason,
989 enum GNUNET_SCHEDULER_Priority priority)
994 void *backtrace_array[50];
997 GNUNET_assert (NULL != task);
998 GNUNET_assert ((active_task != NULL) ||
999 (reason == GNUNET_SCHEDULER_REASON_STARTUP));
1000 t = GNUNET_malloc (sizeof (struct Task));
1002 t->num_backtrace_strings = backtrace (backtrace_array, 50);
1003 t->backtrace_strings =
1004 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1009 t->callback_cls = task_cls;
1012 t->start_time = GNUNET_TIME_absolute_get ();
1015 t->priority = priority;
1016 t->lifeness = current_lifeness;
1017 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding continuation task: %llu / %p\n", t->id,
1019 queue_ready_task (t);
1024 * Continue the current execution with the given function. This is
1025 * similar to the other "add" functions except that there is no delay
1026 * and the reason code can be specified.
1028 * @param task main function of the task
1029 * @param task_cls closure for 'main'
1030 * @param reason reason for task invocation
1033 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls,
1034 enum GNUNET_SCHEDULER_Reason reason)
1036 GNUNET_SCHEDULER_add_continuation_with_priority (task, task_cls,
1038 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
1043 * Schedule a new task to be run with a specified priority.
1045 * @param prio how important is the new task?
1046 * @param task main function of the task
1047 * @param task_cls closure of task
1048 * @return unique task identifier for the job
1049 * only valid until "task" is started!
1051 GNUNET_SCHEDULER_TaskIdentifier
1052 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1053 GNUNET_SCHEDULER_Task task, void *task_cls)
1055 return GNUNET_SCHEDULER_add_select (prio,
1056 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1063 * Schedule a new task to be run with a specified delay. The task
1064 * will be scheduled for execution once the delay has expired.
1066 * @param delay when should this operation time out? Use
1067 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1068 * @param priority priority to use for the task
1069 * @param task main function of the task
1070 * @param task_cls closure of task
1071 * @return unique task identifier for the job
1072 * only valid until "task" is started!
1074 GNUNET_SCHEDULER_TaskIdentifier
1075 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1076 enum GNUNET_SCHEDULER_Priority priority,
1077 GNUNET_SCHEDULER_Task task, void *task_cls)
1084 void *backtrace_array[MAX_TRACE_DEPTH];
1087 GNUNET_assert (active_task != NULL);
1088 GNUNET_assert (NULL != task);
1089 t = GNUNET_malloc (sizeof (struct Task));
1091 t->callback_cls = task_cls;
1093 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1094 t->backtrace_strings =
1095 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1101 t->start_time = GNUNET_TIME_absolute_get ();
1103 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1104 t->priority = priority;
1105 t->lifeness = current_lifeness;
1106 /* try tail first (optimization in case we are
1107 * appending to a long list of tasks with timeouts) */
1108 prev = pending_timeout_last;
1111 if (prev->timeout.abs_value_us > t->timeout.abs_value_us)
1114 pos = prev->next; /* heuristic success! */
1118 /* heuristic failed, do traversal of timeout list */
1119 pos = pending_timeout;
1121 while ((pos != NULL) &&
1122 ((pos->timeout.abs_value_us <= t->timeout.abs_value_us) ||
1123 (0 != pos->reason)))
1129 pending_timeout = t;
1133 /* hyper-optimization... */
1134 pending_timeout_last = t;
1136 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1141 for (i = 0; i < t->num_backtrace_strings; i++)
1142 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1143 t->backtrace_strings[i]);
1150 * Schedule a new task to be run with a specified delay. The task
1151 * will be scheduled for execution once the delay has expired. It
1152 * will be run with the DEFAULT priority.
1154 * @param delay when should this operation time out? Use
1155 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1156 * @param task main function of the task
1157 * @param task_cls closure of task
1158 * @return unique task identifier for the job
1159 * only valid until "task" is started!
1161 GNUNET_SCHEDULER_TaskIdentifier
1162 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1163 GNUNET_SCHEDULER_Task task, void *task_cls)
1165 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1166 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1172 * Schedule a new task to be run as soon as possible. Note that this
1173 * does not guarantee that this will be the next task that is being
1174 * run, as other tasks with higher priority (or that are already ready
1175 * to run) might get to run first. Just as with delays, clients must
1176 * not rely on any particular order of execution between tasks
1177 * scheduled concurrently.
1179 * The task will be run with the DEFAULT priority.
1181 * @param task main function of the task
1182 * @param task_cls closure of @a task
1183 * @return unique task identifier for the job
1184 * only valid until "task" is started!
1186 GNUNET_SCHEDULER_TaskIdentifier
1187 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task, void *task_cls)
1189 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO, task, task_cls);
1194 * Schedule a new task to be run as soon as possible with the
1195 * (transitive) ignore-shutdown flag either explicitly set or
1196 * explicitly enabled. This task (and all tasks created from it,
1197 * other than by another call to this function) will either count or
1198 * not count for the 'lifeness' of the process. This API is only
1199 * useful in a few special cases.
1201 * @param lifeness GNUNET_YES if the task counts for lifeness, GNUNET_NO if not.
1202 * @param task main function of the task
1203 * @param task_cls closure of task
1204 * @return unique task identifier for the job
1205 * only valid until "task" is started!
1207 GNUNET_SCHEDULER_TaskIdentifier
1208 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1209 GNUNET_SCHEDULER_Task task,
1212 GNUNET_SCHEDULER_TaskIdentifier ret;
1215 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1216 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1218 GNUNET_assert (pending->id == ret);
1219 pending->lifeness = lifeness;
1225 * Schedule a new task to be run with a specified delay or when any of
1226 * the specified file descriptor sets is ready. The delay can be used
1227 * as a timeout on the socket(s) being ready. The task will be
1228 * scheduled for execution once either the delay has expired or any of
1229 * the socket operations is ready. This is the most general
1230 * function of the "add" family. Note that the "prerequisite_task"
1231 * must be satisfied in addition to any of the other conditions. In
1232 * other words, the task will be started when
1234 * (prerequisite-run)
1238 * || shutdown-active )
1241 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1242 * which means that the task will only be run after we receive SIGTERM
1243 * @param priority priority to use
1244 * @param rfd file descriptor we want to read (can be -1)
1245 * @param wfd file descriptors we want to write (can be -1)
1246 * @param task main function of the task
1247 * @param task_cls closure of task
1248 * @return unique task identifier for the job
1249 * only valid until "task" is started!
1252 static GNUNET_SCHEDULER_TaskIdentifier
1253 add_without_sets (struct GNUNET_TIME_Relative delay,
1254 enum GNUNET_SCHEDULER_Priority priority,
1256 GNUNET_SCHEDULER_Task task, void *task_cls)
1261 void *backtrace_array[MAX_TRACE_DEPTH];
1264 GNUNET_assert (active_task != NULL);
1265 GNUNET_assert (NULL != task);
1266 t = GNUNET_malloc (sizeof (struct Task));
1268 t->callback_cls = task_cls;
1270 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1271 t->backtrace_strings =
1272 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1277 int flags = fcntl (rfd, F_GETFD);
1279 if ((flags == -1) && (errno == EBADF))
1281 LOG (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", rfd);
1285 for (i = 0; i < t->num_backtrace_strings; i++)
1286 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n", t->backtrace_strings[i]);
1293 int flags = fcntl (wfd, F_GETFD);
1295 if (flags == -1 && errno == EBADF)
1297 LOG (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", wfd);
1301 for (i = 0; i < t->num_backtrace_strings; i++)
1302 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n", t->backtrace_strings[i]);
1309 GNUNET_assert (wfd >= -1);
1313 t->start_time = GNUNET_TIME_absolute_get ();
1315 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1316 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1317 t->lifeness = current_lifeness;
1320 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1321 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1326 for (i = 0; i < t->num_backtrace_strings; i++)
1327 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1328 t->backtrace_strings[i]);
1337 * Schedule a new task to be run with a specified delay or when the
1338 * specified file descriptor is ready for reading. The delay can be
1339 * used as a timeout on the socket being ready. The task will be
1340 * scheduled for execution once either the delay has expired or the
1341 * socket operation is ready. It will be run with the DEFAULT priority.
1343 * @param delay when should this operation time out? Use
1344 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1345 * @param rfd read file-descriptor
1346 * @param task main function of the task
1347 * @param task_cls closure of task
1348 * @return unique task identifier for the job
1349 * only valid until "task" is started!
1351 GNUNET_SCHEDULER_TaskIdentifier
1352 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1353 struct GNUNET_NETWORK_Handle *rfd,
1354 GNUNET_SCHEDULER_Task task, void *task_cls)
1356 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1357 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1358 rfd, task, task_cls);
1363 * Schedule a new task to be run with a specified priority and to be
1364 * run after the specified delay or when the specified file descriptor
1365 * is ready for reading. The delay can be used as a timeout on the
1366 * socket being ready. The task will be scheduled for execution once
1367 * either the delay has expired or the socket operation is ready. It
1368 * will be run with the DEFAULT priority.
1370 * @param delay when should this operation time out? Use
1371 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1372 * @param priority priority to use for the task
1373 * @param rfd read file-descriptor
1374 * @param task main function of the task
1375 * @param task_cls closure of task
1376 * @return unique task identifier for the job
1377 * only valid until "task" is started!
1379 GNUNET_SCHEDULER_TaskIdentifier
1380 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1381 enum GNUNET_SCHEDULER_Priority priority,
1382 struct GNUNET_NETWORK_Handle *rfd,
1383 GNUNET_SCHEDULER_Task task, void *task_cls)
1386 struct GNUNET_NETWORK_FDSet *rs;
1387 GNUNET_SCHEDULER_TaskIdentifier ret;
1389 GNUNET_assert (rfd != NULL);
1390 rs = GNUNET_NETWORK_fdset_create ();
1391 GNUNET_NETWORK_fdset_set (rs, rfd);
1393 GNUNET_SCHEDULER_add_select (priority,
1396 GNUNET_NETWORK_fdset_destroy (rs);
1399 return add_without_sets (delay,
1401 GNUNET_NETWORK_get_fd (rfd), -1, task,
1409 * Schedule a new task to be run with a specified delay or when the
1410 * specified file descriptor is ready for writing. The delay can be
1411 * used as a timeout on the socket being ready. The task will be
1412 * scheduled for execution once either the delay has expired or the
1413 * socket operation is ready. It will be run with the priority of
1416 * @param delay when should this operation time out? Use
1417 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1418 * @param wfd write file-descriptor
1419 * @param task main function of the task
1420 * @param task_cls closure of task
1421 * @return unique task identifier for the job
1422 * only valid until "task" is started!
1424 GNUNET_SCHEDULER_TaskIdentifier
1425 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1426 struct GNUNET_NETWORK_Handle *wfd,
1427 GNUNET_SCHEDULER_Task task, void *task_cls)
1430 struct GNUNET_NETWORK_FDSet *ws;
1431 GNUNET_SCHEDULER_TaskIdentifier ret;
1433 GNUNET_assert (wfd != NULL);
1434 ws = GNUNET_NETWORK_fdset_create ();
1435 GNUNET_NETWORK_fdset_set (ws, wfd);
1437 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1440 GNUNET_NETWORK_fdset_destroy (ws);
1443 GNUNET_assert (GNUNET_NETWORK_get_fd (wfd) >= 0);
1444 return add_without_sets (delay,
1445 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1446 -1, GNUNET_NETWORK_get_fd (wfd), task,
1453 * Schedule a new task to be run with a specified delay or when the
1454 * specified file descriptor is ready for reading. The delay can be
1455 * used as a timeout on the socket being ready. The task will be
1456 * scheduled for execution once either the delay has expired or the
1457 * socket operation is ready. It will be run with the DEFAULT priority.
1459 * @param delay when should this operation time out? Use
1460 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1461 * @param rfd read file-descriptor
1462 * @param task main function of the task
1463 * @param task_cls closure of task
1464 * @return unique task identifier for the job
1465 * only valid until "task" is started!
1467 GNUNET_SCHEDULER_TaskIdentifier
1468 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1469 const struct GNUNET_DISK_FileHandle *rfd,
1470 GNUNET_SCHEDULER_Task task, void *task_cls)
1473 struct GNUNET_NETWORK_FDSet *rs;
1474 GNUNET_SCHEDULER_TaskIdentifier ret;
1476 GNUNET_assert (rfd != NULL);
1477 rs = GNUNET_NETWORK_fdset_create ();
1478 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1480 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1483 GNUNET_NETWORK_fdset_destroy (rs);
1488 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1489 return add_without_sets (delay,
1490 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1491 fd, -1, task, task_cls);
1498 * Schedule a new task to be run with a specified delay or when the
1499 * specified file descriptor is ready for writing. The delay can be
1500 * used as a timeout on the socket being ready. The task will be
1501 * scheduled for execution once either the delay has expired or the
1502 * socket operation is ready. It will be run with the DEFAULT priority.
1504 * @param delay when should this operation time out? Use
1505 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1506 * @param wfd write file-descriptor
1507 * @param task main function of the task
1508 * @param task_cls closure of task
1509 * @return unique task identifier for the job
1510 * only valid until "task" is started!
1512 GNUNET_SCHEDULER_TaskIdentifier
1513 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1514 const struct GNUNET_DISK_FileHandle *wfd,
1515 GNUNET_SCHEDULER_Task task, void *task_cls)
1518 struct GNUNET_NETWORK_FDSet *ws;
1519 GNUNET_SCHEDULER_TaskIdentifier ret;
1521 GNUNET_assert (wfd != NULL);
1522 ws = GNUNET_NETWORK_fdset_create ();
1523 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1525 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1528 GNUNET_NETWORK_fdset_destroy (ws);
1533 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1534 GNUNET_assert (fd >= 0);
1535 return add_without_sets (delay,
1536 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1537 -1, fd, task, task_cls);
1545 * Schedule a new task to be run with a specified delay or when any of
1546 * the specified file descriptor sets is ready. The delay can be used
1547 * as a timeout on the socket(s) being ready. The task will be
1548 * scheduled for execution once either the delay has expired or any of
1549 * the socket operations is ready. This is the most general
1550 * function of the "add" family. Note that the "prerequisite_task"
1551 * must be satisfied in addition to any of the other conditions. In
1552 * other words, the task will be started when
1554 * (prerequisite-run)
1558 * || (shutdown-active && run-on-shutdown) )
1561 * @param prio how important is this task?
1562 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1563 * which means that the task will only be run after we receive SIGTERM
1564 * @param rs set of file descriptors we want to read (can be NULL)
1565 * @param ws set of file descriptors we want to write (can be NULL)
1566 * @param task main function of the task
1567 * @param task_cls closure of task
1568 * @return unique task identifier for the job
1569 * only valid until "task" is started!
1571 GNUNET_SCHEDULER_TaskIdentifier
1572 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1573 struct GNUNET_TIME_Relative delay,
1574 const struct GNUNET_NETWORK_FDSet *rs,
1575 const struct GNUNET_NETWORK_FDSet *ws,
1576 GNUNET_SCHEDULER_Task task, void *task_cls)
1581 void *backtrace_array[MAX_TRACE_DEPTH];
1584 GNUNET_assert (NULL != active_task);
1585 GNUNET_assert (NULL != task);
1586 t = GNUNET_malloc (sizeof (struct Task));
1588 t->callback_cls = task_cls;
1590 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1591 t->backtrace_strings =
1592 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1598 t->read_set = GNUNET_NETWORK_fdset_create ();
1599 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1603 t->write_set = GNUNET_NETWORK_fdset_create ();
1604 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1608 t->start_time = GNUNET_TIME_absolute_get ();
1610 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1612 check_priority ((prio ==
1613 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1615 t->lifeness = current_lifeness;
1618 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1619 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1624 for (i = 0; i < t->num_backtrace_strings; i++)
1625 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1626 t->backtrace_strings[i]);
1631 /* end of scheduler.c */