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;
234 * Check that the given priority is legal (and return it).
236 * @param p priority value to check
237 * @return p on success, 0 on error
239 static enum GNUNET_SCHEDULER_Priority
240 check_priority (enum GNUNET_SCHEDULER_Priority p)
242 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
245 return 0; /* make compiler happy */
250 * Is a task with this identifier still pending? Also updates
251 * "lowest_pending_id" as a side-effect (for faster checks in the
252 * future), but only if the return value is "GNUNET_NO" (and
253 * the "lowest_pending_id" check failed).
255 * @param id which task are we checking for
256 * @return GNUNET_YES if so, GNUNET_NO if not
259 is_pending (GNUNET_SCHEDULER_TaskIdentifier id)
262 enum GNUNET_SCHEDULER_Priority p;
263 GNUNET_SCHEDULER_TaskIdentifier min;
265 if (id < lowest_pending_id)
267 min = -1; /* maximum value */
277 pos = pending_timeout;
286 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
298 lowest_pending_id = min;
304 * Update all sets and timeout for select.
306 * @param rs read-set, set to all FDs we would like to read (updated)
307 * @param ws write-set, set to all FDs we would like to write (updated)
308 * @param timeout next timeout (updated)
311 update_sets (struct GNUNET_NETWORK_FDSet *rs,
312 struct GNUNET_NETWORK_FDSet *ws,
313 struct GNUNET_TIME_Relative *timeout)
316 struct GNUNET_TIME_Absolute now;
317 struct GNUNET_TIME_Relative to;
319 now = GNUNET_TIME_absolute_get ();
320 pos = pending_timeout;
323 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
324 if (timeout->rel_value > to.rel_value)
326 if (pos->reason != 0)
327 *timeout = GNUNET_TIME_UNIT_ZERO;
332 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
333 (GNUNET_YES == is_pending (pos->prereq_id)))
338 if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
340 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
341 if (timeout->rel_value > to.rel_value)
344 if (pos->read_fd != -1)
345 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
346 if (pos->write_fd != -1)
347 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
348 if (pos->read_set != NULL)
349 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
350 if (pos->write_set != NULL)
351 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
352 if (pos->reason != 0)
353 *timeout = GNUNET_TIME_UNIT_ZERO;
360 * Check if the ready set overlaps with the set we want to have ready.
361 * If so, update the want set (set all FDs that are ready). If not,
364 * @param ready set that is ready
365 * @param want set that we want to be ready
366 * @return GNUNET_YES if there was some overlap
369 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
370 struct GNUNET_NETWORK_FDSet *want)
372 if ( (NULL == want) || (NULL == ready) )
374 if (GNUNET_NETWORK_fdset_overlap (ready, want))
376 /* copy all over (yes, there maybe unrelated bits,
377 but this should not hurt well-written clients) */
378 GNUNET_NETWORK_fdset_copy (want, ready);
386 * Check if the given task is eligible to run now.
387 * Also set the reason why it is eligible.
389 * @param task task to check if it is ready
390 * @param now the current time
391 * @param rs set of FDs ready for reading
392 * @param ws set of FDs ready for writing
393 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
396 is_ready (struct Task *task,
397 struct GNUNET_TIME_Absolute now,
398 const struct GNUNET_NETWORK_FDSet *rs,
399 const struct GNUNET_NETWORK_FDSet *ws)
401 enum GNUNET_SCHEDULER_Reason reason;
403 reason = task->reason;
404 if (now.abs_value >= task->timeout.abs_value)
405 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
406 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
407 ( ( (task->read_fd != -1) &&
408 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd)) ) ||
409 (set_overlaps (rs, task->read_set) ) ) )
410 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
411 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
412 ( ( (task->write_fd != -1) &&
413 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)) ) ||
414 (set_overlaps (ws, task->write_set) ) ) )
415 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
417 return GNUNET_NO; /* not ready */
418 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
420 if (GNUNET_YES == is_pending (task->prereq_id))
422 task->reason = reason;
423 return GNUNET_NO; /* prereq waiting */
425 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
427 task->reason = reason;
433 * Put a task that is ready for execution into the ready queue.
435 * @param handle the scheduler
436 * @param task task ready for execution
439 queue_ready_task (struct Task *task)
441 enum GNUNET_SCHEDULER_Priority p = task->priority;
442 if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
443 p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
444 task->next = ready[check_priority (p)];
445 ready[check_priority (p)] = task;
451 * Check which tasks are ready and move them
452 * to the respective ready queue.
454 * @param handle the scheduler
455 * @param rs FDs ready for reading
456 * @param ws FDs ready for writing
459 check_ready (const struct GNUNET_NETWORK_FDSet *rs,
460 const struct GNUNET_NETWORK_FDSet *ws)
465 struct GNUNET_TIME_Absolute now;
467 now = GNUNET_TIME_absolute_get ();
469 pos = pending_timeout;
473 if (now.abs_value >= pos->timeout.abs_value)
474 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
475 if (0 == pos->reason)
477 pending_timeout = next;
478 if (pending_timeout_last == pos)
479 pending_timeout_last = NULL;
480 queue_ready_task (pos);
487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
488 "Checking readiness of task: %llu / %p\n",
489 pos->id, pos->callback_cls);
492 if (GNUNET_YES == is_ready (pos, now, rs, ws))
498 queue_ready_task (pos);
509 * Request the shutdown of a scheduler. Marks all currently
510 * pending tasks as ready because of shutdown. This will
511 * cause all tasks to run (as soon as possible, respecting
512 * priorities and prerequisite tasks). Note that tasks
513 * scheduled AFTER this call may still be delayed arbitrarily.
515 GNUNET_SCHEDULER_shutdown ()
520 pos = pending_timeout;
523 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
524 /* we don't move the task into the ready queue yet; check_ready
525 will do that later, possibly adding additional
532 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
533 /* we don't move the task into the ready queue yet; check_ready
534 will do that later, possibly adding additional
538 for (i=0;i<GNUNET_SCHEDULER_PRIORITY_COUNT;i++)
543 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
544 /* we don't move the task into the ready queue yet; check_ready
545 will do that later, possibly adding additional
554 * Destroy a task (release associated resources)
556 * @param t task to destroy
559 destroy_task (struct Task *t)
561 if (NULL != t->read_set)
562 GNUNET_NETWORK_fdset_destroy (t->read_set);
563 if (NULL != t->write_set)
564 GNUNET_NETWORK_fdset_destroy (t->write_set);
566 GNUNET_free (t->backtrace_strings);
573 * Run at least one task in the highest-priority queue that is not
574 * empty. Keep running tasks until we are either no longer running
575 * "URGENT" tasks or until we have at least one "pending" task (which
576 * may become ready, hence we should select on it). Naturally, if
577 * there are no more ready tasks, we also return.
579 * @param rs FDs ready for reading
580 * @param ws FDs ready for writing
583 run_ready (struct GNUNET_NETWORK_FDSet *rs,
584 struct GNUNET_NETWORK_FDSet *ws)
586 enum GNUNET_SCHEDULER_Priority p;
588 struct GNUNET_SCHEDULER_TaskContext tc;
590 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
593 if (ready_count == 0)
595 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
596 /* yes, p>0 is correct, 0 is "KEEP" which should
597 always be an empty queue (see assertion)! */
598 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
604 GNUNET_assert (pos != NULL); /* ready_count wrong? */
605 ready[p] = pos->next;
607 if (current_priority != pos->priority)
609 current_priority = pos->priority;
610 (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), pos->priority);
614 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
615 DELAY_THRESHOLD.rel_value)
617 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
618 "Task %llu took %llums to be scheduled\n",
620 (unsigned long long) GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value);
623 tc.reason = pos->reason;
624 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
625 if ( (pos->read_fd != -1) &&
626 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
627 GNUNET_NETWORK_fdset_set_native (rs,
629 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
630 if ( (pos->write_fd != -1) &&
631 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) )
632 GNUNET_NETWORK_fdset_set_native (ws,
634 if ( ( (tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
635 (pos->write_fd != -1) &&
636 (! GNUNET_NETWORK_fdset_test_native (ws,
638 abort (); // added to ready in previous select loop!
640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
641 "Running task: %llu / %p\n", pos->id, pos->callback_cls);
643 pos->callback (pos->callback_cls, &tc);
646 for (i=0;i<pos->num_backtrace_strings;i++)
647 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
648 "Task %llu trace %d: %s\n",
651 pos->backtrace_strings[i]);
657 while ( (pending == NULL) || (p >= max_priority_added) );
661 * Pipe used to communicate shutdown via signal.
663 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
666 * Signal handler called for SIGPIPE.
676 * Signal handler called for signals that should cause us to shutdown.
679 sighandler_shutdown ()
682 int old_errno = errno; /* backup errno */
684 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
685 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE), &c,
692 * Initialize and run scheduler. This function will return when all
693 * tasks have completed. On systems with signals, receiving a SIGTERM
694 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
695 * to be run after the active task is complete. As a result, SIGTERM
696 * causes all active tasks to be scheduled with reason
697 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
698 * afterwards will execute normally!). Note that any particular signal
699 * will only shut down one scheduler; applications should always only
700 * create a single scheduler.
702 * @param task task to run immediately
703 * @param task_cls closure of task
706 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
708 struct GNUNET_NETWORK_FDSet *rs;
709 struct GNUNET_NETWORK_FDSet *ws;
710 struct GNUNET_TIME_Relative timeout;
712 struct GNUNET_SIGNAL_Context *shc_int;
713 struct GNUNET_SIGNAL_Context *shc_term;
715 struct GNUNET_SIGNAL_Context *shc_quit;
716 struct GNUNET_SIGNAL_Context *shc_hup;
717 struct GNUNET_SIGNAL_Context *shc_pipe;
719 unsigned long long last_tr;
720 unsigned int busy_wait_warning;
721 const struct GNUNET_DISK_FileHandle *pr;
724 GNUNET_assert (active_task == NULL);
725 rs = GNUNET_NETWORK_fdset_create ();
726 ws = GNUNET_NETWORK_fdset_create ();
727 GNUNET_assert (shutdown_pipe_handle == NULL);
728 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
729 GNUNET_assert (shutdown_pipe_handle != NULL);
730 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_READ);
731 GNUNET_assert (pr != NULL);
732 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
733 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
735 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
736 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
737 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
739 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
740 GNUNET_SCHEDULER_add_continuation (task,
742 GNUNET_SCHEDULER_REASON_STARTUP);
744 busy_wait_warning = 0;
745 while ((pending != NULL) ||
746 (pending_timeout != NULL) ||
749 GNUNET_NETWORK_fdset_zero (rs);
750 GNUNET_NETWORK_fdset_zero (ws);
751 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
752 update_sets (rs, ws, &timeout);
753 GNUNET_NETWORK_fdset_handle_set (rs, pr);
756 /* no blocking, more work already ready! */
757 timeout = GNUNET_TIME_UNIT_ZERO;
759 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
760 if (ret == GNUNET_SYSERR)
765 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
769 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid());
778 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
780 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
781 _("Looks like we're busy waiting...\n"));
782 sleep (1); /* mitigate */
784 check_ready (rs, ws);
786 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
788 /* consume the signal */
789 GNUNET_DISK_file_read (pr, &c, sizeof (c));
790 /* mark all active tasks as ready due to shutdown */
791 GNUNET_SCHEDULER_shutdown ();
793 if (last_tr == tasks_run)
800 busy_wait_warning = 0;
803 GNUNET_SIGNAL_handler_uninstall (shc_int);
804 GNUNET_SIGNAL_handler_uninstall (shc_term);
806 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
807 GNUNET_SIGNAL_handler_uninstall (shc_quit);
808 GNUNET_SIGNAL_handler_uninstall (shc_hup);
810 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
811 shutdown_pipe_handle = NULL;
812 GNUNET_NETWORK_fdset_destroy (rs);
813 GNUNET_NETWORK_fdset_destroy (ws);
818 * Obtain the reason code for why the current task was
819 * started. Will return the same value as
820 * the GNUNET_SCHEDULER_TaskContext's reason field.
822 * @return reason(s) why the current task is run
824 enum GNUNET_SCHEDULER_Reason
825 GNUNET_SCHEDULER_get_reason ()
827 GNUNET_assert (active_task != NULL);
828 return active_task->reason;
833 * Get information about the current load of this scheduler. Use this
834 * function to determine if an elective task should be added or simply
835 * dropped (if the decision should be made based on the number of
836 * tasks ready to run).
838 * @param p priority level to look at
839 * @return number of tasks pending right now
842 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
847 GNUNET_assert (active_task != NULL);
848 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
850 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
851 p = current_priority;
853 pos = ready[check_priority (p)];
864 * Cancel the task with the specified identifier.
865 * The task must not yet have run.
867 * @param task id of the task to cancel
868 * @return original closure of the task
871 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
875 enum GNUNET_SCHEDULER_Priority p;
879 GNUNET_assert (active_task != NULL);
902 if (pending_timeout_last == t)
903 pending_timeout_last = NULL;
909 GNUNET_assert (p < GNUNET_SCHEDULER_PRIORITY_COUNT);
933 pending_timeout = t->next;
943 prev->next = t->next;
945 ret = t->callback_cls;
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
948 "Canceling task: %llu / %p\n", task, t->callback_cls);
956 * Continue the current execution with the given function. This is
957 * similar to the other "add" functions except that there is no delay
958 * and the reason code can be specified.
960 * @param task main function of the task
961 * @param task_cls closure for 'main'
962 * @param reason reason for task invocation
965 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task,
967 enum GNUNET_SCHEDULER_Reason reason)
971 void *backtrace_array[50];
974 GNUNET_assert ( (active_task != NULL) ||
975 (reason == GNUNET_SCHEDULER_REASON_STARTUP) );
976 t = GNUNET_malloc (sizeof (struct Task));
978 t->num_backtrace_strings = backtrace(backtrace_array, 50);
979 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
984 t->callback_cls = task_cls;
987 t->start_time = GNUNET_TIME_absolute_get ();
990 t->priority = current_priority;
992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
993 "Adding continuation task: %llu / %p\n",
994 t->id, t->callback_cls);
996 queue_ready_task (t);
1002 * Schedule a new task to be run after the specified prerequisite task
1003 * has completed. It will be run with the priority of the calling
1006 * @param prerequisite_task run this task after the task with the given
1007 * task identifier completes (and any of our other
1008 * conditions, such as delay, read or write-readiness
1009 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1010 * on completion of other tasks (this will cause the task to run as
1011 * soon as possible).
1012 * @param task main function of the task
1013 * @param task_cls closure of task
1014 * @return unique task identifier for the job
1015 * only valid until "task" is started!
1017 GNUNET_SCHEDULER_TaskIdentifier
1018 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1019 GNUNET_SCHEDULER_Task task, void *task_cls)
1021 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1023 GNUNET_TIME_UNIT_ZERO,
1024 NULL, NULL, task, task_cls);
1029 * Schedule a new task to be run with a specified priority.
1031 * @param prio how important is the new task?
1032 * @param task main function of the task
1033 * @param task_cls closure of task
1034 * @return unique task identifier for the job
1035 * only valid until "task" is started!
1037 GNUNET_SCHEDULER_TaskIdentifier
1038 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1039 GNUNET_SCHEDULER_Task task,
1042 return GNUNET_SCHEDULER_add_select (prio,
1043 GNUNET_SCHEDULER_NO_TASK,
1044 GNUNET_TIME_UNIT_ZERO,
1045 NULL, NULL, task, task_cls);
1051 * Schedule a new task to be run with a specified delay. The task
1052 * will be scheduled for execution once the delay has expired. It
1053 * will be run with the priority of the calling task.
1055 * @param delay when should this operation time out? Use
1056 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1057 * @param task main function of the task
1058 * @param task_cls closure of task
1059 * @return unique task identifier for the job
1060 * only valid until "task" is started!
1062 GNUNET_SCHEDULER_TaskIdentifier
1063 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1064 GNUNET_SCHEDULER_Task task, void *task_cls)
1067 /* new, optimized version */
1072 void *backtrace_array[MAX_TRACE_DEPTH];
1075 GNUNET_assert (active_task != NULL);
1076 GNUNET_assert (NULL != task);
1077 t = GNUNET_malloc (sizeof (struct Task));
1079 t->callback_cls = task_cls;
1081 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1082 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1088 t->start_time = GNUNET_TIME_absolute_get ();
1090 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1091 t->priority = current_priority;
1092 /* try tail first (optimization in case we are
1093 appending to a long list of tasks with timeouts) */
1094 prev = pending_timeout_last;
1097 if (prev->timeout.abs_value > t->timeout.abs_value)
1100 pos = prev->next; /* heuristic success! */
1104 /* heuristic failed, do traversal of timeout list */
1105 pos = pending_timeout;
1107 while ( (pos != NULL) &&
1108 ( (pos->timeout.abs_value <= t->timeout.abs_value) ||
1109 (pos->reason != 0) ) )
1115 pending_timeout = t;
1119 /* hyper-optimization... */
1120 pending_timeout_last = t;
1123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1129 for (i=0;i<t->num_backtrace_strings;i++)
1130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1131 "Task %llu trace %d: %s\n",
1134 t->backtrace_strings[i]);
1139 /* unoptimized version */
1140 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1141 GNUNET_SCHEDULER_NO_TASK, delay,
1142 NULL, NULL, task, task_cls);
1149 * Schedule a new task to be run as soon as possible. The task
1150 * will be run with the priority of the calling task.
1152 * @param task main function of the task
1153 * @param task_cls closure of task
1154 * @return unique task identifier for the job
1155 * only valid until "task" is started!
1157 GNUNET_SCHEDULER_TaskIdentifier
1158 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task,
1161 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1162 GNUNET_SCHEDULER_NO_TASK,
1163 GNUNET_TIME_UNIT_ZERO,
1164 NULL, NULL, task, task_cls);
1171 * Schedule a new task to be run with a specified delay or when any of
1172 * the specified file descriptor sets is ready. The delay can be used
1173 * as a timeout on the socket(s) being ready. The task will be
1174 * scheduled for execution once either the delay has expired or any of
1175 * the socket operations is ready. This is the most general
1176 * function of the "add" family. Note that the "prerequisite_task"
1177 * must be satisfied in addition to any of the other conditions. In
1178 * other words, the task will be started when
1180 * (prerequisite-run)
1184 * || shutdown-active )
1187 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1188 * which means that the task will only be run after we receive SIGTERM
1189 * @param rfd file descriptor we want to read (can be -1)
1190 * @param wfd file descriptors we want to write (can be -1)
1191 * @param task main function of the task
1192 * @param task_cls closure of task
1193 * @return unique task identifier for the job
1194 * only valid until "task" is started!
1196 GNUNET_SCHEDULER_TaskIdentifier
1197 add_without_sets (struct GNUNET_TIME_Relative delay,
1200 GNUNET_SCHEDULER_Task task, void *task_cls)
1204 void *backtrace_array[MAX_TRACE_DEPTH];
1207 GNUNET_assert (active_task != NULL);
1208 GNUNET_assert (NULL != task);
1209 t = GNUNET_malloc (sizeof (struct Task));
1211 t->callback_cls = task_cls;
1213 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1214 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1220 t->start_time = GNUNET_TIME_absolute_get ();
1222 t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
1223 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1224 t->priority = check_priority (current_priority);
1227 max_priority_added = GNUNET_MAX (max_priority_added,
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1231 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1236 for (i=0;i<t->num_backtrace_strings;i++)
1237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1238 "Task %llu trace %d: %s\n",
1241 t->backtrace_strings[i]);
1249 * Schedule a new task to be run with a specified delay or when the
1250 * specified file descriptor is ready for reading. The delay can be
1251 * used as a timeout on the socket being ready. The task will be
1252 * scheduled for execution once either the delay has expired or the
1253 * socket operation is ready. It will be run with the priority of
1256 * @param delay when should this operation time out? Use
1257 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1258 * @param rfd read file-descriptor
1259 * @param task main function of the task
1260 * @param task_cls closure of task
1261 * @return unique task identifier for the job
1262 * only valid until "task" is started!
1264 GNUNET_SCHEDULER_TaskIdentifier
1265 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1266 struct GNUNET_NETWORK_Handle * rfd,
1267 GNUNET_SCHEDULER_Task task, void *task_cls)
1269 return add_without_sets (delay,
1270 GNUNET_NETWORK_get_fd (rfd),
1278 * Schedule a new task to be run with a specified delay or when the
1279 * specified file descriptor is ready for writing. The delay can be
1280 * used as a timeout on the socket being ready. The task will be
1281 * scheduled for execution once either the delay has expired or the
1282 * socket operation is ready. It will be run with the priority of
1285 * @param delay when should this operation time out? Use
1286 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1287 * @param wfd write file-descriptor
1288 * @param task main function of the task
1289 * @param task_cls closure of task
1290 * @return unique task identifier for the job
1291 * only valid until "task" is started!
1293 GNUNET_SCHEDULER_TaskIdentifier
1294 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1295 struct GNUNET_NETWORK_Handle * wfd,
1296 GNUNET_SCHEDULER_Task task, void *task_cls)
1298 return add_without_sets (delay,
1300 GNUNET_NETWORK_get_fd (wfd),
1307 * Schedule a new task to be run with a specified delay or when the
1308 * specified file descriptor is ready for reading. The delay can be
1309 * used as a timeout on the socket being ready. The task will be
1310 * scheduled for execution once either the delay has expired or the
1311 * socket operation is ready. It will be run with the priority of
1314 * @param delay when should this operation time out? Use
1315 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1316 * @param rfd read file-descriptor
1317 * @param task main function of the task
1318 * @param task_cls closure of task
1319 * @return unique task identifier for the job
1320 * only valid until "task" is started!
1322 GNUNET_SCHEDULER_TaskIdentifier
1323 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1324 const struct GNUNET_DISK_FileHandle * rfd,
1325 GNUNET_SCHEDULER_Task task, void *task_cls)
1328 struct GNUNET_NETWORK_FDSet *rs;
1329 GNUNET_SCHEDULER_TaskIdentifier ret;
1331 GNUNET_assert (rfd != NULL);
1332 rs = GNUNET_NETWORK_fdset_create ();
1333 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1334 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1335 GNUNET_SCHEDULER_NO_TASK, delay,
1336 rs, NULL, task, task_cls);
1337 GNUNET_NETWORK_fdset_destroy (rs);
1342 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1343 return add_without_sets (delay,
1354 * Schedule a new task to be run with a specified delay or when the
1355 * specified file descriptor is ready for writing. The delay can be
1356 * used as a timeout on the socket being ready. The task will be
1357 * scheduled for execution once either the delay has expired or the
1358 * socket operation is ready. It will be run with the priority of
1361 * @param delay when should this operation time out? Use
1362 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1363 * @param wfd write file-descriptor
1364 * @param task main function of the task
1365 * @param task_cls closure of task
1366 * @return unique task identifier for the job
1367 * only valid until "task" is started!
1369 GNUNET_SCHEDULER_TaskIdentifier
1370 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1371 const struct GNUNET_DISK_FileHandle * wfd,
1372 GNUNET_SCHEDULER_Task task, void *task_cls)
1375 struct GNUNET_NETWORK_FDSet *ws;
1376 GNUNET_SCHEDULER_TaskIdentifier ret;
1378 GNUNET_assert (wfd != NULL);
1379 ws = GNUNET_NETWORK_fdset_create ();
1380 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1381 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1382 GNUNET_SCHEDULER_NO_TASK,
1383 delay, NULL, ws, task, task_cls);
1384 GNUNET_NETWORK_fdset_destroy (ws);
1389 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1390 return add_without_sets (delay,
1402 * Schedule a new task to be run with a specified delay or when any of
1403 * the specified file descriptor sets is ready. The delay can be used
1404 * as a timeout on the socket(s) being ready. The task will be
1405 * scheduled for execution once either the delay has expired or any of
1406 * the socket operations is ready. This is the most general
1407 * function of the "add" family. Note that the "prerequisite_task"
1408 * must be satisfied in addition to any of the other conditions. In
1409 * other words, the task will be started when
1411 * (prerequisite-run)
1415 * || (shutdown-active && run-on-shutdown) )
1418 * @param prio how important is this task?
1419 * @param prerequisite_task run this task after the task with the given
1420 * task identifier completes (and any of our other
1421 * conditions, such as delay, read or write-readiness
1422 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1423 * on completion of other tasks.
1424 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1425 * which means that the task will only be run after we receive SIGTERM
1426 * @param rs set of file descriptors we want to read (can be NULL)
1427 * @param ws set of file descriptors we want to write (can be NULL)
1428 * @param task main function of the task
1429 * @param task_cls closure of task
1430 * @return unique task identifier for the job
1431 * only valid until "task" is started!
1433 GNUNET_SCHEDULER_TaskIdentifier
1434 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1435 GNUNET_SCHEDULER_TaskIdentifier
1437 struct GNUNET_TIME_Relative delay,
1438 const struct GNUNET_NETWORK_FDSet * rs,
1439 const struct GNUNET_NETWORK_FDSet * ws,
1440 GNUNET_SCHEDULER_Task task, void *task_cls)
1444 void *backtrace_array[MAX_TRACE_DEPTH];
1447 GNUNET_assert (active_task != NULL);
1448 GNUNET_assert (NULL != task);
1449 t = GNUNET_malloc (sizeof (struct Task));
1451 t->callback_cls = task_cls;
1453 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1454 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1460 t->read_set = GNUNET_NETWORK_fdset_create ();
1461 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1465 t->write_set = GNUNET_NETWORK_fdset_create ();
1466 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1470 t->start_time = GNUNET_TIME_absolute_get ();
1472 t->prereq_id = prerequisite_task;
1473 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1475 check_priority ((prio ==
1476 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority
1480 max_priority_added = GNUNET_MAX (max_priority_added,
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1484 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1489 for (i=0;i<t->num_backtrace_strings;i++)
1490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1491 "Task %llu trace %d: %s\n",
1494 t->backtrace_strings[i]);
1499 /* end of scheduler.c */