2 This file is part of GNUnet
3 (C) 2009 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.
155 * Array of strings which make up a backtrace from the point when this
156 * task was scheduled (essentially, who scheduled the task?)
158 char **backtrace_strings;
161 * Size of the backtrace_strings array
163 int num_backtrace_strings;
171 * List of tasks waiting for an event.
173 static struct Task *pending;
176 * List of tasks waiting ONLY for a timeout event.
177 * Sorted by timeout (earliest first). Used so that
178 * we do not traverse the list of these tasks when
179 * building select sets (we just look at the head
180 * to determine the respective timeout ONCE).
182 static struct Task *pending_timeout;
185 * Last inserted task waiting ONLY for a timeout event.
186 * Used to (heuristically) speed up insertion.
188 static struct Task *pending_timeout_last;
191 * ID of the task that is running right now.
193 static struct Task *active_task;
196 * List of tasks ready to run right now,
197 * grouped by importance.
199 static struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
202 * Identity of the last task queued. Incremented for each task to
203 * generate a unique task ID (it is virtually impossible to start
204 * more than 2^64 tasks during the lifetime of a process).
206 static GNUNET_SCHEDULER_TaskIdentifier last_id;
209 * Highest number so that all tasks with smaller identifiers
210 * have already completed. Also the lowest number of a task
211 * still waiting to be executed.
213 static GNUNET_SCHEDULER_TaskIdentifier lowest_pending_id;
216 * Number of tasks on the ready list.
218 static unsigned int ready_count;
221 * How many tasks have we run so far?
223 static unsigned long long tasks_run;
226 * Priority of the task running right now. Only
227 * valid while a task is running.
229 static enum GNUNET_SCHEDULER_Priority current_priority;
232 * Priority of the highest task added in the current select
235 static enum GNUNET_SCHEDULER_Priority max_priority_added;
239 * Check that the given priority is legal (and return it).
241 * @param p priority value to check
242 * @return p on success, 0 on error
244 static enum GNUNET_SCHEDULER_Priority
245 check_priority (enum GNUNET_SCHEDULER_Priority p)
247 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
250 return 0; /* make compiler happy */
255 * Is a task with this identifier still pending? Also updates
256 * "lowest_pending_id" as a side-effect (for faster checks in the
257 * future), but only if the return value is "GNUNET_NO" (and
258 * the "lowest_pending_id" check failed).
260 * @param id which task are we checking for
261 * @return GNUNET_YES if so, GNUNET_NO if not
264 is_pending (GNUNET_SCHEDULER_TaskIdentifier id)
267 enum GNUNET_SCHEDULER_Priority p;
268 GNUNET_SCHEDULER_TaskIdentifier min;
270 if (id < lowest_pending_id)
272 min = -1; /* maximum value */
282 pos = pending_timeout;
291 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
303 lowest_pending_id = min;
309 * Update all sets and timeout for select.
311 * @param rs read-set, set to all FDs we would like to read (updated)
312 * @param ws write-set, set to all FDs we would like to write (updated)
313 * @param timeout next timeout (updated)
316 update_sets (struct GNUNET_NETWORK_FDSet *rs,
317 struct GNUNET_NETWORK_FDSet *ws,
318 struct GNUNET_TIME_Relative *timeout)
321 struct GNUNET_TIME_Absolute now;
322 struct GNUNET_TIME_Relative to;
324 now = GNUNET_TIME_absolute_get ();
325 pos = pending_timeout;
328 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
329 if (timeout->rel_value > to.rel_value)
331 if (pos->reason != 0)
332 *timeout = GNUNET_TIME_UNIT_ZERO;
337 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
338 (GNUNET_YES == is_pending (pos->prereq_id)))
343 if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
345 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
346 if (timeout->rel_value > to.rel_value)
349 if (pos->read_fd != -1)
350 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
351 if (pos->write_fd != -1)
352 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
353 if (pos->read_set != NULL)
354 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
355 if (pos->write_set != NULL)
356 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
357 if (pos->reason != 0)
358 *timeout = GNUNET_TIME_UNIT_ZERO;
365 * Check if the ready set overlaps with the set we want to have ready.
366 * If so, update the want set (set all FDs that are ready). If not,
369 * @param ready set that is ready
370 * @param want set that we want to be ready
371 * @return GNUNET_YES if there was some overlap
374 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
375 struct GNUNET_NETWORK_FDSet *want)
377 if ( (NULL == want) || (NULL == ready) )
379 if (GNUNET_NETWORK_fdset_overlap (ready, want))
381 /* copy all over (yes, there maybe unrelated bits,
382 but this should not hurt well-written clients) */
383 GNUNET_NETWORK_fdset_copy (want, ready);
391 * Check if the given task is eligible to run now.
392 * Also set the reason why it is eligible.
394 * @param task task to check if it is ready
395 * @param now the current time
396 * @param rs set of FDs ready for reading
397 * @param ws set of FDs ready for writing
398 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
401 is_ready (struct Task *task,
402 struct GNUNET_TIME_Absolute now,
403 const struct GNUNET_NETWORK_FDSet *rs,
404 const struct GNUNET_NETWORK_FDSet *ws)
406 enum GNUNET_SCHEDULER_Reason reason;
408 reason = task->reason;
409 if (now.abs_value >= task->timeout.abs_value)
410 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
411 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
412 ( ( (task->read_fd != -1) &&
413 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd)) ) ||
414 (set_overlaps (rs, task->read_set) ) ) )
415 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
416 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
417 ( ( (task->write_fd != -1) &&
418 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)) ) ||
419 (set_overlaps (ws, task->write_set) ) ) )
420 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
422 return GNUNET_NO; /* not ready */
423 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
425 if (GNUNET_YES == is_pending (task->prereq_id))
427 task->reason = reason;
428 return GNUNET_NO; /* prereq waiting */
430 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
432 task->reason = reason;
438 * Put a task that is ready for execution into the ready queue.
440 * @param task task ready for execution
443 queue_ready_task (struct Task *task)
445 enum GNUNET_SCHEDULER_Priority p = task->priority;
446 if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
447 p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
448 task->next = ready[check_priority (p)];
449 ready[check_priority (p)] = task;
455 * Check which tasks are ready and move them
456 * to the respective ready queue.
458 * @param rs FDs ready for reading
459 * @param ws FDs ready for writing
462 check_ready (const struct GNUNET_NETWORK_FDSet *rs,
463 const struct GNUNET_NETWORK_FDSet *ws)
468 struct GNUNET_TIME_Absolute now;
470 now = GNUNET_TIME_absolute_get ();
472 pos = pending_timeout;
476 if (now.abs_value >= pos->timeout.abs_value)
477 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
478 if (0 == pos->reason)
480 pending_timeout = next;
481 if (pending_timeout_last == pos)
482 pending_timeout_last = NULL;
483 queue_ready_task (pos);
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "Checking readiness of task: %llu / %p\n",
492 pos->id, pos->callback_cls);
495 if (GNUNET_YES == is_ready (pos, now, rs, ws))
501 queue_ready_task (pos);
512 * Request the shutdown of a scheduler. Marks all currently
513 * pending tasks as ready because of shutdown. This will
514 * cause all tasks to run (as soon as possible, respecting
515 * priorities and prerequisite tasks). Note that tasks
516 * scheduled AFTER this call may still be delayed arbitrarily.
519 GNUNET_SCHEDULER_shutdown ()
524 pos = pending_timeout;
527 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
528 /* we don't move the task into the ready queue yet; check_ready
529 will do that later, possibly adding additional
536 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
537 /* we don't move the task into the ready queue yet; check_ready
538 will do that later, possibly adding additional
542 for (i=0;i<GNUNET_SCHEDULER_PRIORITY_COUNT;i++)
547 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
548 /* we don't move the task into the ready queue yet; check_ready
549 will do that later, possibly adding additional
558 * Destroy a task (release associated resources)
560 * @param t task to destroy
563 destroy_task (struct Task *t)
565 if (NULL != t->read_set)
566 GNUNET_NETWORK_fdset_destroy (t->read_set);
567 if (NULL != t->write_set)
568 GNUNET_NETWORK_fdset_destroy (t->write_set);
570 GNUNET_free (t->backtrace_strings);
577 * Run at least one task in the highest-priority queue that is not
578 * empty. Keep running tasks until we are either no longer running
579 * "URGENT" tasks or until we have at least one "pending" task (which
580 * may become ready, hence we should select on it). Naturally, if
581 * there are no more ready tasks, we also return.
583 * @param rs FDs ready for reading
584 * @param ws FDs ready for writing
587 run_ready (struct GNUNET_NETWORK_FDSet *rs,
588 struct GNUNET_NETWORK_FDSet *ws)
590 enum GNUNET_SCHEDULER_Priority p;
592 struct GNUNET_SCHEDULER_TaskContext tc;
594 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
597 if (ready_count == 0)
599 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
600 /* yes, p>0 is correct, 0 is "KEEP" which should
601 always be an empty queue (see assertion)! */
602 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
608 GNUNET_assert (pos != NULL); /* ready_count wrong? */
609 ready[p] = pos->next;
611 if (current_priority != pos->priority)
613 current_priority = pos->priority;
614 (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
619 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
620 DELAY_THRESHOLD.rel_value)
622 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
623 "Task %llu took %llums to be scheduled\n",
625 (unsigned long long) GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value);
628 tc.reason = pos->reason;
629 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
630 if ( (pos->read_fd != -1) &&
631 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
632 GNUNET_NETWORK_fdset_set_native (rs,
634 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
635 if ( (pos->write_fd != -1) &&
636 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) )
637 GNUNET_NETWORK_fdset_set_native (ws,
639 if ( ( (tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
640 (pos->write_fd != -1) &&
641 (! GNUNET_NETWORK_fdset_test_native (ws,
643 abort (); // added to ready in previous select loop!
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "Running task: %llu / %p\n", pos->id, pos->callback_cls);
648 pos->callback (pos->callback_cls, &tc);
651 for (i=0;i<pos->num_backtrace_strings;i++)
652 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
653 "Task %llu trace %d: %s\n",
656 pos->backtrace_strings[i]);
662 while ( (pending == NULL) || (p >= max_priority_added) );
666 * Pipe used to communicate shutdown via signal.
668 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
671 * Signal handler called for SIGPIPE.
681 * Signal handler called for signals that should cause us to shutdown.
684 sighandler_shutdown ()
687 int old_errno = errno; /* backup errno */
689 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
690 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE), &c,
697 * Initialize and run scheduler. This function will return when all
698 * tasks have completed. On systems with signals, receiving a SIGTERM
699 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
700 * to be run after the active task is complete. As a result, SIGTERM
701 * causes all active tasks to be scheduled with reason
702 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
703 * afterwards will execute normally!). Note that any particular signal
704 * will only shut down one scheduler; applications should always only
705 * create a single scheduler.
707 * @param task task to run immediately
708 * @param task_cls closure of task
711 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
713 struct GNUNET_NETWORK_FDSet *rs;
714 struct GNUNET_NETWORK_FDSet *ws;
715 struct GNUNET_TIME_Relative timeout;
717 struct GNUNET_SIGNAL_Context *shc_int;
718 struct GNUNET_SIGNAL_Context *shc_term;
720 struct GNUNET_SIGNAL_Context *shc_quit;
721 struct GNUNET_SIGNAL_Context *shc_hup;
722 struct GNUNET_SIGNAL_Context *shc_pipe;
724 unsigned long long last_tr;
725 unsigned int busy_wait_warning;
726 const struct GNUNET_DISK_FileHandle *pr;
729 GNUNET_assert (active_task == NULL);
730 rs = GNUNET_NETWORK_fdset_create ();
731 ws = GNUNET_NETWORK_fdset_create ();
732 GNUNET_assert (shutdown_pipe_handle == NULL);
733 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
734 GNUNET_assert (shutdown_pipe_handle != NULL);
735 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_READ);
736 GNUNET_assert (pr != NULL);
737 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
738 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
740 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
741 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
742 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
744 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
745 GNUNET_SCHEDULER_add_continuation (task,
747 GNUNET_SCHEDULER_REASON_STARTUP);
748 #if ENABLE_WINDOWS_WORKAROUNDS
749 GNUNET_SCHEDULER_add_continuation (&GNUNET_OS_install_parent_control_handler,
750 NULL, GNUNET_SCHEDULER_REASON_STARTUP);
753 busy_wait_warning = 0;
754 while ((pending != NULL) ||
755 (pending_timeout != NULL) ||
758 GNUNET_NETWORK_fdset_zero (rs);
759 GNUNET_NETWORK_fdset_zero (ws);
760 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
761 update_sets (rs, ws, &timeout);
762 GNUNET_NETWORK_fdset_handle_set (rs, pr);
765 /* no blocking, more work already ready! */
766 timeout = GNUNET_TIME_UNIT_ZERO;
768 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
769 if (ret == GNUNET_SYSERR)
774 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
778 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid());
781 if (0 != system (lsof))
782 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "system");
788 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
790 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
791 _("Looks like we're busy waiting...\n"));
792 sleep (1); /* mitigate */
794 check_ready (rs, ws);
796 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
798 /* consume the signal */
799 GNUNET_DISK_file_read (pr, &c, sizeof (c));
800 /* mark all active tasks as ready due to shutdown */
801 GNUNET_SCHEDULER_shutdown ();
803 if (last_tr == tasks_run)
810 busy_wait_warning = 0;
813 GNUNET_SIGNAL_handler_uninstall (shc_int);
814 GNUNET_SIGNAL_handler_uninstall (shc_term);
816 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
817 GNUNET_SIGNAL_handler_uninstall (shc_quit);
818 GNUNET_SIGNAL_handler_uninstall (shc_hup);
820 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
821 shutdown_pipe_handle = NULL;
822 GNUNET_NETWORK_fdset_destroy (rs);
823 GNUNET_NETWORK_fdset_destroy (ws);
828 * Obtain the reason code for why the current task was
829 * started. Will return the same value as
830 * the GNUNET_SCHEDULER_TaskContext's reason field.
832 * @return reason(s) why the current task is run
834 enum GNUNET_SCHEDULER_Reason
835 GNUNET_SCHEDULER_get_reason ()
837 GNUNET_assert (active_task != NULL);
838 return active_task->reason;
843 * Get information about the current load of this scheduler. Use this
844 * function to determine if an elective task should be added or simply
845 * dropped (if the decision should be made based on the number of
846 * tasks ready to run).
848 * @param p priority level to look at
849 * @return number of tasks pending right now
852 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
857 GNUNET_assert (active_task != NULL);
858 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
860 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
861 p = current_priority;
863 pos = ready[check_priority (p)];
874 * Cancel the task with the specified identifier.
875 * The task must not yet have run.
877 * @param task id of the task to cancel
878 * @return original closure of the task
881 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
885 enum GNUNET_SCHEDULER_Priority p;
889 GNUNET_assert (active_task != NULL);
912 if (pending_timeout_last == t)
913 pending_timeout_last = NULL;
919 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT)
921 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
922 _("Attempt to cancel dead task %llu!\n"),
923 (unsigned long long) task);
949 pending_timeout = t->next;
959 prev->next = t->next;
961 ret = t->callback_cls;
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "Canceling task: %llu / %p\n", task, t->callback_cls);
972 * Continue the current execution with the given function. This is
973 * similar to the other "add" functions except that there is no delay
974 * and the reason code can be specified.
976 * @param task main function of the task
977 * @param task_cls closure for 'main'
978 * @param reason reason for task invocation
981 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task,
983 enum GNUNET_SCHEDULER_Reason reason)
987 void *backtrace_array[50];
990 GNUNET_assert ( (active_task != NULL) ||
991 (reason == GNUNET_SCHEDULER_REASON_STARTUP) );
992 t = GNUNET_malloc (sizeof (struct Task));
994 t->num_backtrace_strings = backtrace(backtrace_array, 50);
995 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1000 t->callback_cls = task_cls;
1003 t->start_time = GNUNET_TIME_absolute_get ();
1006 t->priority = current_priority;
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1009 "Adding continuation task: %llu / %p\n",
1010 t->id, t->callback_cls);
1012 queue_ready_task (t);
1018 * Schedule a new task to be run after the specified prerequisite task
1019 * has completed. It will be run with the priority of the calling
1022 * @param prerequisite_task run this task after the task with the given
1023 * task identifier completes (and any of our other
1024 * conditions, such as delay, read or write-readiness
1025 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1026 * on completion of other tasks (this will cause the task to run as
1027 * soon as possible).
1028 * @param task main function of the task
1029 * @param task_cls closure of task
1030 * @return unique task identifier for the job
1031 * only valid until "task" is started!
1033 GNUNET_SCHEDULER_TaskIdentifier
1034 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1035 GNUNET_SCHEDULER_Task task, void *task_cls)
1037 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1039 GNUNET_TIME_UNIT_ZERO,
1040 NULL, NULL, task, task_cls);
1045 * Schedule a new task to be run with a specified priority.
1047 * @param prio how important is the new task?
1048 * @param task main function of the task
1049 * @param task_cls closure of task
1050 * @return unique task identifier for the job
1051 * only valid until "task" is started!
1053 GNUNET_SCHEDULER_TaskIdentifier
1054 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1055 GNUNET_SCHEDULER_Task task,
1058 return GNUNET_SCHEDULER_add_select (prio,
1059 GNUNET_SCHEDULER_NO_TASK,
1060 GNUNET_TIME_UNIT_ZERO,
1061 NULL, NULL, task, task_cls);
1067 * Schedule a new task to be run with a specified delay. The task
1068 * will be scheduled for execution once the delay has expired. It
1069 * will be run with the priority of the calling task.
1071 * @param delay when should this operation time out? Use
1072 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1073 * @param task main function of the task
1074 * @param task_cls closure of task
1075 * @return unique task identifier for the job
1076 * only valid until "task" is started!
1078 GNUNET_SCHEDULER_TaskIdentifier
1079 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1080 GNUNET_SCHEDULER_Task task, void *task_cls)
1083 /* new, optimized version */
1088 void *backtrace_array[MAX_TRACE_DEPTH];
1091 GNUNET_assert (active_task != NULL);
1092 GNUNET_assert (NULL != task);
1093 t = GNUNET_malloc (sizeof (struct Task));
1095 t->callback_cls = task_cls;
1097 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1098 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1104 t->start_time = GNUNET_TIME_absolute_get ();
1106 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1107 t->priority = current_priority;
1108 /* try tail first (optimization in case we are
1109 appending to a long list of tasks with timeouts) */
1110 prev = pending_timeout_last;
1113 if (prev->timeout.abs_value > t->timeout.abs_value)
1116 pos = prev->next; /* heuristic success! */
1120 /* heuristic failed, do traversal of timeout list */
1121 pos = pending_timeout;
1123 while ( (pos != NULL) &&
1124 ( (pos->timeout.abs_value <= t->timeout.abs_value) ||
1125 (pos->reason != 0) ) )
1131 pending_timeout = t;
1135 /* hyper-optimization... */
1136 pending_timeout_last = t;
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1145 for (i=0;i<t->num_backtrace_strings;i++)
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 "Task %llu trace %d: %s\n",
1150 t->backtrace_strings[i]);
1155 /* unoptimized version */
1156 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1157 GNUNET_SCHEDULER_NO_TASK, delay,
1158 NULL, NULL, task, task_cls);
1165 * Schedule a new task to be run as soon as possible. The task
1166 * will be run with the priority of the calling task.
1168 * @param task main function of the task
1169 * @param task_cls closure of task
1170 * @return unique task identifier for the job
1171 * only valid until "task" is started!
1173 GNUNET_SCHEDULER_TaskIdentifier
1174 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task,
1177 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1178 GNUNET_SCHEDULER_NO_TASK,
1179 GNUNET_TIME_UNIT_ZERO,
1180 NULL, NULL, task, task_cls);
1187 * Schedule a new task to be run with a specified delay or when any of
1188 * the specified file descriptor sets is ready. The delay can be used
1189 * as a timeout on the socket(s) being ready. The task will be
1190 * scheduled for execution once either the delay has expired or any of
1191 * the socket operations is ready. This is the most general
1192 * function of the "add" family. Note that the "prerequisite_task"
1193 * must be satisfied in addition to any of the other conditions. In
1194 * other words, the task will be started when
1196 * (prerequisite-run)
1200 * || shutdown-active )
1203 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1204 * which means that the task will only be run after we receive SIGTERM
1205 * @param rfd file descriptor we want to read (can be -1)
1206 * @param wfd file descriptors we want to write (can be -1)
1207 * @param task main function of the task
1208 * @param task_cls closure of task
1209 * @return unique task identifier for the job
1210 * only valid until "task" is started!
1213 GNUNET_SCHEDULER_TaskIdentifier
1214 add_without_sets (struct GNUNET_TIME_Relative delay,
1217 GNUNET_SCHEDULER_Task task, void *task_cls)
1221 void *backtrace_array[MAX_TRACE_DEPTH];
1224 GNUNET_assert (active_task != NULL);
1225 GNUNET_assert (NULL != task);
1226 t = GNUNET_malloc (sizeof (struct Task));
1228 t->callback_cls = task_cls;
1230 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1231 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1236 int flags = fcntl(rfd, F_GETFD);
1237 if (flags == -1 && errno == EBADF)
1239 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", rfd);
1243 for (i=0;i<t->num_backtrace_strings;i++)
1244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1246 t->backtrace_strings[i]);
1253 int flags = fcntl(wfd, F_GETFD);
1254 if (flags == -1 && errno == EBADF)
1256 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", wfd);
1260 for (i=0;i<t->num_backtrace_strings;i++)
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1263 t->backtrace_strings[i]);
1270 GNUNET_assert(wfd >= -1);
1274 t->start_time = GNUNET_TIME_absolute_get ();
1276 t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
1277 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1278 t->priority = check_priority (current_priority);
1281 max_priority_added = GNUNET_MAX (max_priority_added,
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1285 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1290 for (i=0;i<t->num_backtrace_strings;i++)
1291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1292 "Task %llu trace %d: %s\n",
1295 t->backtrace_strings[i]);
1304 * Schedule a new task to be run with a specified delay or when the
1305 * specified file descriptor is ready for reading. The delay can be
1306 * used as a timeout on the socket being ready. The task will be
1307 * scheduled for execution once either the delay has expired or the
1308 * socket operation is ready. It will be run with the priority of
1311 * @param delay when should this operation time out? Use
1312 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1313 * @param rfd read file-descriptor
1314 * @param task main function of the task
1315 * @param task_cls closure of task
1316 * @return unique task identifier for the job
1317 * only valid until "task" is started!
1319 GNUNET_SCHEDULER_TaskIdentifier
1320 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1321 struct GNUNET_NETWORK_Handle * rfd,
1322 GNUNET_SCHEDULER_Task task, void *task_cls)
1325 struct GNUNET_NETWORK_FDSet *rs;
1326 GNUNET_SCHEDULER_TaskIdentifier ret;
1328 GNUNET_assert (rfd != NULL);
1329 rs = GNUNET_NETWORK_fdset_create ();
1330 GNUNET_NETWORK_fdset_set (rs, rfd);
1331 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1332 GNUNET_SCHEDULER_NO_TASK, delay,
1333 rs, NULL, task, task_cls);
1334 GNUNET_NETWORK_fdset_destroy (rs);
1337 return add_without_sets (delay,
1338 GNUNET_NETWORK_get_fd (rfd),
1347 * Schedule a new task to be run with a specified delay or when the
1348 * specified file descriptor is ready for writing. The delay can be
1349 * used as a timeout on the socket being ready. The task will be
1350 * scheduled for execution once either the delay has expired or the
1351 * socket operation is ready. It will be run with the priority of
1354 * @param delay when should this operation time out? Use
1355 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1356 * @param wfd write file-descriptor
1357 * @param task main function of the task
1358 * @param task_cls closure of task
1359 * @return unique task identifier for the job
1360 * only valid until "task" is started!
1362 GNUNET_SCHEDULER_TaskIdentifier
1363 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1364 struct GNUNET_NETWORK_Handle * wfd,
1365 GNUNET_SCHEDULER_Task task, void *task_cls)
1368 struct GNUNET_NETWORK_FDSet *ws;
1369 GNUNET_SCHEDULER_TaskIdentifier ret;
1371 GNUNET_assert (wfd != NULL);
1372 ws = GNUNET_NETWORK_fdset_create ();
1373 GNUNET_NETWORK_fdset_set (ws, wfd);
1374 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1375 GNUNET_SCHEDULER_NO_TASK, delay,
1376 NULL, ws, task, task_cls);
1377 GNUNET_NETWORK_fdset_destroy (ws);
1380 GNUNET_assert(GNUNET_NETWORK_get_fd(wfd) >= 0);
1381 return add_without_sets (delay,
1383 GNUNET_NETWORK_get_fd (wfd),
1391 * Schedule a new task to be run with a specified delay or when the
1392 * specified file descriptor is ready for reading. The delay can be
1393 * used as a timeout on the socket being ready. The task will be
1394 * scheduled for execution once either the delay has expired or the
1395 * socket operation is ready. It will be run with the priority of
1398 * @param delay when should this operation time out? Use
1399 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1400 * @param rfd read file-descriptor
1401 * @param task main function of the task
1402 * @param task_cls closure of task
1403 * @return unique task identifier for the job
1404 * only valid until "task" is started!
1406 GNUNET_SCHEDULER_TaskIdentifier
1407 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1408 const struct GNUNET_DISK_FileHandle * rfd,
1409 GNUNET_SCHEDULER_Task task, void *task_cls)
1412 struct GNUNET_NETWORK_FDSet *rs;
1413 GNUNET_SCHEDULER_TaskIdentifier ret;
1415 GNUNET_assert (rfd != NULL);
1416 rs = GNUNET_NETWORK_fdset_create ();
1417 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1418 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1419 GNUNET_SCHEDULER_NO_TASK, delay,
1420 rs, NULL, task, task_cls);
1421 GNUNET_NETWORK_fdset_destroy (rs);
1426 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1427 return add_without_sets (delay,
1438 * Schedule a new task to be run with a specified delay or when the
1439 * specified file descriptor is ready for writing. The delay can be
1440 * used as a timeout on the socket being ready. The task will be
1441 * scheduled for execution once either the delay has expired or the
1442 * socket operation is ready. It will be run with the priority of
1445 * @param delay when should this operation time out? Use
1446 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1447 * @param wfd write file-descriptor
1448 * @param task main function of the task
1449 * @param task_cls closure of task
1450 * @return unique task identifier for the job
1451 * only valid until "task" is started!
1453 GNUNET_SCHEDULER_TaskIdentifier
1454 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1455 const struct GNUNET_DISK_FileHandle * wfd,
1456 GNUNET_SCHEDULER_Task task, void *task_cls)
1459 struct GNUNET_NETWORK_FDSet *ws;
1460 GNUNET_SCHEDULER_TaskIdentifier ret;
1462 GNUNET_assert (wfd != NULL);
1463 ws = GNUNET_NETWORK_fdset_create ();
1464 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1465 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1466 GNUNET_SCHEDULER_NO_TASK,
1467 delay, NULL, ws, task, task_cls);
1468 GNUNET_NETWORK_fdset_destroy (ws);
1473 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1474 GNUNET_assert(fd >= 0);
1475 return add_without_sets (delay,
1487 * Schedule a new task to be run with a specified delay or when any of
1488 * the specified file descriptor sets is ready. The delay can be used
1489 * as a timeout on the socket(s) being ready. The task will be
1490 * scheduled for execution once either the delay has expired or any of
1491 * the socket operations is ready. This is the most general
1492 * function of the "add" family. Note that the "prerequisite_task"
1493 * must be satisfied in addition to any of the other conditions. In
1494 * other words, the task will be started when
1496 * (prerequisite-run)
1500 * || (shutdown-active && run-on-shutdown) )
1503 * @param prio how important is this task?
1504 * @param prerequisite_task run this task after the task with the given
1505 * task identifier completes (and any of our other
1506 * conditions, such as delay, read or write-readiness
1507 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1508 * on completion of other tasks.
1509 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1510 * which means that the task will only be run after we receive SIGTERM
1511 * @param rs set of file descriptors we want to read (can be NULL)
1512 * @param ws set of file descriptors we want to write (can be NULL)
1513 * @param task main function of the task
1514 * @param task_cls closure of task
1515 * @return unique task identifier for the job
1516 * only valid until "task" is started!
1518 GNUNET_SCHEDULER_TaskIdentifier
1519 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1520 GNUNET_SCHEDULER_TaskIdentifier
1522 struct GNUNET_TIME_Relative delay,
1523 const struct GNUNET_NETWORK_FDSet * rs,
1524 const struct GNUNET_NETWORK_FDSet * ws,
1525 GNUNET_SCHEDULER_Task task, void *task_cls)
1529 void *backtrace_array[MAX_TRACE_DEPTH];
1532 GNUNET_assert (active_task != NULL);
1533 GNUNET_assert (NULL != task);
1534 t = GNUNET_malloc (sizeof (struct Task));
1536 t->callback_cls = task_cls;
1538 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1539 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1545 t->read_set = GNUNET_NETWORK_fdset_create ();
1546 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1550 t->write_set = GNUNET_NETWORK_fdset_create ();
1551 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1555 t->start_time = GNUNET_TIME_absolute_get ();
1557 t->prereq_id = prerequisite_task;
1558 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1560 check_priority ((prio ==
1561 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority
1565 max_priority_added = GNUNET_MAX (max_priority_added,
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1569 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1574 for (i=0;i<t->num_backtrace_strings;i++)
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1576 "Task %llu trace %d: %s\n",
1579 t->backtrace_strings[i]);
1584 /* end of scheduler.c */