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 * ID of the task that is running right now.
115 struct Task *active_task;
118 * List of tasks ready to run right now,
119 * grouped by importance.
121 struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
124 * Identity of the last task queued. Incremented for each task to
125 * generate a unique task ID (it is virtually impossible to start
126 * more than 2^64 tasks during the lifetime of a process).
128 GNUNET_SCHEDULER_TaskIdentifier last_id;
131 * Highest number so that all tasks with smaller identifiers
132 * have already completed. Also the lowest number of a task
133 * still waiting to be executed.
135 GNUNET_SCHEDULER_TaskIdentifier lowest_pending_id;
138 * Number of tasks on the ready list.
140 unsigned int ready_count;
143 * How many tasks have we run so far?
145 unsigned long long tasks_run;
148 * Priority of the task running right now. Only
149 * valid while a task is running.
151 enum GNUNET_SCHEDULER_Priority current_priority;
157 * Check that the given priority is legal (and return it).
159 * @param p priority value to check
160 * @return p on success, 0 on error
162 static enum GNUNET_SCHEDULER_Priority
163 check_priority (enum GNUNET_SCHEDULER_Priority p)
165 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
168 return 0; /* make compiler happy */
173 * Is a task with this identifier still pending? Also updates
174 * "lowest_pending_id" as a side-effect (for faster checks in the
175 * future), but only if the return value is "GNUNET_NO" (and
176 * the "lowest_pending_id" check failed).
178 * @param sched the scheduler
179 * @param id which task are we checking for
180 * @return GNUNET_YES if so, GNUNET_NO if not
183 is_pending (struct GNUNET_SCHEDULER_Handle *sched,
184 GNUNET_SCHEDULER_TaskIdentifier id)
187 enum GNUNET_SCHEDULER_Priority p;
188 GNUNET_SCHEDULER_TaskIdentifier min;
190 if (id < sched->lowest_pending_id)
192 min = -1; /* maximum value */
193 pos = sched->pending;
202 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
204 pos = sched->ready[p];
214 sched->lowest_pending_id = min;
220 * Update all sets and timeout for select.
222 * @param sched the scheduler
223 * @param rs read-set, set to all FDs we would like to read (updated)
224 * @param ws write-set, set to all FDs we would like to write (updated)
225 * @param timeout next timeout (updated)
228 update_sets (struct GNUNET_SCHEDULER_Handle *sched,
229 struct GNUNET_NETWORK_FDSet *rs,
230 struct GNUNET_NETWORK_FDSet *ws,
231 struct GNUNET_TIME_Relative *timeout)
235 pos = sched->pending;
238 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
239 (GNUNET_YES == is_pending (sched, pos->prereq_id)))
245 if (pos->timeout.value != GNUNET_TIME_UNIT_FOREVER_ABS.value)
247 struct GNUNET_TIME_Relative to;
249 to = GNUNET_TIME_absolute_get_remaining (pos->timeout);
250 if (timeout->value > to.value)
253 if (pos->read_set != NULL)
254 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
255 if (pos->write_set != NULL)
256 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
257 if (pos->reason != 0)
258 *timeout = GNUNET_TIME_UNIT_ZERO;
265 * Check if the ready set overlaps with the set we want to have ready.
266 * If so, update the want set (set all FDs that are ready). If not,
269 * @param ready set that is ready
270 * @param want set that we want to be ready
271 * @return GNUNET_YES if there was some overlap
274 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
275 struct GNUNET_NETWORK_FDSet *want)
279 if (GNUNET_NETWORK_fdset_overlap (ready, want))
281 /* copy all over (yes, there maybe unrelated bits,
282 but this should not hurt well-written clients) */
283 GNUNET_NETWORK_fdset_copy (want, ready);
291 * Check if the given task is eligible to run now.
292 * Also set the reason why it is eligible.
294 * @param sched the scheduler
295 * @param task task to check if it is ready
296 * @param now the current time
297 * @param rs set of FDs ready for reading
298 * @param ws set of FDs ready for writing
299 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
302 is_ready (struct GNUNET_SCHEDULER_Handle *sched,
304 struct GNUNET_TIME_Absolute now,
305 const struct GNUNET_NETWORK_FDSet *rs,
306 const struct GNUNET_NETWORK_FDSet *ws)
308 if (now.value >= task->timeout.value)
309 task->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
310 if ((0 == (task->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
311 (rs != NULL) && (set_overlaps (rs, task->read_set)))
312 task->reason |= GNUNET_SCHEDULER_REASON_READ_READY;
313 if ((0 == (task->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
314 (ws != NULL) && (set_overlaps (ws, task->write_set)))
315 task->reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
316 if (task->reason == 0)
317 return GNUNET_NO; /* not ready */
318 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
320 if (GNUNET_YES == is_pending (sched, task->prereq_id))
321 return GNUNET_NO; /* prereq waiting */
322 task->reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
329 * Put a task that is ready for execution into the ready queue.
331 * @param handle the scheduler
332 * @param task task ready for execution
335 queue_ready_task (struct GNUNET_SCHEDULER_Handle *handle, struct Task *task)
337 task->next = handle->ready[check_priority (task->priority)];
338 handle->ready[check_priority (task->priority)] = task;
339 handle->ready_count++;
344 * Check which tasks are ready and move them
345 * to the respective ready queue.
347 * @param handle the scheduler
348 * @param rs FDs ready for reading
349 * @param ws FDs ready for writing
352 check_ready (struct GNUNET_SCHEDULER_Handle *handle,
353 const struct GNUNET_NETWORK_FDSet *rs,
354 const struct GNUNET_NETWORK_FDSet *ws)
359 struct GNUNET_TIME_Absolute now;
361 now = GNUNET_TIME_absolute_get ();
363 pos = handle->pending;
367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
368 "Checking readyness of task: %llu / %p\n",
369 pos->id, pos->callback_cls);
372 if (GNUNET_YES == is_ready (handle, pos, now, rs, ws))
375 handle->pending = next;
378 queue_ready_task (handle, pos);
389 * Request the shutdown of a scheduler. Marks all currently
390 * pending tasks as ready because of shutdown. This will
391 * cause all tasks to run (as soon as possible, respecting
392 * priorities and prerequisite tasks). Note that tasks
393 * scheduled AFTER this call may still be delayed arbitrarily.
395 * @param sched the scheduler
398 GNUNET_SCHEDULER_shutdown (struct GNUNET_SCHEDULER_Handle *sched)
403 pos = sched->pending;
406 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
407 /* we don't move the task into the ready queue yet; check_ready
408 will do that later, possibly adding additional
412 for (i=0;i<GNUNET_SCHEDULER_PRIORITY_COUNT;i++)
414 pos = sched->ready[i];
417 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
418 /* we don't move the task into the ready queue yet; check_ready
419 will do that later, possibly adding additional
428 * Destroy a task (release associated resources)
430 * @param t task to destroy
433 destroy_task (struct Task *t)
435 if (NULL != t->read_set)
436 GNUNET_NETWORK_fdset_destroy (t->read_set);
437 if (NULL != t->write_set)
438 GNUNET_NETWORK_fdset_destroy (t->write_set);
444 * Run at least one task in the highest-priority queue that is not
445 * empty. Keep running tasks until we are either no longer running
446 * "URGENT" tasks or until we have at least one "pending" task (which
447 * may become ready, hence we should select on it). Naturally, if
448 * there are no more ready tasks, we also return.
450 * @param sched the scheduler
453 run_ready (struct GNUNET_SCHEDULER_Handle *sched)
455 enum GNUNET_SCHEDULER_Priority p;
457 struct GNUNET_SCHEDULER_TaskContext tc;
461 if (sched->ready_count == 0)
463 GNUNET_assert (sched->ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
464 /* yes, p>0 is correct, 0 is "KEEP" which should
465 always be an empty queue (see assertion)! */
466 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
468 pos = sched->ready[p];
472 GNUNET_assert (pos != NULL); /* ready_count wrong? */
473 sched->ready[p] = pos->next;
474 sched->ready_count--;
475 sched->current_priority = p;
476 GNUNET_assert (pos->priority == p);
477 sched->active_task = pos;
479 tc.reason = pos->reason;
480 tc.read_ready = pos->read_set;
481 tc.write_ready = pos->write_set;
482 pos->callback (pos->callback_cls, &tc);
484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
485 "Running task: %llu / %p\n", pos->id, pos->callback_cls);
487 sched->active_task = NULL;
491 while ((sched->pending == NULL) || (p == GNUNET_SCHEDULER_PRIORITY_URGENT));
495 * Pipe used to communicate shutdown via signal.
497 static struct GNUNET_DISK_PipeHandle *sigpipe;
501 * Signal handler called for signals that should cause us to shutdown.
504 sighandler_shutdown ()
508 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
509 (sigpipe, GNUNET_DISK_PIPE_END_WRITE), &c,
515 * Initialize and run scheduler. This function will return when all
516 * tasks have completed. On systems with signals, receiving a SIGTERM
517 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
518 * to be run after the active task is complete. As a result, SIGTERM
519 * causes all active tasks to be scheduled with reason
520 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
521 * afterwards will execute normally!). Note that any particular signal
522 * will only shut down one scheduler; applications should always only
523 * create a single scheduler.
525 * @param task task to run immediately
526 * @param task_cls closure of task
529 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
531 struct GNUNET_SCHEDULER_Handle sched;
532 struct GNUNET_NETWORK_FDSet *rs;
533 struct GNUNET_NETWORK_FDSet *ws;
534 struct GNUNET_TIME_Relative timeout;
536 struct GNUNET_SIGNAL_Context *shc_int;
537 struct GNUNET_SIGNAL_Context *shc_term;
538 struct GNUNET_SIGNAL_Context *shc_quit;
539 struct GNUNET_SIGNAL_Context *shc_hup;
540 unsigned long long last_tr;
541 unsigned int busy_wait_warning;
542 const struct GNUNET_DISK_FileHandle *pr;
545 rs = GNUNET_NETWORK_fdset_create ();
546 ws = GNUNET_NETWORK_fdset_create ();
547 GNUNET_assert (sigpipe == NULL);
548 sigpipe = GNUNET_DISK_pipe (GNUNET_NO);
549 GNUNET_assert (sigpipe != NULL);
550 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
551 GNUNET_assert (pr != NULL);
552 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
553 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
555 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
556 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
558 memset (&sched, 0, sizeof (sched));
559 sched.current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
560 GNUNET_SCHEDULER_add_continuation (&sched,
563 GNUNET_SCHEDULER_REASON_STARTUP);
565 busy_wait_warning = 0;
566 while ((sched.pending != NULL) || (sched.ready_count > 0))
568 GNUNET_NETWORK_fdset_zero (rs);
569 GNUNET_NETWORK_fdset_zero (ws);
570 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
571 update_sets (&sched, rs, ws, &timeout);
572 GNUNET_NETWORK_fdset_handle_set (rs, pr);
573 if (sched.ready_count > 0)
575 /* no blocking, more work already ready! */
576 timeout = GNUNET_TIME_UNIT_ZERO;
578 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
579 if (ret == GNUNET_SYSERR)
583 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
586 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
588 /* consume the signal */
589 GNUNET_DISK_file_read (pr, &c, sizeof (c));
590 /* mark all active tasks as ready due to shutdown */
591 GNUNET_SCHEDULER_shutdown (&sched);
593 if (last_tr == sched.tasks_run)
599 last_tr = sched.tasks_run;
600 busy_wait_warning = 0;
602 if ((ret == 0) && (timeout.value == 0) && (busy_wait_warning > 16))
604 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
605 _("Looks like we're busy waiting...\n"));
606 sleep (1); /* mitigate */
608 check_ready (&sched, rs, ws);
611 GNUNET_SIGNAL_handler_uninstall (shc_int);
612 GNUNET_SIGNAL_handler_uninstall (shc_term);
614 GNUNET_SIGNAL_handler_uninstall (shc_quit);
615 GNUNET_SIGNAL_handler_uninstall (shc_hup);
617 GNUNET_DISK_pipe_close (sigpipe);
619 GNUNET_NETWORK_fdset_destroy (rs);
620 GNUNET_NETWORK_fdset_destroy (ws);
625 * Obtain the reason code for why the current task was
626 * started. Will return the same value as
627 * the GNUNET_SCHEDULER_TaskContext's reason field.
629 * @param sched scheduler to query
630 * @return reason(s) why the current task is run
632 enum GNUNET_SCHEDULER_Reason
633 GNUNET_SCHEDULER_get_reason (struct GNUNET_SCHEDULER_Handle *sched)
635 return sched->active_task->reason;
640 * Get information about the current load of this scheduler. Use this
641 * function to determine if an elective task should be added or simply
642 * dropped (if the decision should be made based on the number of
643 * tasks ready to run).
645 * @param sched scheduler to query
646 * @param p priority level to look at
647 * @return number of tasks pending right now
650 GNUNET_SCHEDULER_get_load (struct GNUNET_SCHEDULER_Handle *sched,
651 enum GNUNET_SCHEDULER_Priority p)
656 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
657 return sched->ready_count;
658 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
659 p = sched->current_priority;
661 pos = sched->ready[p];
672 * Cancel the task with the specified identifier.
673 * The task must not yet have run.
675 * @param sched scheduler to use
676 * @param task id of the task to cancel
677 * @return original closure of the task
680 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Handle *sched,
681 GNUNET_SCHEDULER_TaskIdentifier task)
685 enum GNUNET_SCHEDULER_Priority p;
701 GNUNET_assert (p < GNUNET_SCHEDULER_PRIORITY_COUNT);
708 sched->ready_count--;
718 sched->pending = t->next;
720 sched->ready[p] = t->next;
723 prev->next = t->next;
724 ret = t->callback_cls;
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727 "Canceling task: %llu / %p\n", task, t->callback_cls);
735 * Continue the current execution with the given function. This is
736 * similar to the other "add" functions except that there is no delay
737 * and the reason code can be specified.
739 * @param sched scheduler to use
740 * @param task main function of the task
741 * @param task_cls closure for 'main'
742 * @param reason reason for task invocation
745 GNUNET_SCHEDULER_add_continuation (struct GNUNET_SCHEDULER_Handle *sched,
746 GNUNET_SCHEDULER_Task task,
748 enum GNUNET_SCHEDULER_Reason reason)
752 t = GNUNET_malloc (sizeof (struct Task));
754 t->callback_cls = task_cls;
755 t->id = ++sched->last_id;
757 t->priority = sched->current_priority;
759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
760 "Adding continuation task: %llu / %p\n",
761 t->id, t->callback_cls);
763 queue_ready_task (sched, t);
769 * Schedule a new task to be run after the specified prerequisite task
770 * has completed. It will be run with the priority of the calling
773 * @param sched scheduler to use
774 * @param prerequisite_task run this task after the task with the given
775 * task identifier completes (and any of our other
776 * conditions, such as delay, read or write-readyness
777 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
778 * on completion of other tasks (this will cause the task to run as
780 * @param task main function of the task
781 * @param task_cls closure of task
782 * @return unique task identifier for the job
783 * only valid until "task" is started!
785 GNUNET_SCHEDULER_TaskIdentifier
786 GNUNET_SCHEDULER_add_after (struct GNUNET_SCHEDULER_Handle *sched,
787 GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
788 GNUNET_SCHEDULER_Task task, void *task_cls)
790 return GNUNET_SCHEDULER_add_select (sched,
791 GNUNET_SCHEDULER_PRIORITY_KEEP,
793 GNUNET_TIME_UNIT_ZERO,
794 NULL, NULL, task, task_cls);
799 * Schedule a new task to be run with a specified priority.
801 * @param sched scheduler to use
802 * @param prio how important is the new task?
803 * @param task main function of the task
804 * @param task_cls closure of task
805 * @return unique task identifier for the job
806 * only valid until "task" is started!
808 GNUNET_SCHEDULER_TaskIdentifier
809 GNUNET_SCHEDULER_add_with_priority (struct GNUNET_SCHEDULER_Handle * sched,
810 enum GNUNET_SCHEDULER_Priority prio,
811 GNUNET_SCHEDULER_Task task,
814 return GNUNET_SCHEDULER_add_select (sched,
816 GNUNET_SCHEDULER_NO_TASK,
817 GNUNET_TIME_UNIT_ZERO,
818 NULL, NULL, task, task_cls);
824 * Schedule a new task to be run with a specified delay. The task
825 * will be scheduled for execution once the delay has expired. It
826 * will be run with the priority of the calling task.
828 * @param sched scheduler to use
829 * @param delay when should this operation time out? Use
830 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
831 * @param task main function of the task
832 * @param task_cls closure of task
833 * @return unique task identifier for the job
834 * only valid until "task" is started!
836 GNUNET_SCHEDULER_TaskIdentifier
837 GNUNET_SCHEDULER_add_delayed (struct GNUNET_SCHEDULER_Handle * sched,
838 struct GNUNET_TIME_Relative delay,
839 GNUNET_SCHEDULER_Task task, void *task_cls)
841 return GNUNET_SCHEDULER_add_select (sched,
842 GNUNET_SCHEDULER_PRIORITY_KEEP,
843 GNUNET_SCHEDULER_NO_TASK, delay,
844 NULL, NULL, task, task_cls);
850 * Schedule a new task to be run as soon as possible. The task
851 * will be run with the priority of the calling task.
853 * @param sched scheduler to use
854 * @param task main function of the task
855 * @param task_cls closure of task
856 * @return unique task identifier for the job
857 * only valid until "task" is started!
859 GNUNET_SCHEDULER_TaskIdentifier
860 GNUNET_SCHEDULER_add_now (struct GNUNET_SCHEDULER_Handle *sched,
861 GNUNET_SCHEDULER_Task task,
864 return GNUNET_SCHEDULER_add_select (sched,
865 GNUNET_SCHEDULER_PRIORITY_KEEP,
866 GNUNET_SCHEDULER_NO_TASK,
867 GNUNET_TIME_UNIT_ZERO,
868 NULL, NULL, task, task_cls);
874 * Schedule a new task to be run with a specified delay or when the
875 * specified file descriptor is ready for reading. The delay can be
876 * used as a timeout on the socket being ready. The task will be
877 * scheduled for execution once either the delay has expired or the
878 * socket operation is ready. It will be run with the priority of
881 * @param sched scheduler to use
882 * @param delay when should this operation time out? Use
883 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
884 * @param rfd read file-descriptor
885 * @param task main function of the task
886 * @param task_cls closure of task
887 * @return unique task identifier for the job
888 * only valid until "task" is started!
890 GNUNET_SCHEDULER_TaskIdentifier
891 GNUNET_SCHEDULER_add_read_net (struct GNUNET_SCHEDULER_Handle * sched,
892 struct GNUNET_TIME_Relative delay,
893 struct GNUNET_NETWORK_Handle * rfd,
894 GNUNET_SCHEDULER_Task task, void *task_cls)
896 struct GNUNET_NETWORK_FDSet *rs;
897 GNUNET_SCHEDULER_TaskIdentifier ret;
899 GNUNET_assert (rfd != NULL);
900 rs = GNUNET_NETWORK_fdset_create ();
901 GNUNET_NETWORK_fdset_set (rs, rfd);
902 ret = GNUNET_SCHEDULER_add_select (sched,
903 GNUNET_SCHEDULER_PRIORITY_KEEP,
904 GNUNET_SCHEDULER_NO_TASK,
905 delay, rs, NULL, task, task_cls);
906 GNUNET_NETWORK_fdset_destroy (rs);
912 * Schedule a new task to be run with a specified delay or when the
913 * specified file descriptor is ready for writing. The delay can be
914 * used as a timeout on the socket being ready. The task will be
915 * scheduled for execution once either the delay has expired or the
916 * socket operation is ready. It will be run with the priority of
919 * @param sched scheduler to use
920 * @param delay when should this operation time out? Use
921 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
922 * @param wfd write file-descriptor
923 * @param task main function of the task
924 * @param task_cls closure of task
925 * @return unique task identifier for the job
926 * only valid until "task" is started!
928 GNUNET_SCHEDULER_TaskIdentifier
929 GNUNET_SCHEDULER_add_write_net (struct GNUNET_SCHEDULER_Handle * sched,
930 struct GNUNET_TIME_Relative delay,
931 struct GNUNET_NETWORK_Handle * wfd,
932 GNUNET_SCHEDULER_Task task, void *task_cls)
934 struct GNUNET_NETWORK_FDSet *ws;
935 GNUNET_SCHEDULER_TaskIdentifier ret;
937 GNUNET_assert (wfd != NULL);
938 ws = GNUNET_NETWORK_fdset_create ();
939 GNUNET_NETWORK_fdset_set (ws, wfd);
940 ret = GNUNET_SCHEDULER_add_select (sched,
941 GNUNET_SCHEDULER_PRIORITY_KEEP,
942 GNUNET_SCHEDULER_NO_TASK, delay,
943 NULL, ws, task, task_cls);
944 GNUNET_NETWORK_fdset_destroy (ws);
950 * Schedule a new task to be run with a specified delay or when the
951 * specified file descriptor is ready for reading. The delay can be
952 * used as a timeout on the socket being ready. The task will be
953 * scheduled for execution once either the delay has expired or the
954 * socket operation is ready. It will be run with the priority of
957 * @param sched scheduler to use
958 * @param delay when should this operation time out? Use
959 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
960 * @param rfd read file-descriptor
961 * @param task main function of the task
962 * @param task_cls closure of task
963 * @return unique task identifier for the job
964 * only valid until "task" is started!
966 GNUNET_SCHEDULER_TaskIdentifier
967 GNUNET_SCHEDULER_add_read_file (struct GNUNET_SCHEDULER_Handle * sched,
968 struct GNUNET_TIME_Relative delay,
969 const struct GNUNET_DISK_FileHandle * rfd,
970 GNUNET_SCHEDULER_Task task, void *task_cls)
972 struct GNUNET_NETWORK_FDSet *rs;
973 GNUNET_SCHEDULER_TaskIdentifier ret;
975 GNUNET_assert (rfd != NULL);
976 rs = GNUNET_NETWORK_fdset_create ();
977 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
978 ret = GNUNET_SCHEDULER_add_select (sched,
979 GNUNET_SCHEDULER_PRIORITY_KEEP,
980 GNUNET_SCHEDULER_NO_TASK, delay,
981 rs, NULL, task, task_cls);
982 GNUNET_NETWORK_fdset_destroy (rs);
988 * Schedule a new task to be run with a specified delay or when the
989 * specified file descriptor is ready for writing. The delay can be
990 * used as a timeout on the socket being ready. The task will be
991 * scheduled for execution once either the delay has expired or the
992 * socket operation is ready. It will be run with the priority of
995 * @param sched scheduler to use
996 * @param delay when should this operation time out? Use
997 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
998 * @param wfd write file-descriptor
999 * @param task main function of the task
1000 * @param task_cls closure of task
1001 * @return unique task identifier for the job
1002 * only valid until "task" is started!
1004 GNUNET_SCHEDULER_TaskIdentifier
1005 GNUNET_SCHEDULER_add_write_file (struct GNUNET_SCHEDULER_Handle * sched,
1006 struct GNUNET_TIME_Relative delay,
1007 const struct GNUNET_DISK_FileHandle * wfd,
1008 GNUNET_SCHEDULER_Task task, void *task_cls)
1010 struct GNUNET_NETWORK_FDSet *ws;
1011 GNUNET_SCHEDULER_TaskIdentifier ret;
1013 GNUNET_assert (wfd != NULL);
1014 ws = GNUNET_NETWORK_fdset_create ();
1015 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1016 ret = GNUNET_SCHEDULER_add_select (sched,
1017 GNUNET_SCHEDULER_PRIORITY_KEEP,
1018 GNUNET_SCHEDULER_NO_TASK,
1019 delay, NULL, ws, task, task_cls);
1020 GNUNET_NETWORK_fdset_destroy (ws);
1027 * Schedule a new task to be run with a specified delay or when any of
1028 * the specified file descriptor sets is ready. The delay can be used
1029 * as a timeout on the socket(s) being ready. The task will be
1030 * scheduled for execution once either the delay has expired or any of
1031 * the socket operations is ready. This is the most general
1032 * function of the "add" family. Note that the "prerequisite_task"
1033 * must be satisfied in addition to any of the other conditions. In
1034 * other words, the task will be started when
1036 * (prerequisite-run)
1040 * || (shutdown-active && run-on-shutdown) )
1043 * @param sched scheduler to use
1044 * @param prio how important is this task?
1045 * @param prerequisite_task run this task after the task with the given
1046 * task identifier completes (and any of our other
1047 * conditions, such as delay, read or write-readyness
1048 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1049 * on completion of other tasks.
1050 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1051 * which means that the task will only be run after we receive SIGTERM
1052 * @param rs set of file descriptors we want to read (can be NULL)
1053 * @param ws set of file descriptors we want to write (can be NULL)
1054 * @param task main function of the task
1055 * @param task_cls closure of task
1056 * @return unique task identifier for the job
1057 * only valid until "task" is started!
1059 GNUNET_SCHEDULER_TaskIdentifier
1060 GNUNET_SCHEDULER_add_select (struct GNUNET_SCHEDULER_Handle * sched,
1061 enum GNUNET_SCHEDULER_Priority prio,
1062 GNUNET_SCHEDULER_TaskIdentifier
1064 struct GNUNET_TIME_Relative delay,
1065 const struct GNUNET_NETWORK_FDSet * rs,
1066 const struct GNUNET_NETWORK_FDSet * ws,
1067 GNUNET_SCHEDULER_Task task, void *task_cls)
1071 t = GNUNET_malloc (sizeof (struct Task));
1073 t->callback_cls = task_cls;
1076 t->read_set = GNUNET_NETWORK_fdset_create ();
1077 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1081 t->write_set = GNUNET_NETWORK_fdset_create ();
1082 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1084 t->id = ++sched->last_id;
1085 t->prereq_id = prerequisite_task;
1086 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1088 check_priority ((prio ==
1089 GNUNET_SCHEDULER_PRIORITY_KEEP) ? sched->current_priority
1091 t->next = sched->pending;
1094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1095 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1100 /* end of scheduler.c */