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"
36 * Use lsof to generate file descriptor reports on select error?
37 * (turn of for stable releases).
39 #define USE_LSOF GNUNET_YES
42 * Obtain trace information for all scheduler calls that schedule tasks.
44 #define EXECINFO GNUNET_NO
47 * Depth of the traces collected via EXECINFO.
49 #define MAX_TRACE_DEPTH 50
52 #define DEBUG_TASKS GNUNET_NO
55 * Should we figure out which tasks are delayed for a while
56 * before they are run? (Consider using in combination with EXECINFO).
58 #define PROFILE_DELAYS GNUNET_NO
61 * Task that were in the queue for longer than this are reported if
62 * PROFILE_DELAYS is active.
64 #define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
67 * Linked list of pending tasks.
72 * This is a linked list.
77 * Function to run when ready.
79 GNUNET_SCHEDULER_Task callback;
82 * Closure for the callback.
87 * Set of file descriptors this task is waiting
88 * for for reading. Once ready, this is updated
89 * to reflect the set of file descriptors ready
92 struct GNUNET_NETWORK_FDSet *read_set;
95 * Set of file descriptors this task is waiting for for writing.
96 * Once ready, this is updated to reflect the set of file
97 * descriptors ready for operation.
99 struct GNUNET_NETWORK_FDSet *write_set;
102 * Unique task identifier.
104 GNUNET_SCHEDULER_TaskIdentifier id;
107 * Identifier of a prerequisite task.
109 GNUNET_SCHEDULER_TaskIdentifier prereq_id;
112 * Absolute timeout value for the task, or
113 * GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
115 struct GNUNET_TIME_Absolute timeout;
119 * When was the task scheduled?
121 struct GNUNET_TIME_Absolute start_time;
125 * Why is the task ready? Set after task is added to ready queue.
126 * Initially set to zero. All reasons that have already been
127 * satisfied (i.e. read or write ready) will be set over time.
129 enum GNUNET_SCHEDULER_Reason reason;
134 enum GNUNET_SCHEDULER_Priority priority;
138 * Array of strings which make up a backtrace from the point when this
139 * task was scheduled (essentially, who scheduled the task?)
141 char **backtrace_strings;
144 * Size of the backtrace_strings array
146 int num_backtrace_strings;
153 * Handle for the scheduling service.
155 struct GNUNET_SCHEDULER_Handle
159 * List of tasks waiting for an event.
161 struct Task *pending;
164 * ID of the task that is running right now.
166 struct Task *active_task;
169 * List of tasks ready to run right now,
170 * grouped by importance.
172 struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
175 * Identity of the last task queued. Incremented for each task to
176 * generate a unique task ID (it is virtually impossible to start
177 * more than 2^64 tasks during the lifetime of a process).
179 GNUNET_SCHEDULER_TaskIdentifier last_id;
182 * Highest number so that all tasks with smaller identifiers
183 * have already completed. Also the lowest number of a task
184 * still waiting to be executed.
186 GNUNET_SCHEDULER_TaskIdentifier lowest_pending_id;
189 * Number of tasks on the ready list.
191 unsigned int ready_count;
194 * How many tasks have we run so far?
196 unsigned long long tasks_run;
199 * Priority of the task running right now. Only
200 * valid while a task is running.
202 enum GNUNET_SCHEDULER_Priority current_priority;
205 * How 'nice' are we right now?
213 * Check that the given priority is legal (and return it).
215 * @param p priority value to check
216 * @return p on success, 0 on error
218 static enum GNUNET_SCHEDULER_Priority
219 check_priority (enum GNUNET_SCHEDULER_Priority p)
221 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
224 return 0; /* make compiler happy */
229 * Is a task with this identifier still pending? Also updates
230 * "lowest_pending_id" as a side-effect (for faster checks in the
231 * future), but only if the return value is "GNUNET_NO" (and
232 * the "lowest_pending_id" check failed).
234 * @param sched the scheduler
235 * @param id which task are we checking for
236 * @return GNUNET_YES if so, GNUNET_NO if not
239 is_pending (struct GNUNET_SCHEDULER_Handle *sched,
240 GNUNET_SCHEDULER_TaskIdentifier id)
243 enum GNUNET_SCHEDULER_Priority p;
244 GNUNET_SCHEDULER_TaskIdentifier min;
246 if (id < sched->lowest_pending_id)
248 min = -1; /* maximum value */
249 pos = sched->pending;
258 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
260 pos = sched->ready[p];
270 sched->lowest_pending_id = min;
276 * Update all sets and timeout for select.
278 * @param sched the scheduler
279 * @param rs read-set, set to all FDs we would like to read (updated)
280 * @param ws write-set, set to all FDs we would like to write (updated)
281 * @param timeout next timeout (updated)
284 update_sets (struct GNUNET_SCHEDULER_Handle *sched,
285 struct GNUNET_NETWORK_FDSet *rs,
286 struct GNUNET_NETWORK_FDSet *ws,
287 struct GNUNET_TIME_Relative *timeout)
291 pos = sched->pending;
294 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
295 (GNUNET_YES == is_pending (sched, pos->prereq_id)))
301 if (pos->timeout.value != GNUNET_TIME_UNIT_FOREVER_ABS.value)
303 struct GNUNET_TIME_Relative to;
305 to = GNUNET_TIME_absolute_get_remaining (pos->timeout);
306 if (timeout->value > to.value)
309 if (pos->read_set != NULL)
310 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
311 if (pos->write_set != NULL)
312 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
313 if (pos->reason != 0)
314 *timeout = GNUNET_TIME_UNIT_ZERO;
321 * Check if the ready set overlaps with the set we want to have ready.
322 * If so, update the want set (set all FDs that are ready). If not,
325 * @param ready set that is ready
326 * @param want set that we want to be ready
327 * @return GNUNET_YES if there was some overlap
330 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
331 struct GNUNET_NETWORK_FDSet *want)
335 if (GNUNET_NETWORK_fdset_overlap (ready, want))
337 /* copy all over (yes, there maybe unrelated bits,
338 but this should not hurt well-written clients) */
339 GNUNET_NETWORK_fdset_copy (want, ready);
347 * Check if the given task is eligible to run now.
348 * Also set the reason why it is eligible.
350 * @param sched the scheduler
351 * @param task task to check if it is ready
352 * @param now the current time
353 * @param rs set of FDs ready for reading
354 * @param ws set of FDs ready for writing
355 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
358 is_ready (struct GNUNET_SCHEDULER_Handle *sched,
360 struct GNUNET_TIME_Absolute now,
361 const struct GNUNET_NETWORK_FDSet *rs,
362 const struct GNUNET_NETWORK_FDSet *ws)
364 if (now.value >= task->timeout.value)
365 task->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
366 if ((0 == (task->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
367 (rs != NULL) && (set_overlaps (rs, task->read_set)))
368 task->reason |= GNUNET_SCHEDULER_REASON_READ_READY;
369 if ((0 == (task->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
370 (ws != NULL) && (set_overlaps (ws, task->write_set)))
371 task->reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
372 if (task->reason == 0)
373 return GNUNET_NO; /* not ready */
374 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
376 if (GNUNET_YES == is_pending (sched, task->prereq_id))
377 return GNUNET_NO; /* prereq waiting */
378 task->reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
385 * Put a task that is ready for execution into the ready queue.
387 * @param handle the scheduler
388 * @param task task ready for execution
391 queue_ready_task (struct GNUNET_SCHEDULER_Handle *handle, struct Task *task)
393 enum GNUNET_SCHEDULER_Priority p = task->priority;
394 if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
395 p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
396 task->next = handle->ready[check_priority (p)];
397 handle->ready[check_priority (p)] = task;
398 handle->ready_count++;
403 * Check which tasks are ready and move them
404 * to the respective ready queue.
406 * @param handle the scheduler
407 * @param rs FDs ready for reading
408 * @param ws FDs ready for writing
411 check_ready (struct GNUNET_SCHEDULER_Handle *handle,
412 const struct GNUNET_NETWORK_FDSet *rs,
413 const struct GNUNET_NETWORK_FDSet *ws)
418 struct GNUNET_TIME_Absolute now;
420 now = GNUNET_TIME_absolute_get ();
422 pos = handle->pending;
426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "Checking readiness of task: %llu / %p\n",
428 pos->id, pos->callback_cls);
431 if (GNUNET_YES == is_ready (handle, pos, now, rs, ws))
434 handle->pending = next;
437 queue_ready_task (handle, pos);
448 * Request the shutdown of a scheduler. Marks all currently
449 * pending tasks as ready because of shutdown. This will
450 * cause all tasks to run (as soon as possible, respecting
451 * priorities and prerequisite tasks). Note that tasks
452 * scheduled AFTER this call may still be delayed arbitrarily.
454 * @param sched the scheduler
457 GNUNET_SCHEDULER_shutdown (struct GNUNET_SCHEDULER_Handle *sched)
462 pos = sched->pending;
465 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
466 /* we don't move the task into the ready queue yet; check_ready
467 will do that later, possibly adding additional
471 for (i=0;i<GNUNET_SCHEDULER_PRIORITY_COUNT;i++)
473 pos = sched->ready[i];
476 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
477 /* we don't move the task into the ready queue yet; check_ready
478 will do that later, possibly adding additional
487 * Destroy a task (release associated resources)
489 * @param t task to destroy
492 destroy_task (struct Task *t)
494 if (NULL != t->read_set)
495 GNUNET_NETWORK_fdset_destroy (t->read_set);
496 if (NULL != t->write_set)
497 GNUNET_NETWORK_fdset_destroy (t->write_set);
499 GNUNET_free (t->backtrace_strings);
506 * Run at least one task in the highest-priority queue that is not
507 * empty. Keep running tasks until we are either no longer running
508 * "URGENT" tasks or until we have at least one "pending" task (which
509 * may become ready, hence we should select on it). Naturally, if
510 * there are no more ready tasks, we also return.
512 * @param sched the scheduler
515 run_ready (struct GNUNET_SCHEDULER_Handle *sched)
517 enum GNUNET_SCHEDULER_Priority p;
519 struct GNUNET_SCHEDULER_TaskContext tc;
523 if (sched->ready_count == 0)
525 GNUNET_assert (sched->ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
526 /* yes, p>0 is correct, 0 is "KEEP" which should
527 always be an empty queue (see assertion)! */
528 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
530 pos = sched->ready[p];
534 GNUNET_assert (pos != NULL); /* ready_count wrong? */
535 sched->ready[p] = pos->next;
536 sched->ready_count--;
537 if (sched->current_priority != pos->priority)
539 sched->current_priority = pos->priority;
540 (void) GNUNET_OS_set_process_priority (0, pos->priority);
542 sched->active_task = pos;
544 if (GNUNET_TIME_absolute_get_duration (pos->start_time).value >
545 DELAY_THRESHOLD.value)
547 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
548 "Task %u took %llums to be scheduled\n",
550 (unsigned long long) GNUNET_TIME_absolute_get_duration (pos->start_time).value);
553 for (i=0;i<pos->num_backtrace_strings;i++)
554 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
555 "Task %u trace %d: %s\n",
558 pos->backtrace_strings[i]);
563 tc.reason = pos->reason;
564 tc.read_ready = pos->read_set;
565 tc.write_ready = pos->write_set;
566 pos->callback (pos->callback_cls, &tc);
568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
569 "Running task: %llu / %p\n", pos->id, pos->callback_cls);
571 sched->active_task = NULL;
575 while ((sched->pending == NULL) || (p == GNUNET_SCHEDULER_PRIORITY_URGENT));
579 * Pipe used to communicate shutdown via signal.
581 static struct GNUNET_DISK_PipeHandle *sigpipe;
585 * Signal handler called for signals that should cause us to shutdown.
588 sighandler_shutdown ()
592 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
593 (sigpipe, GNUNET_DISK_PIPE_END_WRITE), &c,
599 * Initialize and run scheduler. This function will return when all
600 * tasks have completed. On systems with signals, receiving a SIGTERM
601 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
602 * to be run after the active task is complete. As a result, SIGTERM
603 * causes all active tasks to be scheduled with reason
604 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
605 * afterwards will execute normally!). Note that any particular signal
606 * will only shut down one scheduler; applications should always only
607 * create a single scheduler.
609 * @param task task to run immediately
610 * @param task_cls closure of task
613 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
615 struct GNUNET_SCHEDULER_Handle sched;
616 struct GNUNET_NETWORK_FDSet *rs;
617 struct GNUNET_NETWORK_FDSet *ws;
618 struct GNUNET_TIME_Relative timeout;
620 struct GNUNET_SIGNAL_Context *shc_int;
621 struct GNUNET_SIGNAL_Context *shc_term;
622 struct GNUNET_SIGNAL_Context *shc_quit;
623 struct GNUNET_SIGNAL_Context *shc_hup;
624 unsigned long long last_tr;
625 unsigned int busy_wait_warning;
626 const struct GNUNET_DISK_FileHandle *pr;
629 rs = GNUNET_NETWORK_fdset_create ();
630 ws = GNUNET_NETWORK_fdset_create ();
631 GNUNET_assert (sigpipe == NULL);
632 sigpipe = GNUNET_DISK_pipe (GNUNET_NO);
633 GNUNET_assert (sigpipe != NULL);
634 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
635 GNUNET_assert (pr != NULL);
636 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
637 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
639 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
640 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
642 memset (&sched, 0, sizeof (sched));
643 sched.current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
644 GNUNET_SCHEDULER_add_continuation (&sched,
647 GNUNET_SCHEDULER_REASON_STARTUP);
649 busy_wait_warning = 0;
650 while ((sched.pending != NULL) || (sched.ready_count > 0))
652 GNUNET_NETWORK_fdset_zero (rs);
653 GNUNET_NETWORK_fdset_zero (ws);
654 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
655 update_sets (&sched, rs, ws, &timeout);
656 GNUNET_NETWORK_fdset_handle_set (rs, pr);
657 if (sched.ready_count > 0)
659 /* no blocking, more work already ready! */
660 timeout = GNUNET_TIME_UNIT_ZERO;
662 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
663 if (ret == GNUNET_SYSERR)
667 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
671 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid());
680 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
682 /* consume the signal */
683 GNUNET_DISK_file_read (pr, &c, sizeof (c));
684 /* mark all active tasks as ready due to shutdown */
685 GNUNET_SCHEDULER_shutdown (&sched);
687 if (last_tr == sched.tasks_run)
693 last_tr = sched.tasks_run;
694 busy_wait_warning = 0;
696 if ((ret == 0) && (timeout.value == 0) && (busy_wait_warning > 16))
698 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
699 _("Looks like we're busy waiting...\n"));
700 sleep (1); /* mitigate */
702 check_ready (&sched, rs, ws);
705 GNUNET_SIGNAL_handler_uninstall (shc_int);
706 GNUNET_SIGNAL_handler_uninstall (shc_term);
708 GNUNET_SIGNAL_handler_uninstall (shc_quit);
709 GNUNET_SIGNAL_handler_uninstall (shc_hup);
711 GNUNET_DISK_pipe_close (sigpipe);
713 GNUNET_NETWORK_fdset_destroy (rs);
714 GNUNET_NETWORK_fdset_destroy (ws);
719 * Obtain the reason code for why the current task was
720 * started. Will return the same value as
721 * the GNUNET_SCHEDULER_TaskContext's reason field.
723 * @param sched scheduler to query
724 * @return reason(s) why the current task is run
726 enum GNUNET_SCHEDULER_Reason
727 GNUNET_SCHEDULER_get_reason (struct GNUNET_SCHEDULER_Handle *sched)
729 return sched->active_task->reason;
734 * Get information about the current load of this scheduler. Use this
735 * function to determine if an elective task should be added or simply
736 * dropped (if the decision should be made based on the number of
737 * tasks ready to run).
739 * @param sched scheduler to query
740 * @param p priority level to look at
741 * @return number of tasks pending right now
744 GNUNET_SCHEDULER_get_load (struct GNUNET_SCHEDULER_Handle *sched,
745 enum GNUNET_SCHEDULER_Priority p)
750 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
751 return sched->ready_count;
752 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
753 p = sched->current_priority;
755 pos = sched->ready[p];
766 * Cancel the task with the specified identifier.
767 * The task must not yet have run.
769 * @param sched scheduler to use
770 * @param task id of the task to cancel
771 * @return original closure of the task
774 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Handle *sched,
775 GNUNET_SCHEDULER_TaskIdentifier task)
779 enum GNUNET_SCHEDULER_Priority p;
795 GNUNET_assert (p < GNUNET_SCHEDULER_PRIORITY_COUNT);
802 sched->ready_count--;
812 sched->pending = t->next;
814 sched->ready[p] = t->next;
817 prev->next = t->next;
818 ret = t->callback_cls;
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "Canceling task: %llu / %p\n", task, t->callback_cls);
829 * Continue the current execution with the given function. This is
830 * similar to the other "add" functions except that there is no delay
831 * and the reason code can be specified.
833 * @param sched scheduler to use
834 * @param task main function of the task
835 * @param task_cls closure for 'main'
836 * @param reason reason for task invocation
839 GNUNET_SCHEDULER_add_continuation (struct GNUNET_SCHEDULER_Handle *sched,
840 GNUNET_SCHEDULER_Task task,
842 enum GNUNET_SCHEDULER_Reason reason)
846 void *backtrace_array[50];
848 t = GNUNET_malloc (sizeof (struct Task));
850 t->num_backtrace_strings = backtrace(backtrace_array, 50);
851 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
854 t->callback_cls = task_cls;
855 t->id = ++sched->last_id;
857 t->start_time = GNUNET_TIME_absolute_get ();
860 t->priority = sched->current_priority;
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
863 "Adding continuation task: %llu / %p\n",
864 t->id, t->callback_cls);
866 queue_ready_task (sched, t);
872 * Schedule a new task to be run after the specified prerequisite task
873 * has completed. It will be run with the priority of the calling
876 * @param sched scheduler to use
877 * @param prerequisite_task run this task after the task with the given
878 * task identifier completes (and any of our other
879 * conditions, such as delay, read or write-readiness
880 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
881 * on completion of other tasks (this will cause the task to run as
883 * @param task main function of the task
884 * @param task_cls closure of task
885 * @return unique task identifier for the job
886 * only valid until "task" is started!
888 GNUNET_SCHEDULER_TaskIdentifier
889 GNUNET_SCHEDULER_add_after (struct GNUNET_SCHEDULER_Handle *sched,
890 GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
891 GNUNET_SCHEDULER_Task task, void *task_cls)
893 return GNUNET_SCHEDULER_add_select (sched,
894 GNUNET_SCHEDULER_PRIORITY_KEEP,
896 GNUNET_TIME_UNIT_ZERO,
897 NULL, NULL, task, task_cls);
902 * Schedule a new task to be run with a specified priority.
904 * @param sched scheduler to use
905 * @param prio how important is the new task?
906 * @param task main function of the task
907 * @param task_cls closure of task
908 * @return unique task identifier for the job
909 * only valid until "task" is started!
911 GNUNET_SCHEDULER_TaskIdentifier
912 GNUNET_SCHEDULER_add_with_priority (struct GNUNET_SCHEDULER_Handle * sched,
913 enum GNUNET_SCHEDULER_Priority prio,
914 GNUNET_SCHEDULER_Task task,
917 return GNUNET_SCHEDULER_add_select (sched,
919 GNUNET_SCHEDULER_NO_TASK,
920 GNUNET_TIME_UNIT_ZERO,
921 NULL, NULL, task, task_cls);
927 * Schedule a new task to be run with a specified delay. The task
928 * will be scheduled for execution once the delay has expired. It
929 * will be run with the priority of the calling task.
931 * @param sched scheduler to use
932 * @param delay when should this operation time out? Use
933 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
934 * @param task main function of the task
935 * @param task_cls closure of task
936 * @return unique task identifier for the job
937 * only valid until "task" is started!
939 GNUNET_SCHEDULER_TaskIdentifier
940 GNUNET_SCHEDULER_add_delayed (struct GNUNET_SCHEDULER_Handle * sched,
941 struct GNUNET_TIME_Relative delay,
942 GNUNET_SCHEDULER_Task task, void *task_cls)
944 return GNUNET_SCHEDULER_add_select (sched,
945 GNUNET_SCHEDULER_PRIORITY_KEEP,
946 GNUNET_SCHEDULER_NO_TASK, delay,
947 NULL, NULL, task, task_cls);
953 * Schedule a new task to be run as soon as possible. The task
954 * will be run with the priority of the calling task.
956 * @param sched scheduler to use
957 * @param task main function of the task
958 * @param task_cls closure of task
959 * @return unique task identifier for the job
960 * only valid until "task" is started!
962 GNUNET_SCHEDULER_TaskIdentifier
963 GNUNET_SCHEDULER_add_now (struct GNUNET_SCHEDULER_Handle *sched,
964 GNUNET_SCHEDULER_Task task,
967 return GNUNET_SCHEDULER_add_select (sched,
968 GNUNET_SCHEDULER_PRIORITY_KEEP,
969 GNUNET_SCHEDULER_NO_TASK,
970 GNUNET_TIME_UNIT_ZERO,
971 NULL, NULL, task, task_cls);
977 * Schedule a new task to be run with a specified delay or when the
978 * specified file descriptor is ready for reading. The delay can be
979 * used as a timeout on the socket being ready. The task will be
980 * scheduled for execution once either the delay has expired or the
981 * socket operation is ready. It will be run with the priority of
984 * @param sched scheduler to use
985 * @param delay when should this operation time out? Use
986 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
987 * @param rfd read file-descriptor
988 * @param task main function of the task
989 * @param task_cls closure of task
990 * @return unique task identifier for the job
991 * only valid until "task" is started!
993 GNUNET_SCHEDULER_TaskIdentifier
994 GNUNET_SCHEDULER_add_read_net (struct GNUNET_SCHEDULER_Handle * sched,
995 struct GNUNET_TIME_Relative delay,
996 struct GNUNET_NETWORK_Handle * rfd,
997 GNUNET_SCHEDULER_Task task, void *task_cls)
999 struct GNUNET_NETWORK_FDSet *rs;
1000 GNUNET_SCHEDULER_TaskIdentifier ret;
1002 GNUNET_assert (rfd != NULL);
1003 rs = GNUNET_NETWORK_fdset_create ();
1004 GNUNET_NETWORK_fdset_set (rs, rfd);
1005 ret = GNUNET_SCHEDULER_add_select (sched,
1006 GNUNET_SCHEDULER_PRIORITY_KEEP,
1007 GNUNET_SCHEDULER_NO_TASK,
1008 delay, rs, NULL, task, task_cls);
1009 GNUNET_NETWORK_fdset_destroy (rs);
1015 * Schedule a new task to be run with a specified delay or when the
1016 * specified file descriptor is ready for writing. The delay can be
1017 * used as a timeout on the socket being ready. The task will be
1018 * scheduled for execution once either the delay has expired or the
1019 * socket operation is ready. It will be run with the priority of
1022 * @param sched scheduler to use
1023 * @param delay when should this operation time out? Use
1024 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1025 * @param wfd write file-descriptor
1026 * @param task main function of the task
1027 * @param task_cls closure of task
1028 * @return unique task identifier for the job
1029 * only valid until "task" is started!
1031 GNUNET_SCHEDULER_TaskIdentifier
1032 GNUNET_SCHEDULER_add_write_net (struct GNUNET_SCHEDULER_Handle * sched,
1033 struct GNUNET_TIME_Relative delay,
1034 struct GNUNET_NETWORK_Handle * wfd,
1035 GNUNET_SCHEDULER_Task task, void *task_cls)
1037 struct GNUNET_NETWORK_FDSet *ws;
1038 GNUNET_SCHEDULER_TaskIdentifier ret;
1040 GNUNET_assert (wfd != NULL);
1041 ws = GNUNET_NETWORK_fdset_create ();
1042 GNUNET_NETWORK_fdset_set (ws, wfd);
1043 ret = GNUNET_SCHEDULER_add_select (sched,
1044 GNUNET_SCHEDULER_PRIORITY_KEEP,
1045 GNUNET_SCHEDULER_NO_TASK, delay,
1046 NULL, ws, task, task_cls);
1047 GNUNET_NETWORK_fdset_destroy (ws);
1053 * Schedule a new task to be run with a specified delay or when the
1054 * specified file descriptor is ready for reading. The delay can be
1055 * used as a timeout on the socket being ready. The task will be
1056 * scheduled for execution once either the delay has expired or the
1057 * socket operation is ready. It will be run with the priority of
1060 * @param sched scheduler to use
1061 * @param delay when should this operation time out? Use
1062 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1063 * @param rfd read file-descriptor
1064 * @param task main function of the task
1065 * @param task_cls closure of task
1066 * @return unique task identifier for the job
1067 * only valid until "task" is started!
1069 GNUNET_SCHEDULER_TaskIdentifier
1070 GNUNET_SCHEDULER_add_read_file (struct GNUNET_SCHEDULER_Handle * sched,
1071 struct GNUNET_TIME_Relative delay,
1072 const struct GNUNET_DISK_FileHandle * rfd,
1073 GNUNET_SCHEDULER_Task task, void *task_cls)
1075 struct GNUNET_NETWORK_FDSet *rs;
1076 GNUNET_SCHEDULER_TaskIdentifier ret;
1078 GNUNET_assert (rfd != NULL);
1079 rs = GNUNET_NETWORK_fdset_create ();
1080 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1081 ret = GNUNET_SCHEDULER_add_select (sched,
1082 GNUNET_SCHEDULER_PRIORITY_KEEP,
1083 GNUNET_SCHEDULER_NO_TASK, delay,
1084 rs, NULL, task, task_cls);
1085 GNUNET_NETWORK_fdset_destroy (rs);
1091 * Schedule a new task to be run with a specified delay or when the
1092 * specified file descriptor is ready for writing. The delay can be
1093 * used as a timeout on the socket being ready. The task will be
1094 * scheduled for execution once either the delay has expired or the
1095 * socket operation is ready. It will be run with the priority of
1098 * @param sched scheduler to use
1099 * @param delay when should this operation time out? Use
1100 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1101 * @param wfd write file-descriptor
1102 * @param task main function of the task
1103 * @param task_cls closure of task
1104 * @return unique task identifier for the job
1105 * only valid until "task" is started!
1107 GNUNET_SCHEDULER_TaskIdentifier
1108 GNUNET_SCHEDULER_add_write_file (struct GNUNET_SCHEDULER_Handle * sched,
1109 struct GNUNET_TIME_Relative delay,
1110 const struct GNUNET_DISK_FileHandle * wfd,
1111 GNUNET_SCHEDULER_Task task, void *task_cls)
1113 struct GNUNET_NETWORK_FDSet *ws;
1114 GNUNET_SCHEDULER_TaskIdentifier ret;
1116 GNUNET_assert (wfd != NULL);
1117 ws = GNUNET_NETWORK_fdset_create ();
1118 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1119 ret = GNUNET_SCHEDULER_add_select (sched,
1120 GNUNET_SCHEDULER_PRIORITY_KEEP,
1121 GNUNET_SCHEDULER_NO_TASK,
1122 delay, NULL, ws, task, task_cls);
1123 GNUNET_NETWORK_fdset_destroy (ws);
1130 * Schedule a new task to be run with a specified delay or when any of
1131 * the specified file descriptor sets is ready. The delay can be used
1132 * as a timeout on the socket(s) being ready. The task will be
1133 * scheduled for execution once either the delay has expired or any of
1134 * the socket operations is ready. This is the most general
1135 * function of the "add" family. Note that the "prerequisite_task"
1136 * must be satisfied in addition to any of the other conditions. In
1137 * other words, the task will be started when
1139 * (prerequisite-run)
1143 * || (shutdown-active && run-on-shutdown) )
1146 * @param sched scheduler to use
1147 * @param prio how important is this task?
1148 * @param prerequisite_task run this task after the task with the given
1149 * task identifier completes (and any of our other
1150 * conditions, such as delay, read or write-readiness
1151 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1152 * on completion of other tasks.
1153 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1154 * which means that the task will only be run after we receive SIGTERM
1155 * @param rs set of file descriptors we want to read (can be NULL)
1156 * @param ws set of file descriptors we want to write (can be NULL)
1157 * @param task main function of the task
1158 * @param task_cls closure of task
1159 * @return unique task identifier for the job
1160 * only valid until "task" is started!
1162 GNUNET_SCHEDULER_TaskIdentifier
1163 GNUNET_SCHEDULER_add_select (struct GNUNET_SCHEDULER_Handle * sched,
1164 enum GNUNET_SCHEDULER_Priority prio,
1165 GNUNET_SCHEDULER_TaskIdentifier
1167 struct GNUNET_TIME_Relative delay,
1168 const struct GNUNET_NETWORK_FDSet * rs,
1169 const struct GNUNET_NETWORK_FDSet * ws,
1170 GNUNET_SCHEDULER_Task task, void *task_cls)
1174 void *backtrace_array[MAX_TRACE_DEPTH];
1177 GNUNET_assert (NULL != task);
1178 t = GNUNET_malloc (sizeof (struct Task));
1180 t->callback_cls = task_cls;
1182 t->num_backtrace_strings = backtrace(backtrace_array, MAX_TRACE_DEPTH);
1183 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1187 t->read_set = GNUNET_NETWORK_fdset_create ();
1188 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1192 t->write_set = GNUNET_NETWORK_fdset_create ();
1193 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1195 t->id = ++sched->last_id;
1197 t->start_time = GNUNET_TIME_absolute_get ();
1199 t->prereq_id = prerequisite_task;
1200 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1202 check_priority ((prio ==
1203 GNUNET_SCHEDULER_PRIORITY_KEEP) ? sched->current_priority
1205 t->next = sched->pending;
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1209 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1214 /* end of scheduler.c */