2 This file is part of GNUnet
3 (C) 2009, 2011 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.
154 * Should the existence of this task in the queue be counted as
155 * reason to not shutdown the scheduler?
161 * Array of strings which make up a backtrace from the point when this
162 * task was scheduled (essentially, who scheduled the task?)
164 char **backtrace_strings;
167 * Size of the backtrace_strings array
169 int num_backtrace_strings;
177 * List of tasks waiting for an event.
179 static struct Task *pending;
182 * List of tasks waiting ONLY for a timeout event.
183 * Sorted by timeout (earliest first). Used so that
184 * we do not traverse the list of these tasks when
185 * building select sets (we just look at the head
186 * to determine the respective timeout ONCE).
188 static struct Task *pending_timeout;
191 * Last inserted task waiting ONLY for a timeout event.
192 * Used to (heuristically) speed up insertion.
194 static struct Task *pending_timeout_last;
197 * ID of the task that is running right now.
199 static struct Task *active_task;
202 * List of tasks ready to run right now,
203 * grouped by importance.
205 static struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
208 * Identity of the last task queued. Incremented for each task to
209 * generate a unique task ID (it is virtually impossible to start
210 * more than 2^64 tasks during the lifetime of a process).
212 static GNUNET_SCHEDULER_TaskIdentifier last_id;
215 * Highest number so that all tasks with smaller identifiers
216 * have already completed. Also the lowest number of a task
217 * still waiting to be executed.
219 static GNUNET_SCHEDULER_TaskIdentifier lowest_pending_id;
222 * Number of tasks on the ready list.
224 static unsigned int ready_count;
227 * How many tasks have we run so far?
229 static unsigned long long tasks_run;
232 * Priority of the task running right now. Only
233 * valid while a task is running.
235 static enum GNUNET_SCHEDULER_Priority current_priority;
238 * Priority of the highest task added in the current select
241 static enum GNUNET_SCHEDULER_Priority max_priority_added;
244 * Value of the 'lifeness' flag for the current task.
246 static int current_lifeness;
249 * Function to use as a select() in the scheduler.
250 * Defaults to GNUNET_NETWORK_socket_select ()
252 GNUNET_SCHEDULER_select scheduler_select = GNUNET_NETWORK_socket_select;
255 * Sets the select function to use in the scheduler (scheduler_select).
257 * @param new_select new select function to use
258 * @return previously used select function
260 GNUNET_SCHEDULER_select
261 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select)
263 GNUNET_SCHEDULER_select old_select = scheduler_select;
264 scheduler_select = new_select;
265 if (scheduler_select == NULL)
266 scheduler_select = GNUNET_NETWORK_socket_select;
271 * Gets the select function currently used in the scheduler.
273 * @return currently used select function
275 GNUNET_SCHEDULER_select
276 GNUNET_SCHEDULER_get_select ()
278 return scheduler_select;
282 * Check that the given priority is legal (and return it).
284 * @param p priority value to check
285 * @return p on success, 0 on error
287 static enum GNUNET_SCHEDULER_Priority
288 check_priority (enum GNUNET_SCHEDULER_Priority p)
290 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
293 return 0; /* make compiler happy */
298 * Is a task with this identifier still pending? Also updates
299 * "lowest_pending_id" as a side-effect (for faster checks in the
300 * future), but only if the return value is "GNUNET_NO" (and
301 * the "lowest_pending_id" check failed).
303 * @param id which task are we checking for
304 * @return GNUNET_YES if so, GNUNET_NO if not
307 is_pending (GNUNET_SCHEDULER_TaskIdentifier id)
310 enum GNUNET_SCHEDULER_Priority p;
311 GNUNET_SCHEDULER_TaskIdentifier min;
313 if (id < lowest_pending_id)
315 min = -1; /* maximum value */
325 pos = pending_timeout;
334 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
346 lowest_pending_id = min;
352 * Update all sets and timeout for select.
354 * @param rs read-set, set to all FDs we would like to read (updated)
355 * @param ws write-set, set to all FDs we would like to write (updated)
356 * @param timeout next timeout (updated)
359 update_sets (struct GNUNET_NETWORK_FDSet *rs,
360 struct GNUNET_NETWORK_FDSet *ws,
361 struct GNUNET_TIME_Relative *timeout)
364 struct GNUNET_TIME_Absolute now;
365 struct GNUNET_TIME_Relative to;
367 now = GNUNET_TIME_absolute_get ();
368 pos = pending_timeout;
371 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
372 if (timeout->rel_value > to.rel_value)
374 if (pos->reason != 0)
375 *timeout = GNUNET_TIME_UNIT_ZERO;
380 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
381 (GNUNET_YES == is_pending (pos->prereq_id)))
386 if (pos->timeout.abs_value != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value)
388 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
389 if (timeout->rel_value > to.rel_value)
392 if (pos->read_fd != -1)
393 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
394 if (pos->write_fd != -1)
395 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
396 if (pos->read_set != NULL)
397 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
398 if (pos->write_set != NULL)
399 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
400 if (pos->reason != 0)
401 *timeout = GNUNET_TIME_UNIT_ZERO;
408 * Check if the ready set overlaps with the set we want to have ready.
409 * If so, update the want set (set all FDs that are ready). If not,
412 * @param ready set that is ready
413 * @param want set that we want to be ready
414 * @return GNUNET_YES if there was some overlap
417 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
418 struct GNUNET_NETWORK_FDSet *want)
420 if ( (NULL == want) || (NULL == ready) )
422 if (GNUNET_NETWORK_fdset_overlap (ready, want))
424 /* copy all over (yes, there maybe unrelated bits,
425 but this should not hurt well-written clients) */
426 GNUNET_NETWORK_fdset_copy (want, ready);
434 * Check if the given task is eligible to run now.
435 * Also set the reason why it is eligible.
437 * @param task task to check if it is ready
438 * @param now the current time
439 * @param rs set of FDs ready for reading
440 * @param ws set of FDs ready for writing
441 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
444 is_ready (struct Task *task,
445 struct GNUNET_TIME_Absolute now,
446 const struct GNUNET_NETWORK_FDSet *rs,
447 const struct GNUNET_NETWORK_FDSet *ws)
449 enum GNUNET_SCHEDULER_Reason reason;
451 reason = task->reason;
452 if (now.abs_value >= task->timeout.abs_value)
453 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
454 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
455 ( ( (task->read_fd != -1) &&
456 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd)) ) ||
457 (set_overlaps (rs, task->read_set) ) ) )
458 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
459 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
460 ( ( (task->write_fd != -1) &&
461 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)) ) ||
462 (set_overlaps (ws, task->write_set) ) ) )
463 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
465 return GNUNET_NO; /* not ready */
466 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
468 if (GNUNET_YES == is_pending (task->prereq_id))
470 task->reason = reason;
471 return GNUNET_NO; /* prereq waiting */
473 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
475 task->reason = reason;
481 * Put a task that is ready for execution into the ready queue.
483 * @param task task ready for execution
486 queue_ready_task (struct Task *task)
488 enum GNUNET_SCHEDULER_Priority p = task->priority;
489 if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
490 p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
491 task->next = ready[check_priority (p)];
492 ready[check_priority (p)] = task;
498 * Check which tasks are ready and move them
499 * to the respective ready queue.
501 * @param rs FDs ready for reading
502 * @param ws FDs ready for writing
505 check_ready (const struct GNUNET_NETWORK_FDSet *rs,
506 const struct GNUNET_NETWORK_FDSet *ws)
511 struct GNUNET_TIME_Absolute now;
513 now = GNUNET_TIME_absolute_get ();
515 pos = pending_timeout;
519 if (now.abs_value >= pos->timeout.abs_value)
520 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
521 if (0 == pos->reason)
523 pending_timeout = next;
524 if (pending_timeout_last == pos)
525 pending_timeout_last = NULL;
526 queue_ready_task (pos);
533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
534 "Checking readiness of task: %llu / %p\n",
535 pos->id, pos->callback_cls);
538 if (GNUNET_YES == is_ready (pos, now, rs, ws))
544 queue_ready_task (pos);
555 * Request the shutdown of a scheduler. Marks all currently
556 * pending tasks as ready because of shutdown. This will
557 * cause all tasks to run (as soon as possible, respecting
558 * priorities and prerequisite tasks). Note that tasks
559 * scheduled AFTER this call may still be delayed arbitrarily.
562 GNUNET_SCHEDULER_shutdown ()
567 pos = pending_timeout;
570 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
571 /* we don't move the task into the ready queue yet; check_ready
572 will do that later, possibly adding additional
579 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
580 /* we don't move the task into the ready queue yet; check_ready
581 will do that later, possibly adding additional
585 for (i=0;i<GNUNET_SCHEDULER_PRIORITY_COUNT;i++)
590 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
591 /* we don't move the task into the ready queue yet; check_ready
592 will do that later, possibly adding additional
601 * Destroy a task (release associated resources)
603 * @param t task to destroy
606 destroy_task (struct Task *t)
608 if (NULL != t->read_set)
609 GNUNET_NETWORK_fdset_destroy (t->read_set);
610 if (NULL != t->write_set)
611 GNUNET_NETWORK_fdset_destroy (t->write_set);
613 GNUNET_free (t->backtrace_strings);
620 * Run at least one task in the highest-priority queue that is not
621 * empty. Keep running tasks until we are either no longer running
622 * "URGENT" tasks or until we have at least one "pending" task (which
623 * may become ready, hence we should select on it). Naturally, if
624 * there are no more ready tasks, we also return.
626 * @param rs FDs ready for reading
627 * @param ws FDs ready for writing
630 run_ready (struct GNUNET_NETWORK_FDSet *rs,
631 struct GNUNET_NETWORK_FDSet *ws)
633 enum GNUNET_SCHEDULER_Priority p;
635 struct GNUNET_SCHEDULER_TaskContext tc;
637 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
640 if (ready_count == 0)
642 GNUNET_assert (ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
643 /* yes, p>0 is correct, 0 is "KEEP" which should
644 always be an empty queue (see assertion)! */
645 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
651 GNUNET_assert (pos != NULL); /* ready_count wrong? */
652 ready[p] = pos->next;
654 if (current_priority != pos->priority)
656 current_priority = pos->priority;
657 (void) GNUNET_OS_set_process_priority (GNUNET_OS_process_current (),
660 current_lifeness = pos->lifeness;
663 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value >
664 DELAY_THRESHOLD.rel_value)
666 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
667 "Task %llu took %llums to be scheduled\n",
669 (unsigned long long) GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value);
672 tc.reason = pos->reason;
673 tc.read_ready = (pos->read_set == NULL) ? rs : pos->read_set;
674 if ( (pos->read_fd != -1) &&
675 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
676 GNUNET_NETWORK_fdset_set_native (rs,
678 tc.write_ready = (pos->write_set == NULL) ? ws : pos->write_set;
679 if ( (pos->write_fd != -1) &&
680 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) )
681 GNUNET_NETWORK_fdset_set_native (ws,
683 if ( ( (tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY) != 0) &&
684 (pos->write_fd != -1) &&
685 (! GNUNET_NETWORK_fdset_test_native (ws,
687 abort (); // added to ready in previous select loop!
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "Running task: %llu / %p\n", pos->id, pos->callback_cls);
692 pos->callback (pos->callback_cls, &tc);
695 for (i=0;i<pos->num_backtrace_strings;i++)
696 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
697 "Task %llu trace %d: %s\n",
700 pos->backtrace_strings[i]);
706 while ( (pending == NULL) || (p >= max_priority_added) );
710 * Pipe used to communicate shutdown via signal.
712 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
715 * Signal handler called for SIGPIPE.
725 * Signal handler called for signals that should cause us to shutdown.
728 sighandler_shutdown ()
731 int old_errno = errno; /* backup errno */
733 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
734 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE), &c,
741 * Check if the system is still life. Trigger shutdown if we
742 * have tasks, but none of them give us lifeness.
744 * @return GNUNET_OK to continue the main loop,
753 for (t = pending; NULL != t; t = t->next)
754 if (t->lifeness == GNUNET_YES)
756 for (t = pending_timeout; NULL != t; t = t->next)
757 if (t->lifeness == GNUNET_YES)
759 if ( (NULL != pending) || (NULL != pending_timeout) )
761 GNUNET_SCHEDULER_shutdown ();
769 * Initialize and run scheduler. This function will return when all
770 * tasks have completed. On systems with signals, receiving a SIGTERM
771 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
772 * to be run after the active task is complete. As a result, SIGTERM
773 * causes all active tasks to be scheduled with reason
774 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
775 * afterwards will execute normally!). Note that any particular signal
776 * will only shut down one scheduler; applications should always only
777 * create a single scheduler.
779 * @param task task to run immediately
780 * @param task_cls closure of task
783 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
785 struct GNUNET_NETWORK_FDSet *rs;
786 struct GNUNET_NETWORK_FDSet *ws;
787 struct GNUNET_TIME_Relative timeout;
789 struct GNUNET_SIGNAL_Context *shc_int;
790 struct GNUNET_SIGNAL_Context *shc_term;
792 struct GNUNET_SIGNAL_Context *shc_quit;
793 struct GNUNET_SIGNAL_Context *shc_hup;
794 struct GNUNET_SIGNAL_Context *shc_pipe;
796 unsigned long long last_tr;
797 unsigned int busy_wait_warning;
798 const struct GNUNET_DISK_FileHandle *pr;
801 GNUNET_assert (active_task == NULL);
802 rs = GNUNET_NETWORK_fdset_create ();
803 ws = GNUNET_NETWORK_fdset_create ();
804 GNUNET_assert (shutdown_pipe_handle == NULL);
805 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO);
806 GNUNET_assert (shutdown_pipe_handle != NULL);
807 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_READ);
808 GNUNET_assert (pr != NULL);
809 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
810 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
812 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE, &sighandler_pipe);
813 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
814 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
816 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
817 current_lifeness = GNUNET_YES;
818 GNUNET_SCHEDULER_add_continuation (task,
820 GNUNET_SCHEDULER_REASON_STARTUP);
821 #if ENABLE_WINDOWS_WORKAROUNDS
822 active_task = (void*) (long) -1; /* force passing of sanity check */
823 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
824 &GNUNET_OS_install_parent_control_handler,
829 busy_wait_warning = 0;
830 while (GNUNET_OK == check_lifeness ())
832 GNUNET_NETWORK_fdset_zero (rs);
833 GNUNET_NETWORK_fdset_zero (ws);
834 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
835 update_sets (rs, ws, &timeout);
836 GNUNET_NETWORK_fdset_handle_set (rs, pr);
839 /* no blocking, more work already ready! */
840 timeout = GNUNET_TIME_UNIT_ZERO;
842 ret = scheduler_select (rs, ws, NULL, timeout);
843 if (ret == GNUNET_SYSERR)
848 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
852 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid());
855 if (0 != system (lsof))
856 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "system");
862 if ((ret == 0) && (timeout.rel_value == 0) && (busy_wait_warning > 16))
864 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
865 _("Looks like we're busy waiting...\n"));
866 sleep (1); /* mitigate */
868 check_ready (rs, ws);
870 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
872 /* consume the signal */
873 GNUNET_DISK_file_read (pr, &c, sizeof (c));
874 /* mark all active tasks as ready due to shutdown */
875 GNUNET_SCHEDULER_shutdown ();
877 if (last_tr == tasks_run)
884 busy_wait_warning = 0;
887 GNUNET_SIGNAL_handler_uninstall (shc_int);
888 GNUNET_SIGNAL_handler_uninstall (shc_term);
890 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
891 GNUNET_SIGNAL_handler_uninstall (shc_quit);
892 GNUNET_SIGNAL_handler_uninstall (shc_hup);
894 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
895 shutdown_pipe_handle = NULL;
896 GNUNET_NETWORK_fdset_destroy (rs);
897 GNUNET_NETWORK_fdset_destroy (ws);
902 * Obtain the reason code for why the current task was
903 * started. Will return the same value as
904 * the GNUNET_SCHEDULER_TaskContext's reason field.
906 * @return reason(s) why the current task is run
908 enum GNUNET_SCHEDULER_Reason
909 GNUNET_SCHEDULER_get_reason ()
911 GNUNET_assert (active_task != NULL);
912 return active_task->reason;
917 * Get information about the current load of this scheduler. Use this
918 * function to determine if an elective task should be added or simply
919 * dropped (if the decision should be made based on the number of
920 * tasks ready to run).
922 * @param p priority level to look at
923 * @return number of tasks pending right now
926 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
931 GNUNET_assert (active_task != NULL);
932 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
934 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
935 p = current_priority;
937 pos = ready[check_priority (p)];
948 * Cancel the task with the specified identifier.
949 * The task must not yet have run.
951 * @param task id of the task to cancel
952 * @return original closure of the task
955 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task)
959 enum GNUNET_SCHEDULER_Priority p;
963 GNUNET_assert (active_task != NULL);
986 if (pending_timeout_last == t)
987 pending_timeout_last = NULL;
993 if (p >= GNUNET_SCHEDULER_PRIORITY_COUNT)
995 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
996 _("Attempt to cancel dead task %llu!\n"),
997 (unsigned long long) task);
1023 pending_timeout = t->next;
1033 prev->next = t->next;
1035 ret = t->callback_cls;
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1038 "Canceling task: %llu / %p\n", task, t->callback_cls);
1046 * Continue the current execution with the given function. This is
1047 * similar to the other "add" functions except that there is no delay
1048 * and the reason code can be specified.
1050 * @param task main function of the task
1051 * @param task_cls closure for 'main'
1052 * @param reason reason for task invocation
1055 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task,
1057 enum GNUNET_SCHEDULER_Reason reason)
1061 void *backtrace_array[50];
1064 GNUNET_assert ( (active_task != NULL) ||
1065 (reason == GNUNET_SCHEDULER_REASON_STARTUP) );
1066 t = GNUNET_malloc (sizeof (struct Task));
1068 t->num_backtrace_strings = backtrace(backtrace_array, 50);
1069 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1074 t->callback_cls = task_cls;
1077 t->start_time = GNUNET_TIME_absolute_get ();
1080 t->priority = current_priority;
1081 t->lifeness = current_lifeness;
1083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1084 "Adding continuation task: %llu / %p\n",
1085 t->id, t->callback_cls);
1087 queue_ready_task (t);
1093 * Schedule a new task to be run after the specified prerequisite task
1094 * has completed. It will be run with the priority of the calling
1097 * @param prerequisite_task run this task after the task with the given
1098 * task identifier completes (and any of our other
1099 * conditions, such as delay, read or write-readiness
1100 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1101 * on completion of other tasks (this will cause the task to run as
1102 * soon as possible).
1103 * @param task main function of the task
1104 * @param task_cls closure of task
1105 * @return unique task identifier for the job
1106 * only valid until "task" is started!
1108 GNUNET_SCHEDULER_TaskIdentifier
1109 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
1110 GNUNET_SCHEDULER_Task task, void *task_cls)
1112 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1114 GNUNET_TIME_UNIT_ZERO,
1115 NULL, NULL, task, task_cls);
1120 * Schedule a new task to be run with a specified priority.
1122 * @param prio how important is the new task?
1123 * @param task main function of the task
1124 * @param task_cls closure of task
1125 * @return unique task identifier for the job
1126 * only valid until "task" is started!
1128 GNUNET_SCHEDULER_TaskIdentifier
1129 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1130 GNUNET_SCHEDULER_Task task,
1133 return GNUNET_SCHEDULER_add_select (prio,
1134 GNUNET_SCHEDULER_NO_TASK,
1135 GNUNET_TIME_UNIT_ZERO,
1136 NULL, NULL, task, task_cls);
1142 * Schedule a new task to be run with a specified delay. The task
1143 * will be scheduled for execution once the delay has expired. It
1144 * will be run with the priority of the calling task.
1146 * @param delay when should this operation time out? Use
1147 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1148 * @param task main function of the task
1149 * @param task_cls closure of task
1150 * @return unique task identifier for the job
1151 * only valid until "task" is started!
1153 GNUNET_SCHEDULER_TaskIdentifier
1154 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1155 GNUNET_SCHEDULER_Task task, void *task_cls)
1158 /* new, optimized version */
1163 void *backtrace_array[MAX_TRACE_DEPTH];
1166 GNUNET_assert (active_task != NULL);
1167 GNUNET_assert (NULL != task);
1168 t = GNUNET_malloc (sizeof (struct Task));
1170 t->callback_cls = task_cls;
1172 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1173 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1179 t->start_time = GNUNET_TIME_absolute_get ();
1181 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1182 t->priority = current_priority;
1183 t->lifeness = current_lifeness;
1184 /* try tail first (optimization in case we are
1185 appending to a long list of tasks with timeouts) */
1186 prev = pending_timeout_last;
1189 if (prev->timeout.abs_value > t->timeout.abs_value)
1192 pos = prev->next; /* heuristic success! */
1196 /* heuristic failed, do traversal of timeout list */
1197 pos = pending_timeout;
1199 while ( (pos != NULL) &&
1200 ( (pos->timeout.abs_value <= t->timeout.abs_value) ||
1201 (pos->reason != 0) ) )
1207 pending_timeout = t;
1211 /* hyper-optimization... */
1212 pending_timeout_last = t;
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1216 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1221 for (i=0;i<t->num_backtrace_strings;i++)
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "Task %llu trace %d: %s\n",
1226 t->backtrace_strings[i]);
1231 /* unoptimized version */
1232 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1233 GNUNET_SCHEDULER_NO_TASK, delay,
1234 NULL, NULL, task, task_cls);
1241 * Schedule a new task to be run as soon as possible. The task
1242 * will be run with the priority of the calling task.
1244 * @param task main function of the task
1245 * @param task_cls closure of task
1246 * @return unique task identifier for the job
1247 * only valid until "task" is started!
1249 GNUNET_SCHEDULER_TaskIdentifier
1250 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task,
1253 return GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1254 GNUNET_SCHEDULER_NO_TASK,
1255 GNUNET_TIME_UNIT_ZERO,
1256 NULL, NULL, task, task_cls);
1261 * Schedule a new task to be run as soon as possible with the
1262 * (transitive) ignore-shutdown flag either explicitly set or
1263 * explicitly enabled. This task (and all tasks created from it,
1264 * other than by another call to this function) will either count or
1265 * not count for the 'lifeness' of the process. This API is only
1266 * useful in a few special cases.
1268 * @param lifeness GNUNET_YES if the task counts for lifeness, GNUNET_NO if not.
1269 * @param task main function of the task
1270 * @param task_cls closure of task
1271 * @return unique task identifier for the job
1272 * only valid until "task" is started!
1274 GNUNET_SCHEDULER_TaskIdentifier
1275 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1276 GNUNET_SCHEDULER_Task task,
1279 GNUNET_SCHEDULER_TaskIdentifier ret;
1281 ret = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_KEEP,
1282 GNUNET_SCHEDULER_NO_TASK,
1283 GNUNET_TIME_UNIT_ZERO,
1284 NULL, NULL, task, task_cls);
1285 GNUNET_assert (pending->id == ret);
1286 pending->lifeness = lifeness;
1294 * Schedule a new task to be run with a specified delay or when any of
1295 * the specified file descriptor sets is ready. The delay can be used
1296 * as a timeout on the socket(s) being ready. The task will be
1297 * scheduled for execution once either the delay has expired or any of
1298 * the socket operations is ready. This is the most general
1299 * function of the "add" family. Note that the "prerequisite_task"
1300 * must be satisfied in addition to any of the other conditions. In
1301 * other words, the task will be started when
1303 * (prerequisite-run)
1307 * || shutdown-active )
1310 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1311 * which means that the task will only be run after we receive SIGTERM
1312 * @param rfd file descriptor we want to read (can be -1)
1313 * @param wfd file descriptors we want to write (can be -1)
1314 * @param task main function of the task
1315 * @param task_cls closure of task
1316 * @return unique task identifier for the job
1317 * only valid until "task" is started!
1320 GNUNET_SCHEDULER_TaskIdentifier
1321 add_without_sets (struct GNUNET_TIME_Relative delay,
1324 GNUNET_SCHEDULER_Task task, void *task_cls)
1328 void *backtrace_array[MAX_TRACE_DEPTH];
1331 GNUNET_assert (active_task != NULL);
1332 GNUNET_assert (NULL != task);
1333 t = GNUNET_malloc (sizeof (struct Task));
1335 t->callback_cls = task_cls;
1337 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1338 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1343 int flags = fcntl(rfd, F_GETFD);
1344 if (flags == -1 && errno == EBADF)
1346 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", rfd);
1350 for (i=0;i<t->num_backtrace_strings;i++)
1351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 t->backtrace_strings[i]);
1360 int flags = fcntl(wfd, F_GETFD);
1361 if (flags == -1 && errno == EBADF)
1363 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Got invalid file descriptor %d!\n", wfd);
1367 for (i=0;i<t->num_backtrace_strings;i++)
1368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1370 t->backtrace_strings[i]);
1377 GNUNET_assert(wfd >= -1);
1381 t->start_time = GNUNET_TIME_absolute_get ();
1383 t->prereq_id = GNUNET_SCHEDULER_NO_TASK;
1384 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1385 t->priority = check_priority (current_priority);
1386 t->lifeness = current_lifeness;
1389 max_priority_added = GNUNET_MAX (max_priority_added,
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1393 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1398 for (i=0;i<t->num_backtrace_strings;i++)
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1400 "Task %llu trace %d: %s\n",
1403 t->backtrace_strings[i]);
1412 * Schedule a new task to be run with a specified delay or when the
1413 * specified file descriptor is ready for reading. The delay can be
1414 * used as a timeout on the socket being ready. The task will be
1415 * scheduled for execution once either the delay has expired or the
1416 * socket operation is ready. It will be run with the priority of
1419 * @param delay when should this operation time out? Use
1420 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1421 * @param rfd read file-descriptor
1422 * @param task main function of the task
1423 * @param task_cls closure of task
1424 * @return unique task identifier for the job
1425 * only valid until "task" is started!
1427 GNUNET_SCHEDULER_TaskIdentifier
1428 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1429 struct GNUNET_NETWORK_Handle * rfd,
1430 GNUNET_SCHEDULER_Task task, void *task_cls)
1433 struct GNUNET_NETWORK_FDSet *rs;
1434 GNUNET_SCHEDULER_TaskIdentifier ret;
1436 GNUNET_assert (rfd != NULL);
1437 rs = GNUNET_NETWORK_fdset_create ();
1438 GNUNET_NETWORK_fdset_set (rs, rfd);
1439 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1440 GNUNET_SCHEDULER_NO_TASK, delay,
1441 rs, NULL, task, task_cls);
1442 GNUNET_NETWORK_fdset_destroy (rs);
1445 return add_without_sets (delay,
1446 GNUNET_NETWORK_get_fd (rfd),
1455 * Schedule a new task to be run with a specified delay or when the
1456 * specified file descriptor is ready for writing. The delay can be
1457 * used as a timeout on the socket being ready. The task will be
1458 * scheduled for execution once either the delay has expired or the
1459 * socket operation is ready. It will be run with the priority of
1462 * @param delay when should this operation time out? Use
1463 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1464 * @param wfd write file-descriptor
1465 * @param task main function of the task
1466 * @param task_cls closure of task
1467 * @return unique task identifier for the job
1468 * only valid until "task" is started!
1470 GNUNET_SCHEDULER_TaskIdentifier
1471 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1472 struct GNUNET_NETWORK_Handle * wfd,
1473 GNUNET_SCHEDULER_Task task, void *task_cls)
1476 struct GNUNET_NETWORK_FDSet *ws;
1477 GNUNET_SCHEDULER_TaskIdentifier ret;
1479 GNUNET_assert (wfd != NULL);
1480 ws = GNUNET_NETWORK_fdset_create ();
1481 GNUNET_NETWORK_fdset_set (ws, wfd);
1482 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1483 GNUNET_SCHEDULER_NO_TASK, delay,
1484 NULL, ws, task, task_cls);
1485 GNUNET_NETWORK_fdset_destroy (ws);
1488 GNUNET_assert(GNUNET_NETWORK_get_fd(wfd) >= 0);
1489 return add_without_sets (delay,
1491 GNUNET_NETWORK_get_fd (wfd),
1499 * Schedule a new task to be run with a specified delay or when the
1500 * specified file descriptor is ready for reading. The delay can be
1501 * used as a timeout on the socket being ready. The task will be
1502 * scheduled for execution once either the delay has expired or the
1503 * socket operation is ready. It will be run with the priority of
1506 * @param delay when should this operation time out? Use
1507 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1508 * @param rfd read file-descriptor
1509 * @param task main function of the task
1510 * @param task_cls closure of task
1511 * @return unique task identifier for the job
1512 * only valid until "task" is started!
1514 GNUNET_SCHEDULER_TaskIdentifier
1515 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1516 const struct GNUNET_DISK_FileHandle * rfd,
1517 GNUNET_SCHEDULER_Task task, void *task_cls)
1520 struct GNUNET_NETWORK_FDSet *rs;
1521 GNUNET_SCHEDULER_TaskIdentifier ret;
1523 GNUNET_assert (rfd != NULL);
1524 rs = GNUNET_NETWORK_fdset_create ();
1525 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1526 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1527 GNUNET_SCHEDULER_NO_TASK, delay,
1528 rs, NULL, task, task_cls);
1529 GNUNET_NETWORK_fdset_destroy (rs);
1534 GNUNET_DISK_internal_file_handle_ (rfd, &fd, sizeof (int));
1535 return add_without_sets (delay,
1546 * Schedule a new task to be run with a specified delay or when the
1547 * specified file descriptor is ready for writing. The delay can be
1548 * used as a timeout on the socket being ready. The task will be
1549 * scheduled for execution once either the delay has expired or the
1550 * socket operation is ready. It will be run with the priority of
1553 * @param delay when should this operation time out? Use
1554 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1555 * @param wfd write file-descriptor
1556 * @param task main function of the task
1557 * @param task_cls closure of task
1558 * @return unique task identifier for the job
1559 * only valid until "task" is started!
1561 GNUNET_SCHEDULER_TaskIdentifier
1562 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1563 const struct GNUNET_DISK_FileHandle * wfd,
1564 GNUNET_SCHEDULER_Task task, void *task_cls)
1567 struct GNUNET_NETWORK_FDSet *ws;
1568 GNUNET_SCHEDULER_TaskIdentifier ret;
1570 GNUNET_assert (wfd != NULL);
1571 ws = GNUNET_NETWORK_fdset_create ();
1572 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1573 ret = GNUNET_SCHEDULER_add_select (check_priority (current_priority),
1574 GNUNET_SCHEDULER_NO_TASK,
1575 delay, NULL, ws, task, task_cls);
1576 GNUNET_NETWORK_fdset_destroy (ws);
1581 GNUNET_DISK_internal_file_handle_ (wfd, &fd, sizeof (int));
1582 GNUNET_assert(fd >= 0);
1583 return add_without_sets (delay,
1595 * Schedule a new task to be run with a specified delay or when any of
1596 * the specified file descriptor sets is ready. The delay can be used
1597 * as a timeout on the socket(s) being ready. The task will be
1598 * scheduled for execution once either the delay has expired or any of
1599 * the socket operations is ready. This is the most general
1600 * function of the "add" family. Note that the "prerequisite_task"
1601 * must be satisfied in addition to any of the other conditions. In
1602 * other words, the task will be started when
1604 * (prerequisite-run)
1608 * || (shutdown-active && run-on-shutdown) )
1611 * @param prio how important is this task?
1612 * @param prerequisite_task run this task after the task with the given
1613 * task identifier completes (and any of our other
1614 * conditions, such as delay, read or write-readiness
1615 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1616 * on completion of other tasks.
1617 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1618 * which means that the task will only be run after we receive SIGTERM
1619 * @param rs set of file descriptors we want to read (can be NULL)
1620 * @param ws set of file descriptors we want to write (can be NULL)
1621 * @param task main function of the task
1622 * @param task_cls closure of task
1623 * @return unique task identifier for the job
1624 * only valid until "task" is started!
1626 GNUNET_SCHEDULER_TaskIdentifier
1627 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1628 GNUNET_SCHEDULER_TaskIdentifier
1630 struct GNUNET_TIME_Relative delay,
1631 const struct GNUNET_NETWORK_FDSet * rs,
1632 const struct GNUNET_NETWORK_FDSet * ws,
1633 GNUNET_SCHEDULER_Task task, void *task_cls)
1637 void *backtrace_array[MAX_TRACE_DEPTH];
1640 GNUNET_assert (active_task != NULL);
1641 GNUNET_assert (NULL != task);
1642 t = GNUNET_malloc (sizeof (struct Task));
1644 t->callback_cls = task_cls;
1646 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1647 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1653 t->read_set = GNUNET_NETWORK_fdset_create ();
1654 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1658 t->write_set = GNUNET_NETWORK_fdset_create ();
1659 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1663 t->start_time = GNUNET_TIME_absolute_get ();
1665 t->prereq_id = prerequisite_task;
1666 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1668 check_priority ((prio ==
1669 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority
1671 t->lifeness = current_lifeness;
1674 max_priority_added = GNUNET_MAX (max_priority_added,
1677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1678 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1683 for (i=0;i<t->num_backtrace_strings;i++)
1684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1685 "Task %llu trace %d: %s\n",
1688 t->backtrace_strings[i]);
1693 /* end of scheduler.c */