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 * Handle for the scheduling service.
168 struct GNUNET_SCHEDULER_Handle
172 * List of tasks waiting for an event.
174 struct Task *pending;
177 * List of tasks waiting ONLY for a timeout event.
178 * Sorted by timeout (earliest first). Used so that
179 * we do not traverse the list of these tasks when
180 * building select sets (we just look at the head
181 * to determine the respective timeout ONCE).
183 struct Task *pending_timeout;
186 * Last inserted task waiting ONLY for a timeout event.
187 * Used to (heuristically) speed up insertion.
189 struct Task *pending_timeout_last;
192 * ID of the task that is running right now.
194 struct Task *active_task;
197 * List of tasks ready to run right now,
198 * grouped by importance.
200 struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
203 * Identity of the last task queued. Incremented for each task to
204 * generate a unique task ID (it is virtually impossible to start
205 * more than 2^64 tasks during the lifetime of a process).
207 GNUNET_SCHEDULER_TaskIdentifier last_id;
210 * Highest number so that all tasks with smaller identifiers
211 * have already completed. Also the lowest number of a task
212 * still waiting to be executed.
214 GNUNET_SCHEDULER_TaskIdentifier lowest_pending_id;
217 * Number of tasks on the ready list.
219 unsigned int ready_count;
222 * How many tasks have we run so far?
224 unsigned long long tasks_run;
227 * Priority of the task running right now. Only
228 * valid while a task is running.
230 enum GNUNET_SCHEDULER_Priority current_priority;
233 * Priority of the highest task added in the current select
236 enum GNUNET_SCHEDULER_Priority max_priority_added;
239 * How 'nice' are we right now?
247 * Check that the given priority is legal (and return it).
249 * @param p priority value to check
250 * @return p on success, 0 on error
252 static enum GNUNET_SCHEDULER_Priority
253 check_priority (enum GNUNET_SCHEDULER_Priority p)
255 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
258 return 0; /* make compiler happy */
263 * Is a task with this identifier still pending? Also updates
264 * "lowest_pending_id" as a side-effect (for faster checks in the
265 * future), but only if the return value is "GNUNET_NO" (and
266 * the "lowest_pending_id" check failed).
268 * @param sched the scheduler
269 * @param id which task are we checking for
270 * @return GNUNET_YES if so, GNUNET_NO if not
273 is_pending (struct GNUNET_SCHEDULER_Handle *sched,
274 GNUNET_SCHEDULER_TaskIdentifier id)
277 enum GNUNET_SCHEDULER_Priority p;
278 GNUNET_SCHEDULER_TaskIdentifier min;
280 if (id < sched->lowest_pending_id)
282 min = -1; /* maximum value */
283 pos = sched->pending;
292 pos = sched->pending_timeout;
301 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
303 pos = sched->ready[p];
313 sched->lowest_pending_id = min;
319 * Update all sets and timeout for select.
321 * @param sched the scheduler
322 * @param rs read-set, set to all FDs we would like to read (updated)
323 * @param ws write-set, set to all FDs we would like to write (updated)
324 * @param timeout next timeout (updated)
327 update_sets (struct GNUNET_SCHEDULER_Handle *sched,
328 struct GNUNET_NETWORK_FDSet *rs,
329 struct GNUNET_NETWORK_FDSet *ws,
330 struct GNUNET_TIME_Relative *timeout)
333 struct GNUNET_TIME_Absolute now;
334 struct GNUNET_TIME_Relative to;
336 now = GNUNET_TIME_absolute_get ();
337 pos = sched->pending_timeout;
340 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
341 if (timeout->value > to.value)
343 if (pos->reason != 0)
344 *timeout = GNUNET_TIME_UNIT_ZERO;
346 pos = sched->pending;
349 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
350 (GNUNET_YES == is_pending (sched, pos->prereq_id)))
355 if (pos->timeout.value != GNUNET_TIME_UNIT_FOREVER_ABS.value)
357 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
358 if (timeout->value > to.value)
361 if (pos->read_fd != -1)
362 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
363 if (pos->write_fd != -1)
364 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
365 if (pos->read_set != NULL)
366 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
367 if (pos->write_set != NULL)
368 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
369 if (pos->reason != 0)
370 *timeout = GNUNET_TIME_UNIT_ZERO;
377 * Check if the ready set overlaps with the set we want to have ready.
378 * If so, update the want set (set all FDs that are ready). If not,
381 * @param ready set that is ready
382 * @param want set that we want to be ready
383 * @return GNUNET_YES if there was some overlap
386 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
387 struct GNUNET_NETWORK_FDSet *want)
389 if ( (NULL == want) || (NULL == ready) )
391 if (GNUNET_NETWORK_fdset_overlap (ready, want))
393 /* copy all over (yes, there maybe unrelated bits,
394 but this should not hurt well-written clients) */
395 GNUNET_NETWORK_fdset_copy (want, ready);
403 * Check if the given task is eligible to run now.
404 * Also set the reason why it is eligible.
406 * @param sched the scheduler
407 * @param task task to check if it is ready
408 * @param now the current time
409 * @param rs set of FDs ready for reading
410 * @param ws set of FDs ready for writing
411 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
414 is_ready (struct GNUNET_SCHEDULER_Handle *sched,
416 struct GNUNET_TIME_Absolute now,
417 const struct GNUNET_NETWORK_FDSet *rs,
418 const struct GNUNET_NETWORK_FDSet *ws)
420 enum GNUNET_SCHEDULER_Reason reason;
422 reason = task->reason;
423 if (now.value >= task->timeout.value)
424 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
425 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
426 ( ( (task->read_fd != -1) &&
427 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd)) ) ||
428 (set_overlaps (rs, task->read_set) ) ) )
429 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
430 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
431 ( ( (task->write_fd != -1) &&
432 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)) ) ||
433 (set_overlaps (ws, task->write_set) ) ) )
434 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
436 return GNUNET_NO; /* not ready */
437 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
439 if (GNUNET_YES == is_pending (sched, task->prereq_id))
441 task->reason = reason;
442 return GNUNET_NO; /* prereq waiting */
444 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
446 task->reason = reason;
452 * Put a task that is ready for execution into the ready queue.
454 * @param handle the scheduler
455 * @param task task ready for execution
458 queue_ready_task (struct GNUNET_SCHEDULER_Handle *handle,
461 enum GNUNET_SCHEDULER_Priority p = task->priority;
462 if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
463 p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
464 task->next = handle->ready[check_priority (p)];
465 handle->ready[check_priority (p)] = task;
466 handle->ready_count++;
471 * Check which tasks are ready and move them
472 * to the respective ready queue.
474 * @param handle the scheduler
475 * @param rs FDs ready for reading
476 * @param ws FDs ready for writing
479 check_ready (struct GNUNET_SCHEDULER_Handle *handle,
480 const struct GNUNET_NETWORK_FDSet *rs,
481 const struct GNUNET_NETWORK_FDSet *ws)
486 struct GNUNET_TIME_Absolute now;
488 now = GNUNET_TIME_absolute_get ();
490 pos = handle->pending_timeout;
494 if (now.value >= pos->timeout.value)
495 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
496 if (0 == pos->reason)
498 handle->pending_timeout = next;
499 if (handle->pending_timeout_last == pos)
500 handle->pending_timeout_last = NULL;
501 queue_ready_task (handle, pos);
504 pos = handle->pending;
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509 "Checking readiness of task: %llu / %p\n",
510 pos->id, pos->callback_cls);
513 if (GNUNET_YES == is_ready (handle, pos, now, rs, ws))
516 handle->pending = next;
519 queue_ready_task (handle, pos);
530 * Request the shutdown of a scheduler. Marks all currently
531 * pending tasks as ready because of shutdown. This will
532 * cause all tasks to run (as soon as possible, respecting
533 * priorities and prerequisite tasks). Note that tasks
534 * scheduled AFTER this call may still be delayed arbitrarily.
536 * @param sched the scheduler
539 GNUNET_SCHEDULER_shutdown (struct GNUNET_SCHEDULER_Handle *sched)
544 pos = sched->pending_timeout;
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
553 pos = sched->pending;
556 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
557 /* we don't move the task into the ready queue yet; check_ready
558 will do that later, possibly adding additional
562 for (i=0;i<GNUNET_SCHEDULER_PRIORITY_COUNT;i++)
564 pos = sched->ready[i];
567 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
568 /* we don't move the task into the ready queue yet; check_ready
569 will do that later, possibly adding additional
578 * Destroy a task (release associated resources)
580 * @param t task to destroy
583 destroy_task (struct Task *t)
585 if (NULL != t->read_set)
586 GNUNET_NETWORK_fdset_destroy (t->read_set);
587 if (NULL != t->write_set)
588 GNUNET_NETWORK_fdset_destroy (t->write_set);
590 GNUNET_free (t->backtrace_strings);
597 * Run at least one task in the highest-priority queue that is not
598 * empty. Keep running tasks until we are either no longer running
599 * "URGENT" tasks or until we have at least one "pending" task (which
600 * may become ready, hence we should select on it). Naturally, if
601 * there are no more ready tasks, we also return.
603 * @param sched the scheduler
604 * @param rs FDs ready for reading
605 * @param ws FDs ready for writing
608 run_ready (struct GNUNET_SCHEDULER_Handle *sched,
609 struct GNUNET_NETWORK_FDSet *rs,
610 struct GNUNET_NETWORK_FDSet *ws)
612 enum GNUNET_SCHEDULER_Priority p;
614 struct GNUNET_SCHEDULER_TaskContext tc;
616 sched->max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
619 if (sched->ready_count == 0)
621 GNUNET_assert (sched->ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
622 /* yes, p>0 is correct, 0 is "KEEP" which should
623 always be an empty queue (see assertion)! */
624 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
626 pos = sched->ready[p];
630 GNUNET_assert (pos != NULL); /* ready_count wrong? */
631 sched->ready[p] = pos->next;
632 sched->ready_count--;
633 if (sched->current_priority != pos->priority)
635 sched->current_priority = pos->priority;
636 (void) GNUNET_OS_set_process_priority (0, pos->priority);
638 sched->active_task = pos;
640 if (GNUNET_TIME_absolute_get_duration (pos->start_time).value >
641 DELAY_THRESHOLD.value)
643 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
644 "Task %u took %llums to be scheduled\n",
646 (unsigned long long) GNUNET_TIME_absolute_get_duration (pos->start_time).value);
650 tc.reason = pos->reason;
651 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
652 if ( (pos->read_fd != -1) &&
653 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
654 GNUNET_NETWORK_fdset_set_native (rs,
656 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
657 if ( (pos->write_fd != -1) &&
658 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) )
659 GNUNET_NETWORK_fdset_set_native (ws,
661 if ( ( (tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
662 (pos->write_fd != -1) &&
663 (! GNUNET_NETWORK_fdset_test_native (ws,
665 abort (); // added to ready in previous select loop!
667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
668 "Running task: %llu / %p\n", pos->id, pos->callback_cls);
670 pos->callback (pos->callback_cls, &tc);
673 for (i=0;i<pos->num_backtrace_strings;i++)
674 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
675 "Task %u trace %d: %s\n",
678 pos->backtrace_strings[i]);
680 sched->active_task = NULL;
684 while ( (sched->pending == NULL) || (p >= sched->max_priority_added) );
688 * Pipe used to communicate shutdown via signal.
690 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
693 * Signal handler called for SIGPIPE.
702 * Signal handler called for signals that should cause us to shutdown.
705 sighandler_shutdown ()
708 int old_errno = errno; /* backup errno */
710 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
711 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE), &c,
718 * Initialize and run scheduler. This function will return when all
719 * tasks have completed. On systems with signals, receiving a SIGTERM
720 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
721 * to be run after the active task is complete. As a result, SIGTERM
722 * causes all active tasks to be scheduled with reason
723 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
724 * afterwards will execute normally!). Note that any particular signal
725 * will only shut down one scheduler; applications should always only
726 * create a single scheduler.
728 * @param task task to run immediately
729 * @param task_cls closure of task
732 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
734 struct GNUNET_SCHEDULER_Handle sched;
735 struct GNUNET_NETWORK_FDSet *rs;
736 struct GNUNET_NETWORK_FDSet *ws;
737 struct GNUNET_TIME_Relative timeout;
739 struct GNUNET_SIGNAL_Context *shc_int;
740 struct GNUNET_SIGNAL_Context *shc_term;
742 struct GNUNET_SIGNAL_Context *shc_quit;
743 struct GNUNET_SIGNAL_Context *shc_hup;
744 struct GNUNET_SIGNAL_Context *shc_pipe;
746 unsigned long long last_tr;
747 unsigned int busy_wait_warning;
748 const struct GNUNET_DISK_FileHandle *pr;
751 rs = GNUNET_NETWORK_fdset_create ();
752 ws = GNUNET_NETWORK_fdset_create ();
753 GNUNET_assert (shutdown_pipe_handle == NULL);
754 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
755 GNUNET_assert (shutdown_pipe_handle != NULL);
756 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_READ);
757 GNUNET_assert (pr != NULL);
758 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
759 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
761 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
762 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
763 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
765 memset (&sched, 0, sizeof (sched));
766 sched.current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
767 GNUNET_SCHEDULER_add_continuation (&sched,
770 GNUNET_SCHEDULER_REASON_STARTUP);
772 busy_wait_warning = 0;
773 while ((sched.pending != NULL) ||
774 (sched.pending_timeout != NULL) ||
775 (sched.ready_count > 0))
777 GNUNET_NETWORK_fdset_zero (rs);
778 GNUNET_NETWORK_fdset_zero (ws);
779 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
780 update_sets (&sched, rs, ws, &timeout);
781 GNUNET_NETWORK_fdset_handle_set (rs, pr);
782 if (sched.ready_count > 0)
784 /* no blocking, more work already ready! */
785 timeout = GNUNET_TIME_UNIT_ZERO;
787 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
788 if (ret == GNUNET_SYSERR)
793 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
797 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid());
806 if ((ret == 0) && (timeout.value == 0) && (busy_wait_warning > 16))
808 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
809 _("Looks like we're busy waiting...\n"));
810 sleep (1); /* mitigate */
812 check_ready (&sched, rs, ws);
813 run_ready (&sched, rs, ws);
814 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
816 /* consume the signal */
817 GNUNET_DISK_file_read (pr, &c, sizeof (c));
818 /* mark all active tasks as ready due to shutdown */
819 GNUNET_SCHEDULER_shutdown (&sched);
821 if (last_tr == sched.tasks_run)
827 last_tr = sched.tasks_run;
828 busy_wait_warning = 0;
831 GNUNET_SIGNAL_handler_uninstall (shc_int);
832 GNUNET_SIGNAL_handler_uninstall (shc_term);
834 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
835 GNUNET_SIGNAL_handler_uninstall (shc_quit);
836 GNUNET_SIGNAL_handler_uninstall (shc_hup);
838 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
839 shutdown_pipe_handle = NULL;
840 GNUNET_NETWORK_fdset_destroy (rs);
841 GNUNET_NETWORK_fdset_destroy (ws);
846 * Obtain the reason code for why the current task was
847 * started. Will return the same value as
848 * the GNUNET_SCHEDULER_TaskContext's reason field.
850 * @param sched scheduler to query
851 * @return reason(s) why the current task is run
853 enum GNUNET_SCHEDULER_Reason
854 GNUNET_SCHEDULER_get_reason (struct GNUNET_SCHEDULER_Handle *sched)
856 return sched->active_task->reason;
861 * Get information about the current load of this scheduler. Use this
862 * function to determine if an elective task should be added or simply
863 * dropped (if the decision should be made based on the number of
864 * tasks ready to run).
866 * @param sched scheduler to query
867 * @param p priority level to look at
868 * @return number of tasks pending right now
871 GNUNET_SCHEDULER_get_load (struct GNUNET_SCHEDULER_Handle *sched,
872 enum GNUNET_SCHEDULER_Priority p)
877 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
878 return sched->ready_count;
879 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
880 p = sched->current_priority;
882 pos = sched->ready[check_priority (p)];
893 * Cancel the task with the specified identifier.
894 * The task must not yet have run.
896 * @param sched scheduler to use
897 * @param task id of the task to cancel
898 * @return original closure of the task
901 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Handle *sched,
902 GNUNET_SCHEDULER_TaskIdentifier task)
906 enum GNUNET_SCHEDULER_Priority p;
924 t = sched->pending_timeout;
932 if (sched->pending_timeout_last == t)
933 sched->pending_timeout_last = NULL;
939 GNUNET_assert (p < GNUNET_SCHEDULER_PRIORITY_COUNT);
946 sched->ready_count--;
959 sched->pending = t->next;
963 sched->pending_timeout = t->next;
968 sched->ready[p] = t->next;
973 prev->next = t->next;
975 ret = t->callback_cls;
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 "Canceling task: %llu / %p\n", task, t->callback_cls);
986 * Continue the current execution with the given function. This is
987 * similar to the other "add" functions except that there is no delay
988 * and the reason code can be specified.
990 * @param sched scheduler to use
991 * @param task main function of the task
992 * @param task_cls closure for 'main'
993 * @param reason reason for task invocation
996 GNUNET_SCHEDULER_add_continuation (struct GNUNET_SCHEDULER_Handle *sched,
997 GNUNET_SCHEDULER_Task task,
999 enum GNUNET_SCHEDULER_Reason reason)
1003 void *backtrace_array[50];
1005 t = GNUNET_malloc (sizeof (struct Task));
1007 t->num_backtrace_strings = backtrace(backtrace_array, 50);
1008 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1013 t->callback_cls = task_cls;
1014 t->id = ++sched->last_id;
1016 t->start_time = GNUNET_TIME_absolute_get ();
1019 t->priority = sched->current_priority;
1021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1022 "Adding continuation task: %llu / %p\n",
1023 t->id, t->callback_cls);
1025 queue_ready_task (sched, t);
1031 * Schedule a new task to be run after the specified prerequisite task
1032 * has completed. It will be run with the priority of the calling
1035 * @param sched scheduler to use
1036 * @param prerequisite_task run this task after the task with the given
1037 * task identifier completes (and any of our other
1038 * conditions, such as delay, read or write-readiness
1039 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1040 * on completion of other tasks (this will cause the task to run as
1041 * soon as possible).
1042 * @param task main function of the task
1043 * @param task_cls closure of task
1044 * @return unique task identifier for the job
1045 * only valid until "task" is started!
1047 GNUNET_SCHEDULER_TaskIdentifier
1048 GNUNET_SCHEDULER_add_after (struct GNUNET_SCHEDULER_Handle *sched,
1049 GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1050 GNUNET_SCHEDULER_Task task, void *task_cls)
1052 return GNUNET_SCHEDULER_add_select (sched,
1053 GNUNET_SCHEDULER_PRIORITY_KEEP,
1055 GNUNET_TIME_UNIT_ZERO,
1056 NULL, NULL, task, task_cls);
1061 * Schedule a new task to be run with a specified priority.
1063 * @param sched scheduler to use
1064 * @param prio how important is the new task?
1065 * @param task main function of the task
1066 * @param task_cls closure of task
1067 * @return unique task identifier for the job
1068 * only valid until "task" is started!
1070 GNUNET_SCHEDULER_TaskIdentifier
1071 GNUNET_SCHEDULER_add_with_priority (struct GNUNET_SCHEDULER_Handle * sched,
1072 enum GNUNET_SCHEDULER_Priority prio,
1073 GNUNET_SCHEDULER_Task task,
1076 return GNUNET_SCHEDULER_add_select (sched,
1078 GNUNET_SCHEDULER_NO_TASK,
1079 GNUNET_TIME_UNIT_ZERO,
1080 NULL, NULL, task, task_cls);
1086 * Schedule a new task to be run with a specified delay. The task
1087 * will be scheduled for execution once the delay has expired. It
1088 * will be run with the priority of the calling task.
1090 * @param sched scheduler to use
1091 * @param delay when should this operation time out? Use
1092 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1093 * @param task main function of the task
1094 * @param task_cls closure of task
1095 * @return unique task identifier for the job
1096 * only valid until "task" is started!
1098 GNUNET_SCHEDULER_TaskIdentifier
1099 GNUNET_SCHEDULER_add_delayed (struct GNUNET_SCHEDULER_Handle * sched,
1100 struct GNUNET_TIME_Relative delay,
1101 GNUNET_SCHEDULER_Task task, void *task_cls)
1104 /* new, optimized version */
1109 void *backtrace_array[MAX_TRACE_DEPTH];
1112 GNUNET_assert (NULL != task);
1113 t = GNUNET_malloc (sizeof (struct Task));
1115 t->callback_cls = task_cls;
1117 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1118 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1122 t->id = ++sched->last_id;
1124 t->start_time = GNUNET_TIME_absolute_get ();
1126 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1127 t->priority = sched->current_priority;
1128 /* try tail first (optimization in case we are
1129 appending to a long list of tasks with timeouts) */
1130 prev = sched->pending_timeout_last;
1133 if (prev->timeout.value > t->timeout.value)
1136 pos = prev->next; /* heuristic success! */
1140 /* heuristic failed, do traversal of timeout list */
1141 pos = sched->pending_timeout;
1143 while ( (pos != NULL) &&
1144 ( (pos->timeout.value <= t->timeout.value) ||
1145 (pos->reason != 0) ) )
1151 sched->pending_timeout = t;
1155 /* hyper-optimization... */
1156 sched->pending_timeout_last = t;
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1160 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1165 for (i=0;i<t->num_backtrace_strings;i++)
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1167 "Task %u trace %d: %s\n",
1170 t->backtrace_strings[i]);
1175 /* unoptimized version */
1176 return GNUNET_SCHEDULER_add_select (sched,
1177 GNUNET_SCHEDULER_PRIORITY_KEEP,
1178 GNUNET_SCHEDULER_NO_TASK, delay,
1179 NULL, NULL, task, task_cls);
1186 * Schedule a new task to be run as soon as possible. The task
1187 * will be run with the priority of the calling task.
1189 * @param sched scheduler to use
1190 * @param task main function of the task
1191 * @param task_cls closure of task
1192 * @return unique task identifier for the job
1193 * only valid until "task" is started!
1195 GNUNET_SCHEDULER_TaskIdentifier
1196 GNUNET_SCHEDULER_add_now (struct GNUNET_SCHEDULER_Handle *sched,
1197 GNUNET_SCHEDULER_Task task,
1200 return GNUNET_SCHEDULER_add_select (sched,
1201 GNUNET_SCHEDULER_PRIORITY_KEEP,
1202 GNUNET_SCHEDULER_NO_TASK,
1203 GNUNET_TIME_UNIT_ZERO,
1204 NULL, NULL, task, task_cls);
1211 * Schedule a new task to be run with a specified delay or when any of
1212 * the specified file descriptor sets is ready. The delay can be used
1213 * as a timeout on the socket(s) being ready. The task will be
1214 * scheduled for execution once either the delay has expired or any of
1215 * the socket operations is ready. This is the most general
1216 * function of the "add" family. Note that the "prerequisite_task"
1217 * must be satisfied in addition to any of the other conditions. In
1218 * other words, the task will be started when
1220 * (prerequisite-run)
1224 * || shutdown-active )
1227 * @param sched scheduler to use
1228 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1229 * which means that the task will only be run after we receive SIGTERM
1230 * @param rfd file descriptor we want to read (can be -1)
1231 * @param wfd file descriptors we want to write (can be -1)
1232 * @param task main function of the task
1233 * @param task_cls closure of task
1234 * @return unique task identifier for the job
1235 * only valid until "task" is started!
1237 GNUNET_SCHEDULER_TaskIdentifier
1238 add_without_sets (struct GNUNET_SCHEDULER_Handle * sched,
1239 struct GNUNET_TIME_Relative delay,
1242 GNUNET_SCHEDULER_Task task, void *task_cls)
1246 void *backtrace_array[MAX_TRACE_DEPTH];
1249 GNUNET_assert (NULL != task);
1250 t = GNUNET_malloc (sizeof (struct Task));
1252 t->callback_cls = task_cls;
1254 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1255 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1259 t->id = ++sched->last_id;
1261 t->start_time = GNUNET_TIME_absolute_get ();
1263 t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
1264 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1265 t->priority = check_priority (sched->current_priority);
1266 t->next = sched->pending;
1268 sched->max_priority_added = GNUNET_MAX (sched->max_priority_added,
1271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1272 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1277 for (i=0;i<t->num_backtrace_strings;i++)
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1279 "Task %u trace %d: %s\n",
1282 t->backtrace_strings[i]);
1290 * Schedule a new task to be run with a specified delay or when the
1291 * specified file descriptor is ready for reading. The delay can be
1292 * used as a timeout on the socket being ready. The task will be
1293 * scheduled for execution once either the delay has expired or the
1294 * socket operation is ready. It will be run with the priority of
1297 * @param sched scheduler to use
1298 * @param delay when should this operation time out? Use
1299 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1300 * @param rfd read file-descriptor
1301 * @param task main function of the task
1302 * @param task_cls closure of task
1303 * @return unique task identifier for the job
1304 * only valid until "task" is started!
1306 GNUNET_SCHEDULER_TaskIdentifier
1307 GNUNET_SCHEDULER_add_read_net (struct GNUNET_SCHEDULER_Handle * sched,
1308 struct GNUNET_TIME_Relative delay,
1309 struct GNUNET_NETWORK_Handle * rfd,
1310 GNUNET_SCHEDULER_Task task, void *task_cls)
1312 return add_without_sets (sched,
1314 GNUNET_NETWORK_get_fd (rfd),
1322 * Schedule a new task to be run with a specified delay or when the
1323 * specified file descriptor is ready for writing. The delay can be
1324 * used as a timeout on the socket being ready. The task will be
1325 * scheduled for execution once either the delay has expired or the
1326 * socket operation is ready. It will be run with the priority of
1329 * @param sched scheduler to use
1330 * @param delay when should this operation time out? Use
1331 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1332 * @param wfd write file-descriptor
1333 * @param task main function of the task
1334 * @param task_cls closure of task
1335 * @return unique task identifier for the job
1336 * only valid until "task" is started!
1338 GNUNET_SCHEDULER_TaskIdentifier
1339 GNUNET_SCHEDULER_add_write_net (struct GNUNET_SCHEDULER_Handle * sched,
1340 struct GNUNET_TIME_Relative delay,
1341 struct GNUNET_NETWORK_Handle * wfd,
1342 GNUNET_SCHEDULER_Task task, void *task_cls)
1344 return add_without_sets (sched,
1347 GNUNET_NETWORK_get_fd (wfd),
1354 * Schedule a new task to be run with a specified delay or when the
1355 * specified file descriptor is ready for reading. The delay can be
1356 * used as a timeout on the socket being ready. The task will be
1357 * scheduled for execution once either the delay has expired or the
1358 * socket operation is ready. It will be run with the priority of
1361 * @param sched scheduler to use
1362 * @param delay when should this operation time out? Use
1363 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1364 * @param rfd read 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_read_file (struct GNUNET_SCHEDULER_Handle * sched,
1372 struct GNUNET_TIME_Relative delay,
1373 const struct GNUNET_DISK_FileHandle * rfd,
1374 GNUNET_SCHEDULER_Task task, void *task_cls)
1377 struct GNUNET_NETWORK_FDSet *rs;
1378 GNUNET_SCHEDULER_TaskIdentifier ret;
1380 GNUNET_assert (rfd != NULL);
1381 rs = GNUNET_NETWORK_fdset_create ();
1382 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1383 ret = GNUNET_SCHEDULER_add_select (sched,
1384 GNUNET_SCHEDULER_PRIORITY_KEEP,
1385 GNUNET_SCHEDULER_NO_TASK, delay,
1386 rs, NULL, task, task_cls);
1387 GNUNET_NETWORK_fdset_destroy (rs);
1392 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1393 return add_without_sets (sched,
1405 * Schedule a new task to be run with a specified delay or when the
1406 * specified file descriptor is ready for writing. The delay can be
1407 * used as a timeout on the socket being ready. The task will be
1408 * scheduled for execution once either the delay has expired or the
1409 * socket operation is ready. It will be run with the priority of
1412 * @param sched scheduler to use
1413 * @param delay when should this operation time out? Use
1414 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1415 * @param wfd write file-descriptor
1416 * @param task main function of the task
1417 * @param task_cls closure of task
1418 * @return unique task identifier for the job
1419 * only valid until "task" is started!
1421 GNUNET_SCHEDULER_TaskIdentifier
1422 GNUNET_SCHEDULER_add_write_file (struct GNUNET_SCHEDULER_Handle * sched,
1423 struct GNUNET_TIME_Relative delay,
1424 const struct GNUNET_DISK_FileHandle * wfd,
1425 GNUNET_SCHEDULER_Task task, void *task_cls)
1428 struct GNUNET_NETWORK_FDSet *ws;
1429 GNUNET_SCHEDULER_TaskIdentifier ret;
1431 GNUNET_assert (wfd != NULL);
1432 ws = GNUNET_NETWORK_fdset_create ();
1433 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1434 ret = GNUNET_SCHEDULER_add_select (sched,
1435 GNUNET_SCHEDULER_PRIORITY_KEEP,
1436 GNUNET_SCHEDULER_NO_TASK,
1437 delay, NULL, ws, task, task_cls);
1438 GNUNET_NETWORK_fdset_destroy (ws);
1443 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1444 return add_without_sets (sched,
1457 * Schedule a new task to be run with a specified delay or when any of
1458 * the specified file descriptor sets is ready. The delay can be used
1459 * as a timeout on the socket(s) being ready. The task will be
1460 * scheduled for execution once either the delay has expired or any of
1461 * the socket operations is ready. This is the most general
1462 * function of the "add" family. Note that the "prerequisite_task"
1463 * must be satisfied in addition to any of the other conditions. In
1464 * other words, the task will be started when
1466 * (prerequisite-run)
1470 * || (shutdown-active && run-on-shutdown) )
1473 * @param sched scheduler to use
1474 * @param prio how important is this task?
1475 * @param prerequisite_task run this task after the task with the given
1476 * task identifier completes (and any of our other
1477 * conditions, such as delay, read or write-readiness
1478 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1479 * on completion of other tasks.
1480 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1481 * which means that the task will only be run after we receive SIGTERM
1482 * @param rs set of file descriptors we want to read (can be NULL)
1483 * @param ws set of file descriptors we want to write (can be NULL)
1484 * @param task main function of the task
1485 * @param task_cls closure of task
1486 * @return unique task identifier for the job
1487 * only valid until "task" is started!
1489 GNUNET_SCHEDULER_TaskIdentifier
1490 GNUNET_SCHEDULER_add_select (struct GNUNET_SCHEDULER_Handle * sched,
1491 enum GNUNET_SCHEDULER_Priority prio,
1492 GNUNET_SCHEDULER_TaskIdentifier
1494 struct GNUNET_TIME_Relative delay,
1495 const struct GNUNET_NETWORK_FDSet * rs,
1496 const struct GNUNET_NETWORK_FDSet * ws,
1497 GNUNET_SCHEDULER_Task task, void *task_cls)
1501 void *backtrace_array[MAX_TRACE_DEPTH];
1504 GNUNET_assert (NULL != task);
1505 t = GNUNET_malloc (sizeof (struct Task));
1507 t->callback_cls = task_cls;
1509 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1510 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1516 t->read_set = GNUNET_NETWORK_fdset_create ();
1517 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1521 t->write_set = GNUNET_NETWORK_fdset_create ();
1522 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1524 t->id = ++sched->last_id;
1526 t->start_time = GNUNET_TIME_absolute_get ();
1528 t->prereq_id = prerequisite_task;
1529 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1531 check_priority ((prio ==
1532 GNUNET_SCHEDULER_PRIORITY_KEEP) ? sched->current_priority
1534 t->next = sched->pending;
1536 sched->max_priority_added = GNUNET_MAX (sched->max_priority_added,
1539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1540 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1545 for (i=0;i<t->num_backtrace_strings;i++)
1546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1547 "Task %u trace %d: %s\n",
1550 t->backtrace_strings[i]);
1555 /* end of scheduler.c */