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 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
1058 void initFdINfo(GNUNET_SCHEDULER_FdInfo *fdi, struct GNUNET_SCHEDULER_Task *task)
1060 if (-1 != task->read_fd)
1062 fdi->sock=task->read_fd;
1063 }else if (-1 != task->write_fd){
1064 fdi->sock=task->write_fd;
1065 } else if (NULL != task->read_set){
1066 fdi->fd=task->read_set;
1067 }else if (NULL != task->write_set){
1068 fdi->fd=task->write_set;
1073 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
1075 enum GNUNET_SCHEDULER_Priority p;
1077 GNUNET_SCHEDULER_FdInfo *fdi;
1079 GNUNET_assert ( (NULL != active_task) ||
1080 (GNUNET_NO == task->lifeness) );
1081 if (! task->in_ready_list)
1083 if ( (-1 == task->read_fd) &&
1084 (-1 == task->write_fd) &&
1085 (NULL == task->read_set) &&
1086 (NULL == task->write_set) )
1088 if (GNUNET_YES == task->on_shutdown)
1089 GNUNET_CONTAINER_DLL_remove (shutdown_head,
1093 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1094 pending_timeout_tail,
1096 if (task == pending_timeout_last)
1097 pending_timeout_last = NULL;
1101 /*GNUNET_CONTAINER_DLL_remove (pending_head,
1104 fdi = GNUNET_new (struct GNUNET_SCHEDULER_FdInfo);
1105 initFdINfo(fdi, task);
1106 scheduler_driver->del(scheduler_driver->cls, task, fdi);
1111 p = check_priority (task->priority);
1112 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1117 ret = task->callback_cls;
1118 LOG (GNUNET_ERROR_TYPE_DEBUG,
1119 "Canceling task %p\n",
1121 destroy_task (task);
1127 * Initialize backtrace data for task @a t
1129 * @param t task to initialize
1132 init_backtrace (struct GNUNET_SCHEDULER_Task *t)
1135 void *backtrace_array[MAX_TRACE_DEPTH];
1137 t->num_backtrace_strings
1138 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1139 t->backtrace_strings =
1140 backtrace_symbols (backtrace_array,
1141 t->num_backtrace_strings);
1148 * Continue the current execution with the given function. This is
1149 * similar to the other "add" functions except that there is no delay
1150 * and the reason code can be specified.
1152 * @param task main function of the task
1153 * @param task_cls closure for @a task
1154 * @param reason reason for task invocation
1155 * @param priority priority to use for the task
1158 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
1160 enum GNUNET_SCHEDULER_Reason reason,
1161 enum GNUNET_SCHEDULER_Priority priority)
1163 struct GNUNET_SCHEDULER_Task *t;
1165 GNUNET_assert (NULL != task);
1166 GNUNET_assert ((NULL != active_task) ||
1167 (GNUNET_SCHEDULER_REASON_STARTUP == reason));
1168 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1172 t->callback_cls = task_cls;
1174 t->start_time = GNUNET_TIME_absolute_get ();
1177 t->priority = priority;
1178 t->lifeness = current_lifeness;
1179 LOG (GNUNET_ERROR_TYPE_DEBUG,
1180 "Adding continuation task %p\n",
1183 queue_ready_task (t);
1188 * Schedule a new task to be run at the specified time. The task
1189 * will be scheduled for execution at time @a at.
1191 * @param at time when the operation should run
1192 * @param priority priority to use for the task
1193 * @param task main function of the task
1194 * @param task_cls closure of @a task
1195 * @return unique task identifier for the job
1196 * only valid until @a task is started!
1198 struct GNUNET_SCHEDULER_Task *
1199 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1200 enum GNUNET_SCHEDULER_Priority priority,
1201 GNUNET_SCHEDULER_TaskCallback task,
1204 struct GNUNET_SCHEDULER_Task *t;
1205 struct GNUNET_SCHEDULER_Task *pos;
1206 struct GNUNET_SCHEDULER_Task *prev;
1208 GNUNET_assert (NULL != active_task);
1209 GNUNET_assert (NULL != task);
1210 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1212 t->callback_cls = task_cls;
1216 t->start_time = GNUNET_TIME_absolute_get ();
1219 t->priority = priority;
1220 t->lifeness = current_lifeness;
1221 /* try tail first (optimization in case we are
1222 * appending to a long list of tasks with timeouts) */
1223 if ( (NULL == pending_timeout_head) ||
1224 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us) )
1226 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1227 pending_timeout_tail,
1232 /* first move from heuristic start backwards to before start time */
1233 prev = pending_timeout_last;
1234 while ( (NULL != prev) &&
1235 (prev->timeout.abs_value_us > t->timeout.abs_value_us) )
1237 /* now, move from heuristic start (or head of list) forward to insertion point */
1239 pos = pending_timeout_head;
1242 while ( (NULL != pos) &&
1243 ( (pos->timeout.abs_value_us <= t->timeout.abs_value_us) ||
1244 (0 != pos->reason) ) )
1249 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1250 pending_timeout_tail,
1254 /* finally, update heuristic insertion point to last insertion... */
1255 pending_timeout_last = t;
1257 LOG (GNUNET_ERROR_TYPE_DEBUG,
1258 "Adding task: %p\n",
1266 * Schedule a new task to be run with a specified delay. The task
1267 * will be scheduled for execution once the delay has expired.
1269 * @param delay when should this operation time out?
1270 * @param priority priority to use for the task
1271 * @param task main function of the task
1272 * @param task_cls closure of @a task
1273 * @return unique task identifier for the job
1274 * only valid until @a task is started!
1276 struct GNUNET_SCHEDULER_Task *
1277 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1278 enum GNUNET_SCHEDULER_Priority priority,
1279 GNUNET_SCHEDULER_TaskCallback task,
1282 return GNUNET_SCHEDULER_add_at_with_priority (GNUNET_TIME_relative_to_absolute (delay),
1290 * Schedule a new task to be run with a specified priority.
1292 * @param prio how important is the new task?
1293 * @param task main function of the task
1294 * @param task_cls closure of @a task
1295 * @return unique task identifier for the job
1296 * only valid until @a task is started!
1298 struct GNUNET_SCHEDULER_Task *
1299 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1300 GNUNET_SCHEDULER_TaskCallback task,
1303 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1311 * Schedule a new task to be run at the specified time. The task
1312 * will be scheduled for execution once specified time has been
1313 * reached. It will be run with the DEFAULT priority.
1315 * @param at time at which this operation should run
1316 * @param task main function of the task
1317 * @param task_cls closure of @a task
1318 * @return unique task identifier for the job
1319 * only valid until @a task is started!
1321 struct GNUNET_SCHEDULER_Task *
1322 GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1323 GNUNET_SCHEDULER_TaskCallback task,
1326 return GNUNET_SCHEDULER_add_at_with_priority (at,
1327 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1334 * Schedule a new task to be run with a specified delay. The task
1335 * will be scheduled for execution once the delay has expired. It
1336 * will be run with the DEFAULT priority.
1338 * @param delay when should this operation time out?
1339 * @param task main function of the task
1340 * @param task_cls closure of @a task
1341 * @return unique task identifier for the job
1342 * only valid until @a task is started!
1344 struct GNUNET_SCHEDULER_Task *
1345 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1346 GNUNET_SCHEDULER_TaskCallback task,
1349 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1350 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1357 * Schedule a new task to be run as soon as possible. Note that this
1358 * does not guarantee that this will be the next task that is being
1359 * run, as other tasks with higher priority (or that are already ready
1360 * to run) might get to run first. Just as with delays, clients must
1361 * not rely on any particular order of execution between tasks
1362 * scheduled concurrently.
1364 * The task will be run with the DEFAULT priority.
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_now (GNUNET_SCHEDULER_TaskCallback task,
1375 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO,
1382 * Schedule a new task to be run on shutdown, that is when a CTRL-C
1383 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
1386 * @param task main function of the task
1387 * @param task_cls closure of @a task
1388 * @return unique task identifier for the job
1389 * only valid until @a task is started!
1391 struct GNUNET_SCHEDULER_Task *
1392 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1395 struct GNUNET_SCHEDULER_Task *t;
1397 GNUNET_assert (NULL != active_task);
1398 GNUNET_assert (NULL != task);
1399 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1401 t->callback_cls = task_cls;
1405 t->start_time = GNUNET_TIME_absolute_get ();
1407 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1408 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1409 t->on_shutdown = GNUNET_YES;
1410 t->lifeness = GNUNET_YES;
1411 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1414 LOG (GNUNET_ERROR_TYPE_DEBUG,
1415 "Adding task: %p\n",
1423 * Schedule a new task to be run as soon as possible with the
1424 * (transitive) ignore-shutdown flag either explicitly set or
1425 * explicitly enabled. This task (and all tasks created from it,
1426 * other than by another call to this function) will either count or
1427 * not count for the "lifeness" of the process. This API is only
1428 * useful in a few special cases.
1430 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
1431 * @param task main function of the task
1432 * @param task_cls closure of @a task
1433 * @return unique task identifier for the job
1434 * only valid until @a task is started!
1436 struct GNUNET_SCHEDULER_Task *
1437 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1438 GNUNET_SCHEDULER_TaskCallback task,
1441 struct GNUNET_SCHEDULER_Task *ret;
1443 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1444 ret->lifeness = lifeness;
1450 * Schedule a new task to be run with a specified delay or when any of
1451 * the specified file descriptor sets is ready. The delay can be used
1452 * as a timeout on the socket(s) being ready. The task will be
1453 * scheduled for execution once either the delay has expired or any of
1454 * the socket operations is ready. This is the most general
1455 * function of the "add" family. Note that the "prerequisite_task"
1456 * must be satisfied in addition to any of the other conditions. In
1457 * other words, the task will be started when
1459 * (prerequisite-run)
1465 * @param delay how long should we wait?
1466 * @param priority priority to use
1467 * @param rfd file descriptor we want to read (can be -1)
1468 * @param wfd file descriptors we want to write (can be -1)
1469 * @param task main function of the task
1470 * @param task_cls closure of @a task
1471 * @return unique task identifier for the job
1472 * only valid until @a task is started!
1475 static struct GNUNET_SCHEDULER_Task *
1476 add_without_sets (struct GNUNET_TIME_Relative delay,
1477 enum GNUNET_SCHEDULER_Priority priority,
1480 GNUNET_SCHEDULER_TaskCallback task,
1483 struct GNUNET_SCHEDULER_Task *t;
1485 GNUNET_assert (NULL != active_task);
1486 GNUNET_assert (NULL != task);
1487 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1489 t->callback_cls = task_cls;
1493 int flags = fcntl (rfd, F_GETFD);
1495 if ((flags == -1) && (errno == EBADF))
1497 LOG (GNUNET_ERROR_TYPE_ERROR,
1498 "Got invalid file descriptor %d!\n",
1506 int flags = fcntl (wfd, F_GETFD);
1508 if (flags == -1 && errno == EBADF)
1510 LOG (GNUNET_ERROR_TYPE_ERROR,
1511 "Got invalid file descriptor %d!\n",
1519 GNUNET_assert (wfd >= -1);
1522 t->start_time = GNUNET_TIME_absolute_get ();
1524 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1525 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1526 t->lifeness = current_lifeness;
1527 /*GNUNET_CONTAINER_DLL_insert (pending_head,
1530 fdi = GNUNET_new (struct GNUNET_SCHEDULER_FdInfo);
1531 initFdINfo(fdi, task);
1532 scheduler_driver->add(scheduler_driver, t , fdi);
1533 max_priority_added = GNUNET_MAX (max_priority_added,
1535 LOG (GNUNET_ERROR_TYPE_DEBUG,
1545 * Schedule a new task to be run with a specified delay or when the
1546 * specified file descriptor is ready for reading. The delay can be
1547 * used as a timeout on the socket being ready. The task will be
1548 * scheduled for execution once either the delay has expired or the
1549 * socket operation is ready. It will be run with the DEFAULT priority.
1551 * @param delay when should this operation time out?
1552 * @param rfd read file-descriptor
1553 * @param task main function of the task
1554 * @param task_cls closure of @a task
1555 * @return unique task identifier for the job
1556 * only valid until @a task is started!
1558 struct GNUNET_SCHEDULER_Task *
1559 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1560 struct GNUNET_NETWORK_Handle *rfd,
1561 GNUNET_SCHEDULER_TaskCallback task,
1564 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1565 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1566 rfd, task, task_cls);
1571 * Schedule a new task to be run with a specified priority and to be
1572 * run after the specified delay or when the specified file descriptor
1573 * is ready for reading. The delay can be used as a timeout on the
1574 * socket being ready. The task will be scheduled for execution once
1575 * either the delay has expired or the socket operation is ready. It
1576 * will be run with the DEFAULT priority.
1578 * @param delay when should this operation time out?
1579 * @param priority priority to use for the task
1580 * @param rfd read file-descriptor
1581 * @param task main function of the task
1582 * @param task_cls closure of @a task
1583 * @return unique task identifier for the job
1584 * only valid until @a task is started!
1586 struct GNUNET_SCHEDULER_Task *
1587 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1588 enum GNUNET_SCHEDULER_Priority priority,
1589 struct GNUNET_NETWORK_Handle *rfd,
1590 GNUNET_SCHEDULER_TaskCallback task,
1593 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1602 * Schedule a new task to be run with a specified delay or when the
1603 * specified file descriptor is ready for writing. The delay can be
1604 * used as a timeout on the socket being ready. The task will be
1605 * scheduled for execution once either the delay has expired or the
1606 * socket operation is ready. It will be run with the priority of
1609 * @param delay when should this operation time out?
1610 * @param wfd write file-descriptor
1611 * @param task main function of the task
1612 * @param task_cls closure of @a task
1613 * @return unique task identifier for the job
1614 * only valid until @a task is started!
1616 struct GNUNET_SCHEDULER_Task *
1617 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1618 struct GNUNET_NETWORK_Handle *wfd,
1619 GNUNET_SCHEDULER_TaskCallback task,
1622 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1623 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1625 GNUNET_NO, GNUNET_YES,
1630 * Schedule a new task to be run with a specified delay or when the
1631 * specified file descriptor is ready. The delay can be
1632 * used as a timeout on the socket being ready. The task will be
1633 * scheduled for execution once either the delay has expired or the
1634 * socket operation is ready.
1636 * @param delay when should this operation time out?
1637 * @param priority priority of the task
1638 * @param fd file-descriptor
1639 * @param on_read whether to poll the file-descriptor for readability
1640 * @param on_write whether to poll the file-descriptor for writability
1641 * @param task main function of the task
1642 * @param task_cls closure of task
1643 * @return unique task identifier for the job
1644 * only valid until "task" is started!
1646 struct GNUNET_SCHEDULER_Task *
1647 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1648 enum GNUNET_SCHEDULER_Priority priority,
1649 struct GNUNET_NETWORK_Handle *fd,
1652 GNUNET_SCHEDULER_TaskCallback task,
1656 struct GNUNET_NETWORK_FDSet *s;
1657 struct GNUNET_SCHEDULER_Task * ret;
1659 GNUNET_assert (NULL != fd);
1660 s = GNUNET_NETWORK_fdset_create ();
1661 GNUNET_NETWORK_fdset_set (s, fd);
1662 ret = GNUNET_SCHEDULER_add_select (
1665 on_write ? s : NULL,
1667 GNUNET_NETWORK_fdset_destroy (s);
1670 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1671 return add_without_sets (delay, priority,
1672 on_read ? GNUNET_NETWORK_get_fd (fd) : -1,
1673 on_write ? GNUNET_NETWORK_get_fd (fd) : -1,
1680 * Schedule a new task to be run with a specified delay or when the
1681 * specified file descriptor is ready for reading. The delay can be
1682 * used as a timeout on the socket being ready. The task will be
1683 * scheduled for execution once either the delay has expired or the
1684 * socket operation is ready. It will be run with the DEFAULT priority.
1686 * @param delay when should this operation time out?
1687 * @param rfd read file-descriptor
1688 * @param task main function of the task
1689 * @param task_cls closure of @a task
1690 * @return unique task identifier for the job
1691 * only valid until @a task is started!
1693 struct GNUNET_SCHEDULER_Task *
1694 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1695 const struct GNUNET_DISK_FileHandle *rfd,
1696 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1698 return GNUNET_SCHEDULER_add_file_with_priority (
1699 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1700 rfd, GNUNET_YES, GNUNET_NO,
1706 * Schedule a new task to be run with a specified delay or when the
1707 * specified file descriptor is ready for writing. The delay can be
1708 * used as a timeout on the socket being ready. The task will be
1709 * scheduled for execution once either the delay has expired or the
1710 * socket operation is ready. It will be run with the DEFAULT priority.
1712 * @param delay when should this operation time out?
1713 * @param wfd write file-descriptor
1714 * @param task main function of the task
1715 * @param task_cls closure of @a task
1716 * @return unique task identifier for the job
1717 * only valid until @a task is started!
1719 struct GNUNET_SCHEDULER_Task *
1720 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1721 const struct GNUNET_DISK_FileHandle *wfd,
1722 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1724 return GNUNET_SCHEDULER_add_file_with_priority (
1725 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1726 wfd, GNUNET_NO, GNUNET_YES,
1732 * Schedule a new task to be run with a specified delay or when the
1733 * specified file descriptor is ready. The delay can be
1734 * used as a timeout on the socket being ready. The task will be
1735 * scheduled for execution once either the delay has expired or the
1736 * socket operation is ready.
1738 * @param delay when should this operation time out?
1739 * @param priority priority of the task
1740 * @param fd file-descriptor
1741 * @param on_read whether to poll the file-descriptor for readability
1742 * @param on_write whether to poll the file-descriptor for writability
1743 * @param task main function of the task
1744 * @param task_cls closure of @a task
1745 * @return unique task identifier for the job
1746 * only valid until @a task is started!
1748 struct GNUNET_SCHEDULER_Task *
1749 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1750 enum GNUNET_SCHEDULER_Priority priority,
1751 const struct GNUNET_DISK_FileHandle *fd,
1752 int on_read, int on_write,
1753 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1756 struct GNUNET_NETWORK_FDSet *s;
1757 struct GNUNET_SCHEDULER_Task * ret;
1759 GNUNET_assert (NULL != fd);
1760 s = GNUNET_NETWORK_fdset_create ();
1761 GNUNET_NETWORK_fdset_handle_set (s, fd);
1762 ret = GNUNET_SCHEDULER_add_select (
1765 on_write ? s : NULL,
1767 GNUNET_NETWORK_fdset_destroy (s);
1772 GNUNET_DISK_internal_file_handle_ (fd, &real_fd, sizeof (int));
1773 GNUNET_assert (real_fd >= 0);
1774 return add_without_sets (
1776 on_read ? real_fd : -1,
1777 on_write ? real_fd : -1,
1784 * Schedule a new task to be run with a specified delay or when any of
1785 * the specified file descriptor sets is ready. The delay can be used
1786 * as a timeout on the socket(s) being ready. The task will be
1787 * scheduled for execution once either the delay has expired or any of
1788 * the socket operations is ready. This is the most general
1789 * function of the "add" family. Note that the "prerequisite_task"
1790 * must be satisfied in addition to any of the other conditions. In
1791 * other words, the task will be started when
1793 * (prerequisite-run)
1796 * || any-ws-ready) )
1799 * @param prio how important is this task?
1800 * @param delay how long should we wait?
1801 * @param rs set of file descriptors we want to read (can be NULL)
1802 * @param ws set of file descriptors we want to write (can be NULL)
1803 * @param task main function of the task
1804 * @param task_cls closure of @a task
1805 * @return unique task identifier for the job
1806 * only valid until @a task is started!
1808 struct GNUNET_SCHEDULER_Task *
1809 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1810 struct GNUNET_TIME_Relative delay,
1811 const struct GNUNET_NETWORK_FDSet *rs,
1812 const struct GNUNET_NETWORK_FDSet *ws,
1813 GNUNET_SCHEDULER_TaskCallback task,
1816 struct GNUNET_SCHEDULER_Task *t;
1818 if ( (NULL == rs) &&
1820 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1824 GNUNET_assert (NULL != active_task);
1825 GNUNET_assert (NULL != task);
1826 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1828 t->callback_cls = task_cls;
1833 t->read_set = GNUNET_NETWORK_fdset_create ();
1834 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1838 t->write_set = GNUNET_NETWORK_fdset_create ();
1839 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1842 t->start_time = GNUNET_TIME_absolute_get ();
1844 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1846 check_priority ((prio ==
1847 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1849 t->lifeness = current_lifeness;
1850 /*GNUNET_CONTAINER_DLL_insert (pending_head,
1853 fdi = GNUNET_new (struct GNUNET_SCHEDULER_FdInfo);
1854 initFdINfo(fdi, task);
1855 scheduler_driver->add(scheduler_driver, t , fdi);
1856 max_priority_added = GNUNET_MAX (max_priority_added,
1858 LOG (GNUNET_ERROR_TYPE_DEBUG,
1867 * Function used by event-loop implementations to signal the scheduler
1868 * that a particular @a task is ready due to an event of type @a et.
1870 * This function will then queue the task to notify the application
1871 * that the task is ready (with the respective priority).
1873 * @param task the task that is ready, NULL for wake up calls
1874 * @param et information about why the task is ready
1877 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1878 enum GNUNET_SCHEDULER_EventType et)
1880 enum GNUNET_SCHEDULER_Reason reason;
1881 struct GNUNET_TIME_Absolute now;
1883 now = GNUNET_TIME_absolute_get ();
1884 reason = task->reason;
1885 if (now.abs_value_us >= task->timeout.abs_value_us)
1886 reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1887 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1888 (0 != (GNUNET_SCHEDULER_ET_IN & et)) )
1889 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
1890 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
1891 (0 != (GNUNET_SCHEDULER_ET_OUT & et)) )
1892 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
1893 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1894 task->reason = reason;
1895 task->fds = &task->fdx;
1898 queue_ready_task (task);
1903 * Function called by the driver to tell the scheduler to run some of
1904 * the tasks that are ready. This function may return even though
1905 * there are tasks left to run just to give other tasks a chance as
1906 * well. If we return #GNUNET_YES, the driver should call this
1907 * function again as soon as possible, while if we return #GNUNET_NO
1908 * it must block until the operating system has more work as the
1909 * scheduler has no more work to do right now.
1911 * @param sh scheduler handle that was given to the `loop`
1912 * @return #GNUNET_OK if there are more tasks that are ready,
1913 * and thus we would like to run more (yield to avoid
1914 * blocking other activities for too long)
1915 * #GNUNET_NO if we are done running tasks (yield to block)
1916 * #GNUNET_SYSERR on error
1919 GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh)
1921 enum GNUNET_SCHEDULER_Priority p;
1922 struct GNUNET_SCHEDULER_Task *pos;
1923 struct GNUNET_TIME_Absolute now;
1925 /* check for tasks that reached the timeout! */
1926 now = GNUNET_TIME_absolute_get ();
1927 while (NULL != (pos = pending_timeout_head))
1929 if (now.abs_value_us >= pos->timeout.abs_value_us)
1930 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1931 if (0 == pos->reason)
1933 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1934 pending_timeout_tail,
1936 if (pending_timeout_last == pos)
1937 pending_timeout_last = NULL;
1938 queue_ready_task (pos);
1941 if (0 == ready_count)
1944 /* find out which task priority level we are going to
1945 process this time */
1946 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
1947 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
1948 /* yes, p>0 is correct, 0 is "KEEP" which should
1949 * always be an empty queue (see assertion)! */
1950 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
1952 pos = ready_head[p];
1956 GNUNET_assert (NULL != pos); /* ready_count wrong? */
1958 /* process all tasks at this priority level, then yield */
1959 while (NULL != (pos = ready_head[p]))
1961 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1965 current_priority = pos->priority;
1966 current_lifeness = pos->lifeness;
1969 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
1970 DELAY_THRESHOLD.rel_value_us)
1972 LOG (GNUNET_ERROR_TYPE_DEBUG,
1973 "Task %p took %s to be scheduled\n",
1975 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
1979 tc.reason = pos->reason;
1980 GNUNET_NETWORK_fdset_zero (sh->rs);
1981 GNUNET_NETWORK_fdset_zero (sh->ws);
1982 tc.fds_len = pos->fds_len;
1984 tc.read_ready = (NULL == pos->read_set) ? sh->rs : pos->read_set;
1985 if ( (-1 != pos->read_fd) &&
1986 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_READ_READY)) )
1987 GNUNET_NETWORK_fdset_set_native (sh->rs,
1989 tc.write_ready = (NULL == pos->write_set) ? sh->ws : pos->write_set;
1990 if ((-1 != pos->write_fd) &&
1991 (0 != (pos->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)))
1992 GNUNET_NETWORK_fdset_set_native (sh->ws,
1994 LOG (GNUNET_ERROR_TYPE_DEBUG,
1995 "Running task: %p\n",
1997 pos->callback (pos->callback_cls);
1999 dump_backtrace (pos);
2003 if (0 == ready_count)
2010 * Initialize and run scheduler. This function will return when all
2011 * tasks have completed. On systems with signals, receiving a SIGTERM
2012 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
2013 * to be run after the active task is complete. As a result, SIGTERM
2014 * causes all shutdown tasks to be scheduled with reason
2015 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
2016 * afterwards will execute normally!). Note that any particular
2017 * signal will only shut down one scheduler; applications should
2018 * always only create a single scheduler.
2020 * @param driver drive to use for the event loop
2021 * @param task task to run first (and immediately)
2022 * @param task_cls closure of @a task
2023 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
2026 GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
2027 GNUNET_SCHEDULER_TaskCallback task,
2031 struct GNUNET_SIGNAL_Context *shc_int;
2032 struct GNUNET_SIGNAL_Context *shc_term;
2033 #if (SIGTERM != GNUNET_TERM_SIG)
2034 struct GNUNET_SIGNAL_Context *shc_gterm;
2037 struct GNUNET_SIGNAL_Context *shc_quit;
2038 struct GNUNET_SIGNAL_Context *shc_hup;
2039 struct GNUNET_SIGNAL_Context *shc_pipe;
2041 struct GNUNET_SCHEDULER_Task tsk;
2042 const struct GNUNET_DISK_FileHandle *pr;
2043 scheduler_driver = driver;
2045 /* general set-up */
2046 GNUNET_assert (NULL == active_task);
2047 GNUNET_assert (NULL == shutdown_pipe_handle);
2048 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
2052 GNUNET_assert (NULL != shutdown_pipe_handle);
2053 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
2054 GNUNET_DISK_PIPE_END_READ);
2055 GNUNET_assert (NULL != pr);
2058 /* install signal handlers */
2059 LOG (GNUNET_ERROR_TYPE_DEBUG,
2060 "Registering signal handlers\n");
2061 shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2062 &sighandler_shutdown);
2063 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2064 &sighandler_shutdown);
2065 #if (SIGTERM != GNUNET_TERM_SIG)
2066 shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2067 &sighandler_shutdown);
2070 shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2072 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2073 &sighandler_shutdown);
2074 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2075 &sighandler_shutdown);
2078 /* Setup initial tasks */
2079 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2080 current_lifeness = GNUNET_YES;
2086 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
2088 GNUNET_SCHEDULER_REASON_STARTUP,
2089 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
2090 GNUNET_SCHEDULER_add_now_with_lifeness (GNUNET_NO,
2091 &GNUNET_OS_install_parent_control_handler,
2094 driver->set_wakeup (driver->cls,
2095 GNUNET_TIME_absolute_get ());
2097 /* begin main event loop */
2098 sh.rs = GNUNET_NETWORK_fdset_create ();
2099 sh.ws = GNUNET_NETWORK_fdset_create ();
2100 GNUNET_NETWORK_fdset_handle_set (rs, pr);
2102 ret = driver->loop (driver->cls,
2104 GNUNET_NETWORK_fdset_destroy (sh.rs);
2105 GNUNET_NETWORK_fdset_destroy (sh.ws);
2107 /* uninstall signal handlers */
2108 GNUNET_SIGNAL_handler_uninstall (shc_int);
2109 GNUNET_SIGNAL_handler_uninstall (shc_term);
2110 #if (SIGTERM != GNUNET_TERM_SIG)
2111 GNUNET_SIGNAL_handler_uninstall (shc_gterm);
2114 GNUNET_SIGNAL_handler_uninstall (shc_pipe);
2115 GNUNET_SIGNAL_handler_uninstall (shc_quit);
2116 GNUNET_SIGNAL_handler_uninstall (shc_hup);
2118 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2119 shutdown_pipe_handle = NULL;
2124 select_add(void *cls,
2125 struct GNUNET_SCHEDULER_Task *task,
2126 struct GNUNET_SCHEDULER_FdInfo *fdi){
2128 GNUNET_CONTAINER_DLL_insert (pending_head,
2135 select_del(void *cls,
2136 struct GNUNET_SCHEDULER_Task *task,
2137 struct GNUNET_SCHEDULER_FdInfo *fdi){
2139 GNUNET_CONTAINER_DLL_remove (pending_head,
2147 select_loop(void *cls,
2148 struct GNUNET_SCHEDULER_Handle *sh){
2150 while_live(sh-rs, sh->ws);
2155 select_set_wakeup(void *cls,
2156 struct GNUNET_TIME_Absolute dt){
2164 * Obtain the driver for using select() as the event loop.
2166 * @return NULL on error
2168 const struct GNUNET_SCHEDULER_Driver *
2169 GNUNET_SCHEDULER_driver_select ()
2172 GNUNET_SCHEDULER_Driver *select_driver;
2174 select_driver = GNUNET_new (struct GNUNET_SCHEDULER_Driver);
2176 select_driver->loop = &select_loop;
2177 select_driver->add = &select_add;
2178 select_driver->del = &select_del;
2179 select_driver->set_wakeup = &select_set_wakeup;
2182 return select_driver;
2186 /* end of scheduler.c */