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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file util/scheduler.c
20 * @brief schedule computations using continuation passing style
21 * @author Christian Grothoff
24 #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_do_work(). 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 * context of the SIGINT handler
96 struct GNUNET_SIGNAL_Context *shc_int;
99 * context of the SIGTERM handler
101 struct GNUNET_SIGNAL_Context *shc_term;
103 #if (SIGTERM != GNUNET_TERM_SIG)
105 * context of the TERM_SIG handler
107 struct GNUNET_SIGNAL_Context *shc_gterm;
112 * context of the SIGQUIT handler
114 struct GNUNET_SIGNAL_Context *shc_quit;
117 * context of the SIGHUP handler
119 struct GNUNET_SIGNAL_Context *shc_hup;
122 * context of hte SIGPIPE handler
124 struct GNUNET_SIGNAL_Context *shc_pipe;
130 * Entry in list of pending tasks.
132 struct GNUNET_SCHEDULER_Task
135 * This is a linked list.
137 struct GNUNET_SCHEDULER_Task *next;
140 * This is a linked list.
142 struct GNUNET_SCHEDULER_Task *prev;
145 * Function to run when ready.
147 GNUNET_SCHEDULER_TaskCallback callback;
150 * Closure for the @e callback.
155 * Information about which FDs are ready for this task (and why).
157 struct GNUNET_SCHEDULER_FdInfo *fds;
160 * Storage location used for @e fds if we want to avoid
161 * a separate malloc() call in the common case that this
162 * task is only about a single FD.
164 struct GNUNET_SCHEDULER_FdInfo fdx;
167 * Size of the @e fds array.
169 unsigned int fds_len;
172 * Do we own the network and file handles referenced by the FdInfo
173 * structs in the fds array. This will only be GNUNET_YES if the
174 * task was created by the #GNUNET_SCHEDULER_add_select function.
179 * Absolute timeout value for the task, or
180 * #GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
182 struct GNUNET_TIME_Absolute timeout;
186 * When was the task scheduled?
188 struct GNUNET_TIME_Absolute start_time;
192 * Why is the task ready? Set after task is added to ready queue.
193 * Initially set to zero. All reasons that have already been
194 * satisfied (i.e. read or write ready) will be set over time.
196 enum GNUNET_SCHEDULER_Reason reason;
201 enum GNUNET_SCHEDULER_Priority priority;
204 * Set if we only wait for reading from a single FD, otherwise -1.
209 * Set if we only wait for writing to a single FD, otherwise -1.
214 * Should the existence of this task in the queue be counted as
215 * reason to not shutdown the scheduler?
220 * Is this task run on shutdown?
225 * Is this task in the ready list?
231 * Array of strings which make up a backtrace from the point when this
232 * task was scheduled (essentially, who scheduled the task?)
234 char **backtrace_strings;
237 * Size of the backtrace_strings array
239 int num_backtrace_strings;
246 * A struct representing an event the select driver is waiting for
250 struct Scheduled *prev;
252 struct Scheduled *next;
255 * the task, the event is related to
257 struct GNUNET_SCHEDULER_Task *task;
260 * information about the network socket / file descriptor where
261 * the event is expected to occur
263 struct GNUNET_SCHEDULER_FdInfo *fdi;
266 * the event types (multiple event types can be ORed) the select
267 * driver is expected to wait for
269 enum GNUNET_SCHEDULER_EventType et;
274 * Driver context used by GNUNET_SCHEDULER_run
279 * the head of a DLL containing information about the events the
280 * select driver is waiting for
282 struct Scheduled *scheduled_head;
285 * the tail of a DLL containing information about the events the
286 * select driver is waiting for
288 struct Scheduled *scheduled_tail;
291 * the time when the select driver will wake up again (after
294 struct GNUNET_TIME_Absolute timeout;
299 * The driver used for the event loop. Will be handed over to
300 * the scheduler in #GNUNET_SCHEDULER_do_work(), persisted
301 * there in this variable for later use in functions like
302 * #GNUNET_SCHEDULER_add_select(), #add_without_sets() and
303 * #GNUNET_SCHEDULER_cancel().
305 static const struct GNUNET_SCHEDULER_Driver *scheduler_driver;
308 * Head of list of tasks waiting for an event.
310 static struct GNUNET_SCHEDULER_Task *pending_head;
313 * Tail of list of tasks waiting for an event.
315 static struct GNUNET_SCHEDULER_Task *pending_tail;
318 * Head of list of tasks waiting for shutdown.
320 static struct GNUNET_SCHEDULER_Task *shutdown_head;
323 * Tail of list of tasks waiting for shutdown.
325 static struct GNUNET_SCHEDULER_Task *shutdown_tail;
328 * List of tasks waiting ONLY for a timeout event.
329 * Sorted by timeout (earliest first). Used so that
330 * we do not traverse the list of these tasks when
331 * building select sets (we just look at the head
332 * to determine the respective timeout ONCE).
334 static struct GNUNET_SCHEDULER_Task *pending_timeout_head;
337 * List of tasks waiting ONLY for a timeout event.
338 * Sorted by timeout (earliest first). Used so that
339 * we do not traverse the list of these tasks when
340 * building select sets (we just look at the head
341 * to determine the respective timeout ONCE).
343 static struct GNUNET_SCHEDULER_Task *pending_timeout_tail;
346 * Last inserted task waiting ONLY for a timeout event.
347 * Used to (heuristically) speed up insertion.
349 static struct GNUNET_SCHEDULER_Task *pending_timeout_last;
352 * ID of the task that is running right now.
354 static struct GNUNET_SCHEDULER_Task *active_task;
357 * Head of list of tasks ready to run right now, grouped by importance.
359 static struct GNUNET_SCHEDULER_Task *ready_head[GNUNET_SCHEDULER_PRIORITY_COUNT];
362 * Tail of list of tasks ready to run right now, grouped by importance.
364 static struct GNUNET_SCHEDULER_Task *ready_tail[GNUNET_SCHEDULER_PRIORITY_COUNT];
367 * Task for installing parent control handlers (it might happen that the
368 * scheduler is shutdown before this task is executed, so
369 * GNUNET_SCHEDULER_shutdown must cancel it in that case)
371 static struct GNUNET_SCHEDULER_Task *install_parent_control_task;
374 * Task for reading from a pipe that signal handlers will use to initiate
377 static struct GNUNET_SCHEDULER_Task *shutdown_pipe_task;
380 * Number of tasks on the ready list.
382 static unsigned int ready_count;
385 * Priority of the task running right now. Only
386 * valid while a task is running.
388 static enum GNUNET_SCHEDULER_Priority current_priority;
391 * Priority of the highest task added in the current select
394 static enum GNUNET_SCHEDULER_Priority max_priority_added;
397 * Value of the 'lifeness' flag for the current task.
399 static int current_lifeness;
402 * Function to use as a select() in the scheduler.
403 * If NULL, we use GNUNET_NETWORK_socket_select().
405 static GNUNET_SCHEDULER_select scheduler_select;
408 * Task context of the current task.
410 static struct GNUNET_SCHEDULER_TaskContext tc;
413 * Closure for #scheduler_select.
415 static void *scheduler_select_cls;
419 * Sets the select function to use in the scheduler (scheduler_select).
421 * @param new_select new select function to use
422 * @param new_select_cls closure for @a new_select
423 * @return previously used select function, NULL for default
426 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
427 void *new_select_cls)
429 scheduler_select = new_select;
430 scheduler_select_cls = new_select_cls;
435 * Check that the given priority is legal (and return it).
437 * @param p priority value to check
438 * @return p on success, 0 on error
440 static enum GNUNET_SCHEDULER_Priority
441 check_priority (enum GNUNET_SCHEDULER_Priority p)
443 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
446 return 0; /* make compiler happy */
451 * chooses the nearest timeout from all pending tasks, to be used
452 * to tell the driver the next wakeup time (using its set_wakeup
455 struct GNUNET_TIME_Absolute
458 struct GNUNET_SCHEDULER_Task *pos;
459 struct GNUNET_TIME_Absolute now;
460 struct GNUNET_TIME_Absolute timeout;
462 pos = pending_timeout_head;
463 now = GNUNET_TIME_absolute_get ();
464 timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
467 if (0 != pos->reason)
473 timeout = pos->timeout;
476 for (pos = pending_head; NULL != pos; pos = pos->next)
478 if (0 != pos->reason)
482 else if ((pos->timeout.abs_value_us != GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) &&
483 (timeout.abs_value_us > pos->timeout.abs_value_us))
485 timeout = pos->timeout;
493 * Put a task that is ready for execution into the ready queue.
495 * @param task task ready for execution
498 queue_ready_task (struct GNUNET_SCHEDULER_Task *task)
500 enum GNUNET_SCHEDULER_Priority p = check_priority (task->priority);
502 GNUNET_CONTAINER_DLL_insert (ready_head[p],
505 task->in_ready_list = GNUNET_YES;
511 * Request the shutdown of a scheduler. Marks all tasks
512 * awaiting shutdown as ready. Note that tasks
513 * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call
514 * will be delayed until the next shutdown signal.
517 GNUNET_SCHEDULER_shutdown ()
519 struct GNUNET_SCHEDULER_Task *pos;
521 LOG (GNUNET_ERROR_TYPE_DEBUG,
522 "GNUNET_SCHEDULER_shutdown\n");
523 if (NULL != install_parent_control_task)
525 GNUNET_SCHEDULER_cancel (install_parent_control_task);
526 install_parent_control_task = NULL;
528 if (NULL != shutdown_pipe_task)
530 GNUNET_SCHEDULER_cancel (shutdown_pipe_task);
531 shutdown_pipe_task = NULL;
533 while (NULL != (pos = shutdown_head))
535 GNUNET_CONTAINER_DLL_remove (shutdown_head,
538 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
539 queue_ready_task (pos);
545 * Output stack trace of task @a t.
547 * @param t task to dump stack trace of
550 dump_backtrace (struct GNUNET_SCHEDULER_Task *t)
553 for (unsigned int i = 0; i < t->num_backtrace_strings; i++)
554 LOG (GNUNET_ERROR_TYPE_WARNING,
555 "Task %p trace %u: %s\n",
558 t->backtrace_strings[i]);
566 * Destroy a task (release associated resources)
568 * @param t task to destroy
571 destroy_task (struct GNUNET_SCHEDULER_Task *t)
575 LOG (GNUNET_ERROR_TYPE_DEBUG,
576 "destroying task %p\n",
579 if (GNUNET_YES == t->own_handles)
581 for (i = 0; i != t->fds_len; ++i)
583 const struct GNUNET_NETWORK_Handle *fd = t->fds[i].fd;
584 const struct GNUNET_DISK_FileHandle *fh = t->fds[i].fh;
587 GNUNET_NETWORK_socket_free_memory_only_ ((struct GNUNET_NETWORK_Handle *) fd);
591 // FIXME: on WIN32 this is not enough! A function
592 // GNUNET_DISK_file_free_memory_only would be nice
593 GNUNET_free ((void *) fh);
599 GNUNET_array_grow (t->fds, t->fds_len, 0);
602 GNUNET_free (t->backtrace_strings);
609 * Pipe used to communicate shutdown via signal.
611 static struct GNUNET_DISK_PipeHandle *shutdown_pipe_handle;
614 * Process ID of this process at the time we installed the various
620 * Signal handler called for SIGPIPE.
632 // * Wait for a short time.
633 // * Sleeps for @a ms ms (as that should be long enough for virtually all
634 // * modern systems to context switch and allow another process to do
635 // * some 'real' work).
637 // * @param ms how many ms to wait
640 //short_wait (unsigned int ms)
642 // struct GNUNET_TIME_Relative timeout;
644 // timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, ms);
645 // (void) GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
650 * Signal handler called for signals that should cause us to shutdown.
653 sighandler_shutdown ()
656 int old_errno = errno; /* backup errno */
658 if (getpid () != my_pid)
659 exit (1); /* we have fork'ed since the signal handler was created,
660 * ignore the signal, see https://gnunet.org/vfork discussion */
661 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
662 (shutdown_pipe_handle, GNUNET_DISK_PIPE_END_WRITE),
669 shutdown_if_no_lifeness ()
671 struct GNUNET_SCHEDULER_Task *t;
675 for (t = pending_head; NULL != t; t = t->next)
676 if (GNUNET_YES == t->lifeness)
678 for (t = shutdown_head; NULL != t; t = t->next)
679 if (GNUNET_YES == t->lifeness)
681 for (t = pending_timeout_head; NULL != t; t = t->next)
682 if (GNUNET_YES == t->lifeness)
685 GNUNET_SCHEDULER_shutdown ();
690 select_loop (struct GNUNET_SCHEDULER_Handle *sh,
691 struct DriverContext *context);
695 * Initialize and run scheduler. This function will return when all
696 * tasks have completed. On systems with signals, receiving a SIGTERM
697 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown()
698 * to be run after the active task is complete. As a result, SIGTERM
699 * causes all active tasks to be scheduled with reason
700 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
701 * afterwards will execute normally!). Note that any particular signal
702 * will only shut down one scheduler; applications should always only
703 * create a single scheduler.
705 * @param task task to run immediately
706 * @param task_cls closure of @a task
709 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
712 struct GNUNET_SCHEDULER_Handle *sh;
713 struct GNUNET_SCHEDULER_Driver *driver;
714 struct DriverContext context = {.scheduled_head = NULL,
715 .scheduled_tail = NULL,
716 .timeout = GNUNET_TIME_absolute_get ()};
718 driver = GNUNET_SCHEDULER_driver_select ();
719 driver->cls = &context;
720 sh = GNUNET_SCHEDULER_driver_init (driver);
721 GNUNET_SCHEDULER_add_with_reason_and_priority (task,
723 GNUNET_SCHEDULER_REASON_STARTUP,
724 GNUNET_SCHEDULER_PRIORITY_DEFAULT);
727 GNUNET_SCHEDULER_driver_done (sh);
728 GNUNET_free (driver);
733 * Obtain the task context, giving the reason why the current task was
736 * @return current tasks' scheduler context
738 const struct GNUNET_SCHEDULER_TaskContext *
739 GNUNET_SCHEDULER_get_task_context ()
741 GNUNET_assert (NULL != active_task);
747 * Get information about the current load of this scheduler. Use this
748 * function to determine if an elective task should be added or simply
749 * dropped (if the decision should be made based on the number of
750 * tasks ready to run).
752 * @param p priority level to look at
753 * @return number of tasks pending right now
756 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p)
758 struct GNUNET_SCHEDULER_Task *pos;
761 GNUNET_assert (NULL != active_task);
762 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
764 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
765 p = current_priority;
767 for (pos = ready_head[check_priority (p)]; NULL != pos; pos = pos->next)
774 init_fd_info (struct GNUNET_SCHEDULER_Task *t,
775 const struct GNUNET_NETWORK_Handle *const *read_nh,
776 unsigned int read_nh_len,
777 const struct GNUNET_NETWORK_Handle *const *write_nh,
778 unsigned int write_nh_len,
779 const struct GNUNET_DISK_FileHandle *const *read_fh,
780 unsigned int read_fh_len,
781 const struct GNUNET_DISK_FileHandle *const *write_fh,
782 unsigned int write_fh_len)
784 // FIXME: if we have exactly two network handles / exactly two file handles
785 // and they are equal, we can make one FdInfo with both
786 // GNUNET_SCHEDULER_ET_IN and GNUNET_SCHEDULER_ET_OUT set.
787 struct GNUNET_SCHEDULER_FdInfo *fdi;
789 t->fds_len = read_nh_len + write_nh_len + read_fh_len + write_fh_len;
794 if (1 == read_nh_len)
796 GNUNET_assert (NULL != read_nh);
797 GNUNET_assert (NULL != *read_nh);
799 fdi->et = GNUNET_SCHEDULER_ET_IN;
800 fdi->sock = GNUNET_NETWORK_get_fd (*read_nh);
801 t->read_fd = fdi->sock;
804 else if (1 == write_nh_len)
806 GNUNET_assert (NULL != write_nh);
807 GNUNET_assert (NULL != *write_nh);
809 fdi->et = GNUNET_SCHEDULER_ET_OUT;
810 fdi->sock = GNUNET_NETWORK_get_fd (*write_nh);
812 t->write_fd = fdi->sock;
814 else if (1 == read_fh_len)
816 GNUNET_assert (NULL != read_fh);
817 GNUNET_assert (NULL != *read_fh);
819 fdi->et = GNUNET_SCHEDULER_ET_IN;
820 fdi->sock = (*read_fh)->fd; // FIXME: does not work under WIN32
821 t->read_fd = fdi->sock;
826 GNUNET_assert (NULL != write_fh);
827 GNUNET_assert (NULL != *write_fh);
829 fdi->et = GNUNET_SCHEDULER_ET_OUT;
830 fdi->sock = (*write_fh)->fd; // FIXME: does not work under WIN32
832 t->write_fd = fdi->sock;
837 fdi = GNUNET_new_array (t->fds_len, struct GNUNET_SCHEDULER_FdInfo);
842 for (i = 0; i != read_nh_len; ++i)
844 fdi->fd = read_nh[i];
845 GNUNET_assert (NULL != fdi->fd);
846 fdi->et = GNUNET_SCHEDULER_ET_IN;
847 fdi->sock = GNUNET_NETWORK_get_fd (read_nh[i]);
850 for (i = 0; i != write_nh_len; ++i)
852 fdi->fd = write_nh[i];
853 GNUNET_assert (NULL != fdi->fd);
854 fdi->et = GNUNET_SCHEDULER_ET_OUT;
855 fdi->sock = GNUNET_NETWORK_get_fd (write_nh[i]);
858 for (i = 0; i != read_fh_len; ++i)
860 fdi->fh = read_fh[i];
861 GNUNET_assert (NULL != fdi->fh);
862 fdi->et = GNUNET_SCHEDULER_ET_IN;
863 fdi->sock = (read_fh[i])->fd; // FIXME: does not work under WIN32
866 for (i = 0; i != write_fh_len; ++i)
868 fdi->fh = write_fh[i];
869 GNUNET_assert (NULL != fdi->fh);
870 fdi->et = GNUNET_SCHEDULER_ET_OUT;
871 fdi->sock = (write_fh[i])->fd; // FIXME: does not work under WIN32
879 * calls the given function @a func on each FdInfo related to @a t.
880 * Optionally updates the event type field in each FdInfo after calling
884 * @param driver_func the function to call with each FdInfo contained in
886 * @param if_not_ready only call @a driver_func on FdInfos that are not
888 * @param et the event type to be set in each FdInfo after calling
889 * @a driver_func on it, or -1 if no updating not desired.
892 driver_add_multiple (struct GNUNET_SCHEDULER_Task *t)
894 struct GNUNET_SCHEDULER_FdInfo *fdi;
895 int success = GNUNET_YES;
897 for (unsigned int i = 0; i != t->fds_len; ++i)
900 success = scheduler_driver->add (scheduler_driver->cls,
903 fdi->et = GNUNET_SCHEDULER_ET_NONE;
905 if (GNUNET_YES != success)
907 LOG (GNUNET_ERROR_TYPE_ERROR,
908 "driver could not add task\n");
914 install_parent_control_handler (void *cls)
916 install_parent_control_task = NULL;
917 GNUNET_OS_install_parent_control_handler (NULL);
922 shutdown_pipe_cb (void *cls)
925 const struct GNUNET_DISK_FileHandle *pr;
927 shutdown_pipe_task = NULL;
928 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
929 GNUNET_DISK_PIPE_END_READ);
930 GNUNET_assert (! GNUNET_DISK_handle_invalid (pr));
931 /* consume the signal */
932 GNUNET_DISK_file_read (pr, &c, sizeof (c));
933 /* mark all active tasks as ready due to shutdown */
934 GNUNET_SCHEDULER_shutdown ();
936 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
944 * Cancel the task with the specified identifier.
945 * The task must not yet have run. Only allowed to be called as long as the
946 * scheduler is running, that is one of the following conditions is met:
948 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
949 * - #GNUNET_SCHEDULER_driver_init has been run and
950 * #GNUNET_SCHEDULER_driver_done has not been called yet
952 * @param task id of the task to cancel
953 * @return original closure of the task
956 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task)
958 enum GNUNET_SCHEDULER_Priority p;
962 LOG (GNUNET_ERROR_TYPE_DEBUG,
963 "canceling task %p\n",
966 /* scheduler must be running */
967 GNUNET_assert (NULL != scheduler_driver);
968 GNUNET_assert ( (NULL != active_task) ||
969 (GNUNET_NO == task->lifeness) );
970 is_fd_task = (NULL != task->fds);
973 int del_result = scheduler_driver->del (scheduler_driver->cls, task);
974 if (GNUNET_OK != del_result)
976 LOG (GNUNET_ERROR_TYPE_ERROR,
977 "driver could not delete task\n");
981 if (! task->in_ready_list)
985 GNUNET_CONTAINER_DLL_remove (pending_head,
989 else if (GNUNET_YES == task->on_shutdown)
991 GNUNET_CONTAINER_DLL_remove (shutdown_head,
997 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
998 pending_timeout_tail,
1000 if (pending_timeout_last == task)
1001 pending_timeout_last = NULL;
1006 p = check_priority (task->priority);
1007 GNUNET_CONTAINER_DLL_remove (ready_head[p],
1012 ret = task->callback_cls;
1013 destroy_task (task);
1019 * Initialize backtrace data for task @a t
1021 * @param t task to initialize
1024 init_backtrace (struct GNUNET_SCHEDULER_Task *t)
1027 void *backtrace_array[MAX_TRACE_DEPTH];
1029 t->num_backtrace_strings
1030 = backtrace (backtrace_array, MAX_TRACE_DEPTH);
1031 t->backtrace_strings =
1032 backtrace_symbols (backtrace_array,
1033 t->num_backtrace_strings);
1042 * Continue the current execution with the given function. This is
1043 * similar to the other "add" functions except that there is no delay
1044 * and the reason code can be specified.
1046 * @param task main function of the task
1047 * @param task_cls closure for @a task
1048 * @param reason reason for task invocation
1049 * @param priority priority to use for the task
1052 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
1054 enum GNUNET_SCHEDULER_Reason reason,
1055 enum GNUNET_SCHEDULER_Priority priority)
1057 struct GNUNET_SCHEDULER_Task *t;
1059 GNUNET_assert (NULL != task);
1060 GNUNET_assert ((NULL != active_task) ||
1061 (GNUNET_SCHEDULER_REASON_STARTUP == reason));
1062 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1066 t->callback_cls = task_cls;
1068 t->start_time = GNUNET_TIME_absolute_get ();
1071 t->priority = check_priority (priority);
1072 t->lifeness = current_lifeness;
1073 LOG (GNUNET_ERROR_TYPE_DEBUG,
1074 "Adding continuation task %p\n",
1077 queue_ready_task (t);
1082 * Schedule a new task to be run at the specified time. The task
1083 * will be scheduled for execution at time @a at.
1085 * @param at time when the operation should run
1086 * @param priority priority to use for the task
1087 * @param task main function of the task
1088 * @param task_cls closure of @a task
1089 * @return unique task identifier for the job
1090 * only valid until @a task is started!
1092 struct GNUNET_SCHEDULER_Task *
1093 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
1094 enum GNUNET_SCHEDULER_Priority priority,
1095 GNUNET_SCHEDULER_TaskCallback task,
1098 struct GNUNET_SCHEDULER_Task *t;
1099 struct GNUNET_SCHEDULER_Task *pos;
1100 struct GNUNET_SCHEDULER_Task *prev;
1102 GNUNET_assert (NULL != active_task);
1103 GNUNET_assert (NULL != task);
1104 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1106 t->callback_cls = task_cls;
1110 t->start_time = GNUNET_TIME_absolute_get ();
1113 t->priority = check_priority (priority);
1114 t->lifeness = current_lifeness;
1115 /* try tail first (optimization in case we are
1116 * appending to a long list of tasks with timeouts) */
1117 if ( (NULL == pending_timeout_head) ||
1118 (at.abs_value_us < pending_timeout_head->timeout.abs_value_us) )
1120 GNUNET_CONTAINER_DLL_insert (pending_timeout_head,
1121 pending_timeout_tail,
1126 /* first move from heuristic start backwards to before start time */
1127 prev = pending_timeout_last;
1128 while ( (NULL != prev) &&
1129 (prev->timeout.abs_value_us > t->timeout.abs_value_us) )
1131 /* now, move from heuristic start (or head of list) forward to insertion point */
1133 pos = pending_timeout_head;
1136 while ((NULL != pos) && (pos->timeout.abs_value_us <= t->timeout.abs_value_us))
1141 GNUNET_CONTAINER_DLL_insert_after (pending_timeout_head,
1142 pending_timeout_tail,
1146 /* finally, update heuristic insertion point to last insertion... */
1147 pending_timeout_last = t;
1149 LOG (GNUNET_ERROR_TYPE_DEBUG,
1158 * Schedule a new task to be run with a specified delay. The task
1159 * will be scheduled for execution once the delay has expired.
1161 * @param delay when should this operation time out?
1162 * @param priority priority to use for the task
1163 * @param task main function of the task
1164 * @param task_cls closure of @a task
1165 * @return unique task identifier for the job
1166 * only valid until @a task is started!
1168 struct GNUNET_SCHEDULER_Task *
1169 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
1170 enum GNUNET_SCHEDULER_Priority priority,
1171 GNUNET_SCHEDULER_TaskCallback task,
1174 return GNUNET_SCHEDULER_add_at_with_priority (GNUNET_TIME_relative_to_absolute (delay),
1182 * Schedule a new task to be run with a specified priority.
1184 * @param prio how important is the new task?
1185 * @param task main function of the task
1186 * @param task_cls closure of @a task
1187 * @return unique task identifier for the job
1188 * only valid until @a task is started!
1190 struct GNUNET_SCHEDULER_Task *
1191 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
1192 GNUNET_SCHEDULER_TaskCallback task,
1195 return GNUNET_SCHEDULER_add_delayed_with_priority (GNUNET_TIME_UNIT_ZERO,
1203 * Schedule a new task to be run at the specified time. The task
1204 * will be scheduled for execution once specified time has been
1205 * reached. It will be run with the DEFAULT priority.
1207 * @param at time at which this operation should run
1208 * @param task main function of the task
1209 * @param task_cls closure of @a task
1210 * @return unique task identifier for the job
1211 * only valid until @a task is started!
1213 struct GNUNET_SCHEDULER_Task *
1214 GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
1215 GNUNET_SCHEDULER_TaskCallback task,
1218 return GNUNET_SCHEDULER_add_at_with_priority (at,
1219 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1226 * Schedule a new task to be run with a specified delay. The task
1227 * will be scheduled for execution once the delay has expired. It
1228 * will be run with the DEFAULT priority.
1230 * @param delay when should this operation time out?
1231 * @param task main function of the task
1232 * @param task_cls closure of @a task
1233 * @return unique task identifier for the job
1234 * only valid until @a task is started!
1236 struct GNUNET_SCHEDULER_Task *
1237 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
1238 GNUNET_SCHEDULER_TaskCallback task,
1241 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1242 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1249 * Schedule a new task to be run as soon as possible. Note that this
1250 * does not guarantee that this will be the next task that is being
1251 * run, as other tasks with higher priority (or that are already ready
1252 * to run) might get to run first. Just as with delays, clients must
1253 * not rely on any particular order of execution between tasks
1254 * scheduled concurrently.
1256 * The task will be run with the DEFAULT priority.
1258 * @param task main function of the task
1259 * @param task_cls closure of @a task
1260 * @return unique task identifier for the job
1261 * only valid until @a task is started!
1263 struct GNUNET_SCHEDULER_Task *
1264 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
1267 return GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_ZERO,
1274 * Schedule a new task to be run on shutdown, that is when a CTRL-C
1275 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
1278 * @param task main function of the task
1279 * @param task_cls closure of @a task
1280 * @return unique task identifier for the job
1281 * only valid until @a task is started!
1283 struct GNUNET_SCHEDULER_Task *
1284 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
1287 struct GNUNET_SCHEDULER_Task *t;
1289 GNUNET_assert (NULL != active_task);
1290 GNUNET_assert (NULL != task);
1291 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1293 t->callback_cls = task_cls;
1297 t->start_time = GNUNET_TIME_absolute_get ();
1299 t->timeout = GNUNET_TIME_UNIT_FOREVER_ABS;
1300 t->priority = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
1301 t->on_shutdown = GNUNET_YES;
1302 t->lifeness = GNUNET_NO;
1303 GNUNET_CONTAINER_DLL_insert (shutdown_head,
1306 LOG (GNUNET_ERROR_TYPE_DEBUG,
1307 "Adding shutdown task %p\n",
1315 * Schedule a new task to be run as soon as possible with the
1316 * (transitive) ignore-shutdown flag either explicitly set or
1317 * explicitly enabled. This task (and all tasks created from it,
1318 * other than by another call to this function) will either count or
1319 * not count for the "lifeness" of the process. This API is only
1320 * useful in a few special cases.
1322 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
1323 * @param task main function of the task
1324 * @param task_cls closure of @a task
1325 * @return unique task identifier for the job
1326 * only valid until @a task is started!
1328 struct GNUNET_SCHEDULER_Task *
1329 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
1330 GNUNET_SCHEDULER_TaskCallback task,
1333 struct GNUNET_SCHEDULER_Task *ret;
1335 ret = GNUNET_SCHEDULER_add_now (task, task_cls);
1336 ret->lifeness = lifeness;
1343 * check a raw file descriptor and abort if it is bad (for debugging purposes)
1345 * @param t the task related to the file descriptor
1346 * @param raw_fd the raw file descriptor to check
1349 check_fd (struct GNUNET_SCHEDULER_Task *t, int raw_fd)
1353 int flags = fcntl (raw_fd, F_GETFD);
1355 if ((flags == -1) && (errno == EBADF))
1357 LOG (GNUNET_ERROR_TYPE_ERROR,
1358 "Got invalid file descriptor %d!\n",
1369 * Schedule a new task to be run with a specified delay or when any of
1370 * the specified file descriptor sets is ready. The delay can be used
1371 * as a timeout on the socket(s) being ready. The task will be
1372 * scheduled for execution once either the delay has expired or any of
1373 * the socket operations is ready. This is the most general
1374 * function of the "add" family. Note that the "prerequisite_task"
1375 * must be satisfied in addition to any of the other conditions. In
1376 * other words, the task will be started when
1378 * (prerequisite-run)
1384 * @param delay how long should we wait?
1385 * @param priority priority to use
1386 * @param rfd file descriptor we want to read (can be -1)
1387 * @param wfd file descriptors we want to write (can be -1)
1388 * @param task main function of the task
1389 * @param task_cls closure of @a task
1390 * @return unique task identifier for the job
1391 * only valid until @a task is started!
1394 static struct GNUNET_SCHEDULER_Task *
1395 add_without_sets (struct GNUNET_TIME_Relative delay,
1396 enum GNUNET_SCHEDULER_Priority priority,
1397 const struct GNUNET_NETWORK_Handle *read_nh,
1398 const struct GNUNET_NETWORK_Handle *write_nh,
1399 const struct GNUNET_DISK_FileHandle *read_fh,
1400 const struct GNUNET_DISK_FileHandle *write_fh,
1401 GNUNET_SCHEDULER_TaskCallback task,
1404 struct GNUNET_SCHEDULER_Task *t;
1406 GNUNET_assert (NULL != active_task);
1407 GNUNET_assert (NULL != task);
1408 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1419 t->callback_cls = task_cls;
1421 check_fd (t, NULL != read_nh ? GNUNET_NETWORK_get_fd (read_nh) : -1);
1422 check_fd (t, NULL != write_nh ? GNUNET_NETWORK_get_fd (write_nh) : -1);
1423 check_fd (t, NULL != read_fh ? read_fh->fd : -1);
1424 check_fd (t, NULL != write_fh ? write_fh->fd : -1);
1427 t->start_time = GNUNET_TIME_absolute_get ();
1429 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1430 t->priority = check_priority ((priority == GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority : priority);
1431 t->lifeness = current_lifeness;
1432 GNUNET_CONTAINER_DLL_insert (pending_head,
1435 driver_add_multiple (t);
1436 max_priority_added = GNUNET_MAX (max_priority_added,
1445 * Schedule a new task to be run with a specified delay or when the
1446 * specified file descriptor is ready for reading. The delay can be
1447 * used as a timeout on the socket being ready. The task will be
1448 * scheduled for execution once either the delay has expired or the
1449 * socket operation is ready. It will be run with the DEFAULT priority.
1450 * Only allowed to be called as long as the scheduler is running, that
1451 * is one of the following conditions is met:
1453 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1454 * - #GNUNET_SCHEDULER_driver_init has been run and
1455 * #GNUNET_SCHEDULER_driver_done has not been called yet
1457 * @param delay when should this operation time out?
1458 * @param rfd read file-descriptor
1459 * @param task main function of the task
1460 * @param task_cls closure of @a task
1461 * @return unique task identifier for the job
1462 * only valid until @a task is started!
1464 struct GNUNET_SCHEDULER_Task *
1465 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
1466 struct GNUNET_NETWORK_Handle *rfd,
1467 GNUNET_SCHEDULER_TaskCallback task,
1470 return GNUNET_SCHEDULER_add_read_net_with_priority (delay,
1471 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1472 rfd, task, task_cls);
1477 * Schedule a new task to be run with a specified priority and to be
1478 * run after the specified delay or when the specified file descriptor
1479 * is ready for reading. The delay can be used as a timeout on the
1480 * socket being ready. The task will be scheduled for execution once
1481 * either the delay has expired or the socket operation is ready. It
1482 * will be run with the DEFAULT priority.
1483 * Only allowed to be called as long as the scheduler is running, that
1484 * is one of the following conditions is met:
1486 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1487 * - #GNUNET_SCHEDULER_driver_init has been run and
1488 * #GNUNET_SCHEDULER_driver_done has not been called yet
1490 * @param delay when should this operation time out?
1491 * @param priority priority to use for the task
1492 * @param rfd read file-descriptor
1493 * @param task main function of the task
1494 * @param task_cls closure of @a task
1495 * @return unique task identifier for the job
1496 * only valid until @a task is started!
1498 struct GNUNET_SCHEDULER_Task *
1499 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
1500 enum GNUNET_SCHEDULER_Priority priority,
1501 struct GNUNET_NETWORK_Handle *rfd,
1502 GNUNET_SCHEDULER_TaskCallback task,
1505 return GNUNET_SCHEDULER_add_net_with_priority (delay, priority,
1514 * Schedule a new task to be run with a specified delay or when the
1515 * specified file descriptor is ready for writing. The delay can be
1516 * used as a timeout on the socket being ready. The task will be
1517 * scheduled for execution once either the delay has expired or the
1518 * socket operation is ready. It will be run with the priority of
1520 * Only allowed to be called as long as the scheduler is running, that
1521 * is one of the following conditions is met:
1523 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1524 * - #GNUNET_SCHEDULER_driver_init has been run and
1525 * #GNUNET_SCHEDULER_driver_done has not been called yet
1527 * @param delay when should this operation time out?
1528 * @param wfd write file-descriptor
1529 * @param task main function of the task
1530 * @param task_cls closure of @a task
1531 * @return unique task identifier for the job
1532 * only valid until @a task is started!
1534 struct GNUNET_SCHEDULER_Task *
1535 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
1536 struct GNUNET_NETWORK_Handle *wfd,
1537 GNUNET_SCHEDULER_TaskCallback task,
1540 return GNUNET_SCHEDULER_add_net_with_priority (delay,
1541 GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1543 GNUNET_NO, GNUNET_YES,
1548 * Schedule a new task to be run with a specified delay or when the
1549 * specified file descriptor is ready. The delay can be
1550 * used as a timeout on the socket being ready. The task will be
1551 * scheduled for execution once either the delay has expired or the
1552 * socket operation is ready.
1553 * Only allowed to be called as long as the scheduler is running, that
1554 * is one of the following conditions is met:
1556 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1557 * - #GNUNET_SCHEDULER_driver_init has been run and
1558 * #GNUNET_SCHEDULER_driver_done has not been called yet
1560 * @param delay when should this operation time out?
1561 * @param priority priority of the task
1562 * @param fd file-descriptor
1563 * @param on_read whether to poll the file-descriptor for readability
1564 * @param on_write whether to poll the file-descriptor for writability
1565 * @param task main function of the task
1566 * @param task_cls closure of task
1567 * @return unique task identifier for the job
1568 * only valid until "task" is started!
1570 struct GNUNET_SCHEDULER_Task *
1571 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
1572 enum GNUNET_SCHEDULER_Priority priority,
1573 struct GNUNET_NETWORK_Handle *fd,
1576 GNUNET_SCHEDULER_TaskCallback task,
1579 /* scheduler must be running */
1580 GNUNET_assert (NULL != scheduler_driver);
1583 struct GNUNET_NETWORK_FDSet *s;
1584 struct GNUNET_SCHEDULER_Task * ret;
1586 GNUNET_assert (NULL != fd);
1587 s = GNUNET_NETWORK_fdset_create ();
1588 GNUNET_NETWORK_fdset_set (s, fd);
1589 ret = GNUNET_SCHEDULER_add_select (
1592 on_write ? s : NULL,
1594 GNUNET_NETWORK_fdset_destroy (s);
1597 GNUNET_assert (on_read || on_write);
1598 GNUNET_assert (GNUNET_NETWORK_get_fd (fd) >= 0);
1599 return add_without_sets (delay, priority,
1600 on_read ? fd : NULL,
1601 on_write ? fd : NULL,
1610 * Schedule a new task to be run with a specified delay or when the
1611 * specified file descriptor is ready for reading. The delay can be
1612 * used as a timeout on the socket being ready. The task will be
1613 * scheduled for execution once either the delay has expired or the
1614 * socket operation is ready. It will be run with the DEFAULT priority.
1615 * Only allowed to be called as long as the scheduler is running, that
1616 * is one of the following conditions is met:
1618 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1619 * - #GNUNET_SCHEDULER_driver_init has been run and
1620 * #GNUNET_SCHEDULER_driver_done has not been called yet
1622 * @param delay when should this operation time out?
1623 * @param rfd read file-descriptor
1624 * @param task main function of the task
1625 * @param task_cls closure of @a task
1626 * @return unique task identifier for the job
1627 * only valid until @a task is started!
1629 struct GNUNET_SCHEDULER_Task *
1630 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
1631 const struct GNUNET_DISK_FileHandle *rfd,
1632 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1634 return GNUNET_SCHEDULER_add_file_with_priority (
1635 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1636 rfd, GNUNET_YES, GNUNET_NO,
1642 * Schedule a new task to be run with a specified delay or when the
1643 * specified file descriptor is ready for writing. The delay can be
1644 * used as a timeout on the socket being ready. The task will be
1645 * scheduled for execution once either the delay has expired or the
1646 * socket operation is ready. It will be run with the DEFAULT priority.
1647 * Only allowed to be called as long as the scheduler is running, that
1648 * is one of the following conditions is met:
1650 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1651 * - #GNUNET_SCHEDULER_driver_init has been run and
1652 * #GNUNET_SCHEDULER_driver_done has not been called yet
1654 * @param delay when should this operation time out?
1655 * @param wfd write file-descriptor
1656 * @param task main function of the task
1657 * @param task_cls closure of @a task
1658 * @return unique task identifier for the job
1659 * only valid until @a task is started!
1661 struct GNUNET_SCHEDULER_Task *
1662 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
1663 const struct GNUNET_DISK_FileHandle *wfd,
1664 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1666 return GNUNET_SCHEDULER_add_file_with_priority (
1667 delay, GNUNET_SCHEDULER_PRIORITY_DEFAULT,
1668 wfd, GNUNET_NO, GNUNET_YES,
1674 * Schedule a new task to be run with a specified delay or when the
1675 * specified file descriptor is ready. The delay can be
1676 * used as a timeout on the socket being ready. The task will be
1677 * scheduled for execution once either the delay has expired or the
1678 * socket operation is ready.
1679 * Only allowed to be called as long as the scheduler is running, that
1680 * is one of the following conditions is met:
1682 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1683 * - #GNUNET_SCHEDULER_driver_init has been run and
1684 * #GNUNET_SCHEDULER_driver_done has not been called yet
1686 * @param delay when should this operation time out?
1687 * @param priority priority of the task
1688 * @param fd file-descriptor
1689 * @param on_read whether to poll the file-descriptor for readability
1690 * @param on_write whether to poll the file-descriptor for writability
1691 * @param task main function of the task
1692 * @param task_cls closure of @a task
1693 * @return unique task identifier for the job
1694 * only valid until @a task is started!
1696 struct GNUNET_SCHEDULER_Task *
1697 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
1698 enum GNUNET_SCHEDULER_Priority priority,
1699 const struct GNUNET_DISK_FileHandle *fd,
1700 int on_read, int on_write,
1701 GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
1703 /* scheduler must be running */
1704 GNUNET_assert (NULL != scheduler_driver);
1707 struct GNUNET_NETWORK_FDSet *s;
1708 struct GNUNET_SCHEDULER_Task * ret;
1710 GNUNET_assert (NULL != fd);
1711 s = GNUNET_NETWORK_fdset_create ();
1712 GNUNET_NETWORK_fdset_handle_set (s, fd);
1713 ret = GNUNET_SCHEDULER_add_select (
1716 on_write ? s : NULL,
1718 GNUNET_NETWORK_fdset_destroy (s);
1721 GNUNET_assert (on_read || on_write);
1722 GNUNET_assert (fd->fd >= 0);
1723 return add_without_sets (delay, priority,
1726 on_read ? fd : NULL,
1727 on_write ? fd : NULL,
1734 extract_handles (const struct GNUNET_NETWORK_FDSet *fdset,
1735 const struct GNUNET_NETWORK_Handle ***ntarget,
1736 unsigned int *extracted_nhandles,
1737 const struct GNUNET_DISK_FileHandle ***ftarget,
1738 unsigned int *extracted_fhandles)
1740 // FIXME: this implementation only works for unix, for WIN32 the file handles
1741 // in fdset must be handled separately
1742 const struct GNUNET_NETWORK_Handle **nhandles;
1743 const struct GNUNET_DISK_FileHandle **fhandles;
1744 unsigned int nhandles_len;
1745 unsigned int fhandles_len;
1751 for (int sock = 0; sock != fdset->nsds; ++sock)
1753 if (GNUNET_YES == GNUNET_NETWORK_fdset_test_native (fdset, sock))
1755 struct GNUNET_NETWORK_Handle *nhandle;
1756 struct GNUNET_DISK_FileHandle *fhandle;
1758 nhandle = GNUNET_NETWORK_socket_box_native (sock);
1759 if (NULL != nhandle)
1761 GNUNET_array_append (nhandles, nhandles_len, nhandle);
1765 fhandle = GNUNET_DISK_get_handle_from_int_fd (sock);
1766 if (NULL != fhandle)
1768 GNUNET_array_append (fhandles, fhandles_len, fhandle);
1777 *ntarget = nhandles_len > 0 ? nhandles : NULL;
1778 *ftarget = fhandles_len > 0 ? fhandles : NULL;
1779 *extracted_nhandles = nhandles_len;
1780 *extracted_fhandles = fhandles_len;
1785 * Schedule a new task to be run with a specified delay or when any of
1786 * the specified file descriptor sets is ready. The delay can be used
1787 * as a timeout on the socket(s) being ready. The task will be
1788 * scheduled for execution once either the delay has expired or any of
1789 * the socket operations is ready. This is the most general
1790 * function of the "add" family. Note that the "prerequisite_task"
1791 * must be satisfied in addition to any of the other conditions. In
1792 * other words, the task will be started when
1794 * (prerequisite-run)
1797 * || any-ws-ready) )
1799 * Only allowed to be called as long as the scheduler is running, that
1800 * is one of the following conditions is met:
1802 * - #GNUNET_SCHEDULER_run has been called and has not returned yet
1803 * - #GNUNET_SCHEDULER_driver_init has been run and
1804 * #GNUNET_SCHEDULER_driver_done has not been called yet
1806 * @param prio how important is this task?
1807 * @param delay how long should we wait?
1808 * @param rs set of file descriptors we want to read (can be NULL)
1809 * @param ws set of file descriptors we want to write (can be NULL)
1810 * @param task main function of the task
1811 * @param task_cls closure of @a task
1812 * @return unique task identifier for the job
1813 * only valid until @a task is started!
1815 struct GNUNET_SCHEDULER_Task *
1816 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
1817 struct GNUNET_TIME_Relative delay,
1818 const struct GNUNET_NETWORK_FDSet *rs,
1819 const struct GNUNET_NETWORK_FDSet *ws,
1820 GNUNET_SCHEDULER_TaskCallback task,
1823 struct GNUNET_SCHEDULER_Task *t;
1824 const struct GNUNET_NETWORK_Handle **read_nhandles = NULL;
1825 const struct GNUNET_NETWORK_Handle **write_nhandles = NULL;
1826 const struct GNUNET_DISK_FileHandle **read_fhandles = NULL;
1827 const struct GNUNET_DISK_FileHandle **write_fhandles = NULL;
1828 unsigned int read_nhandles_len = 0;
1829 unsigned int write_nhandles_len = 0;
1830 unsigned int read_fhandles_len = 0;
1831 unsigned int write_fhandles_len = 0;
1833 /* scheduler must be running */
1834 GNUNET_assert (NULL != scheduler_driver);
1835 GNUNET_assert (NULL != active_task);
1836 GNUNET_assert (NULL != task);
1837 int no_rs = (NULL == rs);
1838 int no_ws = (NULL == ws);
1839 int empty_rs = (NULL != rs) && (0 == rs->nsds);
1840 int empty_ws = (NULL != ws) && (0 == ws->nsds);
1841 int no_fds = (no_rs && no_ws) ||
1842 (empty_rs && empty_ws) ||
1843 (no_rs && empty_ws) ||
1844 (no_ws && empty_rs);
1849 extract_handles (rs,
1853 &read_fhandles_len);
1857 extract_handles (ws,
1859 &write_nhandles_len,
1861 &write_fhandles_len);
1865 * here we consider the case that a GNUNET_NETWORK_FDSet might be empty
1866 * although its maximum FD number (nsds) is greater than 0. We handle
1867 * this case gracefully because some libraries such as libmicrohttpd
1868 * only provide a hint what the maximum FD number in an FD set might be
1869 * and not the exact FD number (see e.g. gnunet-rest-service.c)
1871 int no_fds_extracted = (0 == read_nhandles_len) &&
1872 (0 == read_fhandles_len) &&
1873 (0 == write_nhandles_len) &&
1874 (0 == write_fhandles_len);
1875 if (no_fds || no_fds_extracted)
1876 return GNUNET_SCHEDULER_add_delayed_with_priority (delay,
1880 t = GNUNET_new (struct GNUNET_SCHEDULER_Task);
1889 write_fhandles_len);
1891 t->callback_cls = task_cls;
1892 t->own_handles = GNUNET_YES;
1893 /* free the arrays of pointers to network / file handles, the actual
1894 * handles will be freed in destroy_task */
1895 GNUNET_array_grow (read_nhandles, read_nhandles_len, 0);
1896 GNUNET_array_grow (write_nhandles, write_nhandles_len, 0);
1897 GNUNET_array_grow (read_fhandles, read_fhandles_len, 0);
1898 GNUNET_array_grow (write_fhandles, write_fhandles_len, 0);
1900 t->start_time = GNUNET_TIME_absolute_get ();
1902 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1904 check_priority ((prio ==
1905 GNUNET_SCHEDULER_PRIORITY_KEEP) ? current_priority :
1907 t->lifeness = current_lifeness;
1908 GNUNET_CONTAINER_DLL_insert (pending_head,
1911 driver_add_multiple (t);
1912 max_priority_added = GNUNET_MAX (max_priority_added,
1914 LOG (GNUNET_ERROR_TYPE_DEBUG,
1923 * Function used by event-loop implementations to signal the scheduler
1924 * that a particular @a task is ready due to an event specified in the
1925 * et field of @a fdi.
1927 * This function will then queue the task to notify the application
1928 * that the task is ready (with the respective priority).
1930 * @param task the task that is ready
1931 * @param fdi information about the related FD
1934 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
1935 struct GNUNET_SCHEDULER_FdInfo *fdi)
1937 enum GNUNET_SCHEDULER_Reason reason;
1939 reason = task->reason;
1940 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
1941 (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et)) )
1942 reason |= GNUNET_SCHEDULER_REASON_READ_READY;
1943 if ( (0 == (reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
1944 (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et)) )
1945 reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
1946 reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
1947 task->reason = reason;
1948 if (GNUNET_NO == task->in_ready_list)
1950 GNUNET_CONTAINER_DLL_remove (pending_head,
1953 queue_ready_task (task);
1959 * Function called by external event loop implementations to tell the
1960 * scheduler to run some of the tasks that are ready. Must be called
1961 * only after #GNUNET_SCHEDULER_driver_init has been called and before
1962 * #GNUNET_SCHEDULER_driver_done is called.
1963 * This function may return even though there are tasks left to run
1964 * just to give other tasks a chance as well. If we return #GNUNET_YES,
1965 * the event loop implementation should call this function again as
1966 * soon as possible, while if we return #GNUNET_NO it must block until
1967 * either the operating system has more work (the scheduler has no more
1968 * work to do right now) or the timeout set by the scheduler (using the
1969 * set_wakeup callback) is reached.
1971 * @param sh scheduler handle that was returned by
1972 * #GNUNET_SCHEDULER_driver_init
1973 * @return #GNUNET_YES 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) #GNUNET_NO
1976 * if we are done running tasks (yield to block)
1979 GNUNET_SCHEDULER_do_work (struct GNUNET_SCHEDULER_Handle *sh)
1981 enum GNUNET_SCHEDULER_Priority p;
1982 struct GNUNET_SCHEDULER_Task *pos;
1983 struct GNUNET_TIME_Absolute now;
1985 /* check for tasks that reached the timeout! */
1986 now = GNUNET_TIME_absolute_get ();
1987 pos = pending_timeout_head;
1990 struct GNUNET_SCHEDULER_Task *next = pos->next;
1991 if (now.abs_value_us >= pos->timeout.abs_value_us)
1992 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
1993 if (0 == pos->reason)
1995 GNUNET_CONTAINER_DLL_remove (pending_timeout_head,
1996 pending_timeout_tail,
1998 if (pending_timeout_last == pos)
1999 pending_timeout_last = NULL;
2000 queue_ready_task (pos);
2006 struct GNUNET_SCHEDULER_Task *next = pos->next;
2007 if (now.abs_value_us >= pos->timeout.abs_value_us)
2009 pos->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
2010 GNUNET_CONTAINER_DLL_remove (pending_head,
2013 queue_ready_task (pos);
2018 if (0 == ready_count)
2020 struct GNUNET_TIME_Absolute timeout = get_timeout ();
2022 if (timeout.abs_value_us > now.abs_value_us)
2025 * The driver called this function before the current timeout was
2026 * reached (and no FD tasks are ready). This can happen in the
2027 * rare case when the system time is changed while the driver is
2028 * waiting for the timeout, so we handle this gracefully. It might
2029 * also be a programming error in the driver though.
2031 LOG (GNUNET_ERROR_TYPE_DEBUG,
2032 "GNUNET_SCHEDULER_do_work did not find any ready "
2033 "tasks and timeout has not been reached yet.\n");
2037 * the current timeout was reached but no ready tasks were found,
2038 * internal scheduler error!
2043 /* find out which task priority level we are going to
2044 process this time */
2045 max_priority_added = GNUNET_SCHEDULER_PRIORITY_KEEP;
2046 GNUNET_assert (NULL == ready_head[GNUNET_SCHEDULER_PRIORITY_KEEP]);
2047 /* yes, p>0 is correct, 0 is "KEEP" which should
2048 * always be an empty queue (see assertion)! */
2049 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
2051 pos = ready_head[p];
2055 GNUNET_assert (NULL != pos); /* ready_count wrong? */
2057 /* process all tasks at this priority level, then yield */
2058 while (NULL != (pos = ready_head[p]))
2060 GNUNET_CONTAINER_DLL_remove (ready_head[p],
2064 current_priority = pos->priority;
2065 current_lifeness = pos->lifeness;
2068 if (GNUNET_TIME_absolute_get_duration (pos->start_time).rel_value_us >
2069 DELAY_THRESHOLD.rel_value_us)
2071 LOG (GNUNET_ERROR_TYPE_DEBUG,
2072 "Task %p took %s to be scheduled\n",
2074 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (pos->start_time),
2078 tc.reason = pos->reason;
2079 GNUNET_NETWORK_fdset_zero (sh->rs);
2080 GNUNET_NETWORK_fdset_zero (sh->ws);
2081 // FIXME: do we have to remove FdInfos from fds if they are not ready?
2082 tc.fds_len = pos->fds_len;
2084 for (unsigned int i = 0; i != pos->fds_len; ++i)
2086 struct GNUNET_SCHEDULER_FdInfo *fdi = &pos->fds[i];
2087 if (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et))
2089 GNUNET_NETWORK_fdset_set_native (sh->rs,
2092 if (0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et))
2094 GNUNET_NETWORK_fdset_set_native (sh->ws,
2098 tc.read_ready = sh->rs;
2099 tc.write_ready = sh->ws;
2100 LOG (GNUNET_ERROR_TYPE_DEBUG,
2101 "Running task %p\n",
2103 GNUNET_assert (NULL != pos->callback);
2104 pos->callback (pos->callback_cls);
2105 if (NULL != pos->fds)
2107 int del_result = scheduler_driver->del (scheduler_driver->cls, pos);
2108 if (GNUNET_OK != del_result)
2110 LOG (GNUNET_ERROR_TYPE_ERROR,
2111 "driver could not delete task %p\n", pos);
2116 dump_backtrace (pos);
2119 shutdown_if_no_lifeness ();
2120 if (0 == ready_count)
2122 scheduler_driver->set_wakeup (scheduler_driver->cls,
2126 scheduler_driver->set_wakeup (scheduler_driver->cls,
2127 GNUNET_TIME_absolute_get ());
2133 * Function called by external event loop implementations to initialize
2134 * the scheduler. An external implementation has to provide @a driver
2135 * which contains callbacks for the scheduler (see definition of struct
2136 * #GNUNET_SCHEDULER_Driver). The callbacks are used to instruct the
2137 * external implementation to watch for events. If it detects any of
2138 * those events it is expected to call #GNUNET_SCHEDULER_do_work to let
2139 * the scheduler handle it. If an event is related to a specific task
2140 * (e.g. the scheduler gave instructions to watch a file descriptor),
2141 * the external implementation is expected to mark that task ready
2142 * before by calling #GNUNET_SCHEDULER_task_ready.
2144 * This function has to be called before any tasks are scheduled and
2145 * before GNUNET_SCHEDULER_do_work is called for the first time. It
2146 * allocates resources that have to be freed again by calling
2147 * #GNUNET_SCHEDULER_driver_done.
2149 * This function installs the same signal handlers as
2150 * #GNUNET_SCHEDULER_run. This means SIGTERM (and other similar signals)
2151 * will induce a call to #GNUNET_SCHEDULER_shutdown during the next
2152 * call to #GNUNET_SCHEDULER_do_work. As a result, SIGTERM causes all
2153 * active tasks to be scheduled with reason
2154 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added afterwards
2155 * will execute normally!). Note that any particular signal will only
2156 * shut down one scheduler; applications should always only create a
2159 * @param driver to use for the event loop
2160 * @return handle to be passed to #GNUNET_SCHEDULER_do_work and
2161 * #GNUNET_SCHEDULER_driver_done
2163 struct GNUNET_SCHEDULER_Handle *
2164 GNUNET_SCHEDULER_driver_init (const struct GNUNET_SCHEDULER_Driver *driver)
2166 struct GNUNET_SCHEDULER_Handle *sh;
2167 struct GNUNET_SCHEDULER_Task tsk;
2168 const struct GNUNET_DISK_FileHandle *pr;
2170 /* general set-up */
2171 GNUNET_assert (NULL == active_task);
2172 GNUNET_assert (NULL == shutdown_pipe_handle);
2173 sh = GNUNET_new (struct GNUNET_SCHEDULER_Handle);
2174 shutdown_pipe_handle = GNUNET_DISK_pipe (GNUNET_NO,
2178 GNUNET_assert (NULL != shutdown_pipe_handle);
2179 pr = GNUNET_DISK_pipe_handle (shutdown_pipe_handle,
2180 GNUNET_DISK_PIPE_END_READ);
2182 scheduler_driver = driver;
2184 /* install signal handlers */
2185 LOG (GNUNET_ERROR_TYPE_DEBUG,
2186 "Registering signal handlers\n");
2187 sh->shc_int = GNUNET_SIGNAL_handler_install (SIGINT,
2188 &sighandler_shutdown);
2189 sh->shc_term = GNUNET_SIGNAL_handler_install (SIGTERM,
2190 &sighandler_shutdown);
2191 #if (SIGTERM != GNUNET_TERM_SIG)
2192 sh->shc_gterm = GNUNET_SIGNAL_handler_install (GNUNET_TERM_SIG,
2193 &sighandler_shutdown);
2196 sh->shc_pipe = GNUNET_SIGNAL_handler_install (SIGPIPE,
2198 sh->shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT,
2199 &sighandler_shutdown);
2200 sh->shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP,
2201 &sighandler_shutdown);
2204 /* Setup initial tasks */
2205 current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
2206 current_lifeness = GNUNET_NO;
2211 install_parent_control_task =
2212 GNUNET_SCHEDULER_add_now (&install_parent_control_handler,
2214 shutdown_pipe_task =
2215 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_UNIT_FOREVER_REL,
2219 current_lifeness = GNUNET_YES;
2221 scheduler_driver->set_wakeup (scheduler_driver->cls,
2223 /* begin main event loop */
2224 sh->rs = GNUNET_NETWORK_fdset_create ();
2225 sh->ws = GNUNET_NETWORK_fdset_create ();
2226 GNUNET_NETWORK_fdset_handle_set (sh->rs, pr);
2232 * Counter-part of #GNUNET_SCHEDULER_driver_init. Has to be called
2233 * by external event loop implementations after the scheduler has
2234 * shut down. This is the case if both of the following conditions
2237 * - all tasks the scheduler has added through the driver's add
2238 * callback have been removed again through the driver's del
2240 * - the timeout the scheduler has set through the driver's
2241 * add_wakeup callback is FOREVER
2243 * @param sh the handle returned by #GNUNET_SCHEDULER_driver_init
2246 GNUNET_SCHEDULER_driver_done (struct GNUNET_SCHEDULER_Handle *sh)
2248 GNUNET_assert (NULL == pending_head);
2249 GNUNET_assert (NULL == pending_timeout_head);
2250 GNUNET_assert (NULL == shutdown_head);
2251 for (int i = 0; i != GNUNET_SCHEDULER_PRIORITY_COUNT; ++i)
2253 GNUNET_assert (NULL == ready_head[i]);
2255 GNUNET_NETWORK_fdset_destroy (sh->rs);
2256 GNUNET_NETWORK_fdset_destroy (sh->ws);
2258 /* uninstall signal handlers */
2259 GNUNET_SIGNAL_handler_uninstall (sh->shc_int);
2260 GNUNET_SIGNAL_handler_uninstall (sh->shc_term);
2261 #if (SIGTERM != GNUNET_TERM_SIG)
2262 GNUNET_SIGNAL_handler_uninstall (sh->shc_gterm);
2265 GNUNET_SIGNAL_handler_uninstall (sh->shc_pipe);
2266 GNUNET_SIGNAL_handler_uninstall (sh->shc_quit);
2267 GNUNET_SIGNAL_handler_uninstall (sh->shc_hup);
2269 GNUNET_DISK_pipe_close (shutdown_pipe_handle);
2270 shutdown_pipe_handle = NULL;
2271 scheduler_driver = NULL;
2277 select_loop (struct GNUNET_SCHEDULER_Handle *sh,
2278 struct DriverContext *context)
2280 struct GNUNET_NETWORK_FDSet *rs;
2281 struct GNUNET_NETWORK_FDSet *ws;
2284 GNUNET_assert (NULL != context);
2285 rs = GNUNET_NETWORK_fdset_create ();
2286 ws = GNUNET_NETWORK_fdset_create ();
2287 while ( (NULL != context->scheduled_head) ||
2288 (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us != context->timeout.abs_value_us) )
2290 LOG (GNUNET_ERROR_TYPE_DEBUG,
2291 "select timeout = %s\n",
2292 GNUNET_STRINGS_absolute_time_to_string (context->timeout));
2294 GNUNET_NETWORK_fdset_zero (rs);
2295 GNUNET_NETWORK_fdset_zero (ws);
2297 for (struct Scheduled *pos = context->scheduled_head;
2301 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et))
2303 GNUNET_NETWORK_fdset_set_native (rs, pos->fdi->sock);
2305 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et))
2307 GNUNET_NETWORK_fdset_set_native (ws, pos->fdi->sock);
2310 struct GNUNET_TIME_Relative time_remaining =
2311 GNUNET_TIME_absolute_get_remaining (context->timeout);
2312 if (NULL == scheduler_select)
2314 select_result = GNUNET_NETWORK_socket_select (rs,
2321 select_result = scheduler_select (scheduler_select_cls,
2327 if (select_result == GNUNET_SYSERR)
2332 LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR,
2344 if (0 != system (lsof))
2345 LOG_STRERROR (GNUNET_ERROR_TYPE_WARNING,
2350 for (struct Scheduled *s = context->scheduled_head;
2354 int flags = fcntl (s->fdi->sock,
2357 if ( (flags == -1) &&
2360 LOG (GNUNET_ERROR_TYPE_ERROR,
2361 "Got invalid file descriptor %d!\n",
2364 dump_backtrace (s->task);
2370 GNUNET_NETWORK_fdset_destroy (rs);
2371 GNUNET_NETWORK_fdset_destroy (ws);
2372 return GNUNET_SYSERR;
2374 if (select_result > 0)
2376 for (struct Scheduled *pos = context->scheduled_head;
2380 int is_ready = GNUNET_NO;
2382 if (0 != (GNUNET_SCHEDULER_ET_IN & pos->et) &&
2384 GNUNET_NETWORK_fdset_test_native (rs,
2387 pos->fdi->et |= GNUNET_SCHEDULER_ET_IN;
2388 is_ready = GNUNET_YES;
2390 if (0 != (GNUNET_SCHEDULER_ET_OUT & pos->et) &&
2392 GNUNET_NETWORK_fdset_test_native (ws,
2395 pos->fdi->et |= GNUNET_SCHEDULER_ET_OUT;
2396 is_ready = GNUNET_YES;
2398 if (GNUNET_YES == is_ready)
2400 GNUNET_SCHEDULER_task_ready (pos->task,
2405 if (GNUNET_YES == GNUNET_SCHEDULER_do_work (sh))
2407 LOG (GNUNET_ERROR_TYPE_DEBUG,
2408 "scheduler has more tasks ready!\n");
2411 GNUNET_NETWORK_fdset_destroy (rs);
2412 GNUNET_NETWORK_fdset_destroy (ws);
2418 select_add (void *cls,
2419 struct GNUNET_SCHEDULER_Task *task,
2420 struct GNUNET_SCHEDULER_FdInfo *fdi)
2422 struct DriverContext *context = cls;
2423 GNUNET_assert (NULL != context);
2424 GNUNET_assert (NULL != task);
2425 GNUNET_assert (NULL != fdi);
2426 GNUNET_assert (0 != (GNUNET_SCHEDULER_ET_IN & fdi->et) ||
2427 0 != (GNUNET_SCHEDULER_ET_OUT & fdi->et));
2429 if (!((NULL != fdi->fd) ^ (NULL != fdi->fh)) || (fdi->sock < 0))
2431 /* exactly one out of {fd, hf} must be != NULL and the OS handle must be valid */
2432 return GNUNET_SYSERR;
2435 struct Scheduled *scheduled = GNUNET_new (struct Scheduled);
2436 scheduled->task = task;
2437 scheduled->fdi = fdi;
2438 scheduled->et = fdi->et;
2440 GNUNET_CONTAINER_DLL_insert (context->scheduled_head,
2441 context->scheduled_tail,
2448 select_del (void *cls,
2449 struct GNUNET_SCHEDULER_Task *task)
2451 struct DriverContext *context;
2452 struct Scheduled *pos;
2455 GNUNET_assert (NULL != cls);
2458 ret = GNUNET_SYSERR;
2459 pos = context->scheduled_head;
2462 struct Scheduled *next = pos->next;
2463 if (pos->task == task)
2465 GNUNET_CONTAINER_DLL_remove (context->scheduled_head,
2466 context->scheduled_tail,
2478 select_set_wakeup (void *cls,
2479 struct GNUNET_TIME_Absolute dt)
2481 struct DriverContext *context = cls;
2483 GNUNET_assert (NULL != context);
2484 context->timeout = dt;
2489 * Obtain the driver for using select() as the event loop.
2491 * @return NULL on error
2493 struct GNUNET_SCHEDULER_Driver *
2494 GNUNET_SCHEDULER_driver_select ()
2496 struct GNUNET_SCHEDULER_Driver *select_driver;
2497 select_driver = GNUNET_new (struct GNUNET_SCHEDULER_Driver);
2499 select_driver->add = &select_add;
2500 select_driver->del = &select_del;
2501 select_driver->set_wakeup = &select_set_wakeup;
2503 return select_driver;
2507 /* end of scheduler.c */