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 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 if (pending_timeout_last == pos)
543 pending_timeout_last = NULL;
544 queue_ready_task (pos);
550 if (GNUNET_YES == is_ready (pos, now, rs, ws))
552 GNUNET_CONTAINER_DLL_remove (pending_head,
555 queue_ready_task (pos);
563 * Request the shutdown of a scheduler. Marks all tasks
564 * awaiting shutdown as ready. Note that tasks
565 * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call
566 * will be delayed until the next shutdown signal.
569 GNUNET_SCHEDULER_shutdown ()
571 struct GNUNET_SCHEDULER_Task *pos;
573 while (NULL != (pos = shutdown_head))
575 GNUNET_CONTAINER_DLL_remove (shutdown_head,
578 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
579 queue_ready_task (pos);
585 * Destroy a task (release associated resources)
587 * @param t task to destroy
590 destroy_task (struct GNUNET_SCHEDULER_Task *t)
592 if (NULL != t->read_set)
593 GNUNET_NETWORK_fdset_destroy (t->read_set);
594 if (NULL != t->write_set)
595 GNUNET_NETWORK_fdset_destroy (t->write_set);
597 GNUNET_free (t->backtrace_strings);
604 * Output stack trace of task @a t.
606 * @param t task to dump stack trace of
609 dump_backtrace (struct GNUNET_SCHEDULER_Task *t)
614 for (i = 0; i < t->num_backtrace_strings; i++)
615 LOG (GNUNET_ERROR_TYPE_DEBUG,
616 "Task %p trace %u: %s\n",
619 t->backtrace_strings[i]);
625 * Run at least one task in the highest-priority queue that is not
626 * empty. Keep running tasks until we are either no longer running
627 * "URGENT" tasks or until we have at least one "pending" task (which
628 * may become ready, hence we should select on it). Naturally, if
629 * there are no more ready tasks, we also return.
631 * @param rs FDs ready for reading
632 * @param ws FDs ready for writing
635 run_ready (struct GNUNET_NETWORK_FDSet *rs,
636 struct GNUNET_NETWORK_FDSet *ws)
638 enum GNUNET_SCHEDULER_Priority p;
639 struct GNUNET_SCHEDULER_Task *pos;
641 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
644 if (0 == ready_count)
646 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
647 /* yes, p>0 is correct, 0 is "KEEP" which should
648 * always be an empty queue (see assertion)! */
649 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
655 GNUNET_assert (NULL != pos); /* ready_count wrong? */
656 GNUNET_CONTAINER_DLL_remove (ready_head[p],
660 current_priority = pos->priority;
661 current_lifeness = pos->lifeness;
664 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
665 DELAY_THRESHOLD.rel_value_us)
667 LOG (GNUNET_ERROR_TYPE_DEBUG,
668 "Task %p took %s to be scheduled\n",
670 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
674 tc.reason = pos->reason;
675 tc.read_ready = (NULL == pos->read_set) ? rs : pos->read_set;
676 if ((-1 != pos->read_fd) &&
677 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)))
678 GNUNET_NETWORK_fdset_set_native (rs, pos->read_fd);
679 tc.write_ready = (NULL == pos->write_set) ? ws : pos->write_set;
680 if ((-1 != pos->write_fd) &&
681 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
682 GNUNET_NETWORK_fdset_set_native (ws, pos->write_fd);
683 if ((0 != (tc.reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
684 (-1 != pos->write_fd) &&
685 (!GNUNET_NETWORK_fdset_test_native (ws, pos->write_fd)))
686 GNUNET_assert (0); // added to ready in previous select loop!
687 LOG (GNUNET_ERROR_TYPE_DEBUG,
688 "Running task: %p\n",
690 pos->callback (pos->callback_cls);
691 dump_backtrace (pos);
696 while ((NULL == pending_head) || (p >= max_priority_added));
701 * Pipe used to communicate shutdown via signal.
703 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
706 * Process ID of this process at the time we installed the various
712 * Signal handler called for SIGPIPE.
724 * Wait for a short time.
725 * Sleeps for @a ms ms (as that should be long enough for virtually all
726 * modern systems to context switch and allow another process to do
729 * @param ms how many ms to wait
732 short_wait (unsigned int ms)
734 struct GNUNET_TIME_Relative timeout;
736 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
737 (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
742 * Signal handler called for signals that should cause us to shutdown.
745 sighandler_shutdown ()
748 int old_errno = errno; /* backup errno */
750 if (getpid () != my_pid)
751 exit (1); /* we have fork'ed since the signal handler was created,
752 * ignore the signal, see https://gnunet.org/vfork discussion */
753 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
754 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
761 * Check if the system is still alive. Trigger shutdown if we
762 * have tasks, but none of them give us lifeness.
764 * @return #GNUNET_OK to continue the main loop,
770 struct GNUNET_SCHEDULER_Task *t;
774 for (t = pending_head; NULL != t; t = t->next)
775 if (t->lifeness == GNUNET_YES)
777 for (t = shutdown_head; NULL != t; t = t->next)
778 if (t->lifeness == GNUNET_YES)
780 for (t = pending_timeout_head; NULL != t; t = t->next)
781 if (t->lifeness == GNUNET_YES)
783 if (NULL != shutdown_head)
785 GNUNET_SCHEDULER_shutdown ();
793 void while_live(struct GNUNET_NETWORK_FDSet *rs, struct GNUNET_NETWORK_FDSet *ws)
796 unsigned int busy_wait_warning;
797 unsigned long long last_tr;
798 const struct GNUNET_DISK_FileHandle *pr;
800 struct GNUNET_TIME_Relative timeout;
802 busy_wait_warning = 0;
803 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
804 GNUNET_DISK_PIPE_END_READ);
805 GNUNET_assert (NULL != pr);
808 while (GNUNET_OK == check_lifeness ())
810 GNUNET_NETWORK_fdset_zero (rs);
811 GNUNET_NETWORK_fdset_zero (ws);
812 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
813 update_sets (rs, ws, &timeout);
814 GNUNET_NETWORK_fdset_handle_set (rs, pr);
817 /* no blocking, more work already ready! */
818 timeout = GNUNET_TIME_UNIT_ZERO;
820 if (NULL == scheduler_select)
821 ret = GNUNET_NETWORK_socket_select (rs,
826 ret = scheduler_select (scheduler_select_cls,
831 if (ret == GNUNET_SYSERR)
836 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "select");
841 snprintf (lsof, sizeof (lsof), "lsof -p %d", getpid ());
844 if (0 != system (lsof))
845 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
850 struct GNUNET_SCHEDULER_Task *t;
852 for (t = pending_head; NULL != t; t = t->next)
854 if (-1 != t->read_fd)
856 int flags = fcntl (t->read_fd, F_GETFD);
857 if ((flags == -1) && (errno == EBADF))
859 LOG (GNUNET_ERROR_TYPE_ERROR,
860 "Got invalid file descriptor %d!\n",
865 if (-1 != t->write_fd)
867 int flags = fcntl (t->write_fd, F_GETFD);
868 if ((flags == -1) && (errno == EBADF))
870 LOG (GNUNET_ERROR_TYPE_ERROR,
871 "Got invalid file descriptor %d!\n",
883 (0 == timeout.rel_value_us) &&
884 (busy_wait_warning > 16) )
886 LOG (GNUNET_ERROR_TYPE_WARNING,
887 "Looks like we're busy waiting...\n");
888 short_wait (100); /* mitigate */
890 check_ready (rs, ws);
892 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
894 /* consume the signal */
895 GNUNET_DISK_file_read (pr, &c, sizeof (c));
896 /* mark all active tasks as ready due to shutdown */
897 GNUNET_SCHEDULER_shutdown ();
899 if (last_tr == tasks_run)
907 busy_wait_warning = 0;
913 * Initialize and run scheduler. This function will return when all
914 * tasks have completed. On systems with signals, receiving a SIGTERM
915 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown()
916 * to be run after the active task is complete. As a result, SIGTERM
917 * causes all active tasks to be scheduled with reason
918 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
919 * afterwards will execute normally!). Note that any particular signal
920 * will only shut down one scheduler; applications should always only
921 * create a single scheduler.
923 * @param task task to run immediately
924 * @param task_cls closure of @a task
927 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
930 struct GNUNET_NETWORK_FDSet *rs;
931 struct GNUNET_NETWORK_FDSet *ws;
934 struct GNUNET_SIGNAL_Context *shc_int;
935 struct GNUNET_SIGNAL_Context *shc_term;
936 #if (SIGTERM != GNUNET_TERM_SIG)
937 struct GNUNET_SIGNAL_Context *shc_gterm;
941 struct GNUNET_SIGNAL_Context *shc_quit;
942 struct GNUNET_SIGNAL_Context *shc_hup;
943 struct GNUNET_SIGNAL_Context *shc_pipe;
946 unsigned int busy_wait_warning;
950 GNUNET_assert (NULL == active_task);
951 rs = GNUNET_NETWORK_fdset_create ();
952 ws = GNUNET_NETWORK_fdset_create ();
953 GNUNET_assert (NULL == shutdown_pipe_handle);
954 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
958 GNUNET_assert (NULL != shutdown_pipe_handle);
961 LOG (GNUNET_ERROR_TYPE_DEBUG,
962 "Registering signal handlers\n");
963 shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
964 &sighandler_shutdown);
965 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
966 &sighandler_shutdown);
967 #if (SIGTERM != GNUNET_TERM_SIG)
968 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
969 &sighandler_shutdown);
972 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
974 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
975 &sighandler_shutdown);
976 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
977 &sighandler_shutdown);
979 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
980 current_lifeness = GNUNET_YES;
981 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
983 GNUNET_SCHEDULER_REASON_STARTUP,
984 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
985 active_task = (void *) (long) -1; /* force passing of sanity check */
986 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
987 &GNUNET_OS_install_parent_control_handler,
993 GNUNET_SIGNAL_handler_uninstall (shc_int);
994 GNUNET_SIGNAL_handler_uninstall (shc_term);
995 #if (SIGTERM != GNUNET_TERM_SIG)
996 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
999 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
1000 GNUNET_SIGNAL_handler_uninstall (shc_quit);
1001 GNUNET_SIGNAL_handler_uninstall (shc_hup);
1003 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
1004 shutdown_pipe_handle = NULL;
1005 GNUNET_NETWORK_fdset_destroy (rs);
1006 GNUNET_NETWORK_fdset_destroy (ws);
1011 * Obtain the task context, giving the reason why the current task was
1014 * @return current tasks' scheduler context
1016 const struct GNUNET_SCHEDULER_TaskContext *
1017 GNUNET_SCHEDULER_get_task_context ()
1019 GNUNET_assert (NULL != active_task);
1025 * Get information about the current load of this scheduler. Use this
1026 * function to determine if an elective task should be added or simply
1027 * dropped (if the decision should be made based on the number of
1028 * tasks ready to run).
1030 * @param p priority level to look at
1031 * @return number of tasks pending right now
1034 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
1036 struct GNUNET_SCHEDULER_Task *pos;
1039 GNUNET_assert (NULL != active_task);
1040 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
1042 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
1043 p = current_priority;
1045 for (pos = ready_head[check_priority (p)]; NULL != pos; pos = pos->next)
1052 * Cancel the task with the specified identifier.
1053 * The task must not yet have run.
1055 * @param task id of the task to cancel
1056 * @return original closure of the task
1059 initFdInfo(struct GNUNET_SCHEDULER_Task *task,
1060 struct GNUNET_NETWORK_Handle *read_nh,
1061 struct GNUNET_NETWORK_Handle *write_nh,
1062 struct GNUNET_DISK_FileHandle *read_fh,
1063 struct GNUNET_DISK_FileHandle *write_fh)
1065 // either only network handles or only file handles are allowed
1066 GNUNET_assert (!((NULL != read_nh || NULL != write_nh) && (NULL != read_fh || NULL != write_fh)));
1068 if (NULL != read_nh && NULL != write_nh)
1072 GNUNET_new_array (2, struct GNUNET_SCHEDULER_FdInfo);
1073 struct GNUNET_SCHEDULER_FdInfo *read_fdi = t->fds;
1074 struct GNUENT_SCHEDULER_FdInfo *write_fdi = t->fds + 1;
1075 read_fdi->fd = read_nh;
1076 read_fdi->et = GNUNET_SCHEDULER_ET_IN;
1077 read_fdi->sock = GNUNET_NETWORK_get_fd (read_nh);
1078 write_fdi->fd = write_nh;
1079 write_fdi->et = GNUNET_SCHEDULER_ET_OUT;
1080 write_fdi->sock = GNUNET_NETWORK_get_fd (write_nh);
1082 else if (NULL != read_fh && NULL != write_fh)
1086 GNUNET_new_array (2, struct GNUNET_SCHEDULER_FdInfo);
1087 struct GNUNET_SCHEDULER_FdInfo *read_fdi = t->fds;
1088 struct GNUENT_SCHEDULER_FdInfo *write_fdi = t->fds + 1;
1089 read_fdi->fh = read_fh;
1090 read_fdi->et = GNUNET_SCHEDULER_ET_IN;
1091 write_fdi->fh = write_fh;
1092 write_fdi->et = GNUNET_SCHEDULER_ET_OUT;
1094 else if (NULL != read_nh)
1096 struct GNUNET_SCHEDULER_FdInfo *read_fdi = &t->fdx;
1097 read_fdi->fd = read_nh;
1098 read_fdi->et = GNUNET_SCHEDULER_ET_IN;
1099 read_fdi->sock = GNUNET_NETWORK_get_fd (read_nh);
1101 else if (NULL != write_nh)
1103 struct GNUNET_SCHEDULER_FdInfo *write_fdi = &t->fdx;
1104 write_fdi->fd = write_nh;
1105 write_fdi->et = GNUNET_SCHEDULER_ET_OUT;
1106 write_fdi->sock = GNUNET_NETWORK_get_fd (write_nh);
1108 else if (NULL != read_fh)
1110 struct GNUNET_SCHEDULER_FdInfo *read_fdi = &t->fdx;
1111 read_fdi->fh = read_fh;
1112 read_fdi->et = GNUNET_SCHEDULER_ET_IN;
1114 else if (NULL != write_fh)
1116 struct GNUNET_SCHEDULER_FdInfo *write_fdi = &t->fdx;
1117 write_fdi->fh = write_fh;
1118 write_fdi->et = GNUNET_SCHEDULER_ET_OUT;
1123 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
1125 enum GNUNET_SCHEDULER_Priority p;
1127 struct GNUNET_SCHEDULER_FdInfo *fdi;
1129 GNUNET_assert ( (NULL != active_task) ||
1130 (GNUNET_NO == task->lifeness) );
1131 if (! task->in_ready_list)
1133 if ( (-1 == task->read_fd) &&
1134 (-1 == task->write_fd) &&
1135 (NULL == task->read_set) &&
1136 (NULL == task->write_set) )
1138 if (GNUNET_YES == task->on_shutdown)
1139 GNUNET_CONTAINER_DLL_remove (shutdown_head,
1143 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1144 pending_timeout_tail,
1146 if (task == pending_timeout_last)
1147 pending_timeout_last = NULL;
1151 /*GNUNET_CONTAINER_DLL_remove (pending_head,
1154 fdi = GNUNET_new (struct GNUNET_SCHEDULER_FdInfo);
1155 initFdInfo(fdi, task);
1156 scheduler_driver->del(scheduler_driver->cls, task, fdi);
1161 p = check_priority (task->priority);
1162 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1167 ret = task->callback_cls;
1168 LOG (GNUNET_ERROR_TYPE_DEBUG,
1169 "Canceling task %p\n",
1171 destroy_task (task);
1177 * Initialize backtrace data for task @a t
1179 * @param t task to initialize
1182 init_backtrace (struct GNUNET_SCHEDULER_Task *t)
1185 void *backtrace_array[MAX_TRACE_DEPTH];
1187 t->num_backtrace_strings
1188 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1189 t->backtrace_strings =
1190 backtrace_symbols (backtrace_array,
1191 t->num_backtrace_strings);
1198 * Continue the current execution with the given function. This is
1199 * similar to the other "add" functions except that there is no delay
1200 * and the reason code can be specified.
1202 * @param task main function of the task
1203 * @param task_cls closure for @a task
1204 * @param reason reason for task invocation
1205 * @param priority priority to use for the task
1208 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
1210 enum GNUNET_SCHEDULER_Reason reason,
1211 enum GNUNET_SCHEDULER_Priority priority)
1213 struct GNUNET_SCHEDULER_Task *t;
1215 GNUNET_assert (NULL != task);
1216 GNUNET_assert ((NULL != active_task) ||
1217 (GNUNET_SCHEDULER_REASON_STARTUP == reason));
1218 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1222 t->callback_cls = task_cls;
1224 t->start_time = GNUNET_TIME_absolute_get ();
1227 t->priority = priority;
1228 t->lifeness = current_lifeness;
1229 LOG (GNUNET_ERROR_TYPE_DEBUG,
1230 "Adding continuation task %p\n",
1233 queue_ready_task (t);
1238 * Schedule a new task to be run at the specified time. The task
1239 * will be scheduled for execution at time @a at.
1241 * @param at time when the operation should run
1242 * @param priority priority to use for the task
1243 * @param task main function of the task
1244 * @param task_cls closure of @a task
1245 * @return unique task identifier for the job
1246 * only valid until @a task is started!
1248 struct GNUNET_SCHEDULER_Task *
1249 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1250 enum GNUNET_SCHEDULER_Priority priority,
1251 GNUNET_SCHEDULER_TaskCallback task,
1254 struct GNUNET_SCHEDULER_Task *t;
1255 struct GNUNET_SCHEDULER_Task *pos;
1256 struct GNUNET_SCHEDULER_Task *prev;
1258 GNUNET_assert (NULL != active_task);
1259 GNUNET_assert (NULL != task);
1260 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1262 t->callback_cls = task_cls;
1266 t->start_time = GNUNET_TIME_absolute_get ();
1269 t->priority = priority;
1270 t->lifeness = current_lifeness;
1271 /* try tail first (optimization in case we are
1272 * appending to a long list of tasks with timeouts) */
1273 if ( (NULL == pending_timeout_head) ||
1274 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us) )
1276 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1277 pending_timeout_tail,
1282 /* first move from heuristic start backwards to before start time */
1283 prev = pending_timeout_last;
1284 while ( (NULL != prev) &&
1285 (prev->timeout.abs_value_us > t->timeout.abs_value_us) )
1287 /* now, move from heuristic start (or head of list) forward to insertion point */
1289 pos = pending_timeout_head;
1292 while ( (NULL != pos) &&
1293 ( (pos->timeout.abs_value_us <= t->timeout.abs_value_us) ||
1294 (0 != pos->reason) ) )
1299 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1300 pending_timeout_tail,
1304 /* finally, update heuristic insertion point to last insertion... */
1305 pending_timeout_last = t;
1307 LOG (GNUNET_ERROR_TYPE_DEBUG,
1308 "Adding task: %p\n",
1316 * Schedule a new task to be run with a specified delay. The task
1317 * will be scheduled for execution once the delay has expired.
1319 * @param delay when should this operation time out?
1320 * @param priority priority to use for the task
1321 * @param task main function of the task
1322 * @param task_cls closure of @a task
1323 * @return unique task identifier for the job
1324 * only valid until @a task is started!
1326 struct GNUNET_SCHEDULER_Task *
1327 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1328 enum GNUNET_SCHEDULER_Priority priority,
1329 GNUNET_SCHEDULER_TaskCallback task,
1332 return GNUNET_SCHEDULER_add_at_with_priority (GNUNET_TIME_relative_to_absolute (delay),
1340 * Schedule a new task to be run with a specified priority.
1342 * @param prio how important is the new task?
1343 * @param task main function of the task
1344 * @param task_cls closure of @a task
1345 * @return unique task identifier for the job
1346 * only valid until @a task is started!
1348 struct GNUNET_SCHEDULER_Task *
1349 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1350 GNUNET_SCHEDULER_TaskCallback task,
1353 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1361 * Schedule a new task to be run at the specified time. The task
1362 * will be scheduled for execution once specified time has been
1363 * reached. It will be run with the DEFAULT priority.
1365 * @param at time at which this operation should run
1366 * @param task main function of the task
1367 * @param task_cls closure of @a task
1368 * @return unique task identifier for the job
1369 * only valid until @a task is started!
1371 struct GNUNET_SCHEDULER_Task *
1372 GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1373 GNUNET_SCHEDULER_TaskCallback task,
1376 return GNUNET_SCHEDULER_add_at_with_priority (at,
1377 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1384 * Schedule a new task to be run with a specified delay. The task
1385 * will be scheduled for execution once the delay has expired. It
1386 * will be run with the DEFAULT priority.
1388 * @param delay when should this operation time out?
1389 * @param task main function of the task
1390 * @param task_cls closure of @a task
1391 * @return unique task identifier for the job
1392 * only valid until @a task is started!
1394 struct GNUNET_SCHEDULER_Task *
1395 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1396 GNUNET_SCHEDULER_TaskCallback task,
1399 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1400 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1407 * Schedule a new task to be run as soon as possible. Note that this
1408 * does not guarantee that this will be the next task that is being
1409 * run, as other tasks with higher priority (or that are already ready
1410 * to run) might get to run first. Just as with delays, clients must
1411 * not rely on any particular order of execution between tasks
1412 * scheduled concurrently.
1414 * The task will be run with the DEFAULT priority.
1416 * @param task main function of the task
1417 * @param task_cls closure of @a task
1418 * @return unique task identifier for the job
1419 * only valid until @a task is started!
1421 struct GNUNET_SCHEDULER_Task *
1422 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
1425 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO,
1432 * Schedule a new task to be run on shutdown, that is when a CTRL-C
1433 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
1436 * @param task main function of the task
1437 * @param task_cls closure of @a task
1438 * @return unique task identifier for the job
1439 * only valid until @a task is started!
1441 struct GNUNET_SCHEDULER_Task *
1442 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1445 struct GNUNET_SCHEDULER_Task *t;
1447 GNUNET_assert (NULL != active_task);
1448 GNUNET_assert (NULL != task);
1449 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1451 t->callback_cls = task_cls;
1455 t->start_time = GNUNET_TIME_absolute_get ();
1457 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1458 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1459 t->on_shutdown = GNUNET_YES;
1460 t->lifeness = GNUNET_YES;
1461 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1464 LOG (GNUNET_ERROR_TYPE_DEBUG,
1465 "Adding task: %p\n",
1473 * Schedule a new task to be run as soon as possible with the
1474 * (transitive) ignore-shutdown flag either explicitly set or
1475 * explicitly enabled. This task (and all tasks created from it,
1476 * other than by another call to this function) will either count or
1477 * not count for the "lifeness" of the process. This API is only
1478 * useful in a few special cases.
1480 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
1481 * @param task main function of the task
1482 * @param task_cls closure of @a task
1483 * @return unique task identifier for the job
1484 * only valid until @a task is started!
1486 struct GNUNET_SCHEDULER_Task *
1487 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1488 GNUNET_SCHEDULER_TaskCallback task,
1491 struct GNUNET_SCHEDULER_Task *ret;
1493 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1494 ret->lifeness = lifeness;
1500 * Schedule a new task to be run with a specified delay or when any of
1501 * the specified file descriptor sets is ready. The delay can be used
1502 * as a timeout on the socket(s) being ready. The task will be
1503 * scheduled for execution once either the delay has expired or any of
1504 * the socket operations is ready. This is the most general
1505 * function of the "add" family. Note that the "prerequisite_task"
1506 * must be satisfied in addition to any of the other conditions. In
1507 * other words, the task will be started when
1509 * (prerequisite-run)
1515 * @param delay how long should we wait?
1516 * @param priority priority to use
1517 * @param rfd file descriptor we want to read (can be -1)
1518 * @param wfd file descriptors we want to write (can be -1)
1519 * @param task main function of the task
1520 * @param task_cls closure of @a task
1521 * @return unique task identifier for the job
1522 * only valid until @a task is started!
1525 static struct GNUNET_SCHEDULER_Task *
1526 add_without_sets (struct GNUNET_TIME_Relative delay,
1527 enum GNUNET_SCHEDULER_Priority priority,
1528 struct GNUNET_NETWORK_Handle *read_nh,
1529 struct GNUNET_NETWORK_Handle *write_nh,
1530 struct GNUNET_DISK_FileHandle *read_fh,
1531 struct GNUNET_DISK_FileHandle *write_fh,
1534 GNUNET_SCHEDULER_TaskCallback task,
1537 struct GNUNET_SCHEDULER_Task *t;
1539 GNUNET_assert (NULL != active_task);
1540 GNUNET_assert (NULL != task);
1541 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1543 t->callback_cls = task_cls;
1547 int flags = fcntl (rfd, F_GETFD);
1549 if ((flags == -1) && (errno == EBADF))
1551 LOG (GNUNET_ERROR_TYPE_ERROR,
1552 "Got invalid file descriptor %d!\n",
1560 int flags = fcntl (wfd, F_GETFD);
1562 if (flags == -1 && errno == EBADF)
1564 LOG (GNUNET_ERROR_TYPE_ERROR,
1565 "Got invalid file descriptor %d!\n",
1573 GNUNET_assert (wfd >= -1);
1576 t->start_time = GNUNET_TIME_absolute_get ();
1578 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1579 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1580 t->lifeness = current_lifeness;
1581 //GNUNET_CONTAINER_DLL_insert (pending_head,
1584 //fdi = GNUNET_new (struct GNUNET_SCHEDULER_FdInfo);
1585 //initFdInfo(fdi, t);
1586 //scheduler_driver->add(scheduler_driver->cls, t , fdi);
1587 //initFdInfo (&t->fdx, read_nh, write_nh, read_fh, write_fh)
1588 initFdInfo (t, read_nh, write_nh, read_fh, write_fh);
1589 // FIXME: call add for each fdi if t->fds is a list
1590 scheduler_driver->add(scheduler_driver->cls, t , fdi);
1592 max_priority_added = GNUNET_MAX (max_priority_added,
1594 LOG (GNUNET_ERROR_TYPE_DEBUG,
1604 * Schedule a new task to be run with a specified delay or when the
1605 * specified file descriptor is ready for reading. The delay can be
1606 * used as a timeout on the socket being ready. The task will be
1607 * scheduled for execution once either the delay has expired or the
1608 * socket operation is ready. It will be run with the DEFAULT priority.
1610 * @param delay when should this operation time out?
1611 * @param rfd read file-descriptor
1612 * @param task main function of the task
1613 * @param task_cls closure of @a task
1614 * @return unique task identifier for the job
1615 * only valid until @a task is started!
1617 struct GNUNET_SCHEDULER_Task *
1618 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1619 struct GNUNET_NETWORK_Handle *rfd,
1620 GNUNET_SCHEDULER_TaskCallback task,
1623 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1624 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1625 rfd, task, task_cls);
1630 * Schedule a new task to be run with a specified priority and to be
1631 * run after the specified delay or when the specified file descriptor
1632 * is ready for reading. The delay can be used as a timeout on the
1633 * socket being ready. The task will be scheduled for execution once
1634 * either the delay has expired or the socket operation is ready. It
1635 * will be run with the DEFAULT priority.
1637 * @param delay when should this operation time out?
1638 * @param priority priority to use for the task
1639 * @param rfd read file-descriptor
1640 * @param task main function of the task
1641 * @param task_cls closure of @a task
1642 * @return unique task identifier for the job
1643 * only valid until @a task is started!
1645 struct GNUNET_SCHEDULER_Task *
1646 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1647 enum GNUNET_SCHEDULER_Priority priority,
1648 struct GNUNET_NETWORK_Handle *rfd,
1649 GNUNET_SCHEDULER_TaskCallback task,
1652 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1661 * Schedule a new task to be run with a specified delay or when the
1662 * specified file descriptor is ready for writing. The delay can be
1663 * used as a timeout on the socket being ready. The task will be
1664 * scheduled for execution once either the delay has expired or the
1665 * socket operation is ready. It will be run with the priority of
1668 * @param delay when should this operation time out?
1669 * @param wfd write file-descriptor
1670 * @param task main function of the task
1671 * @param task_cls closure of @a task
1672 * @return unique task identifier for the job
1673 * only valid until @a task is started!
1675 struct GNUNET_SCHEDULER_Task *
1676 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1677 struct GNUNET_NETWORK_Handle *wfd,
1678 GNUNET_SCHEDULER_TaskCallback task,
1681 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1682 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1684 GNUNET_NO, GNUNET_YES,
1689 * Schedule a new task to be run with a specified delay or when the
1690 * specified file descriptor is ready. 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.
1695 * @param delay when should this operation time out?
1696 * @param priority priority of the task
1697 * @param fd file-descriptor
1698 * @param on_read whether to poll the file-descriptor for readability
1699 * @param on_write whether to poll the file-descriptor for writability
1700 * @param task main function of the task
1701 * @param task_cls closure of task
1702 * @return unique task identifier for the job
1703 * only valid until "task" is started!
1705 struct GNUNET_SCHEDULER_Task *
1706 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1707 enum GNUNET_SCHEDULER_Priority priority,
1708 struct GNUNET_NETWORK_Handle *fd,
1711 GNUNET_SCHEDULER_TaskCallback task,
1715 struct GNUNET_NETWORK_FDSet *s;
1716 struct GNUNET_SCHEDULER_Task * ret;
1718 GNUNET_assert (NULL != fd);
1719 s = GNUNET_NETWORK_fdset_create ();
1720 GNUNET_NETWORK_fdset_set (s, fd);
1721 ret = GNUNET_SCHEDULER_add_select (
1724 on_write ? s : NULL,
1726 GNUNET_NETWORK_fdset_destroy (s);
1729 GNUNET_assert (on_read || on_write);
1730 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1731 return add_without_sets (delay, priority,
1732 on_read ? fd : NULL,
1733 on_write ? fd : NULL,
1742 * Schedule a new task to be run with a specified delay or when the
1743 * specified file descriptor is ready for reading. The delay can be
1744 * used as a timeout on the socket being ready. The task will be
1745 * scheduled for execution once either the delay has expired or the
1746 * socket operation is ready. It will be run with the DEFAULT priority.
1748 * @param delay when should this operation time out?
1749 * @param rfd read file-descriptor
1750 * @param task main function of the task
1751 * @param task_cls closure of @a task
1752 * @return unique task identifier for the job
1753 * only valid until @a task is started!
1755 struct GNUNET_SCHEDULER_Task *
1756 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1757 const struct GNUNET_DISK_FileHandle *rfd,
1758 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1760 return GNUNET_SCHEDULER_add_file_with_priority (
1761 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1762 rfd, GNUNET_YES, GNUNET_NO,
1768 * Schedule a new task to be run with a specified delay or when the
1769 * specified file descriptor is ready for writing. The delay can be
1770 * used as a timeout on the socket being ready. The task will be
1771 * scheduled for execution once either the delay has expired or the
1772 * socket operation is ready. It will be run with the DEFAULT priority.
1774 * @param delay when should this operation time out?
1775 * @param wfd write file-descriptor
1776 * @param task main function of the task
1777 * @param task_cls closure of @a task
1778 * @return unique task identifier for the job
1779 * only valid until @a task is started!
1781 struct GNUNET_SCHEDULER_Task *
1782 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1783 const struct GNUNET_DISK_FileHandle *wfd,
1784 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1786 return GNUNET_SCHEDULER_add_file_with_priority (
1787 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1788 wfd, GNUNET_NO, GNUNET_YES,
1794 * Schedule a new task to be run with a specified delay or when the
1795 * specified file descriptor is ready. The delay can be
1796 * used as a timeout on the socket being ready. The task will be
1797 * scheduled for execution once either the delay has expired or the
1798 * socket operation is ready.
1800 * @param delay when should this operation time out?
1801 * @param priority priority of the task
1802 * @param fd file-descriptor
1803 * @param on_read whether to poll the file-descriptor for readability
1804 * @param on_write whether to poll the file-descriptor for writability
1805 * @param task main function of the task
1806 * @param task_cls closure of @a task
1807 * @return unique task identifier for the job
1808 * only valid until @a task is started!
1810 struct GNUNET_SCHEDULER_Task *
1811 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1812 enum GNUNET_SCHEDULER_Priority priority,
1813 const struct GNUNET_DISK_FileHandle *fd,
1814 int on_read, int on_write,
1815 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1818 struct GNUNET_NETWORK_FDSet *s;
1819 struct GNUNET_SCHEDULER_Task * ret;
1821 GNUNET_assert (NULL != fd);
1822 s = GNUNET_NETWORK_fdset_create ();
1823 GNUNET_NETWORK_fdset_handle_set (s, fd);
1824 ret = GNUNET_SCHEDULER_add_select (
1827 on_write ? s : NULL,
1829 GNUNET_NETWORK_fdset_destroy (s);
1832 GNUNET_assert (on_read || on_write);
1833 GNUNET_assert(fd->fd >= 0);
1834 return add_without_sets (delay, priority,
1837 on_read ? fd : NULL,
1838 on_write ? fd : NULL,
1845 * Schedule a new task to be run with a specified delay or when any of
1846 * the specified file descriptor sets is ready. The delay can be used
1847 * as a timeout on the socket(s) being ready. The task will be
1848 * scheduled for execution once either the delay has expired or any of
1849 * the socket operations is ready. This is the most general
1850 * function of the "add" family. Note that the "prerequisite_task"
1851 * must be satisfied in addition to any of the other conditions. In
1852 * other words, the task will be started when
1854 * (prerequisite-run)
1857 * || any-ws-ready) )
1860 * @param prio how important is this task?
1861 * @param delay how long should we wait?
1862 * @param rs set of file descriptors we want to read (can be NULL)
1863 * @param ws set of file descriptors we want to write (can be NULL)
1864 * @param task main function of the task
1865 * @param task_cls closure of @a task
1866 * @return unique task identifier for the job
1867 * only valid until @a task is started!
1869 struct GNUNET_SCHEDULER_Task *
1870 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1871 struct GNUNET_TIME_Relative delay,
1872 const struct GNUNET_NETWORK_FDSet *rs,
1873 const struct GNUNET_NETWORK_FDSet *ws,
1874 GNUNET_SCHEDULER_TaskCallback task,
1877 struct GNUNET_SCHEDULER_Task *t;
1879 if ( (NULL == rs) &&
1881 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1885 GNUNET_assert (NULL != active_task);
1886 GNUNET_assert (NULL != task);
1887 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1889 t->callback_cls = task_cls;
1894 t->read_set = GNUNET_NETWORK_fdset_create ();
1895 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1899 t->write_set = GNUNET_NETWORK_fdset_create ();
1900 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1903 t->start_time = GNUNET_TIME_absolute_get ();
1905 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1907 check_priority ((prio ==
1908 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1910 t->lifeness = current_lifeness;
1911 /*GNUNET_CONTAINER_DLL_insert (pending_head,
1914 fdi = GNUNET_new (struct GNUNET_SCHEDULER_FdInfo);
1916 scheduler_driver->add(scheduler_driver->cls, t , fdi);
1917 max_priority_added = GNUNET_MAX (max_priority_added,
1919 LOG (GNUNET_ERROR_TYPE_DEBUG,
1928 * Function used by event-loop implementations to signal the scheduler
1929 * that a particular @a task is ready due to an event of type @a et.
1931 * This function will then queue the task to notify the application
1932 * that the task is ready (with the respective priority).
1934 * @param task the task that is ready, NULL for wake up calls
1935 * @param et information about why the task is ready
1938 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1939 enum GNUNET_SCHEDULER_EventType et)
1941 enum GNUNET_SCHEDULER_Reason reason;
1942 struct GNUNET_TIME_Absolute now;
1944 now = GNUNET_TIME_absolute_get ();
1945 reason = task->reason;
1946 if (now.abs_value_us >= task->timeout.abs_value_us)
1947 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1948 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1949 (0 != (GNUNET_SCHEDULER_ET_IN & et)) )
1950 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
1951 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
1952 (0 != (GNUNET_SCHEDULER_ET_OUT & et)) )
1953 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
1954 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1955 task->reason = reason;
1956 task->fds = &task->fdx;
1959 queue_ready_task (task);
1964 * Function called by the driver to tell the scheduler to run some of
1965 * the tasks that are ready. This function may return even though
1966 * there are tasks left to run just to give other tasks a chance as
1967 * well. If we return #GNUNET_YES, the driver should call this
1968 * function again as soon as possible, while if we return #GNUNET_NO
1969 * it must block until the operating system has more work as the
1970 * scheduler has no more work to do right now.
1972 * @param sh scheduler handle that was given to the `loop`
1973 * @return #GNUNET_OK if there are more tasks that are ready,
1974 * and thus we would like to run more (yield to avoid
1975 * blocking other activities for too long)
1976 * #GNUNET_NO if we are done running tasks (yield to block)
1977 * #GNUNET_SYSERR on error
1980 GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh)
1982 enum GNUNET_SCHEDULER_Priority p;
1983 struct GNUNET_SCHEDULER_Task *pos;
1984 struct GNUNET_TIME_Absolute now;
1986 /* check for tasks that reached the timeout! */
1987 now = GNUNET_TIME_absolute_get ();
1988 while (NULL != (pos = pending_timeout_head))
1990 if (now.abs_value_us >= pos->timeout.abs_value_us)
1991 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1992 if (0 == pos->reason)
1994 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1995 pending_timeout_tail,
1997 if (pending_timeout_last == pos)
1998 pending_timeout_last = NULL;
1999 queue_ready_task (pos);
2002 if (0 == ready_count)
2005 /* find out which task priority level we are going to
2006 process this time */
2007 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
2008 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
2009 /* yes, p>0 is correct, 0 is "KEEP" which should
2010 * always be an empty queue (see assertion)! */
2011 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
2013 pos = ready_head[p];
2017 GNUNET_assert (NULL != pos); /* ready_count wrong? */
2019 /* process all tasks at this priority level, then yield */
2020 while (NULL != (pos = ready_head[p]))
2022 GNUNET_CONTAINER_DLL_remove (ready_head[p],
2026 current_priority = pos->priority;
2027 current_lifeness = pos->lifeness;
2030 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
2031 DELAY_THRESHOLD.rel_value_us)
2033 LOG (GNUNET_ERROR_TYPE_DEBUG,
2034 "Task %p took %s to be scheduled\n",
2036 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
2040 tc.reason = pos->reason;
2041 GNUNET_NETWORK_fdset_zero (sh->rs);
2042 GNUNET_NETWORK_fdset_zero (sh->ws);
2043 tc.fds_len = pos->fds_len;
2045 tc.read_ready = (NULL == pos->read_set) ? sh->rs : pos->read_set;
2046 if ( (-1 != pos->read_fd) &&
2047 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
2048 GNUNET_NETWORK_fdset_set_native (sh->rs,
2050 tc.write_ready = (NULL == pos->write_set) ? sh->ws : pos->write_set;
2051 if ((-1 != pos->write_fd) &&
2052 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
2053 GNUNET_NETWORK_fdset_set_native (sh->ws,
2055 LOG (GNUNET_ERROR_TYPE_DEBUG,
2056 "Running task: %p\n",
2058 pos->callback (pos->callback_cls);
2060 dump_backtrace (pos);
2064 if (0 == ready_count)
2071 * Initialize and run scheduler. This function will return when all
2072 * tasks have completed. On systems with signals, receiving a SIGTERM
2073 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
2074 * to be run after the active task is complete. As a result, SIGTERM
2075 * causes all shutdown tasks to be scheduled with reason
2076 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
2077 * afterwards will execute normally!). Note that any particular
2078 * signal will only shut down one scheduler; applications should
2079 * always only create a single scheduler.
2081 * @param driver drive to use for the event loop
2082 * @param task task to run first (and immediately)
2083 * @param task_cls closure of @a task
2084 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2087 GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
2088 GNUNET_SCHEDULER_TaskCallback task,
2092 struct GNUNET_SIGNAL_Context *shc_int;
2093 struct GNUNET_SIGNAL_Context *shc_term;
2094 #if (SIGTERM != GNUNET_TERM_SIG)
2095 struct GNUNET_SIGNAL_Context *shc_gterm;
2098 struct GNUNET_SIGNAL_Context *shc_quit;
2099 struct GNUNET_SIGNAL_Context *shc_hup;
2100 struct GNUNET_SIGNAL_Context *shc_pipe;
2102 struct GNUNET_SCHEDULER_Task tsk;
2103 const struct GNUNET_DISK_FileHandle *pr;
2104 scheduler_driver = driver;
2106 /* general set-up */
2107 GNUNET_assert (NULL == active_task);
2108 GNUNET_assert (NULL == shutdown_pipe_handle);
2109 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
2113 GNUNET_assert (NULL != shutdown_pipe_handle);
2114 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
2115 GNUNET_DISK_PIPE_END_READ);
2116 GNUNET_assert (NULL != pr);
2119 /* install signal handlers */
2120 LOG (GNUNET_ERROR_TYPE_DEBUG,
2121 "Registering signal handlers\n");
2122 shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2123 &sighandler_shutdown);
2124 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2125 &sighandler_shutdown);
2126 #if (SIGTERM != GNUNET_TERM_SIG)
2127 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2128 &sighandler_shutdown);
2131 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2133 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2134 &sighandler_shutdown);
2135 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2136 &sighandler_shutdown);
2139 /* Setup initial tasks */
2140 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2141 current_lifeness = GNUNET_YES;
2147 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
2149 GNUNET_SCHEDULER_REASON_STARTUP,
2150 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
2151 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
2152 &GNUNET_OS_install_parent_control_handler,
2155 driver->set_wakeup (driver->cls,
2156 GNUNET_TIME_absolute_get ());
2158 /* begin main event loop */
2159 sh.rs = GNUNET_NETWORK_fdset_create ();
2160 sh.ws = GNUNET_NETWORK_fdset_create ();
2161 GNUNET_NETWORK_fdset_handle_set (rs, pr);
2163 ret = driver->loop (driver->cls,
2165 GNUNET_NETWORK_fdset_destroy (sh.rs);
2166 GNUNET_NETWORK_fdset_destroy (sh.ws);
2168 /* uninstall signal handlers */
2169 GNUNET_SIGNAL_handler_uninstall (shc_int);
2170 GNUNET_SIGNAL_handler_uninstall (shc_term);
2171 #if (SIGTERM != GNUNET_TERM_SIG)
2172 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
2175 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
2176 GNUNET_SIGNAL_handler_uninstall (shc_quit);
2177 GNUNET_SIGNAL_handler_uninstall (shc_hup);
2179 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2180 shutdown_pipe_handle = NULL;
2185 select_add(void *cls,
2186 struct GNUNET_SCHEDULER_Task *task,
2187 struct GNUNET_SCHEDULER_FdInfo *fdi){
2189 GNUNET_CONTAINER_DLL_insert (pending_head,
2196 select_del(void *cls,
2197 struct GNUNET_SCHEDULER_Task *task,
2198 struct GNUNET_SCHEDULER_FdInfo *fdi){
2200 GNUNET_CONTAINER_DLL_remove (pending_head,
2208 select_loop(void *cls,
2209 struct GNUNET_SCHEDULER_Handle *sh){
2211 while_live(sh-rs, sh->ws);
2216 select_set_wakeup(void *cls,
2217 struct GNUNET_TIME_Absolute dt){
2225 * Obtain the driver for using select() as the event loop.
2227 * @return NULL on error
2229 const struct GNUNET_SCHEDULER_Driver *
2230 GNUNET_SCHEDULER_driver_select ()
2233 GNUNET_SCHEDULER_Driver *select_driver;
2235 select_driver = GNUNET_new (struct GNUNET_SCHEDULER_Driver);
2237 select_driver->loop = &select_loop;
2238 select_driver->add = &select_add;
2239 select_driver->del = &select_del;
2240 select_driver->set_wakeup = &select_set_wakeup;
2243 return select_driver;
2247 /* end of scheduler.c */