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 * Function to use as a select() in the scheduler.
250 * If NULL, we use GNUNET_NETWORK_socket_select ().
252 static GNUNET_SCHEDULER_select scheduler_select;
255 * Closure for 'scheduler_select'.
257 static void *scheduler_select_cls;
260 * Sets the select function to use in the scheduler (scheduler_select).
262 * @param new_select new select function to use
263 * @return previously used select function, NULL for default
266 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
267 void *new_select_cls)
269 scheduler_select = new_select;
270 scheduler_select_cls = new_select_cls;
275 * Check that the given priority is legal (and return it).
277 * @param p priority value to check
278 * @return p on success, 0 on error
280 static enum GNUNET_SCHEDULER_Priority
281 check_priority (enum GNUNET_SCHEDULER_Priority p)
283 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
286 return 0; /* make compiler happy */
291 * Is a task with this identifier still pending? Also updates
292 * "lowest_pending_id" as a side-effect (for faster checks in the
293 * future), but only if the return value is "GNUNET_NO" (and
294 * the "lowest_pending_id" check failed).
296 * @param id which task are we checking for
297 * @return GNUNET_YES if so, GNUNET_NO if not
300 is_pending (GNUNET_SCHEDULER_TaskIdentifier id)
303 enum GNUNET_SCHEDULER_Priority p;
304 GNUNET_SCHEDULER_TaskIdentifier min;
306 if (id < lowest_pending_id)
308 min = -1; /* maximum value */
318 pos = pending_timeout;
327 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
339 lowest_pending_id = min;
345 * Update all sets and timeout for select.
347 * @param rs read-set, set to all FDs we would like to read (updated)
348 * @param ws write-set, set to all FDs we would like to write (updated)
349 * @param timeout next timeout (updated)
352 update_sets (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws,
353 struct GNUNET_TIME_Relative *timeout)
356 struct GNUNET_TIME_Absolute now;
357 struct GNUNET_TIME_Relative to;
359 now = GNUNET_TIME_absolute_get ();
360 pos = pending_timeout;
363 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
364 if (timeout->rel_value > to.rel_value)
366 if (pos->reason != 0)
367 *timeout = GNUNET_TIME_UNIT_ZERO;
372 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
373 (GNUNET_YES == is_pending (pos->prereq_id)))
378 if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
380 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
381 if (timeout->rel_value > to.rel_value)
384 if (pos->read_fd != -1)
385 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
386 if (pos->write_fd != -1)
387 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
388 if (pos->read_set != NULL)
389 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
390 if (pos->write_set != NULL)
391 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
392 if (pos->reason != 0)
393 *timeout = GNUNET_TIME_UNIT_ZERO;
400 * Check if the ready set overlaps with the set we want to have ready.
401 * If so, update the want set (set all FDs that are ready). If not,
404 * @param ready set that is ready
405 * @param want set that we want to be ready
406 * @return GNUNET_YES if there was some overlap
409 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
410 struct GNUNET_NETWORK_FDSet *want)
412 if ((NULL == want) || (NULL == ready))
414 if (GNUNET_NETWORK_fdset_overlap (ready, want))
416 /* copy all over (yes, there maybe unrelated bits,
417 * but this should not hurt well-written clients) */
418 GNUNET_NETWORK_fdset_copy (want, ready);
426 * Check if the given task is eligible to run now.
427 * Also set the reason why it is eligible.
429 * @param task task to check if it is ready
430 * @param now the current time
431 * @param rs set of FDs ready for reading
432 * @param ws set of FDs ready for writing
433 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
436 is_ready (struct Task *task, struct GNUNET_TIME_Absolute now,
437 const struct GNUNET_NETWORK_FDSet *rs,
438 const struct GNUNET_NETWORK_FDSet *ws)
440 enum GNUNET_SCHEDULER_Reason reason;
442 reason = task->reason;
443 if (now.abs_value >= task->timeout.abs_value)
444 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
445 if ((0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
446 (((task->read_fd != -1) &&
447 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd))) ||
448 (set_overlaps (rs, task->read_set))))
449 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
450 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
451 (((task->write_fd != -1) &&
452 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)))
453 || (set_overlaps (ws, task->write_set))))
454 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
456 return GNUNET_NO; /* not ready */
457 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
459 if (GNUNET_YES == is_pending (task->prereq_id))
461 task->reason = reason;
462 return GNUNET_NO; /* prereq waiting */
464 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
466 task->reason = reason;
472 * Put a task that is ready for execution into the ready queue.
474 * @param task task ready for execution
477 queue_ready_task (struct Task *task)
479 enum GNUNET_SCHEDULER_Priority p = task->priority;
481 if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
482 p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
483 task->next = ready[check_priority (p)];
484 ready[check_priority (p)] = task;
490 * Check which tasks are ready and move them
491 * to the respective ready queue.
493 * @param rs FDs ready for reading
494 * @param ws FDs ready for writing
497 check_ready (const struct GNUNET_NETWORK_FDSet *rs,
498 const struct GNUNET_NETWORK_FDSet *ws)
503 struct GNUNET_TIME_Absolute now;
505 now = GNUNET_TIME_absolute_get ();
507 pos = pending_timeout;
511 if (now.abs_value >= pos->timeout.abs_value)
512 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
513 if (0 == pos->reason)
515 pending_timeout = next;
516 if (pending_timeout_last == pos)
517 pending_timeout_last = NULL;
518 queue_ready_task (pos);
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
526 "Checking readiness of task: %llu / %p\n", pos->id,
530 if (GNUNET_YES == is_ready (pos, now, rs, ws))
536 queue_ready_task (pos);
547 * Request the shutdown of a scheduler. Marks all currently
548 * pending tasks as ready because of shutdown. This will
549 * cause all tasks to run (as soon as possible, respecting
550 * priorities and prerequisite tasks). Note that tasks
551 * scheduled AFTER this call may still be delayed arbitrarily.
554 GNUNET_SCHEDULER_shutdown ()
559 pos = pending_timeout;
562 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
563 /* we don't move the task into the ready queue yet; check_ready
564 * will do that later, possibly adding additional
565 * readiness-factors */
571 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
572 /* we don't move the task into the ready queue yet; check_ready
573 * will do that later, possibly adding additional
574 * readiness-factors */
577 for (i = 0; i < GNUNET_SCHEDULER_PRIORITY_COUNT; i++)
582 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
583 /* we don't move the task into the ready queue yet; check_ready
584 * will do that later, possibly adding additional
585 * readiness-factors */
593 * Destroy a task (release associated resources)
595 * @param t task to destroy
598 destroy_task (struct Task *t)
600 if (NULL != t->read_set)
601 GNUNET_NETWORK_fdset_destroy (t->read_set);
602 if (NULL != t->write_set)
603 GNUNET_NETWORK_fdset_destroy (t->write_set);
605 GNUNET_free (t->backtrace_strings);
612 * Run at least one task in the highest-priority queue that is not
613 * empty. Keep running tasks until we are either no longer running
614 * "URGENT" tasks or until we have at least one "pending" task (which
615 * may become ready, hence we should select on it). Naturally, if
616 * there are no more ready tasks, we also return.
618 * @param rs FDs ready for reading
619 * @param ws FDs ready for writing
622 run_ready (struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws)
624 enum GNUNET_SCHEDULER_Priority p;
626 struct GNUNET_SCHEDULER_TaskContext tc;
628 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
631 if (ready_count == 0)
633 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
634 /* yes, p>0 is correct, 0 is "KEEP" which should
635 * always be an empty queue (see assertion)! */
636 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
642 GNUNET_assert (pos != NULL); /* ready_count wrong? */
643 ready[p] = pos->next;
645 if (current_priority != pos->priority)
647 current_priority = pos->priority;
648 (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
651 current_lifeness = pos->lifeness;
654 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
655 DELAY_THRESHOLD.rel_value)
657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
658 "Task %llu took %llums to be scheduled\n", pos->id,
660 GNUNET_TIME_absolute_get_duration
661 (pos->start_time).rel_value);
664 tc.reason = pos->reason;
665 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
666 if ((pos->read_fd != -1) &&
667 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)))
668 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
669 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
670 if ((pos->write_fd != -1) &&
671 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
672 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
673 if (((tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
674 (pos->write_fd != -1) &&
675 (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd)))
676 abort (); // added to ready in previous select loop!
678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Running task: %llu / %p\n", pos->id,
681 pos->callback (pos->callback_cls, &tc);
685 for (i = 0; i < pos->num_backtrace_strings; i++)
686 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Task %llu trace %d: %s\n", pos->id,
687 i, pos->backtrace_strings[i]);
693 while ((pending == NULL) || (p >= max_priority_added));
697 * Pipe used to communicate shutdown via signal.
699 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
702 * Signal handler called for SIGPIPE.
712 * Signal handler called for signals that should cause us to shutdown.
715 sighandler_shutdown ()
718 int old_errno = errno; /* backup errno */
720 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
721 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
728 * Check if the system is still life. Trigger shutdown if we
729 * have tasks, but none of them give us lifeness.
731 * @return GNUNET_OK to continue the main loop,
741 for (t = pending; NULL != t; t = t->next)
742 if (t->lifeness == GNUNET_YES)
744 for (t = pending_timeout; NULL != t; t = t->next)
745 if (t->lifeness == GNUNET_YES)
747 if ((NULL != pending) || (NULL != pending_timeout))
749 GNUNET_SCHEDULER_shutdown ();
757 * Initialize and run scheduler. This function will return when all
758 * tasks have completed. On systems with signals, receiving a SIGTERM
759 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
760 * to be run after the active task is complete. As a result, SIGTERM
761 * causes all active tasks to be scheduled with reason
762 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
763 * afterwards will execute normally!). Note that any particular signal
764 * will only shut down one scheduler; applications should always only
765 * create a single scheduler.
767 * @param task task to run immediately
768 * @param task_cls closure of task
771 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
773 struct GNUNET_NETWORK_FDSet *rs;
774 struct GNUNET_NETWORK_FDSet *ws;
775 struct GNUNET_TIME_Relative timeout;
777 struct GNUNET_SIGNAL_Context *shc_int;
778 struct GNUNET_SIGNAL_Context *shc_term;
781 struct GNUNET_SIGNAL_Context *shc_quit;
782 struct GNUNET_SIGNAL_Context *shc_hup;
783 struct GNUNET_SIGNAL_Context *shc_pipe;
785 unsigned long long last_tr;
786 unsigned int busy_wait_warning;
787 const struct GNUNET_DISK_FileHandle *pr;
790 GNUNET_assert (active_task == NULL);
791 rs = GNUNET_NETWORK_fdset_create ();
792 ws = GNUNET_NETWORK_fdset_create ();
793 GNUNET_assert (shutdown_pipe_handle == NULL);
794 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
795 GNUNET_assert (shutdown_pipe_handle != NULL);
796 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
797 GNUNET_DISK_PIPE_END_READ);
798 GNUNET_assert (pr != NULL);
799 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
800 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
802 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
803 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
804 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
806 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
807 current_lifeness = GNUNET_YES;
808 GNUNET_SCHEDULER_add_continuation (task, task_cls,
809 GNUNET_SCHEDULER_REASON_STARTUP);
810 #if ENABLE_WINDOWS_WORKAROUNDS
811 active_task = (void *) (long) -1; /* force passing of sanity check */
812 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
813 &GNUNET_OS_install_parent_control_handler,
818 busy_wait_warning = 0;
819 while (GNUNET_OK == check_lifeness ())
821 GNUNET_NETWORK_fdset_zero (rs);
822 GNUNET_NETWORK_fdset_zero (ws);
823 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
824 update_sets (rs, ws, &timeout);
825 GNUNET_NETWORK_fdset_handle_set (rs, pr);
828 /* no blocking, more work already ready! */
829 timeout = GNUNET_TIME_UNIT_ZERO;
831 if (NULL == scheduler_select)
832 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
834 ret = scheduler_select (scheduler_select_cls, rs, ws, NULL, timeout);
835 if (ret == GNUNET_SYSERR)
840 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
845 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
848 if (0 != system (lsof))
849 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "system");
855 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
857 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
858 _("Looks like we're busy waiting...\n"));
859 sleep (1); /* mitigate */
861 check_ready (rs, ws);
863 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
865 /* consume the signal */
866 GNUNET_DISK_file_read (pr, &c, sizeof (c));
867 /* mark all active tasks as ready due to shutdown */
868 GNUNET_SCHEDULER_shutdown ();
870 if (last_tr == tasks_run)
877 busy_wait_warning = 0;
880 GNUNET_SIGNAL_handler_uninstall (shc_int);
881 GNUNET_SIGNAL_handler_uninstall (shc_term);
883 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
884 GNUNET_SIGNAL_handler_uninstall (shc_quit);
885 GNUNET_SIGNAL_handler_uninstall (shc_hup);
887 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
888 shutdown_pipe_handle = NULL;
889 GNUNET_NETWORK_fdset_destroy (rs);
890 GNUNET_NETWORK_fdset_destroy (ws);
895 * Obtain the reason code for why the current task was
896 * started. Will return the same value as
897 * the GNUNET_SCHEDULER_TaskContext's reason field.
899 * @return reason(s) why the current task is run
901 enum GNUNET_SCHEDULER_Reason
902 GNUNET_SCHEDULER_get_reason ()
904 GNUNET_assert (active_task != NULL);
905 return active_task->reason;
910 * Get information about the current load of this scheduler. Use this
911 * function to determine if an elective task should be added or simply
912 * dropped (if the decision should be made based on the number of
913 * tasks ready to run).
915 * @param p priority level to look at
916 * @return number of tasks pending right now
919 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
924 GNUNET_assert (active_task != NULL);
925 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
927 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
928 p = current_priority;
930 pos = ready[check_priority (p)];
941 * Cancel the task with the specified identifier.
942 * The task must not yet have run.
944 * @param task id of the task to cancel
945 * @return original closure of the task
948 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
952 enum GNUNET_SCHEDULER_Priority p;
956 GNUNET_assert (active_task != NULL);
979 if (pending_timeout_last == t)
980 pending_timeout_last = NULL;
986 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT)
988 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
989 _("Attempt to cancel dead task %llu!\n"),
990 (unsigned long long) task);
1016 pending_timeout = t->next;
1026 prev->next = t->next;
1028 ret = t->callback_cls;
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Canceling task: %llu / %p\n", task,
1039 * Continue the current execution with the given function. This is
1040 * similar to the other "add" functions except that there is no delay
1041 * and the reason code can be specified.
1043 * @param task main function of the task
1044 * @param task_cls closure for 'main'
1045 * @param reason reason for task invocation
1048 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls,
1049 enum GNUNET_SCHEDULER_Reason reason)
1054 void *backtrace_array[50];
1057 GNUNET_assert (NULL != task);
1058 GNUNET_assert ((active_task != NULL) ||
1059 (reason == GNUNET_SCHEDULER_REASON_STARTUP));
1060 t = GNUNET_malloc (sizeof (struct Task));
1062 t->num_backtrace_strings = backtrace (backtrace_array, 50);
1063 t->backtrace_strings =
1064 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1069 t->callback_cls = task_cls;
1072 t->start_time = GNUNET_TIME_absolute_get ();
1075 t->priority = current_priority;
1076 t->lifeness = current_lifeness;
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding continuation task: %llu / %p\n",
1079 t->id, t->callback_cls);
1081 queue_ready_task (t);
1087 * Schedule a new task to be run after the specified prerequisite task
1088 * has completed. It will be run with the priority of the calling
1091 * @param prerequisite_task run this task after the task with the given
1092 * task identifier completes (and any of our other
1093 * conditions, such as delay, read or write-readiness
1094 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1095 * on completion of other tasks (this will cause the task to run as
1096 * soon as possible).
1097 * @param task main function of the task
1098 * @param task_cls closure of task
1099 * @return unique task identifier for the job
1100 * only valid until "task" is started!
1102 GNUNET_SCHEDULER_TaskIdentifier
1103 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1104 GNUNET_SCHEDULER_Task task, void *task_cls)
1106 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1107 prerequisite_task, GNUNET_TIME_UNIT_ZERO,
1108 NULL, NULL, task, task_cls);
1113 * Schedule a new task to be run with a specified priority.
1115 * @param prio how important is the new task?
1116 * @param task main function of the task
1117 * @param task_cls closure of task
1118 * @return unique task identifier for the job
1119 * only valid until "task" is started!
1121 GNUNET_SCHEDULER_TaskIdentifier
1122 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1123 GNUNET_SCHEDULER_Task task, void *task_cls)
1125 return GNUNET_SCHEDULER_add_select (prio, GNUNET_SCHEDULER_NO_TASK,
1126 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
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 priority of the calling task.
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)
1149 /* new, optimized version */
1155 void *backtrace_array[MAX_TRACE_DEPTH];
1158 GNUNET_assert (active_task != NULL);
1159 GNUNET_assert (NULL != task);
1160 t = GNUNET_malloc (sizeof (struct Task));
1162 t->callback_cls = task_cls;
1164 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1165 t->backtrace_strings =
1166 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1172 t->start_time = GNUNET_TIME_absolute_get ();
1174 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1175 t->priority = current_priority;
1176 t->lifeness = current_lifeness;
1177 /* try tail first (optimization in case we are
1178 * appending to a long list of tasks with timeouts) */
1179 prev = pending_timeout_last;
1182 if (prev->timeout.abs_value > t->timeout.abs_value)
1185 pos = prev->next; /* heuristic success! */
1189 /* heuristic failed, do traversal of timeout list */
1190 pos = pending_timeout;
1192 while ((pos != NULL) &&
1193 ((pos->timeout.abs_value <= t->timeout.abs_value) ||
1194 (pos->reason != 0)))
1200 pending_timeout = t;
1204 /* hyper-optimization... */
1205 pending_timeout_last = t;
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1214 for (i = 0; i < t->num_backtrace_strings; i++)
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1216 t->backtrace_strings[i]);
1221 /* unoptimized version */
1222 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1223 GNUNET_SCHEDULER_NO_TASK, delay, NULL,
1224 NULL, task, task_cls);
1231 * Schedule a new task to be run as soon as possible. The task
1232 * will be run with the priority of the calling task.
1234 * @param task main function of the task
1235 * @param task_cls closure of task
1236 * @return unique task identifier for the job
1237 * only valid until "task" is started!
1239 GNUNET_SCHEDULER_TaskIdentifier
1240 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task, void *task_cls)
1242 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1243 GNUNET_SCHEDULER_NO_TASK,
1244 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1250 * Schedule a new task to be run as soon as possible with the
1251 * (transitive) ignore-shutdown flag either explicitly set or
1252 * explicitly enabled. This task (and all tasks created from it,
1253 * other than by another call to this function) will either count or
1254 * not count for the 'lifeness' of the process. This API is only
1255 * useful in a few special cases.
1257 * @param lifeness GNUNET_YES if the task counts for lifeness, GNUNET_NO if not.
1258 * @param task main function of the task
1259 * @param task_cls closure of task
1260 * @return unique task identifier for the job
1261 * only valid until "task" is started!
1263 GNUNET_SCHEDULER_TaskIdentifier
1264 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1265 GNUNET_SCHEDULER_Task task,
1268 GNUNET_SCHEDULER_TaskIdentifier ret;
1271 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1272 GNUNET_SCHEDULER_NO_TASK,
1273 GNUNET_TIME_UNIT_ZERO, NULL, NULL, task,
1275 GNUNET_assert (pending->id == ret);
1276 pending->lifeness = lifeness;
1284 * Schedule a new task to be run with a specified delay or when any of
1285 * the specified file descriptor sets is ready. The delay can be used
1286 * as a timeout on the socket(s) being ready. The task will be
1287 * scheduled for execution once either the delay has expired or any of
1288 * the socket operations is ready. This is the most general
1289 * function of the "add" family. Note that the "prerequisite_task"
1290 * must be satisfied in addition to any of the other conditions. In
1291 * other words, the task will be started when
1293 * (prerequisite-run)
1297 * || shutdown-active )
1300 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1301 * which means that the task will only be run after we receive SIGTERM
1302 * @param rfd file descriptor we want to read (can be -1)
1303 * @param wfd file descriptors we want to write (can be -1)
1304 * @param task main function of the task
1305 * @param task_cls closure of task
1306 * @return unique task identifier for the job
1307 * only valid until "task" is started!
1310 GNUNET_SCHEDULER_TaskIdentifier
1311 add_without_sets (struct GNUNET_TIME_Relative delay, int rfd, int wfd,
1312 GNUNET_SCHEDULER_Task task, void *task_cls)
1317 void *backtrace_array[MAX_TRACE_DEPTH];
1320 GNUNET_assert (active_task != NULL);
1321 GNUNET_assert (NULL != task);
1322 t = GNUNET_malloc (sizeof (struct Task));
1324 t->callback_cls = task_cls;
1326 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1327 t->backtrace_strings =
1328 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1333 int flags = fcntl (rfd, F_GETFD);
1335 if (flags == -1 && errno == EBADF)
1337 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n",
1342 for (i = 0; i < t->num_backtrace_strings; i++)
1343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n",
1344 t->backtrace_strings[i]);
1351 int flags = fcntl (wfd, F_GETFD);
1353 if (flags == -1 && errno == EBADF)
1355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n",
1360 for (i = 0; i < t->num_backtrace_strings; i++)
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trace: %s\n",
1362 t->backtrace_strings[i]);
1369 GNUNET_assert (wfd >= -1);
1373 t->start_time = GNUNET_TIME_absolute_get ();
1375 t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
1376 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1377 t->priority = check_priority (current_priority);
1378 t->lifeness = current_lifeness;
1381 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1389 for (i = 0; i < t->num_backtrace_strings; i++)
1390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1391 t->backtrace_strings[i]);
1400 * Schedule a new task to be run with a specified delay or when the
1401 * specified file descriptor is ready for reading. The delay can be
1402 * used as a timeout on the socket being ready. The task will be
1403 * scheduled for execution once either the delay has expired or the
1404 * socket operation is ready. It will be run with the priority of
1407 * @param delay when should this operation time out? Use
1408 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1409 * @param rfd read file-descriptor
1410 * @param task main function of the task
1411 * @param task_cls closure of task
1412 * @return unique task identifier for the job
1413 * only valid until "task" is started!
1415 GNUNET_SCHEDULER_TaskIdentifier
1416 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1417 struct GNUNET_NETWORK_Handle *rfd,
1418 GNUNET_SCHEDULER_Task task, void *task_cls)
1421 struct GNUNET_NETWORK_FDSet *rs;
1422 GNUNET_SCHEDULER_TaskIdentifier ret;
1424 GNUNET_assert (rfd != NULL);
1425 rs = GNUNET_NETWORK_fdset_create ();
1426 GNUNET_NETWORK_fdset_set (rs, rfd);
1428 GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1429 GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL,
1431 GNUNET_NETWORK_fdset_destroy (rs);
1434 return add_without_sets (delay, GNUNET_NETWORK_get_fd (rfd), -1, task,
1441 * Schedule a new task to be run with a specified delay or when the
1442 * specified file descriptor is ready for writing. The delay can be
1443 * used as a timeout on the socket being ready. The task will be
1444 * scheduled for execution once either the delay has expired or the
1445 * socket operation is ready. It will be run with the priority of
1448 * @param delay when should this operation time out? Use
1449 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1450 * @param wfd write file-descriptor
1451 * @param task main function of the task
1452 * @param task_cls closure of task
1453 * @return unique task identifier for the job
1454 * only valid until "task" is started!
1456 GNUNET_SCHEDULER_TaskIdentifier
1457 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1458 struct GNUNET_NETWORK_Handle *wfd,
1459 GNUNET_SCHEDULER_Task task, void *task_cls)
1462 struct GNUNET_NETWORK_FDSet *ws;
1463 GNUNET_SCHEDULER_TaskIdentifier ret;
1465 GNUNET_assert (wfd != NULL);
1466 ws = GNUNET_NETWORK_fdset_create ();
1467 GNUNET_NETWORK_fdset_set (ws, wfd);
1469 GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1470 GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws,
1472 GNUNET_NETWORK_fdset_destroy (ws);
1475 GNUNET_assert (GNUNET_NETWORK_get_fd (wfd) >= 0);
1476 return add_without_sets (delay, -1, GNUNET_NETWORK_get_fd (wfd), task,
1483 * Schedule a new task to be run with a specified delay or when the
1484 * specified file descriptor is ready for reading. The delay can be
1485 * used as a timeout on the socket being ready. The task will be
1486 * scheduled for execution once either the delay has expired or the
1487 * socket operation is ready. It will be run with the priority of
1490 * @param delay when should this operation time out? Use
1491 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1492 * @param rfd read file-descriptor
1493 * @param task main function of the task
1494 * @param task_cls closure of task
1495 * @return unique task identifier for the job
1496 * only valid until "task" is started!
1498 GNUNET_SCHEDULER_TaskIdentifier
1499 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1500 const struct GNUNET_DISK_FileHandle *rfd,
1501 GNUNET_SCHEDULER_Task task, void *task_cls)
1504 struct GNUNET_NETWORK_FDSet *rs;
1505 GNUNET_SCHEDULER_TaskIdentifier ret;
1507 GNUNET_assert (rfd != NULL);
1508 rs = GNUNET_NETWORK_fdset_create ();
1509 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1511 GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1512 GNUNET_SCHEDULER_NO_TASK, delay, rs, NULL,
1514 GNUNET_NETWORK_fdset_destroy (rs);
1519 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1520 return add_without_sets (delay, fd, -1, task, task_cls);
1527 * Schedule a new task to be run with a specified delay or when the
1528 * specified file descriptor is ready for writing. The delay can be
1529 * used as a timeout on the socket being ready. The task will be
1530 * scheduled for execution once either the delay has expired or the
1531 * socket operation is ready. It will be run with the priority of
1534 * @param delay when should this operation time out? Use
1535 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1536 * @param wfd write file-descriptor
1537 * @param task main function of the task
1538 * @param task_cls closure of task
1539 * @return unique task identifier for the job
1540 * only valid until "task" is started!
1542 GNUNET_SCHEDULER_TaskIdentifier
1543 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1544 const struct GNUNET_DISK_FileHandle *wfd,
1545 GNUNET_SCHEDULER_Task task, void *task_cls)
1548 struct GNUNET_NETWORK_FDSet *ws;
1549 GNUNET_SCHEDULER_TaskIdentifier ret;
1551 GNUNET_assert (wfd != NULL);
1552 ws = GNUNET_NETWORK_fdset_create ();
1553 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1555 GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1556 GNUNET_SCHEDULER_NO_TASK, delay, NULL, ws,
1558 GNUNET_NETWORK_fdset_destroy (ws);
1563 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1564 GNUNET_assert (fd >= 0);
1565 return add_without_sets (delay, -1, fd, task, task_cls);
1573 * Schedule a new task to be run with a specified delay or when any of
1574 * the specified file descriptor sets is ready. The delay can be used
1575 * as a timeout on the socket(s) being ready. The task will be
1576 * scheduled for execution once either the delay has expired or any of
1577 * the socket operations is ready. This is the most general
1578 * function of the "add" family. Note that the "prerequisite_task"
1579 * must be satisfied in addition to any of the other conditions. In
1580 * other words, the task will be started when
1582 * (prerequisite-run)
1586 * || (shutdown-active && run-on-shutdown) )
1589 * @param prio how important is this task?
1590 * @param prerequisite_task run this task after the task with the given
1591 * task identifier completes (and any of our other
1592 * conditions, such as delay, read or write-readiness
1593 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1594 * on completion of other tasks.
1595 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1596 * which means that the task will only be run after we receive SIGTERM
1597 * @param rs set of file descriptors we want to read (can be NULL)
1598 * @param ws set of file descriptors we want to write (can be NULL)
1599 * @param task main function of the task
1600 * @param task_cls closure of task
1601 * @return unique task identifier for the job
1602 * only valid until "task" is started!
1604 GNUNET_SCHEDULER_TaskIdentifier
1605 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1606 GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1607 struct GNUNET_TIME_Relative delay,
1608 const struct GNUNET_NETWORK_FDSet *rs,
1609 const struct GNUNET_NETWORK_FDSet *ws,
1610 GNUNET_SCHEDULER_Task task, void *task_cls)
1615 void *backtrace_array[MAX_TRACE_DEPTH];
1618 GNUNET_assert (active_task != NULL);
1619 GNUNET_assert (NULL != task);
1620 t = GNUNET_malloc (sizeof (struct Task));
1622 t->callback_cls = task_cls;
1624 t->num_backtrace_strings = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1625 t->backtrace_strings =
1626 backtrace_symbols (backtrace_array, t->num_backtrace_strings);
1632 t->read_set = GNUNET_NETWORK_fdset_create ();
1633 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1637 t->write_set = GNUNET_NETWORK_fdset_create ();
1638 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1642 t->start_time = GNUNET_TIME_absolute_get ();
1644 t->prereq_id = prerequisite_task;
1645 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1647 check_priority ((prio ==
1648 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1650 t->lifeness = current_lifeness;
1653 max_priority_added = GNUNET_MAX (max_priority_added, t->priority);
1655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding task: %llu / %p\n", t->id,
1661 for (i = 0; i < t->num_backtrace_strings; i++)
1662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Task %llu trace %d: %s\n", t->id, i,
1663 t->backtrace_strings[i]);
1668 /* end of scheduler.c */