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;
720 #if (SIGTERM != GNUNET_TERM_SIG)
721 struct GNUNET_SIGNAL_Context *shc_gterm;
725 struct GNUNET_SIGNAL_Context *shc_quit;
726 struct GNUNET_SIGNAL_Context *shc_hup;
727 struct GNUNET_SIGNAL_Context *shc_pipe;
729 unsigned long long last_tr;
730 unsigned int busy_wait_warning;
731 const struct GNUNET_DISK_FileHandle *pr;
734 GNUNET_assert (active_task == NULL);
735 rs = GNUNET_NETWORK_fdset_create ();
736 ws = GNUNET_NETWORK_fdset_create ();
737 GNUNET_assert (shutdown_pipe_handle == NULL);
738 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
739 GNUNET_assert (shutdown_pipe_handle != NULL);
740 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
741 GNUNET_DISK_PIPE_END_READ);
742 GNUNET_assert (pr != NULL);
744 LOG (GNUNET_ERROR_TYPE_DEBUG, "Registering signal handlers\n");
745 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
746 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
747 #if (SIGTERM != GNUNET_TERM_SIG)
748 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG, &sighandler_shutdown);
751 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
752 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
753 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
755 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
756 current_lifeness = GNUNET_YES;
757 GNUNET_SCHEDULER_add_continuation (task, task_cls,
758 GNUNET_SCHEDULER_REASON_STARTUP);
759 active_task = (void *) (long) -1; /* force passing of sanity check */
760 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
761 &GNUNET_OS_install_parent_control_handler,
765 busy_wait_warning = 0;
766 while (GNUNET_OK == check_lifeness ())
768 GNUNET_NETWORK_fdset_zero (rs);
769 GNUNET_NETWORK_fdset_zero (ws);
770 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
771 update_sets (rs, ws, &timeout);
772 GNUNET_NETWORK_fdset_handle_set (rs, pr);
775 /* no blocking, more work already ready! */
776 timeout = GNUNET_TIME_UNIT_ZERO;
778 if (NULL == scheduler_select)
779 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
781 ret = scheduler_select (scheduler_select_cls, rs, ws, NULL, timeout);
782 if (ret == GNUNET_SYSERR)
787 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
792 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
795 if (0 != system (lsof))
796 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "system");
802 if ((0 == ret) && (0 == timeout.rel_value_us) && (busy_wait_warning > 16))
804 LOG (GNUNET_ERROR_TYPE_WARNING, _("Looks like we're busy waiting...\n"));
805 short_wait (100); /* mitigate */
807 check_ready (rs, ws);
809 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
811 /* consume the signal */
812 GNUNET_DISK_file_read (pr, &c, sizeof (c));
813 /* mark all active tasks as ready due to shutdown */
814 GNUNET_SCHEDULER_shutdown ();
816 if (last_tr == tasks_run)
824 busy_wait_warning = 0;
827 GNUNET_SIGNAL_handler_uninstall (shc_int);
828 GNUNET_SIGNAL_handler_uninstall (shc_term);
829 #if (SIGTERM != GNUNET_TERM_SIG)
830 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
833 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
834 GNUNET_SIGNAL_handler_uninstall (shc_quit);
835 GNUNET_SIGNAL_handler_uninstall (shc_hup);
837 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
838 shutdown_pipe_handle = NULL;
839 GNUNET_NETWORK_fdset_destroy (rs);
840 GNUNET_NETWORK_fdset_destroy (ws);
845 * Obtain the reason code for why the current task was
846 * started. Will return the same value as
847 * the GNUNET_SCHEDULER_TaskContext's reason field.
849 * @return reason(s) why the current task is run
851 enum GNUNET_SCHEDULER_Reason
852 GNUNET_SCHEDULER_get_reason ()
854 GNUNET_assert (active_task != NULL);
855 return active_task->reason;
860 * Get information about the current load of this scheduler. Use this
861 * function to determine if an elective task should be added or simply
862 * dropped (if the decision should be made based on the number of
863 * tasks ready to run).
865 * @param p priority level to look at
866 * @return number of tasks pending right now
869 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
874 GNUNET_assert (active_task != NULL);
875 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
877 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
878 p = current_priority;
880 pos = ready[check_priority (p)];
891 * Cancel the task with the specified identifier.
892 * The task must not yet have run.
894 * @param task id of the task to cancel
895 * @return original closure of the task
898 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
902 enum GNUNET_SCHEDULER_Priority p;
906 GNUNET_assert (active_task != NULL);
929 if (pending_timeout_last == t)
930 pending_timeout_last = NULL;
936 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT)
938 LOG (GNUNET_ERROR_TYPE_ERROR, _("Attempt to cancel dead task %llu!\n"),
939 (unsigned long long) task);
965 pending_timeout = t->next;
975 prev->next = t->next;
977 ret = t->callback_cls;
978 LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling task: %llu / %p\n", task,
986 * Continue the current execution with the given function. This is
987 * similar to the other "add" functions except that there is no delay
988 * and the reason code can be specified.
990 * @param task main function of the task
991 * @param task_cls closure for 'main'
992 * @param reason reason for task invocation
993 * @param priority priority to use for the task
996 GNUNET_SCHEDULER_add_continuation_with_priority (GNUNET_SCHEDULER_Task task, void *task_cls,
997 enum GNUNET_SCHEDULER_Reason reason,
998 enum GNUNET_SCHEDULER_Priority priority)
1003 void *backtrace_array[50];
1006 GNUNET_assert (NULL != task);
1007 GNUNET_assert ((active_task != NULL) ||
1008 (reason == GNUNET_SCHEDULER_REASON_STARTUP));
1009 t = GNUNET_malloc (sizeof (struct Task));
1011 t->num_backtrace_strings = backtrace (backtrace_array, 50);
1012 t->backtrace_strings =
1013 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1018 t->callback_cls = task_cls;
1021 t->start_time = GNUNET_TIME_absolute_get ();
1024 t->priority = priority;
1025 t->lifeness = current_lifeness;
1026 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding continuation task: %llu / %p\n", t->id,
1028 queue_ready_task (t);
1033 * Continue the current execution with the given function. This is
1034 * similar to the other "add" functions except that there is no delay
1035 * and the reason code can be specified.
1037 * @param task main function of the task
1038 * @param task_cls closure for 'main'
1039 * @param reason reason for task invocation
1042 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls,
1043 enum GNUNET_SCHEDULER_Reason reason)
1045 GNUNET_SCHEDULER_add_continuation_with_priority (task, task_cls,
1047 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
1052 * Schedule a new task to be run with a specified priority.
1054 * @param prio how important is the new task?
1055 * @param task main function of the task
1056 * @param task_cls closure of task
1057 * @return unique task identifier for the job
1058 * only valid until "task" is started!
1060 GNUNET_SCHEDULER_TaskIdentifier
1061 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1062 GNUNET_SCHEDULER_Task task, void *task_cls)
1064 return GNUNET_SCHEDULER_add_select (prio,
1065 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1072 * Schedule a new task to be run with a specified delay. The task
1073 * will be scheduled for execution once the delay has expired.
1075 * @param delay when should this operation time out? Use
1076 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1077 * @param priority priority to use for the task
1078 * @param task main function of the task
1079 * @param task_cls closure of task
1080 * @return unique task identifier for the job
1081 * only valid until "task" is started!
1083 GNUNET_SCHEDULER_TaskIdentifier
1084 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1085 enum GNUNET_SCHEDULER_Priority priority,
1086 GNUNET_SCHEDULER_Task task, void *task_cls)
1093 void *backtrace_array[MAX_TRACE_DEPTH];
1096 GNUNET_assert (active_task != NULL);
1097 GNUNET_assert (NULL != task);
1098 t = GNUNET_malloc (sizeof (struct Task));
1100 t->callback_cls = task_cls;
1102 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1103 t->backtrace_strings =
1104 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1110 t->start_time = GNUNET_TIME_absolute_get ();
1112 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1113 t->priority = priority;
1114 t->lifeness = current_lifeness;
1115 /* try tail first (optimization in case we are
1116 * appending to a long list of tasks with timeouts) */
1117 prev = pending_timeout_last;
1120 if (prev->timeout.abs_value_us > t->timeout.abs_value_us)
1123 pos = prev->next; /* heuristic success! */
1127 /* heuristic failed, do traversal of timeout list */
1128 pos = pending_timeout;
1130 while ((pos != NULL) &&
1131 ((pos->timeout.abs_value_us <= t->timeout.abs_value_us) ||
1132 (0 != pos->reason)))
1138 pending_timeout = t;
1142 /* hyper-optimization... */
1143 pending_timeout_last = t;
1145 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1150 for (i = 0; i < t->num_backtrace_strings; i++)
1151 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1152 t->backtrace_strings[i]);
1159 * Schedule a new task to be run with a specified delay. The task
1160 * will be scheduled for execution once the delay has expired. It
1161 * will be run with the DEFAULT priority.
1163 * @param delay when should this operation time out? Use
1164 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1165 * @param task main function of the task
1166 * @param task_cls closure of task
1167 * @return unique task identifier for the job
1168 * only valid until "task" is started!
1170 GNUNET_SCHEDULER_TaskIdentifier
1171 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1172 GNUNET_SCHEDULER_Task task, void *task_cls)
1174 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1175 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1181 * Schedule a new task to be run as soon as possible. Note that this
1182 * does not guarantee that this will be the next task that is being
1183 * run, as other tasks with higher priority (or that are already ready
1184 * to run) might get to run first. Just as with delays, clients must
1185 * not rely on any particular order of execution between tasks
1186 * scheduled concurrently.
1188 * The task will be run with the DEFAULT priority.
1190 * @param task main function of the task
1191 * @param task_cls closure of @a task
1192 * @return unique task identifier for the job
1193 * only valid until "task" is started!
1195 GNUNET_SCHEDULER_TaskIdentifier
1196 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task, void *task_cls)
1198 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO, task, task_cls);
1203 * Schedule a new task to be run as soon as possible with the
1204 * (transitive) ignore-shutdown flag either explicitly set or
1205 * explicitly enabled. This task (and all tasks created from it,
1206 * other than by another call to this function) will either count or
1207 * not count for the 'lifeness' of the process. This API is only
1208 * useful in a few special cases.
1210 * @param lifeness GNUNET_YES if the task counts for lifeness, GNUNET_NO if not.
1211 * @param task main function of the task
1212 * @param task_cls closure of task
1213 * @return unique task identifier for the job
1214 * only valid until "task" is started!
1216 GNUNET_SCHEDULER_TaskIdentifier
1217 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1218 GNUNET_SCHEDULER_Task task,
1221 GNUNET_SCHEDULER_TaskIdentifier ret;
1224 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1225 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1227 GNUNET_assert (pending->id == ret);
1228 pending->lifeness = lifeness;
1234 * Schedule a new task to be run with a specified delay or when any of
1235 * the specified file descriptor sets is ready. The delay can be used
1236 * as a timeout on the socket(s) being ready. The task will be
1237 * scheduled for execution once either the delay has expired or any of
1238 * the socket operations is ready. This is the most general
1239 * function of the "add" family. Note that the "prerequisite_task"
1240 * must be satisfied in addition to any of the other conditions. In
1241 * other words, the task will be started when
1243 * (prerequisite-run)
1247 * || shutdown-active )
1250 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1251 * which means that the task will only be run after we receive SIGTERM
1252 * @param priority priority to use
1253 * @param rfd file descriptor we want to read (can be -1)
1254 * @param wfd file descriptors we want to write (can be -1)
1255 * @param task main function of the task
1256 * @param task_cls closure of task
1257 * @return unique task identifier for the job
1258 * only valid until "task" is started!
1261 static GNUNET_SCHEDULER_TaskIdentifier
1262 add_without_sets (struct GNUNET_TIME_Relative delay,
1263 enum GNUNET_SCHEDULER_Priority priority,
1265 GNUNET_SCHEDULER_Task task, void *task_cls)
1270 void *backtrace_array[MAX_TRACE_DEPTH];
1273 GNUNET_assert (active_task != NULL);
1274 GNUNET_assert (NULL != task);
1275 t = GNUNET_malloc (sizeof (struct Task));
1277 t->callback_cls = task_cls;
1279 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1280 t->backtrace_strings =
1281 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1286 int flags = fcntl (rfd, F_GETFD);
1288 if ((flags == -1) && (errno == EBADF))
1290 LOG (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", rfd);
1294 for (i = 0; i < t->num_backtrace_strings; i++)
1295 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n", t->backtrace_strings[i]);
1302 int flags = fcntl (wfd, F_GETFD);
1304 if (flags == -1 && errno == EBADF)
1306 LOG (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", wfd);
1310 for (i = 0; i < t->num_backtrace_strings; i++)
1311 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n", t->backtrace_strings[i]);
1318 GNUNET_assert (wfd >= -1);
1322 t->start_time = GNUNET_TIME_absolute_get ();
1324 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1325 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1326 t->lifeness = current_lifeness;
1329 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1330 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1335 for (i = 0; i < t->num_backtrace_strings; i++)
1336 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1337 t->backtrace_strings[i]);
1346 * Schedule a new task to be run with a specified delay or when the
1347 * specified file descriptor is ready for reading. The delay can be
1348 * used as a timeout on the socket being ready. The task will be
1349 * scheduled for execution once either the delay has expired or the
1350 * socket operation is ready. It will be run with the DEFAULT priority.
1352 * @param delay when should this operation time out? Use
1353 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1354 * @param rfd read file-descriptor
1355 * @param task main function of the task
1356 * @param task_cls closure of task
1357 * @return unique task identifier for the job
1358 * only valid until "task" is started!
1360 GNUNET_SCHEDULER_TaskIdentifier
1361 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1362 struct GNUNET_NETWORK_Handle *rfd,
1363 GNUNET_SCHEDULER_Task task, void *task_cls)
1365 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1366 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1367 rfd, task, task_cls);
1372 * Schedule a new task to be run with a specified priority and to be
1373 * run after the specified delay or when the specified file descriptor
1374 * is ready for reading. The delay can be used as a timeout on the
1375 * socket being ready. The task will be scheduled for execution once
1376 * either the delay has expired or the socket operation is ready. It
1377 * 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 priority priority to use for the task
1382 * @param rfd read file-descriptor
1383 * @param task main function of the task
1384 * @param task_cls closure of task
1385 * @return unique task identifier for the job
1386 * only valid until "task" is started!
1388 GNUNET_SCHEDULER_TaskIdentifier
1389 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1390 enum GNUNET_SCHEDULER_Priority priority,
1391 struct GNUNET_NETWORK_Handle *rfd,
1392 GNUNET_SCHEDULER_Task task, void *task_cls)
1395 struct GNUNET_NETWORK_FDSet *rs;
1396 GNUNET_SCHEDULER_TaskIdentifier ret;
1398 GNUNET_assert (rfd != NULL);
1399 rs = GNUNET_NETWORK_fdset_create ();
1400 GNUNET_NETWORK_fdset_set (rs, rfd);
1402 GNUNET_SCHEDULER_add_select (priority,
1405 GNUNET_NETWORK_fdset_destroy (rs);
1408 return add_without_sets (delay,
1410 GNUNET_NETWORK_get_fd (rfd), -1, task,
1418 * Schedule a new task to be run with a specified delay or when the
1419 * specified file descriptor is ready for writing. The delay can be
1420 * used as a timeout on the socket being ready. The task will be
1421 * scheduled for execution once either the delay has expired or the
1422 * socket operation is ready. It will be run with the priority of
1425 * @param delay when should this operation time out? Use
1426 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1427 * @param wfd write file-descriptor
1428 * @param task main function of the task
1429 * @param task_cls closure of task
1430 * @return unique task identifier for the job
1431 * only valid until "task" is started!
1433 GNUNET_SCHEDULER_TaskIdentifier
1434 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1435 struct GNUNET_NETWORK_Handle *wfd,
1436 GNUNET_SCHEDULER_Task task, void *task_cls)
1439 struct GNUNET_NETWORK_FDSet *ws;
1440 GNUNET_SCHEDULER_TaskIdentifier ret;
1442 GNUNET_assert (wfd != NULL);
1443 ws = GNUNET_NETWORK_fdset_create ();
1444 GNUNET_NETWORK_fdset_set (ws, wfd);
1446 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1449 GNUNET_NETWORK_fdset_destroy (ws);
1452 GNUNET_assert (GNUNET_NETWORK_get_fd (wfd) >= 0);
1453 return add_without_sets (delay,
1454 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1455 -1, GNUNET_NETWORK_get_fd (wfd), task,
1462 * Schedule a new task to be run with a specified delay or when the
1463 * specified file descriptor is ready for reading. The delay can be
1464 * used as a timeout on the socket being ready. The task will be
1465 * scheduled for execution once either the delay has expired or the
1466 * socket operation is ready. It will be run with the DEFAULT priority.
1468 * @param delay when should this operation time out? Use
1469 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1470 * @param rfd read file-descriptor
1471 * @param task main function of the task
1472 * @param task_cls closure of task
1473 * @return unique task identifier for the job
1474 * only valid until "task" is started!
1476 GNUNET_SCHEDULER_TaskIdentifier
1477 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1478 const struct GNUNET_DISK_FileHandle *rfd,
1479 GNUNET_SCHEDULER_Task task, void *task_cls)
1482 struct GNUNET_NETWORK_FDSet *rs;
1483 GNUNET_SCHEDULER_TaskIdentifier ret;
1485 GNUNET_assert (rfd != NULL);
1486 rs = GNUNET_NETWORK_fdset_create ();
1487 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1489 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1492 GNUNET_NETWORK_fdset_destroy (rs);
1497 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1498 return add_without_sets (delay,
1499 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1500 fd, -1, task, task_cls);
1507 * Schedule a new task to be run with a specified delay or when the
1508 * specified file descriptor is ready for writing. The delay can be
1509 * used as a timeout on the socket being ready. The task will be
1510 * scheduled for execution once either the delay has expired or the
1511 * socket operation is ready. It will be run with the DEFAULT priority.
1513 * @param delay when should this operation time out? Use
1514 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1515 * @param wfd write file-descriptor
1516 * @param task main function of the task
1517 * @param task_cls closure of task
1518 * @return unique task identifier for the job
1519 * only valid until "task" is started!
1521 GNUNET_SCHEDULER_TaskIdentifier
1522 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1523 const struct GNUNET_DISK_FileHandle *wfd,
1524 GNUNET_SCHEDULER_Task task, void *task_cls)
1527 struct GNUNET_NETWORK_FDSet *ws;
1528 GNUNET_SCHEDULER_TaskIdentifier ret;
1530 GNUNET_assert (wfd != NULL);
1531 ws = GNUNET_NETWORK_fdset_create ();
1532 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1534 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1537 GNUNET_NETWORK_fdset_destroy (ws);
1542 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1543 GNUNET_assert (fd >= 0);
1544 return add_without_sets (delay,
1545 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1546 -1, fd, task, task_cls);
1554 * Schedule a new task to be run with a specified delay or when any of
1555 * the specified file descriptor sets is ready. The delay can be used
1556 * as a timeout on the socket(s) being ready. The task will be
1557 * scheduled for execution once either the delay has expired or any of
1558 * the socket operations is ready. This is the most general
1559 * function of the "add" family. Note that the "prerequisite_task"
1560 * must be satisfied in addition to any of the other conditions. In
1561 * other words, the task will be started when
1563 * (prerequisite-run)
1567 * || (shutdown-active && run-on-shutdown) )
1570 * @param prio how important is this task?
1571 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1572 * which means that the task will only be run after we receive SIGTERM
1573 * @param rs set of file descriptors we want to read (can be NULL)
1574 * @param ws set of file descriptors we want to write (can be NULL)
1575 * @param task main function of the task
1576 * @param task_cls closure of task
1577 * @return unique task identifier for the job
1578 * only valid until "task" is started!
1580 GNUNET_SCHEDULER_TaskIdentifier
1581 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1582 struct GNUNET_TIME_Relative delay,
1583 const struct GNUNET_NETWORK_FDSet *rs,
1584 const struct GNUNET_NETWORK_FDSet *ws,
1585 GNUNET_SCHEDULER_Task task, void *task_cls)
1590 void *backtrace_array[MAX_TRACE_DEPTH];
1593 GNUNET_assert (NULL != active_task);
1594 GNUNET_assert (NULL != task);
1595 t = GNUNET_malloc (sizeof (struct Task));
1597 t->callback_cls = task_cls;
1599 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1600 t->backtrace_strings =
1601 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1607 t->read_set = GNUNET_NETWORK_fdset_create ();
1608 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1612 t->write_set = GNUNET_NETWORK_fdset_create ();
1613 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1617 t->start_time = GNUNET_TIME_absolute_get ();
1619 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1621 check_priority ((prio ==
1622 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1624 t->lifeness = current_lifeness;
1627 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1628 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1633 for (i = 0; i < t->num_backtrace_strings; i++)
1634 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1635 t->backtrace_strings[i]);
1640 /* end of scheduler.c */