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 sched the scheduler
256 * @param id which task are we checking for
257 * @return GNUNET_YES if so, GNUNET_NO if not
260 is_pending (GNUNET_SCHEDULER_TaskIdentifier id)
263 enum GNUNET_SCHEDULER_Priority p;
264 GNUNET_SCHEDULER_TaskIdentifier min;
266 if (id < lowest_pending_id)
268 min = -1; /* maximum value */
278 pos = pending_timeout;
287 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
299 lowest_pending_id = min;
305 * Update all sets and timeout for select.
307 * @param sched the scheduler
308 * @param rs read-set, set to all FDs we would like to read (updated)
309 * @param ws write-set, set to all FDs we would like to write (updated)
310 * @param timeout next timeout (updated)
313 update_sets (struct GNUNET_NETWORK_FDSet *rs,
314 struct GNUNET_NETWORK_FDSet *ws,
315 struct GNUNET_TIME_Relative *timeout)
318 struct GNUNET_TIME_Absolute now;
319 struct GNUNET_TIME_Relative to;
321 now = GNUNET_TIME_absolute_get ();
322 pos = pending_timeout;
325 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
326 if (timeout->rel_value > to.rel_value)
328 if (pos->reason != 0)
329 *timeout = GNUNET_TIME_UNIT_ZERO;
334 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
335 (GNUNET_YES == is_pending (pos->prereq_id)))
340 if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
342 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
343 if (timeout->rel_value > to.rel_value)
346 if (pos->read_fd != -1)
347 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
348 if (pos->write_fd != -1)
349 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
350 if (pos->read_set != NULL)
351 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
352 if (pos->write_set != NULL)
353 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
354 if (pos->reason != 0)
355 *timeout = GNUNET_TIME_UNIT_ZERO;
362 * Check if the ready set overlaps with the set we want to have ready.
363 * If so, update the want set (set all FDs that are ready). If not,
366 * @param ready set that is ready
367 * @param want set that we want to be ready
368 * @return GNUNET_YES if there was some overlap
371 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
372 struct GNUNET_NETWORK_FDSet *want)
374 if ( (NULL == want) || (NULL == ready) )
376 if (GNUNET_NETWORK_fdset_overlap (ready, want))
378 /* copy all over (yes, there maybe unrelated bits,
379 but this should not hurt well-written clients) */
380 GNUNET_NETWORK_fdset_copy (want, ready);
388 * Check if the given task is eligible to run now.
389 * Also set the reason why it is eligible.
391 * @param sched the scheduler
392 * @param task task to check if it is ready
393 * @param now the current time
394 * @param rs set of FDs ready for reading
395 * @param ws set of FDs ready for writing
396 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
399 is_ready (struct Task *task,
400 struct GNUNET_TIME_Absolute now,
401 const struct GNUNET_NETWORK_FDSet *rs,
402 const struct GNUNET_NETWORK_FDSet *ws)
404 enum GNUNET_SCHEDULER_Reason reason;
406 reason = task->reason;
407 if (now.abs_value >= task->timeout.abs_value)
408 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
409 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
410 ( ( (task->read_fd != -1) &&
411 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd)) ) ||
412 (set_overlaps (rs, task->read_set) ) ) )
413 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
414 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
415 ( ( (task->write_fd != -1) &&
416 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)) ) ||
417 (set_overlaps (ws, task->write_set) ) ) )
418 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
420 return GNUNET_NO; /* not ready */
421 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
423 if (GNUNET_YES == is_pending (task->prereq_id))
425 task->reason = reason;
426 return GNUNET_NO; /* prereq waiting */
428 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
430 task->reason = reason;
436 * Put a task that is ready for execution into the ready queue.
438 * @param handle the scheduler
439 * @param task task ready for execution
442 queue_ready_task (struct Task *task)
444 enum GNUNET_SCHEDULER_Priority p = task->priority;
445 if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
446 p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
447 task->next = ready[check_priority (p)];
448 ready[check_priority (p)] = task;
454 * Check which tasks are ready and move them
455 * to the respective ready queue.
457 * @param handle the scheduler
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.
518 * @param sched the scheduler
521 GNUNET_SCHEDULER_shutdown ()
526 GNUNET_assert (active_task != NULL);
527 pos = pending_timeout;
530 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
531 /* we don't move the task into the ready queue yet; check_ready
532 will do that later, possibly adding additional
539 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
540 /* we don't move the task into the ready queue yet; check_ready
541 will do that later, possibly adding additional
545 for (i=0;i<GNUNET_SCHEDULER_PRIORITY_COUNT;i++)
550 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
551 /* we don't move the task into the ready queue yet; check_ready
552 will do that later, possibly adding additional
561 * Destroy a task (release associated resources)
563 * @param t task to destroy
566 destroy_task (struct Task *t)
568 if (NULL != t->read_set)
569 GNUNET_NETWORK_fdset_destroy (t->read_set);
570 if (NULL != t->write_set)
571 GNUNET_NETWORK_fdset_destroy (t->write_set);
573 GNUNET_free (t->backtrace_strings);
580 * Run at least one task in the highest-priority queue that is not
581 * empty. Keep running tasks until we are either no longer running
582 * "URGENT" tasks or until we have at least one "pending" task (which
583 * may become ready, hence we should select on it). Naturally, if
584 * there are no more ready tasks, we also return.
586 * @param sched the scheduler
587 * @param rs FDs ready for reading
588 * @param ws FDs ready for writing
591 run_ready (struct GNUNET_NETWORK_FDSet *rs,
592 struct GNUNET_NETWORK_FDSet *ws)
594 enum GNUNET_SCHEDULER_Priority p;
596 struct GNUNET_SCHEDULER_TaskContext tc;
598 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
601 if (ready_count == 0)
603 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
604 /* yes, p>0 is correct, 0 is "KEEP" which should
605 always be an empty queue (see assertion)! */
606 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
612 GNUNET_assert (pos != NULL); /* ready_count wrong? */
613 ready[p] = pos->next;
615 if (current_priority != pos->priority)
617 current_priority = pos->priority;
618 (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (), pos->priority);
622 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
623 DELAY_THRESHOLD.rel_value)
625 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
626 "Task %llu took %llums to be scheduled\n",
628 (unsigned long long) GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value);
631 tc.reason = pos->reason;
632 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
633 if ( (pos->read_fd != -1) &&
634 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
635 GNUNET_NETWORK_fdset_set_native (rs,
637 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
638 if ( (pos->write_fd != -1) &&
639 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) )
640 GNUNET_NETWORK_fdset_set_native (ws,
642 if ( ( (tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
643 (pos->write_fd != -1) &&
644 (! GNUNET_NETWORK_fdset_test_native (ws,
646 abort (); // added to ready in previous select loop!
648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
649 "Running task: %llu / %p\n", pos->id, pos->callback_cls);
651 pos->callback (pos->callback_cls, &tc);
654 for (i=0;i<pos->num_backtrace_strings;i++)
655 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
656 "Task %llu trace %d: %s\n",
659 pos->backtrace_strings[i]);
665 while ( (pending == NULL) || (p >= max_priority_added) );
669 * Pipe used to communicate shutdown via signal.
671 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
674 * Signal handler called for SIGPIPE.
684 * Signal handler called for signals that should cause us to shutdown.
687 sighandler_shutdown ()
690 int old_errno = errno; /* backup errno */
692 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
693 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE), &c,
700 * Initialize and run scheduler. This function will return when all
701 * tasks have completed. On systems with signals, receiving a SIGTERM
702 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
703 * to be run after the active task is complete. As a result, SIGTERM
704 * causes all active tasks to be scheduled with reason
705 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
706 * afterwards will execute normally!). Note that any particular signal
707 * will only shut down one scheduler; applications should always only
708 * create a single scheduler.
710 * @param task task to run immediately
711 * @param task_cls closure of task
714 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
716 struct GNUNET_NETWORK_FDSet *rs;
717 struct GNUNET_NETWORK_FDSet *ws;
718 struct GNUNET_TIME_Relative timeout;
720 struct GNUNET_SIGNAL_Context *shc_int;
721 struct GNUNET_SIGNAL_Context *shc_term;
723 struct GNUNET_SIGNAL_Context *shc_quit;
724 struct GNUNET_SIGNAL_Context *shc_hup;
725 struct GNUNET_SIGNAL_Context *shc_pipe;
727 unsigned long long last_tr;
728 unsigned int busy_wait_warning;
729 const struct GNUNET_DISK_FileHandle *pr;
732 GNUNET_assert (active_task == NULL);
733 rs = GNUNET_NETWORK_fdset_create ();
734 ws = GNUNET_NETWORK_fdset_create ();
735 GNUNET_assert (shutdown_pipe_handle == NULL);
736 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
737 GNUNET_assert (shutdown_pipe_handle != NULL);
738 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_READ);
739 GNUNET_assert (pr != NULL);
740 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
741 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
743 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
744 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
745 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
747 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
748 GNUNET_SCHEDULER_add_continuation (task,
750 GNUNET_SCHEDULER_REASON_STARTUP);
752 busy_wait_warning = 0;
753 while ((pending != NULL) ||
754 (pending_timeout != NULL) ||
757 GNUNET_NETWORK_fdset_zero (rs);
758 GNUNET_NETWORK_fdset_zero (ws);
759 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
760 update_sets (rs, ws, &timeout);
761 GNUNET_NETWORK_fdset_handle_set (rs, pr);
764 /* no blocking, more work already ready! */
765 timeout = GNUNET_TIME_UNIT_ZERO;
767 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
768 if (ret == GNUNET_SYSERR)
773 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
777 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid());
786 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
788 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
789 _("Looks like we're busy waiting...\n"));
790 sleep (1); /* mitigate */
792 check_ready (rs, ws);
794 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
796 /* consume the signal */
797 GNUNET_DISK_file_read (pr, &c, sizeof (c));
798 /* mark all active tasks as ready due to shutdown */
799 GNUNET_SCHEDULER_shutdown ();
801 if (last_tr == tasks_run)
808 busy_wait_warning = 0;
811 GNUNET_SIGNAL_handler_uninstall (shc_int);
812 GNUNET_SIGNAL_handler_uninstall (shc_term);
814 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
815 GNUNET_SIGNAL_handler_uninstall (shc_quit);
816 GNUNET_SIGNAL_handler_uninstall (shc_hup);
818 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
819 shutdown_pipe_handle = NULL;
820 GNUNET_NETWORK_fdset_destroy (rs);
821 GNUNET_NETWORK_fdset_destroy (ws);
826 * Obtain the reason code for why the current task was
827 * started. Will return the same value as
828 * the GNUNET_SCHEDULER_TaskContext's reason field.
830 * @param sched scheduler to query
831 * @return reason(s) why the current task is run
833 enum GNUNET_SCHEDULER_Reason
834 GNUNET_SCHEDULER_get_reason ()
836 GNUNET_assert (active_task != NULL);
837 return active_task->reason;
842 * Get information about the current load of this scheduler. Use this
843 * function to determine if an elective task should be added or simply
844 * dropped (if the decision should be made based on the number of
845 * tasks ready to run).
847 * @param sched scheduler to query
848 * @param p priority level to look at
849 * @return number of tasks pending right now
852 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
857 GNUNET_assert (active_task != NULL);
858 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
860 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
861 p = current_priority;
863 pos = ready[check_priority (p)];
874 * Cancel the task with the specified identifier.
875 * The task must not yet have run.
877 * @param sched scheduler to use
878 * @param task id of the task to cancel
879 * @return original closure of the task
882 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
886 enum GNUNET_SCHEDULER_Priority p;
890 GNUNET_assert (active_task != NULL);
913 if (pending_timeout_last == t)
914 pending_timeout_last = NULL;
920 GNUNET_assert (p < GNUNET_SCHEDULER_PRIORITY_COUNT);
944 pending_timeout = t->next;
954 prev->next = t->next;
956 ret = t->callback_cls;
958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
959 "Canceling task: %llu / %p\n", task, t->callback_cls);
967 * Continue the current execution with the given function. This is
968 * similar to the other "add" functions except that there is no delay
969 * and the reason code can be specified.
971 * @param sched scheduler to use
972 * @param task main function of the task
973 * @param task_cls closure for 'main'
974 * @param reason reason for task invocation
977 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task,
979 enum GNUNET_SCHEDULER_Reason reason)
983 void *backtrace_array[50];
986 GNUNET_assert ( (active_task != NULL) ||
987 (reason == GNUNET_SCHEDULER_REASON_STARTUP) );
988 t = GNUNET_malloc (sizeof (struct Task));
990 t->num_backtrace_strings = backtrace(backtrace_array, 50);
991 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
996 t->callback_cls = task_cls;
999 t->start_time = GNUNET_TIME_absolute_get ();
1002 t->priority = current_priority;
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005 "Adding continuation task: %llu / %p\n",
1006 t->id, t->callback_cls);
1008 queue_ready_task (t);
1014 * Schedule a new task to be run after the specified prerequisite task
1015 * has completed. It will be run with the priority of the calling
1018 * @param sched scheduler to use
1019 * @param prerequisite_task run this task after the task with the given
1020 * task identifier completes (and any of our other
1021 * conditions, such as delay, read or write-readiness
1022 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1023 * on completion of other tasks (this will cause the task to run as
1024 * soon as possible).
1025 * @param task main function of the task
1026 * @param task_cls closure of task
1027 * @return unique task identifier for the job
1028 * only valid until "task" is started!
1030 GNUNET_SCHEDULER_TaskIdentifier
1031 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1032 GNUNET_SCHEDULER_Task task, void *task_cls)
1034 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1036 GNUNET_TIME_UNIT_ZERO,
1037 NULL, NULL, task, task_cls);
1042 * Schedule a new task to be run with a specified priority.
1044 * @param sched scheduler to use
1045 * @param prio how important is the new task?
1046 * @param task main function of the task
1047 * @param task_cls closure of task
1048 * @return unique task identifier for the job
1049 * only valid until "task" is started!
1051 GNUNET_SCHEDULER_TaskIdentifier
1052 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1053 GNUNET_SCHEDULER_Task task,
1056 return GNUNET_SCHEDULER_add_select (prio,
1057 GNUNET_SCHEDULER_NO_TASK,
1058 GNUNET_TIME_UNIT_ZERO,
1059 NULL, NULL, task, task_cls);
1065 * Schedule a new task to be run with a specified delay. The task
1066 * will be scheduled for execution once the delay has expired. It
1067 * will be run with the priority of the calling task.
1069 * @param sched scheduler to use
1070 * @param delay when should this operation time out? Use
1071 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1072 * @param task main function of the task
1073 * @param task_cls closure of task
1074 * @return unique task identifier for the job
1075 * only valid until "task" is started!
1077 GNUNET_SCHEDULER_TaskIdentifier
1078 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1079 GNUNET_SCHEDULER_Task task, void *task_cls)
1082 /* new, optimized version */
1087 void *backtrace_array[MAX_TRACE_DEPTH];
1090 GNUNET_assert (active_task != NULL);
1091 GNUNET_assert (NULL != task);
1092 t = GNUNET_malloc (sizeof (struct Task));
1094 t->callback_cls = task_cls;
1096 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1097 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1103 t->start_time = GNUNET_TIME_absolute_get ();
1105 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1106 t->priority = current_priority;
1107 /* try tail first (optimization in case we are
1108 appending to a long list of tasks with timeouts) */
1109 prev = pending_timeout_last;
1112 if (prev->timeout.abs_value > t->timeout.abs_value)
1115 pos = prev->next; /* heuristic success! */
1119 /* heuristic failed, do traversal of timeout list */
1120 pos = pending_timeout;
1122 while ( (pos != NULL) &&
1123 ( (pos->timeout.abs_value <= t->timeout.abs_value) ||
1124 (pos->reason != 0) ) )
1130 pending_timeout = t;
1134 /* hyper-optimization... */
1135 pending_timeout_last = t;
1138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1139 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1144 for (i=0;i<t->num_backtrace_strings;i++)
1145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146 "Task %llu trace %d: %s\n",
1149 t->backtrace_strings[i]);
1154 /* unoptimized version */
1155 return GNUNET_SCHEDULER_add_select (sched,
1156 GNUNET_SCHEDULER_PRIORITY_KEEP,
1157 GNUNET_SCHEDULER_NO_TASK, delay,
1158 NULL, NULL, task, task_cls);
1165 * Schedule a new task to be run as soon as possible. The task
1166 * will be run with the priority of the calling task.
1168 * @param sched scheduler to use
1169 * @param task main function of the task
1170 * @param task_cls closure of task
1171 * @return unique task identifier for the job
1172 * only valid until "task" is started!
1174 GNUNET_SCHEDULER_TaskIdentifier
1175 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task,
1178 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1179 GNUNET_SCHEDULER_NO_TASK,
1180 GNUNET_TIME_UNIT_ZERO,
1181 NULL, NULL, task, task_cls);
1188 * Schedule a new task to be run with a specified delay or when any of
1189 * the specified file descriptor sets is ready. The delay can be used
1190 * as a timeout on the socket(s) being ready. The task will be
1191 * scheduled for execution once either the delay has expired or any of
1192 * the socket operations is ready. This is the most general
1193 * function of the "add" family. Note that the "prerequisite_task"
1194 * must be satisfied in addition to any of the other conditions. In
1195 * other words, the task will be started when
1197 * (prerequisite-run)
1201 * || shutdown-active )
1204 * @param sched scheduler to use
1205 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1206 * which means that the task will only be run after we receive SIGTERM
1207 * @param rfd file descriptor we want to read (can be -1)
1208 * @param wfd file descriptors we want to write (can be -1)
1209 * @param task main function of the task
1210 * @param task_cls closure of task
1211 * @return unique task identifier for the job
1212 * only valid until "task" is started!
1214 GNUNET_SCHEDULER_TaskIdentifier
1215 add_without_sets (struct GNUNET_TIME_Relative delay,
1218 GNUNET_SCHEDULER_Task task, void *task_cls)
1222 void *backtrace_array[MAX_TRACE_DEPTH];
1225 GNUNET_assert (active_task != NULL);
1226 GNUNET_assert (NULL != task);
1227 t = GNUNET_malloc (sizeof (struct Task));
1229 t->callback_cls = task_cls;
1231 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1232 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1238 t->start_time = GNUNET_TIME_absolute_get ();
1240 t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
1241 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1242 t->priority = check_priority (current_priority);
1245 max_priority_added = GNUNET_MAX (max_priority_added,
1248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1249 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1254 for (i=0;i<t->num_backtrace_strings;i++)
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1256 "Task %llu trace %d: %s\n",
1259 t->backtrace_strings[i]);
1267 * Schedule a new task to be run with a specified delay or when the
1268 * specified file descriptor is ready for reading. The delay can be
1269 * used as a timeout on the socket being ready. The task will be
1270 * scheduled for execution once either the delay has expired or the
1271 * socket operation is ready. It will be run with the priority of
1274 * @param sched scheduler to use
1275 * @param delay when should this operation time out? Use
1276 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1277 * @param rfd read file-descriptor
1278 * @param task main function of the task
1279 * @param task_cls closure of task
1280 * @return unique task identifier for the job
1281 * only valid until "task" is started!
1283 GNUNET_SCHEDULER_TaskIdentifier
1284 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1285 struct GNUNET_NETWORK_Handle * rfd,
1286 GNUNET_SCHEDULER_Task task, void *task_cls)
1288 return add_without_sets (delay,
1289 GNUNET_NETWORK_get_fd (rfd),
1297 * Schedule a new task to be run with a specified delay or when the
1298 * specified file descriptor is ready for writing. The delay can be
1299 * used as a timeout on the socket being ready. The task will be
1300 * scheduled for execution once either the delay has expired or the
1301 * socket operation is ready. It will be run with the priority of
1304 * @param sched scheduler to use
1305 * @param delay when should this operation time out? Use
1306 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1307 * @param wfd write file-descriptor
1308 * @param task main function of the task
1309 * @param task_cls closure of task
1310 * @return unique task identifier for the job
1311 * only valid until "task" is started!
1313 GNUNET_SCHEDULER_TaskIdentifier
1314 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1315 struct GNUNET_NETWORK_Handle * wfd,
1316 GNUNET_SCHEDULER_Task task, void *task_cls)
1318 return add_without_sets (delay,
1320 GNUNET_NETWORK_get_fd (wfd),
1327 * Schedule a new task to be run with a specified delay or when the
1328 * specified file descriptor is ready for reading. The delay can be
1329 * used as a timeout on the socket being ready. The task will be
1330 * scheduled for execution once either the delay has expired or the
1331 * socket operation is ready. It will be run with the priority of
1334 * @param sched scheduler to use
1335 * @param delay when should this operation time out? Use
1336 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1337 * @param rfd read file-descriptor
1338 * @param task main function of the task
1339 * @param task_cls closure of task
1340 * @return unique task identifier for the job
1341 * only valid until "task" is started!
1343 GNUNET_SCHEDULER_TaskIdentifier
1344 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1345 const struct GNUNET_DISK_FileHandle * rfd,
1346 GNUNET_SCHEDULER_Task task, void *task_cls)
1349 struct GNUNET_NETWORK_FDSet *rs;
1350 GNUNET_SCHEDULER_TaskIdentifier ret;
1352 GNUNET_assert (rfd != NULL);
1353 rs = GNUNET_NETWORK_fdset_create ();
1354 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1355 ret = GNUNET_SCHEDULER_add_select (sched,
1356 GNUNET_SCHEDULER_PRIORITY_KEEP,
1357 GNUNET_SCHEDULER_NO_TASK, delay,
1358 rs, NULL, task, task_cls);
1359 GNUNET_NETWORK_fdset_destroy (rs);
1364 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1365 return add_without_sets (delay,
1376 * Schedule a new task to be run with a specified delay or when the
1377 * specified file descriptor is ready for writing. The delay can be
1378 * used as a timeout on the socket being ready. The task will be
1379 * scheduled for execution once either the delay has expired or the
1380 * socket operation is ready. It will be run with the priority of
1383 * @param sched scheduler to use
1384 * @param delay when should this operation time out? Use
1385 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1386 * @param wfd write file-descriptor
1387 * @param task main function of the task
1388 * @param task_cls closure of task
1389 * @return unique task identifier for the job
1390 * only valid until "task" is started!
1392 GNUNET_SCHEDULER_TaskIdentifier
1393 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1394 const struct GNUNET_DISK_FileHandle * wfd,
1395 GNUNET_SCHEDULER_Task task, void *task_cls)
1398 struct GNUNET_NETWORK_FDSet *ws;
1399 GNUNET_SCHEDULER_TaskIdentifier ret;
1401 GNUNET_assert (wfd != NULL);
1402 ws = GNUNET_NETWORK_fdset_create ();
1403 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1404 ret = GNUNET_SCHEDULER_add_select (sched,
1405 GNUNET_SCHEDULER_PRIORITY_KEEP,
1406 GNUNET_SCHEDULER_NO_TASK,
1407 delay, NULL, ws, task, task_cls);
1408 GNUNET_NETWORK_fdset_destroy (ws);
1413 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1414 return add_without_sets (delay,
1426 * Schedule a new task to be run with a specified delay or when any of
1427 * the specified file descriptor sets is ready. The delay can be used
1428 * as a timeout on the socket(s) being ready. The task will be
1429 * scheduled for execution once either the delay has expired or any of
1430 * the socket operations is ready. This is the most general
1431 * function of the "add" family. Note that the "prerequisite_task"
1432 * must be satisfied in addition to any of the other conditions. In
1433 * other words, the task will be started when
1435 * (prerequisite-run)
1439 * || (shutdown-active && run-on-shutdown) )
1442 * @param sched scheduler to use
1443 * @param prio how important is this task?
1444 * @param prerequisite_task run this task after the task with the given
1445 * task identifier completes (and any of our other
1446 * conditions, such as delay, read or write-readiness
1447 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1448 * on completion of other tasks.
1449 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1450 * which means that the task will only be run after we receive SIGTERM
1451 * @param rs set of file descriptors we want to read (can be NULL)
1452 * @param ws set of file descriptors we want to write (can be NULL)
1453 * @param task main function of the task
1454 * @param task_cls closure of task
1455 * @return unique task identifier for the job
1456 * only valid until "task" is started!
1458 GNUNET_SCHEDULER_TaskIdentifier
1459 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1460 GNUNET_SCHEDULER_TaskIdentifier
1462 struct GNUNET_TIME_Relative delay,
1463 const struct GNUNET_NETWORK_FDSet * rs,
1464 const struct GNUNET_NETWORK_FDSet * ws,
1465 GNUNET_SCHEDULER_Task task, void *task_cls)
1469 void *backtrace_array[MAX_TRACE_DEPTH];
1472 GNUNET_assert (active_task != NULL);
1473 GNUNET_assert (NULL != task);
1474 t = GNUNET_malloc (sizeof (struct Task));
1476 t->callback_cls = task_cls;
1478 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1479 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1485 t->read_set = GNUNET_NETWORK_fdset_create ();
1486 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1490 t->write_set = GNUNET_NETWORK_fdset_create ();
1491 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1495 t->start_time = GNUNET_TIME_absolute_get ();
1497 t->prereq_id = prerequisite_task;
1498 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1500 check_priority ((prio ==
1501 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority
1505 max_priority_added = GNUNET_MAX (max_priority_added,
1508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1509 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1514 for (i=0;i<t->num_backtrace_strings;i++)
1515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1516 "Task %llu trace %d: %s\n",
1519 t->backtrace_strings[i]);
1524 /* end of scheduler.c */