2 This file is part of GNUnet
3 (C) 2009 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 2, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file util/scheduler.c
23 * @brief schedule computations using continuation passing style
24 * @author Christian Grothoff
27 #include "gnunet_common.h"
28 #include "gnunet_scheduler_lib.h"
29 #include "gnunet_signal_lib.h"
30 #include "gnunet_time_lib.h"
32 #define DEBUG_TASKS GNUNET_NO
35 * Linked list of pending tasks.
40 * This is a linked list.
45 * Function to run when ready.
47 GNUNET_SCHEDULER_Task callback;
50 * Closure for the callback.
55 * Set of file descriptors this task is waiting
56 * for for reading. Once ready, this is updated
57 * to reflect the set of file descriptors ready
60 struct GNUNET_NETWORK_FDSet *read_set;
63 * Set of file descriptors this task is waiting
64 * for for writing. Once ready, this is updated
65 * to reflect the set of file descriptors ready
68 struct GNUNET_NETWORK_FDSet *write_set;
71 * Unique task identifier.
73 GNUNET_SCHEDULER_TaskIdentifier id;
76 * Identifier of a prerequisite task.
78 GNUNET_SCHEDULER_TaskIdentifier prereq_id;
81 * Absolute timeout value for the task, or
82 * GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
84 struct GNUNET_TIME_Absolute timeout;
87 * Why is the task ready? Set after task is added to ready queue.
88 * Initially set to zero. All reasons that have already been
89 * satisfied (i.e. read or write ready) will be set over time.
91 enum GNUNET_SCHEDULER_Reason reason;
96 enum GNUNET_SCHEDULER_Priority priority;
102 * Handle for the scheduling service.
104 struct GNUNET_SCHEDULER_Handle
108 * List of tasks waiting for an event.
110 struct Task *pending;
113 * List of tasks ready to run right now,
114 * grouped by importance.
116 struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
119 * Identity of the last task queued. Incremented for each task to
120 * generate a unique task ID (it is virtually impossible to start
121 * more than 2^64 tasks during the lifetime of a process).
123 GNUNET_SCHEDULER_TaskIdentifier last_id;
126 * Highest number so that all tasks with smaller identifiers
127 * have already completed. Also the lowest number of a task
128 * still waiting to be executed.
130 GNUNET_SCHEDULER_TaskIdentifier lowest_pending_id;
133 * Number of tasks on the ready list.
135 unsigned int ready_count;
138 * How many tasks have we run so far?
140 unsigned long long tasks_run;
143 * Priority of the task running right now. Only
144 * valid while a task is running.
146 enum GNUNET_SCHEDULER_Priority current_priority;
152 * Check that the given priority is legal (and return it).
154 * @param p priority value to check
155 * @return p on success, 0 on error
157 static enum GNUNET_SCHEDULER_Priority
158 check_priority (enum GNUNET_SCHEDULER_Priority p)
160 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
163 return 0; /* make compiler happy */
168 * Is a task with this identifier still pending? Also updates
169 * "lowest_pending_id" as a side-effect (for faster checks in the
170 * future), but only if the return value is "GNUNET_NO" (and
171 * the "lowest_pending_id" check failed).
173 * @param sched the scheduler
174 * @param id which task are we checking for
175 * @return GNUNET_YES if so, GNUNET_NO if not
178 is_pending (struct GNUNET_SCHEDULER_Handle *sched,
179 GNUNET_SCHEDULER_TaskIdentifier id)
182 enum GNUNET_SCHEDULER_Priority p;
183 GNUNET_SCHEDULER_TaskIdentifier min;
185 if (id < sched->lowest_pending_id)
187 min = -1; /* maximum value */
188 pos = sched->pending;
197 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
199 pos = sched->ready[p];
209 sched->lowest_pending_id = min;
215 * Update all sets and timeout for select.
217 * @param sched the scheduler
218 * @param rs read-set, set to all FDs we would like to read (updated)
219 * @param ws write-set, set to all FDs we would like to write (updated)
220 * @param timeout next timeout (updated)
223 update_sets (struct GNUNET_SCHEDULER_Handle *sched,
224 struct GNUNET_NETWORK_FDSet *rs,
225 struct GNUNET_NETWORK_FDSet *ws,
226 struct GNUNET_TIME_Relative *timeout)
230 pos = sched->pending;
233 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
234 (GNUNET_YES == is_pending (sched, pos->prereq_id)))
240 if (pos->timeout.value != GNUNET_TIME_UNIT_FOREVER_ABS.value)
242 struct GNUNET_TIME_Relative to;
244 to = GNUNET_TIME_absolute_get_remaining (pos->timeout);
245 if (timeout->value > to.value)
248 if (pos->read_set != NULL)
249 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
250 if (pos->write_set != NULL)
251 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
252 if (pos->reason != 0)
253 *timeout = GNUNET_TIME_UNIT_ZERO;
260 * Check if the ready set overlaps with the set we want to have ready.
261 * If so, update the want set (set all FDs that are ready). If not,
264 * @param ready set that is ready
265 * @param want set that we want to be ready
266 * @return GNUNET_YES if there was some overlap
269 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
270 struct GNUNET_NETWORK_FDSet *want)
274 if (GNUNET_NETWORK_fdset_overlap (ready, want))
276 /* copy all over (yes, there maybe unrelated bits,
277 but this should not hurt well-written clients) */
278 GNUNET_NETWORK_fdset_copy (want, ready);
286 * Check if the given task is eligible to run now.
287 * Also set the reason why it is eligible.
289 * @param sched the scheduler
290 * @param task task to check if it is ready
291 * @param now the current time
292 * @param rs set of FDs ready for reading
293 * @param ws set of FDs ready for writing
294 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
297 is_ready (struct GNUNET_SCHEDULER_Handle *sched,
299 struct GNUNET_TIME_Absolute now,
300 const struct GNUNET_NETWORK_FDSet *rs,
301 const struct GNUNET_NETWORK_FDSet *ws)
303 if (now.value >= task->timeout.value)
304 task->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
305 if ((0 == (task->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
306 (rs != NULL) && (set_overlaps (rs, task->read_set)))
307 task->reason |= GNUNET_SCHEDULER_REASON_READ_READY;
308 if ((0 == (task->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
309 (ws != NULL) && (set_overlaps (ws, task->write_set)))
310 task->reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
311 if (task->reason == 0)
312 return GNUNET_NO; /* not ready */
313 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
315 if (GNUNET_YES == is_pending (sched, task->prereq_id))
316 return GNUNET_NO; /* prereq waiting */
317 task->reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
324 * Put a task that is ready for execution into the ready queue.
326 * @param handle the scheduler
327 * @param task task ready for execution
330 queue_ready_task (struct GNUNET_SCHEDULER_Handle *handle, struct Task *task)
332 task->next = handle->ready[check_priority (task->priority)];
333 handle->ready[check_priority (task->priority)] = task;
334 handle->ready_count++;
339 * Check which tasks are ready and move them
340 * to the respective ready queue.
342 * @param handle the scheduler
343 * @param rs FDs ready for reading
344 * @param ws FDs ready for writing
347 check_ready (struct GNUNET_SCHEDULER_Handle *handle,
348 const struct GNUNET_NETWORK_FDSet *rs,
349 const struct GNUNET_NETWORK_FDSet *ws)
354 struct GNUNET_TIME_Absolute now;
356 now = GNUNET_TIME_absolute_get ();
358 pos = handle->pending;
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363 "Checking readyness of task: %llu / %p\n",
364 pos->id, pos->callback_cls);
367 if (GNUNET_YES == is_ready (handle, pos, now, rs, ws))
370 handle->pending = next;
373 queue_ready_task (handle, pos);
384 * Request the shutdown of a scheduler. Marks all currently
385 * pending tasks as ready because of shutdown. This will
386 * cause all tasks to run (as soon as possible, respecting
387 * priorities and prerequisite tasks). Note that tasks
388 * scheduled AFTER this call may still be delayed arbitrarily.
390 * @param sched the scheduler
393 GNUNET_SCHEDULER_shutdown (struct GNUNET_SCHEDULER_Handle *sched)
397 pos = sched->pending;
400 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
401 /* we don't move the task into the ready queue yet; check_ready
402 will do that later, possibly adding additional
410 * Destroy a task (release associated resources)
412 * @param t task to destroy
415 destroy_task (struct Task *t)
417 if (NULL != t->read_set)
418 GNUNET_NETWORK_fdset_destroy (t->read_set);
419 if (NULL != t->write_set)
420 GNUNET_NETWORK_fdset_destroy (t->write_set);
426 * Run at least one task in the highest-priority queue that is not
427 * empty. Keep running tasks until we are either no longer running
428 * "URGENT" tasks or until we have at least one "pending" task (which
429 * may become ready, hence we should select on it). Naturally, if
430 * there are no more ready tasks, we also return.
432 * @param sched the scheduler
435 run_ready (struct GNUNET_SCHEDULER_Handle *sched)
437 enum GNUNET_SCHEDULER_Priority p;
439 struct GNUNET_SCHEDULER_TaskContext tc;
443 if (sched->ready_count == 0)
445 GNUNET_assert (sched->ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
446 /* yes, p>0 is correct, 0 is "KEEP" which should
447 always be an empty queue (see assertion)! */
448 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
450 pos = sched->ready[p];
454 GNUNET_assert (pos != NULL); /* ready_count wrong? */
455 sched->ready[p] = pos->next;
456 sched->ready_count--;
457 sched->current_priority = p;
458 GNUNET_assert (pos->priority == p);
460 tc.reason = pos->reason;
461 tc.read_ready = pos->read_set;
462 tc.write_ready = pos->write_set;
463 pos->callback (pos->callback_cls, &tc);
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
466 "Running task: %llu / %p\n", pos->id, pos->callback_cls);
471 while ((sched->pending == NULL) || (p == GNUNET_SCHEDULER_PRIORITY_URGENT));
476 * Pipe used to communicate shutdown via signal.
478 static struct GNUNET_DISK_PipeHandle *sigpipe;
482 * Signal handler called for signals that should cause us to shutdown.
485 sighandler_shutdown ()
489 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
490 (sigpipe, GNUNET_DISK_PIPE_END_WRITE), &c,
497 * Initialize and run scheduler. This function will return when all
498 * tasks have completed. On systems with signals, receiving a SIGTERM
499 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
500 * to be run after the active task is complete. As a result, SIGTERM
501 * causes all active tasks to be scheduled with reason
502 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
503 * afterwards will execute normally!). Note that any particular signal
504 * will only shut down one scheduler; applications should always only
505 * create a single scheduler.
507 * @param task task to run immediately
508 * @param task_cls closure of task
511 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
513 struct GNUNET_SCHEDULER_Handle sched;
514 struct GNUNET_NETWORK_FDSet *rs;
515 struct GNUNET_NETWORK_FDSet *ws;
516 struct GNUNET_TIME_Relative timeout;
518 struct GNUNET_SIGNAL_Context *shc_int;
519 struct GNUNET_SIGNAL_Context *shc_term;
520 struct GNUNET_SIGNAL_Context *shc_quit;
521 struct GNUNET_SIGNAL_Context *shc_hup;
522 unsigned long long last_tr;
523 unsigned int busy_wait_warning;
525 const struct GNUNET_DISK_FileHandle *pr;
529 rs = GNUNET_NETWORK_fdset_create ();
530 ws = GNUNET_NETWORK_fdset_create ();
532 GNUNET_assert (sigpipe == NULL);
533 sigpipe = GNUNET_DISK_pipe (GNUNET_NO);
534 GNUNET_assert (sigpipe != NULL);
535 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
536 GNUNET_assert (pr != NULL);
537 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
538 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
539 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
540 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
542 memset (&sched, 0, sizeof (sched));
543 sched.current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
544 GNUNET_SCHEDULER_add_continuation (&sched,
547 GNUNET_SCHEDULER_REASON_STARTUP);
549 busy_wait_warning = 0;
550 while ( (sched.pending != NULL) || (sched.ready_count > 0) )
552 GNUNET_NETWORK_fdset_zero (rs);
553 GNUNET_NETWORK_fdset_zero (ws);
554 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
555 update_sets (&sched, rs, ws, &timeout);
556 GNUNET_NETWORK_fdset_handle_set (rs, pr);
557 if (sched.ready_count > 0)
559 /* no blocking, more work already ready! */
560 timeout = GNUNET_TIME_UNIT_ZERO;
562 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
564 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
566 /* consume the signal */
567 GNUNET_DISK_file_read (pr, &c, sizeof(c));
568 /* mark all active tasks as ready due to shutdown */
569 GNUNET_SCHEDULER_shutdown (&sched);
572 if (last_tr == sched.tasks_run)
578 last_tr = sched.tasks_run;
579 busy_wait_warning = 0;
581 if ((ret == 0) && (timeout.value == 0) && (busy_wait_warning > 16))
583 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
584 _("Looks like we're busy waiting...\n"));
585 sleep (1); /* mitigate */
587 if (ret == GNUNET_SYSERR)
591 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
594 check_ready (&sched, rs, ws);
598 GNUNET_SIGNAL_handler_uninstall (shc_int);
599 GNUNET_SIGNAL_handler_uninstall (shc_term);
600 GNUNET_SIGNAL_handler_uninstall (shc_quit);
601 GNUNET_SIGNAL_handler_uninstall (shc_hup);
602 GNUNET_DISK_pipe_close (sigpipe);
605 GNUNET_NETWORK_fdset_destroy (rs);
606 GNUNET_NETWORK_fdset_destroy (ws);
612 * Get information about the current load of this scheduler. Use this
613 * function to determine if an elective task should be added or simply
614 * dropped (if the decision should be made based on the number of
615 * tasks ready to run).
617 * @param sched scheduler to query
618 * @param p priority level to look at
619 * @return number of tasks pending right now
622 GNUNET_SCHEDULER_get_load (struct GNUNET_SCHEDULER_Handle *sched,
623 enum GNUNET_SCHEDULER_Priority p)
628 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
629 return sched->ready_count;
630 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
631 p = sched->current_priority;
633 pos = sched->ready[p];
644 * Cancel the task with the specified identifier.
645 * The task must not yet have run.
647 * @param sched scheduler to use
648 * @param task id of the task to cancel
649 * @return original closure of the task
652 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Handle *sched,
653 GNUNET_SCHEDULER_TaskIdentifier task)
657 enum GNUNET_SCHEDULER_Priority p;
673 GNUNET_assert (p < GNUNET_SCHEDULER_PRIORITY_COUNT);
680 sched->ready_count--;
690 sched->pending = t->next;
692 sched->ready[p] = t->next;
695 prev->next = t->next;
696 ret = t->callback_cls;
698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
699 "Canceling task: %llu / %p\n", task, t->callback_cls);
707 * Continue the current execution with the given function. This is
708 * similar to the other "add" functions except that there is no delay
709 * and the reason code can be specified.
711 * @param sched scheduler to use
712 * @param task main function of the task
713 * @param task_cls closure for 'main'
714 * @param reason reason for task invocation
717 GNUNET_SCHEDULER_add_continuation (struct GNUNET_SCHEDULER_Handle *sched,
718 GNUNET_SCHEDULER_Task task,
720 enum GNUNET_SCHEDULER_Reason reason)
724 t = GNUNET_malloc (sizeof (struct Task));
726 t->callback_cls = task_cls;
727 t->id = ++sched->last_id;
729 t->priority = sched->current_priority;
731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
732 "Adding continuation task: %llu / %p\n",
733 t->id, t->callback_cls);
735 queue_ready_task (sched, t);
741 * Schedule a new task to be run after the specified prerequisite task
742 * has completed. It will be run with the priority of the calling
745 * @param sched scheduler to use
746 * @param prerequisite_task run this task after the task with the given
747 * task identifier completes (and any of our other
748 * conditions, such as delay, read or write-readyness
749 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
750 * on completion of other tasks (this will cause the task to run as
752 * @param task main function of the task
753 * @param task_cls closure of task
754 * @return unique task identifier for the job
755 * only valid until "task" is started!
757 GNUNET_SCHEDULER_TaskIdentifier
758 GNUNET_SCHEDULER_add_after (struct GNUNET_SCHEDULER_Handle *sched,
759 GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
760 GNUNET_SCHEDULER_Task task,
763 return GNUNET_SCHEDULER_add_select (sched,
764 GNUNET_SCHEDULER_PRIORITY_KEEP,
766 GNUNET_TIME_UNIT_ZERO,
767 NULL, NULL, task, task_cls);
772 * Schedule a new task to be run with a specified priority.
774 * @param sched scheduler to use
775 * @param prio how important is the new task?
776 * @param task main function of the task
777 * @param task_cls closure of task
778 * @return unique task identifier for the job
779 * only valid until "task" is started!
781 GNUNET_SCHEDULER_TaskIdentifier
782 GNUNET_SCHEDULER_add_with_priority (struct GNUNET_SCHEDULER_Handle *sched,
783 enum GNUNET_SCHEDULER_Priority prio,
784 GNUNET_SCHEDULER_Task task,
787 return GNUNET_SCHEDULER_add_select (sched,
789 GNUNET_SCHEDULER_NO_TASK,
790 GNUNET_TIME_UNIT_ZERO,
791 NULL, NULL, task, task_cls);
797 * Schedule a new task to be run with a specified delay. The task
798 * will be scheduled for execution once the delay has expired. It
799 * will be run with the priority of the calling task.
801 * @param sched scheduler to use
802 * @param delay when should this operation time out? Use
803 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
804 * @param task main function of the task
805 * @param task_cls closure of task
806 * @return unique task identifier for the job
807 * only valid until "task" is started!
809 GNUNET_SCHEDULER_TaskIdentifier
810 GNUNET_SCHEDULER_add_delayed (struct GNUNET_SCHEDULER_Handle *sched,
811 struct GNUNET_TIME_Relative delay,
812 GNUNET_SCHEDULER_Task task,
815 return GNUNET_SCHEDULER_add_select (sched,
816 GNUNET_SCHEDULER_PRIORITY_KEEP,
817 GNUNET_SCHEDULER_NO_TASK, delay,
818 NULL, NULL, task, task_cls);
823 * Schedule a new task to be run with a specified delay or when the
824 * specified file descriptor is ready for reading. The delay can be
825 * used as a timeout on the socket being ready. The task will be
826 * scheduled for execution once either the delay has expired or the
827 * socket operation is ready. It will be run with the priority of
830 * @param sched scheduler to use
831 * @param delay when should this operation time out? Use
832 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
833 * @param rfd read file-descriptor
834 * @param task main function of the task
835 * @param task_cls closure of task
836 * @return unique task identifier for the job
837 * only valid until "task" is started!
839 GNUNET_SCHEDULER_TaskIdentifier
840 GNUNET_SCHEDULER_add_read_net (struct GNUNET_SCHEDULER_Handle *sched,
841 struct GNUNET_TIME_Relative delay,
842 struct GNUNET_NETWORK_Handle *rfd,
843 GNUNET_SCHEDULER_Task task,
846 struct GNUNET_NETWORK_FDSet *rs;
847 GNUNET_SCHEDULER_TaskIdentifier ret;
849 GNUNET_assert (rfd != NULL);
850 rs = GNUNET_NETWORK_fdset_create ();
851 GNUNET_NETWORK_fdset_set (rs, rfd);
852 ret = GNUNET_SCHEDULER_add_select (sched,
853 GNUNET_SCHEDULER_PRIORITY_KEEP,
854 GNUNET_SCHEDULER_NO_TASK,
856 rs, NULL, task, task_cls);
857 GNUNET_NETWORK_fdset_destroy (rs);
863 * Schedule a new task to be run with a specified delay or when the
864 * specified file descriptor is ready for writing. The delay can be
865 * used as a timeout on the socket being ready. The task will be
866 * scheduled for execution once either the delay has expired or the
867 * socket operation is ready. It will be run with the priority of
870 * @param sched scheduler to use
871 * @param delay when should this operation time out? Use
872 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
873 * @param wfd write file-descriptor
874 * @param task main function of the task
875 * @param task_cls closure of task
876 * @return unique task identifier for the job
877 * only valid until "task" is started!
879 GNUNET_SCHEDULER_TaskIdentifier
880 GNUNET_SCHEDULER_add_write_net (struct GNUNET_SCHEDULER_Handle *sched,
881 struct GNUNET_TIME_Relative delay,
882 struct GNUNET_NETWORK_Handle *wfd,
883 GNUNET_SCHEDULER_Task task,
886 struct GNUNET_NETWORK_FDSet *ws;
887 GNUNET_SCHEDULER_TaskIdentifier ret;
889 GNUNET_assert (wfd != NULL);
890 ws = GNUNET_NETWORK_fdset_create ();
891 GNUNET_NETWORK_fdset_set (ws, wfd);
892 ret = GNUNET_SCHEDULER_add_select (sched,
893 GNUNET_SCHEDULER_PRIORITY_KEEP,
894 GNUNET_SCHEDULER_NO_TASK, delay,
895 NULL, ws, task, task_cls);
896 GNUNET_NETWORK_fdset_destroy (ws);
902 * Schedule a new task to be run with a specified delay or when the
903 * specified file descriptor is ready for reading. The delay can be
904 * used as a timeout on the socket being ready. The task will be
905 * scheduled for execution once either the delay has expired or the
906 * socket operation is ready. It will be run with the priority of
909 * @param sched scheduler to use
910 * @param delay when should this operation time out? Use
911 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
912 * @param rfd read file-descriptor
913 * @param task main function of the task
914 * @param task_cls closure of task
915 * @return unique task identifier for the job
916 * only valid until "task" is started!
918 GNUNET_SCHEDULER_TaskIdentifier
919 GNUNET_SCHEDULER_add_read_file (struct GNUNET_SCHEDULER_Handle *sched,
920 struct GNUNET_TIME_Relative delay,
921 const struct GNUNET_DISK_FileHandle *rfd,
922 GNUNET_SCHEDULER_Task task,
925 struct GNUNET_NETWORK_FDSet *rs;
926 GNUNET_SCHEDULER_TaskIdentifier ret;
928 GNUNET_assert (rfd != NULL);
929 rs = GNUNET_NETWORK_fdset_create ();
930 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
931 ret = GNUNET_SCHEDULER_add_select (sched,
932 GNUNET_SCHEDULER_PRIORITY_KEEP,
933 GNUNET_SCHEDULER_NO_TASK, delay,
934 rs, NULL, task, task_cls);
935 GNUNET_NETWORK_fdset_destroy (rs);
941 * Schedule a new task to be run with a specified delay or when the
942 * specified file descriptor is ready for writing. The delay can be
943 * used as a timeout on the socket being ready. The task will be
944 * scheduled for execution once either the delay has expired or the
945 * socket operation is ready. It will be run with the priority of
948 * @param sched scheduler to use
949 * @param delay when should this operation time out? Use
950 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
951 * @param wfd write file-descriptor
952 * @param task main function of the task
953 * @param task_cls closure of task
954 * @return unique task identifier for the job
955 * only valid until "task" is started!
957 GNUNET_SCHEDULER_TaskIdentifier
958 GNUNET_SCHEDULER_add_write_file (struct GNUNET_SCHEDULER_Handle *sched,
959 struct GNUNET_TIME_Relative delay,
960 const struct GNUNET_DISK_FileHandle *wfd,
961 GNUNET_SCHEDULER_Task task,
964 struct GNUNET_NETWORK_FDSet *ws;
965 GNUNET_SCHEDULER_TaskIdentifier ret;
967 GNUNET_assert (wfd != NULL);
968 ws = GNUNET_NETWORK_fdset_create ();
969 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
970 ret = GNUNET_SCHEDULER_add_select (sched,
971 GNUNET_SCHEDULER_PRIORITY_KEEP,
972 GNUNET_SCHEDULER_NO_TASK,
974 NULL, ws, task, task_cls);
975 GNUNET_NETWORK_fdset_destroy (ws);
982 * Schedule a new task to be run with a specified delay or when any of
983 * the specified file descriptor sets is ready. The delay can be used
984 * as a timeout on the socket(s) being ready. The task will be
985 * scheduled for execution once either the delay has expired or any of
986 * the socket operations is ready. This is the most general
987 * function of the "add" family. Note that the "prerequisite_task"
988 * must be satisfied in addition to any of the other conditions. In
989 * other words, the task will be started when
995 * || (shutdown-active && run-on-shutdown) )
998 * @param sched scheduler to use
999 * @param prio how important is this task?
1000 * @param prerequisite_task run this task after the task with the given
1001 * task identifier completes (and any of our other
1002 * conditions, such as delay, read or write-readyness
1003 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1004 * on completion of other tasks.
1005 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1006 * which means that the task will only be run after we receive SIGTERM
1007 * @param rs set of file descriptors we want to read (can be NULL)
1008 * @param ws set of file descriptors we want to write (can be NULL)
1009 * @param task main function of the task
1010 * @param task_cls closure of task
1011 * @return unique task identifier for the job
1012 * only valid until "task" is started!
1014 GNUNET_SCHEDULER_TaskIdentifier
1015 GNUNET_SCHEDULER_add_select (struct GNUNET_SCHEDULER_Handle *sched,
1016 enum GNUNET_SCHEDULER_Priority prio,
1017 GNUNET_SCHEDULER_TaskIdentifier
1019 struct GNUNET_TIME_Relative delay,
1020 const struct GNUNET_NETWORK_FDSet * rs,
1021 const struct GNUNET_NETWORK_FDSet * ws,
1022 GNUNET_SCHEDULER_Task task,
1027 t = GNUNET_malloc (sizeof (struct Task));
1029 t->callback_cls = task_cls;
1032 t->read_set = GNUNET_NETWORK_fdset_create ();
1033 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1037 t->write_set = GNUNET_NETWORK_fdset_create ();
1038 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1040 t->id = ++sched->last_id;
1041 t->prereq_id = prerequisite_task;
1042 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1044 check_priority ((prio ==
1045 GNUNET_SCHEDULER_PRIORITY_KEEP) ? sched->current_priority
1047 t->next = sched->pending;
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1051 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1056 /* end of scheduler.c */