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_YES
44 * Obtain trace information for all scheduler calls that schedule tasks.
46 #define EXECINFO GNUNET_NO
49 * Depth of the traces collected via EXECINFO.
51 #define MAX_TRACE_DEPTH 50
54 #define DEBUG_TASKS GNUNET_NO
57 * Should we figure out which tasks are delayed for a while
58 * before they are run? (Consider using in combination with EXECINFO).
60 #define PROFILE_DELAYS GNUNET_NO
63 * Task that were in the queue for longer than this are reported if
64 * PROFILE_DELAYS is active.
66 #define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
69 * Linked list of pending tasks.
74 * This is a linked list.
79 * Function to run when ready.
81 GNUNET_SCHEDULER_Task callback;
84 * Closure for the callback.
89 * Set of file descriptors this task is waiting
90 * for for reading. Once ready, this is updated
91 * to reflect the set of file descriptors ready
94 struct GNUNET_NETWORK_FDSet *read_set;
97 * Set of file descriptors this task is waiting for for writing.
98 * Once ready, this is updated to reflect the set of file
99 * descriptors ready for operation.
101 struct GNUNET_NETWORK_FDSet *write_set;
104 * Unique task identifier.
106 GNUNET_SCHEDULER_TaskIdentifier id;
109 * Identifier of a prerequisite task.
111 GNUNET_SCHEDULER_TaskIdentifier prereq_id;
114 * Absolute timeout value for the task, or
115 * GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
117 struct GNUNET_TIME_Absolute timeout;
121 * When was the task scheduled?
123 struct GNUNET_TIME_Absolute start_time;
127 * Why is the task ready? Set after task is added to ready queue.
128 * Initially set to zero. All reasons that have already been
129 * satisfied (i.e. read or write ready) will be set over time.
131 enum GNUNET_SCHEDULER_Reason reason;
136 enum GNUNET_SCHEDULER_Priority priority;
139 * Set if we only wait for reading from a single FD, otherwise -1.
144 * Set if we only wait for writing to a single FD, otherwise -1.
150 * Array of strings which make up a backtrace from the point when this
151 * task was scheduled (essentially, who scheduled the task?)
153 char **backtrace_strings;
156 * Size of the backtrace_strings array
158 int num_backtrace_strings;
166 * List of tasks waiting for an event.
168 static struct Task *pending;
171 * List of tasks waiting ONLY for a timeout event.
172 * Sorted by timeout (earliest first). Used so that
173 * we do not traverse the list of these tasks when
174 * building select sets (we just look at the head
175 * to determine the respective timeout ONCE).
177 static struct Task *pending_timeout;
180 * Last inserted task waiting ONLY for a timeout event.
181 * Used to (heuristically) speed up insertion.
183 static struct Task *pending_timeout_last;
186 * ID of the task that is running right now.
188 static struct Task *active_task;
191 * List of tasks ready to run right now,
192 * grouped by importance.
194 static struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
197 * Identity of the last task queued. Incremented for each task to
198 * generate a unique task ID (it is virtually impossible to start
199 * more than 2^64 tasks during the lifetime of a process).
201 static GNUNET_SCHEDULER_TaskIdentifier last_id;
204 * Highest number so that all tasks with smaller identifiers
205 * have already completed. Also the lowest number of a task
206 * still waiting to be executed.
208 static GNUNET_SCHEDULER_TaskIdentifier lowest_pending_id;
211 * Number of tasks on the ready list.
213 static unsigned int ready_count;
216 * How many tasks have we run so far?
218 static unsigned long long tasks_run;
221 * Priority of the task running right now. Only
222 * valid while a task is running.
224 static enum GNUNET_SCHEDULER_Priority current_priority;
227 * Priority of the highest task added in the current select
230 static enum GNUNET_SCHEDULER_Priority max_priority_added;
233 * How 'nice' are we right now?
235 static int nice_level;
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 sched the scheduler
261 * @param id which task are we checking for
262 * @return GNUNET_YES if so, GNUNET_NO if not
265 is_pending (GNUNET_SCHEDULER_TaskIdentifier id)
268 enum GNUNET_SCHEDULER_Priority p;
269 GNUNET_SCHEDULER_TaskIdentifier min;
271 if (id < lowest_pending_id)
273 min = -1; /* maximum value */
283 pos = pending_timeout;
292 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
304 lowest_pending_id = min;
310 * Update all sets and timeout for select.
312 * @param sched the scheduler
313 * @param rs read-set, set to all FDs we would like to read (updated)
314 * @param ws write-set, set to all FDs we would like to write (updated)
315 * @param timeout next timeout (updated)
318 update_sets (struct GNUNET_NETWORK_FDSet *rs,
319 struct GNUNET_NETWORK_FDSet *ws,
320 struct GNUNET_TIME_Relative *timeout)
323 struct GNUNET_TIME_Absolute now;
324 struct GNUNET_TIME_Relative to;
326 now = GNUNET_TIME_absolute_get ();
327 pos = pending_timeout;
330 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
331 if (timeout->rel_value > to.rel_value)
333 if (pos->reason != 0)
334 *timeout = GNUNET_TIME_UNIT_ZERO;
339 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
340 (GNUNET_YES == is_pending (pos->prereq_id)))
345 if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
347 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
348 if (timeout->rel_value > to.rel_value)
351 if (pos->read_fd != -1)
352 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
353 if (pos->write_fd != -1)
354 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
355 if (pos->read_set != NULL)
356 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
357 if (pos->write_set != NULL)
358 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
359 if (pos->reason != 0)
360 *timeout = GNUNET_TIME_UNIT_ZERO;
367 * Check if the ready set overlaps with the set we want to have ready.
368 * If so, update the want set (set all FDs that are ready). If not,
371 * @param ready set that is ready
372 * @param want set that we want to be ready
373 * @return GNUNET_YES if there was some overlap
376 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
377 struct GNUNET_NETWORK_FDSet *want)
379 if ( (NULL == want) || (NULL == ready) )
381 if (GNUNET_NETWORK_fdset_overlap (ready, want))
383 /* copy all over (yes, there maybe unrelated bits,
384 but this should not hurt well-written clients) */
385 GNUNET_NETWORK_fdset_copy (want, ready);
393 * Check if the given task is eligible to run now.
394 * Also set the reason why it is eligible.
396 * @param sched the scheduler
397 * @param task task to check if it is ready
398 * @param now the current time
399 * @param rs set of FDs ready for reading
400 * @param ws set of FDs ready for writing
401 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
404 is_ready (struct Task *task,
405 struct GNUNET_TIME_Absolute now,
406 const struct GNUNET_NETWORK_FDSet *rs,
407 const struct GNUNET_NETWORK_FDSet *ws)
409 enum GNUNET_SCHEDULER_Reason reason;
411 reason = task->reason;
412 if (now.abs_value >= task->timeout.abs_value)
413 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
414 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
415 ( ( (task->read_fd != -1) &&
416 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd)) ) ||
417 (set_overlaps (rs, task->read_set) ) ) )
418 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
419 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
420 ( ( (task->write_fd != -1) &&
421 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)) ) ||
422 (set_overlaps (ws, task->write_set) ) ) )
423 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
425 return GNUNET_NO; /* not ready */
426 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
428 if (GNUNET_YES == is_pending (task->prereq_id))
430 task->reason = reason;
431 return GNUNET_NO; /* prereq waiting */
433 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
435 task->reason = reason;
441 * Put a task that is ready for execution into the ready queue.
443 * @param handle the scheduler
444 * @param task task ready for execution
447 queue_ready_task (struct Task *task)
449 enum GNUNET_SCHEDULER_Priority p = task->priority;
450 if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
451 p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
452 task->next = ready[check_priority (p)];
453 ready[check_priority (p)] = task;
459 * Check which tasks are ready and move them
460 * to the respective ready queue.
462 * @param handle the scheduler
463 * @param rs FDs ready for reading
464 * @param ws FDs ready for writing
467 check_ready (const struct GNUNET_NETWORK_FDSet *rs,
468 const struct GNUNET_NETWORK_FDSet *ws)
473 struct GNUNET_TIME_Absolute now;
475 now = GNUNET_TIME_absolute_get ();
477 pos = pending_timeout;
481 if (now.abs_value >= pos->timeout.abs_value)
482 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
483 if (0 == pos->reason)
485 pending_timeout = next;
486 if (pending_timeout_last == pos)
487 pending_timeout_last = NULL;
488 queue_ready_task (pos);
495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
496 "Checking readiness of task: %llu / %p\n",
497 pos->id, pos->callback_cls);
500 if (GNUNET_YES == is_ready (pos, now, rs, ws))
506 queue_ready_task (pos);
517 * Request the shutdown of a scheduler. Marks all currently
518 * pending tasks as ready because of shutdown. This will
519 * cause all tasks to run (as soon as possible, respecting
520 * priorities and prerequisite tasks). Note that tasks
521 * scheduled AFTER this call may still be delayed arbitrarily.
523 * @param sched the scheduler
526 GNUNET_SCHEDULER_shutdown ()
531 GNUNET_assert (active_task != NULL);
532 pos = pending_timeout;
535 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
536 /* we don't move the task into the ready queue yet; check_ready
537 will do that later, possibly adding additional
544 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
545 /* we don't move the task into the ready queue yet; check_ready
546 will do that later, possibly adding additional
550 for (i=0;i<GNUNET_SCHEDULER_PRIORITY_COUNT;i++)
555 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
556 /* we don't move the task into the ready queue yet; check_ready
557 will do that later, possibly adding additional
566 * Destroy a task (release associated resources)
568 * @param t task to destroy
571 destroy_task (struct Task *t)
573 if (NULL != t->read_set)
574 GNUNET_NETWORK_fdset_destroy (t->read_set);
575 if (NULL != t->write_set)
576 GNUNET_NETWORK_fdset_destroy (t->write_set);
578 GNUNET_free (t->backtrace_strings);
585 * Run at least one task in the highest-priority queue that is not
586 * empty. Keep running tasks until we are either no longer running
587 * "URGENT" tasks or until we have at least one "pending" task (which
588 * may become ready, hence we should select on it). Naturally, if
589 * there are no more ready tasks, we also return.
591 * @param sched the scheduler
592 * @param rs FDs ready for reading
593 * @param ws FDs ready for writing
596 run_ready (struct GNUNET_NETWORK_FDSet *rs,
597 struct GNUNET_NETWORK_FDSet *ws)
599 enum GNUNET_SCHEDULER_Priority p;
601 struct GNUNET_SCHEDULER_TaskContext tc;
603 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
606 if (ready_count == 0)
608 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
609 /* yes, p>0 is correct, 0 is "KEEP" which should
610 always be an empty queue (see assertion)! */
611 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
617 GNUNET_assert (pos != NULL); /* ready_count wrong? */
618 ready[p] = pos->next;
620 if (current_priority != pos->priority)
622 current_priority = pos->priority;
623 (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), pos->priority);
627 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
628 DELAY_THRESHOLD.rel_value)
630 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
631 "Task %llu took %llums to be scheduled\n",
633 (unsigned long long) GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value);
636 tc.reason = pos->reason;
637 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
638 if ( (pos->read_fd != -1) &&
639 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
640 GNUNET_NETWORK_fdset_set_native (rs,
642 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
643 if ( (pos->write_fd != -1) &&
644 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) )
645 GNUNET_NETWORK_fdset_set_native (ws,
647 if ( ( (tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
648 (pos->write_fd != -1) &&
649 (! GNUNET_NETWORK_fdset_test_native (ws,
651 abort (); // added to ready in previous select loop!
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
654 "Running task: %llu / %p\n", pos->id, pos->callback_cls);
656 pos->callback (pos->callback_cls, &tc);
659 for (i=0;i<pos->num_backtrace_strings;i++)
660 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
661 "Task %llu trace %d: %s\n",
664 pos->backtrace_strings[i]);
670 while ( (pending == NULL) || (p >= max_priority_added) );
674 * Pipe used to communicate shutdown via signal.
676 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
679 * Signal handler called for SIGPIPE.
689 * Signal handler called for signals that should cause us to shutdown.
692 sighandler_shutdown ()
695 int old_errno = errno; /* backup errno */
697 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
698 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE), &c,
705 * Initialize and run scheduler. This function will return when all
706 * tasks have completed. On systems with signals, receiving a SIGTERM
707 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
708 * to be run after the active task is complete. As a result, SIGTERM
709 * causes all active tasks to be scheduled with reason
710 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
711 * afterwards will execute normally!). Note that any particular signal
712 * will only shut down one scheduler; applications should always only
713 * create a single scheduler.
715 * @param task task to run immediately
716 * @param task_cls closure of task
719 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
721 struct GNUNET_NETWORK_FDSet *rs;
722 struct GNUNET_NETWORK_FDSet *ws;
723 struct GNUNET_TIME_Relative timeout;
725 struct GNUNET_SIGNAL_Context *shc_int;
726 struct GNUNET_SIGNAL_Context *shc_term;
728 struct GNUNET_SIGNAL_Context *shc_quit;
729 struct GNUNET_SIGNAL_Context *shc_hup;
730 struct GNUNET_SIGNAL_Context *shc_pipe;
732 unsigned long long last_tr;
733 unsigned int busy_wait_warning;
734 const struct GNUNET_DISK_FileHandle *pr;
737 GNUNET_assert (active_task == NULL);
738 rs = GNUNET_NETWORK_fdset_create ();
739 ws = GNUNET_NETWORK_fdset_create ();
740 GNUNET_assert (shutdown_pipe_handle == NULL);
741 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
742 GNUNET_assert (shutdown_pipe_handle != NULL);
743 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_READ);
744 GNUNET_assert (pr != NULL);
745 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
746 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
748 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
749 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
750 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
752 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
753 GNUNET_SCHEDULER_add_continuation (task,
755 GNUNET_SCHEDULER_REASON_STARTUP);
757 busy_wait_warning = 0;
758 while ((pending != NULL) ||
759 (pending_timeout != NULL) ||
762 GNUNET_NETWORK_fdset_zero (rs);
763 GNUNET_NETWORK_fdset_zero (ws);
764 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
765 update_sets (rs, ws, &timeout);
766 GNUNET_NETWORK_fdset_handle_set (rs, pr);
769 /* no blocking, more work already ready! */
770 timeout = GNUNET_TIME_UNIT_ZERO;
772 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
773 if (ret == GNUNET_SYSERR)
778 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
782 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid());
791 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
793 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
794 _("Looks like we're busy waiting...\n"));
795 sleep (1); /* mitigate */
797 check_ready (rs, ws);
799 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
801 /* consume the signal */
802 GNUNET_DISK_file_read (pr, &c, sizeof (c));
803 /* mark all active tasks as ready due to shutdown */
804 GNUNET_SCHEDULER_shutdown ();
806 if (last_tr == tasks_run)
813 busy_wait_warning = 0;
816 GNUNET_SIGNAL_handler_uninstall (shc_int);
817 GNUNET_SIGNAL_handler_uninstall (shc_term);
819 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
820 GNUNET_SIGNAL_handler_uninstall (shc_quit);
821 GNUNET_SIGNAL_handler_uninstall (shc_hup);
823 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
824 shutdown_pipe_handle = NULL;
825 GNUNET_NETWORK_fdset_destroy (rs);
826 GNUNET_NETWORK_fdset_destroy (ws);
831 * Obtain the reason code for why the current task was
832 * started. Will return the same value as
833 * the GNUNET_SCHEDULER_TaskContext's reason field.
835 * @param sched scheduler to query
836 * @return reason(s) why the current task is run
838 enum GNUNET_SCHEDULER_Reason
839 GNUNET_SCHEDULER_get_reason ()
841 GNUNET_assert (active_task != NULL);
842 return active_task->reason;
847 * Get information about the current load of this scheduler. Use this
848 * function to determine if an elective task should be added or simply
849 * dropped (if the decision should be made based on the number of
850 * tasks ready to run).
852 * @param sched scheduler to query
853 * @param p priority level to look at
854 * @return number of tasks pending right now
857 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
862 GNUNET_assert (active_task != NULL);
863 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
865 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
866 p = current_priority;
868 pos = ready[check_priority (p)];
879 * Cancel the task with the specified identifier.
880 * The task must not yet have run.
882 * @param sched scheduler to use
883 * @param task id of the task to cancel
884 * @return original closure of the task
887 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
891 enum GNUNET_SCHEDULER_Priority p;
895 GNUNET_assert (active_task != NULL);
918 if (pending_timeout_last == t)
919 pending_timeout_last = NULL;
925 GNUNET_assert (p < GNUNET_SCHEDULER_PRIORITY_COUNT);
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 sched scheduler to use
977 * @param task main function of the task
978 * @param task_cls closure for 'main'
979 * @param reason reason for task invocation
982 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task,
984 enum GNUNET_SCHEDULER_Reason reason)
988 void *backtrace_array[50];
991 GNUNET_assert (active_task != NULL);
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 sched scheduler to use
1023 * @param prerequisite_task run this task after the task with the given
1024 * task identifier completes (and any of our other
1025 * conditions, such as delay, read or write-readiness
1026 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1027 * on completion of other tasks (this will cause the task to run as
1028 * soon as possible).
1029 * @param task main function of the task
1030 * @param task_cls closure of task
1031 * @return unique task identifier for the job
1032 * only valid until "task" is started!
1034 GNUNET_SCHEDULER_TaskIdentifier
1035 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1036 GNUNET_SCHEDULER_Task task, void *task_cls)
1038 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1040 GNUNET_TIME_UNIT_ZERO,
1041 NULL, NULL, task, task_cls);
1046 * Schedule a new task to be run with a specified priority.
1048 * @param sched scheduler to use
1049 * @param prio how important is the new task?
1050 * @param task main function of the task
1051 * @param task_cls closure of task
1052 * @return unique task identifier for the job
1053 * only valid until "task" is started!
1055 GNUNET_SCHEDULER_TaskIdentifier
1056 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1057 GNUNET_SCHEDULER_Task task,
1060 return GNUNET_SCHEDULER_add_select (prio,
1061 GNUNET_SCHEDULER_NO_TASK,
1062 GNUNET_TIME_UNIT_ZERO,
1063 NULL, NULL, task, task_cls);
1069 * Schedule a new task to be run with a specified delay. The task
1070 * will be scheduled for execution once the delay has expired. It
1071 * will be run with the priority of the calling task.
1073 * @param sched scheduler to use
1074 * @param delay when should this operation time out? Use
1075 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1076 * @param task main function of the task
1077 * @param task_cls closure of task
1078 * @return unique task identifier for the job
1079 * only valid until "task" is started!
1081 GNUNET_SCHEDULER_TaskIdentifier
1082 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1083 GNUNET_SCHEDULER_Task task, void *task_cls)
1086 /* new, optimized version */
1091 void *backtrace_array[MAX_TRACE_DEPTH];
1094 GNUNET_assert (active_task != NULL);
1095 GNUNET_assert (NULL != task);
1096 t = GNUNET_malloc (sizeof (struct Task));
1098 t->callback_cls = task_cls;
1100 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1101 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1107 t->start_time = GNUNET_TIME_absolute_get ();
1109 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1110 t->priority = current_priority;
1111 /* try tail first (optimization in case we are
1112 appending to a long list of tasks with timeouts) */
1113 prev = pending_timeout_last;
1116 if (prev->timeout.abs_value > t->timeout.abs_value)
1119 pos = prev->next; /* heuristic success! */
1123 /* heuristic failed, do traversal of timeout list */
1124 pos = pending_timeout;
1126 while ( (pos != NULL) &&
1127 ( (pos->timeout.abs_value <= t->timeout.abs_value) ||
1128 (pos->reason != 0) ) )
1134 pending_timeout = t;
1138 /* hyper-optimization... */
1139 pending_timeout_last = t;
1142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1148 for (i=0;i<t->num_backtrace_strings;i++)
1149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1150 "Task %llu trace %d: %s\n",
1153 t->backtrace_strings[i]);
1158 /* unoptimized version */
1159 return GNUNET_SCHEDULER_add_select (sched,
1160 GNUNET_SCHEDULER_PRIORITY_KEEP,
1161 GNUNET_SCHEDULER_NO_TASK, delay,
1162 NULL, NULL, task, task_cls);
1169 * Schedule a new task to be run as soon as possible. The task
1170 * will be run with the priority of the calling task.
1172 * @param sched scheduler to use
1173 * @param task main function of the task
1174 * @param task_cls closure of task
1175 * @return unique task identifier for the job
1176 * only valid until "task" is started!
1178 GNUNET_SCHEDULER_TaskIdentifier
1179 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task,
1182 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1183 GNUNET_SCHEDULER_NO_TASK,
1184 GNUNET_TIME_UNIT_ZERO,
1185 NULL, NULL, task, task_cls);
1192 * Schedule a new task to be run with a specified delay or when any of
1193 * the specified file descriptor sets is ready. The delay can be used
1194 * as a timeout on the socket(s) being ready. The task will be
1195 * scheduled for execution once either the delay has expired or any of
1196 * the socket operations is ready. This is the most general
1197 * function of the "add" family. Note that the "prerequisite_task"
1198 * must be satisfied in addition to any of the other conditions. In
1199 * other words, the task will be started when
1201 * (prerequisite-run)
1205 * || shutdown-active )
1208 * @param sched scheduler to use
1209 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1210 * which means that the task will only be run after we receive SIGTERM
1211 * @param rfd file descriptor we want to read (can be -1)
1212 * @param wfd file descriptors we want to write (can be -1)
1213 * @param task main function of the task
1214 * @param task_cls closure of task
1215 * @return unique task identifier for the job
1216 * only valid until "task" is started!
1218 GNUNET_SCHEDULER_TaskIdentifier
1219 add_without_sets (struct GNUNET_TIME_Relative delay,
1222 GNUNET_SCHEDULER_Task task, void *task_cls)
1226 void *backtrace_array[MAX_TRACE_DEPTH];
1229 GNUNET_assert (active_task != NULL);
1230 GNUNET_assert (NULL != task);
1231 t = GNUNET_malloc (sizeof (struct Task));
1233 t->callback_cls = task_cls;
1235 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1236 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1242 t->start_time = GNUNET_TIME_absolute_get ();
1244 t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
1245 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1246 t->priority = check_priority (current_priority);
1249 max_priority_added = GNUNET_MAX (max_priority_added,
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1253 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1258 for (i=0;i<t->num_backtrace_strings;i++)
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1260 "Task %llu trace %d: %s\n",
1263 t->backtrace_strings[i]);
1271 * Schedule a new task to be run with a specified delay or when the
1272 * specified file descriptor is ready for reading. The delay can be
1273 * used as a timeout on the socket being ready. The task will be
1274 * scheduled for execution once either the delay has expired or the
1275 * socket operation is ready. It will be run with the priority of
1278 * @param sched scheduler to use
1279 * @param delay when should this operation time out? Use
1280 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1281 * @param rfd read file-descriptor
1282 * @param task main function of the task
1283 * @param task_cls closure of task
1284 * @return unique task identifier for the job
1285 * only valid until "task" is started!
1287 GNUNET_SCHEDULER_TaskIdentifier
1288 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1289 struct GNUNET_NETWORK_Handle * rfd,
1290 GNUNET_SCHEDULER_Task task, void *task_cls)
1292 return add_without_sets (delay,
1293 GNUNET_NETWORK_get_fd (rfd),
1301 * Schedule a new task to be run with a specified delay or when the
1302 * specified file descriptor is ready for writing. The delay can be
1303 * used as a timeout on the socket being ready. The task will be
1304 * scheduled for execution once either the delay has expired or the
1305 * socket operation is ready. It will be run with the priority of
1308 * @param sched scheduler to use
1309 * @param delay when should this operation time out? Use
1310 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1311 * @param wfd write file-descriptor
1312 * @param task main function of the task
1313 * @param task_cls closure of task
1314 * @return unique task identifier for the job
1315 * only valid until "task" is started!
1317 GNUNET_SCHEDULER_TaskIdentifier
1318 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1319 struct GNUNET_NETWORK_Handle * wfd,
1320 GNUNET_SCHEDULER_Task task, void *task_cls)
1322 return add_without_sets (delay,
1324 GNUNET_NETWORK_get_fd (wfd),
1331 * Schedule a new task to be run with a specified delay or when the
1332 * specified file descriptor is ready for reading. The delay can be
1333 * used as a timeout on the socket being ready. The task will be
1334 * scheduled for execution once either the delay has expired or the
1335 * socket operation is ready. It will be run with the priority of
1338 * @param sched scheduler to use
1339 * @param delay when should this operation time out? Use
1340 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1341 * @param rfd read file-descriptor
1342 * @param task main function of the task
1343 * @param task_cls closure of task
1344 * @return unique task identifier for the job
1345 * only valid until "task" is started!
1347 GNUNET_SCHEDULER_TaskIdentifier
1348 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1349 const struct GNUNET_DISK_FileHandle * rfd,
1350 GNUNET_SCHEDULER_Task task, void *task_cls)
1353 struct GNUNET_NETWORK_FDSet *rs;
1354 GNUNET_SCHEDULER_TaskIdentifier ret;
1356 GNUNET_assert (rfd != NULL);
1357 rs = GNUNET_NETWORK_fdset_create ();
1358 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1359 ret = GNUNET_SCHEDULER_add_select (sched,
1360 GNUNET_SCHEDULER_PRIORITY_KEEP,
1361 GNUNET_SCHEDULER_NO_TASK, delay,
1362 rs, NULL, task, task_cls);
1363 GNUNET_NETWORK_fdset_destroy (rs);
1368 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1369 return add_without_sets (delay,
1380 * Schedule a new task to be run with a specified delay or when the
1381 * specified file descriptor is ready for writing. The delay can be
1382 * used as a timeout on the socket being ready. The task will be
1383 * scheduled for execution once either the delay has expired or the
1384 * socket operation is ready. It will be run with the priority of
1387 * @param sched scheduler to use
1388 * @param delay when should this operation time out? Use
1389 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1390 * @param wfd write file-descriptor
1391 * @param task main function of the task
1392 * @param task_cls closure of task
1393 * @return unique task identifier for the job
1394 * only valid until "task" is started!
1396 GNUNET_SCHEDULER_TaskIdentifier
1397 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1398 const struct GNUNET_DISK_FileHandle * wfd,
1399 GNUNET_SCHEDULER_Task task, void *task_cls)
1402 struct GNUNET_NETWORK_FDSet *ws;
1403 GNUNET_SCHEDULER_TaskIdentifier ret;
1405 GNUNET_assert (wfd != NULL);
1406 ws = GNUNET_NETWORK_fdset_create ();
1407 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1408 ret = GNUNET_SCHEDULER_add_select (sched,
1409 GNUNET_SCHEDULER_PRIORITY_KEEP,
1410 GNUNET_SCHEDULER_NO_TASK,
1411 delay, NULL, ws, task, task_cls);
1412 GNUNET_NETWORK_fdset_destroy (ws);
1417 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1418 return add_without_sets (delay,
1430 * Schedule a new task to be run with a specified delay or when any of
1431 * the specified file descriptor sets is ready. The delay can be used
1432 * as a timeout on the socket(s) being ready. The task will be
1433 * scheduled for execution once either the delay has expired or any of
1434 * the socket operations is ready. This is the most general
1435 * function of the "add" family. Note that the "prerequisite_task"
1436 * must be satisfied in addition to any of the other conditions. In
1437 * other words, the task will be started when
1439 * (prerequisite-run)
1443 * || (shutdown-active && run-on-shutdown) )
1446 * @param sched scheduler to use
1447 * @param prio how important is this task?
1448 * @param prerequisite_task run this task after the task with the given
1449 * task identifier completes (and any of our other
1450 * conditions, such as delay, read or write-readiness
1451 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1452 * on completion of other tasks.
1453 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1454 * which means that the task will only be run after we receive SIGTERM
1455 * @param rs set of file descriptors we want to read (can be NULL)
1456 * @param ws set of file descriptors we want to write (can be NULL)
1457 * @param task main function of the task
1458 * @param task_cls closure of task
1459 * @return unique task identifier for the job
1460 * only valid until "task" is started!
1462 GNUNET_SCHEDULER_TaskIdentifier
1463 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1464 GNUNET_SCHEDULER_TaskIdentifier
1466 struct GNUNET_TIME_Relative delay,
1467 const struct GNUNET_NETWORK_FDSet * rs,
1468 const struct GNUNET_NETWORK_FDSet * ws,
1469 GNUNET_SCHEDULER_Task task, void *task_cls)
1473 void *backtrace_array[MAX_TRACE_DEPTH];
1476 GNUNET_assert (active_task != NULL);
1477 GNUNET_assert (NULL != task);
1478 t = GNUNET_malloc (sizeof (struct Task));
1480 t->callback_cls = task_cls;
1482 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1483 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1489 t->read_set = GNUNET_NETWORK_fdset_create ();
1490 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1494 t->write_set = GNUNET_NETWORK_fdset_create ();
1495 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1499 t->start_time = GNUNET_TIME_absolute_get ();
1501 t->prereq_id = prerequisite_task;
1502 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1504 check_priority ((prio ==
1505 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority
1509 max_priority_added = GNUNET_MAX (max_priority_added,
1512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1513 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1518 for (i=0;i<t->num_backtrace_strings;i++)
1519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1520 "Task %llu trace %d: %s\n",
1523 t->backtrace_strings[i]);
1528 /* end of scheduler.c */