2 This file is part of GNUnet
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/scheduler.c
23 * @brief schedule computations using continuation passing style
24 * @author Christian Grothoff
27 #include "gnunet_common.h"
28 #include "gnunet_os_lib.h"
29 #include "gnunet_scheduler_lib.h"
30 #include "gnunet_signal_lib.h"
31 #include "gnunet_time_lib.h"
38 * Use lsof to generate file descriptor reports on select error?
39 * (turn off for stable releases).
41 #define USE_LSOF GNUNET_NO
44 * Obtain trace information for all scheduler calls that schedule tasks.
46 #define EXECINFO GNUNET_NO
49 * Check each file descriptor before adding
51 #define DEBUG_FDS GNUNET_NO
54 * Depth of the traces collected via EXECINFO.
56 #define MAX_TRACE_DEPTH 50
59 #define DEBUG_TASKS GNUNET_NO
62 * Should we figure out which tasks are delayed for a while
63 * before they are run? (Consider using in combination with EXECINFO).
65 #define PROFILE_DELAYS GNUNET_NO
68 * Task that were in the queue for longer than this are reported if
69 * PROFILE_DELAYS is active.
71 #define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
74 * Linked list of pending tasks.
79 * This is a linked list.
84 * Function to run when ready.
86 GNUNET_SCHEDULER_Task callback;
89 * Closure for the callback.
94 * Set of file descriptors this task is waiting
95 * for for reading. Once ready, this is updated
96 * to reflect the set of file descriptors ready
99 struct GNUNET_NETWORK_FDSet *read_set;
102 * Set of file descriptors this task is waiting for for writing.
103 * Once ready, this is updated to reflect the set of file
104 * descriptors ready for operation.
106 struct GNUNET_NETWORK_FDSet *write_set;
109 * Unique task identifier.
111 GNUNET_SCHEDULER_TaskIdentifier id;
114 * Identifier of a prerequisite task.
116 GNUNET_SCHEDULER_TaskIdentifier prereq_id;
119 * Absolute timeout value for the task, or
120 * GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
122 struct GNUNET_TIME_Absolute timeout;
126 * When was the task scheduled?
128 struct GNUNET_TIME_Absolute start_time;
132 * Why is the task ready? Set after task is added to ready queue.
133 * Initially set to zero. All reasons that have already been
134 * satisfied (i.e. read or write ready) will be set over time.
136 enum GNUNET_SCHEDULER_Reason reason;
141 enum GNUNET_SCHEDULER_Priority priority;
144 * Set if we only wait for reading from a single FD, otherwise -1.
149 * Set if we only wait for writing to a single FD, otherwise -1.
155 * Array of strings which make up a backtrace from the point when this
156 * task was scheduled (essentially, who scheduled the task?)
158 char **backtrace_strings;
161 * Size of the backtrace_strings array
163 int num_backtrace_strings;
171 * List of tasks waiting for an event.
173 static struct Task *pending;
176 * List of tasks waiting ONLY for a timeout event.
177 * Sorted by timeout (earliest first). Used so that
178 * we do not traverse the list of these tasks when
179 * building select sets (we just look at the head
180 * to determine the respective timeout ONCE).
182 static struct Task *pending_timeout;
185 * Last inserted task waiting ONLY for a timeout event.
186 * Used to (heuristically) speed up insertion.
188 static struct Task *pending_timeout_last;
191 * ID of the task that is running right now.
193 static struct Task *active_task;
196 * List of tasks ready to run right now,
197 * grouped by importance.
199 static struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
202 * Identity of the last task queued. Incremented for each task to
203 * generate a unique task ID (it is virtually impossible to start
204 * more than 2^64 tasks during the lifetime of a process).
206 static GNUNET_SCHEDULER_TaskIdentifier last_id;
209 * Highest number so that all tasks with smaller identifiers
210 * have already completed. Also the lowest number of a task
211 * still waiting to be executed.
213 static GNUNET_SCHEDULER_TaskIdentifier lowest_pending_id;
216 * Number of tasks on the ready list.
218 static unsigned int ready_count;
221 * How many tasks have we run so far?
223 static unsigned long long tasks_run;
226 * Priority of the task running right now. Only
227 * valid while a task is running.
229 static enum GNUNET_SCHEDULER_Priority current_priority;
232 * Priority of the highest task added in the current select
235 static enum GNUNET_SCHEDULER_Priority max_priority_added;
239 * Check that the given priority is legal (and return it).
241 * @param p priority value to check
242 * @return p on success, 0 on error
244 static enum GNUNET_SCHEDULER_Priority
245 check_priority (enum GNUNET_SCHEDULER_Priority p)
247 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
250 return 0; /* make compiler happy */
255 * Is a task with this identifier still pending? Also updates
256 * "lowest_pending_id" as a side-effect (for faster checks in the
257 * future), but only if the return value is "GNUNET_NO" (and
258 * the "lowest_pending_id" check failed).
260 * @param id which task are we checking for
261 * @return GNUNET_YES if so, GNUNET_NO if not
264 is_pending (GNUNET_SCHEDULER_TaskIdentifier id)
267 enum GNUNET_SCHEDULER_Priority p;
268 GNUNET_SCHEDULER_TaskIdentifier min;
270 if (id < lowest_pending_id)
272 min = -1; /* maximum value */
282 pos = pending_timeout;
291 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
303 lowest_pending_id = min;
309 * Update all sets and timeout for select.
311 * @param rs read-set, set to all FDs we would like to read (updated)
312 * @param ws write-set, set to all FDs we would like to write (updated)
313 * @param timeout next timeout (updated)
316 update_sets (struct GNUNET_NETWORK_FDSet *rs,
317 struct GNUNET_NETWORK_FDSet *ws,
318 struct GNUNET_TIME_Relative *timeout)
321 struct GNUNET_TIME_Absolute now;
322 struct GNUNET_TIME_Relative to;
324 now = GNUNET_TIME_absolute_get ();
325 pos = pending_timeout;
328 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
329 if (timeout->rel_value > to.rel_value)
331 if (pos->reason != 0)
332 *timeout = GNUNET_TIME_UNIT_ZERO;
337 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
338 (GNUNET_YES == is_pending (pos->prereq_id)))
343 if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
345 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
346 if (timeout->rel_value > to.rel_value)
349 if (pos->read_fd != -1)
350 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
351 if (pos->write_fd != -1)
352 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
353 if (pos->read_set != NULL)
354 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
355 if (pos->write_set != NULL)
356 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
357 if (pos->reason != 0)
358 *timeout = GNUNET_TIME_UNIT_ZERO;
365 * Check if the ready set overlaps with the set we want to have ready.
366 * If so, update the want set (set all FDs that are ready). If not,
369 * @param ready set that is ready
370 * @param want set that we want to be ready
371 * @return GNUNET_YES if there was some overlap
374 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
375 struct GNUNET_NETWORK_FDSet *want)
377 if ( (NULL == want) || (NULL == ready) )
379 if (GNUNET_NETWORK_fdset_overlap (ready, want))
381 /* copy all over (yes, there maybe unrelated bits,
382 but this should not hurt well-written clients) */
383 GNUNET_NETWORK_fdset_copy (want, ready);
391 * Check if the given task is eligible to run now.
392 * Also set the reason why it is eligible.
394 * @param task task to check if it is ready
395 * @param now the current time
396 * @param rs set of FDs ready for reading
397 * @param ws set of FDs ready for writing
398 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
401 is_ready (struct Task *task,
402 struct GNUNET_TIME_Absolute now,
403 const struct GNUNET_NETWORK_FDSet *rs,
404 const struct GNUNET_NETWORK_FDSet *ws)
406 enum GNUNET_SCHEDULER_Reason reason;
408 reason = task->reason;
409 if (now.abs_value >= task->timeout.abs_value)
410 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
411 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
412 ( ( (task->read_fd != -1) &&
413 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd)) ) ||
414 (set_overlaps (rs, task->read_set) ) ) )
415 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
416 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
417 ( ( (task->write_fd != -1) &&
418 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)) ) ||
419 (set_overlaps (ws, task->write_set) ) ) )
420 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
422 return GNUNET_NO; /* not ready */
423 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
425 if (GNUNET_YES == is_pending (task->prereq_id))
427 task->reason = reason;
428 return GNUNET_NO; /* prereq waiting */
430 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
432 task->reason = reason;
438 * Put a task that is ready for execution into the ready queue.
440 * @param task task ready for execution
443 queue_ready_task (struct Task *task)
445 enum GNUNET_SCHEDULER_Priority p = task->priority;
446 if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
447 p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
448 task->next = ready[check_priority (p)];
449 ready[check_priority (p)] = task;
455 * Check which tasks are ready and move them
456 * to the respective ready queue.
458 * @param rs FDs ready for reading
459 * @param ws FDs ready for writing
462 check_ready (const struct GNUNET_NETWORK_FDSet *rs,
463 const struct GNUNET_NETWORK_FDSet *ws)
468 struct GNUNET_TIME_Absolute now;
470 now = GNUNET_TIME_absolute_get ();
472 pos = pending_timeout;
476 if (now.abs_value >= pos->timeout.abs_value)
477 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
478 if (0 == pos->reason)
480 pending_timeout = next;
481 if (pending_timeout_last == pos)
482 pending_timeout_last = NULL;
483 queue_ready_task (pos);
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "Checking readiness of task: %llu / %p\n",
492 pos->id, pos->callback_cls);
495 if (GNUNET_YES == is_ready (pos, now, rs, ws))
501 queue_ready_task (pos);
512 * Request the shutdown of a scheduler. Marks all currently
513 * pending tasks as ready because of shutdown. This will
514 * cause all tasks to run (as soon as possible, respecting
515 * priorities and prerequisite tasks). Note that tasks
516 * scheduled AFTER this call may still be delayed arbitrarily.
519 GNUNET_SCHEDULER_shutdown ()
524 pos = pending_timeout;
527 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
528 /* we don't move the task into the ready queue yet; check_ready
529 will do that later, possibly adding additional
536 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
537 /* we don't move the task into the ready queue yet; check_ready
538 will do that later, possibly adding additional
542 for (i=0;i<GNUNET_SCHEDULER_PRIORITY_COUNT;i++)
547 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
548 /* we don't move the task into the ready queue yet; check_ready
549 will do that later, possibly adding additional
558 * Destroy a task (release associated resources)
560 * @param t task to destroy
563 destroy_task (struct Task *t)
565 if (NULL != t->read_set)
566 GNUNET_NETWORK_fdset_destroy (t->read_set);
567 if (NULL != t->write_set)
568 GNUNET_NETWORK_fdset_destroy (t->write_set);
570 GNUNET_free (t->backtrace_strings);
577 * Run at least one task in the highest-priority queue that is not
578 * empty. Keep running tasks until we are either no longer running
579 * "URGENT" tasks or until we have at least one "pending" task (which
580 * may become ready, hence we should select on it). Naturally, if
581 * there are no more ready tasks, we also return.
583 * @param rs FDs ready for reading
584 * @param ws FDs ready for writing
587 run_ready (struct GNUNET_NETWORK_FDSet *rs,
588 struct GNUNET_NETWORK_FDSet *ws)
590 enum GNUNET_SCHEDULER_Priority p;
592 struct GNUNET_SCHEDULER_TaskContext tc;
594 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
597 if (ready_count == 0)
599 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
600 /* yes, p>0 is correct, 0 is "KEEP" which should
601 always be an empty queue (see assertion)! */
602 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
608 GNUNET_assert (pos != NULL); /* ready_count wrong? */
609 ready[p] = pos->next;
611 if (current_priority != pos->priority)
613 current_priority = pos->priority;
614 (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
619 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
620 DELAY_THRESHOLD.rel_value)
622 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
623 "Task %llu took %llums to be scheduled\n",
625 (unsigned long long) GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value);
628 tc.reason = pos->reason;
629 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
630 if ( (pos->read_fd != -1) &&
631 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
632 GNUNET_NETWORK_fdset_set_native (rs,
634 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
635 if ( (pos->write_fd != -1) &&
636 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) )
637 GNUNET_NETWORK_fdset_set_native (ws,
639 if ( ( (tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
640 (pos->write_fd != -1) &&
641 (! GNUNET_NETWORK_fdset_test_native (ws,
643 abort (); // added to ready in previous select loop!
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "Running task: %llu / %p\n", pos->id, pos->callback_cls);
648 pos->callback (pos->callback_cls, &tc);
651 for (i=0;i<pos->num_backtrace_strings;i++)
652 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
653 "Task %llu trace %d: %s\n",
656 pos->backtrace_strings[i]);
662 while ( (pending == NULL) || (p >= max_priority_added) );
666 * Pipe used to communicate shutdown via signal.
668 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
671 * Signal handler called for SIGPIPE.
681 * Signal handler called for signals that should cause us to shutdown.
684 sighandler_shutdown ()
687 int old_errno = errno; /* backup errno */
689 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
690 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE), &c,
697 * Initialize and run scheduler. This function will return when all
698 * tasks have completed. On systems with signals, receiving a SIGTERM
699 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
700 * to be run after the active task is complete. As a result, SIGTERM
701 * causes all active tasks to be scheduled with reason
702 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
703 * afterwards will execute normally!). Note that any particular signal
704 * will only shut down one scheduler; applications should always only
705 * create a single scheduler.
707 * @param task task to run immediately
708 * @param task_cls closure of task
711 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
713 struct GNUNET_NETWORK_FDSet *rs;
714 struct GNUNET_NETWORK_FDSet *ws;
715 struct GNUNET_TIME_Relative timeout;
717 struct GNUNET_SIGNAL_Context *shc_int;
718 struct GNUNET_SIGNAL_Context *shc_term;
720 struct GNUNET_SIGNAL_Context *shc_quit;
721 struct GNUNET_SIGNAL_Context *shc_hup;
722 struct GNUNET_SIGNAL_Context *shc_pipe;
724 unsigned long long last_tr;
725 unsigned int busy_wait_warning;
726 const struct GNUNET_DISK_FileHandle *pr;
729 GNUNET_assert (active_task == NULL);
730 rs = GNUNET_NETWORK_fdset_create ();
731 ws = GNUNET_NETWORK_fdset_create ();
732 GNUNET_assert (shutdown_pipe_handle == NULL);
733 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
734 GNUNET_assert (shutdown_pipe_handle != NULL);
735 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_READ);
736 GNUNET_assert (pr != NULL);
737 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
738 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
740 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
741 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
742 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
744 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
745 GNUNET_SCHEDULER_add_continuation (task,
747 GNUNET_SCHEDULER_REASON_STARTUP);
748 #if ENABLE_WINDOWS_WORKAROUNDS
749 GNUNET_SCHEDULER_add_continuation (&GNUNET_OS_install_parent_control_handler,
750 NULL, GNUNET_SCHEDULER_REASON_STARTUP);
753 busy_wait_warning = 0;
754 while ((pending != NULL
755 #if ENABLE_WINDOWS_WORKAROUNDS
756 && (pending->callback != parent_control_handler || pending->next != 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());
785 if (0 != system (lsof))
786 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "system");
792 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
794 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
795 _("Looks like we're busy waiting...\n"));
796 sleep (1); /* mitigate */
798 check_ready (rs, ws);
800 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
802 /* consume the signal */
803 GNUNET_DISK_file_read (pr, &c, sizeof (c));
804 /* mark all active tasks as ready due to shutdown */
805 GNUNET_SCHEDULER_shutdown ();
807 if (last_tr == tasks_run)
814 busy_wait_warning = 0;
817 GNUNET_SIGNAL_handler_uninstall (shc_int);
818 GNUNET_SIGNAL_handler_uninstall (shc_term);
820 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
821 GNUNET_SIGNAL_handler_uninstall (shc_quit);
822 GNUNET_SIGNAL_handler_uninstall (shc_hup);
824 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
825 shutdown_pipe_handle = NULL;
826 GNUNET_NETWORK_fdset_destroy (rs);
827 GNUNET_NETWORK_fdset_destroy (ws);
832 * Obtain the reason code for why the current task was
833 * started. Will return the same value as
834 * the GNUNET_SCHEDULER_TaskContext's reason field.
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 p priority level to look at
853 * @return number of tasks pending right now
856 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
861 GNUNET_assert (active_task != NULL);
862 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
864 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
865 p = current_priority;
867 pos = ready[check_priority (p)];
878 * Cancel the task with the specified identifier.
879 * The task must not yet have run.
881 * @param task id of the task to cancel
882 * @return original closure of the task
885 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
889 enum GNUNET_SCHEDULER_Priority p;
893 GNUNET_assert (active_task != NULL);
916 if (pending_timeout_last == t)
917 pending_timeout_last = NULL;
923 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT)
925 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
926 _("Attempt to cancel dead task %llu!\n"),
927 (unsigned long long) task);
953 pending_timeout = t->next;
963 prev->next = t->next;
965 ret = t->callback_cls;
967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
968 "Canceling task: %llu / %p\n", task, t->callback_cls);
976 * Continue the current execution with the given function. This is
977 * similar to the other "add" functions except that there is no delay
978 * and the reason code can be specified.
980 * @param task main function of the task
981 * @param task_cls closure for 'main'
982 * @param reason reason for task invocation
985 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task,
987 enum GNUNET_SCHEDULER_Reason reason)
991 void *backtrace_array[50];
994 GNUNET_assert ( (active_task != NULL) ||
995 (reason == GNUNET_SCHEDULER_REASON_STARTUP) );
996 t = GNUNET_malloc (sizeof (struct Task));
998 t->num_backtrace_strings = backtrace(backtrace_array, 50);
999 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1004 t->callback_cls = task_cls;
1007 t->start_time = GNUNET_TIME_absolute_get ();
1010 t->priority = current_priority;
1012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1013 "Adding continuation task: %llu / %p\n",
1014 t->id, t->callback_cls);
1016 queue_ready_task (t);
1022 * Schedule a new task to be run after the specified prerequisite task
1023 * has completed. It will be run with the priority of the calling
1026 * @param prerequisite_task run this task after the task with the given
1027 * task identifier completes (and any of our other
1028 * conditions, such as delay, read or write-readiness
1029 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1030 * on completion of other tasks (this will cause the task to run as
1031 * soon as possible).
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_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1039 GNUNET_SCHEDULER_Task task, void *task_cls)
1041 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1043 GNUNET_TIME_UNIT_ZERO,
1044 NULL, NULL, task, task_cls);
1049 * Schedule a new task to be run with a specified priority.
1051 * @param prio how important is the new task?
1052 * @param task main function of the task
1053 * @param task_cls closure of task
1054 * @return unique task identifier for the job
1055 * only valid until "task" is started!
1057 GNUNET_SCHEDULER_TaskIdentifier
1058 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1059 GNUNET_SCHEDULER_Task task,
1062 return GNUNET_SCHEDULER_add_select (prio,
1063 GNUNET_SCHEDULER_NO_TASK,
1064 GNUNET_TIME_UNIT_ZERO,
1065 NULL, NULL, task, task_cls);
1071 * Schedule a new task to be run with a specified delay. The task
1072 * will be scheduled for execution once the delay has expired. It
1073 * will be run with the priority of the calling task.
1075 * @param delay when should this operation time out? Use
1076 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1077 * @param task main function of the task
1078 * @param task_cls closure of task
1079 * @return unique task identifier for the job
1080 * only valid until "task" is started!
1082 GNUNET_SCHEDULER_TaskIdentifier
1083 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1084 GNUNET_SCHEDULER_Task task, void *task_cls)
1087 /* new, optimized version */
1092 void *backtrace_array[MAX_TRACE_DEPTH];
1095 GNUNET_assert (active_task != NULL);
1096 GNUNET_assert (NULL != task);
1097 t = GNUNET_malloc (sizeof (struct Task));
1099 t->callback_cls = task_cls;
1101 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1102 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1108 t->start_time = GNUNET_TIME_absolute_get ();
1110 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1111 t->priority = current_priority;
1112 /* try tail first (optimization in case we are
1113 appending to a long list of tasks with timeouts) */
1114 prev = pending_timeout_last;
1117 if (prev->timeout.abs_value > t->timeout.abs_value)
1120 pos = prev->next; /* heuristic success! */
1124 /* heuristic failed, do traversal of timeout list */
1125 pos = pending_timeout;
1127 while ( (pos != NULL) &&
1128 ( (pos->timeout.abs_value <= t->timeout.abs_value) ||
1129 (pos->reason != 0) ) )
1135 pending_timeout = t;
1139 /* hyper-optimization... */
1140 pending_timeout_last = t;
1143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1144 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1149 for (i=0;i<t->num_backtrace_strings;i++)
1150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1151 "Task %llu trace %d: %s\n",
1154 t->backtrace_strings[i]);
1159 /* unoptimized version */
1160 return GNUNET_SCHEDULER_add_select (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 task main function of the task
1173 * @param task_cls closure of task
1174 * @return unique task identifier for the job
1175 * only valid until "task" is started!
1177 GNUNET_SCHEDULER_TaskIdentifier
1178 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task,
1181 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1182 GNUNET_SCHEDULER_NO_TASK,
1183 GNUNET_TIME_UNIT_ZERO,
1184 NULL, NULL, task, task_cls);
1191 * Schedule a new task to be run with a specified delay or when any of
1192 * the specified file descriptor sets is ready. The delay can be used
1193 * as a timeout on the socket(s) being ready. The task will be
1194 * scheduled for execution once either the delay has expired or any of
1195 * the socket operations is ready. This is the most general
1196 * function of the "add" family. Note that the "prerequisite_task"
1197 * must be satisfied in addition to any of the other conditions. In
1198 * other words, the task will be started when
1200 * (prerequisite-run)
1204 * || shutdown-active )
1207 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1208 * which means that the task will only be run after we receive SIGTERM
1209 * @param rfd file descriptor we want to read (can be -1)
1210 * @param wfd file descriptors we want to write (can be -1)
1211 * @param task main function of the task
1212 * @param task_cls closure of task
1213 * @return unique task identifier for the job
1214 * only valid until "task" is started!
1217 GNUNET_SCHEDULER_TaskIdentifier
1218 add_without_sets (struct GNUNET_TIME_Relative delay,
1221 GNUNET_SCHEDULER_Task task, void *task_cls)
1225 void *backtrace_array[MAX_TRACE_DEPTH];
1228 GNUNET_assert (active_task != NULL);
1229 GNUNET_assert (NULL != task);
1230 t = GNUNET_malloc (sizeof (struct Task));
1232 t->callback_cls = task_cls;
1234 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1235 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1240 int flags = fcntl(rfd, F_GETFD);
1241 if (flags == -1 && errno == EBADF)
1243 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", rfd);
1247 for (i=0;i<t->num_backtrace_strings;i++)
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1250 t->backtrace_strings[i]);
1257 int flags = fcntl(wfd, F_GETFD);
1258 if (flags == -1 && errno == EBADF)
1260 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", wfd);
1264 for (i=0;i<t->num_backtrace_strings;i++)
1265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1267 t->backtrace_strings[i]);
1274 GNUNET_assert(wfd >= -1);
1278 t->start_time = GNUNET_TIME_absolute_get ();
1280 t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
1281 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1282 t->priority = check_priority (current_priority);
1285 max_priority_added = GNUNET_MAX (max_priority_added,
1288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1289 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1294 for (i=0;i<t->num_backtrace_strings;i++)
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1296 "Task %llu trace %d: %s\n",
1299 t->backtrace_strings[i]);
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_net (struct GNUNET_TIME_Relative delay,
1325 struct GNUNET_NETWORK_Handle * 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_set (rs, rfd);
1335 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1336 GNUNET_SCHEDULER_NO_TASK, delay,
1337 rs, NULL, task, task_cls);
1338 GNUNET_NETWORK_fdset_destroy (rs);
1341 return add_without_sets (delay,
1342 GNUNET_NETWORK_get_fd (rfd),
1351 * Schedule a new task to be run with a specified delay or when the
1352 * specified file descriptor is ready for writing. The delay can be
1353 * used as a timeout on the socket being ready. The task will be
1354 * scheduled for execution once either the delay has expired or the
1355 * socket operation is ready. It will be run with the priority of
1358 * @param delay when should this operation time out? Use
1359 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1360 * @param wfd write file-descriptor
1361 * @param task main function of the task
1362 * @param task_cls closure of task
1363 * @return unique task identifier for the job
1364 * only valid until "task" is started!
1366 GNUNET_SCHEDULER_TaskIdentifier
1367 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1368 struct GNUNET_NETWORK_Handle * wfd,
1369 GNUNET_SCHEDULER_Task task, void *task_cls)
1372 struct GNUNET_NETWORK_FDSet *ws;
1373 GNUNET_SCHEDULER_TaskIdentifier ret;
1375 GNUNET_assert (wfd != NULL);
1376 ws = GNUNET_NETWORK_fdset_create ();
1377 GNUNET_NETWORK_fdset_set (ws, wfd);
1378 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1379 GNUNET_SCHEDULER_NO_TASK, delay,
1380 NULL, ws, task, task_cls);
1381 GNUNET_NETWORK_fdset_destroy (ws);
1384 GNUNET_assert(GNUNET_NETWORK_get_fd(wfd) >= 0);
1385 return add_without_sets (delay,
1387 GNUNET_NETWORK_get_fd (wfd),
1395 * Schedule a new task to be run with a specified delay or when the
1396 * specified file descriptor is ready for reading. The delay can be
1397 * used as a timeout on the socket being ready. The task will be
1398 * scheduled for execution once either the delay has expired or the
1399 * socket operation is ready. It will be run with the priority of
1402 * @param delay when should this operation time out? Use
1403 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1404 * @param rfd read file-descriptor
1405 * @param task main function of the task
1406 * @param task_cls closure of task
1407 * @return unique task identifier for the job
1408 * only valid until "task" is started!
1410 GNUNET_SCHEDULER_TaskIdentifier
1411 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1412 const struct GNUNET_DISK_FileHandle * rfd,
1413 GNUNET_SCHEDULER_Task task, void *task_cls)
1416 struct GNUNET_NETWORK_FDSet *rs;
1417 GNUNET_SCHEDULER_TaskIdentifier ret;
1419 GNUNET_assert (rfd != NULL);
1420 rs = GNUNET_NETWORK_fdset_create ();
1421 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1422 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1423 GNUNET_SCHEDULER_NO_TASK, delay,
1424 rs, NULL, task, task_cls);
1425 GNUNET_NETWORK_fdset_destroy (rs);
1430 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1431 return add_without_sets (delay,
1442 * Schedule a new task to be run with a specified delay or when the
1443 * specified file descriptor is ready for writing. The delay can be
1444 * used as a timeout on the socket being ready. The task will be
1445 * scheduled for execution once either the delay has expired or the
1446 * socket operation is ready. It will be run with the priority of
1449 * @param delay when should this operation time out? Use
1450 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1451 * @param wfd write file-descriptor
1452 * @param task main function of the task
1453 * @param task_cls closure of task
1454 * @return unique task identifier for the job
1455 * only valid until "task" is started!
1457 GNUNET_SCHEDULER_TaskIdentifier
1458 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1459 const struct GNUNET_DISK_FileHandle * wfd,
1460 GNUNET_SCHEDULER_Task task, void *task_cls)
1463 struct GNUNET_NETWORK_FDSet *ws;
1464 GNUNET_SCHEDULER_TaskIdentifier ret;
1466 GNUNET_assert (wfd != NULL);
1467 ws = GNUNET_NETWORK_fdset_create ();
1468 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1469 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1470 GNUNET_SCHEDULER_NO_TASK,
1471 delay, NULL, ws, task, task_cls);
1472 GNUNET_NETWORK_fdset_destroy (ws);
1477 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1478 GNUNET_assert(fd >= 0);
1479 return add_without_sets (delay,
1491 * Schedule a new task to be run with a specified delay or when any of
1492 * the specified file descriptor sets is ready. The delay can be used
1493 * as a timeout on the socket(s) being ready. The task will be
1494 * scheduled for execution once either the delay has expired or any of
1495 * the socket operations is ready. This is the most general
1496 * function of the "add" family. Note that the "prerequisite_task"
1497 * must be satisfied in addition to any of the other conditions. In
1498 * other words, the task will be started when
1500 * (prerequisite-run)
1504 * || (shutdown-active && run-on-shutdown) )
1507 * @param prio how important is this task?
1508 * @param prerequisite_task run this task after the task with the given
1509 * task identifier completes (and any of our other
1510 * conditions, such as delay, read or write-readiness
1511 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1512 * on completion of other tasks.
1513 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1514 * which means that the task will only be run after we receive SIGTERM
1515 * @param rs set of file descriptors we want to read (can be NULL)
1516 * @param ws set of file descriptors we want to write (can be NULL)
1517 * @param task main function of the task
1518 * @param task_cls closure of task
1519 * @return unique task identifier for the job
1520 * only valid until "task" is started!
1522 GNUNET_SCHEDULER_TaskIdentifier
1523 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1524 GNUNET_SCHEDULER_TaskIdentifier
1526 struct GNUNET_TIME_Relative delay,
1527 const struct GNUNET_NETWORK_FDSet * rs,
1528 const struct GNUNET_NETWORK_FDSet * ws,
1529 GNUNET_SCHEDULER_Task task, void *task_cls)
1533 void *backtrace_array[MAX_TRACE_DEPTH];
1536 GNUNET_assert (active_task != NULL);
1537 GNUNET_assert (NULL != task);
1538 t = GNUNET_malloc (sizeof (struct Task));
1540 t->callback_cls = task_cls;
1542 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1543 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1549 t->read_set = GNUNET_NETWORK_fdset_create ();
1550 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1554 t->write_set = GNUNET_NETWORK_fdset_create ();
1555 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1559 t->start_time = GNUNET_TIME_absolute_get ();
1561 t->prereq_id = prerequisite_task;
1562 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1564 check_priority ((prio ==
1565 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority
1569 max_priority_added = GNUNET_MAX (max_priority_added,
1572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1573 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1578 for (i=0;i<t->num_backtrace_strings;i++)
1579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1580 "Task %llu trace %d: %s\n",
1583 t->backtrace_strings[i]);
1588 /* end of scheduler.c */