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"
33 #define EXECINFO GNUNET_NO
36 #define DEBUG_TASKS GNUNET_NO
39 * Linked list of pending tasks.
44 * This is a linked list.
49 * Function to run when ready.
51 GNUNET_SCHEDULER_Task callback;
54 * Closure for the callback.
59 * Set of file descriptors this task is waiting
60 * for for reading. Once ready, this is updated
61 * to reflect the set of file descriptors ready
64 struct GNUNET_NETWORK_FDSet *read_set;
67 * Set of file descriptors this task is waiting for for writing.
68 * Once ready, this is updated to reflect the set of file
69 * descriptors ready for operation.
71 struct GNUNET_NETWORK_FDSet *write_set;
74 * Unique task identifier.
76 GNUNET_SCHEDULER_TaskIdentifier id;
79 * Identifier of a prerequisite task.
81 GNUNET_SCHEDULER_TaskIdentifier prereq_id;
84 * Absolute timeout value for the task, or
85 * GNUNET_TIME_UNIT_FOREVER_ABS for "no timeout".
87 struct GNUNET_TIME_Absolute timeout;
90 * Why is the task ready? Set after task is added to ready queue.
91 * Initially set to zero. All reasons that have already been
92 * satisfied (i.e. read or write ready) will be set over time.
94 enum GNUNET_SCHEDULER_Reason reason;
99 enum GNUNET_SCHEDULER_Priority priority;
103 * Array of strings which make up a backtrace from the point when this
104 * task was scheduled (essentially, who scheduled the task?)
106 char **backtrace_strings;
109 * Size of the backtrace_strings array
111 int num_backtrace_strings;
118 * Handle for the scheduling service.
120 struct GNUNET_SCHEDULER_Handle
124 * List of tasks waiting for an event.
126 struct Task *pending;
129 * ID of the task that is running right now.
131 struct Task *active_task;
134 * List of tasks ready to run right now,
135 * grouped by importance.
137 struct Task *ready[GNUNET_SCHEDULER_PRIORITY_COUNT];
140 * Identity of the last task queued. Incremented for each task to
141 * generate a unique task ID (it is virtually impossible to start
142 * more than 2^64 tasks during the lifetime of a process).
144 GNUNET_SCHEDULER_TaskIdentifier last_id;
147 * Highest number so that all tasks with smaller identifiers
148 * have already completed. Also the lowest number of a task
149 * still waiting to be executed.
151 GNUNET_SCHEDULER_TaskIdentifier lowest_pending_id;
154 * Number of tasks on the ready list.
156 unsigned int ready_count;
159 * How many tasks have we run so far?
161 unsigned long long tasks_run;
164 * Priority of the task running right now. Only
165 * valid while a task is running.
167 enum GNUNET_SCHEDULER_Priority current_priority;
173 * Check that the given priority is legal (and return it).
175 * @param p priority value to check
176 * @return p on success, 0 on error
178 static enum GNUNET_SCHEDULER_Priority
179 check_priority (enum GNUNET_SCHEDULER_Priority p)
181 if ((p >= 0) && (p < GNUNET_SCHEDULER_PRIORITY_COUNT))
184 return 0; /* make compiler happy */
189 * Is a task with this identifier still pending? Also updates
190 * "lowest_pending_id" as a side-effect (for faster checks in the
191 * future), but only if the return value is "GNUNET_NO" (and
192 * the "lowest_pending_id" check failed).
194 * @param sched the scheduler
195 * @param id which task are we checking for
196 * @return GNUNET_YES if so, GNUNET_NO if not
199 is_pending (struct GNUNET_SCHEDULER_Handle *sched,
200 GNUNET_SCHEDULER_TaskIdentifier id)
203 enum GNUNET_SCHEDULER_Priority p;
204 GNUNET_SCHEDULER_TaskIdentifier min;
206 if (id < sched->lowest_pending_id)
208 min = -1; /* maximum value */
209 pos = sched->pending;
218 for (p = 0; p < GNUNET_SCHEDULER_PRIORITY_COUNT; p++)
220 pos = sched->ready[p];
230 sched->lowest_pending_id = min;
236 * Update all sets and timeout for select.
238 * @param sched the scheduler
239 * @param rs read-set, set to all FDs we would like to read (updated)
240 * @param ws write-set, set to all FDs we would like to write (updated)
241 * @param timeout next timeout (updated)
244 update_sets (struct GNUNET_SCHEDULER_Handle *sched,
245 struct GNUNET_NETWORK_FDSet *rs,
246 struct GNUNET_NETWORK_FDSet *ws,
247 struct GNUNET_TIME_Relative *timeout)
251 pos = sched->pending;
254 if ((pos->prereq_id != GNUNET_SCHEDULER_NO_TASK) &&
255 (GNUNET_YES == is_pending (sched, pos->prereq_id)))
261 if (pos->timeout.value != GNUNET_TIME_UNIT_FOREVER_ABS.value)
263 struct GNUNET_TIME_Relative to;
265 to = GNUNET_TIME_absolute_get_remaining (pos->timeout);
266 if (timeout->value > to.value)
269 if (pos->read_set != NULL)
270 GNUNET_NETWORK_fdset_add (rs, pos->read_set);
271 if (pos->write_set != NULL)
272 GNUNET_NETWORK_fdset_add (ws, pos->write_set);
273 if (pos->reason != 0)
274 *timeout = GNUNET_TIME_UNIT_ZERO;
281 * Check if the ready set overlaps with the set we want to have ready.
282 * If so, update the want set (set all FDs that are ready). If not,
285 * @param ready set that is ready
286 * @param want set that we want to be ready
287 * @return GNUNET_YES if there was some overlap
290 set_overlaps (const struct GNUNET_NETWORK_FDSet *ready,
291 struct GNUNET_NETWORK_FDSet *want)
295 if (GNUNET_NETWORK_fdset_overlap (ready, want))
297 /* copy all over (yes, there maybe unrelated bits,
298 but this should not hurt well-written clients) */
299 GNUNET_NETWORK_fdset_copy (want, ready);
307 * Check if the given task is eligible to run now.
308 * Also set the reason why it is eligible.
310 * @param sched the scheduler
311 * @param task task to check if it is ready
312 * @param now the current time
313 * @param rs set of FDs ready for reading
314 * @param ws set of FDs ready for writing
315 * @return GNUNET_YES if we can run it, GNUNET_NO if not.
318 is_ready (struct GNUNET_SCHEDULER_Handle *sched,
320 struct GNUNET_TIME_Absolute now,
321 const struct GNUNET_NETWORK_FDSet *rs,
322 const struct GNUNET_NETWORK_FDSet *ws)
324 if (now.value >= task->timeout.value)
325 task->reason |= GNUNET_SCHEDULER_REASON_TIMEOUT;
326 if ((0 == (task->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
327 (rs != NULL) && (set_overlaps (rs, task->read_set)))
328 task->reason |= GNUNET_SCHEDULER_REASON_READ_READY;
329 if ((0 == (task->reason & GNUNET_SCHEDULER_REASON_WRITE_READY)) &&
330 (ws != NULL) && (set_overlaps (ws, task->write_set)))
331 task->reason |= GNUNET_SCHEDULER_REASON_WRITE_READY;
332 if (task->reason == 0)
333 return GNUNET_NO; /* not ready */
334 if (task->prereq_id != GNUNET_SCHEDULER_NO_TASK)
336 if (GNUNET_YES == is_pending (sched, task->prereq_id))
337 return GNUNET_NO; /* prereq waiting */
338 task->reason |= GNUNET_SCHEDULER_REASON_PREREQ_DONE;
345 * Put a task that is ready for execution into the ready queue.
347 * @param handle the scheduler
348 * @param task task ready for execution
351 queue_ready_task (struct GNUNET_SCHEDULER_Handle *handle, struct Task *task)
353 enum GNUNET_SCHEDULER_Priority p = task->priority;
354 if (0 != (task->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
355 p = GNUNET_SCHEDULER_PRIORITY_SHUTDOWN;
356 task->next = handle->ready[check_priority (p)];
357 handle->ready[check_priority (p)] = task;
358 handle->ready_count++;
363 * Check which tasks are ready and move them
364 * to the respective ready queue.
366 * @param handle the scheduler
367 * @param rs FDs ready for reading
368 * @param ws FDs ready for writing
371 check_ready (struct GNUNET_SCHEDULER_Handle *handle,
372 const struct GNUNET_NETWORK_FDSet *rs,
373 const struct GNUNET_NETWORK_FDSet *ws)
378 struct GNUNET_TIME_Absolute now;
380 now = GNUNET_TIME_absolute_get ();
382 pos = handle->pending;
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "Checking readiness of task: %llu / %p\n",
388 pos->id, pos->callback_cls);
391 if (GNUNET_YES == is_ready (handle, pos, now, rs, ws))
394 handle->pending = next;
397 queue_ready_task (handle, pos);
408 * Request the shutdown of a scheduler. Marks all currently
409 * pending tasks as ready because of shutdown. This will
410 * cause all tasks to run (as soon as possible, respecting
411 * priorities and prerequisite tasks). Note that tasks
412 * scheduled AFTER this call may still be delayed arbitrarily.
414 * @param sched the scheduler
417 GNUNET_SCHEDULER_shutdown (struct GNUNET_SCHEDULER_Handle *sched)
422 pos = sched->pending;
425 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
426 /* we don't move the task into the ready queue yet; check_ready
427 will do that later, possibly adding additional
431 for (i=0;i<GNUNET_SCHEDULER_PRIORITY_COUNT;i++)
433 pos = sched->ready[i];
436 pos->reason |= GNUNET_SCHEDULER_REASON_SHUTDOWN;
437 /* we don't move the task into the ready queue yet; check_ready
438 will do that later, possibly adding additional
447 * Destroy a task (release associated resources)
449 * @param t task to destroy
452 destroy_task (struct Task *t)
454 if (NULL != t->read_set)
455 GNUNET_NETWORK_fdset_destroy (t->read_set);
456 if (NULL != t->write_set)
457 GNUNET_NETWORK_fdset_destroy (t->write_set);
459 GNUNET_free (t->backtrace_strings);
466 * Run at least one task in the highest-priority queue that is not
467 * empty. Keep running tasks until we are either no longer running
468 * "URGENT" tasks or until we have at least one "pending" task (which
469 * may become ready, hence we should select on it). Naturally, if
470 * there are no more ready tasks, we also return.
472 * @param sched the scheduler
475 run_ready (struct GNUNET_SCHEDULER_Handle *sched)
477 enum GNUNET_SCHEDULER_Priority p;
479 struct GNUNET_SCHEDULER_TaskContext tc;
483 if (sched->ready_count == 0)
485 GNUNET_assert (sched->ready[GNUNET_SCHEDULER_PRIORITY_KEEP] == NULL);
486 /* yes, p>0 is correct, 0 is "KEEP" which should
487 always be an empty queue (see assertion)! */
488 for (p = GNUNET_SCHEDULER_PRIORITY_COUNT - 1; p > 0; p--)
490 pos = sched->ready[p];
494 GNUNET_assert (pos != NULL); /* ready_count wrong? */
495 sched->ready[p] = pos->next;
496 sched->ready_count--;
497 sched->current_priority = pos->priority;
498 sched->active_task = pos;
500 tc.reason = pos->reason;
501 tc.read_ready = pos->read_set;
502 tc.write_ready = pos->write_set;
503 pos->callback (pos->callback_cls, &tc);
505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
506 "Running task: %llu / %p\n", pos->id, pos->callback_cls);
508 sched->active_task = NULL;
512 while ((sched->pending == NULL) || (p == GNUNET_SCHEDULER_PRIORITY_URGENT));
516 * Pipe used to communicate shutdown via signal.
518 static struct GNUNET_DISK_PipeHandle *sigpipe;
522 * Signal handler called for signals that should cause us to shutdown.
525 sighandler_shutdown ()
529 GNUNET_DISK_file_write (GNUNET_DISK_pipe_handle
530 (sigpipe, GNUNET_DISK_PIPE_END_WRITE), &c,
536 * Initialize and run scheduler. This function will return when all
537 * tasks have completed. On systems with signals, receiving a SIGTERM
538 * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
539 * to be run after the active task is complete. As a result, SIGTERM
540 * causes all active tasks to be scheduled with reason
541 * "GNUNET_SCHEDULER_REASON_SHUTDOWN". (However, tasks added
542 * afterwards will execute normally!). Note that any particular signal
543 * will only shut down one scheduler; applications should always only
544 * create a single scheduler.
546 * @param task task to run immediately
547 * @param task_cls closure of task
550 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls)
552 struct GNUNET_SCHEDULER_Handle sched;
553 struct GNUNET_NETWORK_FDSet *rs;
554 struct GNUNET_NETWORK_FDSet *ws;
555 struct GNUNET_TIME_Relative timeout;
557 struct GNUNET_SIGNAL_Context *shc_int;
558 struct GNUNET_SIGNAL_Context *shc_term;
559 struct GNUNET_SIGNAL_Context *shc_quit;
560 struct GNUNET_SIGNAL_Context *shc_hup;
561 unsigned long long last_tr;
562 unsigned int busy_wait_warning;
563 const struct GNUNET_DISK_FileHandle *pr;
566 rs = GNUNET_NETWORK_fdset_create ();
567 ws = GNUNET_NETWORK_fdset_create ();
568 GNUNET_assert (sigpipe == NULL);
569 sigpipe = GNUNET_DISK_pipe (GNUNET_NO);
570 GNUNET_assert (sigpipe != NULL);
571 pr = GNUNET_DISK_pipe_handle (sigpipe, GNUNET_DISK_PIPE_END_READ);
572 GNUNET_assert (pr != NULL);
573 shc_int = GNUNET_SIGNAL_handler_install (SIGINT, &sighandler_shutdown);
574 shc_term = GNUNET_SIGNAL_handler_install (SIGTERM, &sighandler_shutdown);
576 shc_quit = GNUNET_SIGNAL_handler_install (SIGQUIT, &sighandler_shutdown);
577 shc_hup = GNUNET_SIGNAL_handler_install (SIGHUP, &sighandler_shutdown);
579 memset (&sched, 0, sizeof (sched));
580 sched.current_priority = GNUNET_SCHEDULER_PRIORITY_DEFAULT;
581 GNUNET_SCHEDULER_add_continuation (&sched,
584 GNUNET_SCHEDULER_REASON_STARTUP);
586 busy_wait_warning = 0;
587 while ((sched.pending != NULL) || (sched.ready_count > 0))
589 GNUNET_NETWORK_fdset_zero (rs);
590 GNUNET_NETWORK_fdset_zero (ws);
591 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
592 update_sets (&sched, rs, ws, &timeout);
593 GNUNET_NETWORK_fdset_handle_set (rs, pr);
594 if (sched.ready_count > 0)
596 /* no blocking, more work already ready! */
597 timeout = GNUNET_TIME_UNIT_ZERO;
599 ret = GNUNET_NETWORK_socket_select (rs, ws, NULL, timeout);
600 if (ret == GNUNET_SYSERR)
604 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "select");
607 if (GNUNET_NETWORK_fdset_handle_isset (rs, pr))
609 /* consume the signal */
610 GNUNET_DISK_file_read (pr, &c, sizeof (c));
611 /* mark all active tasks as ready due to shutdown */
612 GNUNET_SCHEDULER_shutdown (&sched);
614 if (last_tr == sched.tasks_run)
620 last_tr = sched.tasks_run;
621 busy_wait_warning = 0;
623 if ((ret == 0) && (timeout.value == 0) && (busy_wait_warning > 16))
625 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
626 _("Looks like we're busy waiting...\n"));
627 sleep (1); /* mitigate */
629 check_ready (&sched, rs, ws);
632 GNUNET_SIGNAL_handler_uninstall (shc_int);
633 GNUNET_SIGNAL_handler_uninstall (shc_term);
635 GNUNET_SIGNAL_handler_uninstall (shc_quit);
636 GNUNET_SIGNAL_handler_uninstall (shc_hup);
638 GNUNET_DISK_pipe_close (sigpipe);
640 GNUNET_NETWORK_fdset_destroy (rs);
641 GNUNET_NETWORK_fdset_destroy (ws);
646 * Obtain the reason code for why the current task was
647 * started. Will return the same value as
648 * the GNUNET_SCHEDULER_TaskContext's reason field.
650 * @param sched scheduler to query
651 * @return reason(s) why the current task is run
653 enum GNUNET_SCHEDULER_Reason
654 GNUNET_SCHEDULER_get_reason (struct GNUNET_SCHEDULER_Handle *sched)
656 return sched->active_task->reason;
661 * Get information about the current load of this scheduler. Use this
662 * function to determine if an elective task should be added or simply
663 * dropped (if the decision should be made based on the number of
664 * tasks ready to run).
666 * @param sched scheduler to query
667 * @param p priority level to look at
668 * @return number of tasks pending right now
671 GNUNET_SCHEDULER_get_load (struct GNUNET_SCHEDULER_Handle *sched,
672 enum GNUNET_SCHEDULER_Priority p)
677 if (p == GNUNET_SCHEDULER_PRIORITY_COUNT)
678 return sched->ready_count;
679 if (p == GNUNET_SCHEDULER_PRIORITY_KEEP)
680 p = sched->current_priority;
682 pos = sched->ready[p];
693 * Cancel the task with the specified identifier.
694 * The task must not yet have run.
696 * @param sched scheduler to use
697 * @param task id of the task to cancel
698 * @return original closure of the task
701 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Handle *sched,
702 GNUNET_SCHEDULER_TaskIdentifier task)
706 enum GNUNET_SCHEDULER_Priority p;
722 GNUNET_assert (p < GNUNET_SCHEDULER_PRIORITY_COUNT);
729 sched->ready_count--;
739 sched->pending = t->next;
741 sched->ready[p] = t->next;
744 prev->next = t->next;
745 ret = t->callback_cls;
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
748 "Canceling task: %llu / %p\n", task, t->callback_cls);
756 * Continue the current execution with the given function. This is
757 * similar to the other "add" functions except that there is no delay
758 * and the reason code can be specified.
760 * @param sched scheduler to use
761 * @param task main function of the task
762 * @param task_cls closure for 'main'
763 * @param reason reason for task invocation
766 GNUNET_SCHEDULER_add_continuation (struct GNUNET_SCHEDULER_Handle *sched,
767 GNUNET_SCHEDULER_Task task,
769 enum GNUNET_SCHEDULER_Reason reason)
773 void *backtrace_array[50];
775 t = GNUNET_malloc (sizeof (struct Task));
777 t->num_backtrace_strings = backtrace(backtrace_array, 50);
778 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
781 t->callback_cls = task_cls;
782 t->id = ++sched->last_id;
784 t->priority = sched->current_priority;
786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
787 "Adding continuation task: %llu / %p\n",
788 t->id, t->callback_cls);
790 queue_ready_task (sched, t);
796 * Schedule a new task to be run after the specified prerequisite task
797 * has completed. It will be run with the priority of the calling
800 * @param sched scheduler to use
801 * @param prerequisite_task run this task after the task with the given
802 * task identifier completes (and any of our other
803 * conditions, such as delay, read or write-readiness
804 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
805 * on completion of other tasks (this will cause the task to run as
807 * @param task main function of the task
808 * @param task_cls closure of task
809 * @return unique task identifier for the job
810 * only valid until "task" is started!
812 GNUNET_SCHEDULER_TaskIdentifier
813 GNUNET_SCHEDULER_add_after (struct GNUNET_SCHEDULER_Handle *sched,
814 GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
815 GNUNET_SCHEDULER_Task task, void *task_cls)
817 return GNUNET_SCHEDULER_add_select (sched,
818 GNUNET_SCHEDULER_PRIORITY_KEEP,
820 GNUNET_TIME_UNIT_ZERO,
821 NULL, NULL, task, task_cls);
826 * Schedule a new task to be run with a specified priority.
828 * @param sched scheduler to use
829 * @param prio how important is the new task?
830 * @param task main function of the task
831 * @param task_cls closure of task
832 * @return unique task identifier for the job
833 * only valid until "task" is started!
835 GNUNET_SCHEDULER_TaskIdentifier
836 GNUNET_SCHEDULER_add_with_priority (struct GNUNET_SCHEDULER_Handle * sched,
837 enum GNUNET_SCHEDULER_Priority prio,
838 GNUNET_SCHEDULER_Task task,
841 return GNUNET_SCHEDULER_add_select (sched,
843 GNUNET_SCHEDULER_NO_TASK,
844 GNUNET_TIME_UNIT_ZERO,
845 NULL, NULL, task, task_cls);
851 * Schedule a new task to be run with a specified delay. The task
852 * will be scheduled for execution once the delay has expired. It
853 * will be run with the priority of the calling task.
855 * @param sched scheduler to use
856 * @param delay when should this operation time out? Use
857 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
858 * @param task main function of the task
859 * @param task_cls closure of task
860 * @return unique task identifier for the job
861 * only valid until "task" is started!
863 GNUNET_SCHEDULER_TaskIdentifier
864 GNUNET_SCHEDULER_add_delayed (struct GNUNET_SCHEDULER_Handle * sched,
865 struct GNUNET_TIME_Relative delay,
866 GNUNET_SCHEDULER_Task task, void *task_cls)
868 return GNUNET_SCHEDULER_add_select (sched,
869 GNUNET_SCHEDULER_PRIORITY_KEEP,
870 GNUNET_SCHEDULER_NO_TASK, delay,
871 NULL, NULL, task, task_cls);
877 * Schedule a new task to be run as soon as possible. The task
878 * will be run with the priority of the calling task.
880 * @param sched scheduler to use
881 * @param task main function of the task
882 * @param task_cls closure of task
883 * @return unique task identifier for the job
884 * only valid until "task" is started!
886 GNUNET_SCHEDULER_TaskIdentifier
887 GNUNET_SCHEDULER_add_now (struct GNUNET_SCHEDULER_Handle *sched,
888 GNUNET_SCHEDULER_Task task,
891 return GNUNET_SCHEDULER_add_select (sched,
892 GNUNET_SCHEDULER_PRIORITY_KEEP,
893 GNUNET_SCHEDULER_NO_TASK,
894 GNUNET_TIME_UNIT_ZERO,
895 NULL, NULL, task, task_cls);
901 * Schedule a new task to be run with a specified delay or when the
902 * specified file descriptor is ready for reading. The delay can be
903 * used as a timeout on the socket being ready. The task will be
904 * scheduled for execution once either the delay has expired or the
905 * socket operation is ready. It will be run with the priority of
908 * @param sched scheduler to use
909 * @param delay when should this operation time out? Use
910 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
911 * @param rfd read file-descriptor
912 * @param task main function of the task
913 * @param task_cls closure of task
914 * @return unique task identifier for the job
915 * only valid until "task" is started!
917 GNUNET_SCHEDULER_TaskIdentifier
918 GNUNET_SCHEDULER_add_read_net (struct GNUNET_SCHEDULER_Handle * sched,
919 struct GNUNET_TIME_Relative delay,
920 struct GNUNET_NETWORK_Handle * rfd,
921 GNUNET_SCHEDULER_Task task, void *task_cls)
923 struct GNUNET_NETWORK_FDSet *rs;
924 GNUNET_SCHEDULER_TaskIdentifier ret;
926 GNUNET_assert (rfd != NULL);
927 rs = GNUNET_NETWORK_fdset_create ();
928 GNUNET_NETWORK_fdset_set (rs, rfd);
929 ret = GNUNET_SCHEDULER_add_select (sched,
930 GNUNET_SCHEDULER_PRIORITY_KEEP,
931 GNUNET_SCHEDULER_NO_TASK,
932 delay, rs, NULL, task, task_cls);
933 GNUNET_NETWORK_fdset_destroy (rs);
939 * Schedule a new task to be run with a specified delay or when the
940 * specified file descriptor is ready for writing. The delay can be
941 * used as a timeout on the socket being ready. The task will be
942 * scheduled for execution once either the delay has expired or the
943 * socket operation is ready. It will be run with the priority of
946 * @param sched scheduler to use
947 * @param delay when should this operation time out? Use
948 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
949 * @param wfd write file-descriptor
950 * @param task main function of the task
951 * @param task_cls closure of task
952 * @return unique task identifier for the job
953 * only valid until "task" is started!
955 GNUNET_SCHEDULER_TaskIdentifier
956 GNUNET_SCHEDULER_add_write_net (struct GNUNET_SCHEDULER_Handle * sched,
957 struct GNUNET_TIME_Relative delay,
958 struct GNUNET_NETWORK_Handle * wfd,
959 GNUNET_SCHEDULER_Task task, void *task_cls)
961 struct GNUNET_NETWORK_FDSet *ws;
962 GNUNET_SCHEDULER_TaskIdentifier ret;
964 GNUNET_assert (wfd != NULL);
965 ws = GNUNET_NETWORK_fdset_create ();
966 GNUNET_NETWORK_fdset_set (ws, wfd);
967 ret = GNUNET_SCHEDULER_add_select (sched,
968 GNUNET_SCHEDULER_PRIORITY_KEEP,
969 GNUNET_SCHEDULER_NO_TASK, delay,
970 NULL, ws, task, task_cls);
971 GNUNET_NETWORK_fdset_destroy (ws);
977 * Schedule a new task to be run with a specified delay or when the
978 * specified file descriptor is ready for reading. The delay can be
979 * used as a timeout on the socket being ready. The task will be
980 * scheduled for execution once either the delay has expired or the
981 * socket operation is ready. It will be run with the priority of
984 * @param sched scheduler to use
985 * @param delay when should this operation time out? Use
986 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
987 * @param rfd read file-descriptor
988 * @param task main function of the task
989 * @param task_cls closure of task
990 * @return unique task identifier for the job
991 * only valid until "task" is started!
993 GNUNET_SCHEDULER_TaskIdentifier
994 GNUNET_SCHEDULER_add_read_file (struct GNUNET_SCHEDULER_Handle * sched,
995 struct GNUNET_TIME_Relative delay,
996 const struct GNUNET_DISK_FileHandle * rfd,
997 GNUNET_SCHEDULER_Task task, void *task_cls)
999 struct GNUNET_NETWORK_FDSet *rs;
1000 GNUNET_SCHEDULER_TaskIdentifier ret;
1002 GNUNET_assert (rfd != NULL);
1003 rs = GNUNET_NETWORK_fdset_create ();
1004 GNUNET_NETWORK_fdset_handle_set (rs, rfd);
1005 ret = GNUNET_SCHEDULER_add_select (sched,
1006 GNUNET_SCHEDULER_PRIORITY_KEEP,
1007 GNUNET_SCHEDULER_NO_TASK, delay,
1008 rs, NULL, task, task_cls);
1009 GNUNET_NETWORK_fdset_destroy (rs);
1015 * Schedule a new task to be run with a specified delay or when the
1016 * specified file descriptor is ready for writing. The delay can be
1017 * used as a timeout on the socket being ready. The task will be
1018 * scheduled for execution once either the delay has expired or the
1019 * socket operation is ready. It will be run with the priority of
1022 * @param sched scheduler to use
1023 * @param delay when should this operation time out? Use
1024 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
1025 * @param wfd write file-descriptor
1026 * @param task main function of the task
1027 * @param task_cls closure of task
1028 * @return unique task identifier for the job
1029 * only valid until "task" is started!
1031 GNUNET_SCHEDULER_TaskIdentifier
1032 GNUNET_SCHEDULER_add_write_file (struct GNUNET_SCHEDULER_Handle * sched,
1033 struct GNUNET_TIME_Relative delay,
1034 const struct GNUNET_DISK_FileHandle * wfd,
1035 GNUNET_SCHEDULER_Task task, void *task_cls)
1037 struct GNUNET_NETWORK_FDSet *ws;
1038 GNUNET_SCHEDULER_TaskIdentifier ret;
1040 GNUNET_assert (wfd != NULL);
1041 ws = GNUNET_NETWORK_fdset_create ();
1042 GNUNET_NETWORK_fdset_handle_set (ws, wfd);
1043 ret = GNUNET_SCHEDULER_add_select (sched,
1044 GNUNET_SCHEDULER_PRIORITY_KEEP,
1045 GNUNET_SCHEDULER_NO_TASK,
1046 delay, NULL, ws, task, task_cls);
1047 GNUNET_NETWORK_fdset_destroy (ws);
1054 * Schedule a new task to be run with a specified delay or when any of
1055 * the specified file descriptor sets is ready. The delay can be used
1056 * as a timeout on the socket(s) being ready. The task will be
1057 * scheduled for execution once either the delay has expired or any of
1058 * the socket operations is ready. This is the most general
1059 * function of the "add" family. Note that the "prerequisite_task"
1060 * must be satisfied in addition to any of the other conditions. In
1061 * other words, the task will be started when
1063 * (prerequisite-run)
1067 * || (shutdown-active && run-on-shutdown) )
1070 * @param sched scheduler to use
1071 * @param prio how important is this task?
1072 * @param prerequisite_task run this task after the task with the given
1073 * task identifier completes (and any of our other
1074 * conditions, such as delay, read or write-readiness
1075 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
1076 * on completion of other tasks.
1077 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
1078 * which means that the task will only be run after we receive SIGTERM
1079 * @param rs set of file descriptors we want to read (can be NULL)
1080 * @param ws set of file descriptors we want to write (can be NULL)
1081 * @param task main function of the task
1082 * @param task_cls closure of task
1083 * @return unique task identifier for the job
1084 * only valid until "task" is started!
1086 GNUNET_SCHEDULER_TaskIdentifier
1087 GNUNET_SCHEDULER_add_select (struct GNUNET_SCHEDULER_Handle * sched,
1088 enum GNUNET_SCHEDULER_Priority prio,
1089 GNUNET_SCHEDULER_TaskIdentifier
1091 struct GNUNET_TIME_Relative delay,
1092 const struct GNUNET_NETWORK_FDSet * rs,
1093 const struct GNUNET_NETWORK_FDSet * ws,
1094 GNUNET_SCHEDULER_Task task, void *task_cls)
1098 void *backtrace_array[50];
1100 t = GNUNET_malloc (sizeof (struct Task));
1102 t->callback_cls = task_cls;
1104 t->num_backtrace_strings = backtrace(backtrace_array, 50);
1105 t->backtrace_strings = backtrace_symbols(backtrace_array, t->num_backtrace_strings);
1109 t->read_set = GNUNET_NETWORK_fdset_create ();
1110 GNUNET_NETWORK_fdset_copy (t->read_set, rs);
1114 t->write_set = GNUNET_NETWORK_fdset_create ();
1115 GNUNET_NETWORK_fdset_copy (t->write_set, ws);
1117 t->id = ++sched->last_id;
1118 t->prereq_id = prerequisite_task;
1119 t->timeout = GNUNET_TIME_relative_to_absolute (delay);
1121 check_priority ((prio ==
1122 GNUNET_SCHEDULER_PRIORITY_KEEP) ? sched->current_priority
1124 t->next = sched->pending;
1127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1128 "Adding task: %llu / %p\n", t->id, t->callback_cls);
1133 /* end of scheduler.c */