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.
516 GNUNET_SCHEDULER_shutdown ()
521 pos = pending_timeout;
524 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
525 /* we don't move the task into the ready queue yet; check_ready
526 will do that later, possibly adding additional
533 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
534 /* we don't move the task into the ready queue yet; check_ready
535 will do that later, possibly adding additional
539 for (i=0;i<GNUNET_SCHEDULER_PRIORITY_COUNT;i++)
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
555 * Destroy a task (release associated resources)
557 * @param t task to destroy
560 destroy_task (struct Task *t)
562 if (NULL != t->read_set)
563 GNUNET_NETWORK_fdset_destroy (t->read_set);
564 if (NULL != t->write_set)
565 GNUNET_NETWORK_fdset_destroy (t->write_set);
567 GNUNET_free (t->backtrace_strings);
574 * Run at least one task in the highest-priority queue that is not
575 * empty. Keep running tasks until we are either no longer running
576 * "URGENT" tasks or until we have at least one "pending" task (which
577 * may become ready, hence we should select on it). Naturally, if
578 * there are no more ready tasks, we also return.
580 * @param rs FDs ready for reading
581 * @param ws FDs ready for writing
584 run_ready (struct GNUNET_NETWORK_FDSet *rs,
585 struct GNUNET_NETWORK_FDSet *ws)
587 enum GNUNET_SCHEDULER_Priority p;
589 struct GNUNET_SCHEDULER_TaskContext tc;
591 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
594 if (ready_count == 0)
596 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
597 /* yes, p>0 is correct, 0 is "KEEP" which should
598 always be an empty queue (see assertion)! */
599 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
605 GNUNET_assert (pos != NULL); /* ready_count wrong? */
606 ready[p] = pos->next;
608 if (current_priority != pos->priority)
610 current_priority = pos->priority;
611 (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), pos->priority);
615 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
616 DELAY_THRESHOLD.rel_value)
618 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
619 "Task %llu took %llums to be scheduled\n",
621 (unsigned long long) GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value);
624 tc.reason = pos->reason;
625 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
626 if ( (pos->read_fd != -1) &&
627 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
628 GNUNET_NETWORK_fdset_set_native (rs,
630 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
631 if ( (pos->write_fd != -1) &&
632 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) )
633 GNUNET_NETWORK_fdset_set_native (ws,
635 if ( ( (tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
636 (pos->write_fd != -1) &&
637 (! GNUNET_NETWORK_fdset_test_native (ws,
639 abort (); // added to ready in previous select loop!
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
642 "Running task: %llu / %p\n", pos->id, pos->callback_cls);
644 pos->callback (pos->callback_cls, &tc);
647 for (i=0;i<pos->num_backtrace_strings;i++)
648 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
649 "Task %llu trace %d: %s\n",
652 pos->backtrace_strings[i]);
658 while ( (pending == NULL) || (p >= max_priority_added) );
662 * Pipe used to communicate shutdown via signal.
664 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
667 * Signal handler called for SIGPIPE.
677 * Signal handler called for signals that should cause us to shutdown.
680 sighandler_shutdown ()
683 int old_errno = errno; /* backup errno */
685 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
686 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE), &c,
693 * Initialize and run scheduler. This function will return when all
694 * tasks have completed. On systems with signals, receiving a SIGTERM
695 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
696 * to be run after the active task is complete. As a result, SIGTERM
697 * causes all active tasks to be scheduled with reason
698 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
699 * afterwards will execute normally!). Note that any particular signal
700 * will only shut down one scheduler; applications should always only
701 * create a single scheduler.
703 * @param task task to run immediately
704 * @param task_cls closure of task
707 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
709 struct GNUNET_NETWORK_FDSet *rs;
710 struct GNUNET_NETWORK_FDSet *ws;
711 struct GNUNET_TIME_Relative timeout;
713 struct GNUNET_SIGNAL_Context *shc_int;
714 struct GNUNET_SIGNAL_Context *shc_term;
716 struct GNUNET_SIGNAL_Context *shc_quit;
717 struct GNUNET_SIGNAL_Context *shc_hup;
718 struct GNUNET_SIGNAL_Context *shc_pipe;
720 unsigned long long last_tr;
721 unsigned int busy_wait_warning;
722 const struct GNUNET_DISK_FileHandle *pr;
725 GNUNET_assert (active_task == NULL);
726 rs = GNUNET_NETWORK_fdset_create ();
727 ws = GNUNET_NETWORK_fdset_create ();
728 GNUNET_assert (shutdown_pipe_handle == NULL);
729 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
730 GNUNET_assert (shutdown_pipe_handle != NULL);
731 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_READ);
732 GNUNET_assert (pr != NULL);
733 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
734 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
736 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
737 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
738 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
740 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
741 GNUNET_SCHEDULER_add_continuation (task,
743 GNUNET_SCHEDULER_REASON_STARTUP);
745 busy_wait_warning = 0;
746 while ((pending != NULL) ||
747 (pending_timeout != NULL) ||
750 GNUNET_NETWORK_fdset_zero (rs);
751 GNUNET_NETWORK_fdset_zero (ws);
752 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
753 update_sets (rs, ws, &timeout);
754 GNUNET_NETWORK_fdset_handle_set (rs, pr);
757 /* no blocking, more work already ready! */
758 timeout = GNUNET_TIME_UNIT_ZERO;
760 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
761 if (ret == GNUNET_SYSERR)
766 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
770 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid());
779 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
781 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
782 _("Looks like we're busy waiting...\n"));
783 sleep (1); /* mitigate */
785 check_ready (rs, ws);
787 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
789 /* consume the signal */
790 GNUNET_DISK_file_read (pr, &c, sizeof (c));
791 /* mark all active tasks as ready due to shutdown */
792 GNUNET_SCHEDULER_shutdown ();
794 if (last_tr == tasks_run)
801 busy_wait_warning = 0;
804 GNUNET_SIGNAL_handler_uninstall (shc_int);
805 GNUNET_SIGNAL_handler_uninstall (shc_term);
807 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
808 GNUNET_SIGNAL_handler_uninstall (shc_quit);
809 GNUNET_SIGNAL_handler_uninstall (shc_hup);
811 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
812 shutdown_pipe_handle = NULL;
813 GNUNET_NETWORK_fdset_destroy (rs);
814 GNUNET_NETWORK_fdset_destroy (ws);
819 * Obtain the reason code for why the current task was
820 * started. Will return the same value as
821 * the GNUNET_SCHEDULER_TaskContext's reason field.
823 * @return reason(s) why the current task is run
825 enum GNUNET_SCHEDULER_Reason
826 GNUNET_SCHEDULER_get_reason ()
828 GNUNET_assert (active_task != NULL);
829 return active_task->reason;
834 * Get information about the current load of this scheduler. Use this
835 * function to determine if an elective task should be added or simply
836 * dropped (if the decision should be made based on the number of
837 * tasks ready to run).
839 * @param p priority level to look at
840 * @return number of tasks pending right now
843 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
848 GNUNET_assert (active_task != NULL);
849 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
851 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
852 p = current_priority;
854 pos = ready[check_priority (p)];
865 * Cancel the task with the specified identifier.
866 * The task must not yet have run.
868 * @param task id of the task to cancel
869 * @return original closure of the task
872 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
876 enum GNUNET_SCHEDULER_Priority p;
880 GNUNET_assert (active_task != NULL);
903 if (pending_timeout_last == t)
904 pending_timeout_last = NULL;
910 GNUNET_assert (p < GNUNET_SCHEDULER_PRIORITY_COUNT);
934 pending_timeout = t->next;
944 prev->next = t->next;
946 ret = t->callback_cls;
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
949 "Canceling task: %llu / %p\n", task, t->callback_cls);
957 * Continue the current execution with the given function. This is
958 * similar to the other "add" functions except that there is no delay
959 * and the reason code can be specified.
961 * @param task main function of the task
962 * @param task_cls closure for 'main'
963 * @param reason reason for task invocation
966 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task,
968 enum GNUNET_SCHEDULER_Reason reason)
972 void *backtrace_array[50];
975 GNUNET_assert ( (active_task != NULL) ||
976 (reason == GNUNET_SCHEDULER_REASON_STARTUP) );
977 t = GNUNET_malloc (sizeof (struct Task));
979 t->num_backtrace_strings = backtrace(backtrace_array, 50);
980 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
985 t->callback_cls = task_cls;
988 t->start_time = GNUNET_TIME_absolute_get ();
991 t->priority = current_priority;
993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
994 "Adding continuation task: %llu / %p\n",
995 t->id, t->callback_cls);
997 queue_ready_task (t);
1003 * Schedule a new task to be run after the specified prerequisite task
1004 * has completed. It will be run with the priority of the calling
1007 * @param prerequisite_task run this task after the task with the given
1008 * task identifier completes (and any of our other
1009 * conditions, such as delay, read or write-readiness
1010 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1011 * on completion of other tasks (this will cause the task to run as
1012 * soon as possible).
1013 * @param task main function of the task
1014 * @param task_cls closure of task
1015 * @return unique task identifier for the job
1016 * only valid until "task" is started!
1018 GNUNET_SCHEDULER_TaskIdentifier
1019 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1020 GNUNET_SCHEDULER_Task task, void *task_cls)
1022 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1024 GNUNET_TIME_UNIT_ZERO,
1025 NULL, NULL, task, task_cls);
1030 * Schedule a new task to be run with a specified priority.
1032 * @param prio how important is the new task?
1033 * @param task main function of the task
1034 * @param task_cls closure of task
1035 * @return unique task identifier for the job
1036 * only valid until "task" is started!
1038 GNUNET_SCHEDULER_TaskIdentifier
1039 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1040 GNUNET_SCHEDULER_Task task,
1043 return GNUNET_SCHEDULER_add_select (prio,
1044 GNUNET_SCHEDULER_NO_TASK,
1045 GNUNET_TIME_UNIT_ZERO,
1046 NULL, NULL, task, task_cls);
1052 * Schedule a new task to be run with a specified delay. The task
1053 * will be scheduled for execution once the delay has expired. It
1054 * will be run with the priority of the calling task.
1056 * @param delay when should this operation time out? Use
1057 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1058 * @param task main function of the task
1059 * @param task_cls closure of task
1060 * @return unique task identifier for the job
1061 * only valid until "task" is started!
1063 GNUNET_SCHEDULER_TaskIdentifier
1064 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1065 GNUNET_SCHEDULER_Task task, void *task_cls)
1068 /* new, optimized version */
1073 void *backtrace_array[MAX_TRACE_DEPTH];
1076 GNUNET_assert (active_task != NULL);
1077 GNUNET_assert (NULL != task);
1078 t = GNUNET_malloc (sizeof (struct Task));
1080 t->callback_cls = task_cls;
1082 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1083 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1089 t->start_time = GNUNET_TIME_absolute_get ();
1091 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1092 t->priority = current_priority;
1093 /* try tail first (optimization in case we are
1094 appending to a long list of tasks with timeouts) */
1095 prev = pending_timeout_last;
1098 if (prev->timeout.abs_value > t->timeout.abs_value)
1101 pos = prev->next; /* heuristic success! */
1105 /* heuristic failed, do traversal of timeout list */
1106 pos = pending_timeout;
1108 while ( (pos != NULL) &&
1109 ( (pos->timeout.abs_value <= t->timeout.abs_value) ||
1110 (pos->reason != 0) ) )
1116 pending_timeout = t;
1120 /* hyper-optimization... */
1121 pending_timeout_last = t;
1124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1125 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1130 for (i=0;i<t->num_backtrace_strings;i++)
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 "Task %llu trace %d: %s\n",
1135 t->backtrace_strings[i]);
1140 /* unoptimized version */
1141 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1142 GNUNET_SCHEDULER_NO_TASK, delay,
1143 NULL, NULL, task, task_cls);
1150 * Schedule a new task to be run as soon as possible. The task
1151 * will be run with the priority of the calling task.
1153 * @param task main function of the task
1154 * @param task_cls closure of task
1155 * @return unique task identifier for the job
1156 * only valid until "task" is started!
1158 GNUNET_SCHEDULER_TaskIdentifier
1159 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task,
1162 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1163 GNUNET_SCHEDULER_NO_TASK,
1164 GNUNET_TIME_UNIT_ZERO,
1165 NULL, NULL, task, task_cls);
1172 * Schedule a new task to be run with a specified delay or when any of
1173 * the specified file descriptor sets is ready. The delay can be used
1174 * as a timeout on the socket(s) being ready. The task will be
1175 * scheduled for execution once either the delay has expired or any of
1176 * the socket operations is ready. This is the most general
1177 * function of the "add" family. Note that the "prerequisite_task"
1178 * must be satisfied in addition to any of the other conditions. In
1179 * other words, the task will be started when
1181 * (prerequisite-run)
1185 * || shutdown-active )
1188 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1189 * which means that the task will only be run after we receive SIGTERM
1190 * @param rfd file descriptor we want to read (can be -1)
1191 * @param wfd file descriptors we want to write (can be -1)
1192 * @param task main function of the task
1193 * @param task_cls closure of task
1194 * @return unique task identifier for the job
1195 * only valid until "task" is started!
1197 GNUNET_SCHEDULER_TaskIdentifier
1198 add_without_sets (struct GNUNET_TIME_Relative delay,
1201 GNUNET_SCHEDULER_Task task, void *task_cls)
1205 void *backtrace_array[MAX_TRACE_DEPTH];
1208 GNUNET_assert (active_task != NULL);
1209 GNUNET_assert (NULL != task);
1210 t = GNUNET_malloc (sizeof (struct Task));
1212 t->callback_cls = task_cls;
1214 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1215 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1221 t->start_time = GNUNET_TIME_absolute_get ();
1223 t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
1224 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1225 t->priority = check_priority (current_priority);
1228 max_priority_added = GNUNET_MAX (max_priority_added,
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1232 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1237 for (i=0;i<t->num_backtrace_strings;i++)
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1239 "Task %llu trace %d: %s\n",
1242 t->backtrace_strings[i]);
1250 * Schedule a new task to be run with a specified delay or when the
1251 * specified file descriptor is ready for reading. The delay can be
1252 * used as a timeout on the socket being ready. The task will be
1253 * scheduled for execution once either the delay has expired or the
1254 * socket operation is ready. It will be run with the priority of
1257 * @param delay when should this operation time out? Use
1258 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1259 * @param rfd read file-descriptor
1260 * @param task main function of the task
1261 * @param task_cls closure of task
1262 * @return unique task identifier for the job
1263 * only valid until "task" is started!
1265 GNUNET_SCHEDULER_TaskIdentifier
1266 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1267 struct GNUNET_NETWORK_Handle * rfd,
1268 GNUNET_SCHEDULER_Task task, void *task_cls)
1270 return add_without_sets (delay,
1271 GNUNET_NETWORK_get_fd (rfd),
1279 * Schedule a new task to be run with a specified delay or when the
1280 * specified file descriptor is ready for writing. The delay can be
1281 * used as a timeout on the socket being ready. The task will be
1282 * scheduled for execution once either the delay has expired or the
1283 * socket operation is ready. It will be run with the priority of
1286 * @param delay when should this operation time out? Use
1287 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1288 * @param wfd write file-descriptor
1289 * @param task main function of the task
1290 * @param task_cls closure of task
1291 * @return unique task identifier for the job
1292 * only valid until "task" is started!
1294 GNUNET_SCHEDULER_TaskIdentifier
1295 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1296 struct GNUNET_NETWORK_Handle * wfd,
1297 GNUNET_SCHEDULER_Task task, void *task_cls)
1299 return add_without_sets (delay,
1301 GNUNET_NETWORK_get_fd (wfd),
1308 * Schedule a new task to be run with a specified delay or when the
1309 * specified file descriptor is ready for reading. The delay can be
1310 * used as a timeout on the socket being ready. The task will be
1311 * scheduled for execution once either the delay has expired or the
1312 * socket operation is ready. It will be run with the priority of
1315 * @param delay when should this operation time out? Use
1316 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1317 * @param rfd read file-descriptor
1318 * @param task main function of the task
1319 * @param task_cls closure of task
1320 * @return unique task identifier for the job
1321 * only valid until "task" is started!
1323 GNUNET_SCHEDULER_TaskIdentifier
1324 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1325 const struct GNUNET_DISK_FileHandle * rfd,
1326 GNUNET_SCHEDULER_Task task, void *task_cls)
1329 struct GNUNET_NETWORK_FDSet *rs;
1330 GNUNET_SCHEDULER_TaskIdentifier ret;
1332 GNUNET_assert (rfd != NULL);
1333 rs = GNUNET_NETWORK_fdset_create ();
1334 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1335 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1336 GNUNET_SCHEDULER_NO_TASK, delay,
1337 rs, NULL, task, task_cls);
1338 GNUNET_NETWORK_fdset_destroy (rs);
1343 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1344 return add_without_sets (delay,
1355 * Schedule a new task to be run with a specified delay or when the
1356 * specified file descriptor is ready for writing. The delay can be
1357 * used as a timeout on the socket being ready. The task will be
1358 * scheduled for execution once either the delay has expired or the
1359 * socket operation is ready. It will be run with the priority of
1362 * @param delay when should this operation time out? Use
1363 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1364 * @param wfd write file-descriptor
1365 * @param task main function of the task
1366 * @param task_cls closure of task
1367 * @return unique task identifier for the job
1368 * only valid until "task" is started!
1370 GNUNET_SCHEDULER_TaskIdentifier
1371 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1372 const struct GNUNET_DISK_FileHandle * wfd,
1373 GNUNET_SCHEDULER_Task task, void *task_cls)
1376 struct GNUNET_NETWORK_FDSet *ws;
1377 GNUNET_SCHEDULER_TaskIdentifier ret;
1379 GNUNET_assert (wfd != NULL);
1380 ws = GNUNET_NETWORK_fdset_create ();
1381 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1382 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1383 GNUNET_SCHEDULER_NO_TASK,
1384 delay, NULL, ws, task, task_cls);
1385 GNUNET_NETWORK_fdset_destroy (ws);
1390 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1391 return add_without_sets (delay,
1403 * Schedule a new task to be run with a specified delay or when any of
1404 * the specified file descriptor sets is ready. The delay can be used
1405 * as a timeout on the socket(s) being ready. The task will be
1406 * scheduled for execution once either the delay has expired or any of
1407 * the socket operations is ready. This is the most general
1408 * function of the "add" family. Note that the "prerequisite_task"
1409 * must be satisfied in addition to any of the other conditions. In
1410 * other words, the task will be started when
1412 * (prerequisite-run)
1416 * || (shutdown-active && run-on-shutdown) )
1419 * @param prio how important is this task?
1420 * @param prerequisite_task run this task after the task with the given
1421 * task identifier completes (and any of our other
1422 * conditions, such as delay, read or write-readiness
1423 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1424 * on completion of other tasks.
1425 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1426 * which means that the task will only be run after we receive SIGTERM
1427 * @param rs set of file descriptors we want to read (can be NULL)
1428 * @param ws set of file descriptors we want to write (can be NULL)
1429 * @param task main function of the task
1430 * @param task_cls closure of task
1431 * @return unique task identifier for the job
1432 * only valid until "task" is started!
1434 GNUNET_SCHEDULER_TaskIdentifier
1435 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1436 GNUNET_SCHEDULER_TaskIdentifier
1438 struct GNUNET_TIME_Relative delay,
1439 const struct GNUNET_NETWORK_FDSet * rs,
1440 const struct GNUNET_NETWORK_FDSet * ws,
1441 GNUNET_SCHEDULER_Task task, void *task_cls)
1445 void *backtrace_array[MAX_TRACE_DEPTH];
1448 GNUNET_assert (active_task != NULL);
1449 GNUNET_assert (NULL != task);
1450 t = GNUNET_malloc (sizeof (struct Task));
1452 t->callback_cls = task_cls;
1454 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1455 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1461 t->read_set = GNUNET_NETWORK_fdset_create ();
1462 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1466 t->write_set = GNUNET_NETWORK_fdset_create ();
1467 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1471 t->start_time = GNUNET_TIME_absolute_get ();
1473 t->prereq_id = prerequisite_task;
1474 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1476 check_priority ((prio ==
1477 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority
1481 max_priority_added = GNUNET_MAX (max_priority_added,
1484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1485 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1490 for (i=0;i<t->num_backtrace_strings;i++)
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1492 "Task %llu trace %d: %s\n",
1495 t->backtrace_strings[i]);
1500 /* end of scheduler.c */