2 This file is part of GNUnet
3 Copyright (C) 2009-2017 GNUnet e.V.
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 3, 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file util/scheduler.c
22 * @brief schedule computations using continuation passing style
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
29 #define LOG(kind,...) GNUNET_log_from (kind, "util-scheduler", __VA_ARGS__)
31 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util-scheduler", syscall)
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
60 * Should we figure out which tasks are delayed for a while
61 * before they are run? (Consider using in combination with EXECINFO).
63 #define PROFILE_DELAYS GNUNET_NO
66 * Task that were in the queue for longer than this are reported if
67 * PROFILE_DELAYS is active.
69 #define DELAY_THRESHOLD GNUNET_TIME_UNIT_SECONDS
73 * Argument to be passed from the driver to
74 * #GNUNET_SCHEDULER_run_from_driver(). Contains the
75 * scheduler's internal state.
77 struct GNUNET_SCHEDULER_Handle
80 * Passed here to avoid constantly allocating/deallocating
81 * this element, but generally we want to get rid of this.
84 struct GNUNET_NETWORK_FDSet *rs;
87 * Passed here to avoid constantly allocating/deallocating
88 * this element, but generally we want to get rid of this.
91 struct GNUNET_NETWORK_FDSet *ws;
94 * Driver we used for the event loop.
96 const struct GNUNET_SCHEDULER_Driver *driver;
102 * Entry in list of pending tasks.
104 struct GNUNET_SCHEDULER_Task
107 * This is a linked list.
109 struct GNUNET_SCHEDULER_Task *next;
112 * This is a linked list.
114 struct GNUNET_SCHEDULER_Task *prev;
117 * Function to run when ready.
119 GNUNET_SCHEDULER_TaskCallback callback;
122 * Closure for the @e callback.
127 * Handle to the scheduler's state.
129 const struct GNUNET_SCHEDULER_Handle *sh;
132 * Set of file descriptors this task is waiting
133 * for for reading. Once ready, this is updated
134 * to reflect the set of file descriptors ready
137 struct GNUNET_NETWORK_FDSet *read_set;
140 * Set of file descriptors this task is waiting for for writing.
141 * Once ready, this is updated to reflect the set of file
142 * descriptors ready for operation.
144 struct GNUNET_NETWORK_FDSet *write_set;
147 * Information about which FDs are ready for this task (and why).
149 const struct GNUNET_SCHEDULER_FdInfo *fds;
152 * Storage location used for @e fds if we want to avoid
153 * a separate malloc() call in the common case that this
154 * task is only about a single FD.
156 struct GNUNET_SCHEDULER_FdInfo fdx;
159 * Absolute timeout value for the task, or
160 * #GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
162 struct GNUNET_TIME_Absolute timeout;
166 * When was the task scheduled?
168 struct GNUNET_TIME_Absolute start_time;
172 * Size of the @e fds array.
174 unsigned int fds_len;
177 * Why is the task ready? Set after task is added to ready queue.
178 * Initially set to zero. All reasons that have already been
179 * satisfied (i.e. read or write ready) will be set over time.
181 enum GNUNET_SCHEDULER_Reason reason;
186 enum GNUNET_SCHEDULER_Priority priority;
189 * Set if we only wait for reading from a single FD, otherwise -1.
194 * Set if we only wait for writing to a single FD, otherwise -1.
199 * Should the existence of this task in the queue be counted as
200 * reason to not shutdown the scheduler?
205 * Is this task run on shutdown?
210 * Is this task in the ready list?
216 * Array of strings which make up a backtrace from the point when this
217 * task was scheduled (essentially, who scheduled the task?)
219 char **backtrace_strings;
222 * Size of the backtrace_strings array
224 int num_backtrace_strings;
231 * The driver used for the event loop. Will be handed over to
232 * the scheduler in #GNUNET_SCHEDULER_run_from_driver(), peristed
233 * there in this variable for later use in functions like
234 * #GNUNET_SCHEDULER_add_select(), #add_without_sets() and
235 * #GNUNET_SCHEDULER_cancel().
237 static const struct GNUNET_SCHEDULER_Driver *scheduler_driver;
240 * Head of list of tasks waiting for an event.
242 static struct GNUNET_SCHEDULER_Task *pending_head;
245 * Tail of list of tasks waiting for an event.
247 static struct GNUNET_SCHEDULER_Task *pending_tail;
250 * Head of list of tasks waiting for shutdown.
252 static struct GNUNET_SCHEDULER_Task *shutdown_head;
255 * Tail of list of tasks waiting for shutdown.
257 static struct GNUNET_SCHEDULER_Task *shutdown_tail;
260 * List of tasks waiting ONLY for a timeout event.
261 * Sorted by timeout (earliest first). Used so that
262 * we do not traverse the list of these tasks when
263 * building select sets (we just look at the head
264 * to determine the respective timeout ONCE).
266 static struct GNUNET_SCHEDULER_Task *pending_timeout_head;
269 * List of tasks waiting ONLY for a timeout event.
270 * Sorted by timeout (earliest first). Used so that
271 * we do not traverse the list of these tasks when
272 * building select sets (we just look at the head
273 * to determine the respective timeout ONCE).
275 static struct GNUNET_SCHEDULER_Task *pending_timeout_tail;
278 * Last inserted task waiting ONLY for a timeout event.
279 * Used to (heuristically) speed up insertion.
281 static struct GNUNET_SCHEDULER_Task *pending_timeout_last;
284 * ID of the task that is running right now.
286 static struct GNUNET_SCHEDULER_Task *active_task;
289 * Head of list of tasks ready to run right now, grouped by importance.
291 static struct GNUNET_SCHEDULER_Task *ready_head[GNUNET_SCHEDULER_PRIORITY_COUNT];
294 * Tail of list of tasks ready to run right now, grouped by importance.
296 static struct GNUNET_SCHEDULER_Task *ready_tail[GNUNET_SCHEDULER_PRIORITY_COUNT];
299 * Number of tasks on the ready list.
301 static unsigned int ready_count;
304 * How many tasks have we run so far?
306 static unsigned long long tasks_run;
309 * Priority of the task running right now. Only
310 * valid while a task is running.
312 static enum GNUNET_SCHEDULER_Priority current_priority;
315 * Priority of the highest task added in the current select
318 static enum GNUNET_SCHEDULER_Priority max_priority_added;
321 * Value of the 'lifeness' flag for the current task.
323 static int current_lifeness;
326 * Function to use as a select() in the scheduler.
327 * If NULL, we use GNUNET_NETWORK_socket_select().
329 static GNUNET_SCHEDULER_select scheduler_select;
332 * Task context of the current task.
334 static struct GNUNET_SCHEDULER_TaskContext tc;
337 * Closure for #scheduler_select.
339 static void *scheduler_select_cls;
343 * Sets the select function to use in the scheduler (scheduler_select).
345 * @param new_select new select function to use
346 * @param new_select_cls closure for @a new_select
347 * @return previously used select function, NULL for default
350 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
351 void *new_select_cls)
353 scheduler_select = new_select;
354 scheduler_select_cls = new_select_cls;
359 * Check that the given priority is legal (and return it).
361 * @param p priority value to check
362 * @return p on success, 0 on error
364 static enum GNUNET_SCHEDULER_Priority
365 check_priority (enum GNUNET_SCHEDULER_Priority p)
367 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
370 return 0; /* make compiler happy */
375 * Update all sets and timeout for select.
377 * @param rs read-set, set to all FDs we would like to read (updated)
378 * @param ws write-set, set to all FDs we would like to write (updated)
379 * @param timeout next timeout (updated)
381 void getNextPendingTimeout(struct GNUNET_TIME_Relative *timeout)
384 struct GNUNET_SCHEDULER_Task *pos;
385 struct GNUNET_TIME_Absolute now;
386 struct GNUNET_TIME_Relative to;
388 now = GNUNET_TIME_absolute_get ();
389 pos = pending_timeout_head;
392 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
393 if (timeout->rel_value_us > to.rel_value_us)
395 if (0 != pos->reason)
396 *timeout = GNUNET_TIME_UNIT_ZERO;
401 update_sets (struct GNUNET_NETWORK_FDSet *rs,
402 struct GNUNET_NETWORK_FDSet *ws,
403 struct GNUNET_TIME_Relative *timeout)
405 struct GNUNET_SCHEDULER_Task *pos;
406 struct GNUNET_TIME_Absolute now;
407 struct GNUNET_TIME_Relative to;
409 now = GNUNET_TIME_absolute_get ();
411 getNextPendingTimeout(timeout);
412 for (pos = pending_head; NULL != pos; pos = pos->next)
414 if (pos->timeout.abs_value_us != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
416 to = GNUNET_TIME_absolute_get_difference (now, pos->timeout);
417 if (timeout->rel_value_us > to.rel_value_us)
420 if (-1 != pos->read_fd)
421 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
422 if (-1 != pos->write_fd)
423 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
424 if (NULL != pos->read_set)
425 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
426 if (NULL != pos->write_set)
427 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
428 if (0 != pos->reason)
429 *timeout = GNUNET_TIME_UNIT_ZERO;
435 * Check if the ready set overlaps with the set we want to have ready.
436 * If so, update the want set (set all FDs that are ready). If not,
439 * @param ready set that is ready
440 * @param want set that we want to be ready
441 * @return #GNUNET_YES if there was some overlap
444 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
445 struct GNUNET_NETWORK_FDSet *want)
447 if ((NULL == want) || (NULL == ready))
449 if (GNUNET_NETWORK_fdset_overlap (ready, want))
451 /* copy all over (yes, there maybe unrelated bits,
452 * but this should not hurt well-written clients) */
453 GNUNET_NETWORK_fdset_copy (want, ready);
461 * Check if the given task is eligible to run now.
462 * Also set the reason why it is eligible.
464 * @param task task to check if it is ready
465 * @param now the current time
466 * @param rs set of FDs ready for reading
467 * @param ws set of FDs ready for writing
468 * @return #GNUNET_YES if we can run it, #GNUNET_NO if not.
471 is_ready (struct GNUNET_SCHEDULER_Task *task,
472 struct GNUNET_TIME_Absolute now,
473 const struct GNUNET_NETWORK_FDSet *rs,
474 const struct GNUNET_NETWORK_FDSet *ws)
476 enum GNUNET_SCHEDULER_Reason reason;
478 reason = task->reason;
479 if (now.abs_value_us >= task->timeout.abs_value_us)
480 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
481 if ((0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
482 (((task->read_fd != -1) &&
483 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (rs, task->read_fd))) ||
484 (set_overlaps (rs, task->read_set))))
485 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
486 if ((0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
487 (((task->write_fd != -1) &&
488 (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (ws, task->write_fd)))
489 || (set_overlaps (ws, task->write_set))))
490 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
492 return GNUNET_NO; /* not ready */
493 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
494 task->reason = reason;
500 * Put a task that is ready for execution into the ready queue.
502 * @param task task ready for execution
505 queue_ready_task (struct GNUNET_SCHEDULER_Task *task)
507 enum GNUNET_SCHEDULER_Priority p = check_priority (task->priority);
509 GNUNET_CONTAINER_DLL_insert (ready_head[p],
512 task->in_ready_list = GNUNET_YES;
518 * Check which tasks are ready and move them
519 * to the respective ready queue.
521 * @param rs FDs ready for reading
522 * @param ws FDs ready for writing
525 check_ready (const struct GNUNET_NETWORK_FDSet *rs,
526 const struct GNUNET_NETWORK_FDSet *ws)
528 struct GNUNET_SCHEDULER_Task *pos;
529 struct GNUNET_SCHEDULER_Task *next;
530 struct GNUNET_TIME_Absolute now;
532 now = GNUNET_TIME_absolute_get ();
533 while (NULL != (pos = pending_timeout_head))
535 if (now.abs_value_us >= pos->timeout.abs_value_us)
536 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
537 if (0 == pos->reason)
539 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
540 pending_timeout_tail,
542 scheduler_driver->set_wakeup(scheduler_driver->cls,pending_timeout_head->timeout);
543 if (pending_timeout_last == pos)
544 pending_timeout_last = NULL;
545 queue_ready_task (pos);
551 if (GNUNET_YES == is_ready (pos, now, rs, ws))
553 GNUNET_CONTAINER_DLL_remove (pending_head,
556 queue_ready_task (pos);
564 * Request the shutdown of a scheduler. Marks all tasks
565 * awaiting shutdown as ready. Note that tasks
566 * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call
567 * will be delayed until the next shutdown signal.
570 GNUNET_SCHEDULER_shutdown ()
572 struct GNUNET_SCHEDULER_Task *pos;
574 while (NULL != (pos = shutdown_head))
576 GNUNET_CONTAINER_DLL_remove (shutdown_head,
579 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
580 queue_ready_task (pos);
586 * Destroy a task (release associated resources)
588 * @param t task to destroy
591 destroy_task (struct GNUNET_SCHEDULER_Task *t)
593 if (NULL != t->read_set)
594 GNUNET_NETWORK_fdset_destroy (t->read_set);
595 if (NULL != t->write_set)
596 GNUNET_NETWORK_fdset_destroy (t->write_set);
598 GNUNET_free (t->backtrace_strings);
605 * Output stack trace of task @a t.
607 * @param t task to dump stack trace of
610 dump_backtrace (struct GNUNET_SCHEDULER_Task *t)
615 for (i = 0; i < t->num_backtrace_strings; i++)
616 LOG (GNUNET_ERROR_TYPE_DEBUG,
617 "Task %p trace %u: %s\n",
620 t->backtrace_strings[i]);
626 * Run at least one task in the highest-priority queue that is not
627 * empty. Keep running tasks until we are either no longer running
628 * "URGENT" tasks or until we have at least one "pending" task (which
629 * may become ready, hence we should select on it). Naturally, if
630 * there are no more ready tasks, we also return.
632 * @param rs FDs ready for reading
633 * @param ws FDs ready for writing
636 run_ready (struct GNUNET_NETWORK_FDSet *rs,
637 struct GNUNET_NETWORK_FDSet *ws)
639 enum GNUNET_SCHEDULER_Priority p;
640 struct GNUNET_SCHEDULER_Task *pos;
642 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
645 if (0 == ready_count)
647 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
648 /* yes, p>0 is correct, 0 is "KEEP" which should
649 * always be an empty queue (see assertion)! */
650 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
656 GNUNET_assert (NULL != pos); /* ready_count wrong? */
657 GNUNET_CONTAINER_DLL_remove (ready_head[p],
661 current_priority = pos->priority;
662 current_lifeness = pos->lifeness;
665 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
666 DELAY_THRESHOLD.rel_value_us)
668 LOG (GNUNET_ERROR_TYPE_DEBUG,
669 "Task %p took %s to be scheduled\n",
671 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
675 tc.reason = pos->reason;
676 tc.read_ready = (NULL == pos->read_set) ? rs : pos->read_set;
677 if ((-1 != pos->read_fd) &&
678 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)))
679 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
680 tc.write_ready = (NULL == pos->write_set) ? ws : pos->write_set;
681 if ((-1 != pos->write_fd) &&
682 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
683 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
684 if ((0 != (tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
685 (-1 != pos->write_fd) &&
686 (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd)))
687 GNUNET_assert (0); // added to ready in previous select loop!
688 LOG (GNUNET_ERROR_TYPE_DEBUG,
689 "Running task: %p\n",
691 pos->callback (pos->callback_cls);
692 dump_backtrace (pos);
697 while ((NULL == pending_head) || (p >= max_priority_added));
702 * Pipe used to communicate shutdown via signal.
704 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
707 * Process ID of this process at the time we installed the various
713 * Signal handler called for SIGPIPE.
725 * Wait for a short time.
726 * Sleeps for @a ms ms (as that should be long enough for virtually all
727 * modern systems to context switch and allow another process to do
730 * @param ms how many ms to wait
733 short_wait (unsigned int ms)
735 struct GNUNET_TIME_Relative timeout;
737 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
738 (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
743 * Signal handler called for signals that should cause us to shutdown.
746 sighandler_shutdown ()
749 int old_errno = errno; /* backup errno */
751 if (getpid () != my_pid)
752 exit (1); /* we have fork'ed since the signal handler was created,
753 * ignore the signal, see https://gnunet.org/vfork discussion */
754 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
755 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
762 * Check if the system is still alive. Trigger shutdown if we
763 * have tasks, but none of them give us lifeness.
765 * @return #GNUNET_OK to continue the main loop,
771 struct GNUNET_SCHEDULER_Task *t;
775 for (t = pending_head; NULL != t; t = t->next)
776 if (t->lifeness == GNUNET_YES)
778 for (t = shutdown_head; NULL != t; t = t->next)
779 if (t->lifeness == GNUNET_YES)
781 for (t = pending_timeout_head; NULL != t; t = t->next)
782 if (t->lifeness == GNUNET_YES)
784 if (NULL != shutdown_head)
786 GNUNET_SCHEDULER_shutdown ();
794 int while_live(struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws)
797 unsigned int busy_wait_warning;
798 unsigned long long last_tr;
799 const struct GNUNET_DISK_FileHandle *pr;
801 struct GNUNET_TIME_Relative timeout;
803 busy_wait_warning = 0;
804 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
805 GNUNET_DISK_PIPE_END_READ);
806 GNUNET_assert (NULL != pr);
809 while (GNUNET_OK == check_lifeness ())
811 GNUNET_NETWORK_fdset_zero (rs);
812 GNUNET_NETWORK_fdset_zero (ws);
813 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
814 update_sets (rs, ws, &timeout);
815 GNUNET_NETWORK_fdset_handle_set (rs, pr);
818 /* no blocking, more work already ready! */
819 timeout = GNUNET_TIME_UNIT_ZERO;
821 if (NULL == scheduler_select)
822 ret = GNUNET_NETWORK_socket_select (rs,
827 ret = scheduler_select (scheduler_select_cls,
832 if (ret == GNUNET_SYSERR)
837 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
842 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
845 if (0 != system (lsof))
846 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
851 struct GNUNET_SCHEDULER_Task *t;
853 for (t = pending_head; NULL != t; t = t->next)
855 if (-1 != t->read_fd)
857 int flags = fcntl (t->read_fd, F_GETFD);
858 if ((flags == -1) && (errno == EBADF))
860 LOG (GNUNET_ERROR_TYPE_ERROR,
861 "Got invalid file descriptor %d!\n",
866 if (-1 != t->write_fd)
868 int flags = fcntl (t->write_fd, F_GETFD);
869 if ((flags == -1) && (errno == EBADF))
871 LOG (GNUNET_ERROR_TYPE_ERROR,
872 "Got invalid file descriptor %d!\n",
884 (0 == timeout.rel_value_us) &&
885 (busy_wait_warning > 16) )
887 LOG (GNUNET_ERROR_TYPE_WARNING,
888 "Looks like we're busy waiting...\n");
889 short_wait (100); /* mitigate */
891 check_ready (rs, ws);
893 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
895 /* consume the signal */
896 GNUNET_DISK_file_read (pr, &c, sizeof (c));
897 /* mark all active tasks as ready due to shutdown */
898 GNUNET_SCHEDULER_shutdown ();
900 if (last_tr == tasks_run)
908 busy_wait_warning = 0;
915 * Initialize and run scheduler. This function will return when all
916 * tasks have completed. On systems with signals, receiving a SIGTERM
917 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown()
918 * to be run after the active task is complete. As a result, SIGTERM
919 * causes all active tasks to be scheduled with reason
920 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
921 * afterwards will execute normally!). Note that any particular signal
922 * will only shut down one scheduler; applications should always only
923 * create a single scheduler.
925 * @param task task to run immediately
926 * @param task_cls closure of @a task
929 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
933 GNUNET_SCHEDULER_run_with_driver(GNUNET_SCHEDULER_driver_select (), task, task_cls);
939 * Obtain the task context, giving the reason why the current task was
942 * @return current tasks' scheduler context
944 const struct GNUNET_SCHEDULER_TaskContext *
945 GNUNET_SCHEDULER_get_task_context ()
947 GNUNET_assert (NULL != active_task);
953 * Get information about the current load of this scheduler. Use this
954 * function to determine if an elective task should be added or simply
955 * dropped (if the decision should be made based on the number of
956 * tasks ready to run).
958 * @param p priority level to look at
959 * @return number of tasks pending right now
962 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
964 struct GNUNET_SCHEDULER_Task *pos;
967 GNUNET_assert (NULL != active_task);
968 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
970 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
971 p = current_priority;
973 for (pos = ready_head[check_priority (p)]; NULL != pos; pos = pos->next)
980 initFdInfo(struct GNUNET_SCHEDULER_Task *t,
981 const struct GNUNET_NETWORK_Handle *read_nh,
982 const struct GNUNET_NETWORK_Handle *write_nh,
983 const struct GNUNET_DISK_FileHandle *read_fh,
984 const struct GNUNET_DISK_FileHandle *write_fh)
986 // either only network handles or only file handles are allowed
987 GNUNET_assert (!((NULL != read_nh || NULL != write_nh) && (NULL != read_fh || NULL != write_fh)));
989 if (NULL != read_nh && NULL != write_nh)
992 t->fds = GNUNET_new_array (2, struct GNUNET_SCHEDULER_FdInfo);
993 const struct GNUNET_SCHEDULER_FdInfo read_fdi = { .fd = read_nh, .et = GNUNET_SCHEDULER_ET_IN, .sock = GNUNET_NETWORK_get_fd (read_nh)};
994 const struct GNUNET_SCHEDULER_FdInfo write_fdi = { .fd = write_nh, .et = GNUNET_SCHEDULER_ET_OUT, .sock = GNUNET_NETWORK_get_fd (write_nh)};
996 const struct GNUNET_SCHEDULER_FdInfo array[2] = {read_fdi, write_fdi};
999 else if (NULL != read_fh && NULL != write_fh)
1002 t->fds = GNUNET_new_array (2, struct GNUNET_SCHEDULER_FdInfo);
1003 const struct GNUNET_SCHEDULER_FdInfo read_fdi = { .fh = read_fh, .et = GNUNET_SCHEDULER_ET_IN};
1004 const struct GNUNET_SCHEDULER_FdInfo write_fdi = { .fh = write_fh, .et = GNUNET_SCHEDULER_ET_OUT, .sock = GNUNET_NETWORK_get_fd (write_nh)};
1005 const struct GNUNET_SCHEDULER_FdInfo array[2] = {read_fdi, write_fdi};
1008 else if (NULL != read_nh)
1010 struct GNUNET_SCHEDULER_FdInfo read_fdi = { .fd = read_nh, .et = GNUNET_SCHEDULER_ET_IN, .sock = GNUNET_NETWORK_get_fd (read_nh)};
1013 else if (NULL != write_nh)
1015 struct GNUNET_SCHEDULER_FdInfo write_fdi = { .fd = write_nh, .et = GNUNET_SCHEDULER_ET_OUT, .sock = GNUNET_NETWORK_get_fd (write_nh)};
1018 else if (NULL != read_fh)
1020 struct GNUNET_SCHEDULER_FdInfo read_fdi = { .fh = read_fh, .et = GNUNET_SCHEDULER_ET_IN, .sock = read_fh->fd};
1023 else if (NULL != write_fh)
1025 struct GNUNET_SCHEDULER_FdInfo write_fdi = { .fh = write_fh, .et = GNUNET_SCHEDULER_ET_OUT, .sock = write_fh->fd};
1031 int scheduler_multi_function_call(struct GNUNET_SCHEDULER_Task *t, int (*driver_func)())
1033 if (t->fds_len > 1){
1034 int success = GNUNET_YES;
1035 for (int i = 0; i < t->fds_len;i++){
1036 success = driver_func(scheduler_driver->cls, t , t->fds+i) && success;
1040 return driver_func(scheduler_driver->cls, t , t->fds);
1046 * Cancel the task with the specified identifier.
1047 * The task must not yet have run.
1049 * @param task id of the task to cancel
1050 * @return original closure of the task
1053 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
1055 enum GNUNET_SCHEDULER_Priority p;
1058 GNUNET_assert ( (NULL != active_task) ||
1059 (GNUNET_NO == task->lifeness) );
1060 if (! task->in_ready_list)
1062 if ( (-1 == task->read_fd) &&
1063 (-1 == task->write_fd) &&
1064 (NULL == task->read_set) &&
1065 (NULL == task->write_set) )
1067 if (GNUNET_YES == task->on_shutdown)
1068 GNUNET_CONTAINER_DLL_remove (shutdown_head,
1072 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1073 pending_timeout_tail,
1075 scheduler_driver->set_wakeup(scheduler_driver->cls,pending_timeout_head->timeout);
1076 if (task == pending_timeout_last)
1077 pending_timeout_last = NULL;
1081 GNUNET_CONTAINER_DLL_remove (pending_head,
1084 scheduler_multi_function_call(task, scheduler_driver->del);
1089 p = check_priority (task->priority);
1090 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1095 ret = task->callback_cls;
1096 LOG (GNUNET_ERROR_TYPE_DEBUG,
1097 "Canceling task %p\n",
1099 destroy_task (task);
1105 * Initialize backtrace data for task @a t
1107 * @param t task to initialize
1110 init_backtrace (struct GNUNET_SCHEDULER_Task *t)
1113 void *backtrace_array[MAX_TRACE_DEPTH];
1115 t->num_backtrace_strings
1116 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1117 t->backtrace_strings =
1118 backtrace_symbols (backtrace_array,
1119 t->num_backtrace_strings);
1126 * Continue the current execution with the given function. This is
1127 * similar to the other "add" functions except that there is no delay
1128 * and the reason code can be specified.
1130 * @param task main function of the task
1131 * @param task_cls closure for @a task
1132 * @param reason reason for task invocation
1133 * @param priority priority to use for the task
1136 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
1138 enum GNUNET_SCHEDULER_Reason reason,
1139 enum GNUNET_SCHEDULER_Priority priority)
1141 struct GNUNET_SCHEDULER_Task *t;
1143 GNUNET_assert (NULL != task);
1144 GNUNET_assert ((NULL != active_task) ||
1145 (GNUNET_SCHEDULER_REASON_STARTUP == reason));
1146 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1150 t->callback_cls = task_cls;
1152 t->start_time = GNUNET_TIME_absolute_get ();
1155 t->priority = priority;
1156 t->lifeness = current_lifeness;
1157 LOG (GNUNET_ERROR_TYPE_DEBUG,
1158 "Adding continuation task %p\n",
1161 queue_ready_task (t);
1166 * Schedule a new task to be run at the specified time. The task
1167 * will be scheduled for execution at time @a at.
1169 * @param at time when the operation should run
1170 * @param priority priority to use for the task
1171 * @param task main function of the task
1172 * @param task_cls closure of @a task
1173 * @return unique task identifier for the job
1174 * only valid until @a task is started!
1176 struct GNUNET_SCHEDULER_Task *
1177 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1178 enum GNUNET_SCHEDULER_Priority priority,
1179 GNUNET_SCHEDULER_TaskCallback task,
1182 struct GNUNET_SCHEDULER_Task *t;
1183 struct GNUNET_SCHEDULER_Task *pos;
1184 struct GNUNET_SCHEDULER_Task *prev;
1186 GNUNET_assert (NULL != active_task);
1187 GNUNET_assert (NULL != task);
1188 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1190 t->callback_cls = task_cls;
1194 t->start_time = GNUNET_TIME_absolute_get ();
1197 t->priority = priority;
1198 t->lifeness = current_lifeness;
1199 /* try tail first (optimization in case we are
1200 * appending to a long list of tasks with timeouts) */
1201 if ( (NULL == pending_timeout_head) ||
1202 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us) )
1204 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1205 pending_timeout_tail,
1207 scheduler_driver->set_wakeup(scheduler_driver->cls,pending_timeout_head->timeout);
1211 /* first move from heuristic start backwards to before start time */
1212 prev = pending_timeout_last;
1213 while ( (NULL != prev) &&
1214 (prev->timeout.abs_value_us > t->timeout.abs_value_us) )
1216 /* now, move from heuristic start (or head of list) forward to insertion point */
1218 pos = pending_timeout_head;
1221 while ( (NULL != pos) &&
1222 ( (pos->timeout.abs_value_us <= t->timeout.abs_value_us) ||
1223 (0 != pos->reason) ) )
1228 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1229 pending_timeout_tail,
1233 /* finally, update heuristic insertion point to last insertion... */
1234 pending_timeout_last = t;
1236 LOG (GNUNET_ERROR_TYPE_DEBUG,
1237 "Adding task: %p\n",
1245 * Schedule a new task to be run with a specified delay. The task
1246 * will be scheduled for execution once the delay has expired.
1248 * @param delay when should this operation time out?
1249 * @param priority priority to use for the task
1250 * @param task main function of the task
1251 * @param task_cls closure of @a task
1252 * @return unique task identifier for the job
1253 * only valid until @a task is started!
1255 struct GNUNET_SCHEDULER_Task *
1256 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1257 enum GNUNET_SCHEDULER_Priority priority,
1258 GNUNET_SCHEDULER_TaskCallback task,
1261 return GNUNET_SCHEDULER_add_at_with_priority (GNUNET_TIME_relative_to_absolute (delay),
1269 * Schedule a new task to be run with a specified priority.
1271 * @param prio how important is the new task?
1272 * @param task main function of the task
1273 * @param task_cls closure of @a task
1274 * @return unique task identifier for the job
1275 * only valid until @a task is started!
1277 struct GNUNET_SCHEDULER_Task *
1278 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1279 GNUNET_SCHEDULER_TaskCallback task,
1282 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1290 * Schedule a new task to be run at the specified time. The task
1291 * will be scheduled for execution once specified time has been
1292 * reached. It will be run with the DEFAULT priority.
1294 * @param at time at which this operation should run
1295 * @param task main function of the task
1296 * @param task_cls closure of @a task
1297 * @return unique task identifier for the job
1298 * only valid until @a task is started!
1300 struct GNUNET_SCHEDULER_Task *
1301 GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1302 GNUNET_SCHEDULER_TaskCallback task,
1305 return GNUNET_SCHEDULER_add_at_with_priority (at,
1306 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1313 * Schedule a new task to be run with a specified delay. The task
1314 * will be scheduled for execution once the delay has expired. It
1315 * will be run with the DEFAULT priority.
1317 * @param delay when should this operation time out?
1318 * @param task main function of the task
1319 * @param task_cls closure of @a task
1320 * @return unique task identifier for the job
1321 * only valid until @a task is started!
1323 struct GNUNET_SCHEDULER_Task *
1324 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1325 GNUNET_SCHEDULER_TaskCallback task,
1328 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1329 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1336 * Schedule a new task to be run as soon as possible. Note that this
1337 * does not guarantee that this will be the next task that is being
1338 * run, as other tasks with higher priority (or that are already ready
1339 * to run) might get to run first. Just as with delays, clients must
1340 * not rely on any particular order of execution between tasks
1341 * scheduled concurrently.
1343 * The task will be run with the DEFAULT priority.
1345 * @param task main function of the task
1346 * @param task_cls closure of @a task
1347 * @return unique task identifier for the job
1348 * only valid until @a task is started!
1350 struct GNUNET_SCHEDULER_Task *
1351 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
1354 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO,
1361 * Schedule a new task to be run on shutdown, that is when a CTRL-C
1362 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
1365 * @param task main function of the task
1366 * @param task_cls closure of @a task
1367 * @return unique task identifier for the job
1368 * only valid until @a task is started!
1370 struct GNUNET_SCHEDULER_Task *
1371 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1374 struct GNUNET_SCHEDULER_Task *t;
1376 GNUNET_assert (NULL != active_task);
1377 GNUNET_assert (NULL != task);
1378 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1380 t->callback_cls = task_cls;
1384 t->start_time = GNUNET_TIME_absolute_get ();
1386 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1387 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1388 t->on_shutdown = GNUNET_YES;
1389 t->lifeness = GNUNET_YES;
1390 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1393 LOG (GNUNET_ERROR_TYPE_DEBUG,
1394 "Adding task: %p\n",
1402 * Schedule a new task to be run as soon as possible with the
1403 * (transitive) ignore-shutdown flag either explicitly set or
1404 * explicitly enabled. This task (and all tasks created from it,
1405 * other than by another call to this function) will either count or
1406 * not count for the "lifeness" of the process. This API is only
1407 * useful in a few special cases.
1409 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
1410 * @param task main function of the task
1411 * @param task_cls closure of @a task
1412 * @return unique task identifier for the job
1413 * only valid until @a task is started!
1415 struct GNUNET_SCHEDULER_Task *
1416 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1417 GNUNET_SCHEDULER_TaskCallback task,
1420 struct GNUNET_SCHEDULER_Task *ret;
1422 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1423 ret->lifeness = lifeness;
1429 * Schedule a new task to be run with a specified delay or when any of
1430 * the specified file descriptor sets is ready. The delay can be used
1431 * as a timeout on the socket(s) being ready. The task will be
1432 * scheduled for execution once either the delay has expired or any of
1433 * the socket operations is ready. This is the most general
1434 * function of the "add" family. Note that the "prerequisite_task"
1435 * must be satisfied in addition to any of the other conditions. In
1436 * other words, the task will be started when
1438 * (prerequisite-run)
1444 * @param delay how long should we wait?
1445 * @param priority priority to use
1446 * @param rfd file descriptor we want to read (can be -1)
1447 * @param wfd file descriptors we want to write (can be -1)
1448 * @param task main function of the task
1449 * @param task_cls closure of @a task
1450 * @return unique task identifier for the job
1451 * only valid until @a task is started!
1454 static struct GNUNET_SCHEDULER_Task *
1455 add_without_sets (struct GNUNET_TIME_Relative delay,
1456 enum GNUNET_SCHEDULER_Priority priority,
1457 const struct GNUNET_NETWORK_Handle *read_nh,
1458 const struct GNUNET_NETWORK_Handle *write_nh,
1459 const struct GNUNET_DISK_FileHandle *read_fh,
1460 const struct GNUNET_DISK_FileHandle *write_fh,
1463 GNUNET_SCHEDULER_TaskCallback task,
1466 struct GNUNET_SCHEDULER_Task *t;
1468 GNUNET_assert (NULL != active_task);
1469 GNUNET_assert (NULL != task);
1470 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1471 initFdInfo (t, read_nh, write_nh, read_fh, write_fh);
1473 t->callback_cls = task_cls;
1477 int flags = fcntl (rfd, F_GETFD);
1479 if ((flags == -1) && (errno == EBADF))
1481 LOG (GNUNET_ERROR_TYPE_ERROR,
1482 "Got invalid file descriptor %d!\n",
1490 int flags = fcntl (wfd, F_GETFD);
1492 if (flags == -1 && errno == EBADF)
1494 LOG (GNUNET_ERROR_TYPE_ERROR,
1495 "Got invalid file descriptor %d!\n",
1504 t->start_time = GNUNET_TIME_absolute_get ();
1506 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1507 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1508 t->lifeness = current_lifeness;
1509 GNUNET_CONTAINER_DLL_insert (pending_head,
1512 scheduler_multi_function_call(t, scheduler_driver->add);
1513 max_priority_added = GNUNET_MAX (max_priority_added,
1515 LOG (GNUNET_ERROR_TYPE_DEBUG,
1525 * Schedule a new task to be run with a specified delay or when the
1526 * specified file descriptor is ready for reading. The delay can be
1527 * used as a timeout on the socket being ready. The task will be
1528 * scheduled for execution once either the delay has expired or the
1529 * socket operation is ready. It will be run with the DEFAULT priority.
1531 * @param delay when should this operation time out?
1532 * @param rfd read file-descriptor
1533 * @param task main function of the task
1534 * @param task_cls closure of @a task
1535 * @return unique task identifier for the job
1536 * only valid until @a task is started!
1538 struct GNUNET_SCHEDULER_Task *
1539 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1540 struct GNUNET_NETWORK_Handle *rfd,
1541 GNUNET_SCHEDULER_TaskCallback task,
1544 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1545 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1546 rfd, task, task_cls);
1551 * Schedule a new task to be run with a specified priority and to be
1552 * run after the specified delay or when the specified file descriptor
1553 * is ready for reading. The delay can be used as a timeout on the
1554 * socket being ready. The task will be scheduled for execution once
1555 * either the delay has expired or the socket operation is ready. It
1556 * will be run with the DEFAULT priority.
1558 * @param delay when should this operation time out?
1559 * @param priority priority to use for the task
1560 * @param rfd read file-descriptor
1561 * @param task main function of the task
1562 * @param task_cls closure of @a task
1563 * @return unique task identifier for the job
1564 * only valid until @a task is started!
1566 struct GNUNET_SCHEDULER_Task *
1567 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1568 enum GNUNET_SCHEDULER_Priority priority,
1569 struct GNUNET_NETWORK_Handle *rfd,
1570 GNUNET_SCHEDULER_TaskCallback task,
1573 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1582 * Schedule a new task to be run with a specified delay or when the
1583 * specified file descriptor is ready for writing. The delay can be
1584 * used as a timeout on the socket being ready. The task will be
1585 * scheduled for execution once either the delay has expired or the
1586 * socket operation is ready. It will be run with the priority of
1589 * @param delay when should this operation time out?
1590 * @param wfd write file-descriptor
1591 * @param task main function of the task
1592 * @param task_cls closure of @a task
1593 * @return unique task identifier for the job
1594 * only valid until @a task is started!
1596 struct GNUNET_SCHEDULER_Task *
1597 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1598 struct GNUNET_NETWORK_Handle *wfd,
1599 GNUNET_SCHEDULER_TaskCallback task,
1602 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1603 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1605 GNUNET_NO, GNUNET_YES,
1610 * Schedule a new task to be run with a specified delay or when the
1611 * specified file descriptor is ready. The delay can be
1612 * used as a timeout on the socket being ready. The task will be
1613 * scheduled for execution once either the delay has expired or the
1614 * socket operation is ready.
1616 * @param delay when should this operation time out?
1617 * @param priority priority of the task
1618 * @param fd file-descriptor
1619 * @param on_read whether to poll the file-descriptor for readability
1620 * @param on_write whether to poll the file-descriptor for writability
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 struct GNUNET_SCHEDULER_Task *
1627 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1628 enum GNUNET_SCHEDULER_Priority priority,
1629 struct GNUNET_NETWORK_Handle *fd,
1632 GNUNET_SCHEDULER_TaskCallback task,
1636 struct GNUNET_NETWORK_FDSet *s;
1637 struct GNUNET_SCHEDULER_Task * ret;
1639 GNUNET_assert (NULL != fd);
1640 s = GNUNET_NETWORK_fdset_create ();
1641 GNUNET_NETWORK_fdset_set (s, fd);
1642 ret = GNUNET_SCHEDULER_add_select (
1645 on_write ? s : NULL,
1647 GNUNET_NETWORK_fdset_destroy (s);
1650 GNUNET_assert (on_read || on_write);
1651 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1652 return add_without_sets (delay, priority,
1653 on_read ? fd : NULL,
1654 on_write ? fd : NULL,
1663 * Schedule a new task to be run with a specified delay or when the
1664 * specified file descriptor is ready for reading. The delay can be
1665 * used as a timeout on the socket being ready. The task will be
1666 * scheduled for execution once either the delay has expired or the
1667 * socket operation is ready. It will be run with the DEFAULT priority.
1669 * @param delay when should this operation time out?
1670 * @param rfd read file-descriptor
1671 * @param task main function of the task
1672 * @param task_cls closure of @a task
1673 * @return unique task identifier for the job
1674 * only valid until @a task is started!
1676 struct GNUNET_SCHEDULER_Task *
1677 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1678 const struct GNUNET_DISK_FileHandle *rfd,
1679 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1681 return GNUNET_SCHEDULER_add_file_with_priority (
1682 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1683 rfd, GNUNET_YES, GNUNET_NO,
1689 * Schedule a new task to be run with a specified delay or when the
1690 * specified file descriptor is ready for writing. The delay can be
1691 * used as a timeout on the socket being ready. The task will be
1692 * scheduled for execution once either the delay has expired or the
1693 * socket operation is ready. It will be run with the DEFAULT priority.
1695 * @param delay when should this operation time out?
1696 * @param wfd write file-descriptor
1697 * @param task main function of the task
1698 * @param task_cls closure of @a task
1699 * @return unique task identifier for the job
1700 * only valid until @a task is started!
1702 struct GNUNET_SCHEDULER_Task *
1703 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1704 const struct GNUNET_DISK_FileHandle *wfd,
1705 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1707 return GNUNET_SCHEDULER_add_file_with_priority (
1708 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1709 wfd, GNUNET_NO, GNUNET_YES,
1715 * Schedule a new task to be run with a specified delay or when the
1716 * specified file descriptor is ready. The delay can be
1717 * used as a timeout on the socket being ready. The task will be
1718 * scheduled for execution once either the delay has expired or the
1719 * socket operation is ready.
1721 * @param delay when should this operation time out?
1722 * @param priority priority of the task
1723 * @param fd file-descriptor
1724 * @param on_read whether to poll the file-descriptor for readability
1725 * @param on_write whether to poll the file-descriptor for writability
1726 * @param task main function of the task
1727 * @param task_cls closure of @a task
1728 * @return unique task identifier for the job
1729 * only valid until @a task is started!
1731 struct GNUNET_SCHEDULER_Task *
1732 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1733 enum GNUNET_SCHEDULER_Priority priority,
1734 const struct GNUNET_DISK_FileHandle *fd,
1735 int on_read, int on_write,
1736 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1739 struct GNUNET_NETWORK_FDSet *s;
1740 struct GNUNET_SCHEDULER_Task * ret;
1742 GNUNET_assert (NULL != fd);
1743 s = GNUNET_NETWORK_fdset_create ();
1744 GNUNET_NETWORK_fdset_handle_set (s, fd);
1745 ret = GNUNET_SCHEDULER_add_select (
1748 on_write ? s : NULL,
1750 GNUNET_NETWORK_fdset_destroy (s);
1753 GNUNET_assert (on_read || on_write);
1754 GNUNET_assert(fd->fd >= 0);
1755 return add_without_sets (delay, priority,
1758 on_read ? fd : NULL,
1759 on_write ? fd : NULL,
1766 * Schedule a new task to be run with a specified delay or when any of
1767 * the specified file descriptor sets is ready. The delay can be used
1768 * as a timeout on the socket(s) being ready. The task will be
1769 * scheduled for execution once either the delay has expired or any of
1770 * the socket operations is ready. This is the most general
1771 * function of the "add" family. Note that the "prerequisite_task"
1772 * must be satisfied in addition to any of the other conditions. In
1773 * other words, the task will be started when
1775 * (prerequisite-run)
1778 * || any-ws-ready) )
1781 * @param prio how important is this task?
1782 * @param delay how long should we wait?
1783 * @param rs set of file descriptors we want to read (can be NULL)
1784 * @param ws set of file descriptors we want to write (can be NULL)
1785 * @param task main function of the task
1786 * @param task_cls closure of @a task
1787 * @return unique task identifier for the job
1788 * only valid until @a task is started!
1790 struct GNUNET_SCHEDULER_Task *
1791 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1792 struct GNUNET_TIME_Relative delay,
1793 const struct GNUNET_NETWORK_FDSet *rs,
1794 const struct GNUNET_NETWORK_FDSet *ws,
1795 GNUNET_SCHEDULER_TaskCallback task,
1798 struct GNUNET_SCHEDULER_Task *t;
1800 if ( (NULL == rs) &&
1802 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1806 GNUNET_assert (NULL != active_task);
1807 GNUNET_assert (NULL != task);
1808 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1810 t->callback_cls = task_cls;
1815 t->read_set = GNUNET_NETWORK_fdset_create ();
1816 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1820 t->write_set = GNUNET_NETWORK_fdset_create ();
1821 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1824 t->start_time = GNUNET_TIME_absolute_get ();
1826 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1828 check_priority ((prio ==
1829 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1831 t->lifeness = current_lifeness;
1832 GNUNET_CONTAINER_DLL_insert (pending_head,
1835 scheduler_multi_function_call(t, scheduler_driver->add);
1836 max_priority_added = GNUNET_MAX (max_priority_added,
1838 LOG (GNUNET_ERROR_TYPE_DEBUG,
1847 * Function used by event-loop implementations to signal the scheduler
1848 * that a particular @a task is ready due to an event of type @a et.
1850 * This function will then queue the task to notify the application
1851 * that the task is ready (with the respective priority).
1853 * @param task the task that is ready, NULL for wake up calls
1854 * @param et information about why the task is ready
1857 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1858 enum GNUNET_SCHEDULER_EventType et)
1860 enum GNUNET_SCHEDULER_Reason reason;
1861 struct GNUNET_TIME_Absolute now;
1863 now = GNUNET_TIME_absolute_get ();
1864 reason = task->reason;
1865 if (now.abs_value_us >= task->timeout.abs_value_us)
1866 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1867 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1868 (0 != (GNUNET_SCHEDULER_ET_IN & et)) )
1869 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
1870 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
1871 (0 != (GNUNET_SCHEDULER_ET_OUT & et)) )
1872 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
1873 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1874 task->reason = reason;
1875 task->fds = &task->fdx;
1878 queue_ready_task (task);
1883 * Function called by the driver to tell the scheduler to run some of
1884 * the tasks that are ready. This function may return even though
1885 * there are tasks left to run just to give other tasks a chance as
1886 * well. If we return #GNUNET_YES, the driver should call this
1887 * function again as soon as possible, while if we return #GNUNET_NO
1888 * it must block until the operating system has more work as the
1889 * scheduler has no more work to do right now.
1891 * @param sh scheduler handle that was given to the `loop`
1892 * @return #GNUNET_OK if there are more tasks that are ready,
1893 * and thus we would like to run more (yield to avoid
1894 * blocking other activities for too long)
1895 * #GNUNET_NO if we are done running tasks (yield to block)
1896 * #GNUNET_SYSERR on error
1899 GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh)
1901 enum GNUNET_SCHEDULER_Priority p;
1902 struct GNUNET_SCHEDULER_Task *pos;
1903 struct GNUNET_TIME_Absolute now;
1905 /* check for tasks that reached the timeout! */
1906 now = GNUNET_TIME_absolute_get ();
1907 while (NULL != (pos = pending_timeout_head))
1909 if (now.abs_value_us >= pos->timeout.abs_value_us)
1910 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1911 if (0 == pos->reason)
1913 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1914 pending_timeout_tail,
1916 scheduler_driver->set_wakeup(scheduler_driver->cls,pending_timeout_head->timeout);
1917 if (pending_timeout_last == pos)
1918 pending_timeout_last = NULL;
1919 queue_ready_task (pos);
1922 if (0 == ready_count)
1925 /* find out which task priority level we are going to
1926 process this time */
1927 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
1928 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
1929 /* yes, p>0 is correct, 0 is "KEEP" which should
1930 * always be an empty queue (see assertion)! */
1931 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
1933 pos = ready_head[p];
1937 GNUNET_assert (NULL != pos); /* ready_count wrong? */
1939 /* process all tasks at this priority level, then yield */
1940 while (NULL != (pos = ready_head[p]))
1942 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1946 current_priority = pos->priority;
1947 current_lifeness = pos->lifeness;
1950 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
1951 DELAY_THRESHOLD.rel_value_us)
1953 LOG (GNUNET_ERROR_TYPE_DEBUG,
1954 "Task %p took %s to be scheduled\n",
1956 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
1960 tc.reason = pos->reason;
1961 GNUNET_NETWORK_fdset_zero (sh->rs);
1962 GNUNET_NETWORK_fdset_zero (sh->ws);
1963 tc.fds_len = pos->fds_len;
1965 tc.read_ready = (NULL == pos->read_set) ? sh->rs : pos->read_set;
1966 if ( (-1 != pos->read_fd) &&
1967 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
1968 GNUNET_NETWORK_fdset_set_native (sh->rs,
1970 tc.write_ready = (NULL == pos->write_set) ? sh->ws : pos->write_set;
1971 if ((-1 != pos->write_fd) &&
1972 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
1973 GNUNET_NETWORK_fdset_set_native (sh->ws,
1975 LOG (GNUNET_ERROR_TYPE_DEBUG,
1976 "Running task: %p\n",
1978 pos->callback (pos->callback_cls);
1980 dump_backtrace (pos);
1984 if (0 == ready_count)
1991 * Initialize and run scheduler. This function will return when all
1992 * tasks have completed. On systems with signals, receiving a SIGTERM
1993 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
1994 * to be run after the active task is complete. As a result, SIGTERM
1995 * causes all shutdown tasks to be scheduled with reason
1996 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
1997 * afterwards will execute normally!). Note that any particular
1998 * signal will only shut down one scheduler; applications should
1999 * always only create a single scheduler.
2001 * @param driver drive to use for the event loop
2002 * @param task task to run first (and immediately)
2003 * @param task_cls closure of @a task
2004 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2007 GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
2008 GNUNET_SCHEDULER_TaskCallback task,
2012 struct GNUNET_SCHEDULER_Handle sh;
2013 struct GNUNET_SIGNAL_Context *shc_int;
2014 struct GNUNET_SIGNAL_Context *shc_term;
2015 #if (SIGTERM != GNUNET_TERM_SIG)
2016 struct GNUNET_SIGNAL_Context *shc_gterm;
2019 struct GNUNET_SIGNAL_Context *shc_quit;
2020 struct GNUNET_SIGNAL_Context *shc_hup;
2021 struct GNUNET_SIGNAL_Context *shc_pipe;
2023 struct GNUNET_SCHEDULER_Task tsk;
2024 const struct GNUNET_DISK_FileHandle *pr;
2025 scheduler_driver = driver;
2027 /* general set-up */
2028 GNUNET_assert (NULL == active_task);
2029 GNUNET_assert (NULL == shutdown_pipe_handle);
2030 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
2034 GNUNET_assert (NULL != shutdown_pipe_handle);
2035 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
2036 GNUNET_DISK_PIPE_END_READ);
2037 GNUNET_assert (NULL != pr);
2040 /* install signal handlers */
2041 LOG (GNUNET_ERROR_TYPE_DEBUG,
2042 "Registering signal handlers\n");
2043 shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2044 &sighandler_shutdown);
2045 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2046 &sighandler_shutdown);
2047 #if (SIGTERM != GNUNET_TERM_SIG)
2048 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2049 &sighandler_shutdown);
2052 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2054 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2055 &sighandler_shutdown);
2056 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2057 &sighandler_shutdown);
2060 /* Setup initial tasks */
2061 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2062 current_lifeness = GNUNET_YES;
2068 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
2070 GNUNET_SCHEDULER_REASON_STARTUP,
2071 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
2072 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
2073 &GNUNET_OS_install_parent_control_handler,
2076 driver->set_wakeup (driver->cls,
2077 GNUNET_TIME_absolute_get ());
2079 /* begin main event loop */
2080 sh.rs = GNUNET_NETWORK_fdset_create ();
2081 sh.ws = GNUNET_NETWORK_fdset_create ();
2082 GNUNET_NETWORK_fdset_handle_set (sh.rs, pr);
2084 ret = driver->loop (driver->cls,
2086 GNUNET_NETWORK_fdset_destroy (sh.rs);
2087 GNUNET_NETWORK_fdset_destroy (sh.ws);
2089 /* uninstall signal handlers */
2090 GNUNET_SIGNAL_handler_uninstall (shc_int);
2091 GNUNET_SIGNAL_handler_uninstall (shc_term);
2092 #if (SIGTERM != GNUNET_TERM_SIG)
2093 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
2096 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
2097 GNUNET_SIGNAL_handler_uninstall (shc_quit);
2098 GNUNET_SIGNAL_handler_uninstall (shc_hup);
2100 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2101 shutdown_pipe_handle = NULL;
2106 select_add(void *cls,
2107 struct GNUNET_SCHEDULER_Task *task,
2108 struct GNUNET_SCHEDULER_FdInfo *fdi)
2115 select_del(void *cls,
2116 struct GNUNET_SCHEDULER_Task *task,
2117 struct GNUNET_SCHEDULER_FdInfo *fdi)
2124 select_loop(void *cls,
2125 struct GNUNET_SCHEDULER_Handle *sh)
2127 return while_live(sh->rs, sh->ws);
2132 select_set_wakeup(void *cls,
2133 struct GNUNET_TIME_Absolute dt)
2140 * Obtain the driver for using select() as the event loop.
2142 * @return NULL on error
2144 const struct GNUNET_SCHEDULER_Driver *
2145 GNUNET_SCHEDULER_driver_select ()
2148 struct GNUNET_SCHEDULER_Driver *select_driver;
2150 select_driver = GNUNET_new (struct GNUNET_SCHEDULER_Driver);
2152 select_driver->loop = &select_loop;
2153 select_driver->add = &select_add;
2154 select_driver->del = &select_del;
2155 select_driver->set_wakeup = &select_set_wakeup;
2158 return select_driver;
2162 /* end of scheduler.c */