2 This file is part of GNUnet
3 (C) 2009, 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/scheduler.c
23 * @brief schedule computations using continuation passing style
24 * @author Christian Grothoff
27 #include "gnunet_common.h"
28 #include "gnunet_os_lib.h"
29 #include "gnunet_scheduler_lib.h"
30 #include "gnunet_signal_lib.h"
31 #include "gnunet_time_lib.h"
38 * Use lsof to generate file descriptor reports on select error?
39 * (turn off for stable releases).
41 #define USE_LSOF GNUNET_NO
44 * Obtain trace information for all scheduler calls that schedule tasks.
46 #define EXECINFO GNUNET_NO
49 * Check each file descriptor before adding
51 #define DEBUG_FDS GNUNET_NO
54 * Depth of the traces collected via EXECINFO.
56 #define MAX_TRACE_DEPTH 50
59 #define DEBUG_TASKS GNUNET_NO
62 * Should we figure out which tasks are delayed for a while
63 * before they are run? (Consider using in combination with EXECINFO).
65 #define PROFILE_DELAYS GNUNET_NO
68 * Task that were in the queue for longer than this are reported if
69 * PROFILE_DELAYS is active.
71 #define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
74 * Linked list of pending tasks.
79 * This is a linked list.
84 * Function to run when ready.
86 GNUNET_SCHEDULER_Task callback;
89 * Closure for the callback.
94 * Set of file descriptors this task is waiting
95 * for for reading. Once ready, this is updated
96 * to reflect the set of file descriptors ready
99 struct GNUNET_NETWORK_FDSet *read_set;
102 * Set of file descriptors this task is waiting for for writing.
103 * Once ready, this is updated to reflect the set of file
104 * descriptors ready for operation.
106 struct GNUNET_NETWORK_FDSet *write_set;
109 * Unique task identifier.
111 GNUNET_SCHEDULER_TaskIdentifier id;
114 * Identifier of a prerequisite task.
116 GNUNET_SCHEDULER_TaskIdentifier prereq_id;
119 * Absolute timeout value for the task, or
120 * GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
122 struct GNUNET_TIME_Absolute timeout;
126 * When was the task scheduled?
128 struct GNUNET_TIME_Absolute start_time;
132 * Why is the task ready? Set after task is added to ready queue.
133 * Initially set to zero. All reasons that have already been
134 * satisfied (i.e. read or write ready) will be set over time.
136 enum GNUNET_SCHEDULER_Reason reason;
141 enum GNUNET_SCHEDULER_Priority priority;
144 * Set if we only wait for reading from a single FD, otherwise -1.
149 * Set if we only wait for writing to a single FD, otherwise -1.
154 * Should the existence of this task in the queue be counted as
155 * reason to not shutdown the scheduler?
161 * Array of strings which make up a backtrace from the point when this
162 * task was scheduled (essentially, who scheduled the task?)
164 char **backtrace_strings;
167 * Size of the backtrace_strings array
169 int num_backtrace_strings;
177 * List of tasks waiting for an event.
179 static struct Task *pending;
182 * List of tasks waiting ONLY for a timeout event.
183 * Sorted by timeout (earliest first). Used so that
184 * we do not traverse the list of these tasks when
185 * building select sets (we just look at the head
186 * to determine the respective timeout ONCE).
188 static struct Task *pending_timeout;
191 * Last inserted task waiting ONLY for a timeout event.
192 * Used to (heuristically) speed up insertion.
194 static struct Task *pending_timeout_last;
197 * ID of the task that is running right now.
199 static struct Task *active_task;
202 * List of tasks ready to run right now,
203 * grouped by importance.
205 static struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
208 * Identity of the last task queued. Incremented for each task to
209 * generate a unique task ID (it is virtually impossible to start
210 * more than 2^64 tasks during the lifetime of a process).
212 static GNUNET_SCHEDULER_TaskIdentifier last_id;
215 * Highest number so that all tasks with smaller identifiers
216 * have already completed. Also the lowest number of a task
217 * still waiting to be executed.
219 static GNUNET_SCHEDULER_TaskIdentifier lowest_pending_id;
222 * Number of tasks on the ready list.
224 static unsigned int ready_count;
227 * How many tasks have we run so far?
229 static unsigned long long tasks_run;
232 * Priority of the task running right now. Only
233 * valid while a task is running.
235 static enum GNUNET_SCHEDULER_Priority current_priority;
238 * Priority of the highest task added in the current select
241 static enum GNUNET_SCHEDULER_Priority max_priority_added;
244 * Value of the 'lifeness' flag for the current task.
246 static int current_lifeness;
249 * Check that the given priority is legal (and return it).
251 * @param p priority value to check
252 * @return p on success, 0 on error
254 static enum GNUNET_SCHEDULER_Priority
255 check_priority (enum GNUNET_SCHEDULER_Priority p)
257 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
260 return 0; /* make compiler happy */
265 * Is a task with this identifier still pending? Also updates
266 * "lowest_pending_id" as a side-effect (for faster checks in the
267 * future), but only if the return value is "GNUNET_NO" (and
268 * the "lowest_pending_id" check failed).
270 * @param id which task are we checking for
271 * @return GNUNET_YES if so, GNUNET_NO if not
274 is_pending (GNUNET_SCHEDULER_TaskIdentifier id)
277 enum GNUNET_SCHEDULER_Priority p;
278 GNUNET_SCHEDULER_TaskIdentifier min;
280 if (id < lowest_pending_id)
282 min = -1; /* maximum value */
292 pos = pending_timeout;
301 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
313 lowest_pending_id = min;
319 * Update all sets and timeout for select.
321 * @param rs read-set, set to all FDs we would like to read (updated)
322 * @param ws write-set, set to all FDs we would like to write (updated)
323 * @param timeout next timeout (updated)
326 update_sets (struct GNUNET_NETWORK_FDSet *rs,
327 struct GNUNET_NETWORK_FDSet *ws,
328 struct GNUNET_TIME_Relative *timeout)
331 struct GNUNET_TIME_Absolute now;
332 struct GNUNET_TIME_Relative to;
334 now = GNUNET_TIME_absolute_get ();
335 pos = pending_timeout;
338 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
339 if (timeout->rel_value > to.rel_value)
341 if (pos->reason != 0)
342 *timeout = GNUNET_TIME_UNIT_ZERO;
347 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
348 (GNUNET_YES == is_pending (pos->prereq_id)))
353 if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
355 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
356 if (timeout->rel_value > to.rel_value)
359 if (pos->read_fd != -1)
360 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
361 if (pos->write_fd != -1)
362 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
363 if (pos->read_set != NULL)
364 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
365 if (pos->write_set != NULL)
366 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
367 if (pos->reason != 0)
368 *timeout = GNUNET_TIME_UNIT_ZERO;
375 * Check if the ready set overlaps with the set we want to have ready.
376 * If so, update the want set (set all FDs that are ready). If not,
379 * @param ready set that is ready
380 * @param want set that we want to be ready
381 * @return GNUNET_YES if there was some overlap
384 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
385 struct GNUNET_NETWORK_FDSet *want)
387 if ( (NULL == want) || (NULL == ready) )
389 if (GNUNET_NETWORK_fdset_overlap (ready, want))
391 /* copy all over (yes, there maybe unrelated bits,
392 but this should not hurt well-written clients) */
393 GNUNET_NETWORK_fdset_copy (want, ready);
401 * Check if the given task is eligible to run now.
402 * Also set the reason why it is eligible.
404 * @param task task to check if it is ready
405 * @param now the current time
406 * @param rs set of FDs ready for reading
407 * @param ws set of FDs ready for writing
408 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
411 is_ready (struct Task *task,
412 struct GNUNET_TIME_Absolute now,
413 const struct GNUNET_NETWORK_FDSet *rs,
414 const struct GNUNET_NETWORK_FDSet *ws)
416 enum GNUNET_SCHEDULER_Reason reason;
418 reason = task->reason;
419 if (now.abs_value >= task->timeout.abs_value)
420 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
421 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
422 ( ( (task->read_fd != -1) &&
423 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd)) ) ||
424 (set_overlaps (rs, task->read_set) ) ) )
425 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
426 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
427 ( ( (task->write_fd != -1) &&
428 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)) ) ||
429 (set_overlaps (ws, task->write_set) ) ) )
430 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
432 return GNUNET_NO; /* not ready */
433 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
435 if (GNUNET_YES == is_pending (task->prereq_id))
437 task->reason = reason;
438 return GNUNET_NO; /* prereq waiting */
440 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
442 task->reason = reason;
448 * Put a task that is ready for execution into the ready queue.
450 * @param task task ready for execution
453 queue_ready_task (struct Task *task)
455 enum GNUNET_SCHEDULER_Priority p = task->priority;
456 if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
457 p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
458 task->next = ready[check_priority (p)];
459 ready[check_priority (p)] = task;
465 * Check which tasks are ready and move them
466 * to the respective ready queue.
468 * @param rs FDs ready for reading
469 * @param ws FDs ready for writing
472 check_ready (const struct GNUNET_NETWORK_FDSet *rs,
473 const struct GNUNET_NETWORK_FDSet *ws)
478 struct GNUNET_TIME_Absolute now;
480 now = GNUNET_TIME_absolute_get ();
482 pos = pending_timeout;
486 if (now.abs_value >= pos->timeout.abs_value)
487 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
488 if (0 == pos->reason)
490 pending_timeout = next;
491 if (pending_timeout_last == pos)
492 pending_timeout_last = NULL;
493 queue_ready_task (pos);
500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
501 "Checking readiness of task: %llu / %p\n",
502 pos->id, pos->callback_cls);
505 if (GNUNET_YES == is_ready (pos, now, rs, ws))
511 queue_ready_task (pos);
522 * Request the shutdown of a scheduler. Marks all currently
523 * pending tasks as ready because of shutdown. This will
524 * cause all tasks to run (as soon as possible, respecting
525 * priorities and prerequisite tasks). Note that tasks
526 * scheduled AFTER this call may still be delayed arbitrarily.
529 GNUNET_SCHEDULER_shutdown ()
534 pos = pending_timeout;
537 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
538 /* we don't move the task into the ready queue yet; check_ready
539 will do that later, possibly adding additional
546 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
547 /* we don't move the task into the ready queue yet; check_ready
548 will do that later, possibly adding additional
552 for (i=0;i<GNUNET_SCHEDULER_PRIORITY_COUNT;i++)
557 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
558 /* we don't move the task into the ready queue yet; check_ready
559 will do that later, possibly adding additional
568 * Destroy a task (release associated resources)
570 * @param t task to destroy
573 destroy_task (struct Task *t)
575 if (NULL != t->read_set)
576 GNUNET_NETWORK_fdset_destroy (t->read_set);
577 if (NULL != t->write_set)
578 GNUNET_NETWORK_fdset_destroy (t->write_set);
580 GNUNET_free (t->backtrace_strings);
587 * Run at least one task in the highest-priority queue that is not
588 * empty. Keep running tasks until we are either no longer running
589 * "URGENT" tasks or until we have at least one "pending" task (which
590 * may become ready, hence we should select on it). Naturally, if
591 * there are no more ready tasks, we also return.
593 * @param rs FDs ready for reading
594 * @param ws FDs ready for writing
597 run_ready (struct GNUNET_NETWORK_FDSet *rs,
598 struct GNUNET_NETWORK_FDSet *ws)
600 enum GNUNET_SCHEDULER_Priority p;
602 struct GNUNET_SCHEDULER_TaskContext tc;
604 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
607 if (ready_count == 0)
609 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
610 /* yes, p>0 is correct, 0 is "KEEP" which should
611 always be an empty queue (see assertion)! */
612 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
618 GNUNET_assert (pos != NULL); /* ready_count wrong? */
619 ready[p] = pos->next;
621 if (current_priority != pos->priority)
623 current_priority = pos->priority;
624 (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
627 current_lifeness = pos->lifeness;
630 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
631 DELAY_THRESHOLD.rel_value)
633 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
634 "Task %llu took %llums to be scheduled\n",
636 (unsigned long long) GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value);
639 tc.reason = pos->reason;
640 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
641 if ( (pos->read_fd != -1) &&
642 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
643 GNUNET_NETWORK_fdset_set_native (rs,
645 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
646 if ( (pos->write_fd != -1) &&
647 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) )
648 GNUNET_NETWORK_fdset_set_native (ws,
650 if ( ( (tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
651 (pos->write_fd != -1) &&
652 (! GNUNET_NETWORK_fdset_test_native (ws,
654 abort (); // added to ready in previous select loop!
656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
657 "Running task: %llu / %p\n", pos->id, pos->callback_cls);
659 pos->callback (pos->callback_cls, &tc);
662 for (i=0;i<pos->num_backtrace_strings;i++)
663 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
664 "Task %llu trace %d: %s\n",
667 pos->backtrace_strings[i]);
673 while ( (pending == NULL) || (p >= max_priority_added) );
677 * Pipe used to communicate shutdown via signal.
679 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
682 * Signal handler called for SIGPIPE.
692 * Signal handler called for signals that should cause us to shutdown.
695 sighandler_shutdown ()
698 int old_errno = errno; /* backup errno */
700 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
701 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE), &c,
708 * Check if the system is still life. Trigger shutdown if we
709 * have tasks, but none of them give us lifeness.
711 * @return GNUNET_OK to continue the main loop,
720 for (t = pending; NULL != t; t = t->next)
721 if (t->lifeness == GNUNET_YES)
723 for (t = pending_timeout; NULL != t; t = t->next)
724 if (t->lifeness == GNUNET_YES)
726 if ( (NULL != pending) || (NULL != pending_timeout) )
728 GNUNET_SCHEDULER_shutdown ();
736 * Initialize and run scheduler. This function will return when all
737 * tasks have completed. On systems with signals, receiving a SIGTERM
738 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
739 * to be run after the active task is complete. As a result, SIGTERM
740 * causes all active tasks to be scheduled with reason
741 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
742 * afterwards will execute normally!). Note that any particular signal
743 * will only shut down one scheduler; applications should always only
744 * create a single scheduler.
746 * @param task task to run immediately
747 * @param task_cls closure of task
750 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
752 struct GNUNET_NETWORK_FDSet *rs;
753 struct GNUNET_NETWORK_FDSet *ws;
754 struct GNUNET_TIME_Relative timeout;
756 struct GNUNET_SIGNAL_Context *shc_int;
757 struct GNUNET_SIGNAL_Context *shc_term;
759 struct GNUNET_SIGNAL_Context *shc_quit;
760 struct GNUNET_SIGNAL_Context *shc_hup;
761 struct GNUNET_SIGNAL_Context *shc_pipe;
763 unsigned long long last_tr;
764 unsigned int busy_wait_warning;
765 const struct GNUNET_DISK_FileHandle *pr;
768 GNUNET_assert (active_task == NULL);
769 rs = GNUNET_NETWORK_fdset_create ();
770 ws = GNUNET_NETWORK_fdset_create ();
771 GNUNET_assert (shutdown_pipe_handle == NULL);
772 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
773 GNUNET_assert (shutdown_pipe_handle != NULL);
774 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_READ);
775 GNUNET_assert (pr != NULL);
776 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
777 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
779 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
780 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
781 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
783 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
784 current_lifeness = GNUNET_YES;
785 GNUNET_SCHEDULER_add_continuation (task,
787 GNUNET_SCHEDULER_REASON_STARTUP);
788 #if ENABLE_WINDOWS_WORKAROUNDS
789 active_task = (void*) (long) -1; /* force passing of sanity check */
790 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
791 &GNUNET_OS_install_parent_control_handler,
796 busy_wait_warning = 0;
797 while (GNUNET_OK == check_lifeness ())
799 GNUNET_NETWORK_fdset_zero (rs);
800 GNUNET_NETWORK_fdset_zero (ws);
801 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
802 update_sets (rs, ws, &timeout);
803 GNUNET_NETWORK_fdset_handle_set (rs, pr);
806 /* no blocking, more work already ready! */
807 timeout = GNUNET_TIME_UNIT_ZERO;
809 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
810 if (ret == GNUNET_SYSERR)
815 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
819 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid());
822 if (0 != system (lsof))
823 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "system");
829 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
831 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
832 _("Looks like we're busy waiting...\n"));
833 sleep (1); /* mitigate */
835 check_ready (rs, ws);
837 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
839 /* consume the signal */
840 GNUNET_DISK_file_read (pr, &c, sizeof (c));
841 /* mark all active tasks as ready due to shutdown */
842 GNUNET_SCHEDULER_shutdown ();
844 if (last_tr == tasks_run)
851 busy_wait_warning = 0;
854 GNUNET_SIGNAL_handler_uninstall (shc_int);
855 GNUNET_SIGNAL_handler_uninstall (shc_term);
857 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
858 GNUNET_SIGNAL_handler_uninstall (shc_quit);
859 GNUNET_SIGNAL_handler_uninstall (shc_hup);
861 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
862 shutdown_pipe_handle = NULL;
863 GNUNET_NETWORK_fdset_destroy (rs);
864 GNUNET_NETWORK_fdset_destroy (ws);
869 * Obtain the reason code for why the current task was
870 * started. Will return the same value as
871 * the GNUNET_SCHEDULER_TaskContext's reason field.
873 * @return reason(s) why the current task is run
875 enum GNUNET_SCHEDULER_Reason
876 GNUNET_SCHEDULER_get_reason ()
878 GNUNET_assert (active_task != NULL);
879 return active_task->reason;
884 * Get information about the current load of this scheduler. Use this
885 * function to determine if an elective task should be added or simply
886 * dropped (if the decision should be made based on the number of
887 * tasks ready to run).
889 * @param p priority level to look at
890 * @return number of tasks pending right now
893 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
898 GNUNET_assert (active_task != NULL);
899 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
901 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
902 p = current_priority;
904 pos = ready[check_priority (p)];
915 * Cancel the task with the specified identifier.
916 * The task must not yet have run.
918 * @param task id of the task to cancel
919 * @return original closure of the task
922 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
926 enum GNUNET_SCHEDULER_Priority p;
930 GNUNET_assert (active_task != NULL);
953 if (pending_timeout_last == t)
954 pending_timeout_last = NULL;
960 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT)
962 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
963 _("Attempt to cancel dead task %llu!\n"),
964 (unsigned long long) task);
990 pending_timeout = t->next;
1000 prev->next = t->next;
1002 ret = t->callback_cls;
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005 "Canceling task: %llu / %p\n", task, t->callback_cls);
1013 * Continue the current execution with the given function. This is
1014 * similar to the other "add" functions except that there is no delay
1015 * and the reason code can be specified.
1017 * @param task main function of the task
1018 * @param task_cls closure for 'main'
1019 * @param reason reason for task invocation
1022 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task,
1024 enum GNUNET_SCHEDULER_Reason reason)
1028 void *backtrace_array[50];
1031 GNUNET_assert ( (active_task != NULL) ||
1032 (reason == GNUNET_SCHEDULER_REASON_STARTUP) );
1033 t = GNUNET_malloc (sizeof (struct Task));
1035 t->num_backtrace_strings = backtrace(backtrace_array, 50);
1036 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1041 t->callback_cls = task_cls;
1044 t->start_time = GNUNET_TIME_absolute_get ();
1047 t->priority = current_priority;
1048 t->lifeness = current_lifeness;
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051 "Adding continuation task: %llu / %p\n",
1052 t->id, t->callback_cls);
1054 queue_ready_task (t);
1060 * Schedule a new task to be run after the specified prerequisite task
1061 * has completed. It will be run with the priority of the calling
1064 * @param prerequisite_task run this task after the task with the given
1065 * task identifier completes (and any of our other
1066 * conditions, such as delay, read or write-readiness
1067 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1068 * on completion of other tasks (this will cause the task to run as
1069 * soon as possible).
1070 * @param task main function of the task
1071 * @param task_cls closure of task
1072 * @return unique task identifier for the job
1073 * only valid until "task" is started!
1075 GNUNET_SCHEDULER_TaskIdentifier
1076 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1077 GNUNET_SCHEDULER_Task task, void *task_cls)
1079 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1081 GNUNET_TIME_UNIT_ZERO,
1082 NULL, NULL, task, task_cls);
1087 * Schedule a new task to be run with a specified priority.
1089 * @param prio how important is the new task?
1090 * @param task main function of the task
1091 * @param task_cls closure of task
1092 * @return unique task identifier for the job
1093 * only valid until "task" is started!
1095 GNUNET_SCHEDULER_TaskIdentifier
1096 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1097 GNUNET_SCHEDULER_Task task,
1100 return GNUNET_SCHEDULER_add_select (prio,
1101 GNUNET_SCHEDULER_NO_TASK,
1102 GNUNET_TIME_UNIT_ZERO,
1103 NULL, NULL, task, task_cls);
1109 * Schedule a new task to be run with a specified delay. The task
1110 * will be scheduled for execution once the delay has expired. It
1111 * will be run with the priority of the calling task.
1113 * @param delay when should this operation time out? Use
1114 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1115 * @param task main function of the task
1116 * @param task_cls closure of task
1117 * @return unique task identifier for the job
1118 * only valid until "task" is started!
1120 GNUNET_SCHEDULER_TaskIdentifier
1121 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1122 GNUNET_SCHEDULER_Task task, void *task_cls)
1125 /* new, optimized version */
1130 void *backtrace_array[MAX_TRACE_DEPTH];
1133 GNUNET_assert (active_task != NULL);
1134 GNUNET_assert (NULL != task);
1135 t = GNUNET_malloc (sizeof (struct Task));
1137 t->callback_cls = task_cls;
1139 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1140 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1146 t->start_time = GNUNET_TIME_absolute_get ();
1148 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1149 t->priority = current_priority;
1150 t->lifeness = current_lifeness;
1151 /* try tail first (optimization in case we are
1152 appending to a long list of tasks with timeouts) */
1153 prev = pending_timeout_last;
1156 if (prev->timeout.abs_value > t->timeout.abs_value)
1159 pos = prev->next; /* heuristic success! */
1163 /* heuristic failed, do traversal of timeout list */
1164 pos = pending_timeout;
1166 while ( (pos != NULL) &&
1167 ( (pos->timeout.abs_value <= t->timeout.abs_value) ||
1168 (pos->reason != 0) ) )
1174 pending_timeout = t;
1178 /* hyper-optimization... */
1179 pending_timeout_last = t;
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1183 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1188 for (i=0;i<t->num_backtrace_strings;i++)
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 "Task %llu trace %d: %s\n",
1193 t->backtrace_strings[i]);
1198 /* unoptimized version */
1199 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1200 GNUNET_SCHEDULER_NO_TASK, delay,
1201 NULL, NULL, task, task_cls);
1208 * Schedule a new task to be run as soon as possible. The task
1209 * will be run with the priority of the calling task.
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 (GNUNET_SCHEDULER_Task task,
1220 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1221 GNUNET_SCHEDULER_NO_TASK,
1222 GNUNET_TIME_UNIT_ZERO,
1223 NULL, NULL, task, task_cls);
1228 * Schedule a new task to be run as soon as possible with the
1229 * (transitive) ignore-shutdown flag either explicitly set or
1230 * explicitly enabled. This task (and all tasks created from it,
1231 * other than by another call to this function) will either count or
1232 * not count for the 'lifeness' of the process. This API is only
1233 * useful in a few special cases.
1235 * @param lifeness GNUNET_YES if the task counts for lifeness, GNUNET_NO if not.
1236 * @param task main function of the task
1237 * @param task_cls closure of task
1238 * @return unique task identifier for the job
1239 * only valid until "task" is started!
1241 GNUNET_SCHEDULER_TaskIdentifier
1242 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1243 GNUNET_SCHEDULER_Task task,
1246 GNUNET_SCHEDULER_TaskIdentifier ret;
1248 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1249 GNUNET_SCHEDULER_NO_TASK,
1250 GNUNET_TIME_UNIT_ZERO,
1251 NULL, NULL, task, task_cls);
1252 GNUNET_assert (pending->id == ret);
1253 pending->lifeness = lifeness;
1261 * Schedule a new task to be run with a specified delay or when any of
1262 * the specified file descriptor sets is ready. The delay can be used
1263 * as a timeout on the socket(s) being ready. The task will be
1264 * scheduled for execution once either the delay has expired or any of
1265 * the socket operations is ready. This is the most general
1266 * function of the "add" family. Note that the "prerequisite_task"
1267 * must be satisfied in addition to any of the other conditions. In
1268 * other words, the task will be started when
1270 * (prerequisite-run)
1274 * || shutdown-active )
1277 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1278 * which means that the task will only be run after we receive SIGTERM
1279 * @param rfd file descriptor we want to read (can be -1)
1280 * @param wfd file descriptors we want to write (can be -1)
1281 * @param task main function of the task
1282 * @param task_cls closure of task
1283 * @return unique task identifier for the job
1284 * only valid until "task" is started!
1287 GNUNET_SCHEDULER_TaskIdentifier
1288 add_without_sets (struct GNUNET_TIME_Relative delay,
1291 GNUNET_SCHEDULER_Task task, void *task_cls)
1295 void *backtrace_array[MAX_TRACE_DEPTH];
1298 GNUNET_assert (active_task != NULL);
1299 GNUNET_assert (NULL != task);
1300 t = GNUNET_malloc (sizeof (struct Task));
1302 t->callback_cls = task_cls;
1304 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1305 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1310 int flags = fcntl(rfd, F_GETFD);
1311 if (flags == -1 && errno == EBADF)
1313 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", rfd);
1317 for (i=0;i<t->num_backtrace_strings;i++)
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1320 t->backtrace_strings[i]);
1327 int flags = fcntl(wfd, F_GETFD);
1328 if (flags == -1 && errno == EBADF)
1330 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", wfd);
1334 for (i=0;i<t->num_backtrace_strings;i++)
1335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1337 t->backtrace_strings[i]);
1344 GNUNET_assert(wfd >= -1);
1348 t->start_time = GNUNET_TIME_absolute_get ();
1350 t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
1351 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1352 t->priority = check_priority (current_priority);
1353 t->lifeness = current_lifeness;
1356 max_priority_added = GNUNET_MAX (max_priority_added,
1359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1360 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1365 for (i=0;i<t->num_backtrace_strings;i++)
1366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1367 "Task %llu trace %d: %s\n",
1370 t->backtrace_strings[i]);
1379 * Schedule a new task to be run with a specified delay or when the
1380 * specified file descriptor is ready for reading. The delay can be
1381 * used as a timeout on the socket being ready. The task will be
1382 * scheduled for execution once either the delay has expired or the
1383 * socket operation is ready. It will be run with the priority of
1386 * @param delay when should this operation time out? Use
1387 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1388 * @param rfd read file-descriptor
1389 * @param task main function of the task
1390 * @param task_cls closure of task
1391 * @return unique task identifier for the job
1392 * only valid until "task" is started!
1394 GNUNET_SCHEDULER_TaskIdentifier
1395 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1396 struct GNUNET_NETWORK_Handle * rfd,
1397 GNUNET_SCHEDULER_Task task, void *task_cls)
1400 struct GNUNET_NETWORK_FDSet *rs;
1401 GNUNET_SCHEDULER_TaskIdentifier ret;
1403 GNUNET_assert (rfd != NULL);
1404 rs = GNUNET_NETWORK_fdset_create ();
1405 GNUNET_NETWORK_fdset_set (rs, rfd);
1406 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1407 GNUNET_SCHEDULER_NO_TASK, delay,
1408 rs, NULL, task, task_cls);
1409 GNUNET_NETWORK_fdset_destroy (rs);
1412 return add_without_sets (delay,
1413 GNUNET_NETWORK_get_fd (rfd),
1422 * Schedule a new task to be run with a specified delay or when the
1423 * specified file descriptor is ready for writing. The delay can be
1424 * used as a timeout on the socket being ready. The task will be
1425 * scheduled for execution once either the delay has expired or the
1426 * socket operation is ready. It will be run with the priority of
1429 * @param delay when should this operation time out? Use
1430 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1431 * @param wfd write file-descriptor
1432 * @param task main function of the task
1433 * @param task_cls closure of task
1434 * @return unique task identifier for the job
1435 * only valid until "task" is started!
1437 GNUNET_SCHEDULER_TaskIdentifier
1438 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1439 struct GNUNET_NETWORK_Handle * wfd,
1440 GNUNET_SCHEDULER_Task task, void *task_cls)
1443 struct GNUNET_NETWORK_FDSet *ws;
1444 GNUNET_SCHEDULER_TaskIdentifier ret;
1446 GNUNET_assert (wfd != NULL);
1447 ws = GNUNET_NETWORK_fdset_create ();
1448 GNUNET_NETWORK_fdset_set (ws, wfd);
1449 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1450 GNUNET_SCHEDULER_NO_TASK, delay,
1451 NULL, ws, task, task_cls);
1452 GNUNET_NETWORK_fdset_destroy (ws);
1455 GNUNET_assert(GNUNET_NETWORK_get_fd(wfd) >= 0);
1456 return add_without_sets (delay,
1458 GNUNET_NETWORK_get_fd (wfd),
1466 * Schedule a new task to be run with a specified delay or when the
1467 * specified file descriptor is ready for reading. The delay can be
1468 * used as a timeout on the socket being ready. The task will be
1469 * scheduled for execution once either the delay has expired or the
1470 * socket operation is ready. It will be run with the priority of
1473 * @param delay when should this operation time out? Use
1474 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1475 * @param rfd read file-descriptor
1476 * @param task main function of the task
1477 * @param task_cls closure of task
1478 * @return unique task identifier for the job
1479 * only valid until "task" is started!
1481 GNUNET_SCHEDULER_TaskIdentifier
1482 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1483 const struct GNUNET_DISK_FileHandle * rfd,
1484 GNUNET_SCHEDULER_Task task, void *task_cls)
1487 struct GNUNET_NETWORK_FDSet *rs;
1488 GNUNET_SCHEDULER_TaskIdentifier ret;
1490 GNUNET_assert (rfd != NULL);
1491 rs = GNUNET_NETWORK_fdset_create ();
1492 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1493 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1494 GNUNET_SCHEDULER_NO_TASK, delay,
1495 rs, NULL, task, task_cls);
1496 GNUNET_NETWORK_fdset_destroy (rs);
1501 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1502 return add_without_sets (delay,
1513 * Schedule a new task to be run with a specified delay or when the
1514 * specified file descriptor is ready for writing. The delay can be
1515 * used as a timeout on the socket being ready. The task will be
1516 * scheduled for execution once either the delay has expired or the
1517 * socket operation is ready. It will be run with the priority of
1520 * @param delay when should this operation time out? Use
1521 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1522 * @param wfd write file-descriptor
1523 * @param task main function of the task
1524 * @param task_cls closure of task
1525 * @return unique task identifier for the job
1526 * only valid until "task" is started!
1528 GNUNET_SCHEDULER_TaskIdentifier
1529 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1530 const struct GNUNET_DISK_FileHandle * wfd,
1531 GNUNET_SCHEDULER_Task task, void *task_cls)
1534 struct GNUNET_NETWORK_FDSet *ws;
1535 GNUNET_SCHEDULER_TaskIdentifier ret;
1537 GNUNET_assert (wfd != NULL);
1538 ws = GNUNET_NETWORK_fdset_create ();
1539 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1540 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1541 GNUNET_SCHEDULER_NO_TASK,
1542 delay, NULL, ws, task, task_cls);
1543 GNUNET_NETWORK_fdset_destroy (ws);
1548 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1549 GNUNET_assert(fd >= 0);
1550 return add_without_sets (delay,
1562 * Schedule a new task to be run with a specified delay or when any of
1563 * the specified file descriptor sets is ready. The delay can be used
1564 * as a timeout on the socket(s) being ready. The task will be
1565 * scheduled for execution once either the delay has expired or any of
1566 * the socket operations is ready. This is the most general
1567 * function of the "add" family. Note that the "prerequisite_task"
1568 * must be satisfied in addition to any of the other conditions. In
1569 * other words, the task will be started when
1571 * (prerequisite-run)
1575 * || (shutdown-active && run-on-shutdown) )
1578 * @param prio how important is this task?
1579 * @param prerequisite_task run this task after the task with the given
1580 * task identifier completes (and any of our other
1581 * conditions, such as delay, read or write-readiness
1582 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1583 * on completion of other tasks.
1584 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1585 * which means that the task will only be run after we receive SIGTERM
1586 * @param rs set of file descriptors we want to read (can be NULL)
1587 * @param ws set of file descriptors we want to write (can be NULL)
1588 * @param task main function of the task
1589 * @param task_cls closure of task
1590 * @return unique task identifier for the job
1591 * only valid until "task" is started!
1593 GNUNET_SCHEDULER_TaskIdentifier
1594 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1595 GNUNET_SCHEDULER_TaskIdentifier
1597 struct GNUNET_TIME_Relative delay,
1598 const struct GNUNET_NETWORK_FDSet * rs,
1599 const struct GNUNET_NETWORK_FDSet * ws,
1600 GNUNET_SCHEDULER_Task task, void *task_cls)
1604 void *backtrace_array[MAX_TRACE_DEPTH];
1607 GNUNET_assert (active_task != NULL);
1608 GNUNET_assert (NULL != task);
1609 t = GNUNET_malloc (sizeof (struct Task));
1611 t->callback_cls = task_cls;
1613 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1614 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1620 t->read_set = GNUNET_NETWORK_fdset_create ();
1621 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1625 t->write_set = GNUNET_NETWORK_fdset_create ();
1626 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1630 t->start_time = GNUNET_TIME_absolute_get ();
1632 t->prereq_id = prerequisite_task;
1633 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1635 check_priority ((prio ==
1636 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority
1638 t->lifeness = current_lifeness;
1641 max_priority_added = GNUNET_MAX (max_priority_added,
1644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1645 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1650 for (i=0;i<t->num_backtrace_strings;i++)
1651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1652 "Task %llu trace %d: %s\n",
1655 t->backtrace_strings[i]);
1660 /* end of scheduler.c */