2 This file is part of GNUnet
3 Copyright (C) 2009-2016 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @author Christian Grothoff
25 * API to schedule computations using continuation passing style
27 * @defgroup scheduler Scheduler library
28 * Event loop (scheduler)
30 * Schedule computations using continuation passing style.
35 #ifndef GNUNET_SCHEDULER_LIB_H
36 #define GNUNET_SCHEDULER_LIB_H
41 #if 0 /* keep Emacsens' auto-indent happy */
47 * Opaque reference to a task.
49 struct GNUNET_SCHEDULER_Task;
52 * Reasons why the schedule may have triggered
55 enum GNUNET_SCHEDULER_Reason
58 * This task is not ready.
60 GNUNET_SCHEDULER_REASON_NONE = 0,
63 * This is the very first task run during startup.
65 GNUNET_SCHEDULER_REASON_STARTUP = 1,
68 * We are shutting down and are running all shutdown-related tasks.
70 GNUNET_SCHEDULER_REASON_SHUTDOWN = 2,
73 * The specified timeout has expired.
74 * (also set if the delay given was 0).
76 GNUNET_SCHEDULER_REASON_TIMEOUT = 4,
79 * The reading socket is ready.
81 GNUNET_SCHEDULER_REASON_READ_READY = 8,
84 * The writing socket is ready.
86 GNUNET_SCHEDULER_REASON_WRITE_READY = 16,
89 * The prerequisite task is done.
91 GNUNET_SCHEDULER_REASON_PREREQ_DONE = 32
95 #include "gnunet_time_lib.h"
96 #include "gnunet_network_lib.h"
100 * Possible events on FDs, used as a bitmask.
101 * Modelled after GPollFD.
103 enum GNUNET_SCHEDULER_EventType
107 * No event (useful for timeout).
109 GNUNET_SCHEDULER_ET_NONE = 0,
112 * Data available for reading.
114 GNUNET_SCHEDULER_ET_IN = 1,
117 * Buffer available for writing.
119 GNUNET_SCHEDULER_ET_OUT = 2,
124 GNUNET_SCHEDULER_ET_HUP = 4,
129 GNUNET_SCHEDULER_ET_ERR = 8,
134 GNUNET_SCHEDULER_ET_PRI = 16,
139 GNUNET_SCHEDULER_ET_NVAL = 32
145 * Information about an event relating to a file descriptor/socket.
147 struct GNUNET_SCHEDULER_FdInfo
151 * GNUnet network socket the event is about, matches @a sock,
152 * NULL if this is about a file handle or if no network
153 * handle was given to the scheduler originally.
155 struct GNUNET_NETWORK_Handle *fd;
158 * GNUnet file handle the event is about, matches @a sock,
159 * NULL if this is about a network socket or if no network
160 * handle was given to the scheduler originally.
162 struct GNUNET_DISK_FileHandle *fh;
165 * Type of the event that was generated related to @e sock.
167 enum GNUNET_SCHEDULER_EventType et;
170 * Underlying OS handle the event was about.
178 * Context information passed to each scheduler task.
180 struct GNUNET_SCHEDULER_TaskContext
183 * Reason why the task is run now
185 enum GNUNET_SCHEDULER_Reason reason;
188 * Length of the following array.
190 unsigned int fds_len;
193 * Array of length @e fds_len with information about ready FDs.
194 * Note that we use the same format regardless of the internal
195 * event loop that was used. The given array should only contain
196 * information about file descriptors relevant to the current task.
198 const struct GNUNET_SCHEDULER_FdInfo *fds;
201 * Set of file descriptors ready for reading; note that additional
202 * bits may be set that were not in the original request.
205 const struct GNUNET_NETWORK_FDSet *read_ready;
208 * Set of file descriptors ready for writing; note that additional
209 * bits may be set that were not in the original request.
212 const struct GNUNET_NETWORK_FDSet *write_ready;
218 * Function used by event-loop implementations to signal the scheduler
219 * that a particular @a task is ready due to an event of type @a et.
221 * This function will then queue the task to notify the application
222 * that the task is ready (with the respective priority).
224 * @param task the task that is ready
225 * @param et information about why the task is ready
228 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
229 enum GNUNET_SCHEDULER_EventType et);
233 * Handle to the scheduler's state to be used by the driver.
235 struct GNUNET_SCHEDULER_Handle;
239 * Function called by the driver to tell the scheduler to run some of
240 * the tasks that are ready. This function may return even though
241 * there are tasks left to run just to give other tasks a chance as
242 * well. If we return #GNUNET_YES, the driver should call this
243 * function again as soon as possible, while if we return #GNUNET_NO
244 * it must block until the operating system has more work as the
245 * scheduler has no more work to do right now.
247 * @param sh scheduler handle that was given to the `loop`
248 * @return #GNUNET_OK if there are more tasks that are ready,
249 * and thus we would like to run more (yield to avoid
250 * blocking other activities for too long)
251 * #GNUNET_NO if we are done running tasks (yield to block)
252 * #GNUNET_SYSERR on error
255 GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh);
259 * API a driver has to implement for
260 * #GNUNET_SCHEDULER_run_with_driver().
262 struct GNUNET_SCHEDULER_Driver
266 * Closure to pass to the functions in this struct.
271 * Add a @a task to be run if the conditions given
272 * in @a fdi are satisfied.
275 * @param task task to add
276 * @param fdi conditions to watch for
277 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
278 * (i.e. @a fdi too high or invalid)
282 struct GNUNET_SCHEDULER_Task *task,
283 struct GNUNET_SCHEDULER_FdInfo *fdi);
286 * Delete a @a task from the set of tasks to be run.
289 * @param task task to delete
290 * @param fdi conditions to watch for (must match @e add call)
291 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
292 * (i.e. @a task or @a fdi do not match prior @e add call)
296 struct GNUNET_SCHEDULER_Task *task,
297 const struct GNUNET_SCHEDULER_FdInfo *fdi);
300 * Set time at which we definitively want to get a wakeup call.
303 * @param dt time when we want to wake up next
306 (*set_wakeup)(void *cls,
307 struct GNUNET_TIME_Absolute dt);
310 * Event loop's "main" function, to be called from
311 * #GNUNET_SCHEDULER_run_with_driver() to actually
315 * @param sh scheduler handle to pass to
316 * #GNUNET_SCHEDULER_run_from_driver()
317 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
321 struct GNUNET_SCHEDULER_Handle *sh);
327 * Signature of the main function of a task.
332 (*GNUNET_SCHEDULER_TaskCallback) (void *cls);
336 * Initialize and run scheduler. This function will return when all
337 * tasks have completed. On systems with signals, receiving a SIGTERM
338 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
339 * to be run after the active task is complete. As a result, SIGTERM
340 * causes all shutdown tasks to be scheduled with reason
341 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
342 * afterwards will execute normally!). Note that any particular
343 * signal will only shut down one scheduler; applications should
344 * always only create a single scheduler.
346 * @param driver drive to use for the event loop
347 * @param task task to run first (and immediately)
348 * @param task_cls closure of @a task
349 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
352 GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
353 GNUNET_SCHEDULER_TaskCallback task,
358 * Obtain the driver for using select() as the event loop.
360 * @return NULL on error
362 const struct GNUNET_SCHEDULER_Driver *
363 GNUNET_SCHEDULER_driver_select (void);
367 * Signature of the select function used by the scheduler.
368 * #GNUNET_NETWORK_socket_select matches it.
371 * @param rfds set of sockets to be checked for readability
372 * @param wfds set of sockets to be checked for writability
373 * @param efds set of sockets to be checked for exceptions
374 * @param timeout relative value when to return
375 * @return number of selected sockets, #GNUNET_SYSERR on error
378 (*GNUNET_SCHEDULER_select) (void *cls,
379 struct GNUNET_NETWORK_FDSet *rfds,
380 struct GNUNET_NETWORK_FDSet *wfds,
381 struct GNUNET_NETWORK_FDSet *efds,
382 struct GNUNET_TIME_Relative timeout);
386 * Initialize and run scheduler. This function will return when all
387 * tasks have completed. On systems with signals, receiving a SIGTERM
388 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
389 * to be run after the active task is complete. As a result, SIGTERM
390 * causes all shutdown tasks to be scheduled with reason
391 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
392 * afterwards will execute normally!). Note that any particular
393 * signal will only shut down one scheduler; applications should
394 * always only create a single scheduler.
396 * @param task task to run first (and immediately)
397 * @param task_cls closure of @a task
400 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
404 * Initialize and run scheduler. This function will return when all
405 * tasks have completed. When @ install_signals is GNUNET_YES, then
406 * this function behaves in the same was as GNUNET_SCHEDULER_run does.
407 * If @ install_signals is GNUNET_NO then no signal handlers are
410 * @param install_signals whether to install signals (GNUNET_YES/NO)
411 * @param task task to run first (and immediately)
412 * @param task_cls closure of @a task
415 GNUNET_SCHEDULER_run_with_optional_signals (int install_signals,
416 GNUNET_SCHEDULER_TaskCallback task,
421 * Request the shutdown of a scheduler. Marks all tasks
422 * awaiting shutdown as ready. Note that tasks
423 * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call
424 * will be delayed until the next shutdown signal.
427 GNUNET_SCHEDULER_shutdown (void);
431 * Get information about the current load of this scheduler. Use this
432 * function to determine if an elective task should be added or simply
433 * dropped (if the decision should be made based on the number of
434 * tasks ready to run).
436 * @param p priority-level to query, use KEEP to query the level
437 * of the current task, use COUNT to get the sum over
438 * all priority levels
439 * @return number of tasks pending right now
442 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p);
446 * Obtain the reasoning why the current task was
449 * @return task context with information why the current task is run
451 const struct GNUNET_SCHEDULER_TaskContext *
452 GNUNET_SCHEDULER_get_task_context (void);
456 * Cancel the task with the specified identifier.
457 * The task must not yet have run.
459 * @param task id of the task to cancel
460 * @return the closure of the callback of the cancelled task
463 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task);
467 * Continue the current execution with the given function. This is
468 * similar to the other "add" functions except that there is no delay
469 * and the reason code can be specified.
471 * @param task main function of the task
472 * @param task_cls closure for @a task
473 * @param reason reason for task invocation
474 * @param priority priority to use for the task
477 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
479 enum GNUNET_SCHEDULER_Reason reason,
480 enum GNUNET_SCHEDULER_Priority priority);
484 * Schedule a new task to be run with a specified priority.
486 * @param prio how important is the new task?
487 * @param task main function of the task
488 * @param task_cls closure of @a task
489 * @return unique task identifier for the job
490 * only valid until @a task is started!
492 struct GNUNET_SCHEDULER_Task *
493 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
494 GNUNET_SCHEDULER_TaskCallback task,
499 * Schedule a new task to be run as soon as possible. Note that this
500 * does not guarantee that this will be the next task that is being
501 * run, as other tasks with higher priority (or that are already ready
502 * to run) might get to run first. Just as with delays, clients must
503 * not rely on any particular order of execution between tasks
504 * scheduled concurrently.
506 * The task will be run with the DEFAULT priority.
508 * @param task main function of the task
509 * @param task_cls closure of @a task
510 * @return unique task identifier for the job
511 * only valid until @a task is started!
513 struct GNUNET_SCHEDULER_Task *
514 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
519 * Schedule a new task to be run on shutdown, that is when a CTRL-C
520 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
523 * @param task main function of the task
524 * @param task_cls closure of @a task
525 * @return unique task identifier for the job
526 * only valid until @a task is started!
528 struct GNUNET_SCHEDULER_Task *
529 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
534 * Schedule a new task to be run as soon as possible with the
535 * (transitive) ignore-shutdown flag either explicitly set or
536 * explicitly enabled. This task (and all tasks created from it,
537 * other than by another call to this function) will either count or
538 * not count for the 'lifeness' of the process. This API is only
539 * useful in a few special cases.
541 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
542 * @param task main function of the task
543 * @param task_cls closure of @a task
544 * @return unique task identifier for the job
545 * only valid until @a task is started!
547 struct GNUNET_SCHEDULER_Task *
548 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
549 GNUNET_SCHEDULER_TaskCallback task,
554 * Schedule a new task to be run with a specified delay. The task
555 * will be scheduled for execution once the delay has expired. It
556 * will be run with the DEFAULT priority.
558 * @param delay with which the operation should be run
559 * @param task main function of the task
560 * @param task_cls closure of @a task
561 * @return unique task identifier for the job
562 * only valid until @a task is started!
564 struct GNUNET_SCHEDULER_Task *
565 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
566 GNUNET_SCHEDULER_TaskCallback task,
571 * Schedule a new task to be run at the specified time. The task
572 * will be scheduled for execution once specified time has been
573 * reached. It will be run with the DEFAULT priority.
575 * @param at time at which this operation should run
576 * @param task main function of the task
577 * @param task_cls closure of @a task
578 * @return unique task identifier for the job
579 * only valid until @a task is started!
581 struct GNUNET_SCHEDULER_Task *
582 GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
583 GNUNET_SCHEDULER_TaskCallback task,
588 * Schedule a new task to be run with a specified delay. The task
589 * will be scheduled for execution once the delay has expired.
591 * @param delay when should this operation time out?
592 * @param priority priority to use for the task
593 * @param task main function of the task
594 * @param task_cls closure of @a task
595 * @return unique task identifier for the job
596 * only valid until @a task is started!
598 struct GNUNET_SCHEDULER_Task *
599 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
600 enum GNUNET_SCHEDULER_Priority priority,
601 GNUNET_SCHEDULER_TaskCallback task,
606 * Schedule a new task to be run at the specified time. The task
607 * will be scheduled for execution at time @a at.
609 * @param at time when the operation should run
610 * @param priority priority to use for the task
611 * @param task main function of the task
612 * @param task_cls closure of @a task
613 * @return unique task identifier for the job
614 * only valid until @a task is started!
616 struct GNUNET_SCHEDULER_Task *
617 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
618 enum GNUNET_SCHEDULER_Priority priority,
619 GNUNET_SCHEDULER_TaskCallback task,
624 * Schedule a new task to be run with a specified delay or when the
625 * specified file descriptor is ready for reading. The delay can be
626 * used as a timeout on the socket being ready. The task will be
627 * scheduled for execution once either the delay has expired or the
628 * socket operation is ready. It will be run with the DEFAULT priority.
630 * @param delay when should this operation time out?
631 * @param rfd read file-descriptor
632 * @param task main function of the task
633 * @param task_cls closure of @a task
634 * @return unique task identifier for the job
635 * only valid until @a task is started!
637 struct GNUNET_SCHEDULER_Task *
638 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
639 struct GNUNET_NETWORK_Handle *rfd,
640 GNUNET_SCHEDULER_TaskCallback task,
645 * Schedule a new task to be run with a specified priority and to be
646 * run after the specified delay or when the specified file descriptor
647 * is ready for reading. The delay can be used as a timeout on the
648 * socket being ready. The task will be scheduled for execution once
649 * either the delay has expired or the socket operation is ready. It
650 * will be run with the DEFAULT priority.
652 * @param delay when should this operation time out?
653 * @param priority priority to use for the task
654 * @param rfd read file-descriptor
655 * @param task main function of the task
656 * @param task_cls closure of @a task
657 * @return unique task identifier for the job
658 * only valid until @a task is started!
660 struct GNUNET_SCHEDULER_Task *
661 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
662 enum GNUNET_SCHEDULER_Priority priority,
663 struct GNUNET_NETWORK_Handle *rfd,
664 GNUNET_SCHEDULER_TaskCallback task,
669 * Schedule a new task to be run with a specified delay or when the
670 * specified file descriptor is ready for writing. The delay can be
671 * used as a timeout on the socket being ready. The task will be
672 * scheduled for execution once either the delay has expired or the
673 * socket operation is ready. It will be run with the DEFAULT priority.
675 * * @param delay when should this operation time out?
676 * @param wfd write file-descriptor
677 * @param task main function of the task
678 * @param task_cls closure of @a task
679 * @return unique task identifier for the job
680 * only valid until @a task is started!
682 struct GNUNET_SCHEDULER_Task *
683 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
684 struct GNUNET_NETWORK_Handle *wfd,
685 GNUNET_SCHEDULER_TaskCallback task,
690 * Schedule a new task to be run with a specified delay or when the
691 * specified file descriptor is ready. The delay can be
692 * used as a timeout on the socket being ready. The task will be
693 * scheduled for execution once either the delay has expired or the
694 * socket operation is ready.
696 * @param delay when should this operation time out?
697 * @param priority priority of the task
698 * @param fd file-descriptor
699 * @param on_read whether to poll the file-descriptor for readability
700 * @param on_write whether to poll the file-descriptor for writability
701 * @param task main function of the task
702 * @param task_cls closure of @a task
703 * @return unique task identifier for the job
704 * only valid until "task" is started!
706 struct GNUNET_SCHEDULER_Task *
707 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
708 enum GNUNET_SCHEDULER_Priority priority,
709 struct GNUNET_NETWORK_Handle *fd,
712 GNUNET_SCHEDULER_TaskCallback task,
716 * Schedule a new task to be run with a specified delay or when the
717 * specified file descriptor is ready for reading. The delay can be
718 * used as a timeout on the socket being ready. The task will be
719 * scheduled for execution once either the delay has expired or the
720 * socket operation is ready. It will be run with the DEFAULT priority.
722 * * @param delay when should this operation time out?
723 * @param rfd read file-descriptor
724 * @param task main function of the task
725 * @param task_cls closure of @a task
726 * @return unique task identifier for the job
727 * only valid until "task" is started!
729 struct GNUNET_SCHEDULER_Task *
730 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
731 const struct GNUNET_DISK_FileHandle *rfd,
732 GNUNET_SCHEDULER_TaskCallback task,
737 * Schedule a new task to be run with a specified delay or when the
738 * specified file descriptor is ready for writing. The delay can be
739 * used as a timeout on the socket being ready. The task will be
740 * scheduled for execution once either the delay has expired or the
741 * socket operation is ready. It will be run with the DEFAULT priority.
743 * * @param delay when should this operation time out?
744 * @param wfd write file-descriptor
745 * @param task main function of the task
746 * @param task_cls closure of @a task
747 * @return unique task identifier for the job
748 * only valid until @a task is started!
750 struct GNUNET_SCHEDULER_Task *
751 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
752 const struct GNUNET_DISK_FileHandle *wfd,
753 GNUNET_SCHEDULER_TaskCallback task,
758 * Schedule a new task to be run with a specified delay or when the
759 * specified file descriptor is ready. The delay can be
760 * used as a timeout on the socket being ready. The task will be
761 * scheduled for execution once either the delay has expired or the
762 * socket operation is ready.
764 * @param delay when should this operation time out?
765 * @param priority priority of the task
766 * @param fd file-descriptor
767 * @param on_read whether to poll the file-descriptor for readability
768 * @param on_write whether to poll the file-descriptor for writability
769 * @param task main function of the task
770 * @param task_cls closure of @a task
771 * @return unique task identifier for the job
772 * only valid until @a task is started!
774 struct GNUNET_SCHEDULER_Task *
775 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
776 enum GNUNET_SCHEDULER_Priority priority,
777 const struct GNUNET_DISK_FileHandle *fd,
778 int on_read, int on_write,
779 GNUNET_SCHEDULER_TaskCallback task,
784 * Schedule a new task to be run with a specified delay or when any of
785 * the specified file descriptor sets is ready. The delay can be used
786 * as a timeout on the socket(s) being ready. The task will be
787 * scheduled for execution once either the delay has expired or any of
788 * the socket operations is ready. This is the most general
789 * function of the "add" family. Note that the "prerequisite_task"
790 * must be satisfied in addition to any of the other conditions. In
791 * other words, the task will be started when
799 * @param prio how important is this task?
800 * @param delay how long should we wait?
801 * @param rs set of file descriptors we want to read (can be NULL)
802 * @param ws set of file descriptors we want to write (can be NULL)
803 * @param task main function of the task
804 * @param task_cls closure of @a task
805 * @return unique task identifier for the job
806 * only valid until "task" is started!
808 struct GNUNET_SCHEDULER_Task *
809 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
810 struct GNUNET_TIME_Relative delay,
811 const struct GNUNET_NETWORK_FDSet *rs,
812 const struct GNUNET_NETWORK_FDSet *ws,
813 GNUNET_SCHEDULER_TaskCallback task,
817 * Sets the select function to use in the scheduler (scheduler_select).
819 * @param new_select new select function to use (NULL to reset to default)
820 * @param new_select_cls closure for @a new_select
823 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
824 void *new_select_cls);
827 #if 0 /* keep Emacsens' auto-indent happy */
836 /** @} */ /* end of group scheduler */