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_common.h"
28 #include "gnunet_os_lib.h"
29 #include "gnunet_scheduler_lib.h"
30 #include "gnunet_signal_lib.h"
31 #include "gnunet_time_lib.h"
34 #define LOG(kind,...) GNUNET_log_from (kind, "util-scheduler", __VA_ARGS__)
36 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-scheduler", syscall)
43 * Use lsof to generate file descriptor reports on select error?
44 * (turn off for stable releases).
46 #define USE_LSOF GNUNET_NO
49 * Obtain trace information for all scheduler calls that schedule tasks.
51 #define EXECINFO GNUNET_NO
54 * Check each file descriptor before adding
56 #define DEBUG_FDS GNUNET_NO
59 * Depth of the traces collected via EXECINFO.
61 #define MAX_TRACE_DEPTH 50
65 * Should we figure out which tasks are delayed for a while
66 * before they are run? (Consider using in combination with EXECINFO).
68 #define PROFILE_DELAYS GNUNET_NO
71 * Task that were in the queue for longer than this are reported if
72 * PROFILE_DELAYS is active.
74 #define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
78 * Linked list of pending tasks.
83 * This is a linked list.
88 * Function to run when ready.
90 GNUNET_SCHEDULER_Task callback;
93 * Closure for the callback.
98 * Set of file descriptors this task is waiting
99 * for for reading. Once ready, this is updated
100 * to reflect the set of file descriptors ready
103 struct GNUNET_NETWORK_FDSet *read_set;
106 * Set of file descriptors this task is waiting for for writing.
107 * Once ready, this is updated to reflect the set of file
108 * descriptors ready for operation.
110 struct GNUNET_NETWORK_FDSet *write_set;
113 * Unique task identifier.
115 GNUNET_SCHEDULER_TaskIdentifier id;
118 * Absolute timeout value for the task, or
119 * GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
121 struct GNUNET_TIME_Absolute timeout;
125 * When was the task scheduled?
127 struct GNUNET_TIME_Absolute start_time;
131 * Why is the task ready? Set after task is added to ready queue.
132 * Initially set to zero. All reasons that have already been
133 * satisfied (i.e. read or write ready) will be set over time.
135 enum GNUNET_SCHEDULER_Reason reason;
140 enum GNUNET_SCHEDULER_Priority priority;
143 * Set if we only wait for reading from a single FD, otherwise -1.
148 * Set if we only wait for writing to a single FD, otherwise -1.
153 * Should the existence of this task in the queue be counted as
154 * reason to not shutdown the scheduler?
160 * Array of strings which make up a backtrace from the point when this
161 * task was scheduled (essentially, who scheduled the task?)
163 char **backtrace_strings;
166 * Size of the backtrace_strings array
168 int num_backtrace_strings;
176 * List of tasks waiting for an event.
178 static struct Task *pending;
181 * List of tasks waiting ONLY for a timeout event.
182 * Sorted by timeout (earliest first). Used so that
183 * we do not traverse the list of these tasks when
184 * building select sets (we just look at the head
185 * to determine the respective timeout ONCE).
187 static struct Task *pending_timeout;
190 * Last inserted task waiting ONLY for a timeout event.
191 * Used to (heuristically) speed up insertion.
193 static struct Task *pending_timeout_last;
196 * ID of the task that is running right now.
198 static struct Task *active_task;
201 * List of tasks ready to run right now,
202 * grouped by importance.
204 static struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
207 * Identity of the last task queued. Incremented for each task to
208 * generate a unique task ID (it is virtually impossible to start
209 * more than 2^64 tasks during the lifetime of a process).
211 static GNUNET_SCHEDULER_TaskIdentifier last_id;
214 * Number of tasks on the ready list.
216 static unsigned int ready_count;
219 * How many tasks have we run so far?
221 static unsigned long long tasks_run;
224 * Priority of the task running right now. Only
225 * valid while a task is running.
227 static enum GNUNET_SCHEDULER_Priority current_priority;
230 * Priority of the highest task added in the current select
233 static enum GNUNET_SCHEDULER_Priority max_priority_added;
236 * Value of the 'lifeness' flag for the current task.
238 static int current_lifeness;
241 * Function to use as a select() in the scheduler.
242 * If NULL, we use GNUNET_NETWORK_socket_select ().
244 static GNUNET_SCHEDULER_select scheduler_select;
247 * Closure for 'scheduler_select'.
249 static void *scheduler_select_cls;
252 * Sets the select function to use in the scheduler (scheduler_select).
254 * @param new_select new select function to use
255 * @param new_select_cls closure for 'new_select'
256 * @return previously used select function, NULL for default
259 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
260 void *new_select_cls)
262 scheduler_select = new_select;
263 scheduler_select_cls = new_select_cls;
268 * Check that the given priority is legal (and return it).
270 * @param p priority value to check
271 * @return p on success, 0 on error
273 static enum GNUNET_SCHEDULER_Priority
274 check_priority (enum GNUNET_SCHEDULER_Priority p)
276 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
279 return 0; /* make compiler happy */
284 * Update all sets and timeout for select.
286 * @param rs read-set, set to all FDs we would like to read (updated)
287 * @param ws write-set, set to all FDs we would like to write (updated)
288 * @param timeout next timeout (updated)
291 update_sets (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws,
292 struct GNUNET_TIME_Relative *timeout)
295 struct GNUNET_TIME_Absolute now;
296 struct GNUNET_TIME_Relative to;
298 now = GNUNET_TIME_absolute_get ();
299 pos = pending_timeout;
302 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
303 if (timeout->rel_value_us > to.rel_value_us)
305 if (pos->reason != 0)
306 *timeout = GNUNET_TIME_UNIT_ZERO;
311 if (pos->timeout.abs_value_us != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
313 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
314 if (timeout->rel_value_us > to.rel_value_us)
317 if (pos->read_fd != -1)
318 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
319 if (pos->write_fd != -1)
320 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
321 if (pos->read_set != NULL)
322 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
323 if (pos->write_set != NULL)
324 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
325 if (pos->reason != 0)
326 *timeout = GNUNET_TIME_UNIT_ZERO;
333 * Check if the ready set overlaps with the set we want to have ready.
334 * If so, update the want set (set all FDs that are ready). If not,
337 * @param ready set that is ready
338 * @param want set that we want to be ready
339 * @return GNUNET_YES if there was some overlap
342 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
343 struct GNUNET_NETWORK_FDSet *want)
345 if ((NULL == want) || (NULL == ready))
347 if (GNUNET_NETWORK_fdset_overlap (ready, want))
349 /* copy all over (yes, there maybe unrelated bits,
350 * but this should not hurt well-written clients) */
351 GNUNET_NETWORK_fdset_copy (want, ready);
359 * Check if the given task is eligible to run now.
360 * Also set the reason why it is eligible.
362 * @param task task to check if it is ready
363 * @param now the current time
364 * @param rs set of FDs ready for reading
365 * @param ws set of FDs ready for writing
366 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
369 is_ready (struct Task *task, struct GNUNET_TIME_Absolute now,
370 const struct GNUNET_NETWORK_FDSet *rs,
371 const struct GNUNET_NETWORK_FDSet *ws)
373 enum GNUNET_SCHEDULER_Reason reason;
375 reason = task->reason;
376 if (now.abs_value_us >= task->timeout.abs_value_us)
377 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
378 if ((0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
379 (((task->read_fd != -1) &&
380 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd))) ||
381 (set_overlaps (rs, task->read_set))))
382 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
383 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
384 (((task->write_fd != -1) &&
385 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)))
386 || (set_overlaps (ws, task->write_set))))
387 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
389 return GNUNET_NO; /* not ready */
390 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
391 task->reason = reason;
397 * Put a task that is ready for execution into the ready queue.
399 * @param task task ready for execution
402 queue_ready_task (struct Task *task)
404 enum GNUNET_SCHEDULER_Priority p = task->priority;
406 if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
407 p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
408 task->next = ready[check_priority (p)];
409 ready[check_priority (p)] = task;
415 * Check which tasks are ready and move them
416 * to the respective ready queue.
418 * @param rs FDs ready for reading
419 * @param ws FDs ready for writing
422 check_ready (const struct GNUNET_NETWORK_FDSet *rs,
423 const struct GNUNET_NETWORK_FDSet *ws)
428 struct GNUNET_TIME_Absolute now;
430 now = GNUNET_TIME_absolute_get ();
432 pos = pending_timeout;
436 if (now.abs_value_us >= pos->timeout.abs_value_us)
437 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
438 if (0 == pos->reason)
440 pending_timeout = next;
441 if (pending_timeout_last == pos)
442 pending_timeout_last = NULL;
443 queue_ready_task (pos);
449 LOG (GNUNET_ERROR_TYPE_DEBUG,
450 "Checking readiness of task: %llu / %p\n",
451 pos->id, pos->callback_cls);
453 if (GNUNET_YES == is_ready (pos, now, rs, ws))
459 queue_ready_task (pos);
470 * Request the shutdown of a scheduler. Marks all currently
471 * pending tasks as ready because of shutdown. This will
472 * cause all tasks to run (as soon as possible, respecting
473 * priorities and prerequisite tasks). Note that tasks
474 * scheduled AFTER this call may still be delayed arbitrarily.
477 GNUNET_SCHEDULER_shutdown ()
482 pos = pending_timeout;
485 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
486 /* we don't move the task into the ready queue yet; check_ready
487 * will do that later, possibly adding additional
488 * readiness-factors */
494 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
495 /* we don't move the task into the ready queue yet; check_ready
496 * will do that later, possibly adding additional
497 * readiness-factors */
500 for (i = 0; i < GNUNET_SCHEDULER_PRIORITY_COUNT; i++)
505 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
506 /* we don't move the task into the ready queue yet; check_ready
507 * will do that later, possibly adding additional
508 * readiness-factors */
516 * Destroy a task (release associated resources)
518 * @param t task to destroy
521 destroy_task (struct Task *t)
523 if (NULL != t->read_set)
524 GNUNET_NETWORK_fdset_destroy (t->read_set);
525 if (NULL != t->write_set)
526 GNUNET_NETWORK_fdset_destroy (t->write_set);
528 GNUNET_free (t->backtrace_strings);
535 * Run at least one task in the highest-priority queue that is not
536 * empty. Keep running tasks until we are either no longer running
537 * "URGENT" tasks or until we have at least one "pending" task (which
538 * may become ready, hence we should select on it). Naturally, if
539 * there are no more ready tasks, we also return.
541 * @param rs FDs ready for reading
542 * @param ws FDs ready for writing
545 run_ready (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws)
547 enum GNUNET_SCHEDULER_Priority p;
549 struct GNUNET_SCHEDULER_TaskContext tc;
551 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
554 if (ready_count == 0)
556 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
557 /* yes, p>0 is correct, 0 is "KEEP" which should
558 * always be an empty queue (see assertion)! */
559 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
565 GNUNET_assert (pos != NULL); /* ready_count wrong? */
566 ready[p] = pos->next;
568 current_priority = pos->priority;
569 current_lifeness = pos->lifeness;
572 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
573 DELAY_THRESHOLD.rel_value_us)
575 LOG (GNUNET_ERROR_TYPE_DEBUG,
576 "Task %llu took %s to be scheduled\n",
577 (unsigned long long) pos->id,
578 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time), GNUNET_YES));
581 tc.reason = pos->reason;
582 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
583 if ((pos->read_fd != -1) &&
584 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)))
585 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
586 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
587 if ((pos->write_fd != -1) &&
588 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
589 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
590 if (((tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
591 (pos->write_fd != -1) &&
592 (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd)))
593 GNUNET_abort (); // added to ready in previous select loop!
594 LOG (GNUNET_ERROR_TYPE_DEBUG,
595 "Running task: %llu / %p\n", pos->id,
597 pos->callback (pos->callback_cls, &tc);
601 for (i = 0; i < pos->num_backtrace_strings; i++)
602 LOG (GNUNET_ERROR_TYPE_ERROR, "Task %llu trace %d: %s\n", pos->id, i,
603 pos->backtrace_strings[i]);
609 while ((pending == NULL) || (p >= max_priority_added));
613 * Pipe used to communicate shutdown via signal.
615 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
618 * Process ID of this process at the time we installed the various
624 * Signal handler called for SIGPIPE.
634 * Signal handler called for signals that should cause us to shutdown.
637 sighandler_shutdown ()
640 int old_errno = errno; /* backup errno */
642 if (getpid () != my_pid)
643 exit (1); /* we have fork'ed since the signal handler was created,
644 * ignore the signal, see https://gnunet.org/vfork discussion */
645 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
646 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
653 * Check if the system is still life. Trigger shutdown if we
654 * have tasks, but none of them give us lifeness.
656 * @return GNUNET_OK to continue the main loop,
666 for (t = pending; NULL != t; t = t->next)
667 if (t->lifeness == GNUNET_YES)
669 for (t = pending_timeout; NULL != t; t = t->next)
670 if (t->lifeness == GNUNET_YES)
672 if ((NULL != pending) || (NULL != pending_timeout))
674 GNUNET_SCHEDULER_shutdown ();
682 * Initialize and run scheduler. This function will return when all
683 * tasks have completed. On systems with signals, receiving a SIGTERM
684 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
685 * to be run after the active task is complete. As a result, SIGTERM
686 * causes all active tasks to be scheduled with reason
687 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
688 * afterwards will execute normally!). Note that any particular signal
689 * will only shut down one scheduler; applications should always only
690 * create a single scheduler.
692 * @param task task to run immediately
693 * @param task_cls closure of task
696 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
698 struct GNUNET_NETWORK_FDSet *rs;
699 struct GNUNET_NETWORK_FDSet *ws;
700 struct GNUNET_TIME_Relative timeout;
702 struct GNUNET_SIGNAL_Context *shc_int;
703 struct GNUNET_SIGNAL_Context *shc_term;
706 struct GNUNET_SIGNAL_Context *shc_quit;
707 struct GNUNET_SIGNAL_Context *shc_hup;
708 struct GNUNET_SIGNAL_Context *shc_pipe;
710 unsigned long long last_tr;
711 unsigned int busy_wait_warning;
712 const struct GNUNET_DISK_FileHandle *pr;
715 GNUNET_assert (active_task == NULL);
716 rs = GNUNET_NETWORK_fdset_create ();
717 ws = GNUNET_NETWORK_fdset_create ();
718 GNUNET_assert (shutdown_pipe_handle == NULL);
719 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_NO);
720 GNUNET_assert (shutdown_pipe_handle != NULL);
721 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
722 GNUNET_DISK_PIPE_END_READ);
723 GNUNET_assert (pr != NULL);
725 LOG (GNUNET_ERROR_TYPE_DEBUG, "Registering signal handlers\n");
726 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
727 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
729 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
730 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
731 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
733 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
734 current_lifeness = GNUNET_YES;
735 GNUNET_SCHEDULER_add_continuation (task, task_cls,
736 GNUNET_SCHEDULER_REASON_STARTUP);
737 active_task = (void *) (long) -1; /* force passing of sanity check */
738 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
739 &GNUNET_OS_install_parent_control_handler,
743 busy_wait_warning = 0;
744 while (GNUNET_OK == check_lifeness ())
746 GNUNET_NETWORK_fdset_zero (rs);
747 GNUNET_NETWORK_fdset_zero (ws);
748 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
749 update_sets (rs, ws, &timeout);
750 GNUNET_NETWORK_fdset_handle_set (rs, pr);
753 /* no blocking, more work already ready! */
754 timeout = GNUNET_TIME_UNIT_ZERO;
756 if (NULL == scheduler_select)
757 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
759 ret = scheduler_select (scheduler_select_cls, rs, ws, NULL, timeout);
760 if (ret == GNUNET_SYSERR)
765 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
770 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
773 if (0 != system (lsof))
774 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING, "system");
780 if ((0 == ret) && (0 == timeout.rel_value_us) && (busy_wait_warning > 16))
782 LOG (GNUNET_ERROR_TYPE_WARNING, _("Looks like we're busy waiting...\n"));
783 sleep (1); /* mitigate */
785 check_ready (rs, ws);
787 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
789 /* consume the signal */
790 GNUNET_DISK_file_read (pr, &c, sizeof (c));
791 /* mark all active tasks as ready due to shutdown */
792 GNUNET_SCHEDULER_shutdown ();
794 if (last_tr == tasks_run)
801 busy_wait_warning = 0;
804 GNUNET_SIGNAL_handler_uninstall (shc_int);
805 GNUNET_SIGNAL_handler_uninstall (shc_term);
807 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
808 GNUNET_SIGNAL_handler_uninstall (shc_quit);
809 GNUNET_SIGNAL_handler_uninstall (shc_hup);
811 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
812 shutdown_pipe_handle = NULL;
813 GNUNET_NETWORK_fdset_destroy (rs);
814 GNUNET_NETWORK_fdset_destroy (ws);
819 * Obtain the reason code for why the current task was
820 * started. Will return the same value as
821 * the GNUNET_SCHEDULER_TaskContext's reason field.
823 * @return reason(s) why the current task is run
825 enum GNUNET_SCHEDULER_Reason
826 GNUNET_SCHEDULER_get_reason ()
828 GNUNET_assert (active_task != NULL);
829 return active_task->reason;
834 * Get information about the current load of this scheduler. Use this
835 * function to determine if an elective task should be added or simply
836 * dropped (if the decision should be made based on the number of
837 * tasks ready to run).
839 * @param p priority level to look at
840 * @return number of tasks pending right now
843 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
848 GNUNET_assert (active_task != NULL);
849 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
851 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
852 p = current_priority;
854 pos = ready[check_priority (p)];
865 * Cancel the task with the specified identifier.
866 * The task must not yet have run.
868 * @param task id of the task to cancel
869 * @return original closure of the task
872 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
876 enum GNUNET_SCHEDULER_Priority p;
880 GNUNET_assert (active_task != NULL);
903 if (pending_timeout_last == t)
904 pending_timeout_last = NULL;
910 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT)
912 LOG (GNUNET_ERROR_TYPE_ERROR, _("Attempt to cancel dead task %llu!\n"),
913 (unsigned long long) task);
939 pending_timeout = t->next;
949 prev->next = t->next;
951 ret = t->callback_cls;
952 LOG (GNUNET_ERROR_TYPE_DEBUG, "Canceling task: %llu / %p\n", task,
960 * Continue the current execution with the given function. This is
961 * similar to the other "add" functions except that there is no delay
962 * and the reason code can be specified.
964 * @param task main function of the task
965 * @param task_cls closure for 'main'
966 * @param reason reason for task invocation
967 * @param priority priority to use for the task
970 GNUNET_SCHEDULER_add_continuation_with_priority (GNUNET_SCHEDULER_Task task, void *task_cls,
971 enum GNUNET_SCHEDULER_Reason reason,
972 enum GNUNET_SCHEDULER_Priority priority)
977 void *backtrace_array[50];
980 GNUNET_assert (NULL != task);
981 GNUNET_assert ((active_task != NULL) ||
982 (reason == GNUNET_SCHEDULER_REASON_STARTUP));
983 t = GNUNET_malloc (sizeof (struct Task));
985 t->num_backtrace_strings = backtrace (backtrace_array, 50);
986 t->backtrace_strings =
987 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
992 t->callback_cls = task_cls;
995 t->start_time = GNUNET_TIME_absolute_get ();
998 t->priority = priority;
999 t->lifeness = current_lifeness;
1000 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding continuation task: %llu / %p\n", t->id,
1002 queue_ready_task (t);
1007 * Continue the current execution with the given function. This is
1008 * similar to the other "add" functions except that there is no delay
1009 * and the reason code can be specified.
1011 * @param task main function of the task
1012 * @param task_cls closure for 'main'
1013 * @param reason reason for task invocation
1016 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls,
1017 enum GNUNET_SCHEDULER_Reason reason)
1019 GNUNET_SCHEDULER_add_continuation_with_priority (task, task_cls,
1021 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
1026 * Schedule a new task to be run with a specified priority.
1028 * @param prio how important is the new task?
1029 * @param task main function of the task
1030 * @param task_cls closure of task
1031 * @return unique task identifier for the job
1032 * only valid until "task" is started!
1034 GNUNET_SCHEDULER_TaskIdentifier
1035 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1036 GNUNET_SCHEDULER_Task task, void *task_cls)
1038 return GNUNET_SCHEDULER_add_select (prio,
1039 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1046 * Schedule a new task to be run with a specified delay. The task
1047 * will be scheduled for execution once the delay has expired.
1049 * @param delay when should this operation time out? Use
1050 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1051 * @param priority priority to use for the task
1052 * @param task main function of the task
1053 * @param task_cls closure of task
1054 * @return unique task identifier for the job
1055 * only valid until "task" is started!
1057 GNUNET_SCHEDULER_TaskIdentifier
1058 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1059 enum GNUNET_SCHEDULER_Priority priority,
1060 GNUNET_SCHEDULER_Task task, void *task_cls)
1067 void *backtrace_array[MAX_TRACE_DEPTH];
1070 GNUNET_assert (active_task != NULL);
1071 GNUNET_assert (NULL != task);
1072 t = GNUNET_malloc (sizeof (struct Task));
1074 t->callback_cls = task_cls;
1076 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1077 t->backtrace_strings =
1078 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1084 t->start_time = GNUNET_TIME_absolute_get ();
1086 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1087 t->priority = priority;
1088 t->lifeness = current_lifeness;
1089 /* try tail first (optimization in case we are
1090 * appending to a long list of tasks with timeouts) */
1091 prev = pending_timeout_last;
1094 if (prev->timeout.abs_value_us > t->timeout.abs_value_us)
1097 pos = prev->next; /* heuristic success! */
1101 /* heuristic failed, do traversal of timeout list */
1102 pos = pending_timeout;
1104 while ((pos != NULL) &&
1105 ((pos->timeout.abs_value_us <= t->timeout.abs_value_us) ||
1106 (0 != pos->reason)))
1112 pending_timeout = t;
1116 /* hyper-optimization... */
1117 pending_timeout_last = t;
1119 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1124 for (i = 0; i < t->num_backtrace_strings; i++)
1125 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1126 t->backtrace_strings[i]);
1133 * Schedule a new task to be run with a specified delay. The task
1134 * will be scheduled for execution once the delay has expired. It
1135 * will be run with the DEFAULT priority.
1137 * @param delay when should this operation time out? Use
1138 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1139 * @param task main function of the task
1140 * @param task_cls closure of task
1141 * @return unique task identifier for the job
1142 * only valid until "task" is started!
1144 GNUNET_SCHEDULER_TaskIdentifier
1145 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1146 GNUNET_SCHEDULER_Task task, void *task_cls)
1148 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1149 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1155 * Schedule a new task to be run as soon as possible. The task
1156 * will be run with the DEFAULT priority.
1158 * @param task main function of the task
1159 * @param task_cls closure of task
1160 * @return unique task identifier for the job
1161 * only valid until "task" is started!
1163 GNUNET_SCHEDULER_TaskIdentifier
1164 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task, void *task_cls)
1166 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO, task, task_cls);
1171 * Schedule a new task to be run as soon as possible with the
1172 * (transitive) ignore-shutdown flag either explicitly set or
1173 * explicitly enabled. This task (and all tasks created from it,
1174 * other than by another call to this function) will either count or
1175 * not count for the 'lifeness' of the process. This API is only
1176 * useful in a few special cases.
1178 * @param lifeness GNUNET_YES if the task counts for lifeness, GNUNET_NO if not.
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_now_with_lifeness (int lifeness,
1186 GNUNET_SCHEDULER_Task task,
1189 GNUNET_SCHEDULER_TaskIdentifier ret;
1192 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1193 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1195 GNUNET_assert (pending->id == ret);
1196 pending->lifeness = lifeness;
1202 * Schedule a new task to be run with a specified delay or when any of
1203 * the specified file descriptor sets is ready. The delay can be used
1204 * as a timeout on the socket(s) being ready. The task will be
1205 * scheduled for execution once either the delay has expired or any of
1206 * the socket operations is ready. This is the most general
1207 * function of the "add" family. Note that the "prerequisite_task"
1208 * must be satisfied in addition to any of the other conditions. In
1209 * other words, the task will be started when
1211 * (prerequisite-run)
1215 * || shutdown-active )
1218 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1219 * which means that the task will only be run after we receive SIGTERM
1220 * @param priority priority to use
1221 * @param rfd file descriptor we want to read (can be -1)
1222 * @param wfd file descriptors we want to write (can be -1)
1223 * @param task main function of the task
1224 * @param task_cls closure of task
1225 * @return unique task identifier for the job
1226 * only valid until "task" is started!
1229 static GNUNET_SCHEDULER_TaskIdentifier
1230 add_without_sets (struct GNUNET_TIME_Relative delay,
1231 enum GNUNET_SCHEDULER_Priority priority,
1233 GNUNET_SCHEDULER_Task task, void *task_cls)
1238 void *backtrace_array[MAX_TRACE_DEPTH];
1241 GNUNET_assert (active_task != NULL);
1242 GNUNET_assert (NULL != task);
1243 t = GNUNET_malloc (sizeof (struct Task));
1245 t->callback_cls = task_cls;
1247 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1248 t->backtrace_strings =
1249 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1254 int flags = fcntl (rfd, F_GETFD);
1256 if ((flags == -1) && (errno == EBADF))
1258 LOG (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", rfd);
1262 for (i = 0; i < t->num_backtrace_strings; i++)
1263 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n", t->backtrace_strings[i]);
1270 int flags = fcntl (wfd, F_GETFD);
1272 if (flags == -1 && errno == EBADF)
1274 LOG (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", wfd);
1278 for (i = 0; i < t->num_backtrace_strings; i++)
1279 LOG (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n", t->backtrace_strings[i]);
1286 GNUNET_assert (wfd >= -1);
1290 t->start_time = GNUNET_TIME_absolute_get ();
1292 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1293 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1294 t->lifeness = current_lifeness;
1297 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1298 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1303 for (i = 0; i < t->num_backtrace_strings; i++)
1304 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1305 t->backtrace_strings[i]);
1314 * Schedule a new task to be run with a specified delay or when the
1315 * specified file descriptor is ready for reading. The delay can be
1316 * used as a timeout on the socket being ready. The task will be
1317 * scheduled for execution once either the delay has expired or the
1318 * socket operation is ready. It will be run with the DEFAULT priority.
1320 * @param delay when should this operation time out? Use
1321 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1322 * @param rfd read file-descriptor
1323 * @param task main function of the task
1324 * @param task_cls closure of task
1325 * @return unique task identifier for the job
1326 * only valid until "task" is started!
1328 GNUNET_SCHEDULER_TaskIdentifier
1329 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1330 struct GNUNET_NETWORK_Handle *rfd,
1331 GNUNET_SCHEDULER_Task task, void *task_cls)
1333 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1334 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1335 rfd, task, task_cls);
1340 * Schedule a new task to be run with a specified priority and to be
1341 * run after the specified delay or when the specified file descriptor
1342 * is ready for reading. The delay can be used as a timeout on the
1343 * socket being ready. The task will be scheduled for execution once
1344 * either the delay has expired or the socket operation is ready. It
1345 * will be run with the DEFAULT priority.
1347 * @param delay when should this operation time out? Use
1348 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1349 * @param priority priority to use for the task
1350 * @param rfd read file-descriptor
1351 * @param task main function of the task
1352 * @param task_cls closure of task
1353 * @return unique task identifier for the job
1354 * only valid until "task" is started!
1356 GNUNET_SCHEDULER_TaskIdentifier
1357 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1358 enum GNUNET_SCHEDULER_Priority priority,
1359 struct GNUNET_NETWORK_Handle *rfd,
1360 GNUNET_SCHEDULER_Task task, void *task_cls)
1363 struct GNUNET_NETWORK_FDSet *rs;
1364 GNUNET_SCHEDULER_TaskIdentifier ret;
1366 GNUNET_assert (rfd != NULL);
1367 rs = GNUNET_NETWORK_fdset_create ();
1368 GNUNET_NETWORK_fdset_set (rs, rfd);
1370 GNUNET_SCHEDULER_add_select (priority,
1373 GNUNET_NETWORK_fdset_destroy (rs);
1376 return add_without_sets (delay,
1378 GNUNET_NETWORK_get_fd (rfd), -1, task,
1386 * Schedule a new task to be run with a specified delay or when the
1387 * specified file descriptor is ready for writing. The delay can be
1388 * used as a timeout on the socket being ready. The task will be
1389 * scheduled for execution once either the delay has expired or the
1390 * socket operation is ready. It will be run with the priority of
1393 * @param delay when should this operation time out? Use
1394 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1395 * @param wfd write file-descriptor
1396 * @param task main function of the task
1397 * @param task_cls closure of task
1398 * @return unique task identifier for the job
1399 * only valid until "task" is started!
1401 GNUNET_SCHEDULER_TaskIdentifier
1402 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1403 struct GNUNET_NETWORK_Handle *wfd,
1404 GNUNET_SCHEDULER_Task task, void *task_cls)
1407 struct GNUNET_NETWORK_FDSet *ws;
1408 GNUNET_SCHEDULER_TaskIdentifier ret;
1410 GNUNET_assert (wfd != NULL);
1411 ws = GNUNET_NETWORK_fdset_create ();
1412 GNUNET_NETWORK_fdset_set (ws, wfd);
1414 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1417 GNUNET_NETWORK_fdset_destroy (ws);
1420 GNUNET_assert (GNUNET_NETWORK_get_fd (wfd) >= 0);
1421 return add_without_sets (delay,
1422 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1423 -1, GNUNET_NETWORK_get_fd (wfd), task,
1430 * Schedule a new task to be run with a specified delay or when the
1431 * specified file descriptor is ready for reading. The delay can be
1432 * used as a timeout on the socket being ready. The task will be
1433 * scheduled for execution once either the delay has expired or the
1434 * socket operation is ready. It will be run with the DEFAULT priority.
1436 * @param delay when should this operation time out? Use
1437 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1438 * @param rfd read file-descriptor
1439 * @param task main function of the task
1440 * @param task_cls closure of task
1441 * @return unique task identifier for the job
1442 * only valid until "task" is started!
1444 GNUNET_SCHEDULER_TaskIdentifier
1445 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1446 const struct GNUNET_DISK_FileHandle *rfd,
1447 GNUNET_SCHEDULER_Task task, void *task_cls)
1450 struct GNUNET_NETWORK_FDSet *rs;
1451 GNUNET_SCHEDULER_TaskIdentifier ret;
1453 GNUNET_assert (rfd != NULL);
1454 rs = GNUNET_NETWORK_fdset_create ();
1455 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1457 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1460 GNUNET_NETWORK_fdset_destroy (rs);
1465 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1466 return add_without_sets (delay,
1467 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1468 fd, -1, task, task_cls);
1475 * Schedule a new task to be run with a specified delay or when the
1476 * specified file descriptor is ready for writing. The delay can be
1477 * used as a timeout on the socket being ready. The task will be
1478 * scheduled for execution once either the delay has expired or the
1479 * socket operation is ready. It will be run with the DEFAULT priority.
1481 * @param delay when should this operation time out? Use
1482 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1483 * @param wfd write file-descriptor
1484 * @param task main function of the task
1485 * @param task_cls closure of task
1486 * @return unique task identifier for the job
1487 * only valid until "task" is started!
1489 GNUNET_SCHEDULER_TaskIdentifier
1490 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1491 const struct GNUNET_DISK_FileHandle *wfd,
1492 GNUNET_SCHEDULER_Task task, void *task_cls)
1495 struct GNUNET_NETWORK_FDSet *ws;
1496 GNUNET_SCHEDULER_TaskIdentifier ret;
1498 GNUNET_assert (wfd != NULL);
1499 ws = GNUNET_NETWORK_fdset_create ();
1500 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1502 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1505 GNUNET_NETWORK_fdset_destroy (ws);
1510 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1511 GNUNET_assert (fd >= 0);
1512 return add_without_sets (delay,
1513 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1514 -1, fd, task, task_cls);
1522 * Schedule a new task to be run with a specified delay or when any of
1523 * the specified file descriptor sets is ready. The delay can be used
1524 * as a timeout on the socket(s) being ready. The task will be
1525 * scheduled for execution once either the delay has expired or any of
1526 * the socket operations is ready. This is the most general
1527 * function of the "add" family. Note that the "prerequisite_task"
1528 * must be satisfied in addition to any of the other conditions. In
1529 * other words, the task will be started when
1531 * (prerequisite-run)
1535 * || (shutdown-active && run-on-shutdown) )
1538 * @param prio how important is this task?
1539 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1540 * which means that the task will only be run after we receive SIGTERM
1541 * @param rs set of file descriptors we want to read (can be NULL)
1542 * @param ws set of file descriptors we want to write (can be NULL)
1543 * @param task main function of the task
1544 * @param task_cls closure of task
1545 * @return unique task identifier for the job
1546 * only valid until "task" is started!
1548 GNUNET_SCHEDULER_TaskIdentifier
1549 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1550 struct GNUNET_TIME_Relative delay,
1551 const struct GNUNET_NETWORK_FDSet *rs,
1552 const struct GNUNET_NETWORK_FDSet *ws,
1553 GNUNET_SCHEDULER_Task task, void *task_cls)
1558 void *backtrace_array[MAX_TRACE_DEPTH];
1561 GNUNET_assert (NULL != active_task);
1562 GNUNET_assert (NULL != task);
1563 t = GNUNET_malloc (sizeof (struct Task));
1565 t->callback_cls = task_cls;
1567 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1568 t->backtrace_strings =
1569 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1575 t->read_set = GNUNET_NETWORK_fdset_create ();
1576 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1580 t->write_set = GNUNET_NETWORK_fdset_create ();
1581 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1585 t->start_time = GNUNET_TIME_absolute_get ();
1587 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1589 check_priority ((prio ==
1590 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1592 t->lifeness = current_lifeness;
1595 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1596 LOG (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1601 for (i = 0; i < t->num_backtrace_strings; i++)
1602 LOG (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1603 t->backtrace_strings[i]);
1608 /* end of scheduler.c */